Skip to content

Commit 2794957

Browse files
committed
Use a Filter Line Edit as editor for conditions in Conditional Formats
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 #1976 and comments in PR #2013
1 parent a0b8b6f commit 2794957

2 files changed

Lines changed: 49 additions & 18 deletions

File tree

src/CondFormatManager.cpp

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "ui_CondFormatManager.h"
33
#include "CondFormat.h"
44
#include "Settings.h"
5+
#include "FilterLineEdit.h"
56

67
#include <QColorDialog>
78
#include <QDesktopServices>
@@ -11,6 +12,28 @@
1112
#include <QFontComboBox>
1213
#include <QSpinBox>
1314
#include <QComboBox>
15+
#include <QStyledItemDelegate>
16+
17+
// Styled Item Delegate for non-editable columns (all except Condition)
18+
class DefaultDelegate: public QStyledItemDelegate {
19+
public:
20+
explicit DefaultDelegate(QObject* parent=nullptr): QStyledItemDelegate(parent) {}
21+
QWidget* createEditor(QWidget* /* parent */, const QStyleOptionViewItem& /* option */, const QModelIndex& /* index */) const override {
22+
return nullptr;
23+
}
24+
};
25+
26+
// Styled Item Delegate for the Condition column as a filter line editor.
27+
class FilterEditDelegate: public QStyledItemDelegate {
28+
29+
public:
30+
explicit FilterEditDelegate(QObject* parent=nullptr): QStyledItemDelegate(parent) {}
31+
QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem& /* option */, const QModelIndex& /* index */) const override {
32+
FilterLineEdit* filterEditor = new FilterLineEdit(parent);
33+
filterEditor->setConditionFormatContextMenuEnabled(false);
34+
return filterEditor;
35+
}
36+
};
1437

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

26-
// Resize columns to contents, except for the condition
27-
for(int col = ColumnForeground; col < ColumnFilter; ++col)
28-
ui->tableCondFormats->resizeColumnToContents(col);
29-
49+
// Make condition editable as a filter line editor.
3050
ui->tableCondFormats->setEditTriggers(QAbstractItemView::AllEditTriggers);
51+
ui->tableCondFormats->setItemDelegateForColumn(ColumnFilter, new FilterEditDelegate(this));
52+
53+
// Resize columns to contents and make them not text-editable, except for the condition.
54+
for(int col = ColumnForeground; col < ColumnFilter; ++col) {
55+
ui->tableCondFormats->setItemDelegateForColumn(col, new DefaultDelegate(this));
56+
ui->tableCondFormats->resizeColumnToContents(col);
57+
}
3158

3259
connect(ui->buttonAdd, SIGNAL(clicked(bool)), this, SLOT(addNewItem()));
3360
connect(ui->buttonRemove, SIGNAL(clicked(bool)), this, SLOT(removeItem()));
@@ -64,6 +91,8 @@ void CondFormatManager::addItem(const CondFormat& aCondFormat)
6491
{
6592
int i = ui->tableCondFormats->topLevelItemCount();
6693
QTreeWidgetItem *newItem = new QTreeWidgetItem(ui->tableCondFormats);
94+
newItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
95+
6796
newItem->setForeground(ColumnForeground, aCondFormat.foregroundColor());
6897
newItem->setBackground(ColumnForeground, aCondFormat.foregroundColor());
6998
newItem->setForeground(ColumnBackground, aCondFormat.backgroundColor());
@@ -91,7 +120,6 @@ void CondFormatManager::addItem(const CondFormat& aCondFormat)
91120

92121
newItem->setText(ColumnFilter, aCondFormat.filter());
93122
ui->tableCondFormats->insertTopLevelItem(i, newItem);
94-
ui->tableCondFormats->openPersistentEditor(newItem, ColumnFilter);
95123
}
96124

97125
void CondFormatManager::removeItem()
@@ -134,7 +162,6 @@ void CondFormatManager::moveItem(int offset)
134162
ui->tableCondFormats->setItemWidget(item, ColumnFont, fontCombo2);
135163
ui->tableCondFormats->setItemWidget(item, ColumnSize, sizeBox2);
136164
ui->tableCondFormats->setItemWidget(item, ColumnAlignment, alignCombo2);
137-
ui->tableCondFormats->openPersistentEditor(item, ColumnFilter);
138165
ui->tableCondFormats->setCurrentIndex(ui->tableCondFormats->currentIndex().sibling(newRow,
139166
ui->tableCondFormats->currentIndex().column()));
140167
}
@@ -180,16 +207,20 @@ std::vector<CondFormat> CondFormatManager::getCondFormats()
180207
void CondFormatManager::itemClicked(QTreeWidgetItem* item, int column)
181208
{
182209
switch (column) {
183-
case ColumnForeground:
184-
case ColumnBackground: {
185-
QColor color = QColorDialog::getColor(item->background(column).color(), this);
186-
if(color.isValid()) {
187-
item->setTextColor(column, color);
188-
item->setBackgroundColor(column, color);
189-
item->setToolTip(column, tr("Click to select color"));
190-
}
191-
break;
192-
}
210+
case ColumnForeground:
211+
case ColumnBackground: {
212+
QColor color = QColorDialog::getColor(item->background(column).color(), this);
213+
if(color.isValid()) {
214+
item->setTextColor(column, color);
215+
item->setBackgroundColor(column, color);
216+
}
217+
break;
218+
}
219+
case ColumnBold:
220+
case ColumnItalic:
221+
case ColumnUnderline:
222+
item->setCheckState(column, item->checkState(column) != Qt::Checked ? Qt::Checked : Qt::Unchecked);
223+
break;
193224
default:
194225
// Nothing to do
195226
break;

src/CondFormatManager.ui

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>700</width>
9+
<width>750</width>
1010
<height>400</height>
1111
</rect>
1212
</property>
@@ -17,7 +17,7 @@
1717
<item>
1818
<widget class="QLabel" name="labelTitle">
1919
<property name="text">
20-
<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>
20+
<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>
2121
</property>
2222
<property name="wordWrap">
2323
<bool>true</bool>

0 commit comments

Comments
 (0)