diff --git a/cockatrice/src/decklist.cpp b/cockatrice/src/decklist.cpp index 21a31bf1..ab594003 100644 --- a/cockatrice/src/decklist.cpp +++ b/cockatrice/src/decklist.cpp @@ -3,9 +3,7 @@ #include #include #include -#include #include -#include #include "decklist.h" #include "carddatabase.h" @@ -130,8 +128,8 @@ QVector > InnerDecklistNode::sort(Qt::SortOrder order) return result; } -DeckList::DeckList(CardDatabase *_db, QObject *parent) - : QObject(parent), db(_db) +DeckList::DeckList(QObject *parent) + : QObject(parent) { root = new InnerDecklistNode; } @@ -273,7 +271,7 @@ bool DeckList::saveToFile_Plain(QIODevice *device) return true; } -bool DeckList::loadFromFile(const QString &fileName, FileFormat fmt, QWidget *parent) +bool DeckList::loadFromFile(const QString &fileName, FileFormat fmt) { QFile file(fileName); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) @@ -285,18 +283,11 @@ bool DeckList::loadFromFile(const QString &fileName, FileFormat fmt, QWidget *pa case PlainTextFormat: result = loadFromFile_Plain(&file); break; case CockatriceFormat: result = loadFromFile_Native(&file); break; } - if (result) { + if (result) emit deckLoaded(); - cacheCardPictures(parent); - } return result; } -const QStringList DeckList::fileNameFilters = QStringList() - << QObject::tr("Cockatrice decks (*.cod)") - << QObject::tr("Plain text decks (*.dec *.mwDeck)") - << QObject::tr("All files (*.*)"); - bool DeckList::saveToFile(const QString &fileName, FileFormat fmt) { QFile file(fileName); @@ -311,82 +302,6 @@ bool DeckList::saveToFile(const QString &fileName, FileFormat fmt) return result; } -bool DeckList::loadDialog(QWidget *parent) -{ - QFileDialog dialog(parent, tr("Load deck")); - QSettings settings; - dialog.setDirectory(settings.value("paths/decks").toString()); - dialog.setNameFilters(fileNameFilters); - if (!dialog.exec()) - return false; - - QString fileName = dialog.selectedFiles().at(0); - FileFormat fmt; - switch (fileNameFilters.indexOf(dialog.selectedNameFilter())) { - case 0: fmt = CockatriceFormat; break; - case 1: fmt = PlainTextFormat; break; - default: fmt = PlainTextFormat; break; - } - - if (loadFromFile(fileName, fmt, parent)) { - lastFileName = fileName; - lastFileFormat = fmt; - return true; - } - return false; -} - -bool DeckList::saveDialog(QWidget *parent) -{ - QFileDialog dialog(parent, tr("Save deck")); - QSettings settings; - dialog.setDirectory(settings.value("paths/decks").toString()); - dialog.setAcceptMode(QFileDialog::AcceptSave); - dialog.setConfirmOverwrite(true); - dialog.setDefaultSuffix("cod"); - dialog.setNameFilters(fileNameFilters); - if (!dialog.exec()) - return false; - - QString fileName = dialog.selectedFiles().at(0); - DeckList::FileFormat fmt; - switch (fileNameFilters.indexOf(dialog.selectedNameFilter())) { - case 0: fmt = DeckList::CockatriceFormat; break; - case 1: fmt = DeckList::PlainTextFormat; break; - default: fmt = DeckList::PlainTextFormat; break; - } - - if (saveToFile(fileName, fmt)) { - lastFileName = fileName; - lastFileFormat = fmt; - return true; - } - return false; -} - -void DeckList::cacheCardPicturesHelper(InnerDecklistNode *item, QProgressDialog *progress) -{ - for (int i = 0; i < item->size(); i++) { - DecklistCardNode *node = dynamic_cast(item->at(i)); - if (node) { - db->getCard(node->getName())->loadPixmap(); - progress->setValue(progress->value() + 1); - } else - cacheCardPicturesHelper(dynamic_cast(item->at(i)), progress); - } -} - -void DeckList::cacheCardPictures(QWidget *parent) -{ - int totalCards = root->recursiveCount(); - - QProgressDialog progress(tr("Caching card pictures..."), QString(), 0, totalCards, parent); - progress.setMinimumDuration(1000); - progress.setWindowModality(Qt::WindowModal); - - cacheCardPicturesHelper(root, &progress); -} - void DeckList::cleanList() { root->clearTree(); diff --git a/cockatrice/src/decklist.h b/cockatrice/src/decklist.h index c4f5c388..e719e324 100644 --- a/cockatrice/src/decklist.h +++ b/cockatrice/src/decklist.h @@ -8,7 +8,6 @@ class CardDatabase; class QIODevice; -class QProgressDialog; class InnerDecklistNode; @@ -71,21 +70,17 @@ class DeckList : public QObject { public: enum FileFormat { PlainTextFormat, CockatriceFormat }; private: - static const QStringList fileNameFilters; - void cacheCardPictures(QWidget *parent = 0); - CardDatabase *db; QString name, comments; QString lastFileName; FileFormat lastFileFormat; InnerDecklistNode *root; - void cacheCardPicturesHelper(InnerDecklistNode *item, QProgressDialog *progress); signals: void deckLoaded(); public slots: void setName(const QString &_name = QString()) { name = _name; } void setComments(const QString &_comments = QString()) { comments = _comments; } public: - DeckList(CardDatabase *_db, QObject *parent = 0); + DeckList(QObject *parent = 0); ~DeckList(); QString getName() const { return name; } QString getComments() const { return comments; } @@ -96,7 +91,7 @@ public: bool saveToFile_Native(QIODevice *device); bool loadFromFile_Plain(QIODevice *device); bool saveToFile_Plain(QIODevice *device); - bool loadFromFile(const QString &fileName, FileFormat fmt, QWidget *parent = 0); + bool loadFromFile(const QString &fileName, FileFormat fmt); bool saveToFile(const QString &fileName, FileFormat fmt); bool loadDialog(QWidget *parent = 0); bool saveDialog(QWidget *parent = 0); diff --git a/cockatrice/src/decklistmodel.cpp b/cockatrice/src/decklistmodel.cpp index 9709f469..b98d9a1b 100644 --- a/cockatrice/src/decklistmodel.cpp +++ b/cockatrice/src/decklistmodel.cpp @@ -6,13 +6,15 @@ #include #include #include +#include +#include "main.h" #include "decklistmodel.h" #include "carddatabase.h" -DeckListModel::DeckListModel(CardDatabase *_db, QObject *parent) - : QAbstractItemModel(parent), db(_db) +DeckListModel::DeckListModel(QObject *parent) + : QAbstractItemModel(parent) { - deckList = new DeckList(db, this); + deckList = new DeckList(this); connect(deckList, SIGNAL(deckLoaded()), this, SLOT(rebuildTree())); root = new InnerDecklistNode; } @@ -313,6 +315,29 @@ void DeckListModel::cleanList() reset(); } +void DeckListModel::cacheCardPicturesHelper(InnerDecklistNode *item, QProgressDialog *progress) +{ + for (int i = 0; i < item->size(); i++) { + DecklistCardNode *node = dynamic_cast(item->at(i)); + if (node) { + db->getCard(node->getName())->loadPixmap(); + progress->setValue(progress->value() + 1); + } else + cacheCardPicturesHelper(dynamic_cast(item->at(i)), progress); + } +} + +void DeckListModel::cacheCardPictures(QWidget *parent) +{ + int totalCards = deckList->getRoot()->recursiveCount(); + + QProgressDialog progress(tr("Caching card pictures..."), QString(), 0, totalCards, parent); + progress.setMinimumDuration(1000); + progress.setWindowModality(Qt::WindowModal); + + cacheCardPicturesHelper(deckList->getRoot(), &progress); +} + void DeckListModel::printDeckListNode(QTextCursor *cursor, InnerDecklistNode *node) { static const int totalColumns = 3; diff --git a/cockatrice/src/decklistmodel.h b/cockatrice/src/decklistmodel.h index ba4e6c4f..dbad1ed6 100644 --- a/cockatrice/src/decklistmodel.h +++ b/cockatrice/src/decklistmodel.h @@ -6,6 +6,7 @@ #include "decklist.h" class CardDatabase; +class QProgressDialog; class QPrinter; class QTextCursor; @@ -28,7 +29,7 @@ private slots: public slots: void printDeckList(QPrinter *printer); public: - DeckListModel(CardDatabase *_db, QObject *parent = 0); + DeckListModel(QObject *parent = 0); ~DeckListModel(); int rowCount(const QModelIndex &parent = QModelIndex()) const; int columnCount(const QModelIndex &/*parent*/ = QModelIndex()) const { return 2; } @@ -43,8 +44,8 @@ public: void sort(int column, Qt::SortOrder order = Qt::AscendingOrder); void cleanList(); DeckList *getDeckList() const { return deckList; } + void cacheCardPictures(QWidget *parent = 0); private: - CardDatabase *db; DeckList *deckList; InnerDecklistNode *root; InnerDecklistNode *createNodeIfNeeded(const QString &name, InnerDecklistNode *parent); @@ -53,7 +54,8 @@ private: void sortHelper(InnerDecklistNode *node, Qt::SortOrder order); void debugIndexInfo(const QString &func, const QModelIndex &index) const; void debugShowTree(InnerDecklistNode *node, int depth) const; - + + void cacheCardPicturesHelper(InnerDecklistNode *item, QProgressDialog *progress); void printDeckListNode(QTextCursor *cursor, InnerDecklistNode *node); template T getNode(const QModelIndex &index) const diff --git a/cockatrice/src/dlg_startgame.cpp b/cockatrice/src/dlg_startgame.cpp index 02b96ed8..8a08fbaf 100644 --- a/cockatrice/src/dlg_startgame.cpp +++ b/cockatrice/src/dlg_startgame.cpp @@ -3,12 +3,13 @@ #include "dlg_startgame.h" #include "decklistmodel.h" #include "carddatabase.h" +#include "main.h" -DlgStartGame::DlgStartGame(CardDatabase *_db, QWidget *parent) - : QDialog(parent), db(_db) +DlgStartGame::DlgStartGame(QWidget *parent) + : QDialog(parent) { deckView = new QTreeView; - deckModel = new DeckListModel(db, this); + deckModel = new DeckListModel(this); deckView->setModel(deckModel); deckView->setUniformRowHeights(true); @@ -37,8 +38,8 @@ DlgStartGame::DlgStartGame(CardDatabase *_db, QWidget *parent) void DlgStartGame::actLoad() { - if (!deckModel->getDeckList()->loadDialog(this)) - return; +// if (!deckModel->getDeckList()->loadDialog(this)) +// return; deckView->reset(); deckModel->sort(1); diff --git a/cockatrice/src/dlg_startgame.h b/cockatrice/src/dlg_startgame.h index efd24950..063ad67e 100644 --- a/cockatrice/src/dlg_startgame.h +++ b/cockatrice/src/dlg_startgame.h @@ -11,14 +11,13 @@ class DeckListModel; class DlgStartGame: public QDialog { Q_OBJECT public: - DlgStartGame(CardDatabase *_db, QWidget *parent = 0); + DlgStartGame(QWidget *parent = 0); QStringList getDeckList() const; signals: void newDeckLoaded(const QStringList &cards); private slots: void actLoad(); private: - CardDatabase *db; QTreeView *deckView; DeckListModel *deckModel; diff --git a/cockatrice/src/game.cpp b/cockatrice/src/game.cpp index b3b6245d..1062a1c4 100644 --- a/cockatrice/src/game.cpp +++ b/cockatrice/src/game.cpp @@ -15,8 +15,8 @@ #include "arrowitem.h" #include "protocol_datastructures.h" -Game::Game(CardDatabase *_db, Client *_client, GameScene *_scene, QMenuBar *menuBar, QObject *parent) - : QObject(parent), db(_db), client(_client), scene(_scene), started(false), currentPhase(-1) +Game::Game(Client *_client, GameScene *_scene, QMenuBar *menuBar, QObject *parent) + : QObject(parent), client(_client), scene(_scene), started(false), currentPhase(-1) { connect(client, SIGNAL(gameEvent(const ServerEventData &)), this, SLOT(gameEvent(const ServerEventData &))); @@ -80,7 +80,7 @@ Game::Game(CardDatabase *_db, Client *_client, GameScene *_scene, QMenuBar *menu connect(i.key(), SIGNAL(triggered()), this, SLOT(cardMenuAction())); } - dlgStartGame = new DlgStartGame(db); + dlgStartGame = new DlgStartGame; connect(dlgStartGame, SIGNAL(newDeckLoaded(const QStringList &)), client, SLOT(submitDeck(const QStringList &))); connect(dlgStartGame, SIGNAL(finished(int)), this, SLOT(readyStart())); diff --git a/cockatrice/src/game.h b/cockatrice/src/game.h index 011f578b..874d5807 100644 --- a/cockatrice/src/game.h +++ b/cockatrice/src/game.h @@ -30,7 +30,6 @@ private: *aNextPhase, *aNextTurn, *aRemoveLocalArrows; DlgStartGame *dlgStartGame; - CardDatabase *db; Client *client; GameScene *scene; QStringList spectatorList; @@ -102,7 +101,7 @@ signals: void logSetActivePlayer(Player *player); void setActivePhase(int phase); public: - Game(CardDatabase *_db, Client *_client, GameScene *_scene, QMenuBar *menuBar, QObject *parent = 0); + Game(Client *_client, GameScene *_scene, QMenuBar *menuBar, QObject *parent = 0); ~Game(); void retranslateUi(); void restartGameDialog(); diff --git a/cockatrice/src/tab_game.cpp b/cockatrice/src/tab_game.cpp index 60eff8bb..d33795d7 100644 --- a/cockatrice/src/tab_game.cpp +++ b/cockatrice/src/tab_game.cpp @@ -50,8 +50,19 @@ TabGame::TabGame(Client *_client, int _gameId) connect(phasesToolbar, SIGNAL(signalSetPhase(int)), client, SLOT(setActivePhase(int))); connect(phasesToolbar, SIGNAL(signalNextTurn()), client, SLOT(nextTurn())); -} +/* game = new Game(client, scene, menuBar(), this); + connect(game, SIGNAL(hoverCard(QString)), cardInfo, SLOT(setCard(const QString &))); + connect(game, SIGNAL(playerAdded(Player *)), this, SLOT(playerAdded(Player *))); + connect(game, SIGNAL(playerRemoved(Player *)), scene, SLOT(removePlayer(Player *))); + connect(game, SIGNAL(setActivePhase(int)), phasesToolbar, SLOT(setActivePhase(int))); + connect(phasesToolbar, SIGNAL(signalDrawCard()), game, SLOT(activePlayerDrawCard())); + connect(phasesToolbar, SIGNAL(signalUntapAll()), game, SLOT(activePlayerUntapAll())); + messageLog->connectToGame(game); + + game->queryGameState(); +*/} void TabGame::processGameEvent(GameEvent *event) { +// game->processGameEvent(event); } diff --git a/cockatrice/src/window_deckeditor.cpp b/cockatrice/src/window_deckeditor.cpp index eb80c072..1a3ebd56 100644 --- a/cockatrice/src/window_deckeditor.cpp +++ b/cockatrice/src/window_deckeditor.cpp @@ -14,6 +14,11 @@ void SearchLineEdit::keyPressEvent(QKeyEvent *event) QLineEdit::keyPressEvent(event); } +const QStringList WndDeckEditor::fileNameFilters = QStringList() + << QObject::tr("Cockatrice decks (*.cod)") + << QObject::tr("Plain text decks (*.dec *.mwDeck)") + << QObject::tr("All files (*.*)"); + WndDeckEditor::WndDeckEditor(QWidget *parent) : QMainWindow(parent) { @@ -63,7 +68,7 @@ WndDeckEditor::WndDeckEditor(QWidget *parent) middleFrame->addLayout(verticalToolBarLayout); middleFrame->addStretch(); - deckModel = new DeckListModel(db, this); + deckModel = new DeckListModel(this); deckView = new QTreeView(); deckView->setModel(deckModel); deckView->setUniformRowHeights(true); @@ -237,16 +242,33 @@ void WndDeckEditor::actLoadDeck() if (!confirmClose()) return; + QFileDialog dialog(this, tr("Load deck")); + QSettings settings; + dialog.setDirectory(settings.value("paths/decks").toString()); + dialog.setNameFilters(fileNameFilters); + if (!dialog.exec()) + return; + + QString fileName = dialog.selectedFiles().at(0); + DeckList::FileFormat fmt; + switch (fileNameFilters.indexOf(dialog.selectedNameFilter())) { + case 0: fmt = DeckList::CockatriceFormat; break; + case 1: fmt = DeckList::PlainTextFormat; break; + default: fmt = DeckList::PlainTextFormat; break; + } + DeckList *l = deckModel->getDeckList(); - if (l->loadDialog(this)) { - lastFileName = l->getLastFileName(); - lastFileFormat = l->getLastFileFormat(); + if (l->loadFromFile(fileName, fmt)) { + lastFileName = fileName; + lastFileFormat = fmt; nameEdit->setText(l->getName()); commentsEdit->setText(l->getComments()); deckModel->sort(1); deckView->expandAll(); deckView->resizeColumnToContents(0); setWindowModified(false); + + deckModel->cacheCardPictures(this); } } @@ -263,14 +285,31 @@ bool WndDeckEditor::actSaveDeck() bool WndDeckEditor::actSaveDeckAs() { - DeckList *l = deckModel->getDeckList(); - if (l->saveDialog(this)) { - lastFileName = l->getLastFileName(); - lastFileFormat = l->getLastFileFormat(); + QFileDialog dialog(this, tr("Save deck")); + QSettings settings; + dialog.setDirectory(settings.value("paths/decks").toString()); + dialog.setAcceptMode(QFileDialog::AcceptSave); + dialog.setConfirmOverwrite(true); + dialog.setDefaultSuffix("cod"); + dialog.setNameFilters(fileNameFilters); + if (!dialog.exec()) + return false; + + QString fileName = dialog.selectedFiles().at(0); + DeckList::FileFormat fmt; + switch (fileNameFilters.indexOf(dialog.selectedNameFilter())) { + case 0: fmt = DeckList::CockatriceFormat; break; + case 1: fmt = DeckList::PlainTextFormat; break; + default: fmt = DeckList::PlainTextFormat; break; + } + + if (deckModel->getDeckList()->saveToFile(fileName, fmt)) { + lastFileName = fileName; + lastFileFormat = fmt; setWindowModified(false); return true; - } else - return false; + } + return false; } void WndDeckEditor::actPrintDeck() diff --git a/cockatrice/src/window_deckeditor.h b/cockatrice/src/window_deckeditor.h index c7020a91..6e9f4410 100644 --- a/cockatrice/src/window_deckeditor.h +++ b/cockatrice/src/window_deckeditor.h @@ -51,6 +51,7 @@ private: void recursiveExpand(const QModelIndex &index); bool confirmClose(); + static const QStringList fileNameFilters; QString lastFileName; DeckList::FileFormat lastFileFormat; diff --git a/cockatrice/src/window_main.cpp b/cockatrice/src/window_main.cpp index 4dce8922..354442f7 100644 --- a/cockatrice/src/window_main.cpp +++ b/cockatrice/src/window_main.cpp @@ -110,18 +110,9 @@ void MainWindow::statusChanged(ClientStatus _status) // case StatusPlaying: { /* chatWidget->disableChat(); - game = new Game(db, client, scene, menuBar(), this); - connect(game, SIGNAL(hoverCard(QString)), cardInfo, SLOT(setCard(const QString &))); - connect(game, SIGNAL(playerAdded(Player *)), this, SLOT(playerAdded(Player *))); - connect(game, SIGNAL(playerRemoved(Player *)), scene, SLOT(removePlayer(Player *))); - connect(game, SIGNAL(setActivePhase(int)), phasesToolbar, SLOT(setActivePhase(int))); - connect(phasesToolbar, SIGNAL(signalDrawCard()), game, SLOT(activePlayerDrawCard())); - connect(phasesToolbar, SIGNAL(signalUntapAll()), game, SLOT(activePlayerUntapAll())); - messageLog->connectToGame(game); aRestartGame->setEnabled(true); aLeaveGame->setEnabled(true); - game->queryGameState(); phasesToolbar->show(); view->show();