diff --git a/common/decklist.cpp b/common/decklist.cpp index d9a3d7f3..11119945 100644 --- a/common/decklist.cpp +++ b/common/decklist.cpp @@ -104,6 +104,13 @@ QString InnerDecklistNode::visibleNameFromName(const QString &_name) return _name; } +void InnerDecklistNode::setSortMethod(int method) +{ + sortMethod = method; + for (int i = 0; i < size(); i++) + at(i)->setSortMethod(method); +} + QString InnerDecklistNode::getVisibleName() const { return visibleNameFromName(name); @@ -163,21 +170,95 @@ float InnerDecklistNode::recursivePrice(bool countTotalCards) const } bool InnerDecklistNode::compare(AbstractDecklistNode *other) const +{ + switch (sortMethod) { + case 0: + return compareNumber(other); + case 1: + return compareName(other); + case 2: + return comparePrice(other); + } +} + +bool InnerDecklistNode::compareNumber(AbstractDecklistNode *other) const { InnerDecklistNode *other2 = dynamic_cast(other); - if (other2) - return (getName() > other->getName()); - else + if (other2) { + int n1 = recursiveCount(true); + int n2 = other2->recursiveCount(true); + return (n1 != n2) ? (n1 > n2) : compareName(other); + } else { return false; + } +} + +bool InnerDecklistNode::compareName(AbstractDecklistNode *other) const +{ + InnerDecklistNode *other2 = dynamic_cast(other); + if (other2) { + return (getName() > other2->getName()); + } else { + return false; + } +} + +bool InnerDecklistNode::comparePrice(AbstractDecklistNode *other) const +{ + InnerDecklistNode *other2 = dynamic_cast(other); + if (other2) { + int p1 = 100*recursivePrice(true); + int p2 = 100*other2->recursivePrice(true); + return (p1 != p2) ? (p1 > p2) : compareName(other); + } else { + return false; + } } bool AbstractDecklistCardNode::compare(AbstractDecklistNode *other) const +{ + switch (sortMethod) { + case 0: + return compareNumber(other); + case 1: + return compareName(other); + case 2: + return compareTotalPrice(other); + } +} + +bool AbstractDecklistCardNode::compareNumber(AbstractDecklistNode *other) const { AbstractDecklistCardNode *other2 = dynamic_cast(other); - if (other2) - return (getName() > other->getName()); - else + if (other2) { + int n1 = getNumber(); + int n2 = other2->getNumber(); + return (n1 != n2) ? (n1 > n2) : compareName(other); + } else { return true; + } +} + +bool AbstractDecklistCardNode::compareName(AbstractDecklistNode *other) const +{ + AbstractDecklistCardNode *other2 = dynamic_cast(other); + if (other2) { + return (getName() > other2->getName()); + } else { + return true; + } +} + +bool AbstractDecklistCardNode::compareTotalPrice(AbstractDecklistNode *other) const +{ + AbstractDecklistCardNode *other2 = dynamic_cast(other); + if (other2) { + int p1 = 100*getTotalPrice(); + int p2 = 100*other2->getTotalPrice(); + return (p1 != p2) ? (p1 > p2) : compareName(other); + } else { + return true; + } } class InnerDecklistNode::compareFunctor { diff --git a/common/decklist.h b/common/decklist.h index c5535c2d..6d1579fc 100644 --- a/common/decklist.h +++ b/common/decklist.h @@ -38,9 +38,11 @@ public: class AbstractDecklistNode { protected: InnerDecklistNode *parent; + int sortMethod; public: AbstractDecklistNode(InnerDecklistNode *_parent = 0); virtual ~AbstractDecklistNode() { } + virtual void setSortMethod(int method) { sortMethod = method; } virtual QString getName() const = 0; InnerDecklistNode *getParent() const { return parent; } int depth() const; @@ -59,6 +61,7 @@ public: InnerDecklistNode(const QString &_name = QString(), InnerDecklistNode *_parent = 0) : AbstractDecklistNode(_parent), name(_name) { } InnerDecklistNode(InnerDecklistNode *other, InnerDecklistNode *_parent = 0); virtual ~InnerDecklistNode(); + void setSortMethod(int method); QString getName() const { return name; } void setName(const QString &_name) { name = _name; } static QString visibleNameFromName(const QString &_name); @@ -69,6 +72,9 @@ public: int recursiveCount(bool countTotalCards = false) const; float recursivePrice(bool countTotalCards = false) const; bool compare(AbstractDecklistNode *other) const; + bool compareNumber(AbstractDecklistNode *other) const; + bool compareName(AbstractDecklistNode *other) const; + bool comparePrice(AbstractDecklistNode *other) const; QVector > sort(Qt::SortOrder order = Qt::AscendingOrder); bool readElement(QXmlStreamReader *xml); @@ -87,6 +93,9 @@ public: float getTotalPrice() const { return getNumber() * getPrice(); } int height() const { return 0; } bool compare(AbstractDecklistNode *other) const; + bool compareNumber(AbstractDecklistNode *other) const; + bool compareName(AbstractDecklistNode *other) const; + bool compareTotalPrice(AbstractDecklistNode *other) const; bool readElement(QXmlStreamReader *xml); void writeElement(QXmlStreamWriter *xml);