fixed issue #49: when editing a remote deck, be able to directly save it there

This commit is contained in:
Max-Wilhelm Bruker 2012-05-09 21:52:09 +02:00
parent f00eaa83d4
commit 0eee6d7ab9
23 changed files with 324 additions and 181 deletions

View file

@ -264,21 +264,19 @@ QVector<QPair<int, int> > InnerDecklistNode::sort(Qt::SortOrder order)
return result;
}
const QStringList DeckList::fileNameFilters = QStringList()
<< QObject::tr("Cockatrice decks (*.cod)")
<< QObject::tr("Plain text decks (*.dec *.mwDeck)")
<< QObject::tr("All files (*.*)");
DeckList::DeckList()
{
root = new InnerDecklistNode;
}
DeckList::DeckList(DeckList *other)
DeckList::DeckList(const DeckList &other)
: name(other.name),
comments(other.comments),
deckHash(other.deckHash)
{
root = new InnerDecklistNode(other->getRoot());
root = new InnerDecklistNode(other.getRoot());
QMapIterator<QString, SideboardPlan *> spIterator(other->getSideboardPlans());
QMapIterator<QString, SideboardPlan *> spIterator(other.getSideboardPlans());
while (spIterator.hasNext()) {
spIterator.next();
sideboardPlans.insert(spIterator.key(), new SideboardPlan(spIterator.key(), spIterator.value()->getMoveList()));
@ -289,9 +287,7 @@ DeckList::DeckList(DeckList *other)
DeckList::DeckList(const QString &nativeString)
{
root = new InnerDecklistNode;
QXmlStreamReader xml(nativeString);
loadFromXml(&xml);
loadFromString_Native(nativeString);
}
DeckList::~DeckList()
@ -362,13 +358,14 @@ void DeckList::write(QXmlStreamWriter *xml)
xml->writeEndElement();
}
void DeckList::loadFromXml(QXmlStreamReader *xml)
bool DeckList::loadFromXml(QXmlStreamReader *xml)
{
cleanList();
while (!xml->atEnd()) {
xml->readNext();
if (xml->isStartElement()) {
if (xml->name() != "cockatrice_deck")
return;
return false;
while (!xml->atEnd()) {
xml->readNext();
if (!readElement(xml))
@ -377,6 +374,13 @@ void DeckList::loadFromXml(QXmlStreamReader *xml)
}
}
updateDeckHash();
return true;
}
bool DeckList::loadFromString_Native(const QString &nativeString)
{
QXmlStreamReader xml(nativeString);
return loadFromXml(&xml);
}
QString DeckList::writeToString_Native()
@ -410,6 +414,8 @@ bool DeckList::saveToFile_Native(QIODevice *device)
bool DeckList::loadFromStream_Plain(QTextStream &in)
{
cleanList();
InnerDecklistNode *main = 0, *side = 0;
int okRows = 0;
@ -474,52 +480,13 @@ bool DeckList::saveToFile_Plain(QIODevice *device)
return saveToStream_Plain(out);
}
bool DeckList::loadFromFile(const QString &fileName, FileFormat fmt)
{
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return false;
cleanList();
bool result = false;
switch (fmt) {
case PlainTextFormat: result = loadFromFile_Plain(&file); break;
case CockatriceFormat: result = loadFromFile_Native(&file); break;
}
if (result)
emit deckLoaded();
return result;
}
bool DeckList::saveToFile(const QString &fileName, FileFormat fmt)
{
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return false;
bool result = false;
switch (fmt) {
case PlainTextFormat: result = saveToFile_Plain(&file); break;
case CockatriceFormat: result = saveToFile_Native(&file); break;
}
return result;
}
DeckList::FileFormat DeckList::getFormatFromNameFilter(const QString &selectedNameFilter)
{
switch (fileNameFilters.indexOf(selectedNameFilter)) {
case 0: return CockatriceFormat;
case 1: return PlainTextFormat;
}
return PlainTextFormat;
}
void DeckList::cleanList()
{
root->clearTree();
setName();
setComments();
updateDeckHash();
deckHash = QString();
emit deckHashChanged();
}
void DeckList::getCardListHelper(InnerDecklistNode *item, QSet<QString> &result) const

View file

@ -107,44 +107,36 @@ public:
void setName(const QString &_name) { name = _name; }
float getPrice() const { return price; }
void setPrice(const float _price) { price = _price; }
};
};
class DeckList : public QObject {
Q_OBJECT
public:
enum FileFormat { PlainTextFormat, CockatriceFormat };
private:
QString name, comments;
QString lastFileName;
QString deckHash;
FileFormat lastFileFormat;
QMap<QString, SideboardPlan *> sideboardPlans;
InnerDecklistNode *root;
QString currentElementText;
void getCardListHelper(InnerDecklistNode *node, QSet<QString> &result) const;
signals:
void deckLoaded();
void deckHashChanged();
public slots:
void setName(const QString &_name = QString()) { name = _name; }
void setComments(const QString &_comments = QString()) { comments = _comments; }
public:
static const QStringList fileNameFilters;
DeckList();
DeckList(DeckList *other);
DeckList(const DeckList &other);
DeckList(const QString &nativeString);
~DeckList();
QString getName() const { return name; }
QString getComments() const { return comments; }
QString getLastFileName() const { return lastFileName; }
FileFormat getLastFileFormat() const { return lastFileFormat; }
QList<MoveCard_ToZone> getCurrentSideboardPlan();
void setCurrentSideboardPlan(const QList<MoveCard_ToZone> &plan);
const QMap<QString, SideboardPlan *> &getSideboardPlans() const { return sideboardPlans; }
bool readElement(QXmlStreamReader *xml);
void write(QXmlStreamWriter *xml);
void loadFromXml(QXmlStreamReader *xml);
bool loadFromXml(QXmlStreamReader *xml);
bool loadFromString_Native(const QString &nativeString);
QString writeToString_Native();
bool loadFromFile_Native(QIODevice *device);
bool saveToFile_Native(QIODevice *device);
@ -152,9 +144,6 @@ public:
bool loadFromFile_Plain(QIODevice *device);
bool saveToStream_Plain(QTextStream &stream);
bool saveToFile_Plain(QIODevice *device);
bool loadFromFile(const QString &fileName, FileFormat fmt);
bool saveToFile(const QString &fileName, FileFormat fmt);
static FileFormat getFormatFromNameFilter(const QString &selectedNameFilter);
void cleanList();
bool isEmpty() const { return root->isEmpty() && name.isEmpty() && comments.isEmpty() && sideboardPlans.isEmpty(); }

View file

@ -4,7 +4,7 @@ message Command_DeckUpload {
extend SessionCommand {
optional Command_DeckUpload ext = 1013;
}
optional string path = 1;
optional string deck_list = 2;
optional string path = 1; // to upload a new deck
optional uint32 deck_id = 2; // to replace an existing deck
optional string deck_list = 3;
}