prepared deck list code to be moved out to common

This commit is contained in:
Max-Wilhelm Bruker 2009-11-16 23:58:07 +01:00
parent 77f5ec29eb
commit 34d2649f65
12 changed files with 112 additions and 134 deletions

View file

@ -3,9 +3,7 @@
#include <QTextStream>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include <QProgressDialog>
#include <QVariant>
#include <QSettings>
#include "decklist.h"
#include "carddatabase.h"
@ -130,8 +128,8 @@ QVector<QPair<int, int> > 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<DecklistCardNode *>(item->at(i));
if (node) {
db->getCard(node->getName())->loadPixmap();
progress->setValue(progress->value() + 1);
} else
cacheCardPicturesHelper(dynamic_cast<InnerDecklistNode *>(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();

View file

@ -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);

View file

@ -6,13 +6,15 @@
#include <QTextDocument>
#include <QPrinter>
#include <QTextTable>
#include <QProgressDialog>
#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<DecklistCardNode *>(item->at(i));
if (node) {
db->getCard(node->getName())->loadPixmap();
progress->setValue(progress->value() + 1);
} else
cacheCardPicturesHelper(dynamic_cast<InnerDecklistNode *>(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;

View file

@ -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);
@ -54,6 +55,7 @@ private:
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<typename T> T getNode(const QModelIndex &index) const

View file

@ -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);

View file

@ -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;

View file

@ -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()));

View file

@ -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();

View file

@ -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);
}

View file

@ -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()

View file

@ -51,6 +51,7 @@ private:
void recursiveExpand(const QModelIndex &index);
bool confirmClose();
static const QStringList fileNameFilters;
QString lastFileName;
DeckList::FileFormat lastFileFormat;

View file

@ -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();