Skip to content

Commit 8f5b089

Browse files
committed
Implement proportional column resizing for Addressbook with memory
This is similar to gridcoin-community#2520.
1 parent 0601e82 commit 8f5b089

8 files changed

+131
-5
lines changed

src/qt/addressbookpage.cpp

+94-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ AddressBookPage::AddressBookPage(Mode mode, Tabs tab, QWidget* parent)
2626
, optionsModel(nullptr)
2727
, mode(mode)
2828
, tab(tab)
29+
, m_table_column_sizes({150, 100})
30+
, m_init_column_sizes_set(false)
31+
, m_resize_columns_in_progress(false)
2932
{
3033
ui->setupUi(this);
3134

@@ -102,6 +105,9 @@ AddressBookPage::AddressBookPage(Mode mode, Tabs tab, QWidget* parent)
102105

103106
// Pass through accept action from button box
104107
connect(ui->okayButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
108+
109+
connect(ui->tableView->horizontalHeader(), &QHeaderView::sectionResized,
110+
this, &AddressBookPage::addressBookSectionResized);
105111
}
106112

107113
AddressBookPage::~AddressBookPage()
@@ -144,11 +150,6 @@ void AddressBookPage::setModel(AddressTableModel *model)
144150
ui->tableView->setModel(filterProxyModel);
145151
ui->tableView->sortByColumn(0, Qt::AscendingOrder);
146152

147-
// Set column widths
148-
ui->tableView->horizontalHeader()->setSectionResizeMode(AddressTableModel::Label, QHeaderView::Stretch);
149-
ui->tableView->horizontalHeader()->setSectionResizeMode(AddressTableModel::Address, QHeaderView::ResizeToContents);
150-
ui->tableView->horizontalHeader()->resizeSection(AddressTableModel::Address, 320);
151-
152153
connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged,
153154
this, &AddressBookPage::selectionChanged);
154155

@@ -388,3 +389,91 @@ void AddressBookPage::selectNewAddress(const QModelIndex &parent, int begin, int
388389
newAddressToSelect.clear();
389390
}
390391
}
392+
393+
void AddressBookPage::resizeTableColumns(const bool& neighbor_pair_adjust, const int& index,
394+
const int& old_size, const int& new_size)
395+
{
396+
// This prevents unwanted recursion to here from addressBookSectionResized.
397+
m_resize_columns_in_progress = true;
398+
399+
if (!model) {
400+
m_resize_columns_in_progress = false;
401+
402+
return;
403+
}
404+
405+
if (!m_init_column_sizes_set) {
406+
for (int i = 0; i < (int) m_table_column_sizes.size(); ++i) {
407+
ui->tableView->horizontalHeader()->resizeSection(i, m_table_column_sizes[i]);
408+
}
409+
410+
m_init_column_sizes_set = true;
411+
m_resize_columns_in_progress = false;
412+
413+
return;
414+
}
415+
416+
if (neighbor_pair_adjust) {
417+
if (index != AddressTableModel::all_ColumnIndex.size() - 1) {
418+
int new_neighbor_section_size = ui->tableView->horizontalHeader()->sectionSize(index + 1)
419+
+ old_size - new_size;
420+
421+
ui->tableView->horizontalHeader()->resizeSection(
422+
index + 1, new_neighbor_section_size);
423+
424+
// This detects and deals with the case where the resize of a column tries to force the neighbor
425+
// to a size below its minimum, in which case we have to reverse out the attempt.
426+
if (ui->tableView->horizontalHeader()->sectionSize(index + 1)
427+
!= new_neighbor_section_size) {
428+
ui->tableView->horizontalHeader()->resizeSection(
429+
index,
430+
ui->tableView->horizontalHeader()->sectionSize(index)
431+
+ new_neighbor_section_size
432+
- ui->tableView->horizontalHeader()->sectionSize(index + 1));
433+
}
434+
} else {
435+
// Do not allow the last column to be resized because there is no adjoining neighbor to the right
436+
// and we are maintaining the total width fixed to the size of the containing frame.
437+
ui->tableView->horizontalHeader()->resizeSection(index, old_size);
438+
}
439+
440+
m_resize_columns_in_progress = false;
441+
442+
return;
443+
}
444+
445+
// This is the proportional resize case when the window is resized or the receive or favorites icon button is pressed.
446+
const int width = ui->tableView->horizontalHeader()->width() - 5;
447+
448+
int orig_header_width = 0;
449+
450+
for (const auto& iter : AddressTableModel::all_ColumnIndex) {
451+
orig_header_width += ui->tableView->horizontalHeader()->sectionSize(iter);
452+
}
453+
454+
if (!width || !orig_header_width) return;
455+
456+
for (const auto& iter : AddressTableModel::all_ColumnIndex) {
457+
int section_size = ui->tableView->horizontalHeader()->sectionSize(iter);
458+
459+
ui->tableView->horizontalHeader()->resizeSection(
460+
iter, section_size * width / orig_header_width);
461+
}
462+
463+
m_resize_columns_in_progress = false;
464+
}
465+
466+
void AddressBookPage::resizeEvent(QResizeEvent *event)
467+
{
468+
resizeTableColumns();
469+
470+
QWidget::resizeEvent(event);
471+
}
472+
473+
void AddressBookPage::addressBookSectionResized(int index, int old_size, int new_size)
474+
{
475+
// Avoid implicit recursion between resizeTableColumns and addressBookSectionResized
476+
if (m_resize_columns_in_progress) return;
477+
478+
resizeTableColumns(true, index, old_size, new_size);
479+
}

src/qt/addressbookpage.h

+12
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public slots:
4545
void done(int retval);
4646
void exportClicked();
4747
void changeFilter(const QString& needle);
48+
void resizeTableColumns(const bool& neighbor_pair_adjust = false, const int& index = 0,
49+
const int& old_size = 0, const int& new_size = 0);
50+
51+
protected:
52+
void resizeEvent(QResizeEvent *event) override;
4853

4954
private:
5055
Ui::AddressBookPage *ui;
@@ -59,6 +64,10 @@ public slots:
5964
QAction *deleteAction;
6065
QString newAddressToSelect;
6166

67+
std::vector<int> m_table_column_sizes;
68+
bool m_init_column_sizes_set;
69+
bool m_resize_columns_in_progress;
70+
6271
private slots:
6372
void on_deleteButton_clicked();
6473
void on_newAddressButton_clicked();
@@ -79,6 +88,9 @@ private slots:
7988
/** New entry/entries were added to address table */
8089
void selectNewAddress(const QModelIndex &parent, int begin, int end);
8190

91+
/** Resize address book table columns based on incoming signal */
92+
void addressBookSectionResized(int index, int old_size, int new_size);
93+
8294
signals:
8395
void signMessage(QString addr);
8496
void verifyMessage(QString addr);

src/qt/addresstablemodel.h

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class AddressTableModel : public QAbstractTableModel
2525
Address = 1 /**< Bitcoin address */
2626
};
2727

28+
static constexpr std::initializer_list<ColumnIndex> all_ColumnIndex = {Label, Address};
29+
2830
enum RoleIndex {
2931
TypeRole = Qt::UserRole /**< Type of address (#Send or #Receive) */
3032
};

src/qt/bitcoingui.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,8 @@ void BitcoinGUI::gotoAddressBookPage()
14331433
addressBookAction->setChecked(true);
14341434
centralWidget->setCurrentWidget(addressBookPage);
14351435

1436+
addressBookPage->resizeTableColumns();
1437+
14361438
exportAction->setEnabled(true);
14371439
disconnect(exportAction, &QAction::triggered, nullptr, nullptr);
14381440
connect(exportAction, &QAction::triggered, addressBookPage, &FavoritesPage::exportClicked);
@@ -1443,6 +1445,8 @@ void BitcoinGUI::gotoReceiveCoinsPage()
14431445
receiveCoinsAction->setChecked(true);
14441446
centralWidget->setCurrentWidget(receiveCoinsPage);
14451447

1448+
receiveCoinsPage->resizeTableColumns();
1449+
14461450
exportAction->setEnabled(true);
14471451
disconnect(exportAction, &QAction::triggered, nullptr, nullptr);
14481452
connect(exportAction, &QAction::triggered, receiveCoinsPage, &ReceiveCoinsPage::exportClicked);

src/qt/favoritespage.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,10 @@ void FavoritesPage::updateIcons(const QString& theme)
6262
{
6363
filterLineEditIconAction->setIcon(QIcon(":/icons/" + theme + "_search"));
6464
}
65+
66+
void FavoritesPage::resizeTableColumns()
67+
{
68+
if (addressBookPage) {
69+
addressBookPage->resizeTableColumns();
70+
}
71+
}

src/qt/favoritespage.h

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class FavoritesPage : public QWidget
3131
void setAddressTableModel(AddressTableModel* model);
3232
void setOptionsModel(OptionsModel* model);
3333

34+
void resizeTableColumns();
35+
3436
private:
3537
Ui::FavoritesPage* ui;
3638
AddressBookPage* addressBookPage;

src/qt/receivecoinspage.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,11 @@ void ReceiveCoinsPage::updateIcons(const QString& theme)
6262
{
6363
filterLineEditIconAction->setIcon(QIcon(":/icons/" + theme + "_search"));
6464
}
65+
66+
67+
void ReceiveCoinsPage::resizeTableColumns()
68+
{
69+
if (addressBookPage) {
70+
addressBookPage->resizeTableColumns();
71+
}
72+
}

src/qt/receivecoinspage.h

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class ReceiveCoinsPage : public QWidget
3333
void setAddressTableModel(AddressTableModel *model);
3434
void setOptionsModel(OptionsModel *model);
3535

36+
void resizeTableColumns();
37+
3638
private:
3739
Ui::ReceiveCoinsPage *ui;
3840
AddressBookPage *addressBookPage;

0 commit comments

Comments
 (0)