From 8b252a9e2bef3bd48b980328729f8db414f64180 Mon Sep 17 00:00:00 2001 From: mgrojo Date: Sat, 21 Dec 2019 19:09:43 +0100 Subject: [PATCH] Enhancements in row and column resizing to fit contents A new option is added to the contextual menu of row header to auto-adjust the row heights to fit the contents. When no setting is loaded for a table, the new default for column width will take into account the contents. This is related to #2006 --- src/TableBrowser.cpp | 53 +++++++++++++++++++++++++++++++++++++++----- src/TableBrowser.h | 3 +++ 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/TableBrowser.cpp b/src/TableBrowser.cpp index fdbcc571..0f3bfc67 100644 --- a/src/TableBrowser.cpp +++ b/src/TableBrowser.cpp @@ -29,7 +29,9 @@ TableBrowser::TableBrowser(QWidget* parent) : gotoValidator(new QIntValidator(0, 0, this)), db(nullptr), dbStructureModel(nullptr), - m_model(nullptr) + m_model(nullptr), + m_adjustRows(false), + m_columnsResized(false) { ui->setupUi(this); @@ -317,7 +319,8 @@ void TableBrowser::init(DBBrowserDB* _db) delete m_model; m_model = new SqliteTableModel(*db, this); - connect(m_model, &SqliteTableModel::finishedFetch, this, &TableBrowser::updateRecordsetLabel); + connect(m_model, &SqliteTableModel::finishedFetch, this, &TableBrowser::fetchedData); + } void TableBrowser::reset() @@ -469,9 +472,8 @@ void TableBrowser::updateTable() // Enable editing in general, but lock view editing unlockViewEditing(false); - // Column widths - for(int i=1;icolumnCount();i++) - ui->dataTable->setColumnWidth(i, ui->dataTable->horizontalHeader()->defaultSectionSize()); + // Prepare for setting an initial column width based on the content. + m_columnsResized = false; // Encoding m_model->setEncoding(m_defaultEncoding); @@ -536,6 +538,7 @@ void TableBrowser::updateTable() emit updatePlot(ui->dataTable, m_model, &m_settings[tablename], false); } + // Show/hide menu options depending on whether this is a table or a view if(db->getObjectByName(currentlyBrowsedTableName()) && db->getObjectByName(currentlyBrowsedTableName())->type() == sqlb::Object::Table) { @@ -765,6 +768,7 @@ void TableBrowser::applySettings(const BrowseDataTableSettings& storedData, bool // Column widths for(auto widthIt=storedData.columnWidths.constBegin();widthIt!=storedData.columnWidths.constEnd();++widthIt) ui->dataTable->setColumnWidth(widthIt.key(), widthIt.value()); + m_columnsResized = true; // Filters if(!skipFilters) @@ -1163,6 +1167,22 @@ void TableBrowser::showRecordPopupMenu(const QPoint& pos) deleteRecord(); }); + popupRecordMenu.addSeparator(); + + QAction* adjustRowHeightAction = new QAction(tr("Adjust rows to contents"), &popupRecordMenu); + adjustRowHeightAction->setCheckable(true); + adjustRowHeightAction->setChecked(m_adjustRows); + popupRecordMenu.addAction(adjustRowHeightAction); + + connect(adjustRowHeightAction, &QAction::toggled, [&](bool checked) { + m_adjustRows = checked; + if(m_adjustRows) { + ui->dataTable->verticalHeader()->setResizeContentsPrecision(0); + ui->dataTable->resizeRowsToContents(); + } else + updateTable(); + }); + popupRecordMenu.exec(ui->dataTable->verticalHeader()->mapToGlobal(pos)); } @@ -1531,3 +1551,26 @@ void TableBrowser::find(const QString& expr, bool forward, bool include_first, R } break; } } + +void TableBrowser::fetchedData() +{ + updateRecordsetLabel(); + + // Adjust row height to contents. This has to be done each time new data is fetched. + if(m_adjustRows) + ui->dataTable->resizeRowsToContents(); + + // Don't resize the columns more than once to fit their contents. This is necessary because the finishedFetch signal of the model + // is emitted for each loaded prefetch block and we want to avoid column resizes while scrolling down. + if(m_columnsResized) + return; + m_columnsResized = true; + + // Set column widths according to their contents but make sure they don't exceed a certain size + ui->dataTable->resizeColumnsToContents(); + for(int i = 0; i < m_model->columnCount(); i++) + { + if(ui->dataTable->columnWidth(i) > 300) + ui->dataTable->setColumnWidth(i, 300); + } +} diff --git a/src/TableBrowser.h b/src/TableBrowser.h index 4252edc4..57835fbd 100644 --- a/src/TableBrowser.h +++ b/src/TableBrowser.h @@ -156,6 +156,7 @@ private slots: void saveFilterAsView(); void setTableEncoding(bool forAllTables = false); void setDefaultTableEncoding(); + void fetchedData(); private: enum class ReplaceMode @@ -185,6 +186,8 @@ private: static QString m_defaultEncoding; Palette m_condFormatPalette; + bool m_adjustRows; + bool m_columnsResized; void modifySingleFormat(const bool isRowIdFormat, const QString& filter, const QModelIndex refIndex, std::function changeFunction);