mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-26 16:43:55 -07:00
Add main and sub type as filter options, add helper functions to remove or get specific and all filters. (#5820)
Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
parent
83757f0ea3
commit
ebf2602f31
6 changed files with 146 additions and 1 deletions
|
|
@ -1,5 +1,44 @@
|
||||||
#include "filter_card.h"
|
#include "filter_card.h"
|
||||||
|
|
||||||
|
#include <QJsonObject>
|
||||||
|
|
||||||
|
QJsonObject CardFilter::toJson() const
|
||||||
|
{
|
||||||
|
QJsonObject obj;
|
||||||
|
obj["term"] = trm;
|
||||||
|
obj["type"] = typeName(t);
|
||||||
|
obj["attr"] = attrName(a);
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
CardFilter *CardFilter::fromJson(const QJsonObject &obj)
|
||||||
|
{
|
||||||
|
QString term = obj["term"].toString();
|
||||||
|
QString typeStr = obj["type"].toString();
|
||||||
|
QString attrStr = obj["attr"].toString();
|
||||||
|
|
||||||
|
Type type = TypeEnd;
|
||||||
|
Attr attr = AttrEnd;
|
||||||
|
|
||||||
|
// Convert type string back to enum
|
||||||
|
for (int i = 0; i < TypeEnd; i++) {
|
||||||
|
if (typeName(static_cast<Type>(i)) == typeStr) {
|
||||||
|
type = static_cast<Type>(i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert attr string back to enum
|
||||||
|
for (int i = 0; i < AttrEnd; i++) {
|
||||||
|
if (attrName(static_cast<Attr>(i)) == attrStr) {
|
||||||
|
attr = static_cast<Attr>(i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new CardFilter(term, type, attr);
|
||||||
|
}
|
||||||
|
|
||||||
const QString CardFilter::typeName(Type t)
|
const QString CardFilter::typeName(Type t)
|
||||||
{
|
{
|
||||||
switch (t) {
|
switch (t) {
|
||||||
|
|
@ -43,6 +82,10 @@ const QString CardFilter::attrName(Attr a)
|
||||||
return tr("Loyalty");
|
return tr("Loyalty");
|
||||||
case AttrFormat:
|
case AttrFormat:
|
||||||
return tr("Format");
|
return tr("Format");
|
||||||
|
case AttrMainType:
|
||||||
|
return tr("Main Type");
|
||||||
|
case AttrSubType:
|
||||||
|
return tr("Sub Type");
|
||||||
default:
|
default:
|
||||||
return QString("");
|
return QString("");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,8 +34,10 @@ public:
|
||||||
AttrText,
|
AttrText,
|
||||||
AttrTough,
|
AttrTough,
|
||||||
AttrType,
|
AttrType,
|
||||||
|
AttrMainType,
|
||||||
|
AttrSubType,
|
||||||
AttrFormat,
|
AttrFormat,
|
||||||
AttrEnd
|
AttrEnd,
|
||||||
};
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
@ -59,6 +61,8 @@ public:
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QJsonObject toJson() const;
|
||||||
|
static CardFilter *fromJson(const QJsonObject &json);
|
||||||
static const QString typeName(Type t);
|
static const QString typeName(Type t);
|
||||||
static const QString attrName(Attr a);
|
static const QString attrName(Attr a);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -158,6 +158,18 @@ bool FilterItem::acceptType(const CardInfoPtr info) const
|
||||||
return info->getCardType().contains(term, Qt::CaseInsensitive);
|
return info->getCardType().contains(term, Qt::CaseInsensitive);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool FilterItem::acceptMainType(const CardInfoPtr info) const
|
||||||
|
{
|
||||||
|
const QStringList typeParts = info->getCardType().split(" — ");
|
||||||
|
return !typeParts.isEmpty() && typeParts[0].contains(term, Qt::CaseInsensitive);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FilterItem::acceptSubType(const CardInfoPtr info) const
|
||||||
|
{
|
||||||
|
const QStringList typeParts = info->getCardType().split(" — ");
|
||||||
|
return typeParts.size() > 1 && typeParts[1].contains(term, Qt::CaseInsensitive);
|
||||||
|
}
|
||||||
|
|
||||||
bool FilterItem::acceptColor(const CardInfoPtr info) const
|
bool FilterItem::acceptColor(const CardInfoPtr info) const
|
||||||
{
|
{
|
||||||
QString converted_term = term.trimmed();
|
QString converted_term = term.trimmed();
|
||||||
|
|
@ -411,6 +423,10 @@ bool FilterItem::acceptCardAttr(const CardInfoPtr info, CardFilter::Attr attr) c
|
||||||
return acceptLoyalty(info);
|
return acceptLoyalty(info);
|
||||||
case CardFilter::AttrFormat:
|
case CardFilter::AttrFormat:
|
||||||
return acceptFormat(info);
|
return acceptFormat(info);
|
||||||
|
case CardFilter::AttrMainType:
|
||||||
|
return acceptMainType(info);
|
||||||
|
case CardFilter::AttrSubType:
|
||||||
|
return acceptSubType(info);
|
||||||
default:
|
default:
|
||||||
return true; /* ignore this attribute */
|
return true; /* ignore this attribute */
|
||||||
}
|
}
|
||||||
|
|
@ -504,6 +520,17 @@ bool FilterTree::acceptsCard(const CardInfoPtr info) const
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void FilterTree::removeFiltersByAttr(CardFilter::Attr filterType)
|
||||||
|
{
|
||||||
|
for (int i = childNodes.size() - 1; i >= 0; --i) {
|
||||||
|
auto *child = dynamic_cast<LogicMap *>(childNodes.at(i));
|
||||||
|
|
||||||
|
if (child && child->attr == filterType) {
|
||||||
|
deleteAt(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void FilterTree::clear()
|
void FilterTree::clear()
|
||||||
{
|
{
|
||||||
while (childCount() > 0) {
|
while (childCount() > 0) {
|
||||||
|
|
|
||||||
|
|
@ -97,6 +97,7 @@ protected:
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual ~FilterTreeBranch();
|
virtual ~FilterTreeBranch();
|
||||||
|
void removeFiltersByAttr(CardFilter::Attr filterType);
|
||||||
FilterTreeNode *nodeAt(int i) const override;
|
FilterTreeNode *nodeAt(int i) const override;
|
||||||
void deleteAt(int i) override;
|
void deleteAt(int i) override;
|
||||||
int childCount() const override
|
int childCount() const override
|
||||||
|
|
@ -198,6 +199,8 @@ public:
|
||||||
|
|
||||||
bool acceptName(CardInfoPtr info) const;
|
bool acceptName(CardInfoPtr info) const;
|
||||||
bool acceptType(CardInfoPtr info) const;
|
bool acceptType(CardInfoPtr info) const;
|
||||||
|
bool acceptMainType(CardInfoPtr info) const;
|
||||||
|
bool acceptSubType(CardInfoPtr info) const;
|
||||||
bool acceptColor(CardInfoPtr info) const;
|
bool acceptColor(CardInfoPtr info) const;
|
||||||
bool acceptText(CardInfoPtr info) const;
|
bool acceptText(CardInfoPtr info) const;
|
||||||
bool acceptSet(CardInfoPtr info) const;
|
bool acceptSet(CardInfoPtr info) const;
|
||||||
|
|
@ -266,6 +269,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
bool acceptsCard(CardInfoPtr info) const;
|
bool acceptsCard(CardInfoPtr info) const;
|
||||||
|
void removeFiltersByAttr(CardFilter::Attr filterType);
|
||||||
void clear();
|
void clear();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -78,6 +78,62 @@ void FilterTreeModel::addFilter(const CardFilter *f)
|
||||||
emit layoutChanged();
|
emit layoutChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void FilterTreeModel::clearFiltersOfType(CardFilter::Attr filterType)
|
||||||
|
{
|
||||||
|
emit layoutAboutToBeChanged();
|
||||||
|
|
||||||
|
// Recursively remove all nodes with the given filter type
|
||||||
|
fTree->removeFiltersByAttr(filterType);
|
||||||
|
|
||||||
|
emit layoutChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
QList<const CardFilter *> FilterTreeModel::getFiltersOfType(CardFilter::Attr filterType) const
|
||||||
|
{
|
||||||
|
QList<const CardFilter *> filters;
|
||||||
|
if (!fTree) {
|
||||||
|
return filters;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::function<void(const FilterTreeNode *)> traverse;
|
||||||
|
traverse = [&](const FilterTreeNode *node) {
|
||||||
|
if (const auto *filterItem = dynamic_cast<const FilterItem *>(node)) {
|
||||||
|
if (filterItem->attr() == filterType) {
|
||||||
|
QString text = filterItem->text();
|
||||||
|
filters.append(new CardFilter(text, filterItem->type(), filterItem->attr()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = 0; i < node->childCount(); ++i) {
|
||||||
|
traverse(node->nodeAt(i));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
traverse(fTree);
|
||||||
|
return filters;
|
||||||
|
}
|
||||||
|
|
||||||
|
QList<const CardFilter *> FilterTreeModel::allFilters() const
|
||||||
|
{
|
||||||
|
QList<const CardFilter *> filters;
|
||||||
|
if (!fTree) {
|
||||||
|
return filters;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::function<void(const FilterTreeNode *)> traverse;
|
||||||
|
traverse = [&](const FilterTreeNode *node) {
|
||||||
|
if (const auto *filterItem = dynamic_cast<const FilterItem *>(node)) {
|
||||||
|
QString text = filterItem->text();
|
||||||
|
filters.append(new CardFilter(text, filterItem->type(), filterItem->attr()));
|
||||||
|
}
|
||||||
|
for (int i = 0; i < node->childCount(); ++i) {
|
||||||
|
traverse(node->nodeAt(i));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
traverse(fTree);
|
||||||
|
return filters;
|
||||||
|
}
|
||||||
|
|
||||||
int FilterTreeModel::rowCount(const QModelIndex &parent) const
|
int FilterTreeModel::rowCount(const QModelIndex &parent) const
|
||||||
{
|
{
|
||||||
const FilterTreeNode *node;
|
const FilterTreeNode *node;
|
||||||
|
|
@ -257,3 +313,8 @@ bool FilterTreeModel::removeRows(int row, int count, const QModelIndex &parent)
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void FilterTreeModel::clear()
|
||||||
|
{
|
||||||
|
fTree->clear();
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
#ifndef FILTERTREEMODEL_H
|
#ifndef FILTERTREEMODEL_H
|
||||||
#define FILTERTREEMODEL_H
|
#define FILTERTREEMODEL_H
|
||||||
|
|
||||||
|
#include "filter_card.h"
|
||||||
|
|
||||||
#include <QAbstractItemModel>
|
#include <QAbstractItemModel>
|
||||||
|
|
||||||
class FilterTree;
|
class FilterTree;
|
||||||
|
|
@ -15,6 +17,9 @@ private:
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void addFilter(const CardFilter *f);
|
void addFilter(const CardFilter *f);
|
||||||
|
void clearFiltersOfType(CardFilter::Attr filterType);
|
||||||
|
QList<const CardFilter *> getFiltersOfType(CardFilter::Attr filterType) const;
|
||||||
|
QList<const CardFilter *> allFilters() const;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void proxyBeginInsertRow(const FilterTreeNode *, int);
|
void proxyBeginInsertRow(const FilterTreeNode *, int);
|
||||||
|
|
@ -41,6 +46,7 @@ public:
|
||||||
QModelIndex parent(const QModelIndex &ind) const override;
|
QModelIndex parent(const QModelIndex &ind) const override;
|
||||||
QModelIndex index(int row, int column, const QModelIndex &parent) const override;
|
QModelIndex index(int row, int column, const QModelIndex &parent) const override;
|
||||||
bool removeRows(int row, int count, const QModelIndex &parent) override;
|
bool removeRows(int row, int count, const QModelIndex &parent) override;
|
||||||
|
void clear();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue