Merge remote-tracking branch 'upstream/master' into cmake_qt5

This commit is contained in:
Fabio Bas 2014-06-24 18:13:26 +02:00
commit 5c13c06982
34 changed files with 1505 additions and 1221 deletions

View file

@ -2,6 +2,7 @@
#include <QTextStream>
#include <QVariant>
#include <QCryptographicHash>
#include <QDebug>
#include "decklist.h"
SideboardPlan::SideboardPlan(const QString &_name, const QList<MoveCard_ToZone> &_moveList)
@ -104,6 +105,13 @@ QString InnerDecklistNode::visibleNameFromName(const QString &_name)
return _name;
}
void InnerDecklistNode::setSortMethod(DeckSortMethod method)
{
sortMethod = method;
for (int i = 0; i < size(); i++)
at(i)->setSortMethod(method);
}
QString InnerDecklistNode::getVisibleName() const
{
return visibleNameFromName(name);
@ -163,21 +171,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<InnerDecklistNode *>(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<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
{
switch (sortMethod) {
case ByNumber:
return compareNumber(other);
case ByName:
return compareName(other);
case ByPrice:
return compareTotalPrice(other);
}
}
bool AbstractDecklistCardNode::compareNumber(AbstractDecklistNode *other) const
{
AbstractDecklistCardNode *other2 = dynamic_cast<AbstractDecklistCardNode *>(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<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 {
@ -360,6 +442,11 @@ void DeckList::write(QXmlStreamWriter *xml)
bool DeckList::loadFromXml(QXmlStreamReader *xml)
{
if (xml->error()) {
qDebug() << "Error loading deck from xml: " << xml->errorString();
return false;
}
cleanList();
while (!xml->atEnd()) {
xml->readNext();
@ -374,6 +461,10 @@ bool DeckList::loadFromXml(QXmlStreamReader *xml)
}
}
updateDeckHash();
if (xml->error()) {
qDebug() << "Error loading deck from xml: " << xml->errorString();
return false;
}
return true;
}
@ -396,8 +487,7 @@ QString DeckList::writeToString_Native()
bool DeckList::loadFromFile_Native(QIODevice *device)
{
QXmlStreamReader xml(device);
loadFromXml(&xml);
return true;
return loadFromXml(&xml);
}
bool DeckList::saveToFile_Native(QIODevice *device)
@ -415,7 +505,7 @@ bool DeckList::saveToFile_Native(QIODevice *device)
bool DeckList::loadFromStream_Plain(QTextStream &in)
{
cleanList();
InnerDecklistNode *main = 0, *side = 0;
bool inSideboard = false;
@ -449,7 +539,7 @@ bool DeckList::loadFromStream_Plain(QTextStream &in)
line.remove(rx);
rx.setPattern("\\(.*\\)");
line.remove(rx);
//Filter out post card name editions
//Filter out post card name editions
rx.setPattern("\\|.*$");
line.remove(rx);
line = line.simplified();
@ -462,10 +552,10 @@ bool DeckList::loadFromStream_Plain(QTextStream &in)
continue;
QString cardName = line.mid(i + 1);
// Common differences between cockatrice's card names
// and what's commonly used in decklists
// Common differences between cockatrice's card names
// and what's commonly used in decklists
rx.setPattern("");
cardName.replace(rx, "'");
cardName.replace(rx, "'");
rx.setPattern("Æ");
cardName.replace(rx, "AE");
rx.setPattern("^Aether");

View file

@ -29,24 +29,28 @@ public:
SideboardPlan(const QString &_name = QString(), const QList<MoveCard_ToZone> &_moveList = QList<MoveCard_ToZone>());
bool readElement(QXmlStreamReader *xml);
void write(QXmlStreamWriter *xml);
QString getName() const { return name; }
const QList<MoveCard_ToZone> &getMoveList() const { return moveList; }
void setMoveList(const QList<MoveCard_ToZone> &_moveList);
};
enum DeckSortMethod { ByNumber, ByName, ByPrice };
class AbstractDecklistNode {
protected:
InnerDecklistNode *parent;
DeckSortMethod sortMethod;
public:
AbstractDecklistNode(InnerDecklistNode *_parent = 0);
virtual ~AbstractDecklistNode() { }
virtual void setSortMethod(DeckSortMethod method) { sortMethod = method; }
virtual QString getName() const = 0;
InnerDecklistNode *getParent() const { return parent; }
int depth() const;
virtual int height() const = 0;
virtual bool compare(AbstractDecklistNode *other) const = 0;
virtual bool readElement(QXmlStreamReader *xml) = 0;
virtual void writeElement(QXmlStreamWriter *xml) = 0;
};
@ -59,6 +63,7 @@ public:
InnerDecklistNode(const QString &_name = QString(), InnerDecklistNode *_parent = 0) : AbstractDecklistNode(_parent), name(_name) { }
InnerDecklistNode(InnerDecklistNode *other, InnerDecklistNode *_parent = 0);
virtual ~InnerDecklistNode();
void setSortMethod(DeckSortMethod method);
QString getName() const { return name; }
void setName(const QString &_name) { name = _name; }
static QString visibleNameFromName(const QString &_name);
@ -69,8 +74,11 @@ 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<QPair<int, int> > sort(Qt::SortOrder order = Qt::AscendingOrder);
bool readElement(QXmlStreamReader *xml);
void writeElement(QXmlStreamWriter *xml);
};
@ -87,7 +95,10 @@ 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);
};