@@ -37,6 +37,9 @@ TransactionView::TransactionView(QWidget *parent)
37
37
, transactionProxyModel(nullptr )
38
38
, transactionView(nullptr )
39
39
, searchWidgetIconAction(new QAction())
40
+ , m_table_column_sizes({23 , 120 , 120 , 400 , 100 })
41
+ , m_init_column_sizes_set(false )
42
+ , m_resize_columns_in_progress(false )
40
43
{
41
44
setContentsMargins (0 , 0 , 0 , 0 );
42
45
@@ -181,16 +184,8 @@ void TransactionView::setModel(WalletModel *model)
181
184
transactionView->sortByColumn (TransactionTableModel::Date, Qt::DescendingOrder);
182
185
transactionView->verticalHeader ()->hide ();
183
186
184
- transactionView->horizontalHeader ()->resizeSection (
185
- TransactionTableModel::Status, 23 );
186
- transactionView->horizontalHeader ()->resizeSection (
187
- TransactionTableModel::Date, 120 );
188
- transactionView->horizontalHeader ()->resizeSection (
189
- TransactionTableModel::Type, 120 );
190
- transactionView->horizontalHeader ()->setSectionResizeMode (
191
- TransactionTableModel::ToAddress, QHeaderView::Stretch);
192
- transactionView->horizontalHeader ()->resizeSection (
193
- TransactionTableModel::Amount, 100 );
187
+ connect (transactionView->horizontalHeader (), &QHeaderView::sectionResized,
188
+ this , &TransactionView::txnViewSectionResized);
194
189
}
195
190
196
191
if (model && model->getOptionsModel ()) {
@@ -454,3 +449,91 @@ void TransactionView::updateIcons(const QString& theme)
454
449
{
455
450
searchWidgetIconAction->setIcon (QIcon (" :/icons/" + theme + " _search" ));
456
451
}
452
+
453
+ void TransactionView::resizeTableColumns (const bool & neighbor_pair_adjust, const int & index,
454
+ const int & old_size, const int & new_size)
455
+ {
456
+ // This prevents unwanted recursion to here from txnViewSectionResized.
457
+ m_resize_columns_in_progress = true ;
458
+
459
+ if (!model) {
460
+ m_resize_columns_in_progress = false ;
461
+
462
+ return ;
463
+ }
464
+
465
+ if (!m_init_column_sizes_set) {
466
+ for (int i = 0 ; i < (int ) m_table_column_sizes.size (); ++i) {
467
+ transactionView->horizontalHeader ()->resizeSection (i, m_table_column_sizes[i]);
468
+ }
469
+
470
+ m_init_column_sizes_set = true ;
471
+ m_resize_columns_in_progress = false ;
472
+
473
+ return ;
474
+ }
475
+
476
+ if (neighbor_pair_adjust) {
477
+ if (index != TransactionTableModel::all_ColumnIndex.size () - 1 ) {
478
+ int new_neighbor_section_size = transactionView->horizontalHeader ()->sectionSize (index + 1 )
479
+ + old_size - new_size;
480
+
481
+ transactionView->horizontalHeader ()->resizeSection (
482
+ index + 1 , new_neighbor_section_size);
483
+
484
+ // This detects and deals with the case where the resize of a column tries to force the neighbor
485
+ // to a size below its minimum, in which case we have to reverse out the attempt.
486
+ if (transactionView->horizontalHeader ()->sectionSize (index + 1 )
487
+ != new_neighbor_section_size) {
488
+ transactionView->horizontalHeader ()->resizeSection (
489
+ index ,
490
+ transactionView->horizontalHeader ()->sectionSize (index )
491
+ + new_neighbor_section_size
492
+ - transactionView->horizontalHeader ()->sectionSize (index + 1 ));
493
+ }
494
+ } else {
495
+ // Do not allow the last column to be resized because there is no adjoining neighbor to the right
496
+ // and we are maintaining the total width fixed to the size of the containing frame.
497
+ transactionView->horizontalHeader ()->resizeSection (index , old_size);
498
+ }
499
+
500
+ m_resize_columns_in_progress = false ;
501
+
502
+ return ;
503
+ }
504
+
505
+ // This is the proportional resize case when the window is resized or the history icon button is pressed.
506
+ const int width = transactionView->horizontalHeader ()->width () - 5 ;
507
+
508
+ int orig_header_width = 0 ;
509
+
510
+ for (const auto & iter : TransactionTableModel::all_ColumnIndex) {
511
+ orig_header_width += transactionView->horizontalHeader ()->sectionSize (iter);
512
+ }
513
+
514
+ if (!width || !orig_header_width) return ;
515
+
516
+ for (const auto & iter : TransactionTableModel::all_ColumnIndex) {
517
+ int section_size = transactionView->horizontalHeader ()->sectionSize (iter);
518
+
519
+ transactionView->horizontalHeader ()->resizeSection (
520
+ iter, section_size * width / orig_header_width);
521
+ }
522
+
523
+ m_resize_columns_in_progress = false ;
524
+ }
525
+
526
+ void TransactionView::resizeEvent (QResizeEvent *event)
527
+ {
528
+ resizeTableColumns ();
529
+
530
+ QWidget::resizeEvent (event);
531
+ }
532
+
533
+ void TransactionView::txnViewSectionResized (int index, int old_size, int new_size)
534
+ {
535
+ // Avoid implicit recursion between resizeTableColumns and txnViewSectionResized
536
+ if (m_resize_columns_in_progress) return ;
537
+
538
+ resizeTableColumns (true , index , old_size, new_size);
539
+ }
0 commit comments