Add different compare methods for sorting

Give DeckList nodes the ability to sort based on name, price, or
number.
This commit is contained in:
Buce 2014-02-18 18:26:09 -06:00
parent ed5f02bf7a
commit a62ba91a5d
2 changed files with 96 additions and 6 deletions

View file

@ -104,6 +104,13 @@ QString InnerDecklistNode::visibleNameFromName(const QString &_name)
return _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 QString InnerDecklistNode::getVisibleName() const
{ {
return visibleNameFromName(name); return visibleNameFromName(name);
@ -163,22 +170,96 @@ float InnerDecklistNode::recursivePrice(bool countTotalCards) const
} }
bool InnerDecklistNode::compare(AbstractDecklistNode *other) 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<InnerDecklistNode *>(other); InnerDecklistNode *other2 = dynamic_cast<InnerDecklistNode *>(other);
if (other2) if (other2) {
return (getName() > other->getName()); int n1 = recursiveCount(true);
else int n2 = other2->recursiveCount(true);
return (n1 != n2) ? (n1 > n2) : compareName(other);
} else {
return false; return false;
} }
}
bool InnerDecklistNode::compareName(AbstractDecklistNode *other) const
{
InnerDecklistNode *other2 = dynamic_cast<InnerDecklistNode *>(other);
if (other2) {
return (getName() > other2->getName());
} else {
return false;
}
}
bool InnerDecklistNode::comparePrice(AbstractDecklistNode *other) const
{
InnerDecklistNode *other2 = dynamic_cast<InnerDecklistNode *>(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 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<AbstractDecklistCardNode *>(other); AbstractDecklistCardNode *other2 = dynamic_cast<AbstractDecklistCardNode *>(other);
if (other2) if (other2) {
return (getName() > other->getName()); int n1 = getNumber();
else int n2 = other2->getNumber();
return (n1 != n2) ? (n1 > n2) : compareName(other);
} else {
return true; return true;
} }
}
bool AbstractDecklistCardNode::compareName(AbstractDecklistNode *other) const
{
AbstractDecklistCardNode *other2 = dynamic_cast<AbstractDecklistCardNode *>(other);
if (other2) {
return (getName() > other2->getName());
} else {
return true;
}
}
bool AbstractDecklistCardNode::compareTotalPrice(AbstractDecklistNode *other) const
{
AbstractDecklistCardNode *other2 = dynamic_cast<AbstractDecklistCardNode *>(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 { class InnerDecklistNode::compareFunctor {
private: private:

View file

@ -38,9 +38,11 @@ public:
class AbstractDecklistNode { class AbstractDecklistNode {
protected: protected:
InnerDecklistNode *parent; InnerDecklistNode *parent;
int sortMethod;
public: public:
AbstractDecklistNode(InnerDecklistNode *_parent = 0); AbstractDecklistNode(InnerDecklistNode *_parent = 0);
virtual ~AbstractDecklistNode() { } virtual ~AbstractDecklistNode() { }
virtual void setSortMethod(int method) { sortMethod = method; }
virtual QString getName() const = 0; virtual QString getName() const = 0;
InnerDecklistNode *getParent() const { return parent; } InnerDecklistNode *getParent() const { return parent; }
int depth() const; int depth() const;
@ -59,6 +61,7 @@ public:
InnerDecklistNode(const QString &_name = QString(), InnerDecklistNode *_parent = 0) : AbstractDecklistNode(_parent), name(_name) { } InnerDecklistNode(const QString &_name = QString(), InnerDecklistNode *_parent = 0) : AbstractDecklistNode(_parent), name(_name) { }
InnerDecklistNode(InnerDecklistNode *other, InnerDecklistNode *_parent = 0); InnerDecklistNode(InnerDecklistNode *other, InnerDecklistNode *_parent = 0);
virtual ~InnerDecklistNode(); virtual ~InnerDecklistNode();
void setSortMethod(int method);
QString getName() const { return name; } QString getName() const { return name; }
void setName(const QString &_name) { name = _name; } void setName(const QString &_name) { name = _name; }
static QString visibleNameFromName(const QString &_name); static QString visibleNameFromName(const QString &_name);
@ -69,6 +72,9 @@ public:
int recursiveCount(bool countTotalCards = false) const; int recursiveCount(bool countTotalCards = false) const;
float recursivePrice(bool countTotalCards = false) const; float recursivePrice(bool countTotalCards = false) const;
bool compare(AbstractDecklistNode *other) const; bool compare(AbstractDecklistNode *other) const;
bool compareNumber(AbstractDecklistNode *other) const;
bool compareName(AbstractDecklistNode *other) const;
bool comparePrice(AbstractDecklistNode *other) const;
QVector<QPair<int, int> > sort(Qt::SortOrder order = Qt::AscendingOrder); QVector<QPair<int, int> > sort(Qt::SortOrder order = Qt::AscendingOrder);
bool readElement(QXmlStreamReader *xml); bool readElement(QXmlStreamReader *xml);
@ -87,6 +93,9 @@ public:
float getTotalPrice() const { return getNumber() * getPrice(); } float getTotalPrice() const { return getNumber() * getPrice(); }
int height() const { return 0; } int height() const { return 0; }
bool compare(AbstractDecklistNode *other) const; bool compare(AbstractDecklistNode *other) const;
bool compareNumber(AbstractDecklistNode *other) const;
bool compareName(AbstractDecklistNode *other) const;
bool compareTotalPrice(AbstractDecklistNode *other) const;
bool readElement(QXmlStreamReader *xml); bool readElement(QXmlStreamReader *xml);
void writeElement(QXmlStreamWriter *xml); void writeElement(QXmlStreamWriter *xml);