@@ -26,6 +26,9 @@ AddressBookPage::AddressBookPage(Mode mode, Tabs tab, QWidget* parent)
26
26
, optionsModel(nullptr )
27
27
, mode(mode)
28
28
, tab(tab)
29
+ , m_table_column_sizes({150 , 100 })
30
+ , m_init_column_sizes_set(false )
31
+ , m_resize_columns_in_progress(false )
29
32
{
30
33
ui->setupUi (this );
31
34
@@ -102,6 +105,9 @@ AddressBookPage::AddressBookPage(Mode mode, Tabs tab, QWidget* parent)
102
105
103
106
// Pass through accept action from button box
104
107
connect (ui->okayButtonBox , &QDialogButtonBox::accepted, this , &QDialog::accept);
108
+
109
+ connect (ui->tableView ->horizontalHeader (), &QHeaderView::sectionResized,
110
+ this , &AddressBookPage::addressBookSectionResized);
105
111
}
106
112
107
113
AddressBookPage::~AddressBookPage ()
@@ -144,11 +150,6 @@ void AddressBookPage::setModel(AddressTableModel *model)
144
150
ui->tableView ->setModel (filterProxyModel);
145
151
ui->tableView ->sortByColumn (0 , Qt::AscendingOrder);
146
152
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
-
152
153
connect (ui->tableView ->selectionModel (), &QItemSelectionModel::selectionChanged,
153
154
this , &AddressBookPage::selectionChanged);
154
155
@@ -388,3 +389,91 @@ void AddressBookPage::selectNewAddress(const QModelIndex &parent, int begin, int
388
389
newAddressToSelect.clear ();
389
390
}
390
391
}
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
+ }
0 commit comments