cmake-gui: do not set search filter if regex is invalid

Fixes: #24248
This commit is contained in:
Kyle Edwards 2022-12-14 13:33:00 -05:00 committed by Brad King
parent dcb1c9c080
commit cb00fe0892
6 changed files with 56 additions and 18 deletions

View File

@ -1344,7 +1344,8 @@ void CMakeSetupDialog::showUserChanges()
void CMakeSetupDialog::setSearchFilter(const QString& str)
{
this->CacheValues->selectionModel()->clear();
this->CacheValues->setSearchFilter(str);
const bool valid = this->CacheValues->setSearchFilter(str);
QtCMake::setSearchFilterColor(this->Search, valid);
}
void CMakeSetupDialog::doOutputContextMenu(QPoint pt)

View File

@ -2,6 +2,7 @@
file Copyright.txt or https://cmake.org/licensing for details. */
#include "EnvironmentDialog.h"
#include "QCMakeWidgets.h"
#include <QDialogButtonBox>
#include <QGridLayout>
#include <QItemSelectionModel>
@ -110,14 +111,11 @@ EnvironmentDialog::EnvironmentDialog(const QProcessEnvironment& environment,
&EnvironmentDialog::addEntry);
QObject::connect(this->RemoveEntry, &QAbstractButton::clicked, this,
&EnvironmentDialog::removeSelectedEntries);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
QObject::connect(this->Search, &QLineEdit::textChanged, this->m_filter,
QOverload<const QString&>::of(
&EnvironmentSearchFilter::setFilterRegularExpression));
#else
QObject::connect(this->Search, &QLineEdit::textChanged, this->m_filter,
&EnvironmentSearchFilter::setFilterFixedString);
#endif
QObject::connect(
this->Search, &QLineEdit::textChanged, [this](const QString& text) {
const bool valid = QtCMake::setSearchFilter(this->m_filter, text);
QtCMake::setSearchFilterColor(this->Search, valid);
});
QObject::connect(this->Environment->selectionModel(),
&QItemSelectionModel::selectionChanged, this,
&EnvironmentDialog::selectionChanged);

View File

@ -167,13 +167,9 @@ bool QCMakeCacheView::showAdvanced() const
return this->AdvancedFilter->showAdvanced();
}
void QCMakeCacheView::setSearchFilter(const QString& s)
bool QCMakeCacheView::setSearchFilter(const QString& s)
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
this->SearchFilter->setFilterRegularExpression(s);
#else
this->SearchFilter->setFilterFixedString(s);
#endif
return QtCMake::setSearchFilter(this->SearchFilter, s);
}
QCMakeCacheModel::QCMakeCacheModel(QObject* p)

View File

@ -28,12 +28,13 @@ public:
QSize sizeHint() const { return QSize(200, 200); }
// set the search filter string. any property key or value not matching will
// be filtered out
bool setSearchFilter(const QString&);
public slots:
// set whether to show advanced entries
void setShowAdvanced(bool);
// set the search filter string. any property key or value not matching will
// be filtered out
void setSearchFilter(const QString&);
protected:
QModelIndex moveCursor(CursorAction, Qt::KeyboardModifiers);

View File

@ -10,8 +10,13 @@
#include <QFileDialog>
#include <QFileInfo>
#include <QResizeEvent>
#include <QSortFilterProxyModel>
#include <QToolButton>
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
# include <QRegularExpression>
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
# include <QFileSystemModel>
#else
@ -155,3 +160,32 @@ QString QCMakeFileCompleter::pathFromIndex(const QModelIndex& idx) const
{
return QDir::fromNativeSeparators(QCompleter::pathFromIndex(idx));
}
namespace QtCMake {
bool setSearchFilter(QSortFilterProxyModel* model, const QString& searchString)
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
QRegularExpression const regex(searchString,
QRegularExpression::CaseInsensitiveOption |
QRegularExpression::DontCaptureOption);
if (regex.isValid()) {
model->setFilterRegularExpression(regex);
return true;
}
model->setFilterFixedString(QString{});
return false;
#else
model->setFilterFixedString(searchString);
return true;
#endif
}
void setSearchFilterColor(QLineEdit* edit, bool valid)
{
QPalette palette;
if (!valid) {
palette.setColor(QPalette::Base, Qt::red);
}
edit->setPalette(palette);
}
}

View File

@ -9,6 +9,7 @@
#include <QLineEdit>
class QToolButton;
class QSortFilterProxyModel;
// common widgets for Qt based CMake
@ -76,3 +77,10 @@ public:
}
}
};
namespace QtCMake {
bool setSearchFilter(QSortFilterProxyModel* model,
const QString& searchString);
void setSearchFilterColor(QLineEdit* edit, bool valid);
}