From e8af8cc4b1ff63160548833c1172d46c0640cb72 Mon Sep 17 00:00:00 2001 From: Lioncache Date: Mon, 8 Dec 2025 14:51:33 -0500 Subject: [PATCH] CWorldTreeModel: Use built-in filtering facilities The ability to filter via a string is already built into the proxy model. This also extends the search bar to allow general regular expressions as well out of the box without any additional work. --- src/Editor/WorldEditor/CWorldInfoSidebar.cpp | 4 ++-- src/Editor/WorldEditor/CWorldTreeModel.cpp | 16 ++++++++++------ src/Editor/WorldEditor/CWorldTreeModel.h | 7 ------- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/Editor/WorldEditor/CWorldInfoSidebar.cpp b/src/Editor/WorldEditor/CWorldInfoSidebar.cpp index 9d4fd544..86358b17 100644 --- a/src/Editor/WorldEditor/CWorldInfoSidebar.cpp +++ b/src/Editor/WorldEditor/CWorldInfoSidebar.cpp @@ -56,7 +56,7 @@ void CWorldInfoSidebar::OnActiveProjectChanged(const CGameProject* pProj) mpUI->WorldInfoWidget->setHidden(true); mpUI->AreaInfoWidget->setHidden(true); mpUI->AreaSearchLineEdit->clear(); - mProxyModel.SetFilterString({}); + mProxyModel.setFilterRegularExpression(QString()); mpUI->GameNameLabel->setText(pProj ? TO_QSTRING(pProj->Name()) : QString{}); @@ -82,7 +82,7 @@ void CWorldInfoSidebar::OnActiveProjectChanged(const CGameProject* pProj) void CWorldInfoSidebar::OnAreaFilterStringChanged(const QString& rkFilter) { - mProxyModel.SetFilterString(rkFilter); + mProxyModel.setFilterRegularExpression(QRegularExpression(rkFilter, QRegularExpression::CaseInsensitiveOption)); // Expand top-level items that contain matches for the new filter string int NumTopLevel = mModel.rowCount(QModelIndex()); diff --git a/src/Editor/WorldEditor/CWorldTreeModel.cpp b/src/Editor/WorldEditor/CWorldTreeModel.cpp index 73c2904e..a3265c86 100644 --- a/src/Editor/WorldEditor/CWorldTreeModel.cpp +++ b/src/Editor/WorldEditor/CWorldTreeModel.cpp @@ -362,16 +362,20 @@ bool CWorldTreeProxyModel::lessThan(const QModelIndex& rkSourceLeft, const QMode bool CWorldTreeProxyModel::filterAcceptsRow(int SourceRow, const QModelIndex& rkSourceParent) const { // Always accept worlds - if (!rkSourceParent.isValid() || mFilterString.isEmpty()) + if (!rkSourceParent.isValid()) return true; - const CWorldTreeModel *pModel = qobject_cast(sourceModel()); - ASSERT(pModel != nullptr); + const auto filterExpression = filterRegularExpression(); + if (filterExpression.pattern().isEmpty()) + return true; - for (int iCol = 0; iCol < pModel->columnCount(rkSourceParent); iCol++) + const auto* model = qobject_cast(sourceModel()); + ASSERT(model != nullptr); + + for (int column = 0; column < model->columnCount(rkSourceParent); column++) { - const QModelIndex Index = pModel->index(SourceRow, iCol, rkSourceParent); - if (pModel->data(Index, Qt::DisplayRole).toString().contains(mFilterString, Qt::CaseInsensitive)) + const QModelIndex index = model->index(SourceRow, column, rkSourceParent); + if (model->data(index, Qt::DisplayRole).toString().contains(filterExpression)) return true; } diff --git a/src/Editor/WorldEditor/CWorldTreeModel.h b/src/Editor/WorldEditor/CWorldTreeModel.h index 94f5a1f1..0730f355 100644 --- a/src/Editor/WorldEditor/CWorldTreeModel.h +++ b/src/Editor/WorldEditor/CWorldTreeModel.h @@ -51,17 +51,10 @@ public slots: class CWorldTreeProxyModel : public QSortFilterProxyModel { Q_OBJECT - QString mFilterString; public: bool lessThan(const QModelIndex& rkSourceLeft, const QModelIndex& rkSourceRight) const override; bool filterAcceptsRow(int SourceRow, const QModelIndex& rkSourceParent) const override; - - void SetFilterString(const QString& rkFilter) - { - mFilterString = rkFilter; - invalidate(); - } }; #endif // CWORLDTREEMODEL_H