Skip to content

Commit

Permalink
Use a Filter Line Edit as editor for conditions in Conditional Formats
Browse files Browse the repository at this point in the history
The Filter Line Edit is ideal for this dialog because it has already a
contextual menu for helping user with the syntax and a What's This button.

Moreover, the Line Edit looks better than the integrated persistent editor.

See issue sqlitebrowser#1976 and comments in PR sqlitebrowser#2013
  • Loading branch information
mgrojo committed Oct 5, 2019
1 parent a0b8b6f commit 2794957
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
63 changes: 47 additions & 16 deletions src/CondFormatManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "ui_CondFormatManager.h"
#include "CondFormat.h"
#include "Settings.h"
#include "FilterLineEdit.h"

#include <QColorDialog>
#include <QDesktopServices>
Expand All @@ -11,6 +12,28 @@
#include <QFontComboBox>
#include <QSpinBox>
#include <QComboBox>
#include <QStyledItemDelegate>

// Styled Item Delegate for non-editable columns (all except Condition)
class DefaultDelegate: public QStyledItemDelegate {
public:
explicit DefaultDelegate(QObject* parent=nullptr): QStyledItemDelegate(parent) {}
QWidget* createEditor(QWidget* /* parent */, const QStyleOptionViewItem& /* option */, const QModelIndex& /* index */) const override {
return nullptr;
}
};

// Styled Item Delegate for the Condition column as a filter line editor.
class FilterEditDelegate: public QStyledItemDelegate {

public:
explicit FilterEditDelegate(QObject* parent=nullptr): QStyledItemDelegate(parent) {}
QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem& /* option */, const QModelIndex& /* index */) const override {
FilterLineEdit* filterEditor = new FilterLineEdit(parent);
filterEditor->setConditionFormatContextMenuEnabled(false);
return filterEditor;
}
};

CondFormatManager::CondFormatManager(const std::vector<CondFormat>& condFormats, const QString& encoding, QWidget *parent) :
QDialog(parent),
Expand All @@ -23,11 +46,15 @@ CondFormatManager::CondFormatManager(const std::vector<CondFormat>& condFormats,
for(const CondFormat& aCondFormat : condFormats)
addItem(aCondFormat);

// Resize columns to contents, except for the condition
for(int col = ColumnForeground; col < ColumnFilter; ++col)
ui->tableCondFormats->resizeColumnToContents(col);

// Make condition editable as a filter line editor.
ui->tableCondFormats->setEditTriggers(QAbstractItemView::AllEditTriggers);
ui->tableCondFormats->setItemDelegateForColumn(ColumnFilter, new FilterEditDelegate(this));

// Resize columns to contents and make them not text-editable, except for the condition.
for(int col = ColumnForeground; col < ColumnFilter; ++col) {
ui->tableCondFormats->setItemDelegateForColumn(col, new DefaultDelegate(this));
ui->tableCondFormats->resizeColumnToContents(col);
}

connect(ui->buttonAdd, SIGNAL(clicked(bool)), this, SLOT(addNewItem()));
connect(ui->buttonRemove, SIGNAL(clicked(bool)), this, SLOT(removeItem()));
Expand Down Expand Up @@ -64,6 +91,8 @@ void CondFormatManager::addItem(const CondFormat& aCondFormat)
{
int i = ui->tableCondFormats->topLevelItemCount();
QTreeWidgetItem *newItem = new QTreeWidgetItem(ui->tableCondFormats);
newItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);

newItem->setForeground(ColumnForeground, aCondFormat.foregroundColor());
newItem->setBackground(ColumnForeground, aCondFormat.foregroundColor());
newItem->setForeground(ColumnBackground, aCondFormat.backgroundColor());
Expand Down Expand Up @@ -91,7 +120,6 @@ void CondFormatManager::addItem(const CondFormat& aCondFormat)

newItem->setText(ColumnFilter, aCondFormat.filter());
ui->tableCondFormats->insertTopLevelItem(i, newItem);
ui->tableCondFormats->openPersistentEditor(newItem, ColumnFilter);
}

void CondFormatManager::removeItem()
Expand Down Expand Up @@ -134,7 +162,6 @@ void CondFormatManager::moveItem(int offset)
ui->tableCondFormats->setItemWidget(item, ColumnFont, fontCombo2);
ui->tableCondFormats->setItemWidget(item, ColumnSize, sizeBox2);
ui->tableCondFormats->setItemWidget(item, ColumnAlignment, alignCombo2);
ui->tableCondFormats->openPersistentEditor(item, ColumnFilter);
ui->tableCondFormats->setCurrentIndex(ui->tableCondFormats->currentIndex().sibling(newRow,
ui->tableCondFormats->currentIndex().column()));
}
Expand Down Expand Up @@ -180,16 +207,20 @@ std::vector<CondFormat> CondFormatManager::getCondFormats()
void CondFormatManager::itemClicked(QTreeWidgetItem* item, int column)
{
switch (column) {
case ColumnForeground:
case ColumnBackground: {
QColor color = QColorDialog::getColor(item->background(column).color(), this);
if(color.isValid()) {
item->setTextColor(column, color);
item->setBackgroundColor(column, color);
item->setToolTip(column, tr("Click to select color"));
}
break;
}
case ColumnForeground:
case ColumnBackground: {
QColor color = QColorDialog::getColor(item->background(column).color(), this);
if(color.isValid()) {
item->setTextColor(column, color);
item->setBackgroundColor(column, color);
}
break;
}
case ColumnBold:
case ColumnItalic:
case ColumnUnderline:
item->setCheckState(column, item->checkState(column) != Qt::Checked ? Qt::Checked : Qt::Unchecked);
break;
default:
// Nothing to do
break;
Expand Down
4 changes: 2 additions & 2 deletions src/CondFormatManager.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>700</width>
<width>750</width>
<height>400</height>
</rect>
</property>
Expand All @@ -17,7 +17,7 @@
<item>
<widget class="QLabel" name="labelTitle">
<property name="text">
<string>This dialog allows creating and editing conditional formats. Each cell style will be selected by the first accomplished condition for that cell data. Conditional formats can be moved up and down, where those at higher rows take precedence over those at lower. An empty condition applies to all values.</string>
<string>This dialog allows creating and editing conditional formats. Each cell style will be selected by the first accomplished condition for that cell data. Conditional formats can be moved up and down, where those at higher rows take precedence over those at lower. Syntax for conditions is the same as for filters and an empty condition applies to all values.</string>
</property>
<property name="wordWrap">
<bool>true</bool>
Expand Down

0 comments on commit 2794957

Please sign in to comment.