miscellaneous code cleanup

renamed filter list modules to filter tree to more accurately reflect
the nature of the data structure.
This commit is contained in:
sylvanbasilisk 2014-01-22 20:52:52 +00:00
parent 083005b8a9
commit 16d30fb9e1
13 changed files with 197 additions and 206 deletions

165
cockatrice/src/filtertree.h Normal file
View file

@ -0,0 +1,165 @@
#ifndef FILTERTREE_H
#define FILTERTREE_H
#include <QList>
#include <QMap>
#include <QObject>
#include "cardfilter.h"
class CardInfo;
class FilterTreeNode {
private:
bool enabled;
public:
FilterTreeNode() : enabled(true) {}
virtual bool isEnabled() const { return enabled; }
virtual void enable() { enabled = true; nodeChanged(); }
virtual void disable() { enabled = false; nodeChanged(); }
virtual FilterTreeNode *parent() const { return NULL; }
virtual FilterTreeNode *nodeAt(int i) const { return NULL; }
virtual void deleteAt(int i) {}
virtual int childCount() const { return 0; }
virtual int childIndex(const FilterTreeNode *node) const { return -1; }
virtual int index() const { return (parent() != NULL)? parent()->childIndex(this) : -1; }
virtual QString text() const { return ""; }
virtual bool isLeaf() const { return false; }
virtual const char *textCStr() const { return text().toStdString().c_str(); }
virtual void nodeChanged() const {
printf("%s -> ", textCStr());
if (parent() != NULL) parent()->nodeChanged();
}
virtual void preInsertChild(const FilterTreeNode *p, int i) const {
//printf("%s -> ", textCStr());
if (parent() != NULL) parent()->preInsertChild(p, i);
}
virtual void postInsertChild(const FilterTreeNode *p, int i) const {
//printf("%s -> ", textCStr());
if (parent() != NULL) parent()->postInsertChild(p, i);
}
virtual void preRemoveChild(const FilterTreeNode *p, int i) const {
printf("%s -> ", textCStr());
if (parent() != NULL) parent()->preRemoveChild(p, i);
}
virtual void postRemoveChild(const FilterTreeNode *p, int i) const {
printf("%s -> ", textCStr());
if (parent() != NULL) parent()->postRemoveChild(p, i);
}
};
template <class T>
class FilterTreeBranch : public FilterTreeNode {
protected:
QList<T> childNodes;
public:
~FilterTreeBranch();
FilterTreeNode *nodeAt(int i) const;
void deleteAt(int i);
int childCount() const { return childNodes.size(); }
int childIndex(const FilterTreeNode *node) const;
};
class FilterItemList;
class FilterTree;
class LogicMap : public FilterTreeBranch<FilterItemList *> {
private:
FilterTree *const p;
public:
const CardFilter::Attr attr;
LogicMap(CardFilter::Attr a, FilterTree *parent)
: attr(a), p(parent) {}
const FilterItemList *findTypeList(CardFilter::Type type) const;
FilterItemList *typeList(CardFilter::Type type);
FilterTreeNode *parent() const;
QString text() const { return QString(CardFilter::attrName(attr)); }
};
class FilterItem;
class FilterItemList : public FilterTreeBranch<FilterItem *> {
private:
LogicMap *const p;
public:
const CardFilter::Type type;
FilterItemList(CardFilter::Type t, LogicMap *parent)
: type(t), p(parent) {}
CardFilter::Attr attr() const { return p->attr; }
FilterTreeNode *parent() const { return p; }
int termIndex(const QString &term) const;
FilterTreeNode *termNode(const QString &term);
QString text() const { return QString(CardFilter::typeName(type)); }
bool testTypeAnd(const CardInfo *info, CardFilter::Attr attr) const;
bool testTypeAndNot(const CardInfo *info, CardFilter::Attr attr) const;
bool testTypeOr(const CardInfo *info, CardFilter::Attr attr) const;
bool testTypeOrNot(const CardInfo *info, CardFilter::Attr attr) const;
};
class FilterItem : public FilterTreeNode {
private:
FilterItemList *const p;
public:
const QString term;
FilterItem(QString trm, FilterItemList *parent)
: p(parent), term(trm) {}
CardFilter::Attr attr() const { return p->attr(); }
CardFilter::Type type() const { return p->type; }
FilterTreeNode *parent() const { return p; }
QString text() const { return term; }
bool isLeaf() const { return true; }
bool acceptName(const CardInfo *info) const;
bool acceptType(const CardInfo *info) const;
bool acceptColor(const CardInfo *info) const;
bool acceptText(const CardInfo *info) const;
bool acceptSet(const CardInfo *info) const;
bool acceptManaCost(const CardInfo *info) const;
bool acceptCardAttr(const CardInfo *info, CardFilter::Attr attr) const;
};
class FilterTree : public QObject, public FilterTreeBranch<LogicMap *> {
Q_OBJECT
signals:
void preInsertRow(const FilterTreeNode *parent, int i) const;
void postInsertRow(const FilterTreeNode *parent, int i) const;
void preRemoveRow(const FilterTreeNode *parent, int i) const;
void postRemoveRow(const FilterTreeNode *parent, int i) const;
void changed() const;
private:
LogicMap *attrLogicMap(CardFilter::Attr attr);
FilterItemList *attrTypeList(CardFilter::Attr attr,
CardFilter::Type type);
bool testAttr(const CardInfo *info, const LogicMap *lm) const;
public:
FilterTree();
~FilterTree();
int findTermIndex(CardFilter::Attr attr, CardFilter::Type type,
const QString &term);
int findTermIndex(const CardFilter *f);
FilterTreeNode *termNode(CardFilter::Attr attr, CardFilter::Type type,
const QString &term);
FilterTreeNode *termNode(const CardFilter *f);
FilterTreeNode *attrTypeNode(CardFilter::Attr attr,
CardFilter::Type type);
QString text() const { return QString("root"); }
int index() const { return 0; }
void nodeChanged() const { printf("root\n"); emit changed(); }
void preInsertChild(const FilterTreeNode *p, int i) const { emit preInsertRow(p, i); }
void postInsertChild(const FilterTreeNode *p, int i) const { emit postInsertRow(p, i); }
void preRemoveChild(const FilterTreeNode *p, int i) const { printf("root\n"); emit preRemoveRow(p, i); }
void postRemoveChild(const FilterTreeNode *p, int i) const { printf("root\n"); emit postRemoveRow(p, i); }
bool acceptsCard(const CardInfo *info) const;
};
#endif