started implementing a simple deck editor

This commit is contained in:
Max-Wilhelm Bruker 2009-04-04 19:51:23 +02:00
parent 46a43aade4
commit e6d6162426
13 changed files with 333 additions and 27 deletions

View file

@ -12,5 +12,5 @@ QT += network
#QTPLUGIN += qjpeg
# Input
HEADERS += src/counter.h src/dlg_games.h src/dlg_creategame.h src/dlg_connect.h src/gamesmodel.h src/client.h src/window.h src/servergame.h src/servereventdata.h src/serverresponse.h src/pendingcommand.h src/zonelist.h src/counterlist.h src/cardzone.h src/player.h src/cardlist.h src/carditem.h src/tablezone.h src/handzone.h src/playerlist.h src/game.h src/carddatabase.h src/gameview.h src/decklistmodel.h src/dlg_startgame.h src/cardinfowidget.h src/messagelogwidget.h src/serverzonecard.h src/zoneviewzone.h src/zoneviewwidget.h src/libraryzone.h src/gravezone.h src/rfgzone.h src/sideboardzone.h src/carddragitem.h src/zoneclosebutton.h src/zoneviewlayout.h src/playerarea.h
SOURCES += src/counter.cpp src/dlg_games.cpp src/dlg_creategame.cpp src/dlg_connect.cpp src/client.cpp src/main.cpp src/window.cpp src/servereventdata.cpp src/gamesmodel.cpp src/player.cpp src/cardzone.cpp src/zonelist.cpp src/counterlist.cpp src/cardlist.cpp src/carditem.cpp src/tablezone.cpp src/handzone.cpp src/playerlist.cpp src/game.cpp src/carddatabase.cpp src/gameview.cpp src/decklistmodel.cpp src/dlg_startgame.cpp src/cardinfowidget.cpp src/messagelogwidget.cpp src/zoneviewzone.cpp src/zoneviewwidget.cpp src/libraryzone.cpp src/gravezone.cpp src/rfgzone.cpp src/sideboardzone.cpp src/carddragitem.cpp src/zoneclosebutton.cpp src/zoneviewlayout.cpp src/playerarea.cpp
HEADERS += src/counter.h src/dlg_games.h src/dlg_creategame.h src/dlg_connect.h src/gamesmodel.h src/client.h src/window_main.h src/servergame.h src/servereventdata.h src/serverresponse.h src/pendingcommand.h src/zonelist.h src/counterlist.h src/cardzone.h src/player.h src/cardlist.h src/carditem.h src/tablezone.h src/handzone.h src/playerlist.h src/game.h src/carddatabase.h src/gameview.h src/decklistmodel.h src/dlg_startgame.h src/cardinfowidget.h src/messagelogwidget.h src/serverzonecard.h src/zoneviewzone.h src/zoneviewwidget.h src/libraryzone.h src/gravezone.h src/rfgzone.h src/sideboardzone.h src/carddragitem.h src/zoneclosebutton.h src/zoneviewlayout.h src/playerarea.h src/carddatabasemodel.h src/window_deckeditor.h
SOURCES += src/counter.cpp src/dlg_games.cpp src/dlg_creategame.cpp src/dlg_connect.cpp src/client.cpp src/main.cpp src/window_main.cpp src/servereventdata.cpp src/gamesmodel.cpp src/player.cpp src/cardzone.cpp src/zonelist.cpp src/counterlist.cpp src/cardlist.cpp src/carditem.cpp src/tablezone.cpp src/handzone.cpp src/playerlist.cpp src/game.cpp src/carddatabase.cpp src/gameview.cpp src/decklistmodel.cpp src/dlg_startgame.cpp src/cardinfowidget.cpp src/messagelogwidget.cpp src/zoneviewzone.cpp src/zoneviewwidget.cpp src/libraryzone.cpp src/gravezone.cpp src/rfgzone.cpp src/sideboardzone.cpp src/carddragitem.cpp src/zoneclosebutton.cpp src/zoneviewlayout.cpp src/playerarea.cpp src/carddatabasemodel.cpp src/window_deckeditor.cpp

View file

@ -97,6 +97,17 @@ CardInfo *CardDatabase::getCard(const QString &cardName)
}
}
QList<CardInfo *>CardDatabase::getCardList()
{
QList<CardInfo *> cardList;
QHashIterator<QString, CardInfo *> i(hash);
while (i.hasNext()) {
i.next();
cardList.append(i.value());
}
return cardList;
}
void CardDatabase::importOracle()
{
clear();

View file

@ -43,6 +43,7 @@ public:
~CardDatabase();
void clear();
CardInfo *getCard(const QString &cardName = QString());
QList<CardInfo *> getCardList();
void importOracle();
int loadFromFile(const QString &fileName);
bool saveToFile(const QString &fileName);

View file

@ -0,0 +1,87 @@
#include "carddatabasemodel.h"
CardDatabaseModel::CardDatabaseModel(CardDatabase *_db, QObject *parent)
: QAbstractListModel(parent), db(_db)
{
cardList = db->getCardList();
}
CardDatabaseModel::~CardDatabaseModel()
{
}
int CardDatabaseModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return cardList.size();
}
int CardDatabaseModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 5;
}
QVariant CardDatabaseModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if ((index.row() >= cardList.size()) || (index.column() >= 5))
return QVariant();
if (role != Qt::DisplayRole)
return QVariant();
CardInfo *card = cardList.at(index.row());
switch (index.column()){
case 0: return card->getName();
case 1: return card->getEditions().join(", ");
case 2: return card->getManacost();
case 3: return card->getCardType();
case 4: return card->getPowTough();
default: return QVariant();
}
}
QVariant CardDatabaseModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation != Qt::Horizontal)
return QVariant();
switch (section) {
case 0: return QString(tr("Name"));
case 1: return QString(tr("Sets"));
case 2: return QString(tr("Mana cost"));
case 3: return QString(tr("Card type"));
case 4: return QString(tr("P/T"));
default: return QVariant();
}
}
class CardInfoCompare {
private:
int column;
Qt::SortOrder order;
public:
CardInfoCompare(int _column, Qt::SortOrder _order) : column(_column), order(_order) { }
bool operator()(CardInfo *a, CardInfo *b) const
{
bool result;
switch (column) {
case 0: result = (a->getName() < b->getName()); break;
case 1: result = (a->getEditions().join("") < b->getEditions().join("")); break;
case 2: result = (a->getManacost() < b->getManacost()); break;
case 3: result = (a->getCardType() < b->getCardType()); break;
case 4: result = (a->getPowTough() < b->getPowTough()); break;
default: result = false;
}
return (order == Qt::AscendingOrder) ^ result;
}
};
void CardDatabaseModel::sort(int column, Qt::SortOrder order)
{
CardInfoCompare cmp(column, order);
qSort(cardList.begin(), cardList.end(), cmp);
}

View file

@ -0,0 +1,23 @@
#ifndef CARDDATABASEMODEL_H
#define CARDDATABASEMODEL_H
#include <QAbstractListModel>
#include <QList>
#include "carddatabase.h"
class CardDatabaseModel : public QAbstractListModel {
Q_OBJECT
public:
CardDatabaseModel(CardDatabase *_db, QObject *parent = 0);
~CardDatabaseModel();
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);
private:
QList<CardInfo *> cardList;
CardDatabase *db;
};
#endif

View file

@ -1,6 +1,13 @@
#include "decklistmodel.h"
#include <QFile>
#include <QTextStream>
#include "decklistmodel.h"
#include "carddatabase.h"
DeckListModel::DeckListModel(CardDatabase *_db, QObject *parent)
: QAbstractListModel(parent), db(_db)
{
}
DeckListModel::~DeckListModel()
{
@ -57,13 +64,14 @@ void DeckListModel::cleanList()
while (i.hasNext())
delete i.next();
deckList.clear();
reset();
}
void DeckListModel::loadFromFile(const QString &fileName)
bool DeckListModel::loadFromFile(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
return false;
QTextStream in(&file);
cleanList();
while (!in.atEnd()) {
@ -81,7 +89,23 @@ void DeckListModel::loadFromFile(const QString &fileName)
DecklistRow *row = new DecklistRow(number, line.mid(i + 1), isSideboard);
deckList << row;
}
cacheCardPictures();
reset();
return true;
}
bool DeckListModel::saveToFile(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return false;
QTextStream out(&file);
QListIterator<DecklistRow *> i(deckList);
while (i.hasNext()) {
DecklistRow *r = i.next();
out << QString("%1%2 %3\n").arg(r->isSideboard() ? "SB: " : "").arg(r->getNumber()).arg(r->getCard());
}
return true;
}
DecklistRow *DeckListModel::getRow(int row) const
@ -90,3 +114,12 @@ DecklistRow *DeckListModel::getRow(int row) const
return 0;
return deckList.at(row);
}
void DeckListModel::cacheCardPictures()
{
QListIterator<DecklistRow *> i(deckList);
while (i.hasNext()) {
DecklistRow *r = i.next();
db->getCard(r->getCard())->getPixmap();
}
}

View file

@ -4,6 +4,8 @@
#include <QAbstractListModel>
#include <QList>
class CardDatabase;
class DecklistRow {
private:
int number;
@ -19,18 +21,20 @@ public:
class DeckListModel : public QAbstractListModel {
Q_OBJECT
public:
DeckListModel(QObject *parent = 0)
: QAbstractListModel(parent) { }
DeckListModel(CardDatabase *_db, QObject *parent = 0);
~DeckListModel();
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
void loadFromFile(const QString &fileName);
bool loadFromFile(const QString &fileName);
bool saveToFile(const QString &fileName);
DecklistRow *getRow(int row) const;
private:
QList<DecklistRow *> deckList;
void cleanList();
private:
CardDatabase *db;
QList<DecklistRow *> deckList;
void cacheCardPictures();
};
#endif

View file

@ -8,7 +8,7 @@ DlgStartGame::DlgStartGame(CardDatabase *_db, QWidget *parent)
: QDialog(parent), db(_db)
{
tableView = new QTreeView;
tableModel = new DeckListModel(this);
tableModel = new DeckListModel(db, this);
tableView->setModel(tableModel);
loadButton = new QPushButton(tr("&Load..."));
@ -36,19 +36,9 @@ DlgStartGame::DlgStartGame(CardDatabase *_db, QWidget *parent)
void DlgStartGame::actLoad()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Load deck"), QString(), tr("Deck files (*.dec)"));
if (!fileName.isEmpty())
tableModel->loadFromFile(fileName);
// Precache card pictures
for (int i = 0; i < tableModel->rowCount(); i++) {
QString cardName = tableModel->getRow(i)->getCard();
CardInfo *card = db->getCard(cardName);
if (!card) {
qDebug(QString("Invalid card: %1").arg(cardName).toLatin1());
continue;
}
card->getPixmap();
}
if (fileName.isEmpty())
return;
tableModel->loadFromFile(fileName);
emit newDeckLoaded(getDeckList());
}

View file

@ -22,7 +22,7 @@
#include <QApplication>
#include <QTextCodec>
#include <QtPlugin>
#include "window.h"
#include "window_main.h"
#include <stdio.h>
//Q_IMPORT_PLUGIN(qjpeg)

View file

@ -0,0 +1,106 @@
#include <QtGui>
#include "window_deckeditor.h"
#include "carddatabase.h"
#include "carddatabasemodel.h"
#include "decklistmodel.h"
#include "cardinfowidget.h"
WndDeckEditor::WndDeckEditor(CardDatabase *_db, QWidget *parent)
: QMainWindow(parent), db(_db)
{
databaseModel = new CardDatabaseModel(db);
databaseView = new QTreeView();
databaseView->setModel(databaseModel);
databaseView->setSortingEnabled(true);
connect(databaseView->selectionModel(), SIGNAL(currentRowChanged(const QModelIndex &, const QModelIndex &)), this, SLOT(updateCardInfoLeft(const QModelIndex &, const QModelIndex &)));
deckModel = new DeckListModel(db, this);
deckView = new QTreeView();
deckView->setModel(deckModel);
// deckView->setSortingEnabled(true);
connect(deckView->selectionModel(), SIGNAL(currentRowChanged(const QModelIndex &, const QModelIndex &)), this, SLOT(updateCardInfoRight(const QModelIndex &, const QModelIndex &)));
cardInfo = new CardInfoWidget(db);
QVBoxLayout *middleFrame = new QVBoxLayout;
middleFrame->addWidget(cardInfo);
middleFrame->addStretch();
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addWidget(databaseView);
mainLayout->addLayout(middleFrame);
mainLayout->addWidget(deckView);
QWidget *centralWidget = new QWidget;
centralWidget->setLayout(mainLayout);
setCentralWidget(centralWidget);
setWindowTitle(tr("Card database"));
aNewDeck = new QAction(tr("&New deck"), this);
connect(aNewDeck, SIGNAL(triggered()), this, SLOT(actNewDeck()));
aLoadDeck = new QAction(tr("&Load deck..."), this);
aLoadDeck->setShortcut(tr("Ctrl+L"));
connect(aLoadDeck, SIGNAL(triggered()), this, SLOT(actLoadDeck()));
aSaveDeck = new QAction(tr("&Save deck"), this);
aSaveDeck->setShortcut(tr("Ctrl+S"));
connect(aSaveDeck, SIGNAL(triggered()), this, SLOT(actSaveDeck()));
aSaveDeckAs = new QAction(tr("&Save deck as..."), this);
connect(aSaveDeckAs, SIGNAL(triggered()), this, SLOT(actSaveDeckAs()));
deckMenu = menuBar()->addMenu(tr("Deck"));
deckMenu->addAction(aNewDeck);
deckMenu->addAction(aLoadDeck);
deckMenu->addAction(aSaveDeck);
deckMenu->addAction(aSaveDeckAs);
}
WndDeckEditor::~WndDeckEditor()
{
}
void WndDeckEditor::updateCardInfoLeft(const QModelIndex &current, const QModelIndex &previous)
{
Q_UNUSED(previous);
cardInfo->setCard(current.sibling(current.row(), 0).data().toString());
}
void WndDeckEditor::updateCardInfoRight(const QModelIndex &current, const QModelIndex &previous)
{
Q_UNUSED(previous);
cardInfo->setCard(current.sibling(current.row(), 1).data().toString());
}
void WndDeckEditor::actNewDeck()
{
deckModel->cleanList();
lastFileName = QString();
}
void WndDeckEditor::actLoadDeck()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Load deck"), QString(), tr("Deck files (*.dec)"));
if (fileName.isEmpty())
return;
if (deckModel->loadFromFile(fileName))
lastFileName = fileName;
}
void WndDeckEditor::actSaveDeck()
{
if (lastFileName.isEmpty())
actSaveDeckAs();
else
deckModel->saveToFile(lastFileName);
}
void WndDeckEditor::actSaveDeckAs()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Save deck as"), QString(), tr("Deck files (*.dec)"));
if (!fileName.endsWith(".dec"))
fileName.append(".dec");
if (deckModel->saveToFile(fileName))
lastFileName = fileName;
}

View file

@ -0,0 +1,39 @@
#ifndef WINDOW_DECKEDITOR_H
#define WINDOW_DECKEDITOR_H
#include <QMainWindow>
#include <QAbstractItemModel>
class CardDatabase;
class CardDatabaseModel;
class DeckListModel;
class QTreeView;
class CardInfoWidget;
class WndDeckEditor : public QMainWindow {
Q_OBJECT
private slots:
void updateCardInfoLeft(const QModelIndex &current, const QModelIndex &previous);
void updateCardInfoRight(const QModelIndex &current, const QModelIndex &previous);
void actNewDeck();
void actLoadDeck();
void actSaveDeck();
void actSaveDeckAs();
private:
QString lastFileName;
CardDatabase *db;
CardDatabaseModel *databaseModel;
DeckListModel *deckModel;
QTreeView *databaseView, *deckView;
CardInfoWidget *cardInfo;
QMenu *deckMenu;
QAction *aNewDeck, *aLoadDeck, *aSaveDeck, *aSaveDeckAs;
public:
WndDeckEditor(CardDatabase *_db, QWidget *parent = 0);
~WndDeckEditor();
};
#endif

View file

@ -20,9 +20,10 @@
#include <QtGui>
//#include <QtOpenGL>
#include "window.h"
#include "window_main.h"
#include "dlg_connect.h"
#include "dlg_games.h"
#include "window_deckeditor.h"
#include "cardinfowidget.h"
#include "messagelogwidget.h"
@ -105,6 +106,12 @@ void MainWindow::actLeaveGame()
aGames->setEnabled(true);
}
void MainWindow::actDeckEditor()
{
WndDeckEditor *deckEditor = new WndDeckEditor(db, this);
deckEditor->show();
}
void MainWindow::actExit()
{
close();
@ -170,6 +177,8 @@ void MainWindow::createActions()
aLeaveGame = new QAction(tr("&Leave game"), this);
aLeaveGame->setEnabled(false);
connect(aLeaveGame, SIGNAL(triggered()), this, SLOT(actLeaveGame()));
aDeckEditor = new QAction(tr("&Deck editor"), this);
connect(aDeckEditor, SIGNAL(triggered()), this, SLOT(actDeckEditor()));
aExit = new QAction(tr("&Exit"), this);
connect(aExit, SIGNAL(triggered()), this, SLOT(actExit()));
}
@ -184,6 +193,8 @@ void MainWindow::createMenus()
gameMenu->addAction(aRestartGame);
gameMenu->addAction(aLeaveGame);
gameMenu->addSeparator();
gameMenu->addAction(aDeckEditor);
gameMenu->addSeparator();
gameMenu->addAction(aExit);
actionsMenu = menuBar()->addMenu(tr("&Actions"));

View file

@ -54,6 +54,7 @@ private slots:
void actDisconnect();
void actGames();
void actLeaveGame();
void actDeckEditor();
void actExit();
void updateSceneSize();
@ -64,7 +65,7 @@ private:
void createActions();
void createMenus();
QMenu *gameMenu, *actionsMenu, *cardMenu;
QAction *aConnect, *aDisconnect, *aGames, *aRestartGame, *aLeaveGame, *aExit;
QAction *aConnect, *aDisconnect, *aGames, *aRestartGame, *aLeaveGame, *aDeckEditor, *aExit;
CardInfoWidget *cardInfo;
MessageLogWidget *messageLog;