change tabs into 4 spaces in new files

This commit is contained in:
sylvanbasilisk 2014-03-25 02:28:42 +00:00
parent 0770626270
commit cd4d04be3e
16 changed files with 759 additions and 759 deletions

View file

@ -2,36 +2,36 @@
const char *CardFilter::typeName(Type t) const char *CardFilter::typeName(Type t)
{ {
switch (t) { switch (t) {
case TypeAnd: case TypeAnd:
return "and"; return "and";
case TypeOr: case TypeOr:
return "or"; return "or";
case TypeAndNot: case TypeAndNot:
return "and not"; return "and not";
case TypeOrNot: case TypeOrNot:
return "or not"; return "or not";
default: default:
return ""; return "";
} }
} }
const char *CardFilter::attrName(Attr a) const char *CardFilter::attrName(Attr a)
{ {
switch (a) { switch (a) {
case AttrName: case AttrName:
return "name"; return "name";
case AttrType: case AttrType:
return "type"; return "type";
case AttrColor: case AttrColor:
return "color"; return "color";
case AttrText: case AttrText:
return "text"; return "text";
case AttrSet: case AttrSet:
return "set"; return "set";
case AttrManaCost: case AttrManaCost:
return "mana cost"; return "mana cost";
default: default:
return ""; return "";
} }
} }

View file

@ -5,40 +5,40 @@
class CardFilter { class CardFilter {
public: public:
enum Type { enum Type {
TypeAnd = 0, TypeAnd = 0,
TypeOr, TypeOr,
TypeAndNot, TypeAndNot,
TypeOrNot, TypeOrNot,
TypeEnd TypeEnd
}; };
/* if you add an atribute here you also need to /* if you add an atribute here you also need to
* add its string representation in attrName */ * add its string representation in attrName */
enum Attr { enum Attr {
AttrName = 0, AttrName = 0,
AttrType, AttrType,
AttrColor, AttrColor,
AttrText, AttrText,
AttrSet, AttrSet,
AttrManaCost, AttrManaCost,
AttrEnd AttrEnd
}; };
private: private:
enum Type t; enum Type t;
enum Attr a; enum Attr a;
QString trm; QString trm;
public: public:
CardFilter(QString term, Type type, Attr attr) : trm(term), t(type), a(attr) {}; CardFilter(QString term, Type type, Attr attr) : trm(term), t(type), a(attr) {};
Type type() const { return t; } Type type() const { return t; }
const QString &term() const { return trm; } const QString &term() const { return trm; }
Attr attr() const { return a; } Attr attr() const { return a; }
static const char *typeName(Type t); static const char *typeName(Type t);
static const char *attrName(Attr a); static const char *attrName(Attr a);
}; };
#endif #endif

View file

@ -7,53 +7,53 @@
#include "cardinfotext.h" #include "cardinfotext.h"
CardFrame::CardFrame(int width, int height, CardFrame::CardFrame(int width, int height,
const QString &cardName, QWidget *parent) const QString &cardName, QWidget *parent)
: QStackedWidget(parent) : QStackedWidget(parent)
, info(0) , info(0)
, cardTextOnly(false) , cardTextOnly(false)
{ {
setFrameStyle(QFrame::Panel | QFrame::Raised); setFrameStyle(QFrame::Panel | QFrame::Raised);
setMaximumWidth(width); setMaximumWidth(width);
setMinimumWidth(width); setMinimumWidth(width);
setMaximumHeight(height); setMaximumHeight(height);
setMinimumHeight(height); setMinimumHeight(height);
pic = new CardInfoPicture(width); pic = new CardInfoPicture(width);
addWidget(pic); addWidget(pic);
text = new CardInfoText(); text = new CardInfoText();
addWidget(text); addWidget(text);
connect(pic, SIGNAL(hasPictureChanged()), this, SLOT(hasPictureChanged())); connect(pic, SIGNAL(hasPictureChanged()), this, SLOT(hasPictureChanged()));
setCard(db->getCard(cardName)); setCard(db->getCard(cardName));
} }
void CardFrame::setCard(CardInfo *card) void CardFrame::setCard(CardInfo *card)
{ {
if (info) if (info)
disconnect(info, 0, this, 0); disconnect(info, 0, this, 0);
info = card; info = card;
connect(info, SIGNAL(destroyed()), this, SLOT(clear())); connect(info, SIGNAL(destroyed()), this, SLOT(clear()));
text->setCard(info); text->setCard(info);
pic->setCard(info); pic->setCard(info);
} }
void CardFrame::setCard(const QString &cardName) void CardFrame::setCard(const QString &cardName)
{ {
setCard(db->getCard(cardName)); setCard(db->getCard(cardName));
} }
void CardFrame::setCard(AbstractCardItem *card) void CardFrame::setCard(AbstractCardItem *card)
{ {
setCard(card->getInfo()); setCard(card->getInfo());
} }
void CardFrame::clear() void CardFrame::clear()
{ {
setCard(db->getCard()); setCard(db->getCard());
} }
void CardFrame::hasPictureChanged() void CardFrame::hasPictureChanged()
{ {
if (pic->hasPicture() && !cardTextOnly) if (pic->hasPicture() && !cardTextOnly)
setCurrentWidget(pic); setCurrentWidget(pic);
else else
setCurrentWidget(text); setCurrentWidget(text);
} }

View file

@ -9,28 +9,28 @@ class CardInfoPicture;
class CardInfoText; class CardInfoText;
class CardFrame : public QStackedWidget { class CardFrame : public QStackedWidget {
Q_OBJECT Q_OBJECT
private: private:
CardInfo *info; CardInfo *info;
CardInfoPicture *pic; CardInfoPicture *pic;
CardInfoText *text; CardInfoText *text;
bool cardTextOnly; bool cardTextOnly;
public: public:
CardFrame(int width, int height, const QString &cardName = QString(), CardFrame(int width, int height, const QString &cardName = QString(),
QWidget *parent = 0); QWidget *parent = 0);
void setCardTextOnly(bool status) { cardTextOnly = status; hasPictureChanged(); } void setCardTextOnly(bool status) { cardTextOnly = status; hasPictureChanged(); }
public slots: public slots:
void setCard(CardInfo *card); void setCard(CardInfo *card);
void setCard(const QString &cardName); void setCard(const QString &cardName);
void setCard(AbstractCardItem *card); void setCard(AbstractCardItem *card);
void clear(); void clear();
private slots: private slots:
void hasPictureChanged(); void hasPictureChanged();
void toggleCardTextOnly() { setCardTextOnly(!cardTextOnly); } void toggleCardTextOnly() { setCardTextOnly(!cardTextOnly); }
}; };
#endif #endif

View file

@ -6,48 +6,48 @@
#include "main.h" #include "main.h"
CardInfoPicture::CardInfoPicture(int maximumWidth, QWidget *parent) CardInfoPicture::CardInfoPicture(int maximumWidth, QWidget *parent)
: QLabel(parent) : QLabel(parent)
, info(0) , info(0)
, noPicture(true) , noPicture(true)
{ {
setMaximumWidth(maximumWidth); setMaximumWidth(maximumWidth);
} }
void CardInfoPicture::setNoPicture(bool status) void CardInfoPicture::setNoPicture(bool status)
{ {
if (noPicture != status) { if (noPicture != status) {
noPicture = status; noPicture = status;
emit hasPictureChanged(); emit hasPictureChanged();
} }
} }
void CardInfoPicture::setCard(CardInfo *card) void CardInfoPicture::setCard(CardInfo *card)
{ {
if (info) if (info)
disconnect(info, 0, this, 0); disconnect(info, 0, this, 0);
info = card; info = card;
connect(info, SIGNAL(pixmapUpdated()), this, SLOT(updatePixmap())); connect(info, SIGNAL(pixmapUpdated()), this, SLOT(updatePixmap()));
updatePixmap(); updatePixmap();
} }
void CardInfoPicture::updatePixmap() void CardInfoPicture::updatePixmap()
{ {
qreal aspectRatio = (qreal) CARD_HEIGHT / (qreal) CARD_WIDTH; qreal aspectRatio = (qreal) CARD_HEIGHT / (qreal) CARD_WIDTH;
qreal pixmapWidth = this->width(); qreal pixmapWidth = this->width();
if (pixmapWidth == 0) { if (pixmapWidth == 0) {
setNoPicture(true); setNoPicture(true);
return; return;
} }
QPixmap *resizedPixmap = info->getPixmap(QSize(pixmapWidth, pixmapWidth * aspectRatio)); QPixmap *resizedPixmap = info->getPixmap(QSize(pixmapWidth, pixmapWidth * aspectRatio));
if (resizedPixmap) { if (resizedPixmap) {
setNoPicture(false); setNoPicture(false);
this->setPixmap(*resizedPixmap); this->setPixmap(*resizedPixmap);
} }
else { else {
setNoPicture(true); setNoPicture(true);
this->setPixmap(*(db->getCard()->getPixmap(QSize(pixmapWidth, pixmapWidth * aspectRatio)))); this->setPixmap(*(db->getCard()->getPixmap(QSize(pixmapWidth, pixmapWidth * aspectRatio))));
} }
} }

View file

@ -7,27 +7,27 @@ class AbstractCardItem;
class CardInfo; class CardInfo;
class CardInfoPicture : public QLabel { class CardInfoPicture : public QLabel {
Q_OBJECT Q_OBJECT
signals: signals:
void hasPictureChanged(); void hasPictureChanged();
private: private:
CardInfo *info; CardInfo *info;
bool noPicture; bool noPicture;
public: public:
CardInfoPicture(int maximumWidth, QWidget *parent = 0); CardInfoPicture(int maximumWidth, QWidget *parent = 0);
bool hasPicture() const { return !noPicture; } bool hasPicture() const { return !noPicture; }
private: private:
void setNoPicture(bool status); void setNoPicture(bool status);
public slots: public slots:
void setCard(CardInfo *card); void setCard(CardInfo *card);
private slots: private slots:
void updatePixmap(); void updatePixmap();
}; };

View file

@ -8,60 +8,60 @@
#include "main.h" #include "main.h"
CardInfoText::CardInfoText(QWidget *parent) CardInfoText::CardInfoText(QWidget *parent)
: QFrame(parent) : QFrame(parent)
, info(0) , info(0)
{ {
nameLabel1 = new QLabel; nameLabel1 = new QLabel;
nameLabel2 = new QLabel; nameLabel2 = new QLabel;
nameLabel2->setWordWrap(true); nameLabel2->setWordWrap(true);
manacostLabel1 = new QLabel; manacostLabel1 = new QLabel;
manacostLabel2 = new QLabel; manacostLabel2 = new QLabel;
manacostLabel2->setWordWrap(true); manacostLabel2->setWordWrap(true);
cardtypeLabel1 = new QLabel; cardtypeLabel1 = new QLabel;
cardtypeLabel2 = new QLabel; cardtypeLabel2 = new QLabel;
cardtypeLabel2->setWordWrap(true); cardtypeLabel2->setWordWrap(true);
powtoughLabel1 = new QLabel; powtoughLabel1 = new QLabel;
powtoughLabel2 = new QLabel; powtoughLabel2 = new QLabel;
loyaltyLabel1 = new QLabel; loyaltyLabel1 = new QLabel;
loyaltyLabel2 = new QLabel; loyaltyLabel2 = new QLabel;
textLabel = new QTextEdit(); textLabel = new QTextEdit();
textLabel->setReadOnly(true); textLabel->setReadOnly(true);
QGridLayout *grid = new QGridLayout(this); QGridLayout *grid = new QGridLayout(this);
int row = 0; int row = 0;
grid->addWidget(nameLabel1, row, 0); grid->addWidget(nameLabel1, row, 0);
grid->addWidget(nameLabel2, row++, 1); grid->addWidget(nameLabel2, row++, 1);
grid->addWidget(manacostLabel1, row, 0); grid->addWidget(manacostLabel1, row, 0);
grid->addWidget(manacostLabel2, row++, 1); grid->addWidget(manacostLabel2, row++, 1);
grid->addWidget(cardtypeLabel1, row, 0); grid->addWidget(cardtypeLabel1, row, 0);
grid->addWidget(cardtypeLabel2, row++, 1); grid->addWidget(cardtypeLabel2, row++, 1);
grid->addWidget(powtoughLabel1, row, 0); grid->addWidget(powtoughLabel1, row, 0);
grid->addWidget(powtoughLabel2, row++, 1); grid->addWidget(powtoughLabel2, row++, 1);
grid->addWidget(loyaltyLabel1, row, 0); grid->addWidget(loyaltyLabel1, row, 0);
grid->addWidget(loyaltyLabel2, row++, 1); grid->addWidget(loyaltyLabel2, row++, 1);
grid->addWidget(textLabel, row, 0, -1, 2); grid->addWidget(textLabel, row, 0, -1, 2);
grid->setRowStretch(row, 1); grid->setRowStretch(row, 1);
grid->setColumnStretch(1, 1); grid->setColumnStretch(1, 1);
retranslateUi(); retranslateUi();
} }
void CardInfoText::setCard(CardInfo *card) void CardInfoText::setCard(CardInfo *card)
{ {
nameLabel2->setText(card->getName()); nameLabel2->setText(card->getName());
manacostLabel2->setText(card->getManaCost()); manacostLabel2->setText(card->getManaCost());
cardtypeLabel2->setText(card->getCardType()); cardtypeLabel2->setText(card->getCardType());
powtoughLabel2->setText(card->getPowTough()); powtoughLabel2->setText(card->getPowTough());
loyaltyLabel2->setText(card->getLoyalty() > 0 ? QString::number(card->getLoyalty()) : QString()); loyaltyLabel2->setText(card->getLoyalty() > 0 ? QString::number(card->getLoyalty()) : QString());
textLabel->setText(card->getText()); textLabel->setText(card->getText());
} }
void CardInfoText::retranslateUi() void CardInfoText::retranslateUi()
{ {
nameLabel1->setText(tr("Name:")); nameLabel1->setText(tr("Name:"));
manacostLabel1->setText(tr("Mana cost:")); manacostLabel1->setText(tr("Mana cost:"));
cardtypeLabel1->setText(tr("Card type:")); cardtypeLabel1->setText(tr("Card type:"));
powtoughLabel1->setText(tr("P / T:")); powtoughLabel1->setText(tr("P / T:"));
loyaltyLabel1->setText(tr("Loyalty:")); loyaltyLabel1->setText(tr("Loyalty:"));
} }

View file

@ -8,24 +8,24 @@ class QTextEdit;
class CardInfo; class CardInfo;
class CardInfoText : public QFrame { class CardInfoText : public QFrame {
Q_OBJECT Q_OBJECT
private: private:
QLabel *nameLabel1, *nameLabel2; QLabel *nameLabel1, *nameLabel2;
QLabel *manacostLabel1, *manacostLabel2; QLabel *manacostLabel1, *manacostLabel2;
QLabel *cardtypeLabel1, *cardtypeLabel2; QLabel *cardtypeLabel1, *cardtypeLabel2;
QLabel *powtoughLabel1, *powtoughLabel2; QLabel *powtoughLabel1, *powtoughLabel2;
QLabel *loyaltyLabel1, *loyaltyLabel2; QLabel *loyaltyLabel1, *loyaltyLabel2;
QTextEdit *textLabel; QTextEdit *textLabel;
CardInfo *info; CardInfo *info;
public: public:
CardInfoText(QWidget *parent = 0); CardInfoText(QWidget *parent = 0);
void retranslateUi(); void retranslateUi();
public slots: public slots:
void setCard(CardInfo *card); void setCard(CardInfo *card);
}; };
#endif #endif

View file

@ -8,77 +8,77 @@
#include "cardfilter.h" #include "cardfilter.h"
FilterBuilder::FilterBuilder(QWidget *parent) FilterBuilder::FilterBuilder(QWidget *parent)
: QFrame(parent) : QFrame(parent)
{ {
int i; int i;
QVBoxLayout *layout = new QVBoxLayout; QVBoxLayout *layout = new QVBoxLayout;
QHBoxLayout *addFilter = new QHBoxLayout; QHBoxLayout *addFilter = new QHBoxLayout;
filterCombo = new QComboBox; filterCombo = new QComboBox;
for (i = 0; i < CardFilter::AttrEnd; i++) for (i = 0; i < CardFilter::AttrEnd; i++)
filterCombo->addItem( filterCombo->addItem(
tr(CardFilter::attrName(static_cast<CardFilter::Attr>(i))), tr(CardFilter::attrName(static_cast<CardFilter::Attr>(i))),
QVariant(i) QVariant(i)
); );
typeCombo = new QComboBox; typeCombo = new QComboBox;
for (i = 0; i < CardFilter::TypeEnd; i++) for (i = 0; i < CardFilter::TypeEnd; i++)
typeCombo->addItem( typeCombo->addItem(
tr(CardFilter::typeName(static_cast<CardFilter::Type>(i))), tr(CardFilter::typeName(static_cast<CardFilter::Type>(i))),
QVariant(i) QVariant(i)
); );
QPushButton *ok = new QPushButton("+"); QPushButton *ok = new QPushButton("+");
ok->setMaximumSize(20, 20); ok->setMaximumSize(20, 20);
addFilter->addWidget(ok); addFilter->addWidget(ok);
addFilter->addWidget(typeCombo); addFilter->addWidget(typeCombo);
addFilter->addWidget(filterCombo, Qt::AlignLeft); addFilter->addWidget(filterCombo, Qt::AlignLeft);
edit = new QLineEdit; edit = new QLineEdit;
edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
layout->addLayout(addFilter); layout->addLayout(addFilter);
layout->addWidget(edit); layout->addWidget(edit);
setFrameStyle(QFrame::Panel | QFrame::Raised); setFrameStyle(QFrame::Panel | QFrame::Raised);
layout->setAlignment(Qt::AlignTop); layout->setAlignment(Qt::AlignTop);
setLayout(layout); setLayout(layout);
connect(edit, SIGNAL(returnPressed()), this, SLOT(emit_add())); connect(edit, SIGNAL(returnPressed()), this, SLOT(emit_add()));
connect(ok, SIGNAL(released()), this, SLOT(emit_add())); connect(ok, SIGNAL(released()), this, SLOT(emit_add()));
connect(filterCombo, SIGNAL(currentIndexChanged(int)), edit, SLOT(clear())); connect(filterCombo, SIGNAL(currentIndexChanged(int)), edit, SLOT(clear()));
fltr = NULL; fltr = NULL;
} }
FilterBuilder::~FilterBuilder() FilterBuilder::~FilterBuilder()
{ {
destroyFilter(); destroyFilter();
} }
void FilterBuilder::destroyFilter() void FilterBuilder::destroyFilter()
{ {
if (fltr) if (fltr)
delete fltr; delete fltr;
} }
static int comboCurrentIntData(const QComboBox *combo) static int comboCurrentIntData(const QComboBox *combo)
{ {
return combo->itemData(combo->currentIndex()).toInt(); return combo->itemData(combo->currentIndex()).toInt();
} }
void FilterBuilder::emit_add() void FilterBuilder::emit_add()
{ {
QString txt; QString txt;
txt = edit->text(); txt = edit->text();
if (txt.length() < 1) if (txt.length() < 1)
return; return;
destroyFilter(); destroyFilter();
fltr = new CardFilter(txt, fltr = new CardFilter(txt,
static_cast<CardFilter::Type>(comboCurrentIntData(typeCombo)), static_cast<CardFilter::Type>(comboCurrentIntData(typeCombo)),
static_cast<CardFilter::Attr>(comboCurrentIntData(filterCombo))); static_cast<CardFilter::Attr>(comboCurrentIntData(filterCombo)));
emit add(fltr); emit add(fltr);
edit->clear(); edit->clear();
} }

View file

@ -9,26 +9,26 @@ class QLineEdit;
class CardFilter; class CardFilter;
class FilterBuilder : public QFrame { class FilterBuilder : public QFrame {
Q_OBJECT Q_OBJECT
private: private:
QComboBox *typeCombo; QComboBox *typeCombo;
QComboBox *filterCombo; QComboBox *filterCombo;
QLineEdit *edit; QLineEdit *edit;
CardFilter *fltr; CardFilter *fltr;
void destroyFilter(); void destroyFilter();
public: public:
FilterBuilder(QWidget *parent = 0); FilterBuilder(QWidget *parent = 0);
~FilterBuilder(); ~FilterBuilder();
signals: signals:
void add(const CardFilter *f); void add(const CardFilter *f);
public slots: public slots:
private slots: private slots:
void emit_add(); void emit_add();
protected: protected:
}; };

View file

@ -7,223 +7,223 @@
template <class T> template <class T>
FilterTreeNode *FilterTreeBranch<T>::nodeAt(int i) const FilterTreeNode *FilterTreeBranch<T>::nodeAt(int i) const
{ {
return ((childNodes.size() > i)? childNodes.at(i) : NULL); return ((childNodes.size() > i)? childNodes.at(i) : NULL);
} }
template <class T> template <class T>
void FilterTreeBranch<T>::deleteAt(int i) void FilterTreeBranch<T>::deleteAt(int i)
{ {
preRemoveChild(this, i); preRemoveChild(this, i);
delete childNodes.takeAt(i); delete childNodes.takeAt(i);
postRemoveChild(this, i); postRemoveChild(this, i);
nodeChanged(); nodeChanged();
} }
template <class T> template <class T>
int FilterTreeBranch<T>::childIndex(const FilterTreeNode *node) const int FilterTreeBranch<T>::childIndex(const FilterTreeNode *node) const
{ {
FilterTreeNode *unconst; FilterTreeNode *unconst;
T downcasted; T downcasted;
/* to do the dynamic cast to T we will lose const'ness, but we can /* to do the dynamic cast to T we will lose const'ness, but we can
* trust QList::indexOf */ * trust QList::indexOf */
unconst = (FilterTreeNode *) node; unconst = (FilterTreeNode *) node;
downcasted = dynamic_cast<T>(unconst); downcasted = dynamic_cast<T>(unconst);
if (downcasted == NULL) if (downcasted == NULL)
return -1; return -1;
return childNodes.indexOf(downcasted); return childNodes.indexOf(downcasted);
} }
template <class T> template <class T>
FilterTreeBranch<T>::~FilterTreeBranch() FilterTreeBranch<T>::~FilterTreeBranch()
{ {
while (!childNodes.isEmpty()) while (!childNodes.isEmpty())
delete childNodes.takeFirst(); delete childNodes.takeFirst();
} }
const FilterItemList *LogicMap::findTypeList(CardFilter::Type type) const const FilterItemList *LogicMap::findTypeList(CardFilter::Type type) const
{ {
QList<FilterItemList *>::const_iterator i; QList<FilterItemList *>::const_iterator i;
for (i = childNodes.constBegin(); i != childNodes.constEnd(); i++) for (i = childNodes.constBegin(); i != childNodes.constEnd(); i++)
if ((*i)->type == type) if ((*i)->type == type)
return *i; return *i;
return NULL; return NULL;
} }
FilterItemList *LogicMap::typeList(CardFilter::Type type) FilterItemList *LogicMap::typeList(CardFilter::Type type)
{ {
QList<FilterItemList *>::iterator i; QList<FilterItemList *>::iterator i;
int count; int count;
count = 0; count = 0;
for (i = childNodes.begin(); i != childNodes.end(); i++) { for (i = childNodes.begin(); i != childNodes.end(); i++) {
if ((*i)->type == type) if ((*i)->type == type)
break; break;
count++; count++;
} }
if (i == childNodes.end()) { if (i == childNodes.end()) {
preInsertChild(this, count); preInsertChild(this, count);
i = childNodes.insert(i, new FilterItemList(type, this)); i = childNodes.insert(i, new FilterItemList(type, this));
postInsertChild(this, count); postInsertChild(this, count);
nodeChanged(); nodeChanged();
} }
return *i; return *i;
} }
FilterTreeNode *LogicMap::parent() const FilterTreeNode *LogicMap::parent() const
{ {
return p; return p;
} }
int FilterItemList::termIndex(const QString &term) const int FilterItemList::termIndex(const QString &term) const
{ {
int i; int i;
for (i = 0; i < childNodes.count(); i++) for (i = 0; i < childNodes.count(); i++)
if ((childNodes.at(i))->term == term) if ((childNodes.at(i))->term == term)
return i; return i;
return -1; return -1;
} }
FilterTreeNode *FilterItemList::termNode(const QString &term) FilterTreeNode *FilterItemList::termNode(const QString &term)
{ {
int i, count; int i, count;
FilterItem *fi; FilterItem *fi;
i = termIndex(term); i = termIndex(term);
if (i < 0) { if (i < 0) {
fi = new FilterItem(term, this); fi = new FilterItem(term, this);
count = childNodes.count(); count = childNodes.count();
preInsertChild(this, count); preInsertChild(this, count);
childNodes.append(fi); childNodes.append(fi);
postInsertChild(this, count); postInsertChild(this, count);
nodeChanged(); nodeChanged();
return fi; return fi;
} }
return childNodes.at(i); return childNodes.at(i);
} }
bool FilterItemList::testTypeAnd(const CardInfo *info, CardFilter::Attr attr) const bool FilterItemList::testTypeAnd(const CardInfo *info, CardFilter::Attr attr) const
{ {
QList<FilterItem *>::const_iterator i; QList<FilterItem *>::const_iterator i;
for (i = childNodes.constBegin(); i != childNodes.constEnd(); i++) for (i = childNodes.constBegin(); i != childNodes.constEnd(); i++)
if (!(*i)->acceptCardAttr(info, attr)) if (!(*i)->acceptCardAttr(info, attr))
return false; return false;
return true; return true;
} }
bool FilterItemList::testTypeAndNot(const CardInfo *info, CardFilter::Attr attr) const bool FilterItemList::testTypeAndNot(const CardInfo *info, CardFilter::Attr attr) const
{ {
// if any one in the list is true, return false // if any one in the list is true, return false
return !testTypeOr(info, attr); return !testTypeOr(info, attr);
} }
bool FilterItemList::testTypeOr(const CardInfo *info, CardFilter::Attr attr) const bool FilterItemList::testTypeOr(const CardInfo *info, CardFilter::Attr attr) const
{ {
QList<FilterItem *>::const_iterator i; QList<FilterItem *>::const_iterator i;
for (i = childNodes.constBegin(); i != childNodes.constEnd(); i++) for (i = childNodes.constBegin(); i != childNodes.constEnd(); i++)
if ((*i)->acceptCardAttr(info, attr)) if ((*i)->acceptCardAttr(info, attr))
return true; return true;
return false; return false;
} }
bool FilterItemList::testTypeOrNot(const CardInfo *info, CardFilter::Attr attr) const bool FilterItemList::testTypeOrNot(const CardInfo *info, CardFilter::Attr attr) const
{ {
// if any one in the list is false, return true // if any one in the list is false, return true
return !testTypeAnd(info, attr); return !testTypeAnd(info, attr);
} }
bool FilterItem::acceptName(const CardInfo *info) const bool FilterItem::acceptName(const CardInfo *info) const
{ {
return info->getName().contains(term, Qt::CaseInsensitive); return info->getName().contains(term, Qt::CaseInsensitive);
} }
bool FilterItem::acceptType(const CardInfo *info) const bool FilterItem::acceptType(const CardInfo *info) const
{ {
return info->getCardType().contains(term, Qt::CaseInsensitive); return info->getCardType().contains(term, Qt::CaseInsensitive);
} }
bool FilterItem::acceptColor(const CardInfo *info) const bool FilterItem::acceptColor(const CardInfo *info) const
{ {
QStringList::const_iterator i; QStringList::const_iterator i;
bool status; bool status;
status = false; status = false;
for (i = info->getColors().constBegin(); i != info->getColors().constEnd(); i++) for (i = info->getColors().constBegin(); i != info->getColors().constEnd(); i++)
if ((*i).contains(term, Qt::CaseInsensitive)) { if ((*i).contains(term, Qt::CaseInsensitive)) {
status = true; status = true;
break; break;
} }
return status; return status;
} }
bool FilterItem::acceptText(const CardInfo *info) const bool FilterItem::acceptText(const CardInfo *info) const
{ {
return info->getText().contains(term, Qt::CaseInsensitive); return info->getText().contains(term, Qt::CaseInsensitive);
} }
bool FilterItem::acceptSet(const CardInfo *info) const bool FilterItem::acceptSet(const CardInfo *info) const
{ {
SetList::const_iterator i; SetList::const_iterator i;
bool status; bool status;
status = false; status = false;
for (i = info->getSets().constBegin(); i != info->getSets().constEnd(); i++) for (i = info->getSets().constBegin(); i != info->getSets().constEnd(); i++)
if ((*i)->getShortName() == term if ((*i)->getShortName() == term
|| (*i)->getLongName().contains(term, Qt::CaseInsensitive)) { || (*i)->getLongName().contains(term, Qt::CaseInsensitive)) {
status = true; status = true;
break; break;
} }
return status; return status;
} }
bool FilterItem::acceptManaCost(const CardInfo *info) const bool FilterItem::acceptManaCost(const CardInfo *info) const
{ {
return (info->getManaCost() == term); return (info->getManaCost() == term);
} }
bool FilterItem::acceptCardAttr(const CardInfo *info, CardFilter::Attr attr) const bool FilterItem::acceptCardAttr(const CardInfo *info, CardFilter::Attr attr) const
{ {
bool status; bool status;
if (!isEnabled()) if (!isEnabled())
return true; return true;
switch (attr) { switch (attr) {
case CardFilter::AttrName: case CardFilter::AttrName:
status = acceptName(info); status = acceptName(info);
break; break;
case CardFilter::AttrType: case CardFilter::AttrType:
status = acceptType(info); status = acceptType(info);
break; break;
case CardFilter::AttrColor: case CardFilter::AttrColor:
status = acceptColor(info); status = acceptColor(info);
break; break;
case CardFilter::AttrText: case CardFilter::AttrText:
status = acceptText(info); status = acceptText(info);
break; break;
case CardFilter::AttrSet: case CardFilter::AttrSet:
status = acceptSet(info); status = acceptSet(info);
break; break;
case CardFilter::AttrManaCost: case CardFilter::AttrManaCost:
status = acceptManaCost(info); status = acceptManaCost(info);
break; break;
default: default:
status = true; /* ignore this attribute */ status = true; /* ignore this attribute */
} }
return status; return status;
} }
/* need to define these here to make QT happy, otherwise /* need to define these here to make QT happy, otherwise
@ -234,103 +234,103 @@ FilterTree::~FilterTree() {}
LogicMap *FilterTree::attrLogicMap(CardFilter::Attr attr) LogicMap *FilterTree::attrLogicMap(CardFilter::Attr attr)
{ {
QList<LogicMap *>::iterator i; QList<LogicMap *>::iterator i;
int count; int count;
count = 0; count = 0;
for (i = childNodes.begin(); i != childNodes.end(); i++) { for (i = childNodes.begin(); i != childNodes.end(); i++) {
if ((*i)->attr == attr) if ((*i)->attr == attr)
break; break;
count++; count++;
} }
if (i == childNodes.end()) { if (i == childNodes.end()) {
preInsertChild(this, count); preInsertChild(this, count);
i = childNodes.insert(i, new LogicMap(attr, this)); i = childNodes.insert(i, new LogicMap(attr, this));
postInsertChild(this, count); postInsertChild(this, count);
nodeChanged(); nodeChanged();
} }
return *i; return *i;
} }
FilterItemList *FilterTree::attrTypeList(CardFilter::Attr attr, FilterItemList *FilterTree::attrTypeList(CardFilter::Attr attr,
CardFilter::Type type) CardFilter::Type type)
{ {
return attrLogicMap(attr)->typeList(type); return attrLogicMap(attr)->typeList(type);
} }
int FilterTree::findTermIndex(CardFilter::Attr attr, CardFilter::Type type, int FilterTree::findTermIndex(CardFilter::Attr attr, CardFilter::Type type,
const QString &term) const QString &term)
{ {
attrTypeList(attr, type)->termIndex(term); attrTypeList(attr, type)->termIndex(term);
} }
int FilterTree::findTermIndex(const CardFilter *f) int FilterTree::findTermIndex(const CardFilter *f)
{ {
return findTermIndex(f->attr(), f->type(), f->term()); return findTermIndex(f->attr(), f->type(), f->term());
} }
FilterTreeNode *FilterTree::termNode(CardFilter::Attr attr, CardFilter::Type type, FilterTreeNode *FilterTree::termNode(CardFilter::Attr attr, CardFilter::Type type,
const QString &term) const QString &term)
{ {
return attrTypeList(attr, type)->termNode(term); return attrTypeList(attr, type)->termNode(term);
} }
FilterTreeNode *FilterTree::termNode(const CardFilter *f) FilterTreeNode *FilterTree::termNode(const CardFilter *f)
{ {
return termNode(f->attr(), f->type(), f->term()); return termNode(f->attr(), f->type(), f->term());
} }
FilterTreeNode *FilterTree::attrTypeNode(CardFilter::Attr attr, FilterTreeNode *FilterTree::attrTypeNode(CardFilter::Attr attr,
CardFilter::Type type) CardFilter::Type type)
{ {
return attrTypeList(attr, type); return attrTypeList(attr, type);
} }
bool FilterTree::testAttr(const CardInfo *info, const LogicMap *lm) const bool FilterTree::testAttr(const CardInfo *info, const LogicMap *lm) const
{ {
const FilterItemList *fil; const FilterItemList *fil;
bool status; bool status;
status = true; status = true;
fil = lm->findTypeList(CardFilter::TypeAnd); fil = lm->findTypeList(CardFilter::TypeAnd);
if (fil != NULL && fil->isEnabled() && !fil->testTypeAnd(info, lm->attr)) if (fil != NULL && fil->isEnabled() && !fil->testTypeAnd(info, lm->attr))
return false; return false;
fil = lm->findTypeList(CardFilter::TypeAndNot); fil = lm->findTypeList(CardFilter::TypeAndNot);
if (fil != NULL && fil->isEnabled() && !fil->testTypeAndNot(info, lm->attr)) if (fil != NULL && fil->isEnabled() && !fil->testTypeAndNot(info, lm->attr))
return false; return false;
fil = lm->findTypeList(CardFilter::TypeOr); fil = lm->findTypeList(CardFilter::TypeOr);
if (fil != NULL && fil->isEnabled()) { if (fil != NULL && fil->isEnabled()) {
status = false; status = false;
// if this is true we can return because it is OR'd with the OrNot list // if this is true we can return because it is OR'd with the OrNot list
if (fil->testTypeOr(info, lm->attr)) if (fil->testTypeOr(info, lm->attr))
return true; return true;
} }
fil = lm->findTypeList(CardFilter::TypeOrNot); fil = lm->findTypeList(CardFilter::TypeOrNot);
if (fil != NULL && fil->isEnabled() && fil->testTypeOrNot(info, lm->attr)) if (fil != NULL && fil->isEnabled() && fil->testTypeOrNot(info, lm->attr))
return true; return true;
return status; return status;
} }
bool FilterTree::acceptsCard(const CardInfo *info) const bool FilterTree::acceptsCard(const CardInfo *info) const
{ {
QList<LogicMap *>::const_iterator i; QList<LogicMap *>::const_iterator i;
for (i = childNodes.constBegin(); i != childNodes.constEnd(); i++) for (i = childNodes.constBegin(); i != childNodes.constEnd(); i++)
if ((*i)->isEnabled() && !testAttr(info, *i)) if ((*i)->isEnabled() && !testAttr(info, *i))
return false; return false;
return true; return true;
} }
void FilterTree::clear() void FilterTree::clear()
{ {
while(childCount() > 0) while(childCount() > 0)
deleteAt(0); deleteAt(0);
} }

View file

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

View file

@ -4,269 +4,269 @@
#include "cardfilter.h" #include "cardfilter.h"
FilterTreeModel::FilterTreeModel(QObject *parent) FilterTreeModel::FilterTreeModel(QObject *parent)
: QAbstractItemModel(parent) : QAbstractItemModel(parent)
{ {
fTree = new FilterTree; fTree = new FilterTree;
connect(fTree, connect(fTree,
SIGNAL(preInsertRow(const FilterTreeNode *, int)), SIGNAL(preInsertRow(const FilterTreeNode *, int)),
this, SLOT(proxyBeginInsertRow(const FilterTreeNode *, int))); this, SLOT(proxyBeginInsertRow(const FilterTreeNode *, int)));
connect(fTree, connect(fTree,
SIGNAL(postInsertRow(const FilterTreeNode *, int)), SIGNAL(postInsertRow(const FilterTreeNode *, int)),
this, SLOT(proxyEndInsertRow(const FilterTreeNode *, int))); this, SLOT(proxyEndInsertRow(const FilterTreeNode *, int)));
connect(fTree, connect(fTree,
SIGNAL(preRemoveRow(const FilterTreeNode *, int)), SIGNAL(preRemoveRow(const FilterTreeNode *, int)),
this, SLOT(proxyBeginRemoveRow(const FilterTreeNode *, int))); this, SLOT(proxyBeginRemoveRow(const FilterTreeNode *, int)));
connect(fTree, connect(fTree,
SIGNAL(postRemoveRow(const FilterTreeNode *, int)), SIGNAL(postRemoveRow(const FilterTreeNode *, int)),
this, SLOT(proxyEndRemoveRow(const FilterTreeNode *, int))); this, SLOT(proxyEndRemoveRow(const FilterTreeNode *, int)));
} }
FilterTreeModel::~FilterTreeModel() FilterTreeModel::~FilterTreeModel()
{ {
delete fTree; delete fTree;
} }
void FilterTreeModel::proxyBeginInsertRow(const FilterTreeNode *node, int i) void FilterTreeModel::proxyBeginInsertRow(const FilterTreeNode *node, int i)
{ {
int idx; int idx;
idx = node->index(); idx = node->index();
if (idx >= 0) if (idx >= 0)
beginInsertRows(createIndex(idx, 0, (void *) node), i, i); beginInsertRows(createIndex(idx, 0, (void *) node), i, i);
} }
void FilterTreeModel::proxyEndInsertRow(const FilterTreeNode *node, int) void FilterTreeModel::proxyEndInsertRow(const FilterTreeNode *node, int)
{ {
int idx; int idx;
idx = node->index(); idx = node->index();
if (idx >= 0) if (idx >= 0)
endInsertRows(); endInsertRows();
} }
void FilterTreeModel::proxyBeginRemoveRow(const FilterTreeNode *node, int i) void FilterTreeModel::proxyBeginRemoveRow(const FilterTreeNode *node, int i)
{ {
int idx; int idx;
idx = node->index(); idx = node->index();
if (idx >= 0) if (idx >= 0)
beginRemoveRows(createIndex(idx, 0, (void *) node), i, i); beginRemoveRows(createIndex(idx, 0, (void *) node), i, i);
} }
void FilterTreeModel::proxyEndRemoveRow(const FilterTreeNode *node, int) void FilterTreeModel::proxyEndRemoveRow(const FilterTreeNode *node, int)
{ {
int idx; int idx;
idx = node->index(); idx = node->index();
if (idx >= 0) if (idx >= 0)
endRemoveRows(); endRemoveRows();
} }
FilterTreeNode *FilterTreeModel::indexToNode(const QModelIndex &idx) const FilterTreeNode *FilterTreeModel::indexToNode(const QModelIndex &idx) const
{ {
void *ip; void *ip;
FilterTreeNode *node; FilterTreeNode *node;
if (!idx.isValid()) if (!idx.isValid())
return fTree; return fTree;
ip = idx.internalPointer(); ip = idx.internalPointer();
if (ip == NULL) if (ip == NULL)
return fTree; return fTree;
node = static_cast<FilterTreeNode *>(ip); node = static_cast<FilterTreeNode *>(ip);
return node; return node;
} }
void FilterTreeModel::addFilter(const CardFilter *f) void FilterTreeModel::addFilter(const CardFilter *f)
{ {
emit layoutAboutToBeChanged(); emit layoutAboutToBeChanged();
fTree->termNode(f); fTree->termNode(f);
emit layoutChanged(); emit layoutChanged();
} }
int FilterTreeModel::rowCount(const QModelIndex &parent) const int FilterTreeModel::rowCount(const QModelIndex &parent) const
{ {
const FilterTreeNode *node; const FilterTreeNode *node;
int result; int result;
if (parent.column() > 0) if (parent.column() > 0)
return 0; return 0;
node = indexToNode(parent); node = indexToNode(parent);
if (node) if (node)
result = node->childCount(); result = node->childCount();
else else
result = 0; result = 0;
return result; return result;
} }
int FilterTreeModel::columnCount(const QModelIndex &/*parent*/) const int FilterTreeModel::columnCount(const QModelIndex &/*parent*/) const
{ {
return 1; return 1;
} }
QVariant FilterTreeModel::data(const QModelIndex &index, int role) const QVariant FilterTreeModel::data(const QModelIndex &index, int role) const
{ {
const FilterTreeNode *node; const FilterTreeNode *node;
if (!index.isValid()) if (!index.isValid())
return QVariant(); return QVariant();
if (index.column() >= columnCount()) if (index.column() >= columnCount())
return QVariant(); return QVariant();
node = indexToNode(index); node = indexToNode(index);
if (node == NULL) if (node == NULL)
return QVariant(); return QVariant();
switch (role) { switch (role) {
case Qt::FontRole: case Qt::FontRole:
if (!node->isLeaf()) { if (!node->isLeaf()) {
QFont f; QFont f;
f.setBold(true); f.setBold(true);
return f; return f;
} }
break; break;
case Qt::DisplayRole: case Qt::DisplayRole:
case Qt::EditRole: case Qt::EditRole:
case Qt::ToolTipRole: case Qt::ToolTipRole:
case Qt::StatusTipRole: case Qt::StatusTipRole:
case Qt::WhatsThisRole: case Qt::WhatsThisRole:
if(!node->isLeaf()) if(!node->isLeaf())
return tr(node->textCStr()); return tr(node->textCStr());
else else
return node->text(); return node->text();
case Qt::CheckStateRole: case Qt::CheckStateRole:
if (node->isEnabled()) if (node->isEnabled())
return Qt::Checked; return Qt::Checked;
else else
return Qt::Unchecked; return Qt::Unchecked;
default: default:
return QVariant(); return QVariant();
} }
return QVariant(); return QVariant();
} }
bool FilterTreeModel::setData(const QModelIndex &index, bool FilterTreeModel::setData(const QModelIndex &index,
const QVariant &value, int role) const QVariant &value, int role)
{ {
FilterTreeNode *node; FilterTreeNode *node;
if (!index.isValid()) if (!index.isValid())
return false; return false;
if (index.column() >= columnCount()) if (index.column() >= columnCount())
return false; return false;
if (role != Qt::CheckStateRole ) if (role != Qt::CheckStateRole )
return false; return false;
node = indexToNode(index); node = indexToNode(index);
if (node == NULL || node == fTree) if (node == NULL || node == fTree)
return false; return false;
Qt::CheckState state = static_cast<Qt::CheckState>(value.toInt()); Qt::CheckState state = static_cast<Qt::CheckState>(value.toInt());
if (state == Qt::Checked) if (state == Qt::Checked)
node->enable(); node->enable();
else else
node->disable(); node->disable();
emit dataChanged(index, index); emit dataChanged(index, index);
return true; return true;
} }
Qt::ItemFlags FilterTreeModel::flags(const QModelIndex &index) const Qt::ItemFlags FilterTreeModel::flags(const QModelIndex &index) const
{ {
const FilterTreeNode *node; const FilterTreeNode *node;
Qt::ItemFlags result; Qt::ItemFlags result;
if (!index.isValid()) if (!index.isValid())
return 0; return 0;
node = indexToNode(index); node = indexToNode(index);
if (node == NULL) if (node == NULL)
return 0; return 0;
result = Qt::ItemIsEnabled; result = Qt::ItemIsEnabled;
if (node == fTree) if (node == fTree)
return result; return result;
result |= Qt::ItemIsSelectable; result |= Qt::ItemIsSelectable;
result |= Qt::ItemIsUserCheckable; result |= Qt::ItemIsUserCheckable;
return result; return result;
} }
QModelIndex FilterTreeModel::nodeIndex(const FilterTreeNode *node, int row, int column) const QModelIndex FilterTreeModel::nodeIndex(const FilterTreeNode *node, int row, int column) const
{ {
FilterTreeNode *child; FilterTreeNode *child;
if (column > 0 || row >= node->childCount()) if (column > 0 || row >= node->childCount())
return QModelIndex(); return QModelIndex();
child = node->nodeAt(row); child = node->nodeAt(row);
return createIndex(row, column, child); return createIndex(row, column, child);
} }
QModelIndex FilterTreeModel::index(int row, int column, QModelIndex FilterTreeModel::index(int row, int column,
const QModelIndex &parent) const const QModelIndex &parent) const
{ {
const FilterTreeNode *node; const FilterTreeNode *node;
if (!hasIndex(row, column, parent)) if (!hasIndex(row, column, parent))
return QModelIndex(); return QModelIndex();
node = indexToNode(parent); node = indexToNode(parent);
if (node == NULL) if (node == NULL)
return QModelIndex(); return QModelIndex();
return nodeIndex(node, row, column); return nodeIndex(node, row, column);
} }
QModelIndex FilterTreeModel::parent(const QModelIndex &ind) const QModelIndex FilterTreeModel::parent(const QModelIndex &ind) const
{ {
const FilterTreeNode *node; const FilterTreeNode *node;
FilterTreeNode *parent; FilterTreeNode *parent;
int row; int row;
QModelIndex idx; QModelIndex idx;
if (!ind.isValid()) if (!ind.isValid())
return QModelIndex(); return QModelIndex();
node = indexToNode(ind); node = indexToNode(ind);
if (node == NULL || node == fTree) if (node == NULL || node == fTree)
return QModelIndex(); return QModelIndex();
parent = node->parent(); parent = node->parent();
if (parent) { if (parent) {
row = parent->index(); row = parent->index();
if (row < 0) if (row < 0)
return QModelIndex(); return QModelIndex();
idx = createIndex(row, 0, parent); idx = createIndex(row, 0, parent);
return idx; return idx;
} }
return QModelIndex(); return QModelIndex();
} }
bool FilterTreeModel::removeRows(int row, int count, const QModelIndex & parent) bool FilterTreeModel::removeRows(int row, int count, const QModelIndex & parent)
{ {
FilterTreeNode *node; FilterTreeNode *node;
int i, last; int i, last;
last = row+count-1; last = row+count-1;
if (!parent.isValid() || count < 1 || row < 0) if (!parent.isValid() || count < 1 || row < 0)
return false; return false;
node = indexToNode(parent); node = indexToNode(parent);
if (node == NULL || last >= node->childCount()) if (node == NULL || last >= node->childCount())
return false; return false;
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
node->deleteAt(row); node->deleteAt(row);
if (node != fTree && node->childCount() < 1) if (node != fTree && node->childCount() < 1)
return removeRow(parent.row(), parent.parent()); return removeRow(parent.row(), parent.parent());
return true; return true;
} }

View file

@ -8,37 +8,37 @@ class CardFilter;
class FilterTreeNode; class FilterTreeNode;
class FilterTreeModel : public QAbstractItemModel { class FilterTreeModel : public QAbstractItemModel {
Q_OBJECT Q_OBJECT
private: private:
FilterTree *fTree; FilterTree *fTree;
public slots: public slots:
void addFilter(const CardFilter *f); void addFilter(const CardFilter *f);
private slots: private slots:
void proxyBeginInsertRow(const FilterTreeNode *, int); void proxyBeginInsertRow(const FilterTreeNode *, int);
void proxyEndInsertRow(const FilterTreeNode *, int); void proxyEndInsertRow(const FilterTreeNode *, int);
void proxyBeginRemoveRow(const FilterTreeNode *, int); void proxyBeginRemoveRow(const FilterTreeNode *, int);
void proxyEndRemoveRow(const FilterTreeNode *, int); void proxyEndRemoveRow(const FilterTreeNode *, int);
private: private:
FilterTreeNode *indexToNode(const QModelIndex &idx) const; FilterTreeNode *indexToNode(const QModelIndex &idx) const;
QModelIndex nodeIndex(const FilterTreeNode *node, int row, int column) const; QModelIndex nodeIndex(const FilterTreeNode *node, int row, int column) const;
public: public:
FilterTreeModel(QObject *parent = 0); FilterTreeModel(QObject *parent = 0);
~FilterTreeModel(); ~FilterTreeModel();
FilterTree *filterTree() const { return fTree; } FilterTree *filterTree() const { return fTree; }
int rowCount(const QModelIndex &parent = QModelIndex()) const; int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &/*parent*/ = QModelIndex()) const; int columnCount(const QModelIndex &/*parent*/ = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role) const; QVariant data(const QModelIndex &index, int role) const;
bool setData(const QModelIndex &index, const QVariant &value, bool setData(const QModelIndex &index, const QVariant &value,
int role = Qt::EditRole); int role = Qt::EditRole);
Qt::ItemFlags flags(const QModelIndex &index) const; Qt::ItemFlags flags(const QModelIndex &index) const;
QModelIndex parent(const QModelIndex &ind) const; QModelIndex parent(const QModelIndex &ind) const;
QModelIndex index(int row, int column, QModelIndex index(int row, int column,
const QModelIndex &parent) const; const QModelIndex &parent) const;
bool removeRows(int row, int count, const QModelIndex & parent); bool removeRows(int row, int count, const QModelIndex & parent);
}; };
#endif #endif

View file

@ -2,48 +2,48 @@
#include <QKeyEvent> #include <QKeyEvent>
bool KeySignals::eventFilter(QObject * /*object*/, QEvent *event) { bool KeySignals::eventFilter(QObject * /*object*/, QEvent *event) {
QKeyEvent *kevent; QKeyEvent *kevent;
if(event->type() != QEvent::KeyPress) if(event->type() != QEvent::KeyPress)
return false; return false;
kevent = static_cast<QKeyEvent *>(event); kevent = static_cast<QKeyEvent *>(event);
switch(kevent->key()) { switch(kevent->key()) {
case Qt::Key_Return: case Qt::Key_Return:
case Qt::Key_Enter: case Qt::Key_Enter:
if(kevent->modifiers() & Qt::ControlModifier) if(kevent->modifiers() & Qt::ControlModifier)
emit onCtrlEnter(); emit onCtrlEnter();
else else
emit onEnter(); emit onEnter();
break; break;
case Qt::Key_Right: case Qt::Key_Right:
if(kevent->modifiers() & Qt::ControlModifier) if(kevent->modifiers() & Qt::ControlModifier)
emit onCtrlRight(); emit onCtrlRight();
else else
emit onRight(); emit onRight();
if(!filterLROn) if(!filterLROn)
return false; return false;
break; break;
case Qt::Key_Left: case Qt::Key_Left:
if(kevent->modifiers() & Qt::ControlModifier) if(kevent->modifiers() & Qt::ControlModifier)
emit onCtrlLeft(); emit onCtrlLeft();
else else
emit onLeft(); emit onLeft();
if(!filterLROn) if(!filterLROn)
return false; return false;
break; break;
case Qt::Key_Delete: case Qt::Key_Delete:
case Qt::Key_Backspace: case Qt::Key_Backspace:
emit onDelete(); emit onDelete();
if(!filterDeleteOn) if(!filterDeleteOn)
return false; return false;
break; break;
default: default:
return false; return false;
} }
return true; return true;
} }

View file

@ -2,29 +2,29 @@
#include <QEvent> #include <QEvent>
class KeySignals : public QObject { class KeySignals : public QObject {
Q_OBJECT Q_OBJECT
private: private:
bool filterDeleteOn; bool filterDeleteOn;
bool filterLROn; bool filterLROn;
signals: signals:
void onEnter(); void onEnter();
void onCtrlEnter(); void onCtrlEnter();
void onLeft(); void onLeft();
void onCtrlLeft(); void onCtrlLeft();
void onRight(); void onRight();
void onCtrlRight(); void onCtrlRight();
void onDelete(); void onDelete();
protected: protected:
virtual bool eventFilter(QObject *, QEvent *event); virtual bool eventFilter(QObject *, QEvent *event);
public: public:
KeySignals() KeySignals()
: filterDeleteOn(true) : filterDeleteOn(true)
, filterLROn(true) , filterLROn(true)
{} {}
void filterDelete(bool on) { filterDeleteOn = on; } void filterDelete(bool on) { filterDeleteOn = on; }
void filterLeftRight(bool on) { filterLROn = on; } void filterLeftRight(bool on) { filterLROn = on; }
}; };