Skip to content

Commit

Permalink
Merge pull request #1305 from firoorg/columns_resize
Browse files Browse the repository at this point in the history
Columns resize
  • Loading branch information
levonpetrosyan93 authored Aug 12, 2023
2 parents 9ffd44b + 8582fa4 commit e36b3ac
Show file tree
Hide file tree
Showing 14 changed files with 13 additions and 253 deletions.
13 changes: 0 additions & 13 deletions src/qt/createpcodedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
CreatePcodeDialog::CreatePcodeDialog(const PlatformStyle *_platformStyle, QWidget *parent) :
QDialog(parent),
ui(new Ui::CreatePcodeDialog),
columnResizingFixer(0),
model(0),
platformStyle(_platformStyle)
{
Expand Down Expand Up @@ -81,12 +80,8 @@ void CreatePcodeDialog::setModel(WalletModel *_model)
tableView->setColumnWidth(static_cast<int>(PcodeModel::ColumnIndex::Number), static_cast<int>(ColumnWidths::Number));
tableView->setColumnWidth(static_cast<int>(PcodeModel::ColumnIndex::Pcode), static_cast<int>(ColumnWidths::Pcode));
tableView->setItemDelegateForColumn(int(PcodeModel::ColumnIndex::Pcode), new GUIUtil::TextElideStyledItemDelegate(tableView));

connect(tableView->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &CreatePcodeDialog::pcodesView_selectionChanged);
// Last 2 columns are set by the columnResizingFixer, when the table geometry is ready.
columnResizingFixer = new GUIUtil::TableViewLastColumnResizingFixer(tableView, 70, 70, this, int(PcodeModel::ColumnIndex::Pcode));
columnResizingFixer->stretchColumnWidth(int(PcodeModel::ColumnIndex::Pcode));

ui->createPcodeButton->setEnabled(false);
ui->statusLabel->setText(tr("The label should not be empty."));
Expand Down Expand Up @@ -162,14 +157,6 @@ void CreatePcodeDialog::pcodesView_selectionChanged(QItemSelection const & selec
ui->showPcodeButton->setEnabled(enable);
}

// We override the virtual resizeEvent of the QWidget to adjust tables column
// sizes as the tables width is proportional to the dialogs width.
void CreatePcodeDialog::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
columnResizingFixer->stretchColumnWidth(int(PcodeModel::ColumnIndex::Pcode));
}

void CreatePcodeDialog::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Return)
Expand Down
2 changes: 0 additions & 2 deletions src/qt/createpcodedialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,13 @@ public Q_SLOTS:

private:
Ui::CreatePcodeDialog *ui;
GUIUtil::TableViewLastColumnResizingFixer *columnResizingFixer;
WalletModel *model;
QMenu *contextMenu;
const PlatformStyle *platformStyle;
SendCoinsRecipient recipient;

QModelIndex selectedRow();
void copyColumnToClipboard(int column);
virtual void resizeEvent(QResizeEvent *event);

private Q_SLOTS:
void on_createPcodeButton_clicked();
Expand Down
11 changes: 1 addition & 10 deletions src/qt/elyassetsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ ElyAssetsDialog::ElyAssetsDialog(QWidget *parent) :
ui->balancesTable->setHorizontalHeaderItem(1, new QTableWidgetItem("Property Name"));
ui->balancesTable->setHorizontalHeaderItem(2, new QTableWidgetItem("Reserved"));
ui->balancesTable->setHorizontalHeaderItem(3, new QTableWidgetItem("Available"));
borrowedColumnResizingFixer = new GUIUtil::TableViewLastColumnResizingFixer(ui->balancesTable, 100, 100, this);

// note neither resizetocontents or stretch allow user to adjust - go interactive then manually set widths
#if QT_VERSION < 0x050000
ui->balancesTable->horizontalHeader()->setResizeMode(0, QHeaderView::Interactive);
Expand All @@ -73,7 +73,6 @@ ElyAssetsDialog::ElyAssetsDialog(QWidget *parent) :
ui->balancesTable->resizeColumnToContents(0);
ui->balancesTable->resizeColumnToContents(2);
ui->balancesTable->resizeColumnToContents(3);
borrowedColumnResizingFixer->stretchColumnWidth(1);
ui->balancesTable->verticalHeader()->setVisible(false);
ui->balancesTable->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->balancesTable->setSelectionBehavior(QAbstractItemView::SelectRows);
Expand Down Expand Up @@ -302,11 +301,3 @@ void ElyAssetsDialog::balancesUpdated()
UpdatePropSelector();
propSelectorChanged(); // refresh the table with the currently selected property ID
}

// We override the virtual resizeEvent of the QWidget to adjust tables column
// sizes as the tables width is proportional to the dialogs width.
void ElyAssetsDialog::resizeEvent(QResizeEvent* event)
{
QWidget::resizeEvent(event);
borrowedColumnResizingFixer->stretchColumnWidth(1);
}
3 changes: 0 additions & 3 deletions src/qt/elyassetsdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ class ElyAssetsDialog : public QDialog
QMenu *contextMenu;
QMenu *contextMenuSummary;

GUIUtil::TableViewLastColumnResizingFixer *borrowedColumnResizingFixer;
virtual void resizeEvent(QResizeEvent *event);

public Q_SLOTS:
void propSelectorChanged();
void balancesUpdated();
Expand Down
124 changes: 0 additions & 124 deletions src/qt/guiutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,130 +488,6 @@ bool ToolTipToRichTextFilter::eventFilter(QObject *obj, QEvent *evt)
return QObject::eventFilter(obj, evt);
}

void TableViewLastColumnResizingFixer::connectViewHeadersSignals()
{
connect(tableView->horizontalHeader(), &QHeaderView::sectionResized, this, &TableViewLastColumnResizingFixer::on_sectionResized);
connect(tableView->horizontalHeader(), &QHeaderView::geometriesChanged, this, &TableViewLastColumnResizingFixer::on_geometriesChanged);
}

// We need to disconnect these while handling the resize events, otherwise we can enter infinite loops.
void TableViewLastColumnResizingFixer::disconnectViewHeadersSignals()
{
disconnect(tableView->horizontalHeader(), &QHeaderView::sectionResized, this, &TableViewLastColumnResizingFixer::on_sectionResized);
disconnect(tableView->horizontalHeader(), &QHeaderView::geometriesChanged, this, &TableViewLastColumnResizingFixer::on_geometriesChanged);
}

// Setup the resize mode, handles compatibility for Qt5 and below as the method signatures changed.
// Refactored here for readability.
void TableViewLastColumnResizingFixer::setViewHeaderResizeMode(int logicalIndex, QHeaderView::ResizeMode resizeMode)
{
#if QT_VERSION < 0x050000
tableView->horizontalHeader()->setResizeMode(logicalIndex, resizeMode);
#else
tableView->horizontalHeader()->setSectionResizeMode(logicalIndex, resizeMode);
#endif
}

void TableViewLastColumnResizingFixer::resizeColumn(int nColumnIndex, int width)
{
tableView->setColumnWidth(nColumnIndex, width);
tableView->horizontalHeader()->resizeSection(nColumnIndex, width);
}

int TableViewLastColumnResizingFixer::getColumnsWidth()
{
int nColumnsWidthSum = 0;
for (int i = 0; i < columnCount; i++)
{
nColumnsWidthSum += tableView->horizontalHeader()->sectionSize(i);
}
return nColumnsWidthSum;
}

int TableViewLastColumnResizingFixer::getAvailableWidthForColumn(int column)
{
int nResult = lastColumnMinimumWidth;
int nTableWidth = tableView->horizontalHeader()->width();

if (nTableWidth > 0)
{
int nOtherColsWidth = getColumnsWidth() - tableView->horizontalHeader()->sectionSize(column);
nResult = std::max(nResult, nTableWidth - nOtherColsWidth);
}

return nResult;
}

// Make sure we don't make the columns wider than the table's viewport width.
void TableViewLastColumnResizingFixer::adjustTableColumnsWidth()
{
disconnectViewHeadersSignals();
resizeColumn(lastColumnIndex, getAvailableWidthForColumn(lastColumnIndex));
connectViewHeadersSignals();

int nTableWidth = tableView->horizontalHeader()->width();
int nColsWidth = getColumnsWidth();
if (nColsWidth > nTableWidth)
{
resizeColumn(secondToLastColumnIndex,getAvailableWidthForColumn(secondToLastColumnIndex));
}
}

// Make column use all the space available, useful during window resizing.
void TableViewLastColumnResizingFixer::stretchColumnWidth(int column)
{
disconnectViewHeadersSignals();
resizeColumn(column, getAvailableWidthForColumn(column));
connectViewHeadersSignals();
}

// When a section is resized this is a slot-proxy for ajustAmountColumnWidth().
void TableViewLastColumnResizingFixer::on_sectionResized(int logicalIndex, int oldSize, int newSize)
{
adjustTableColumnsWidth();
int remainingWidth = getAvailableWidthForColumn(logicalIndex);
if (newSize > remainingWidth)
{
resizeColumn(logicalIndex, remainingWidth);
}
}

// When the table's geometry is ready, we manually perform the stretch of the "Message" column,
// as the "Stretch" resize mode does not allow for interactive resizing.
void TableViewLastColumnResizingFixer::on_geometriesChanged()
{
if ((getColumnsWidth() - this->tableView->horizontalHeader()->width()) != 0)
{
disconnectViewHeadersSignals();
resizeColumn(secondToLastColumnIndex, getAvailableWidthForColumn(secondToLastColumnIndex));
connectViewHeadersSignals();
}
}

/**
* Initializes all internal variables and prepares the
* the resize modes of the last 2 columns of the table and
*/
TableViewLastColumnResizingFixer::TableViewLastColumnResizingFixer(QTableView* table, int lastColMinimumWidth, int allColsMinimumWidth, QObject *parent) :
QObject(parent),
tableView(table),
lastColumnMinimumWidth(lastColMinimumWidth),
allColumnsMinimumWidth(allColsMinimumWidth)
{
columnCount = tableView->horizontalHeader()->count();
lastColumnIndex = columnCount - 1;
secondToLastColumnIndex = columnCount - 2;
tableView->horizontalHeader()->setMinimumSectionSize(allColumnsMinimumWidth);
setViewHeaderResizeMode(secondToLastColumnIndex, QHeaderView::Interactive);
setViewHeaderResizeMode(lastColumnIndex, QHeaderView::Interactive);
}

TableViewLastColumnResizingFixer::TableViewLastColumnResizingFixer(QTableView* table, int lastColMinimumWidth, int allColsMinimumWidth, QObject *parent, int resizeColumnIndex):
TableViewLastColumnResizingFixer(table, lastColMinimumWidth, allColsMinimumWidth, parent)
{
secondToLastColumnIndex = resizeColumnIndex;
}

#ifdef WIN32
boost::filesystem::path static StartupShortcutPath()
{
Expand Down
40 changes: 0 additions & 40 deletions src/qt/guiutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,46 +136,6 @@ namespace GUIUtil
int size_threshold;
};

/**
* Makes a QTableView last column feel as if it was being resized from its left border.
* Also makes sure the column widths are never larger than the table's viewport.
* In Qt, all columns are resizable from the right, but it's not intuitive resizing the last column from the right.
* Usually our second to last columns behave as if stretched, and when on strech mode, columns aren't resizable
* interactively or programmatically.
*
* This helper object takes care of this issue.
*
*/
class TableViewLastColumnResizingFixer: public QObject
{
Q_OBJECT

public:
TableViewLastColumnResizingFixer(QTableView* table, int lastColMinimumWidth, int allColsMinimumWidth, QObject *parent);
TableViewLastColumnResizingFixer(QTableView* table, int lastColMinimumWidth, int allColsMinimumWidth, QObject *parent, int resizeColumnIndex);
void stretchColumnWidth(int column);

private:
QTableView* tableView;
int lastColumnMinimumWidth;
int allColumnsMinimumWidth;
int lastColumnIndex;
int columnCount;
int secondToLastColumnIndex;

void adjustTableColumnsWidth();
int getAvailableWidthForColumn(int column);
int getColumnsWidth();
void connectViewHeadersSignals();
void disconnectViewHeadersSignals();
void setViewHeaderResizeMode(int logicalIndex, QHeaderView::ResizeMode resizeMode);
void resizeColumn(int nColumnIndex, int width);

private Q_SLOTS:
void on_sectionResized(int logicalIndex, int oldSize, int newSize);
void on_geometriesChanged();
};

bool GetStartOnSystemStartup();
bool SetStartOnSystemStartup(bool fAutoStart);

Expand Down
13 changes: 2 additions & 11 deletions src/qt/receivecoinsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
ReceiveCoinsDialog::ReceiveCoinsDialog(const PlatformStyle *_platformStyle, QWidget *parent) :
QDialog(parent),
ui(new Ui::ReceiveCoinsDialog),
columnResizingFixer(0),
model(0),
platformStyle(_platformStyle)
{
Expand Down Expand Up @@ -87,11 +86,11 @@ void ReceiveCoinsDialog::setModel(WalletModel *_model)
tableView->setColumnWidth(RecentRequestsTableModel::Date, DATE_COLUMN_WIDTH);
tableView->setColumnWidth(RecentRequestsTableModel::Label, LABEL_COLUMN_WIDTH);
tableView->setColumnWidth(RecentRequestsTableModel::Amount, AMOUNT_MINIMUM_COLUMN_WIDTH);
tableView->horizontalHeader()->setMinimumSectionSize(23);
tableView->horizontalHeader()->setStretchLastSection(true);

connect(tableView->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &ReceiveCoinsDialog::recentRequestsView_selectionChanged);
// Last 2 columns are set by the columnResizingFixer, when the table geometry is ready.
columnResizingFixer = new GUIUtil::TableViewLastColumnResizingFixer(tableView, AMOUNT_MINIMUM_COLUMN_WIDTH, DATE_COLUMN_WIDTH, this);
}
}

Expand Down Expand Up @@ -207,14 +206,6 @@ void ReceiveCoinsDialog::on_removeRequestButton_clicked()
model->getRecentRequestsTableModel()->removeRows(firstIndex.row(), selection.length(), firstIndex.parent());
}

// We override the virtual resizeEvent of the QWidget to adjust tables column
// sizes as the tables width is proportional to the dialogs width.
void ReceiveCoinsDialog::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
columnResizingFixer->stretchColumnWidth(RecentRequestsTableModel::Message);
}

void ReceiveCoinsDialog::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Return)
Expand Down
2 changes: 0 additions & 2 deletions src/qt/receivecoinsdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,12 @@ public Q_SLOTS:

private:
Ui::ReceiveCoinsDialog *ui;
GUIUtil::TableViewLastColumnResizingFixer *columnResizingFixer;
WalletModel *model;
QMenu *contextMenu;
const PlatformStyle *platformStyle;

QModelIndex selectedRow();
void copyColumnToClipboard(int column);
virtual void resizeEvent(QResizeEvent *event);

private Q_SLOTS:
void on_receiveButton_clicked();
Expand Down
9 changes: 1 addition & 8 deletions src/qt/tradehistorydialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ TradeHistoryDialog::TradeHistoryDialog(QWidget *parent) :
ui->tradeHistoryTable->setHorizontalHeaderItem(5, new QTableWidgetItem("Trade Details"));
ui->tradeHistoryTable->setHorizontalHeaderItem(6, new QTableWidgetItem("Sold"));
ui->tradeHistoryTable->setHorizontalHeaderItem(7, new QTableWidgetItem("Received"));
borrowedColumnResizingFixer = new GUIUtil::TableViewLastColumnResizingFixer(ui->tradeHistoryTable, 100, 100, this);
#if QT_VERSION < 0x050000
ui->tradeHistoryTable->horizontalHeader()->setResizeMode(2, QHeaderView::Fixed);
ui->tradeHistoryTable->horizontalHeader()->setResizeMode(3, QHeaderView::Interactive);
Expand Down Expand Up @@ -112,9 +111,9 @@ TradeHistoryDialog::TradeHistoryDialog(QWidget *parent) :
ui->tradeHistoryTable->resizeColumnToContents(4);
ui->tradeHistoryTable->resizeColumnToContents(6);
ui->tradeHistoryTable->resizeColumnToContents(7);
borrowedColumnResizingFixer->stretchColumnWidth(5);
ui->tradeHistoryTable->setSortingEnabled(true);
ui->tradeHistoryTable->horizontalHeader()->setSortIndicator(3, Qt::DescendingOrder);

QAction *copyTxIDAction = new QAction(tr("Copy transaction ID"), this);
QAction *showDetailsAction = new QAction(tr("Show trade details"), this);
contextMenu = new QMenu();
Expand Down Expand Up @@ -570,9 +569,3 @@ void TradeHistoryDialog::showDetails()
PopulateSimpleDialog(strTXText, "Trade Information", "Trade Information");
}
}

void TradeHistoryDialog::resizeEvent(QResizeEvent* event)
{
QWidget::resizeEvent(event);
borrowedColumnResizingFixer->stretchColumnWidth(5);
}
2 changes: 0 additions & 2 deletions src/qt/tradehistorydialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ class TradeHistoryDialog : public QDialog
~TradeHistoryDialog();
void setWalletModel(WalletModel *model);
void setClientModel(ClientModel *model);
GUIUtil::TableViewLastColumnResizingFixer *borrowedColumnResizingFixer;
virtual void resizeEvent(QResizeEvent* event);

private:
Ui::tradeHistoryDialog *ui;
Expand Down
Loading

0 comments on commit e36b3ac

Please sign in to comment.