summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSerhii Moroz <frost.asm@gmail.com>2017-02-13 21:46:27 +0200
committerSerhii Moroz <frost.asm@gmail.com>2017-02-15 10:26:24 +0000
commit3c91c83f41412cb778df404eb54c94ac31a6d421 (patch)
tree230ef6e42cd97184a039e7f75df848b69b833e3e
parentd6a84fe22c90b5cdb9ce2ebdd88b48d6d6c04ff1 (diff)
downloadqt-creator-3c91c83f41412cb778df404eb54c94ac31a6d421.tar.gz
VariableChooser: add filter
Change-Id: I604b39f96ff303502949d581d094067025093ca0 Reviewed-by: hjk <hjk@qt.io> Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
-rw-r--r--src/plugins/coreplugin/variablechooser.cpp39
1 files changed, 38 insertions, 1 deletions
diff --git a/src/plugins/coreplugin/variablechooser.cpp b/src/plugins/coreplugin/variablechooser.cpp
index 1fdf68512c..caef5a9973 100644
--- a/src/plugins/coreplugin/variablechooser.cpp
+++ b/src/plugins/coreplugin/variablechooser.cpp
@@ -80,6 +80,24 @@ private:
VariableChooserPrivate *m_target;
};
+class VariableSortFilterProxyModel : public QSortFilterProxyModel
+{
+public:
+ explicit VariableSortFilterProxyModel(QObject *parent) : QSortFilterProxyModel(parent) {}
+ bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
+ {
+ const QModelIndex index = sourceModel()->index(sourceRow, filterKeyColumn(), sourceParent);
+ if (!index.isValid())
+ return false;
+
+ const QRegExp regexp = filterRegExp();
+ if (regexp.isEmpty() || sourceModel()->rowCount(index) > 0)
+ return true;
+
+ const QString displayText = index.data(Qt::DisplayRole).toString();
+ return displayText.contains(regexp);
+ }
+};
class VariableChooserPrivate : public QObject
{
@@ -101,6 +119,7 @@ public:
void handleItemActivated(const QModelIndex &index);
void insertText(const QString &variable);
void updatePositionAndShow(bool);
+ void updateFilter(const QString &filterText);
QWidget *currentWidget();
@@ -116,6 +135,7 @@ public:
QPointer<QPlainTextEdit> m_plainTextEdit;
QPointer<IconButton> m_iconButton;
+ QLineEdit *m_variableFilter;
VariableTreeView *m_variableTree;
QLabel *m_variableDescription;
QSortFilterProxyModel *m_sortModel;
@@ -251,18 +271,25 @@ VariableChooserPrivate::VariableChooserPrivate(VariableChooser *parent)
m_textEdit(0),
m_plainTextEdit(0),
m_iconButton(0),
+ m_variableFilter(0),
m_variableTree(0),
m_variableDescription(0)
{
m_defaultDescription = VariableChooser::tr("Select a variable to insert.");
+ m_variableFilter = new QLineEdit(q);
m_variableTree = new VariableTreeView(q, this);
m_variableDescription = new QLabel(q);
- m_sortModel = new QSortFilterProxyModel(this);
+ m_variableFilter->setPlaceholderText(VariableChooser::tr("Filter"));
+
+ m_sortModel = new VariableSortFilterProxyModel(this);
m_sortModel->setSourceModel(&m_model);
m_sortModel->sort(0);
+ m_sortModel->setFilterKeyColumn(0);
+ m_sortModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
m_variableTree->setModel(m_sortModel);
+
m_variableDescription->setText(m_defaultDescription);
m_variableDescription->setMinimumSize(QSize(0, 60));
m_variableDescription->setAlignment(Qt::AlignLeft|Qt::AlignTop);
@@ -272,9 +299,12 @@ VariableChooserPrivate::VariableChooserPrivate(VariableChooser *parent)
QVBoxLayout *verticalLayout = new QVBoxLayout(q);
verticalLayout->setContentsMargins(3, 3, 3, 12);
+ verticalLayout->addWidget(m_variableFilter);
verticalLayout->addWidget(m_variableTree);
verticalLayout->addWidget(m_variableDescription);
+ connect(m_variableFilter, &QLineEdit::textChanged,
+ this, &VariableChooserPrivate::updateFilter);
connect(m_variableTree, &QTreeView::activated,
this, &VariableChooserPrivate::handleItemActivated);
connect(qobject_cast<QApplication *>(qApp), &QApplication::focusChanged,
@@ -368,6 +398,7 @@ VariableChooser::VariableChooser(QWidget *parent) :
setWindowFlags(Qt::Tool);
setFocusPolicy(Qt::StrongFocus);
setFocusProxy(d->m_variableTree);
+ setGeometry(QRect(0, 0, 400, 500));
addMacroExpanderProvider([]() { return globalMacroExpander(); });
}
@@ -519,6 +550,12 @@ void VariableChooserPrivate::updatePositionAndShow(bool)
m_variableTree->expandAll();
}
+void VariableChooserPrivate::updateFilter(const QString &filterText)
+{
+ m_sortModel->setFilterWildcard(filterText);
+ m_variableTree->expandAll();
+}
+
/*!
* \internal
*/