Skip to content

Commit

Permalink
Pop-up completion with distinct values
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mgrojo authored and MKleusberg committed Sep 28, 2018
1 parent 04f27cc commit 88d1cbc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/ExtendedTableWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,24 @@ QList<QByteArrayList> parseClipboard(QString clipboard)

}

UniqueFilterModel::UniqueFilterModel(QObject* parent)
: QSortFilterProxyModel(parent)
{
}

bool UniqueFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
QModelIndex index = sourceModel()->index(sourceRow, filterKeyColumn(), sourceParent);
const QString& value = index.data().toString();

if (!value.isEmpty() && !m_uniqueValues.contains(value)) {
const_cast<UniqueFilterModel*>(this)->m_uniqueValues.insert(value);
return true;
}
else
return false;

}

ExtendedTableWidgetEditorDelegate::ExtendedTableWidgetEditorDelegate(QObject* parent)
: QStyledItemDelegate(parent)
Expand All @@ -111,10 +129,16 @@ QWidget* ExtendedTableWidgetEditorDelegate::createEditor(QWidget* parent, const
QLineEdit* editor = new QLineEdit(parent);
// If the row count is not greater than the complete threshold setting, set a completer of values based on current values in the column.
if (index.model()->rowCount() <= Settings::getValue("databrowser", "complete_threshold").toInt()) {
QCompleter *completer = new QCompleter(editor);
completer->setModel(const_cast<QAbstractItemModel*>(index.model()));
QCompleter* completer = new QCompleter(editor);
UniqueFilterModel* completerFilter = new UniqueFilterModel(completer);
// Provide a filter for the source model, so only unique and non-empty values are accepted.
completerFilter->setSourceModel(const_cast<QAbstractItemModel*>(index.model()));
completerFilter->setFilterKeyColumn(index.column());
completer->setModel(completerFilter);
// Complete on this column, using a popup and case-insensitively.
completer->setCompletionColumn(index.column());
completer->setCompletionMode(QCompleter::InlineCompletion);
completer->setCompletionMode(QCompleter::PopupCompletion);
completer->setCaseSensitivity(Qt::CaseInsensitive);
editor->setCompleter(completer);
}
// Set the maximum length to the highest possible value instead of the default 32768.
Expand Down
13 changes: 13 additions & 0 deletions src/ExtendedTableWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,25 @@
#include <QDropEvent>
#include <QDragMoveEvent>
#include <QStyledItemDelegate>
#include <QSortFilterProxyModel>

class QMenu;
class QMimeData;
class FilterTableHeader;
namespace sqlb { class ObjectIdentifier; }

// Filter proxy model that only accepts distinct non-empty values.
class UniqueFilterModel : public QSortFilterProxyModel
{
Q_OBJECT

public:
explicit UniqueFilterModel(QObject* parent = nullptr);
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
private:
QSet<QString> m_uniqueValues;
};

// We use this class to provide editor widgets for the ExtendedTableWidget. It's used for every cell in the table view.
class ExtendedTableWidgetEditorDelegate : public QStyledItemDelegate
{
Expand Down

0 comments on commit 88d1cbc

Please sign in to comment.