forked from qt-creator/qt-creator
VariableChooser: add filter
Change-Id: I604b39f96ff303502949d581d094067025093ca0 Reviewed-by: hjk <hjk@qt.io> Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
@@ -80,6 +80,24 @@ private:
|
|||||||
VariableChooserPrivate *m_target;
|
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
|
class VariableChooserPrivate : public QObject
|
||||||
{
|
{
|
||||||
@@ -101,6 +119,7 @@ public:
|
|||||||
void handleItemActivated(const QModelIndex &index);
|
void handleItemActivated(const QModelIndex &index);
|
||||||
void insertText(const QString &variable);
|
void insertText(const QString &variable);
|
||||||
void updatePositionAndShow(bool);
|
void updatePositionAndShow(bool);
|
||||||
|
void updateFilter(const QString &filterText);
|
||||||
|
|
||||||
QWidget *currentWidget();
|
QWidget *currentWidget();
|
||||||
|
|
||||||
@@ -116,6 +135,7 @@ public:
|
|||||||
QPointer<QPlainTextEdit> m_plainTextEdit;
|
QPointer<QPlainTextEdit> m_plainTextEdit;
|
||||||
QPointer<IconButton> m_iconButton;
|
QPointer<IconButton> m_iconButton;
|
||||||
|
|
||||||
|
QLineEdit *m_variableFilter;
|
||||||
VariableTreeView *m_variableTree;
|
VariableTreeView *m_variableTree;
|
||||||
QLabel *m_variableDescription;
|
QLabel *m_variableDescription;
|
||||||
QSortFilterProxyModel *m_sortModel;
|
QSortFilterProxyModel *m_sortModel;
|
||||||
@@ -251,18 +271,25 @@ VariableChooserPrivate::VariableChooserPrivate(VariableChooser *parent)
|
|||||||
m_textEdit(0),
|
m_textEdit(0),
|
||||||
m_plainTextEdit(0),
|
m_plainTextEdit(0),
|
||||||
m_iconButton(0),
|
m_iconButton(0),
|
||||||
|
m_variableFilter(0),
|
||||||
m_variableTree(0),
|
m_variableTree(0),
|
||||||
m_variableDescription(0)
|
m_variableDescription(0)
|
||||||
{
|
{
|
||||||
m_defaultDescription = VariableChooser::tr("Select a variable to insert.");
|
m_defaultDescription = VariableChooser::tr("Select a variable to insert.");
|
||||||
|
|
||||||
|
m_variableFilter = new QLineEdit(q);
|
||||||
m_variableTree = new VariableTreeView(q, this);
|
m_variableTree = new VariableTreeView(q, this);
|
||||||
m_variableDescription = new QLabel(q);
|
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->setSourceModel(&m_model);
|
||||||
m_sortModel->sort(0);
|
m_sortModel->sort(0);
|
||||||
|
m_sortModel->setFilterKeyColumn(0);
|
||||||
|
m_sortModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
|
||||||
m_variableTree->setModel(m_sortModel);
|
m_variableTree->setModel(m_sortModel);
|
||||||
|
|
||||||
m_variableDescription->setText(m_defaultDescription);
|
m_variableDescription->setText(m_defaultDescription);
|
||||||
m_variableDescription->setMinimumSize(QSize(0, 60));
|
m_variableDescription->setMinimumSize(QSize(0, 60));
|
||||||
m_variableDescription->setAlignment(Qt::AlignLeft|Qt::AlignTop);
|
m_variableDescription->setAlignment(Qt::AlignLeft|Qt::AlignTop);
|
||||||
@@ -272,9 +299,12 @@ VariableChooserPrivate::VariableChooserPrivate(VariableChooser *parent)
|
|||||||
|
|
||||||
QVBoxLayout *verticalLayout = new QVBoxLayout(q);
|
QVBoxLayout *verticalLayout = new QVBoxLayout(q);
|
||||||
verticalLayout->setContentsMargins(3, 3, 3, 12);
|
verticalLayout->setContentsMargins(3, 3, 3, 12);
|
||||||
|
verticalLayout->addWidget(m_variableFilter);
|
||||||
verticalLayout->addWidget(m_variableTree);
|
verticalLayout->addWidget(m_variableTree);
|
||||||
verticalLayout->addWidget(m_variableDescription);
|
verticalLayout->addWidget(m_variableDescription);
|
||||||
|
|
||||||
|
connect(m_variableFilter, &QLineEdit::textChanged,
|
||||||
|
this, &VariableChooserPrivate::updateFilter);
|
||||||
connect(m_variableTree, &QTreeView::activated,
|
connect(m_variableTree, &QTreeView::activated,
|
||||||
this, &VariableChooserPrivate::handleItemActivated);
|
this, &VariableChooserPrivate::handleItemActivated);
|
||||||
connect(qobject_cast<QApplication *>(qApp), &QApplication::focusChanged,
|
connect(qobject_cast<QApplication *>(qApp), &QApplication::focusChanged,
|
||||||
@@ -368,6 +398,7 @@ VariableChooser::VariableChooser(QWidget *parent) :
|
|||||||
setWindowFlags(Qt::Tool);
|
setWindowFlags(Qt::Tool);
|
||||||
setFocusPolicy(Qt::StrongFocus);
|
setFocusPolicy(Qt::StrongFocus);
|
||||||
setFocusProxy(d->m_variableTree);
|
setFocusProxy(d->m_variableTree);
|
||||||
|
setGeometry(QRect(0, 0, 400, 500));
|
||||||
addMacroExpanderProvider([]() { return globalMacroExpander(); });
|
addMacroExpanderProvider([]() { return globalMacroExpander(); });
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -519,6 +550,12 @@ void VariableChooserPrivate::updatePositionAndShow(bool)
|
|||||||
m_variableTree->expandAll();
|
m_variableTree->expandAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void VariableChooserPrivate::updateFilter(const QString &filterText)
|
||||||
|
{
|
||||||
|
m_sortModel->setFilterWildcard(filterText);
|
||||||
|
m_variableTree->expandAll();
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \internal
|
* \internal
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user