Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gui: Fix transaction history table column size behavior #2520

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/qt/bitcoingui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1420,9 +1420,12 @@ void BitcoinGUI::gotoHistoryPage()
historyAction->setChecked(true);
centralWidget->setCurrentWidget(transactionView);

transactionView->resizeTableColumns();

exportAction->setEnabled(true);
disconnect(exportAction, &QAction::triggered, nullptr, nullptr);
connect(exportAction, &QAction::triggered, transactionView, &TransactionView::exportClicked);

}

void BitcoinGUI::gotoAddressBookPage()
Expand Down
2 changes: 2 additions & 0 deletions src/qt/transactiontablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class TransactionTableModel : public QAbstractTableModel
Amount = 4
};

static constexpr std::initializer_list<ColumnIndex> all_ColumnIndex = {Status, Date, Type, ToAddress, Amount};

/** Roles to get specific information from a transaction row.
These are independent of column.
*/
Expand Down
103 changes: 93 additions & 10 deletions src/qt/transactionview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ TransactionView::TransactionView(QWidget *parent)
, transactionProxyModel(nullptr)
, transactionView(nullptr)
, searchWidgetIconAction(new QAction())
, m_table_column_sizes({23, 120, 120, 400, 100})
, m_init_column_sizes_set(false)
, m_resize_columns_in_progress(false)
{
setContentsMargins(0, 0, 0, 0);

Expand Down Expand Up @@ -181,16 +184,8 @@ void TransactionView::setModel(WalletModel *model)
transactionView->sortByColumn(TransactionTableModel::Date, Qt::DescendingOrder);
transactionView->verticalHeader()->hide();

transactionView->horizontalHeader()->resizeSection(
TransactionTableModel::Status, 23);
transactionView->horizontalHeader()->resizeSection(
TransactionTableModel::Date, 120);
transactionView->horizontalHeader()->resizeSection(
TransactionTableModel::Type, 120);
transactionView->horizontalHeader()->setSectionResizeMode(
TransactionTableModel::ToAddress, QHeaderView::Stretch);
transactionView->horizontalHeader()->resizeSection(
TransactionTableModel::Amount, 100);
connect(transactionView->horizontalHeader(), &QHeaderView::sectionResized,
this, &TransactionView::txnViewSectionResized);
}

if (model && model->getOptionsModel()) {
Expand Down Expand Up @@ -454,3 +449,91 @@ void TransactionView::updateIcons(const QString& theme)
{
searchWidgetIconAction->setIcon(QIcon(":/icons/" + theme + "_search"));
}

void TransactionView::resizeTableColumns(const bool& neighbor_pair_adjust, const int& index,
const int& old_size, const int& new_size)
{
// This prevents unwanted recursion to here from txnViewSectionResized.
m_resize_columns_in_progress = true;

if (!model) {
m_resize_columns_in_progress = false;

return;
}

if (!m_init_column_sizes_set) {
for (int i = 0; i < (int) m_table_column_sizes.size(); ++i) {
transactionView->horizontalHeader()->resizeSection(i, m_table_column_sizes[i]);
}

m_init_column_sizes_set = true;
m_resize_columns_in_progress = false;

return;
}

if (neighbor_pair_adjust) {
if (index != TransactionTableModel::all_ColumnIndex.size() - 1) {
int new_neighbor_section_size = transactionView->horizontalHeader()->sectionSize(index + 1)
+ old_size - new_size;

transactionView->horizontalHeader()->resizeSection(
index + 1, new_neighbor_section_size);

// This detects and deals with the case where the resize of a column tries to force the neighbor
// to a size below its minimum, in which case we have to reverse out the attempt.
if (transactionView->horizontalHeader()->sectionSize(index + 1)
!= new_neighbor_section_size) {
transactionView->horizontalHeader()->resizeSection(
index,
transactionView->horizontalHeader()->sectionSize(index)
+ new_neighbor_section_size
- transactionView->horizontalHeader()->sectionSize(index + 1));
}
} else {
// Do not allow the last column to be resized because there is no adjoining neighbor to the right
// and we are maintaining the total width fixed to the size of the containing frame.
transactionView->horizontalHeader()->resizeSection(index, old_size);
}

m_resize_columns_in_progress = false;

return;
}

// This is the proportional resize case when the window is resized or the history icon button is pressed.
const int width = transactionView->horizontalHeader()->width() - 5;

int orig_header_width = 0;

for (const auto& iter : TransactionTableModel::all_ColumnIndex) {
orig_header_width += transactionView->horizontalHeader()->sectionSize(iter);
}

if (!width || !orig_header_width) return;

for (const auto& iter : TransactionTableModel::all_ColumnIndex) {
int section_size = transactionView->horizontalHeader()->sectionSize(iter);

transactionView->horizontalHeader()->resizeSection(
iter, section_size * width / orig_header_width);
}

m_resize_columns_in_progress = false;
}

void TransactionView::resizeEvent(QResizeEvent *event)
{
resizeTableColumns();

QWidget::resizeEvent(event);
}

void TransactionView::txnViewSectionResized(int index, int old_size, int new_size)
{
// Avoid implicit recursion between resizeTableColumns and txnViewSectionResized
if (m_resize_columns_in_progress) return;

resizeTableColumns(true, index, old_size, new_size);
}
12 changes: 11 additions & 1 deletion src/qt/transactionview.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class QModelIndex;
class QMenu;
class QFrame;
class QDateTimeEdit;

QT_END_NAMESPACE

/** Widget showing the transaction list for a wallet, including a filter row.
Expand All @@ -39,6 +40,9 @@ class TransactionView : public QFrame
Range
};

protected:
void resizeEvent(QResizeEvent *event) override;

private:
WalletModel *model;
TransactionFilterProxy *transactionProxyModel;
Expand All @@ -58,6 +62,10 @@ class TransactionView : public QFrame

QWidget *createDateRangeWidget();

std::vector<int> m_table_column_sizes;
bool m_init_column_sizes_set;
bool m_resize_columns_in_progress;

private slots:
void contextualMenu(const QPoint &);
void dateRangeChanged();
Expand All @@ -67,6 +75,7 @@ private slots:
void copyAmount();
void copyTxID();
void updateIcons(const QString& theme);
void txnViewSectionResized(int index, int old_size, int new_size);

signals:
void doubleClicked(const QModelIndex&);
Expand All @@ -79,7 +88,8 @@ public slots:
void changedAmount(const QString &amount);
void exportClicked();
void focusTransaction(const QModelIndex&);

void resizeTableColumns(const bool& neighbor_pair_adjust = false, const int& index = 0,
const int& old_size = 0, const int& new_size = 0);
};

#endif // BITCOIN_QT_TRANSACTIONVIEW_H