From 7b6bc013cddf3652345c1ab3f5458c2a446e1546 Mon Sep 17 00:00:00 2001 From: mgrojo Date: Sat, 13 Apr 2019 13:33:24 +0200 Subject: [PATCH] Improvements in the Conditional Formats Dialog - Empty condition means: apply format to any row. - Rearranged dialog upper buttons so it resembles the Edit Table Dialog. - Added Help button: link to wiki page. - Added Reset button: clear all conditional formats in the dialog. - Fixed some glitches in the move commands: open editors were sometimes closed, using currentIndex and selectedItems caused inconsistencies. - Select colour with a single click and show it in the tooltip. - Remove the unneeded colour name in the colour cells. See issue #1815 --- src/CondFormatManager.cpp | 43 +++++++++++++---- src/CondFormatManager.h | 4 +- src/CondFormatManager.ui | 99 ++++++++++++++++++++++++++++----------- src/sqlitetablemodel.cpp | 3 +- 4 files changed, 111 insertions(+), 38 deletions(-) diff --git a/src/CondFormatManager.cpp b/src/CondFormatManager.cpp index 7b8db97cf..fbbd45318 100644 --- a/src/CondFormatManager.cpp +++ b/src/CondFormatManager.cpp @@ -3,7 +3,11 @@ #include "CondFormat.h" #include "Settings.h" -#include "QColorDialog" +#include +#include +#include +#include +#include CondFormatManager::CondFormatManager(const QVector& condFormats, const QString& encoding, QWidget *parent) : QDialog(parent), @@ -24,7 +28,7 @@ CondFormatManager::CondFormatManager(const QVector& condFormats, con connect(ui->buttonDown, SIGNAL(clicked(bool)), this, SLOT(downItem())); connect(ui->buttonUp, SIGNAL(clicked(bool)), this, SLOT(upItem())); - connect(ui->tableCondFormats, &QTreeWidget::itemDoubleClicked, this, &CondFormatManager::itemDoubleClicked); + connect(ui->tableCondFormats, &QTreeWidget::itemClicked, this, &CondFormatManager::itemClicked); } CondFormatManager::~CondFormatManager() @@ -43,12 +47,13 @@ void CondFormatManager::addNewItem() void CondFormatManager::addItem(const CondFormat& aCondFormat) { int i = ui->tableCondFormats->topLevelItemCount(); - QTreeWidgetItem *newItem = new QTreeWidgetItem({aCondFormat.foregroundColor().name(), - aCondFormat.backgroundColor().name(), aCondFormat.filter()}); + QTreeWidgetItem *newItem = new QTreeWidgetItem({"", "", aCondFormat.filter()}); newItem->setForeground(ColumnForeground, aCondFormat.foregroundColor()); newItem->setBackground(ColumnForeground, aCondFormat.foregroundColor()); newItem->setForeground(ColumnBackground, aCondFormat.backgroundColor()); newItem->setBackground(ColumnBackground, aCondFormat.backgroundColor()); + newItem->setToolTip(ColumnBackground, tr("Click to select color")); + newItem->setToolTip(ColumnForeground, tr("Click to select color")); ui->tableCondFormats->insertTopLevelItem(i, newItem); ui->tableCondFormats->openPersistentEditor(newItem, ColumnFilter); } @@ -61,7 +66,8 @@ void CondFormatManager::removeItem() void CondFormatManager::upItem() { - if (ui->tableCondFormats->selectedItems().isEmpty()) return; + if (!ui->tableCondFormats->currentIndex().isValid()) + return; int selectedRow = ui->tableCondFormats->currentIndex().row(); if(selectedRow == 0) @@ -70,13 +76,14 @@ void CondFormatManager::upItem() QTreeWidgetItem* item; item = ui->tableCondFormats->takeTopLevelItem(selectedRow); ui->tableCondFormats->insertTopLevelItem(selectedRow-1, item); + ui->tableCondFormats->openPersistentEditor(item, ColumnFilter); ui->tableCondFormats->setCurrentIndex(ui->tableCondFormats->currentIndex().sibling(selectedRow-1, ui->tableCondFormats->currentIndex().column())); } void CondFormatManager::downItem() { - if (ui->tableCondFormats->selectedItems().isEmpty()) return; + if (!ui->tableCondFormats->currentIndex().isValid()) return; int selectedRow = ui->tableCondFormats->currentIndex().row(); if(selectedRow == ui->tableCondFormats->topLevelItemCount() - 1) @@ -85,6 +92,7 @@ void CondFormatManager::downItem() QTreeWidgetItem* item; item = ui->tableCondFormats->takeTopLevelItem(selectedRow); ui->tableCondFormats->insertTopLevelItem(selectedRow+1, item); + ui->tableCondFormats->openPersistentEditor(item, ColumnFilter); ui->tableCondFormats->setCurrentIndex(ui->tableCondFormats->currentIndex().sibling(selectedRow+1, ui->tableCondFormats->currentIndex().column())); } @@ -104,7 +112,7 @@ QVector CondFormatManager::getCondFormats() } -void CondFormatManager::itemDoubleClicked(QTreeWidgetItem* item, int column) +void CondFormatManager::itemClicked(QTreeWidgetItem* item, int column) { switch (column) { case ColumnForeground: @@ -113,7 +121,7 @@ void CondFormatManager::itemDoubleClicked(QTreeWidgetItem* item, int column) if(color.isValid()) { item->setTextColor(column, color); item->setBackgroundColor(column, color); - item->setText(column, color.name()); + item->setToolTip(column, tr("Click to select color")); } break; } @@ -122,3 +130,22 @@ void CondFormatManager::itemDoubleClicked(QTreeWidgetItem* item, int column) break; } } + +void CondFormatManager::on_buttonBox_clicked(QAbstractButton* button) +{ + if (button == ui->buttonBox->button(QDialogButtonBox::Cancel)) + reject(); + else if (button == ui->buttonBox->button(QDialogButtonBox::Ok)) + accept(); + else if (button == ui->buttonBox->button(QDialogButtonBox::Help)) + QDesktopServices::openUrl(QUrl("https://github.com/sqlitebrowser/sqlitebrowser/wiki/Conditional-Formats")); + else if (button == ui->buttonBox->button(QDialogButtonBox::Reset)) { + if (QMessageBox::warning(this, + QApplication::applicationName(), + tr("Are you sure you want to clear all the conditional formats of this field?"), + QMessageBox::Reset | QMessageBox::Cancel, + QMessageBox::Cancel) == QMessageBox::Reset) + if(ui->tableCondFormats->model()->hasChildren()) + ui->tableCondFormats->model()->removeRows(0, ui->tableCondFormats->model()->rowCount()); + } +} diff --git a/src/CondFormatManager.h b/src/CondFormatManager.h index 9cfdb4b0b..88124f4fd 100644 --- a/src/CondFormatManager.h +++ b/src/CondFormatManager.h @@ -11,6 +11,7 @@ namespace Ui { class CondFormat; class QTreeWidgetItem; +class QAbstractButton; class CondFormatManager : public QDialog { @@ -38,9 +39,10 @@ private slots: void removeItem(); void upItem(); void downItem(); + void on_buttonBox_clicked(QAbstractButton* button); public slots: - void itemDoubleClicked(QTreeWidgetItem* item, int column); + void itemClicked(QTreeWidgetItem* item, int column); }; #endif // CONDFORMATMANAGER_H diff --git a/src/CondFormatManager.ui b/src/CondFormatManager.ui index 61f82ce8b..319dd7251 100644 --- a/src/CondFormatManager.ui +++ b/src/CondFormatManager.ui @@ -14,65 +14,111 @@ Conditional Format Manager + + + + This dialog allows creating and editing conditional formats, where the cell text and background will be colored based on one or more conditions. Conditional formats can be moved up and down, where those at higher rows take precedence over those at lower. + + + true + + + - + + + Add new conditional format + - &Up + &Add - :/icons/up:/icons/up + :/icons/field_add:/icons/field_add + + + Qt::ToolButtonTextBesideIcon + + + true - + + + Remove selected conditional format + - &Down + &Remove - :/icons/down:/icons/down + :/icons/field_delete:/icons/field_delete - - - - - - Qt::Horizontal + + Qt::ToolButtonTextBesideIcon - - - 40 - 20 - + + true - + - + + + Move selected conditional format up + - &Add + Move &up - :/icons/field_add:/icons/field_add + :/icons/up:/icons/up + + + Qt::ToolButtonTextBesideIcon + + + true - + + + Move selected conditional format down + - &Remove + Move &down - :/icons/field_delete:/icons/field_delete + :/icons/down:/icons/down + + + Qt::ToolButtonTextBesideIcon + + + true + + + + Qt::Horizontal + + + + 40 + 20 + + + + @@ -86,9 +132,6 @@ true - - QAbstractItemView::SingleSelection - QAbstractItemView::SelectItems @@ -133,7 +176,7 @@ Qt::Horizontal - QDialogButtonBox::Cancel|QDialogButtonBox::Ok + QDialogButtonBox::Cancel|QDialogButtonBox::Help|QDialogButtonBox::Ok|QDialogButtonBox::Reset diff --git a/src/sqlitetablemodel.cpp b/src/sqlitetablemodel.cpp index 3cf24ebb5..90fc5838c 100644 --- a/src/sqlitetablemodel.cpp +++ b/src/sqlitetablemodel.cpp @@ -278,8 +278,9 @@ QColor SqliteTableModel::getMatchingCondFormatColor(int column, const QString& v else sql = QString("SELECT '%1' %2").arg(value, eachCondFormat.sqlCondition()); + // Empty filter means: apply format to any row. // Query the DB for the condition, waiting in case there is a loading in progress. - if (m_db.querySingleValueFromDb(sql, false, DBBrowserDB::Wait) == "1") + if (eachCondFormat.filter().isEmpty() || m_db.querySingleValueFromDb(sql, false, DBBrowserDB::Wait) == "1") return role == Qt::ForegroundRole ? eachCondFormat.foregroundColor() : eachCondFormat.backgroundColor(); } return QColor();