Skip to content

Commit a70df08

Browse files
committed
[Qt] Issue #2862 Fixed: Receive Tab > 'Amount' table column could not be resized.
1 parent 15ec451 commit a70df08

File tree

2 files changed

+103
-9
lines changed

2 files changed

+103
-9
lines changed

src/qt/receivecoinsdialog.cpp

Lines changed: 86 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,29 @@ ReceiveCoinsDialog::ReceiveCoinsDialog(QWidget *parent) :
5454
connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(clear()));
5555
}
5656

57+
//recent request table's header signals
58+
void ReceiveCoinsDialog::connectRecentRequestsViewHeadersSignals()
59+
{
60+
connect(ui->recentRequestsView->horizontalHeader(),SIGNAL(sectionResized(int,int,int)),this,SLOT(on_sectionResized()));
61+
connect(ui->recentRequestsView->horizontalHeader(),SIGNAL(geometriesChanged()),this,SLOT(on_geometriesChanged()));
62+
}
63+
64+
//we need to disconnect these while handling the resize events, otherwise we can enter an infinite loop
65+
void ReceiveCoinsDialog::disconnectRecentRequestsViewHeadersSignals()
66+
{
67+
disconnect(ui->recentRequestsView->horizontalHeader(),SIGNAL(sectionResized(int,int,int)),this,SLOT(on_sectionResized()));
68+
disconnect(ui->recentRequestsView->horizontalHeader(),SIGNAL(geometriesChanged()),this,SLOT(on_geometriesChanged()));
69+
}
70+
71+
//setup the resize mode, handles compatibility for QT5 and below as the method signatures changed. (refactored here for readability)
72+
void ReceiveCoinsDialog::setRecentRequestViewHeaderResizeMode(int logicalIndex, QHeaderView::ResizeMode resizeMode) {
73+
#if QT_VERSION < 0x050000
74+
ui->recentRequestsView->horizontalHeader()->setResizeMode(logicalIndex, resizeMode);
75+
#else
76+
ui->recentRequestsView->horizontalHeader()->setSectionResizeMode(logicalIndex, resizeMode);
77+
#endif
78+
}
79+
5780
void ReceiveCoinsDialog::setModel(WalletModel *model)
5881
{
5982
this->model = model;
@@ -63,20 +86,23 @@ void ReceiveCoinsDialog::setModel(WalletModel *model)
6386
connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
6487
updateDisplayUnit();
6588

89+
ui->recentRequestsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
6690
ui->recentRequestsView->setModel(model->getRecentRequestsTableModel());
6791
ui->recentRequestsView->setAlternatingRowColors(true);
6892
ui->recentRequestsView->setSelectionBehavior(QAbstractItemView::SelectRows);
6993
ui->recentRequestsView->setSelectionMode(QAbstractItemView::ContiguousSelection);
70-
ui->recentRequestsView->horizontalHeader()->resizeSection(RecentRequestsTableModel::Date, 130);
71-
ui->recentRequestsView->horizontalHeader()->resizeSection(RecentRequestsTableModel::Label, 120);
72-
#if QT_VERSION < 0x050000
73-
ui->recentRequestsView->horizontalHeader()->setResizeMode(RecentRequestsTableModel::Message, QHeaderView::Stretch);
74-
#else
75-
ui->recentRequestsView->horizontalHeader()->setSectionResizeMode(RecentRequestsTableModel::Message, QHeaderView::Stretch);
76-
#endif
77-
ui->recentRequestsView->horizontalHeader()->resizeSection(RecentRequestsTableModel::Amount, 100);
94+
ui->recentRequestsView->setColumnWidth(RecentRequestsTableModel::Date, DATE_COLUMN_WIDTH);
95+
ui->recentRequestsView->setColumnWidth(RecentRequestsTableModel::Label, LABEL_COLUMN_WIDTH);
96+
//(last 2 columns are set when the table geometry is ready)
97+
98+
setRecentRequestViewHeaderResizeMode(RecentRequestsTableModel::Message, QHeaderView::Interactive);
99+
setRecentRequestViewHeaderResizeMode(RecentRequestsTableModel::Amount, QHeaderView::Fixed);
100+
101+
//this honors a minimum size while resizing for all columns. Wish they had a method for a single column.
102+
ui->recentRequestsView->horizontalHeader()->setMinimumSectionSize(MINIMUM_COLUMN_WIDTH);
78103

79104
model->getRecentRequestsTableModel()->sort(RecentRequestsTableModel::Date, Qt::DescendingOrder);
105+
connectRecentRequestsViewHeadersSignals(); //now we're ready to handle events on the table header.
80106
}
81107
}
82108

@@ -185,6 +211,58 @@ void ReceiveCoinsDialog::on_removeRequestButton_clicked()
185211
model->getRecentRequestsTableModel()->removeRows(firstIndex.row(), selection.length(), firstIndex.parent());
186212
}
187213

214+
//given a column index, returns the maximum allowable width by substracting the other column's widths from the table's width
215+
//NOTE: works only for the two columns we care for: The last ("Amount"), and second to last ("Message").
216+
// if given another column number, it will always return the remaining width for the last column ("Amount")
217+
int ReceiveCoinsDialog::getRemainingWidthForColumn(int column)
218+
{
219+
int nResult = AMOUNT_MINIMUM_COLUMN_WIDTH;
220+
221+
QTableView* recentRequestsTableView = ui->recentRequestsView;
222+
QHeaderView* headerView = recentRequestsTableView->horizontalHeader();
223+
int nTableWidth = recentRequestsTableView->width();
224+
225+
if (nTableWidth > 0)
226+
{
227+
int nOtherColsWidth = headerView->sectionSize(RecentRequestsTableModel::Date);
228+
nOtherColsWidth += headerView->sectionSize(RecentRequestsTableModel::Label);
229+
nOtherColsWidth += headerView->sectionSize(column == RecentRequestsTableModel::Amount ? RecentRequestsTableModel::Message : RecentRequestsTableModel::Amount);
230+
nResult = std::max(nResult, nTableWidth - nOtherColsWidth);
231+
}
232+
233+
return nResult;
234+
}
235+
236+
237+
//make sure we don't make the columns wider than the table's viewport's width.
238+
void ReceiveCoinsDialog::adjustRecentRequestsColumnsWidth()
239+
{
240+
disconnectRecentRequestsViewHeadersSignals();
241+
ui->recentRequestsView->setColumnWidth(RecentRequestsTableModel::Amount, getRemainingWidthForColumn(RecentRequestsTableModel::Amount));
242+
ui->recentRequestsView->setColumnWidth(RecentRequestsTableModel::Message, getRemainingWidthForColumn(RecentRequestsTableModel::Message));
243+
connectRecentRequestsViewHeadersSignals();
244+
}
245+
246+
//when a section is resized this is a slot-proxy for ajustAmountColumnWidth()
247+
void ReceiveCoinsDialog::on_sectionResized()
248+
{
249+
adjustRecentRequestsColumnsWidth();
250+
}
251+
252+
//when the table's geometry is ready, we manually perform the Stretch of the "Message" column
253+
//as the "Stretch" resize mode does not allow for interactive resizing.
254+
void ReceiveCoinsDialog::on_geometriesChanged()
255+
{
256+
ui->recentRequestsView->setColumnWidth(RecentRequestsTableModel::Amount, AMOUNT_MINIMUM_COLUMN_WIDTH);
257+
ui->recentRequestsView->setColumnWidth(RecentRequestsTableModel::Message, getRemainingWidthForColumn(RecentRequestsTableModel::Message));
258+
}
259+
260+
//We override the virtual resizeEvent of the QWidget to adjust tablet's column sizes as the table's width is proportional to the dialog's.
261+
void ReceiveCoinsDialog::resizeEvent(QResizeEvent* event) {
262+
QWidget::resizeEvent(event);
263+
adjustRecentRequestsColumnsWidth();
264+
}
265+
188266
void ReceiveCoinsDialog::keyPressEvent(QKeyEvent *event)
189267
{
190268
if (event->key() == Qt::Key_Return)

src/qt/receivecoinsdialog.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <QMenu>
1111
#include <QPoint>
1212
#include <QVariant>
13+
#include <QHeaderView>
1314

1415
namespace Ui {
1516
class ReceiveCoinsDialog;
@@ -27,11 +28,18 @@ class ReceiveCoinsDialog : public QDialog
2728
Q_OBJECT
2829

2930
public:
31+
enum ColumnWidths {
32+
DATE_COLUMN_WIDTH = 130,
33+
LABEL_COLUMN_WIDTH = 120,
34+
AMOUNT_MINIMUM_COLUMN_WIDTH = 160,
35+
MINIMUM_COLUMN_WIDTH = 130
36+
};
37+
3038
explicit ReceiveCoinsDialog(QWidget *parent = 0);
3139
~ReceiveCoinsDialog();
32-
3340
void setModel(WalletModel *model);
3441

42+
3543
public slots:
3644
void clear();
3745
void reject();
@@ -45,12 +53,20 @@ public slots:
4553
WalletModel *model;
4654
QMenu *contextMenu;
4755
void copyColumnToClipboard(int column);
56+
int getRemainingWidthForColumn(int column);
57+
void adjustRecentRequestsColumnsWidth();
58+
void connectRecentRequestsViewHeadersSignals();
59+
void disconnectRecentRequestsViewHeadersSignals();
60+
void setRecentRequestViewHeaderResizeMode(int logicalIndex, QHeaderView::ResizeMode resizeMode);
61+
virtual void resizeEvent(QResizeEvent* event);
4862

4963
private slots:
5064
void on_receiveButton_clicked();
5165
void on_showRequestButton_clicked();
5266
void on_removeRequestButton_clicked();
5367
void on_recentRequestsView_doubleClicked(const QModelIndex &index);
68+
void on_sectionResized();
69+
void on_geometriesChanged();
5470
void updateDisplayUnit();
5571
void showMenu(const QPoint &);
5672
void copyLabel();

0 commit comments

Comments
 (0)