Skip to content

Commit

Permalink
Improvements in the Conditional Formats Dialog
Browse files Browse the repository at this point in the history
- 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 sqlitebrowser#1815
  • Loading branch information
mgrojo committed Apr 13, 2019
1 parent c2eedbc commit 7b6bc01
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 38 deletions.
43 changes: 35 additions & 8 deletions src/CondFormatManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
#include "CondFormat.h"
#include "Settings.h"

#include "QColorDialog"
#include <QColorDialog>
#include <QDesktopServices>
#include <QUrl>
#include <QPushButton>
#include <QMessageBox>

CondFormatManager::CondFormatManager(const QVector<CondFormat>& condFormats, const QString& encoding, QWidget *parent) :
QDialog(parent),
Expand All @@ -24,7 +28,7 @@ CondFormatManager::CondFormatManager(const QVector<CondFormat>& 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()
Expand All @@ -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);
}
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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()));
}
Expand All @@ -104,7 +112,7 @@ QVector<CondFormat> CondFormatManager::getCondFormats()
}


void CondFormatManager::itemDoubleClicked(QTreeWidgetItem* item, int column)
void CondFormatManager::itemClicked(QTreeWidgetItem* item, int column)
{
switch (column) {
case ColumnForeground:
Expand All @@ -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;
}
Expand All @@ -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());
}
}
4 changes: 3 additions & 1 deletion src/CondFormatManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Ui {

class CondFormat;
class QTreeWidgetItem;
class QAbstractButton;

class CondFormatManager : public QDialog
{
Expand Down Expand Up @@ -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
99 changes: 71 additions & 28 deletions src/CondFormatManager.ui
Original file line number Diff line number Diff line change
Expand Up @@ -14,65 +14,111 @@
<string>Conditional Format Manager</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="labelTitle">
<property name="text">
<string>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.</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="buttonUp">
<widget class="QToolButton" name="buttonAdd">
<property name="toolTip">
<string>Add new conditional format</string>
</property>
<property name="text">
<string>&amp;Up</string>
<string>&amp;Add</string>
</property>
<property name="icon">
<iconset resource="icons/icons.qrc">
<normaloff>:/icons/up</normaloff>:/icons/up</iconset>
<normaloff>:/icons/field_add</normaloff>:/icons/field_add</iconset>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextBesideIcon</enum>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="buttonDown">
<widget class="QToolButton" name="buttonRemove">
<property name="toolTip">
<string>Remove selected conditional format</string>
</property>
<property name="text">
<string>&amp;Down</string>
<string>&amp;Remove</string>
</property>
<property name="icon">
<iconset resource="icons/icons.qrc">
<normaloff>:/icons/down</normaloff>:/icons/down</iconset>
<normaloff>:/icons/field_delete</normaloff>:/icons/field_delete</iconset>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextBesideIcon</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
<property name="autoRaise">
<bool>true</bool>
</property>
</spacer>
</widget>
</item>
<item>
<widget class="QPushButton" name="buttonAdd">
<widget class="QToolButton" name="buttonUp">
<property name="toolTip">
<string>Move selected conditional format up</string>
</property>
<property name="text">
<string>&amp;Add</string>
<string>Move &amp;up</string>
</property>
<property name="icon">
<iconset resource="icons/icons.qrc">
<normaloff>:/icons/field_add</normaloff>:/icons/field_add</iconset>
<normaloff>:/icons/up</normaloff>:/icons/up</iconset>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextBesideIcon</enum>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="buttonRemove">
<widget class="QToolButton" name="buttonDown">
<property name="toolTip">
<string>Move selected conditional format down</string>
</property>
<property name="text">
<string>&amp;Remove</string>
<string>Move &amp;down</string>
</property>
<property name="icon">
<iconset resource="icons/icons.qrc">
<normaloff>:/icons/field_delete</normaloff>:/icons/field_delete</iconset>
<normaloff>:/icons/down</normaloff>:/icons/down</iconset>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextBesideIcon</enum>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
Expand All @@ -86,9 +132,6 @@
<property name="tabKeyNavigation">
<bool>true</bool>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectItems</enum>
</property>
Expand Down Expand Up @@ -133,7 +176,7 @@
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Help|QDialogButtonBox::Ok|QDialogButtonBox::Reset</set>
</property>
</widget>
</item>
Expand Down
3 changes: 2 additions & 1 deletion src/sqlitetablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 7b6bc01

Please sign in to comment.