Permalink
Browse files

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.
  • Loading branch information...
mgrojo authored and MKleusberg committed Sep 1, 2018
1 parent 04f27cc commit 88d1cbc29df359f1f7987d97b649923b7a762d57
Showing with 40 additions and 3 deletions.
  1. +27 −3 src/ExtendedTableWidget.cpp
  2. +13 −0 src/ExtendedTableWidget.h
@@ -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)
@@ -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.
@@ -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
{

0 comments on commit 88d1cbc

Please sign in to comment.