Commit 0eee6d7a authored by Max-Wilhelm Bruker's avatar Max-Wilhelm Bruker
Browse files

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

parent f00eaa83
...@@ -28,6 +28,7 @@ SET(cockatrice_SOURCES ...@@ -28,6 +28,7 @@ SET(cockatrice_SOURCES
src/gameview.cpp src/gameview.cpp
src/gameselector.cpp src/gameselector.cpp
src/decklistmodel.cpp src/decklistmodel.cpp
src/deck_loader.cpp
src/dlg_load_deck_from_clipboard.cpp src/dlg_load_deck_from_clipboard.cpp
src/dlg_load_remote_deck.cpp src/dlg_load_remote_deck.cpp
src/cardinfowidget.cpp src/cardinfowidget.cpp
...@@ -101,6 +102,7 @@ SET(cockatrice_HEADERS ...@@ -101,6 +102,7 @@ SET(cockatrice_HEADERS
src/gameview.h src/gameview.h
src/gameselector.h src/gameselector.h
src/decklistmodel.h src/decklistmodel.h
src/deck_loader.h
src/dlg_load_deck_from_clipboard.h src/dlg_load_deck_from_clipboard.h
src/dlg_load_remote_deck.h src/dlg_load_remote_deck.h
src/cardinfowidget.h src/cardinfowidget.h
......
#include <QStringList>
#include <QFile>
#include "deck_loader.h"
#include "decklist.h"
const QStringList DeckLoader::fileNameFilters = QStringList()
<< QObject::tr("Cockatrice decks (*.cod)")
<< QObject::tr("Plain text decks (*.dec *.mwDeck)")
<< QObject::tr("All files (*.*)");
DeckLoader::DeckLoader()
: DeckList(),
lastFileName(QString()),
lastFileFormat(CockatriceFormat),
lastRemoteDeckId(-1)
{
}
DeckLoader::DeckLoader(const QString &nativeString)
: DeckList(nativeString),
lastFileName(QString()),
lastFileFormat(CockatriceFormat),
lastRemoteDeckId(-1)
{
}
DeckLoader::DeckLoader(const DeckList &other)
: DeckList(other),
lastFileName(QString()),
lastFileFormat(CockatriceFormat),
lastRemoteDeckId(-1)
{
}
DeckLoader::DeckLoader(const DeckLoader &other)
: DeckList(other),
lastFileName(other.lastFileName),
lastFileFormat(other.lastFileFormat),
lastRemoteDeckId(other.lastRemoteDeckId)
{
}
bool DeckLoader::loadFromFile(const QString &fileName, FileFormat fmt)
{
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return false;
bool result = false;
switch (fmt) {
case PlainTextFormat: result = loadFromFile_Plain(&file); break;
case CockatriceFormat: result = loadFromFile_Native(&file); break;
}
if (result) {
lastFileName = fileName;
lastFileFormat = fmt;
emit deckLoaded();
}
return result;
}
bool DeckLoader::loadFromRemote(const QString &nativeString, int remoteDeckId)
{
bool result = loadFromString_Native(nativeString);
if (result) {
lastFileName = QString();
lastFileFormat = CockatriceFormat;
lastRemoteDeckId = remoteDeckId;
emit deckLoaded();
}
return result;
}
bool DeckLoader::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;
}
if (result) {
lastFileName = fileName;
lastFileFormat = fmt;
}
return result;
}
DeckLoader::FileFormat DeckLoader::getFormatFromNameFilter(const QString &selectedNameFilter)
{
switch (fileNameFilters.indexOf(selectedNameFilter)) {
case 0: return CockatriceFormat;
case 1: return PlainTextFormat;
}
return PlainTextFormat;
}
#ifndef DECK_LOADER_H
#define DECK_LOADER_H
#include "decklist.h"
class DeckLoader : public DeckList {
Q_OBJECT
signals:
void deckLoaded();
public:
enum FileFormat { PlainTextFormat, CockatriceFormat };
static const QStringList fileNameFilters;
private:
QString lastFileName;
FileFormat lastFileFormat;
int lastRemoteDeckId;
public:
DeckLoader();
DeckLoader(const QString &nativeString);
DeckLoader(const DeckList &other);
DeckLoader(const DeckLoader &other);
const QString &getLastFileName() const { return lastFileName; }
FileFormat getLastFileFormat() const { return lastFileFormat; }
int getLastRemoteDeckId() const { return lastRemoteDeckId; }
static FileFormat getFormatFromNameFilter(const QString &selectedNameFilter);
bool loadFromFile(const QString &fileName, FileFormat fmt);
bool loadFromRemote(const QString &nativeString, int remoteDeckId);
bool saveToFile(const QString &fileName, FileFormat fmt);
};
#endif
...@@ -11,11 +11,12 @@ ...@@ -11,11 +11,12 @@
#include "decklistmodel.h" #include "decklistmodel.h"
#include "carddatabase.h" #include "carddatabase.h"
#include "settingscache.h" #include "settingscache.h"
#include "deck_loader.h"
DeckListModel::DeckListModel(QObject *parent) DeckListModel::DeckListModel(QObject *parent)
: QAbstractItemModel(parent) : QAbstractItemModel(parent)
{ {
deckList = new DeckList; deckList = new DeckLoader;
connect(deckList, SIGNAL(deckLoaded()), this, SLOT(rebuildTree())); connect(deckList, SIGNAL(deckLoaded()), this, SLOT(rebuildTree()));
connect(deckList, SIGNAL(deckHashChanged()), this, SIGNAL(deckHashChanged())); connect(deckList, SIGNAL(deckHashChanged()), this, SIGNAL(deckHashChanged()));
root = new InnerDecklistNode; root = new InnerDecklistNode;
...@@ -304,12 +305,10 @@ void DeckListModel::sort(int /*column*/, Qt::SortOrder order) ...@@ -304,12 +305,10 @@ void DeckListModel::sort(int /*column*/, Qt::SortOrder order)
void DeckListModel::cleanList() void DeckListModel::cleanList()
{ {
root->clearTree(); setDeckList(new DeckLoader);
deckList->cleanList();
reset();
} }
void DeckListModel::setDeckList(DeckList *_deck) void DeckListModel::setDeckList(DeckLoader *_deck)
{ {
delete deckList; delete deckList;
deckList = _deck; deckList = _deck;
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include <QList> #include <QList>
#include "decklist.h" #include "decklist.h"
class DeckLoader;
class CardDatabase; class CardDatabase;
class QProgressDialog; class QProgressDialog;
class QPrinter; class QPrinter;
...@@ -47,11 +48,11 @@ public: ...@@ -47,11 +48,11 @@ public:
QModelIndex addCard(const QString &cardName, const QString &zoneName); QModelIndex addCard(const QString &cardName, const QString &zoneName);
void sort(int column, Qt::SortOrder order = Qt::AscendingOrder); void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);
void cleanList(); void cleanList();
DeckList *getDeckList() const { return deckList; } DeckLoader *getDeckList() const { return deckList; }
void setDeckList(DeckList *_deck); void setDeckList(DeckLoader *_deck);
void pricesUpdated(InnerDecklistNode *node = 0); void pricesUpdated(InnerDecklistNode *node = 0);
private: private:
DeckList *deckList; DeckLoader *deckList;
InnerDecklistNode *root; InnerDecklistNode *root;
InnerDecklistNode *createNodeIfNeeded(const QString &name, InnerDecklistNode *parent); InnerDecklistNode *createNodeIfNeeded(const QString &name, InnerDecklistNode *parent);
QModelIndex nodeToIndex(AbstractDecklistNode *node) const; QModelIndex nodeToIndex(AbstractDecklistNode *node) const;
......
...@@ -285,12 +285,12 @@ void DeckViewScene::clearContents() ...@@ -285,12 +285,12 @@ void DeckViewScene::clearContents()
cardContainers.clear(); cardContainers.clear();
} }
void DeckViewScene::setDeck(DeckList *_deck) void DeckViewScene::setDeck(const DeckList &_deck)
{ {
if (deck) if (deck)
delete deck; delete deck;
deck = _deck; deck = new DeckList(_deck);
rebuildTree(); rebuildTree();
applySideboardPlan(deck->getCurrentSideboardPlan()); applySideboardPlan(deck->getCurrentSideboardPlan());
rearrangeItems(); rearrangeItems();
...@@ -475,7 +475,7 @@ void DeckView::updateSceneRect(const QRectF &rect) ...@@ -475,7 +475,7 @@ void DeckView::updateSceneRect(const QRectF &rect)
fitInView(rect, Qt::KeepAspectRatio); fitInView(rect, Qt::KeepAspectRatio);
} }
void DeckView::setDeck(DeckList *_deck) void DeckView::setDeck(const DeckList &_deck)
{ {
deckViewScene->setDeck(_deck); deckViewScene->setDeck(_deck);
} }
......
...@@ -89,7 +89,7 @@ public: ...@@ -89,7 +89,7 @@ public:
~DeckViewScene(); ~DeckViewScene();
void setLocked(bool _locked) { locked = _locked; } void setLocked(bool _locked) { locked = _locked; }
bool getLocked() const { return locked; } bool getLocked() const { return locked; }
void setDeck(DeckList *_deck); void setDeck(const DeckList &_deck);
void setOptimalAspectRatio(qreal _optimalAspectRatio) { optimalAspectRatio = _optimalAspectRatio; } void setOptimalAspectRatio(qreal _optimalAspectRatio) { optimalAspectRatio = _optimalAspectRatio; }
void rearrangeItems(); void rearrangeItems();
void updateContents(); void updateContents();
...@@ -110,7 +110,7 @@ signals: ...@@ -110,7 +110,7 @@ signals:
void sideboardPlanChanged(); void sideboardPlanChanged();
public: public:
DeckView(QWidget *parent = 0); DeckView(QWidget *parent = 0);
void setDeck(DeckList *_deck); void setDeck(const DeckList &_deck);
void setLocked(bool _locked) { deckViewScene->setLocked(_locked); } void setLocked(bool _locked) { deckViewScene->setLocked(_locked); }
QList<MoveCard_ToZone> getSideboardPlan() const { return deckViewScene->getSideboardPlan(); } QList<MoveCard_ToZone> getSideboardPlan() const { return deckViewScene->getSideboardPlan(); }
void resetSideboardPlan(); void resetSideboardPlan();
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
#include <QDialogButtonBox> #include <QDialogButtonBox>
#include <QMessageBox> #include <QMessageBox>
#include "dlg_load_deck_from_clipboard.h" #include "dlg_load_deck_from_clipboard.h"
#include "decklist.h" #include "deck_loader.h"
DlgLoadDeckFromClipboard::DlgLoadDeckFromClipboard(QWidget *parent) DlgLoadDeckFromClipboard::DlgLoadDeckFromClipboard(QWidget *parent)
: QDialog(parent), deckList(0) : QDialog(parent), deckList(0)
...@@ -47,7 +47,7 @@ void DlgLoadDeckFromClipboard::actOK() ...@@ -47,7 +47,7 @@ void DlgLoadDeckFromClipboard::actOK()
QString buffer = contentsEdit->toPlainText(); QString buffer = contentsEdit->toPlainText();
QTextStream stream(&buffer); QTextStream stream(&buffer);
DeckList *l = new DeckList; DeckLoader *l = new DeckLoader;
if (l->loadFromStream_Plain(stream)) { if (l->loadFromStream_Plain(stream)) {
deckList = l; deckList = l;
accept(); accept();
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
#include <QDialog> #include <QDialog>
class DeckList; class DeckLoader;
class QPlainTextEdit; class QPlainTextEdit;
class QPushButton; class QPushButton;
...@@ -13,10 +13,10 @@ private slots: ...@@ -13,10 +13,10 @@ private slots:
void actOK(); void actOK();
void actRefresh(); void actRefresh();
private: private:
DeckList *deckList; DeckLoader *deckList;
public: public:
DlgLoadDeckFromClipboard(QWidget *parent = 0); DlgLoadDeckFromClipboard(QWidget *parent = 0);
DeckList *getDeckList() const { return deckList; } DeckLoader *getDeckList() const { return deckList; }
private: private:
QPlainTextEdit *contentsEdit; QPlainTextEdit *contentsEdit;
QPushButton *refreshButton; QPushButton *refreshButton;
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
#include "dlg_create_token.h" #include "dlg_create_token.h"
#include "carddatabase.h" #include "carddatabase.h"
#include "color.h" #include "color.h"
#include "decklist.h" #include "deck_loader.h"
#include "main.h" #include "main.h"
#include <QSettings> #include <QSettings>
#include <QPainter> #include <QPainter>
...@@ -747,9 +747,9 @@ void Player::initSayMenu() ...@@ -747,9 +747,9 @@ void Player::initSayMenu()
} }
} }
void Player::setDeck(DeckList *_deck) void Player::setDeck(const DeckLoader &_deck)
{ {
deck = _deck; deck = new DeckLoader(_deck);
aOpenDeckInDeckEditor->setEnabled(deck); aOpenDeckInDeckEditor->setEnabled(deck);
createPredefinedTokenMenu->clear(); createPredefinedTokenMenu->clear();
...@@ -792,7 +792,7 @@ void Player::actAlwaysRevealTopCard() ...@@ -792,7 +792,7 @@ void Player::actAlwaysRevealTopCard()
void Player::actOpenDeckInDeckEditor() void Player::actOpenDeckInDeckEditor()
{ {
emit openDeckEditor(new DeckList(deck)); emit openDeckEditor(*deck);
} }
void Player::actViewGraveyard() void Player::actViewGraveyard()
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
namespace google { namespace protobuf { class Message; } } namespace google { namespace protobuf { class Message; } }
class CardDatabase; class CardDatabase;
class DeckList; class DeckLoader;
class QMenu; class QMenu;
class QAction; class QAction;
class ZoneViewZone; class ZoneViewZone;
...@@ -77,7 +77,7 @@ public: ...@@ -77,7 +77,7 @@ public:
class Player : public QObject, public QGraphicsItem { class Player : public QObject, public QGraphicsItem {
Q_OBJECT Q_OBJECT
signals: signals:
void openDeckEditor(DeckList *deck); void openDeckEditor(const DeckLoader &deck);
void newCardAdded(AbstractCardItem *card); void newCardAdded(AbstractCardItem *card);
// Log events // Log events
void logSay(Player *player, QString message); void logSay(Player *player, QString message);
...@@ -191,7 +191,7 @@ private: ...@@ -191,7 +191,7 @@ private:
bool clearCardsToDelete(); bool clearCardsToDelete();
QList<CardItem *> cardsToDelete; QList<CardItem *> cardsToDelete;
DeckList *deck; DeckLoader *deck;
QStringList predefinedTokens; QStringList predefinedTokens;
PlayerArea *playerArea; PlayerArea *playerArea;
...@@ -263,7 +263,7 @@ public: ...@@ -263,7 +263,7 @@ public:
void retranslateUi(); void retranslateUi();
void clear(); void clear();
TabGame *getGame() const { return game; } TabGame *getGame() const { return game; }
void setDeck(DeckList *_deck); void setDeck(const DeckLoader &_deck);
QMenu *getPlayerMenu() const { return playerMenu; } QMenu *getPlayerMenu() const { return playerMenu; }
int getId() const { return id; } int getId() const { return id; }
QString getName() const; QString getName() const;
......
...@@ -27,6 +27,11 @@ ...@@ -27,6 +27,11 @@
#include "main.h" #include "main.h"
#include "settingscache.h" #include "settingscache.h"
#include "priceupdater.h" #include "priceupdater.h"
#include "tab_supervisor.h"
#include "abstractclient.h"
#include "pending_command.h"
#include "pb/response.pb.h"
#include "pb/command_deck_upload.pb.h"
void SearchLineEdit::keyPressEvent(QKeyEvent *event) void SearchLineEdit::keyPressEvent(QKeyEvent *event)
{ {
...@@ -351,7 +356,7 @@ void TabDeckEditor::actNewDeck() ...@@ -351,7 +356,7 @@ void TabDeckEditor::actNewDeck()
deckModel->cleanList(); deckModel->cleanList();
nameEdit->setText(QString()); nameEdit->setText(QString());
commentsEdit->setText(QString()); commentsEdit->setText(QString());
lastFileName = QString(); hashLabel->setText(QString());
setWindowModified(false); setWindowModified(false);
} }
...@@ -362,24 +367,44 @@ void TabDeckEditor::actLoadDeck() ...@@ -362,24 +367,44 @@ void TabDeckEditor::actLoadDeck()
QFileDialog dialog(this, tr("Load deck")); QFileDialog dialog(this, tr("Load deck"));
dialog.setDirectory(settingsCache->getDeckPath()); dialog.setDirectory(settingsCache->getDeckPath());
dialog.setNameFilters(DeckList::fileNameFilters); dialog.setNameFilters(DeckLoader::fileNameFilters);
if (!dialog.exec()) if (!dialog.exec())
return; return;
QString fileName = dialog.selectedFiles().at(0); QString fileName = dialog.selectedFiles().at(0);
DeckList::FileFormat fmt = DeckList::getFormatFromNameFilter(dialog.selectedNameFilter()); DeckLoader::FileFormat fmt = DeckLoader::getFormatFromNameFilter(dialog.selectedNameFilter());
DeckList *l = new DeckList;
DeckLoader *l = new DeckLoader;
if (l->loadFromFile(fileName, fmt)) if (l->loadFromFile(fileName, fmt))
setDeck(l, fileName, fmt); setDeck(l);
else else
delete l; delete l;
} }
void TabDeckEditor::saveDeckRemoteFinished(const Response &response)
{
if (response.response_code() != Response::RespOk)
QMessageBox::critical(this, tr("Error"), tr("The deck could not be saved."));
else
setWindowModified(false);
}
bool TabDeckEditor::actSaveDeck() bool TabDeckEditor::actSaveDeck()
{ {
if (lastFileName.isEmpty()) DeckLoader *const deck = deckModel->getDeckList();
if (deck->getLastRemoteDeckId() != -1) {
Command_DeckUpload cmd;
cmd.set_deck_id(deck->getLastRemoteDeckId());
cmd.set_deck_list(deck->writeToString_Native().toStdString());
PendingCommand *pend = AbstractClient::prepareSessionCommand(cmd);
connect(pend, SIGNAL(finished(Response, CommandContainer, QVariant)), this, SLOT(saveDeckRemoteFinished(Response)));
tabSupervisor->getClient()->sendCommand(pend);
return true;
} else if (deck->getLastFileName().isEmpty())
return actSaveDeckAs(); return actSaveDeckAs();
else if (deckModel->getDeckList()->saveToFile(lastFileName, lastFileFormat)) { else if (deck->saveToFile(deck->getLastFileName(), deck->getLastFileFormat())) {
setWindowModified(false); setWindowModified(false);
return true; return true;
} }
...@@ -394,22 +419,20 @@ bool TabDeckEditor::actSaveDeckAs() ...@@ -394,22 +419,20 @@ bool TabDeckEditor::actSaveDeckAs()
dialog.setAcceptMode(QFileDialog::AcceptSave); dialog.setAcceptMode(QFileDialog::AcceptSave);
dialog.setConfirmOverwrite(true); dialog.setConfirmOverwrite(true);
dialog.setDefaultSuffix("cod"); dialog.setDefaultSuffix("cod");
dialog.setNameFilters(DeckList::fileNameFilters); dialog.setNameFilters(DeckLoader::fileNameFilters);
dialog.selectFile(deckModel->getDeckList()->getName()); dialog.selectFile(deckModel->getDeckList()->getName());
if (!dialog.exec()) if (!dialog.exec())
return false; return false;
QString fileName = dialog.selectedFiles().at(0); QString fileName = dialog.selectedFiles().at(0);
DeckList::FileFormat fmt = DeckList::getFormatFromNameFilter(dialog.selectedNameFilter()); DeckLoader::FileFormat fmt = DeckLoader::getFormatFromNameFilter(dialog.selectedNameFilter());
if (deckModel->getDeckList()->saveToFile(fileName, fmt)) { if (!deckModel->getDeckList()->saveToFile(fileName, fmt)) {
lastFileName = fileName;
lastFileFormat = fmt;
setWindowModified(false);
return true;
}
QMessageBox::critical(this, tr("Error"), tr("The deck could not be saved.\nPlease check that the directory is writable and try again.")); QMessageBox::critical(this, tr("Error"), tr("The deck could not be saved.\nPlease check that the directory is writable and try again."));
return false; return false;
}
setWindowModified(false);
return true;
} }
void TabDeckEditor::actLoadDeckFromClipboard() void TabDeckEditor::actLoadDeckFromClipboard()
...@@ -550,20 +573,18 @@ void TabDeckEditor::finishedUpdatingPrices() ...@@ -550,20 +573,18 @@ void TabDeckEditor::finishedUpdatingPrices()
aUpdatePrices->setDisabled(false); aUpdatePrices->setDisabled(false);
} }
void TabDeckEditor::setDeck(DeckList *_deck, const QString &_lastFileName, DeckList::FileFormat _lastFileFormat) void TabDeckEditor::setDeck(DeckLoader *_deck)
{ {
deckModel->setDeckList(_deck); deckModel->setDeckList(_deck);
lastFileName = _lastFileName; nameEdit->setText(deckModel->getDeckList()->getName());
lastFileFormat = _lastFileFormat; commentsEdit->setText(deckModel->getDeckList()->getComments());
nameEdit->setText(_deck->getName());
commentsEdit->setText(_deck->getComments());
updateHash(); updateHash();
deckModel->sort(1); deckModel->sort(1);
deckView->expandAll(); deckView->expandAll();
setWindowModified(false); setWindowModified(false);
db->cacheCardPixmaps(_deck->getCardList()); db->cacheCardPixmaps(deckModel->getDeckList()->getCardList());
deckView->expandAll(); deckView->expandAll();
setWindowModified(false); setWindowModified(false);
} }
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
#include "tab.h" #include "tab.h"
#include <QAbstractItemModel> #include <QAbstractItemModel>
#include <QLineEdit> #include <QLineEdit>
#include "decklist.h"
class CardDatabaseModel; class CardDatabaseModel;
class CardDatabaseDisplayModel; class CardDatabaseDisplayModel;
...@@ -15,6 +14,8 @@ class CardInfoWidget; ...@@ -15,6 +14,8 @@ class CardInfoWidget;
class QTextEdit; class QTextEdit;
class DlgCardSearch; class DlgCardSearch;
class QLabel; class QLabel;
class DeckLoader;
class Response;
class SearchLineEdit : public QLineEdit { class SearchLineEdit : public QLineEdit {
private: private:
...@@ -57,14 +58,12 @@ private slots: ...@@ -57,14 +58,12 @@ private slots:
void actUpdatePrices(); void actUpdatePrices();
void finishedUpdatingPrices(); void finishedUpdatingPrices();
void saveDeckRemoteFinished(const Response &r);
private: private:
void addCardHelper(QString zoneName); void addCardHelper(QString zoneName);
void recursiveExpand(const QModelIndex &index); void recursiveExpand(const QModelIndex &index);
bool confirmClose(); bool confirmClose();
QString lastFileName;
DeckList::FileFormat lastFileFormat;
CardDatabaseModel *databaseModel; CardDatabaseModel *databaseModel;
CardDatabaseDisplayModel *databaseDisplayModel; CardDatabaseDisplayModel *databaseDisplayModel;
DeckListModel *deckModel; DeckListModel *deckModel;
...@@ -90,7 +89,7 @@ public: ...@@ -90,7 +89,7 @@ public:
~TabDeckEditor(); ~TabDeckEditor();
void retranslateUi(); void retranslateUi();
QString getTabText() const; QString getTabText() const;
void setDeck(DeckList *_deck, const QString &_lastFileName = QString(), DeckList::FileFormat _lastFileFormat = DeckList::CockatriceFormat); void setDeck(DeckLoader *_deckLoader);
void setWindowModified(bool _windowModified); void setWindowModified(bool _windowModified);
public slots: public slots:
void closeRequest(); void closeRequest();
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "abstractclient.h" #include "abstractclient.h"
#include "decklist.h" #include "decklist.h"
#include "settingscache.h" #include "settingscache.h"
#include "deck_loader.h"
#include "pending_command.h" #include "pending_command.h"
#include "pb/response.pb.h" #include "pb/response.pb.h"
...@@ -128,13 +129,12 @@ void TabDeckStorage::actOpenLocalDeck() ...@@ -128,13 +129,12 @@ void TabDeckStorage::actOpenLocalDeck()
if (localDirModel->isDir(curLeft)) if (localDirModel->isDir(curLeft))
return; return;
QString filePath = localDirModel->filePath(curLeft); QString filePath = localDirModel->filePath(curLeft);
DeckList *deck = new DeckList;
if (!deck->loadFromFile(filePath, DeckList::CockatriceFormat)) DeckLoader deckLoader;
if (!deckLoader.loadFromFile(filePath, DeckLoader::CockatriceFormat))
return; return;
// WndDeckEditor *deckEditor = new WndDeckEditor; emit openDeckEditor(&deckLoader);
// deckEditor->setDeck(deck, filePath, DeckList::CockatriceFormat);
// deckEditor->show();
} }
void TabDeckStorage::actUpload() void TabDeckStorage::actUpload()
...@@ -145,8 +145,8 @@ void TabDeckStorage::actUpload() ...@@ -145,8 +145,8 @@ void TabDeckStorage::actUpload()
QString filePath = localDirModel->filePath(curLeft); QString filePath = localDirModel->filePath(curLeft);
QFile deckFile(filePath); QFile deckFile(filePath);
QFileInfo deckFileInfo(deckFile); QFileInfo deckFileInfo(deckFile);
DeckList deck; DeckLoader deck;
if (!deck.loadFromFile(filePath, DeckList::CockatriceFormat)) if (!deck.loadFromFile(filePath, DeckLoader::CockatriceFormat))
return; return;
if (deck.getName().isEmpty()) { if (deck.getName().isEmpty()) {
bool ok; bool ok;
...@@ -205,20 +205,23 @@ void TabDeckStorage::actOpenRemoteDeck() ...@@ -205,20 +205,23 @@ void TabDeckStorage::actOpenRemoteDeck()
cmd.set_deck_id(curRight->getId()); cmd.set_deck_id(curRight->getId());
PendingCommand *pend = client->prepareSessionCommand(cmd); PendingCommand *pend = client->prepareSessionCommand(cmd);
connect(pend, SIGNAL(finished(Response, CommandContainer, QVariant)), this, SLOT(openRemoteDeckFinished(const Response &))); connect(pend, SIGNAL(finished(Response, CommandContainer, QVariant)), this, SLOT(openRemoteDeckFinished(Response, CommandContainer)));
client->sendCommand(pend); client->sendCommand(pend);
} }
void TabDeckStorage::openRemoteDeckFinished(const Response &r) void TabDeckStorage::openRemoteDeckFinished(const Response &r, const CommandContainer &commandContainer)
{ {
if (r.response_code() != Response::RespOk) if (r.response_code() != Response::RespOk)
return; return;
const Response_DeckDownload &resp = r.GetExtension(Response_DeckDownload::ext); const Response_DeckDownload &resp = r.GetExtension(Response_DeckDownload::ext);
const Command_DeckDownload &cmd = commandContainer.session_command(0).GetExtension(Command_DeckDownload::ext);
DeckLoader loader;
if (!loader.loadFromRemote(QString::fromStdString(resp.deck()), cmd.deck_id()))
return;
// WndDeckEditor *deckEditor = new WndDeckEditor; emit openDeckEditor(&loader);
// deckEditor->setDeck(new DeckList(QString::fromStdString(resp.deck())));
// deckEditor->show();
} }
void TabDeckStorage::actDownload() void TabDeckStorage::actDownload()
...@@ -255,8 +258,8 @@ void TabDeckStorage::downloadFinished(const Response &r, const CommandContainer ...@@ -255,8 +258,8 @@ void TabDeckStorage::downloadFinished(const Response &r, const CommandContainer
const Response_DeckDownload &resp = r.GetExtension(Response_DeckDownload::ext); const Response_DeckDownload &resp = r.GetExtension(Response_DeckDownload::ext);
QString filePath = extraData.toString(); QString filePath = extraData.toString();
DeckList deck(QString::fromStdString(resp.deck())); DeckLoader deck(QString::fromStdString(resp.deck()));
deck.saveToFile(filePath, DeckList::CockatriceFormat); deck.saveToFile(filePath, DeckLoader::CockatriceFormat);
} }
void TabDeckStorage::actNewFolder() void TabDeckStorage::actNewFolder()
......
...@@ -13,6 +13,7 @@ class QGroupBox; ...@@ -13,6 +13,7 @@ class QGroupBox;
class RemoteDeckList_TreeWidget; class RemoteDeckList_TreeWidget;
class CommandContainer; class CommandContainer;
class Response; class Response;
class DeckLoader;
class TabDeckStorage : public Tab { class TabDeckStorage : public Tab {
Q_OBJECT Q_OBJECT
...@@ -34,7 +35,7 @@ private slots: ...@@ -34,7 +35,7 @@ private slots:
void actDeleteLocalDeck(); void actDeleteLocalDeck();
void actOpenRemoteDeck(); void actOpenRemoteDeck();
void openRemoteDeckFinished(const Response &r); void openRemoteDeckFinished(const Response &r, const CommandContainer &commandContainer);
void actDownload(); void actDownload();
void downloadFinished(const Response &r, const CommandContainer &commandContainer, const QVariant &extraData); void downloadFinished(const Response &r, const CommandContainer &commandContainer, const QVariant &extraData);
...@@ -49,6 +50,8 @@ public: ...@@ -49,6 +50,8 @@ public:
TabDeckStorage(TabSupervisor *_tabSupervisor, AbstractClient *_client); TabDeckStorage(TabSupervisor *_tabSupervisor, AbstractClient *_client);
void retranslateUi(); void retranslateUi();
QString getTabText() const { return tr("Deck storage"); } QString getTabText() const { return tr("Deck storage"); }
signals:
void openDeckEditor(const DeckLoader *deckLoader);
}; };
#endif #endif
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
#include "zoneviewzone.h" #include "zoneviewzone.h"
#include "zoneviewwidget.h" #include "zoneviewwidget.h"
#include "deckview.h" #include "deckview.h"
#include "decklist.h" #include "deck_loader.h"
#include "dlg_load_remote_deck.h" #include "dlg_load_remote_deck.h"
#include "abstractclient.h" #include "abstractclient.h"
#include "carditem.h" #include "carditem.h"
...@@ -149,21 +149,20 @@ void DeckViewContainer::loadLocalDeck() ...@@ -149,21 +149,20 @@ void DeckViewContainer::loadLocalDeck()
{ {
QFileDialog dialog(this, tr("Load deck")); QFileDialog dialog(this, tr("Load deck"));
dialog.setDirectory(settingsCache->getDeckPath()); dialog.setDirectory(settingsCache->getDeckPath());
dialog.setNameFilters(DeckList::fileNameFilters); dialog.setNameFilters(DeckLoader::fileNameFilters);
if (!dialog.exec()) if (!dialog.exec())
return; return;
QString fileName = dialog.selectedFiles().at(0); QString fileName = dialog.selectedFiles().at(0);
DeckList::FileFormat fmt = DeckList::getFormatFromNameFilter(dialog.selectedNameFilter()); DeckLoader::FileFormat fmt = DeckLoader::getFormatFromNameFilter(dialog.selectedNameFilter());
DeckList *deck = new DeckList; DeckLoader deck;
if (!deck->loadFromFile(fileName, fmt)) { if (!deck.loadFromFile(fileName, fmt)) {
delete deck; QMessageBox::critical(this, tr("Error"), tr("The selected file could not be loaded."));
// Error message
return; return;
} }
Command_DeckSelect cmd; Command_DeckSelect cmd;
cmd.set_deck(deck->writeToString_Native().toStdString()); cmd.set_deck(deck.writeToString_Native().toStdString());
PendingCommand *pend = static_cast<TabGame *>(parent())->prepareGameCommand(cmd); PendingCommand *pend = static_cast<TabGame *>(parent())->prepareGameCommand(cmd);
connect(pend, SIGNAL(finished(Response, CommandContainer, QVariant)), this, SLOT(deckSelectFinished(const Response &))); connect(pend, SIGNAL(finished(Response, CommandContainer, QVariant)), this, SLOT(deckSelectFinished(const Response &)));
static_cast<TabGame *>(parent())->sendGameCommand(pend, playerId); static_cast<TabGame *>(parent())->sendGameCommand(pend, playerId);
...@@ -184,8 +183,8 @@ void DeckViewContainer::loadRemoteDeck() ...@@ -184,8 +183,8 @@ void DeckViewContainer::loadRemoteDeck()
void DeckViewContainer::deckSelectFinished(const Response &r) void DeckViewContainer::deckSelectFinished(const Response &r)
{ {
const Response_DeckDownload &resp = r.GetExtension(Response_DeckDownload::ext); const Response_DeckDownload &resp = r.GetExtension(Response_DeckDownload::ext);
DeckList *newDeck = new DeckList(QString::fromStdString(resp.deck())); DeckLoader newDeck(QString::fromStdString(resp.deck()));
db->cacheCardPixmaps(newDeck->getCardList()); db->cacheCardPixmaps(newDeck.getCardList());
setDeck(newDeck); setDeck(newDeck);
} }
...@@ -227,7 +226,7 @@ void DeckViewContainer::setSideboardLocked(bool locked) ...@@ -227,7 +226,7 @@ void DeckViewContainer::setSideboardLocked(bool locked)
deckView->resetSideboardPlan(); deckView->resetSideboardPlan();
} }
void DeckViewContainer::setDeck(DeckList *deck) void DeckViewContainer::setDeck(const DeckLoader &deck)
{ {
deckView->setDeck(deck); deckView->setDeck(deck);
readyStartButton->setEnabled(true); readyStartButton->setEnabled(true);
...@@ -949,8 +948,8 @@ void TabGame::eventGameStateChanged(const Event_GameStateChanged &event, int /*e ...@@ -949,8 +948,8 @@ void TabGame::eventGameStateChanged(const Event_GameStateChanged &event, int /*e
if (player->getLocal()) { if (player->getLocal()) {
DeckViewContainer *deckViewContainer = deckViewContainers.value(playerId); DeckViewContainer *deckViewContainer = deckViewContainers.value(playerId);
if (playerInfo.has_deck_list()) { if (playerInfo.has_deck_list()) {
DeckList *newDeck = new DeckList(QString::fromStdString(playerInfo.deck_list())); DeckLoader newDeck(QString::fromStdString(playerInfo.deck_list()));
db->cacheCardPixmaps(newDeck->getCardList()); db->cacheCardPixmaps(newDeck.getCardList());
deckViewContainer->setDeck(newDeck); deckViewContainer->setDeck(newDeck);
player->setDeck(newDeck); player->setDeck(newDeck);
} }
......
...@@ -48,7 +48,7 @@ class CardZone; ...@@ -48,7 +48,7 @@ class CardZone;
class AbstractCardItem; class AbstractCardItem;
class CardItem; class CardItem;
class TabGame; class TabGame;
class DeckList; class DeckLoader;
class QVBoxLayout; class QVBoxLayout;
class QHBoxLayout; class QHBoxLayout;
class GameReplay; class GameReplay;
...@@ -92,7 +92,7 @@ public: ...@@ -92,7 +92,7 @@ public:
void setButtonsVisible(bool _visible); void setButtonsVisible(bool _visible);
void setReadyStart(bool ready); void setReadyStart(bool ready);
void setSideboardLocked(bool locked); void setSideboardLocked(bool locked);
void setDeck(DeckList *deck); void setDeck(const DeckLoader &deck);
}; };
class TabGame : public Tab { class TabGame : public Tab {
...@@ -171,7 +171,7 @@ signals: ...@@ -171,7 +171,7 @@ signals:
void containerProcessingStarted(const GameEventContext &context); void containerProcessingStarted(const GameEventContext &context);
void containerProcessingDone(); void containerProcessingDone();
void openMessageDialog(const QString &userName, bool focus); void openMessageDialog(const QString &userName, bool focus);
void openDeckEditor(DeckList *deck); void openDeckEditor(const DeckLoader &deck);
private slots: private slots:
void replayNextEvent(); void replayNextEvent();
void replayFinished(); void replayFinished();
......
...@@ -155,6 +155,7 @@ void TabSupervisor::start(const ServerInfo_User &_userInfo) ...@@ -155,6 +155,7 @@ void TabSupervisor::start(const ServerInfo_User &_userInfo)
if (userInfo->user_level() & ServerInfo_User::IsRegistered) { if (userInfo->user_level() & ServerInfo_User::IsRegistered) {
tabDeckStorage = new TabDeckStorage(this, client); tabDeckStorage = new TabDeckStorage(this, client);
connect(tabDeckStorage, SIGNAL(openDeckEditor(const DeckLoader *)), this, SLOT(addDeckEditorTab(const DeckLoader *)));
myAddTab(tabDeckStorage); myAddTab(tabDeckStorage);
tabReplays = new TabReplays(this, client); tabReplays = new TabReplays(this, client);
...@@ -279,7 +280,8 @@ void TabSupervisor::gameJoined(const Event_GameJoined &event) ...@@ -279,7 +280,8 @@ void TabSupervisor::gameJoined(const Event_GameJoined &event)
TabGame *tab = new TabGame(this, QList<AbstractClient *>() << client, event, roomGameTypes); TabGame *tab = new TabGame(this, QList<AbstractClient *>() << client, event, roomGameTypes);
connect(tab, SIGNAL(gameClosing(TabGame *)), this, SLOT(gameLeft(TabGame *))); connect(tab, SIGNAL(gameClosing(TabGame *)), this, SLOT(gameLeft(TabGame *)));
connect(tab, SIGNAL(openMessageDialog(const QString &, bool)), this, SLOT(addMessageTab(const QString &, bool))); connect(tab, SIGNAL(openMessageDialog(const QString &, bool)), this, SLOT(addMessageTab(const QString &, bool)));
connect(tab, SIGNAL(openDeckEditor(DeckList *)), this, SLOT(addDeckEditorTab(DeckList *))); connect(tab, SIGNAL(openDeckEditor(DeckList *, QString, DeckList::FileFormat)), this, SLOT(addDeckEditorTab(DeckList *, QString, DeckList::FileFormat)));
connect(tab, SIGNAL(openDeckEditor(DeckList *, int)), this, SLOT(addDeckEditorTab(DeckList *, int)));
int tabIndex = myAddTab(tab); int tabIndex = myAddTab(tab);
addCloseButtonToTab(tab, tabIndex); addCloseButtonToTab(tab, tabIndex);
gameTabs.insert(event.game_info().game_id(), tab); gameTabs.insert(event.game_info().game_id(), tab);
...@@ -384,11 +386,11 @@ void TabSupervisor::talkLeft(TabMessage *tab) ...@@ -384,11 +386,11 @@ void TabSupervisor::talkLeft(TabMessage *tab)
removeTab(indexOf(tab)); removeTab(indexOf(tab));
} }
TabDeckEditor *TabSupervisor::addDeckEditorTab(DeckList *deckToOpen) TabDeckEditor *TabSupervisor::addDeckEditorTab(const DeckLoader *deckToOpen)
{ {
TabDeckEditor *tab = new TabDeckEditor(this); TabDeckEditor *tab = new TabDeckEditor(this);
if (deckToOpen) if (deckToOpen)
tab->setDeck(deckToOpen); tab->setDeck(new DeckLoader(*deckToOpen));
connect(tab, SIGNAL(deckEditorClosing(TabDeckEditor *)), this, SLOT(deckEditorClosed(TabDeckEditor *))); connect(tab, SIGNAL(deckEditorClosing(TabDeckEditor *)), this, SLOT(deckEditorClosed(TabDeckEditor *)));
int tabIndex = myAddTab(tab); int tabIndex = myAddTab(tab);
addCloseButtonToTab(tab, tabIndex); addCloseButtonToTab(tab, tabIndex);
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include <QTabWidget> #include <QTabWidget>
#include <QMap> #include <QMap>
#include <QAbstractButton> #include <QAbstractButton>
#include "deck_loader.h"
class QMenu; class QMenu;
class AbstractClient; class AbstractClient;
...@@ -75,7 +76,7 @@ signals: ...@@ -75,7 +76,7 @@ signals:
void localGameEnded(); void localGameEnded();
void adminLockChanged(bool lock); void adminLockChanged(bool lock);
public slots: public slots:
TabDeckEditor *addDeckEditorTab(DeckList *deckToOpen); TabDeckEditor *addDeckEditorTab(const DeckLoader *deckToOpen);
void openReplay(GameReplay *replay); void openReplay(GameReplay *replay);
private slots: private slots:
void closeButtonPressed(); void closeButtonPressed();
......
...@@ -264,21 +264,19 @@ QVector<QPair<int, int> > InnerDecklistNode::sort(Qt::SortOrder order) ...@@ -264,21 +264,19 @@ QVector<QPair<int, int> > InnerDecklistNode::sort(Qt::SortOrder order)
return result; 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() DeckList::DeckList()
{ {
root = new InnerDecklistNode; 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()) { while (spIterator.hasNext()) {
spIterator.next(); spIterator.next();
sideboardPlans.insert(spIterator.key(), new SideboardPlan(spIterator.key(), spIterator.value()->getMoveList())); sideboardPlans.insert(spIterator.key(), new SideboardPlan(spIterator.key(), spIterator.value()->getMoveList()));
...@@ -289,9 +287,7 @@ DeckList::DeckList(DeckList *other) ...@@ -289,9 +287,7 @@ DeckList::DeckList(DeckList *other)
DeckList::DeckList(const QString &nativeString) DeckList::DeckList(const QString &nativeString)
{ {
root = new InnerDecklistNode; root = new InnerDecklistNode;
loadFromString_Native(nativeString);
QXmlStreamReader xml(nativeString);
loadFromXml(&xml);
} }
DeckList::~DeckList() DeckList::~DeckList()
...@@ -362,13 +358,14 @@ void DeckList::write(QXmlStreamWriter *xml) ...@@ -362,13 +358,14 @@ void DeckList::write(QXmlStreamWriter *xml)
xml->writeEndElement(); xml->writeEndElement();
} }
void DeckList::loadFromXml(QXmlStreamReader *xml) bool DeckList::loadFromXml(QXmlStreamReader *xml)
{ {
cleanList();
while (!xml->atEnd()) { while (!xml->atEnd()) {
xml->readNext(); xml->readNext();
if (xml->isStartElement()) { if (xml->isStartElement()) {
if (xml->name() != "cockatrice_deck") if (xml->name() != "cockatrice_deck")
return; return false;
while (!xml->atEnd()) { while (!xml->atEnd()) {
xml->readNext(); xml->readNext();
if (!readElement(xml)) if (!readElement(xml))
...@@ -377,6 +374,13 @@ void DeckList::loadFromXml(QXmlStreamReader *xml) ...@@ -377,6 +374,13 @@ void DeckList::loadFromXml(QXmlStreamReader *xml)
} }
} }
updateDeckHash(); updateDeckHash();
return true;
}
bool DeckList::loadFromString_Native(const QString &nativeString)
{
QXmlStreamReader xml(nativeString);
return loadFromXml(&xml);
} }
QString DeckList::writeToString_Native() QString DeckList::writeToString_Native()
...@@ -410,6 +414,8 @@ bool DeckList::saveToFile_Native(QIODevice *device) ...@@ -410,6 +414,8 @@ bool DeckList::saveToFile_Native(QIODevice *device)
bool DeckList::loadFromStream_Plain(QTextStream &in) bool DeckList::loadFromStream_Plain(QTextStream &in)
{ {
cleanList();
InnerDecklistNode *main = 0, *side = 0; InnerDecklistNode *main = 0, *side = 0;
int okRows = 0; int okRows = 0;
...@@ -474,52 +480,13 @@ bool DeckList::saveToFile_Plain(QIODevice *device) ...@@ -474,52 +480,13 @@ bool DeckList::saveToFile_Plain(QIODevice *device)
return saveToStream_Plain(out); 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() void DeckList::cleanList()
{ {
root->clearTree(); root->clearTree();
setName(); setName();
setComments(); setComments();
updateDeckHash(); deckHash = QString();
emit deckHashChanged();
} }
void DeckList::getCardListHelper(InnerDecklistNode *item, QSet<QString> &result) const void DeckList::getCardListHelper(InnerDecklistNode *item, QSet<QString> &result) const
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment