mirror of
https://github.com/AxioDL/PrimeWorldEditor.git
synced 2025-12-17 17:05:37 +00:00
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.
This commit is contained in:
@@ -56,7 +56,7 @@ void CWorldInfoSidebar::OnActiveProjectChanged(const CGameProject* pProj)
|
|||||||
mpUI->WorldInfoWidget->setHidden(true);
|
mpUI->WorldInfoWidget->setHidden(true);
|
||||||
mpUI->AreaInfoWidget->setHidden(true);
|
mpUI->AreaInfoWidget->setHidden(true);
|
||||||
mpUI->AreaSearchLineEdit->clear();
|
mpUI->AreaSearchLineEdit->clear();
|
||||||
mProxyModel.SetFilterString({});
|
mProxyModel.setFilterRegularExpression(QString());
|
||||||
|
|
||||||
mpUI->GameNameLabel->setText(pProj ? TO_QSTRING(pProj->Name()) : 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)
|
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
|
// Expand top-level items that contain matches for the new filter string
|
||||||
int NumTopLevel = mModel.rowCount(QModelIndex());
|
int NumTopLevel = mModel.rowCount(QModelIndex());
|
||||||
|
|||||||
@@ -362,16 +362,20 @@ bool CWorldTreeProxyModel::lessThan(const QModelIndex& rkSourceLeft, const QMode
|
|||||||
bool CWorldTreeProxyModel::filterAcceptsRow(int SourceRow, const QModelIndex& rkSourceParent) const
|
bool CWorldTreeProxyModel::filterAcceptsRow(int SourceRow, const QModelIndex& rkSourceParent) const
|
||||||
{
|
{
|
||||||
// Always accept worlds
|
// Always accept worlds
|
||||||
if (!rkSourceParent.isValid() || mFilterString.isEmpty())
|
if (!rkSourceParent.isValid())
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
const CWorldTreeModel *pModel = qobject_cast<CWorldTreeModel*>(sourceModel());
|
const auto filterExpression = filterRegularExpression();
|
||||||
ASSERT(pModel != nullptr);
|
if (filterExpression.pattern().isEmpty())
|
||||||
|
return true;
|
||||||
|
|
||||||
for (int iCol = 0; iCol < pModel->columnCount(rkSourceParent); iCol++)
|
const auto* model = qobject_cast<CWorldTreeModel*>(sourceModel());
|
||||||
|
ASSERT(model != nullptr);
|
||||||
|
|
||||||
|
for (int column = 0; column < model->columnCount(rkSourceParent); column++)
|
||||||
{
|
{
|
||||||
const QModelIndex Index = pModel->index(SourceRow, iCol, rkSourceParent);
|
const QModelIndex index = model->index(SourceRow, column, rkSourceParent);
|
||||||
if (pModel->data(Index, Qt::DisplayRole).toString().contains(mFilterString, Qt::CaseInsensitive))
|
if (model->data(index, Qt::DisplayRole).toString().contains(filterExpression))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -51,17 +51,10 @@ public slots:
|
|||||||
class CWorldTreeProxyModel : public QSortFilterProxyModel
|
class CWorldTreeProxyModel : public QSortFilterProxyModel
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
QString mFilterString;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
bool lessThan(const QModelIndex& rkSourceLeft, const QModelIndex& rkSourceRight) const override;
|
bool lessThan(const QModelIndex& rkSourceLeft, const QModelIndex& rkSourceRight) const override;
|
||||||
bool filterAcceptsRow(int SourceRow, const QModelIndex& rkSourceParent) const override;
|
bool filterAcceptsRow(int SourceRow, const QModelIndex& rkSourceParent) const override;
|
||||||
|
|
||||||
void SetFilterString(const QString& rkFilter)
|
|
||||||
{
|
|
||||||
mFilterString = rkFilter;
|
|
||||||
invalidate();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // CWORLDTREEMODEL_H
|
#endif // CWORLDTREEMODEL_H
|
||||||
|
|||||||
Reference in New Issue
Block a user