almost finished pre-game deck loading

This commit is contained in:
Max-Wilhelm Bruker 2009-11-22 01:30:16 +01:00
parent 8dcf81654e
commit cf21528a69
16 changed files with 140 additions and 64 deletions

View file

@ -23,6 +23,7 @@ HEADERS += src/counter.h \
src/game.h \ src/game.h \
src/carddatabase.h \ src/carddatabase.h \
src/gameview.h \ src/gameview.h \
src/deck_picturecacher.h \
src/decklistmodel.h \ src/decklistmodel.h \
src/dlg_startgame.h \ src/dlg_startgame.h \
src/cardinfowidget.h \ src/cardinfowidget.h \
@ -69,6 +70,7 @@ SOURCES += src/counter.cpp \
src/game.cpp \ src/game.cpp \
src/carddatabase.cpp \ src/carddatabase.cpp \
src/gameview.cpp \ src/gameview.cpp \
src/deck_picturecacher.cpp \
src/decklistmodel.cpp \ src/decklistmodel.cpp \
src/dlg_startgame.cpp \ src/dlg_startgame.cpp \
src/cardinfowidget.cpp \ src/cardinfowidget.cpp \

View file

@ -0,0 +1,29 @@
#include <QProgressDialog>
#include "deck_picturecacher.h"
#include "decklist.h"
#include "carddatabase.h"
#include "main.h"
void Deck_PictureCacher::cacheHelper(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
cacheHelper(dynamic_cast<InnerDecklistNode *>(item->at(i)), progress);
}
}
void Deck_PictureCacher::cachePictures(DeckList *deck, QWidget *parent)
{
int totalCards = deck->getRoot()->recursiveCount();
QProgressDialog progress(tr("Caching card pictures..."), QString(), 0, totalCards, parent);
progress.setMinimumDuration(1000);
progress.setWindowModality(Qt::WindowModal);
cacheHelper(deck->getRoot(), &progress);
}

View file

@ -0,0 +1,19 @@
#ifndef DECK_PICTURECACHER_H
#define DECK_PICTURECACHER_H
#include <QObject>
class InnerDecklistNode;
class QProgressDialog;
class DeckList;
class QWidget;
class Deck_PictureCacher : public QObject {
Q_OBJECT
private:
static void cacheHelper(InnerDecklistNode *item, QProgressDialog *progress);
public:
static void cachePictures(DeckList *deck, QWidget *parent);
};
#endif

View file

@ -315,29 +315,6 @@ void DeckListModel::cleanList()
reset(); 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) void DeckListModel::printDeckListNode(QTextCursor *cursor, InnerDecklistNode *node)
{ {
static const int totalColumns = 3; static const int totalColumns = 3;

View file

@ -44,7 +44,6 @@ public:
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; } DeckList *getDeckList() const { return deckList; }
void cacheCardPictures(QWidget *parent = 0);
private: private:
DeckList *deckList; DeckList *deckList;
InnerDecklistNode *root; InnerDecklistNode *root;
@ -55,7 +54,6 @@ private:
void debugIndexInfo(const QString &func, const QModelIndex &index) const; void debugIndexInfo(const QString &func, const QModelIndex &index) const;
void debugShowTree(InnerDecklistNode *node, int depth) const; void debugShowTree(InnerDecklistNode *node, int depth) const;
void cacheCardPicturesHelper(InnerDecklistNode *item, QProgressDialog *progress);
void printDeckListNode(QTextCursor *cursor, InnerDecklistNode *node); void printDeckListNode(QTextCursor *cursor, InnerDecklistNode *node);
template<typename T> T getNode(const QModelIndex &index) const template<typename T> T getNode(const QModelIndex &index) const

View file

@ -37,7 +37,6 @@ private:
bool started; bool started;
int currentPhase; int currentPhase;
Player *getActiveLocalPlayer() const;
public slots: public slots:
void activePlayerDrawCard(); void activePlayerDrawCard();
void activePlayerUntapAll(); void activePlayerUntapAll();
@ -107,6 +106,7 @@ public:
void restartGameDialog(); void restartGameDialog();
void hoverCardEvent(CardItem *card); void hoverCardEvent(CardItem *card);
Player *addPlayer(int playerId, const QString &playerName, bool local); Player *addPlayer(int playerId, const QString &playerName, bool local);
Player *getActiveLocalPlayer() const;
const QMap<int, Player *> &getPlayers() const { return players; } const QMap<int, Player *> &getPlayers() const { return players; }
void queryGameState(); void queryGameState();
}; };

View file

@ -12,6 +12,8 @@
#include "zoneviewlayout.h" #include "zoneviewlayout.h"
#include "deckview.h" #include "deckview.h"
#include "decklist.h" #include "decklist.h"
#include "deck_picturecacher.h"
#include "protocol_items.h"
#include "main.h" #include "main.h"
TabGame::TabGame(Client *_client, int _gameId) TabGame::TabGame(Client *_client, int _gameId)
@ -22,12 +24,17 @@ TabGame::TabGame(Client *_client, int _gameId)
gameView = new GameView(scene); gameView = new GameView(scene);
gameView->hide(); gameView->hide();
deckView = new DeckView; loadLocalButton = new QPushButton;
loadRemoteButton = new QPushButton;
DeckList *foo = new DeckList; QHBoxLayout *buttonHBox = new QHBoxLayout;
foo->loadFromFile("/home/brukie/cockatrice/decks/adfb.cod", DeckList::CockatriceFormat); buttonHBox->addWidget(loadLocalButton);
deckView->setDeck(foo); buttonHBox->addWidget(loadRemoteButton);
// deckView->hide(); buttonHBox->addStretch();
deckView = new DeckView;
QVBoxLayout *deckViewLayout = new QVBoxLayout;
deckViewLayout->addLayout(buttonHBox);
deckViewLayout->addWidget(deckView);
cardInfo = new CardInfoWidget(db); cardInfo = new CardInfoWidget(db);
messageLog = new MessageLogWidget; messageLog = new MessageLogWidget;
@ -50,13 +57,16 @@ TabGame::TabGame(Client *_client, int _gameId)
QHBoxLayout *mainLayout = new QHBoxLayout; QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addWidget(phasesToolbar); mainLayout->addWidget(phasesToolbar);
mainLayout->addWidget(gameView, 10); mainLayout->addWidget(gameView, 10);
mainLayout->addWidget(deckView, 10); mainLayout->addLayout(deckViewLayout, 10);
mainLayout->addLayout(verticalLayout); mainLayout->addLayout(verticalLayout);
aCloseMostRecentZoneView = new QAction(this); aCloseMostRecentZoneView = new QAction(this);
connect(aCloseMostRecentZoneView, SIGNAL(triggered()), zoneLayout, SLOT(closeMostRecentZoneView())); connect(aCloseMostRecentZoneView, SIGNAL(triggered()), zoneLayout, SLOT(closeMostRecentZoneView()));
addAction(aCloseMostRecentZoneView); addAction(aCloseMostRecentZoneView);
connect(loadLocalButton, SIGNAL(clicked()), this, SLOT(loadLocalDeck()));
connect(loadRemoteButton, SIGNAL(clicked()), this, SLOT(loadRemoteDeck()));
connect(sayEdit, SIGNAL(returnPressed()), this, SLOT(actSay())); connect(sayEdit, SIGNAL(returnPressed()), this, SLOT(actSay()));
// connect(client, SIGNAL(maxPingTime(int, int)), pingWidget, SLOT(setPercentage(int, int))); // connect(client, SIGNAL(maxPingTime(int, int)), pingWidget, SLOT(setPercentage(int, int)));
@ -81,6 +91,8 @@ TabGame::TabGame(Client *_client, int _gameId)
void TabGame::retranslateUi() void TabGame::retranslateUi()
{ {
loadLocalButton->setText(tr("Load &local deck"));
loadRemoteButton->setText(tr("Load deck from &server"));
sayLabel->setText(tr("&Say:")); sayLabel->setText(tr("&Say:"));
cardInfo->retranslateUi(); cardInfo->retranslateUi();
// if (game) // if (game)
@ -94,3 +106,43 @@ void TabGame::processGameEvent(GameEvent *event)
{ {
// game->processGameEvent(event); // game->processGameEvent(event);
} }
void TabGame::loadLocalDeck()
{
QFileDialog dialog(this, tr("Load deck"));
QSettings settings;
dialog.setDirectory(settings.value("paths/decks").toString());
dialog.setNameFilters(DeckList::fileNameFilters);
if (!dialog.exec())
return;
QString fileName = dialog.selectedFiles().at(0);
DeckList::FileFormat fmt = DeckList::getFormatFromNameFilter(dialog.selectedNameFilter());
DeckList *deck = new DeckList;
if (!deck->loadFromFile(fileName, fmt)) {
delete deck;
// Error message
return;
}
Command_DeckSelect *cmd = new Command_DeckSelect(gameId, deck, -1);
connect(cmd, SIGNAL(finished(ProtocolResponse *)), this, SLOT(deckSelectFinished(ProtocolResponse *)));
client->sendCommand(cmd);
}
void TabGame::loadRemoteDeck()
{
}
void TabGame::deckSelectFinished(ProtocolResponse *r)
{
Response_DeckDownload *resp = qobject_cast<Response_DeckDownload *>(r);
if (!resp)
return;
Command_DeckSelect *cmd = static_cast<Command_DeckSelect *>(sender());
delete cmd->getDeck();
Deck_PictureCacher::cachePictures(resp->getDeck(), this);
deckView->setDeck(resp->getDeck());
}

View file

@ -18,6 +18,7 @@ class QPushButton;
class ZoneViewLayout; class ZoneViewLayout;
class ZoneViewWidget; class ZoneViewWidget;
class PhasesToolbar; class PhasesToolbar;
class ProtocolResponse;
class TabGame : public QWidget { class TabGame : public QWidget {
Q_OBJECT Q_OBJECT
@ -25,6 +26,7 @@ private:
Client *client; Client *client;
int gameId; int gameId;
QPushButton *loadLocalButton, *loadRemoteButton;
CardInfoWidget *cardInfo; CardInfoWidget *cardInfo;
MessageLogWidget *messageLog; MessageLogWidget *messageLog;
QLabel *sayLabel; QLabel *sayLabel;
@ -37,6 +39,9 @@ private:
ZoneViewLayout *zoneLayout; ZoneViewLayout *zoneLayout;
QAction *aCloseMostRecentZoneView; QAction *aCloseMostRecentZoneView;
private slots: private slots:
void loadLocalDeck();
void loadRemoteDeck();
void deckSelectFinished(ProtocolResponse *r);
public: public:
TabGame(Client *_client, int _gameId); TabGame(Client *_client, int _gameId);
void retranslateUi(); void retranslateUi();

View file

@ -5,6 +5,7 @@
#include "carddatabasemodel.h" #include "carddatabasemodel.h"
#include "decklistmodel.h" #include "decklistmodel.h"
#include "cardinfowidget.h" #include "cardinfowidget.h"
#include "deck_picturecacher.h"
#include "main.h" #include "main.h"
void SearchLineEdit::keyPressEvent(QKeyEvent *event) void SearchLineEdit::keyPressEvent(QKeyEvent *event)
@ -14,11 +15,6 @@ void SearchLineEdit::keyPressEvent(QKeyEvent *event)
QLineEdit::keyPressEvent(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) WndDeckEditor::WndDeckEditor(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
{ {
@ -245,18 +241,12 @@ void WndDeckEditor::actLoadDeck()
QFileDialog dialog(this, tr("Load deck")); QFileDialog dialog(this, tr("Load deck"));
QSettings settings; QSettings settings;
dialog.setDirectory(settings.value("paths/decks").toString()); dialog.setDirectory(settings.value("paths/decks").toString());
dialog.setNameFilters(fileNameFilters); dialog.setNameFilters(DeckList::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::FileFormat fmt = DeckList::getFormatFromNameFilter(dialog.selectedNameFilter());
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(); DeckList *l = deckModel->getDeckList();
if (l->loadFromFile(fileName, fmt)) { if (l->loadFromFile(fileName, fmt)) {
lastFileName = fileName; lastFileName = fileName;
@ -268,7 +258,7 @@ void WndDeckEditor::actLoadDeck()
deckView->resizeColumnToContents(0); deckView->resizeColumnToContents(0);
setWindowModified(false); setWindowModified(false);
deckModel->cacheCardPictures(this); Deck_PictureCacher::cachePictures(l, this);
} }
} }
@ -291,17 +281,12 @@ bool WndDeckEditor::actSaveDeckAs()
dialog.setAcceptMode(QFileDialog::AcceptSave); dialog.setAcceptMode(QFileDialog::AcceptSave);
dialog.setConfirmOverwrite(true); dialog.setConfirmOverwrite(true);
dialog.setDefaultSuffix("cod"); dialog.setDefaultSuffix("cod");
dialog.setNameFilters(fileNameFilters); dialog.setNameFilters(DeckList::fileNameFilters);
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::FileFormat fmt = DeckList::getFormatFromNameFilter(dialog.selectedNameFilter());
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)) { if (deckModel->getDeckList()->saveToFile(fileName, fmt)) {
lastFileName = fileName; lastFileName = fileName;

View file

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

View file

@ -173,6 +173,11 @@ 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(QObject *parent) DeckList::DeckList(QObject *parent)
: QObject(parent), currentZone(0) : QObject(parent), currentZone(0)
{ {
@ -341,6 +346,15 @@ bool DeckList::saveToFile(const QString &fileName, FileFormat fmt)
return result; 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();

View file

@ -95,6 +95,7 @@ public slots:
void setName(const QString &_name = QString()) { name = _name; } void setName(const QString &_name = QString()) { name = _name; }
void setComments(const QString &_comments = QString()) { comments = _comments; } void setComments(const QString &_comments = QString()) { comments = _comments; }
public: public:
static const QStringList fileNameFilters;
DeckList(QObject *parent = 0); DeckList(QObject *parent = 0);
~DeckList(); ~DeckList();
QString getName() const { return name; } QString getName() const { return name; }
@ -112,8 +113,7 @@ public:
bool saveToFile_Plain(QIODevice *device); bool saveToFile_Plain(QIODevice *device);
bool loadFromFile(const QString &fileName, FileFormat fmt); bool loadFromFile(const QString &fileName, FileFormat fmt);
bool saveToFile(const QString &fileName, FileFormat fmt); bool saveToFile(const QString &fileName, FileFormat fmt);
bool loadDialog(QWidget *parent = 0); static FileFormat getFormatFromNameFilter(const QString &selectedNameFilter);
bool saveDialog(QWidget *parent = 0);
void cleanList(); void cleanList();

View file

@ -141,7 +141,7 @@ void Command_DeckUpload::writeElement(QXmlStreamWriter *xml)
} }
Command_DeckSelect::Command_DeckSelect(int _gameId, DeckList *_deck, int _deckId) Command_DeckSelect::Command_DeckSelect(int _gameId, DeckList *_deck, int _deckId)
: GameCommand("deck_upload", _gameId), deck(_deck), deckId(_deckId), readFinished(false) : GameCommand("deck_select", _gameId), deck(_deck), deckId(_deckId), readFinished(false)
{ {
setParameter("deck_id", _deckId); setParameter("deck_id", _deckId);
} }
@ -299,11 +299,6 @@ Response_DeckDownload::Response_DeckDownload(int _cmdId, ResponseCode _responseC
{ {
} }
Response_DeckDownload::~Response_DeckDownload()
{
delete deck;
}
bool Response_DeckDownload::readElement(QXmlStreamReader *xml) bool Response_DeckDownload::readElement(QXmlStreamReader *xml)
{ {
if (readFinished) if (readFinished)

View file

@ -212,7 +212,6 @@ protected:
void writeElement(QXmlStreamWriter *xml); void writeElement(QXmlStreamWriter *xml);
public: public:
Response_DeckDownload(int _cmdId = -1, ResponseCode _responseCode = RespOk, DeckList *_deck = 0); Response_DeckDownload(int _cmdId = -1, ResponseCode _responseCode = RespOk, DeckList *_deck = 0);
~Response_DeckDownload();
int getItemId() const { return ItemId_Response_DeckDownload; } int getItemId() const { return ItemId_Response_DeckDownload; }
static ProtocolItem *newItem() { return new Response_DeckDownload; } static ProtocolItem *newItem() { return new Response_DeckDownload; }
DeckList *getDeck() const { return deck; } DeckList *getDeck() const { return deck; }

View file

@ -263,7 +263,8 @@ ResponseCode Server_ProtocolHandler::cmdDeckSelect(Command_DeckSelect *cmd, Serv
game->sendGameEvent(new Event_DeckSelect(-1, player->getPlayerId(), cmd->getDeckId())); game->sendGameEvent(new Event_DeckSelect(-1, player->getPlayerId(), cmd->getDeckId()));
return RespOk; sendProtocolItem(new Response_DeckDownload(cmd->getCmdId(), RespOk, deck));
return RespNothing;
} }
ResponseCode Server_ProtocolHandler::cmdSay(Command_Say *cmd, Server_Game *game, Server_Player *player) ResponseCode Server_ProtocolHandler::cmdSay(Command_Say *cmd, Server_Game *game, Server_Player *player)

View file

@ -316,5 +316,6 @@ ResponseCode ServerSocketInterface::cmdDeckDownload(Command_DeckDownload *cmd)
return r; return r;
} }
sendProtocolItem(new Response_DeckDownload(cmd->getCmdId(), RespOk, deck)); sendProtocolItem(new Response_DeckDownload(cmd->getCmdId(), RespOk, deck));
delete deck;
return RespNothing; return RespNothing;
} }