Skip to content

Commit 88d1cbc

Browse files
mgrojoMKleusberg
authored andcommitted
Pop-up completion with distinct values
A QSortFilterProxyModel is used for filtering the values in the column so only distinct values are accepted for the completion. With this change the less intrusive pop-up completion can be used, displaying only unique values in the completion menu.
1 parent 04f27cc commit 88d1cbc

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/ExtendedTableWidget.cpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,24 @@ QList<QByteArrayList> parseClipboard(QString clipboard)
100100

101101
}
102102

103+
UniqueFilterModel::UniqueFilterModel(QObject* parent)
104+
: QSortFilterProxyModel(parent)
105+
{
106+
}
107+
108+
bool UniqueFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
109+
{
110+
QModelIndex index = sourceModel()->index(sourceRow, filterKeyColumn(), sourceParent);
111+
const QString& value = index.data().toString();
112+
113+
if (!value.isEmpty() && !m_uniqueValues.contains(value)) {
114+
const_cast<UniqueFilterModel*>(this)->m_uniqueValues.insert(value);
115+
return true;
116+
}
117+
else
118+
return false;
119+
120+
}
103121

104122
ExtendedTableWidgetEditorDelegate::ExtendedTableWidgetEditorDelegate(QObject* parent)
105123
: QStyledItemDelegate(parent)
@@ -111,10 +129,16 @@ QWidget* ExtendedTableWidgetEditorDelegate::createEditor(QWidget* parent, const
111129
QLineEdit* editor = new QLineEdit(parent);
112130
// If the row count is not greater than the complete threshold setting, set a completer of values based on current values in the column.
113131
if (index.model()->rowCount() <= Settings::getValue("databrowser", "complete_threshold").toInt()) {
114-
QCompleter *completer = new QCompleter(editor);
115-
completer->setModel(const_cast<QAbstractItemModel*>(index.model()));
132+
QCompleter* completer = new QCompleter(editor);
133+
UniqueFilterModel* completerFilter = new UniqueFilterModel(completer);
134+
// Provide a filter for the source model, so only unique and non-empty values are accepted.
135+
completerFilter->setSourceModel(const_cast<QAbstractItemModel*>(index.model()));
136+
completerFilter->setFilterKeyColumn(index.column());
137+
completer->setModel(completerFilter);
138+
// Complete on this column, using a popup and case-insensitively.
116139
completer->setCompletionColumn(index.column());
117-
completer->setCompletionMode(QCompleter::InlineCompletion);
140+
completer->setCompletionMode(QCompleter::PopupCompletion);
141+
completer->setCaseSensitivity(Qt::CaseInsensitive);
118142
editor->setCompleter(completer);
119143
}
120144
// Set the maximum length to the highest possible value instead of the default 32768.

src/ExtendedTableWidget.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,25 @@
66
#include <QDropEvent>
77
#include <QDragMoveEvent>
88
#include <QStyledItemDelegate>
9+
#include <QSortFilterProxyModel>
910

1011
class QMenu;
1112
class QMimeData;
1213
class FilterTableHeader;
1314
namespace sqlb { class ObjectIdentifier; }
1415

16+
// Filter proxy model that only accepts distinct non-empty values.
17+
class UniqueFilterModel : public QSortFilterProxyModel
18+
{
19+
Q_OBJECT
20+
21+
public:
22+
explicit UniqueFilterModel(QObject* parent = nullptr);
23+
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
24+
private:
25+
QSet<QString> m_uniqueValues;
26+
};
27+
1528
// We use this class to provide editor widgets for the ExtendedTableWidget. It's used for every cell in the table view.
1629
class ExtendedTableWidgetEditorDelegate : public QStyledItemDelegate
1730
{

0 commit comments

Comments
 (0)