From 2b6153d9b8af5b5d806af02eb039c6a857bf0516 Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Fri, 10 Aug 2018 13:52:39 +0200 Subject: [PATCH] Fix browse table settings not being reapplied correctly We have already fixed the rowid column from appearing after almost every action in the Browse Data tab, for example in commit 39302f5b6061cf687a81984406e20070d60b4563. This didn't fix every issue related to this problem though. Hidden columns are shown too and modified column widths are reset. Also this happens for different actions, for example the column widths are also reset when sorting the view or when changing the filters. All these problems are hopefully fixed by this commit. See issue #1475. --- src/MainWindow.cpp | 87 +++++++++++++++++++++++++--------------------- src/MainWindow.h | 4 ++- 2 files changed, 50 insertions(+), 41 deletions(-) diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index c2d839e8..ab53e421 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -601,33 +601,7 @@ void MainWindow::populateTable() m_browseTableModel->setTable(tablename, storedData.sortOrderIndex, storedData.sortOrderMode, v); // There is information stored for this table, so extract it and apply it - - // Show rowid column. Needs to be done before the column widths setting because of the workaround in there and before the filter setting - // because of the filter row generation. - showRowidColumn(storedData.showRowid); - - // Enable editing in general and (un)lock view editing depending on the settings - unlockViewEditing(!storedData.unlockViewPk.isEmpty(), storedData.unlockViewPk); - - // Column hidden status - on_actionShowAllColumns_triggered(); - for(auto hiddenIt=storedData.hiddenColumns.constBegin();hiddenIt!=storedData.hiddenColumns.constEnd();++hiddenIt) - hideColumns(hiddenIt.key(), hiddenIt.value()); - - // Column widths - for(auto widthIt=storedData.columnWidths.constBegin();widthIt!=storedData.columnWidths.constEnd();++widthIt) - ui->dataTable->setColumnWidth(widthIt.key(), widthIt.value()); - - // Sorting - ui->dataTable->filterHeader()->setSortIndicator(storedData.sortOrderIndex, storedData.sortOrderMode); - - // Filters - FilterTableHeader* filterHeader = qobject_cast(ui->dataTable->horizontalHeader()); - for(auto filterIt=storedData.filterValues.constBegin();filterIt!=storedData.filterValues.constEnd();++filterIt) - filterHeader->setFilter(filterIt.key(), filterIt.value()); - - // Encoding - m_browseTableModel->setEncoding(storedData.encoding); + applyBrowseTableSettings(storedData); setRecordsetLabel(); @@ -653,6 +627,39 @@ void MainWindow::populateTable() QApplication::restoreOverrideCursor(); } +void MainWindow::applyBrowseTableSettings(const BrowseDataTableSettings& storedData, bool skipFilters) +{ + // Show rowid column. Needs to be done before the column widths setting because of the workaround in there and before the filter setting + // because of the filter row generation. + showRowidColumn(storedData.showRowid, skipFilters); + + // Enable editing in general and (un)lock view editing depending on the settings + unlockViewEditing(!storedData.unlockViewPk.isEmpty(), storedData.unlockViewPk); + + // Column hidden status + on_actionShowAllColumns_triggered(); + for(auto hiddenIt=storedData.hiddenColumns.constBegin();hiddenIt!=storedData.hiddenColumns.constEnd();++hiddenIt) + hideColumns(hiddenIt.key(), hiddenIt.value()); + + // Column widths + for(auto widthIt=storedData.columnWidths.constBegin();widthIt!=storedData.columnWidths.constEnd();++widthIt) + ui->dataTable->setColumnWidth(widthIt.key(), widthIt.value()); + + // Sorting + ui->dataTable->filterHeader()->setSortIndicator(storedData.sortOrderIndex, storedData.sortOrderMode); + + // Filters + if(!skipFilters) + { + FilterTableHeader* filterHeader = qobject_cast(ui->dataTable->horizontalHeader()); + for(auto filterIt=storedData.filterValues.constBegin();filterIt!=storedData.filterValues.constEnd();++filterIt) + filterHeader->setFilter(filterIt.key(), filterIt.value()); + } + + // Encoding + m_browseTableModel->setEncoding(storedData.encoding); +} + bool MainWindow::fileClose() { // Close the database but stop the closing process here if the user pressed the cancel button in there @@ -1824,9 +1831,8 @@ void MainWindow::browseTableHeaderClicked(int logicalindex) attachPlot(ui->dataTable, m_browseTableModel, &browseTableSettings[currentlyBrowsedTableName()]); - // This seems to be necessary as a workaround for newer Qt versions. Otherwise the rowid column is always shown after changing the data view. - bool showRowid = browseTableSettings[currentlyBrowsedTableName()].showRowid; - ui->dataTable->setColumnHidden(0, !showRowid); + // Reapply the view settings. This seems to be necessary as a workaround for newer Qt versions. + applyBrowseTableSettings(settings); } void MainWindow::resizeEvent(QResizeEvent*) @@ -2666,12 +2672,12 @@ void MainWindow::fileAttach() void MainWindow::updateFilter(int column, const QString& value) { m_browseTableModel->updateFilter(column, value); - browseTableSettings[currentlyBrowsedTableName()].filterValues[column] = value; + BrowseDataTableSettings& settings = browseTableSettings[currentlyBrowsedTableName()]; + settings.filterValues[column] = value; setRecordsetLabel(); - // This seems to be necessary as a workaround for newer Qt versions. Otherwise the rowid column is always shown after changing the data view. - bool showRowid = browseTableSettings[currentlyBrowsedTableName()].showRowid; - ui->dataTable->setColumnHidden(0, !showRowid); + // Reapply the view settings. This seems to be necessary as a workaround for newer Qt versions. + applyBrowseTableSettings(settings, true); } void MainWindow::editEncryption() @@ -2890,7 +2896,7 @@ void MainWindow::editDataColumnDisplayFormat() } } -void MainWindow::showRowidColumn(bool show) +void MainWindow::showRowidColumn(bool show, bool skipFilters) { // Block all signals from the horizontal header. Otherwise the QHeaderView::sectionResized signal causes us trouble ui->dataTable->horizontalHeader()->blockSignals(true); @@ -2915,7 +2921,8 @@ void MainWindow::showRowidColumn(bool show) browseTableSettings[current_table].showRowid = show; // Update the filter row - qobject_cast(ui->dataTable->horizontalHeader())->generateFilters(m_browseTableModel->columnCount(), show); + if(!skipFilters) + qobject_cast(ui->dataTable->horizontalHeader())->generateFilters(m_browseTableModel->columnCount(), show); // Re-enable signals ui->dataTable->horizontalHeader()->blockSignals(false); @@ -3040,11 +3047,11 @@ void MainWindow::unlockViewEditing(bool unlock, QString pk) ui->actionUnlockViewEditing->blockSignals(false); // Save settings for this table - browseTableSettings[currentTable].unlockViewPk = pk; + BrowseDataTableSettings& settings = browseTableSettings[currentTable]; + settings.unlockViewPk = pk; - // This seems to be necessary as a workaround for newer Qt versions. Otherwise the rowid column is always shown after changing the data view. - bool showRowid = browseTableSettings[currentlyBrowsedTableName()].showRowid; - ui->dataTable->setColumnHidden(0, !showRowid); + // Reapply the view settings. This seems to be necessary as a workaround for newer Qt versions. + applyBrowseTableSettings(settings); } sqlb::ObjectIdentifier MainWindow::currentlyBrowsedTableName() const diff --git a/src/MainWindow.h b/src/MainWindow.h index f0da04f5..28b03c82 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -193,6 +193,8 @@ private: StatementType getQueryType(const QString& query) const; + void applyBrowseTableSettings(const BrowseDataTableSettings& storedData, bool skipFilters = false); + protected: void closeEvent(QCloseEvent *); void dragEnterEvent(QDragEnterEvent *event); @@ -277,7 +279,7 @@ private slots: void showDataColumnPopupMenu(const QPoint& pos); void showRecordPopupMenu(const QPoint& pos); void editDataColumnDisplayFormat(); - void showRowidColumn(bool show); + void showRowidColumn(bool show, bool skipFilters = false); void browseDataSetTableEncoding(bool forAllTables = false); void browseDataSetDefaultTableEncoding(); void fileOpenReadOnly();