feat: add initial filtering function
Signed-off-by: Sefa Eyeoglu <contact@scrumplex.net>
This commit is contained in:
@ -20,6 +20,7 @@
|
||||
#include "InstanceList.h"
|
||||
|
||||
#include <QFont>
|
||||
#include <QRegularExpression>
|
||||
#include <QVariant>
|
||||
|
||||
InstanceTableProxyModel::InstanceTableProxyModel(QObject* parent) : QSortFilterProxyModel(parent)
|
||||
@ -79,3 +80,58 @@ QVariant InstanceTableProxyModel::data(const QModelIndex& index, int role) const
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
void InstanceTableProxyModel::setFilterQuery(const QString query)
|
||||
{
|
||||
QList<InstanceFilterQuery> foo = parseFilterQuery(query);
|
||||
setFilterQuery(foo);
|
||||
}
|
||||
|
||||
void InstanceTableProxyModel::setFilterQuery(const QList<InstanceFilterQuery> query)
|
||||
{
|
||||
m_filter = query;
|
||||
invalidateFilter();
|
||||
}
|
||||
|
||||
bool InstanceTableProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const
|
||||
{
|
||||
if (m_filter.isEmpty())
|
||||
return true;
|
||||
|
||||
for (const InstanceFilterQuery& q : m_filter) {
|
||||
const InstanceList::Column c = q.first;
|
||||
const QString query = q.second;
|
||||
|
||||
QModelIndex index = sourceModel()->index(sourceRow, c, sourceParent);
|
||||
QString content = sourceModel()->data(index).toString().toLower();
|
||||
|
||||
if (!query.isNull() && !content.contains(query))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
QList<InstanceFilterQuery> InstanceTableProxyModel::parseFilterQuery(QString query)
|
||||
{
|
||||
const QRegularExpression pattern = QRegularExpression("(?:(?<prefix>\\w+):)?(?:(?<content1>\\w+)|\"(?<content2>.+)\")");
|
||||
QList<InstanceFilterQuery> queries;
|
||||
|
||||
QRegularExpressionMatchIterator it = pattern.globalMatch(query);
|
||||
while (it.hasNext()) {
|
||||
const QRegularExpressionMatch& match = it.next();
|
||||
InstanceList::Column c = InstanceList::NameColumn;
|
||||
|
||||
QString prefix = match.captured("prefix");
|
||||
if (prefix.toLower() == "category")
|
||||
c = InstanceList::CategoryColumn;
|
||||
else if (prefix.toLower() == "version")
|
||||
c = InstanceList::GameVersionColumn;
|
||||
|
||||
QString content = match.captured("content1");
|
||||
if (content.isNull())
|
||||
content = match.captured("content2");
|
||||
|
||||
queries << std::make_pair(c, content.toLower());
|
||||
}
|
||||
return queries;
|
||||
}
|
||||
|
@ -15,18 +15,29 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "InstanceList.h"
|
||||
|
||||
#include <QCollator>
|
||||
#include <QSortFilterProxyModel>
|
||||
|
||||
typedef std::pair<InstanceList::Column, QString> InstanceFilterQuery;
|
||||
|
||||
class InstanceTableProxyModel : public QSortFilterProxyModel {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
InstanceTableProxyModel(QObject* parent = 0);
|
||||
|
||||
void setFilterQuery(const QString query);
|
||||
void setFilterQuery(const QList<InstanceFilterQuery> query);
|
||||
|
||||
static QList<InstanceFilterQuery> parseFilterQuery(const QString query);
|
||||
|
||||
protected:
|
||||
QVariant data(const QModelIndex& index, int role) const override;
|
||||
bool filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const;
|
||||
|
||||
private:
|
||||
QCollator m_naturalSort;
|
||||
QList<InstanceFilterQuery> m_filter;
|
||||
};
|
||||
|
@ -221,6 +221,14 @@ QModelIndex InstanceView::mappedIndex(const QModelIndex& index) const
|
||||
return m_tableProxy->mapToSource(index);
|
||||
}
|
||||
|
||||
void InstanceView::setFilterQuery(const QString& query)
|
||||
{
|
||||
if (m_displayMode == DisplayMode::GridMode) {
|
||||
return m_gridProxy->setFilterQuery(query);
|
||||
}
|
||||
return m_tableProxy->setFilterQuery(query);
|
||||
}
|
||||
|
||||
void InstanceView::setCatDisplayed(bool enabled)
|
||||
{
|
||||
if (enabled) {
|
||||
|
@ -51,6 +51,7 @@ class InstanceView : public QStackedWidget {
|
||||
void storeState();
|
||||
|
||||
void setCatDisplayed(bool enabled);
|
||||
void setFilterQuery(const QString& query);
|
||||
|
||||
void editSelected(InstanceList::Column targetColumn = InstanceList::NameColumn);
|
||||
|
||||
|
Reference in New Issue
Block a user