Switched to Qt RegularExpression for asset search

This commit is contained in:
GPayne
2023-08-10 05:59:20 -06:00
parent 399dc323af
commit 4433abdf79
2 changed files with 18 additions and 8 deletions

View File

@@ -27,13 +27,14 @@
#include <QDialog>
#include <QSortFilterProxyModel>
#include <QRegExp>
#include <QRegularExpression>
#include "assettreemodel.h"
class QTextEdit;
class QTreeView;
class SearchProxyModel : public QSortFilterProxyModel {
Q_OBJECT
public:
/**
* Constructor for SearchProxyModel class
@@ -42,17 +43,19 @@ public:
*/
SearchProxyModel(QObject* parent = nullptr) : QSortFilterProxyModel(parent) {}
public slots:
/**
* Sets the regular expression pattern to apply to the filter
*
* \param pattern The QString reference containing the regex pattern
*/
void setFilterRegExp(const QString& pattern);
void setFilterRegularExpression(const QString& pattern);
protected:
bool filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const override;
private:
QRegularExpression* _regExPattern = nullptr;
bool acceptIndex(const QModelIndex& idx) const;
};

View File

@@ -296,14 +296,17 @@ void AssetsDialog::searchTextChanged(const QString& text) {
}
else {
_assetTree->setModel(_searchProxyModel);
_assetProxyModel->setFilterRegExp(text);
_searchProxyModel->setFilterRegularExpression(text);
_assetTree->expandAll();
}
}
void SearchProxyModel::setFilterRegExp(const QString& pattern) {
QRegExp regex(pattern, Qt::CaseInsensitive, QRegExp::FixedString);
QSortFilterProxyModel::setFilterRegExp(regex);
void SearchProxyModel::setFilterRegularExpression(const QString& pattern) {
_regExPattern = new QRegularExpression(
pattern,
QRegularExpression::CaseInsensitiveOption
);
QSortFilterProxyModel::setFilterRegularExpression(*_regExPattern);
}
bool SearchProxyModel::filterAcceptsRow(int sourceRow,
@@ -316,7 +319,11 @@ bool SearchProxyModel::filterAcceptsRow(int sourceRow,
bool SearchProxyModel::acceptIndex(const QModelIndex& idx) const {
if (idx.isValid()) {
QString text = idx.data(Qt::DisplayRole).toString();
if (filterRegExp().indexIn(text) >= 0) {
if (!_regExPattern) {
return false;
}
QRegularExpressionMatchIterator matchIterator = _regExPattern->globalMatch(text);
if (matchIterator.hasNext()) {
return true;
}
for (int row = 0; row < idx.model()->rowCount(idx); ++row) {