decklist moved to common
This commit is contained in:
parent
c1b7522840
commit
c5bf72b1bf
3 changed files with 109 additions and 59 deletions
|
@ -8,7 +8,7 @@
|
||||||
#include "carddatabase.h"
|
#include "carddatabase.h"
|
||||||
|
|
||||||
AbstractDecklistNode::AbstractDecklistNode(InnerDecklistNode *_parent)
|
AbstractDecklistNode::AbstractDecklistNode(InnerDecklistNode *_parent)
|
||||||
: parent(_parent)
|
: parent(_parent), currentItem(0)
|
||||||
{
|
{
|
||||||
if (parent)
|
if (parent)
|
||||||
parent->append(this);
|
parent->append(this);
|
||||||
|
@ -102,6 +102,47 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
bool InnerDecklistNode::readElement(QXmlStreamReader *xml)
|
||||||
|
{
|
||||||
|
if (currentItem) {
|
||||||
|
if (currentItem->readElement(xml))
|
||||||
|
currentItem = 0;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (xml->isStartElement() && (xml->name() == "zone"))
|
||||||
|
currentItem = new InnerDecklistNode(xml->attributes().value("name").toString(), this);
|
||||||
|
else if (xml->isStartElement() && (xml->name() == "card"))
|
||||||
|
currentItem = new DecklistCardNode(xml->attributes().value("name").toString(), xml->attributes().value("number").toString().toInt(), this);
|
||||||
|
else if (xml->isEndElement() && (xml->name() == "zone"))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void InnerDecklistNode::writeElement(QXmlStreamWriter *xml)
|
||||||
|
{
|
||||||
|
xml->writeStartElement("zone");
|
||||||
|
xml->writeAttribute("name", name);
|
||||||
|
for (int i = 0; i < size(); i++)
|
||||||
|
at(i)->writeElement(xml);
|
||||||
|
xml->writeEndElement(); // zone
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AbstractDecklistCardNode::readElement(QXmlStreamReader *xml)
|
||||||
|
{
|
||||||
|
if (xml->isEndElement())
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void AbstractDecklistCardNode::writeElement(QXmlStreamWriter *xml)
|
||||||
|
{
|
||||||
|
xml->writeEmptyElement("card");
|
||||||
|
xml->writeAttribute("number", QString::number(getNumber()));
|
||||||
|
xml->writeAttribute("name", getName());
|
||||||
|
}
|
||||||
|
|
||||||
QVector<QPair<int, int> > InnerDecklistNode::sort(Qt::SortOrder order)
|
QVector<QPair<int, int> > InnerDecklistNode::sort(Qt::SortOrder order)
|
||||||
{
|
{
|
||||||
QVector<QPair<int, int> > result(size());
|
QVector<QPair<int, int> > result(size());
|
||||||
|
@ -129,7 +170,7 @@ QVector<QPair<int, int> > InnerDecklistNode::sort(Qt::SortOrder order)
|
||||||
}
|
}
|
||||||
|
|
||||||
DeckList::DeckList(QObject *parent)
|
DeckList::DeckList(QObject *parent)
|
||||||
: QObject(parent)
|
: QObject(parent), currentZone(0)
|
||||||
{
|
{
|
||||||
root = new InnerDecklistNode;
|
root = new InnerDecklistNode;
|
||||||
}
|
}
|
||||||
|
@ -139,63 +180,62 @@ DeckList::~DeckList()
|
||||||
delete root;
|
delete root;
|
||||||
}
|
}
|
||||||
|
|
||||||
QXmlStreamReader &operator>>(QXmlStreamReader &xml, InnerDecklistNode *node)
|
bool DeckList::readElement(QXmlStreamReader *xml)
|
||||||
{
|
{
|
||||||
while (!xml.atEnd()) {
|
if (currentZone) {
|
||||||
if (xml.readNext() == QXmlStreamReader::EndElement)
|
if (currentZone->readElement(xml))
|
||||||
break;
|
currentZone = 0;
|
||||||
if (xml.name() == "zone")
|
return true;
|
||||||
xml >> new InnerDecklistNode(xml.attributes().value("name").toString(), node);
|
|
||||||
else if (xml.name() == "card") {
|
|
||||||
const int number = xml.attributes().value("number").toString().toInt();
|
|
||||||
const QString card = xml.attributes().value("name").toString();
|
|
||||||
new DecklistCardNode(card, number, node);
|
|
||||||
while (!xml.atEnd())
|
|
||||||
if (xml.readNext() == QXmlStreamReader::EndElement)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return xml;
|
|
||||||
|
if (xml->isEndElement()) {
|
||||||
|
if (xml->name() == "deckname")
|
||||||
|
name = currentElementText;
|
||||||
|
else if (xml->name() == "comments")
|
||||||
|
comments = currentElementText;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
currentElementText.clear();
|
||||||
|
} else if (xml->isStartElement() && (xml->name() == "zone"))
|
||||||
|
currentZone = new InnerDecklistNode(xml->attributes().value("name").toString(), root);
|
||||||
|
else if (xml->isCharacters() && !xml->isWhitespace())
|
||||||
|
currentElementText = xml->text().toString();
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DeckList::writeElement(QXmlStreamWriter *xml)
|
||||||
|
{
|
||||||
|
xml->writeStartElement("cockatrice_deck");
|
||||||
|
xml->writeAttribute("version", "1");
|
||||||
|
xml->writeTextElement("deckname", name);
|
||||||
|
xml->writeTextElement("comments", comments);
|
||||||
|
|
||||||
|
for (int i = 0; i < root->size(); i++)
|
||||||
|
root->at(i)->writeElement(xml);
|
||||||
|
|
||||||
|
xml->writeEndElement(); // cockatrice_deck
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DeckList::loadFromFile_Native(QIODevice *device)
|
bool DeckList::loadFromFile_Native(QIODevice *device)
|
||||||
{
|
{
|
||||||
QXmlStreamReader xml(device);
|
QXmlStreamReader xml(device);
|
||||||
while (!xml.atEnd()) {
|
while (!xml.atEnd()) {
|
||||||
if (xml.readNext() == QXmlStreamReader::StartElement) {
|
xml.readNext();
|
||||||
|
if (xml.isStartElement()) {
|
||||||
if (xml.name() != "cockatrice_deck")
|
if (xml.name() != "cockatrice_deck")
|
||||||
return false;
|
return false;
|
||||||
while (!xml.atEnd()) {
|
while (!xml.atEnd()) {
|
||||||
if (xml.readNext() == QXmlStreamReader::EndElement)
|
xml.readNext();
|
||||||
break;
|
if (!readElement(&xml))
|
||||||
if (xml.name() == "deckname")
|
if (xml.isEndElement() && (xml.name() == "cockatrice_deck"))
|
||||||
name = xml.readElementText();
|
return true;
|
||||||
else if (xml.name() == "comments")
|
|
||||||
comments = xml.readElementText();
|
|
||||||
else if (xml.name() == "zone")
|
|
||||||
xml >> new InnerDecklistNode(xml.attributes().value("name").toString(), root);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
QXmlStreamWriter &operator<<(QXmlStreamWriter &xml, const AbstractDecklistNode *node)
|
|
||||||
{
|
|
||||||
const InnerDecklistNode *inner = dynamic_cast<const InnerDecklistNode *>(node);
|
|
||||||
if (inner) {
|
|
||||||
xml.writeStartElement("zone");
|
|
||||||
xml.writeAttribute("name", inner->getName());
|
|
||||||
for (int i = 0; i < inner->size(); i++)
|
|
||||||
xml << inner->at(i);
|
|
||||||
xml.writeEndElement(); // zone
|
|
||||||
} else {
|
|
||||||
const AbstractDecklistCardNode *card = dynamic_cast<const AbstractDecklistCardNode *>(node);
|
|
||||||
xml.writeEmptyElement("card");
|
|
||||||
xml.writeAttribute("number", QString::number(card->getNumber()));
|
|
||||||
xml.writeAttribute("name", card->getName());
|
|
||||||
}
|
|
||||||
return xml;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DeckList::saveToFile_Native(QIODevice *device)
|
bool DeckList::saveToFile_Native(QIODevice *device)
|
||||||
|
@ -204,15 +244,7 @@ bool DeckList::saveToFile_Native(QIODevice *device)
|
||||||
xml.setAutoFormatting(true);
|
xml.setAutoFormatting(true);
|
||||||
xml.writeStartDocument();
|
xml.writeStartDocument();
|
||||||
|
|
||||||
xml.writeStartElement("cockatrice_deck");
|
writeElement(&xml);
|
||||||
xml.writeAttribute("version", "1");
|
|
||||||
xml.writeTextElement("deckname", name);
|
|
||||||
xml.writeTextElement("comments", comments);
|
|
||||||
|
|
||||||
for (int i = 0; i < root->size(); i++)
|
|
||||||
xml << root->at(i);
|
|
||||||
|
|
||||||
xml.writeEndElement(); // cockatrice_deck
|
|
||||||
|
|
||||||
xml.writeEndDocument();
|
xml.writeEndDocument();
|
||||||
return true;
|
return true;
|
|
@ -8,12 +8,15 @@
|
||||||
|
|
||||||
class CardDatabase;
|
class CardDatabase;
|
||||||
class QIODevice;
|
class QIODevice;
|
||||||
|
class QXmlStreamReader;
|
||||||
|
class QXmlStreamWriter;
|
||||||
|
|
||||||
class InnerDecklistNode;
|
class InnerDecklistNode;
|
||||||
|
|
||||||
class AbstractDecklistNode {
|
class AbstractDecklistNode {
|
||||||
protected:
|
protected:
|
||||||
InnerDecklistNode *parent;
|
InnerDecklistNode *parent;
|
||||||
|
AbstractDecklistNode *currentItem;
|
||||||
public:
|
public:
|
||||||
AbstractDecklistNode(InnerDecklistNode *_parent = 0);
|
AbstractDecklistNode(InnerDecklistNode *_parent = 0);
|
||||||
virtual ~AbstractDecklistNode() { }
|
virtual ~AbstractDecklistNode() { }
|
||||||
|
@ -22,6 +25,9 @@ public:
|
||||||
int depth() const;
|
int depth() const;
|
||||||
virtual int height() const = 0;
|
virtual int height() const = 0;
|
||||||
virtual bool compare(AbstractDecklistNode *other) const = 0;
|
virtual bool compare(AbstractDecklistNode *other) const = 0;
|
||||||
|
|
||||||
|
virtual bool readElement(QXmlStreamReader *xml) = 0;
|
||||||
|
virtual void writeElement(QXmlStreamWriter *xml) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class InnerDecklistNode : public AbstractDecklistNode, public QList<AbstractDecklistNode *> {
|
class InnerDecklistNode : public AbstractDecklistNode, public QList<AbstractDecklistNode *> {
|
||||||
|
@ -40,6 +46,9 @@ public:
|
||||||
int recursiveCount(bool countTotalCards = false) const;
|
int recursiveCount(bool countTotalCards = false) const;
|
||||||
bool compare(AbstractDecklistNode *other) const;
|
bool compare(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);
|
||||||
|
void writeElement(QXmlStreamWriter *xml);
|
||||||
};
|
};
|
||||||
|
|
||||||
class AbstractDecklistCardNode : public AbstractDecklistNode {
|
class AbstractDecklistCardNode : public AbstractDecklistNode {
|
||||||
|
@ -51,6 +60,9 @@ public:
|
||||||
virtual void setName(const QString &_name) = 0;
|
virtual void setName(const QString &_name) = 0;
|
||||||
int height() const { return 0; }
|
int height() const { return 0; }
|
||||||
bool compare(AbstractDecklistNode *other) const;
|
bool compare(AbstractDecklistNode *other) const;
|
||||||
|
|
||||||
|
bool readElement(QXmlStreamReader *xml);
|
||||||
|
void writeElement(QXmlStreamWriter *xml);
|
||||||
};
|
};
|
||||||
|
|
||||||
class DecklistCardNode : public AbstractDecklistCardNode {
|
class DecklistCardNode : public AbstractDecklistCardNode {
|
||||||
|
@ -74,6 +86,8 @@ private:
|
||||||
QString lastFileName;
|
QString lastFileName;
|
||||||
FileFormat lastFileFormat;
|
FileFormat lastFileFormat;
|
||||||
InnerDecklistNode *root;
|
InnerDecklistNode *root;
|
||||||
|
InnerDecklistNode *currentZone;
|
||||||
|
QString currentElementText;
|
||||||
signals:
|
signals:
|
||||||
void deckLoaded();
|
void deckLoaded();
|
||||||
public slots:
|
public slots:
|
||||||
|
@ -87,6 +101,9 @@ public:
|
||||||
QString getLastFileName() const { return lastFileName; }
|
QString getLastFileName() const { return lastFileName; }
|
||||||
FileFormat getLastFileFormat() const { return lastFileFormat; }
|
FileFormat getLastFileFormat() const { return lastFileFormat; }
|
||||||
|
|
||||||
|
bool readElement(QXmlStreamReader *xml);
|
||||||
|
void writeElement(QXmlStreamWriter *xml);
|
||||||
|
|
||||||
bool loadFromFile_Native(QIODevice *device);
|
bool loadFromFile_Native(QIODevice *device);
|
||||||
bool saveToFile_Native(QIODevice *device);
|
bool saveToFile_Native(QIODevice *device);
|
||||||
bool loadFromFile_Plain(QIODevice *device);
|
bool loadFromFile_Plain(QIODevice *device);
|
|
@ -156,7 +156,7 @@ bool Response_DeckList::Directory::readElement(QXmlStreamReader *xml)
|
||||||
if (currentItem) {
|
if (currentItem) {
|
||||||
if (currentItem->readElement(xml))
|
if (currentItem->readElement(xml))
|
||||||
currentItem = 0;
|
currentItem = 0;
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
if (xml->isStartElement() && (xml->name() == "directory")) {
|
if (xml->isStartElement() && (xml->name() == "directory")) {
|
||||||
currentItem = new Directory(xml->attributes().value("name").toString());
|
currentItem = new Directory(xml->attributes().value("name").toString());
|
||||||
|
@ -164,9 +164,10 @@ bool Response_DeckList::Directory::readElement(QXmlStreamReader *xml)
|
||||||
} else if (xml->isStartElement() && (xml->name() == "file")) {
|
} else if (xml->isStartElement() && (xml->name() == "file")) {
|
||||||
currentItem = new File(xml->attributes().value("name").toString(), xml->attributes().value("id").toString().toInt());
|
currentItem = new File(xml->attributes().value("name").toString(), xml->attributes().value("id").toString().toInt());
|
||||||
append(currentItem);
|
append(currentItem);
|
||||||
} else
|
} else if (xml->isEndElement() && (xml->name() == "directory"))
|
||||||
return false;
|
return true;
|
||||||
return true;
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Response_DeckList::Directory::writeElement(QXmlStreamWriter *xml)
|
void Response_DeckList::Directory::writeElement(QXmlStreamWriter *xml)
|
||||||
|
@ -196,7 +197,7 @@ bool Response_DeckList::readElement(QXmlStreamReader *xml)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return root->readElement(xml);
|
return !root->readElement(xml);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Response_DeckList::writeElement(QXmlStreamWriter *xml)
|
void Response_DeckList::writeElement(QXmlStreamWriter *xml)
|
||||||
|
|
Loading…
Reference in a new issue