Deck editor

This commit is contained in:
Max-Wilhelm Bruker 2009-05-23 20:04:14 +02:00
parent bb30012fbb
commit 3ae865178b
10 changed files with 175 additions and 43 deletions

View file

@ -3,7 +3,7 @@
######################################################################
TEMPLATE = app
TARGET =
TARGET =
DEPENDPATH += . src
INCLUDEPATH += . src
MOC_DIR = build

View file

@ -45,7 +45,7 @@ void CardInfoWidget::setCard(CardInfo *card)
QPixmap *pixmap = card->getPixmap();
if (aspectratio == 0)
aspectratio = (double) pixmap->height() / pixmap->width();
double w = width() * 2.0 / 3.0;
double w = 180;
cardPicture->setPixmap(pixmap->scaled((int) w, (int) (w * aspectratio), Qt::KeepAspectRatio, Qt::SmoothTransformation));
nameLabel2->setText(card->getName());

View file

@ -7,10 +7,9 @@
#include "decklist.h"
#include "carddatabase.h"
DeckList::DeckList(CardDatabase *_db)
: db(_db)
DeckList::DeckList(CardDatabase *_db, QObject *parent)
: QObject(parent), db(_db)
{
}
DeckList::~DeckList()
@ -136,8 +135,10 @@ 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) {
cacheCardPictures(parent);
emit deckLoaded();
}
return result;
}

View file

@ -13,9 +13,11 @@ private:
QString card;
bool sideboard;
public:
DecklistRow(int _number, const QString &_card, bool _sideboard) : number(_number), card(_card), sideboard(_sideboard) { }
DecklistRow(int _number = 1, const QString &_card = QString(), bool _sideboard = false) : number(_number), card(_card), sideboard(_sideboard) { }
int getNumber() const { return number; }
void setNumber(int _number) { number = _number; }
QString getCard() const { return card; }
void setCard(const QString &_card) { card = _card; }
bool isSideboard() const { return sideboard; }
};
@ -30,11 +32,13 @@ private:
QString name, comments;
QString lastFileName;
FileFormat lastFileFormat;
signals:
void deckLoaded();
public slots:
void setName(const QString &_name) { name = _name; }
void setComments(const QString &_comments) { comments = _comments; }
public:
DeckList(CardDatabase *_db);
DeckList(CardDatabase *_db, QObject *parent = 0);
~DeckList();
QString getName() const { return name; }
QString getComments() const { return comments; }

View file

@ -6,34 +6,39 @@
DeckListModel::DeckListModel(CardDatabase *_db, QObject *parent)
: QAbstractListModel(parent), db(_db)
{
deckList = new DeckList(db);
deckList = new DeckList(db, this);
connect(deckList, SIGNAL(deckLoaded()), this, SLOT(resetModel()));
}
DeckListModel::~DeckListModel()
{
delete deckList;
}
int DeckListModel::rowCount(const QModelIndex &parent) const
void DeckListModel::resetModel()
{
Q_UNUSED(parent);
reset();
}
int DeckListModel::rowCount(const QModelIndex &/*parent*/) const
{
// qDebug(QString("rowCount = %1").arg(deckList->size()).toLatin1());
return deckList->size();
}
int DeckListModel::columnCount(const QModelIndex &parent) const
int DeckListModel::columnCount(const QModelIndex &/*parent*/) const
{
Q_UNUSED(parent);
return 2;
}
QVariant DeckListModel::data(const QModelIndex &index, int role) const
{
// qDebug(QString("data() called: index.row = %1, column = %2, role = %3").arg(index.row()).arg(index.column()).arg(role).toLatin1());
if (!index.isValid())
return QVariant();
if ((index.row() >= deckList->size()) || (index.column() >= 2))
return QVariant();
if (role != Qt::DisplayRole)
if ((role != Qt::DisplayRole) && (role != Qt::EditRole))
return QVariant();
DecklistRow *r = deckList->at(index.row());
@ -46,6 +51,7 @@ QVariant DeckListModel::data(const QModelIndex &index, int role) const
QVariant DeckListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
// qDebug(QString("headerData() called: section = %1, orientation = %2, role = %3").arg(section).arg(orientation).arg(role).toLatin1());
if (role != Qt::DisplayRole)
return QVariant();
if (orientation != Qt::Horizontal)
@ -57,15 +63,52 @@ QVariant DeckListModel::headerData(int section, Qt::Orientation orientation, int
}
}
Qt::ItemFlags DeckListModel::flags(const QModelIndex &index) const
{
if (index.column() == 0)
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
else
return QAbstractItemModel::flags(index);
}
bool DeckListModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!index.isValid() || role != Qt::EditRole)
return false;
switch (index.column()) {
case 0: deckList->at(index.row())->setNumber(value.toInt()); break;
case 1: deckList->at(index.row())->setCard(value.toString()); break;
default: return false;
}
emit dataChanged(index, index);
return true;
}
bool DeckListModel::removeRows(int row, int count, const QModelIndex &parent)
{
beginRemoveRows(parent, row, row + count - 1);
for (int i = 0; i < count; i++)
deckList->removeAt(row);
endRemoveRows();
return true;
}
bool DeckListModel::insertRows(int row, int count, const QModelIndex &parent)
{
beginInsertRows(parent, row, row + count - 1);
for (int i = 0; i < count; i++)
deckList->insert(row, new DecklistRow);
endInsertRows();
return true;
}
void DeckListModel::cleanList()
{
deckList->cleanList();
reset();
}
DecklistRow *DeckListModel::getRow(int row) const
{
if (row >= deckList->size())
return 0;
return deckList->at(row);
}

View file

@ -9,6 +9,8 @@ class CardDatabase;
class DeckListModel : public QAbstractListModel {
Q_OBJECT
private slots:
void resetModel();
public:
DeckListModel(CardDatabase *_db, QObject *parent = 0);
~DeckListModel();
@ -16,7 +18,10 @@ public:
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;
DecklistRow *getRow(int row) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
bool setData(const QModelIndex &index, const QVariant &value, int role);
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
void cleanList();
DeckList *getDeckList() const { return deckList; }
bool loadFromFile(const QString &fileName, DeckList::FileFormat fmt);

View file

@ -37,7 +37,7 @@ void DlgStartGame::actLoad()
{
if (!tableModel->getDeckList()->loadDialog(this))
return;
tableView->reset();
emit newDeckLoaded(getDeckList());
}
@ -45,8 +45,9 @@ void DlgStartGame::actLoad()
QStringList DlgStartGame::getDeckList() const
{
QStringList result;
for (int i = 0; i < tableModel->rowCount(); i++) {
DecklistRow *temp = tableModel->getRow(i);
DeckList *deckList = tableModel->getDeckList();
for (int i = 0; i < deckList->size(); i++) {
DecklistRow *temp = deckList->at(i);
for (int j = 0; j < temp->getNumber(); j++)
result << QString("%1%2").arg(temp->isSideboard() ? "SB:" : "").arg(temp->getCard());
}

View file

@ -38,16 +38,16 @@ void myMessageOutput(QtMsgType type, const char *msg)
int main(int argc, char *argv[])
{
qInstallMsgHandler(myMessageOutput);
QApplication app(argc, argv);
app.addLibraryPath("plugins");
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
// qInstallMsgHandler(myMessageOutput);
QApplication app(argc, argv);
app.addLibraryPath("plugins");
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
MainWindow *ui = new MainWindow;
qDebug("main(): MainWindow constructor finished");
ui->show();
qDebug("main(): ui->show() finished");
MainWindow *ui = new MainWindow;
qDebug("main(): MainWindow constructor finished");
ui->show();
qDebug("main(): ui->show() finished");
return app.exec();
return app.exec();
}

View file

@ -18,9 +18,18 @@ WndDeckEditor::WndDeckEditor(CardDatabase *_db, QWidget *parent)
leftFrame->addWidget(databaseView);
cardInfo = new CardInfoWidget(db);
cardInfo->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
QToolBar *verticalToolBar = new QToolBar;
verticalToolBar->setOrientation(Qt::Vertical);
QHBoxLayout *verticalToolBarLayout = new QHBoxLayout;
verticalToolBarLayout->addStretch();
verticalToolBarLayout->addWidget(verticalToolBar);
verticalToolBarLayout->addStretch();
QVBoxLayout *middleFrame = new QVBoxLayout;
middleFrame->addWidget(cardInfo);
middleFrame->addLayout(verticalToolBarLayout);
middleFrame->addStretch();
deckModel = new DeckListModel(db, this);
@ -47,9 +56,9 @@ WndDeckEditor::WndDeckEditor(CardDatabase *_db, QWidget *parent)
rightFrame->addWidget(deckView);
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addLayout(leftFrame);
mainLayout->addLayout(leftFrame, 10);
mainLayout->addLayout(middleFrame);
mainLayout->addLayout(rightFrame);
mainLayout->addLayout(rightFrame, 10);
QWidget *centralWidget = new QWidget;
centralWidget->setLayout(mainLayout);
@ -73,6 +82,20 @@ WndDeckEditor::WndDeckEditor(CardDatabase *_db, QWidget *parent)
deckMenu->addAction(aLoadDeck);
deckMenu->addAction(aSaveDeck);
deckMenu->addAction(aSaveDeckAs);
aAddCard = new QAction(tr("&Add card"), this);
connect(aAddCard, SIGNAL(triggered()), this, SLOT(actAddCard()));
aRemoveCard = new QAction(tr("&Remove card"), this);
connect(aRemoveCard, SIGNAL(triggered()), this, SLOT(actRemoveCard()));
aIncrement = new QAction(tr("&Increment number"), this);
connect(aIncrement, SIGNAL(triggered()), this, SLOT(actIncrement()));
aDecrement = new QAction(tr("&Decrement number"), this);
connect(aDecrement, SIGNAL(triggered()), this, SLOT(actDecrement()));
verticalToolBar->addAction(aAddCard);
verticalToolBar->addAction(aRemoveCard);
verticalToolBar->addAction(aIncrement);
verticalToolBar->addAction(aDecrement);
}
WndDeckEditor::~WndDeckEditor()
@ -80,15 +103,13 @@ WndDeckEditor::~WndDeckEditor()
}
void WndDeckEditor::updateCardInfoLeft(const QModelIndex &current, const QModelIndex &previous)
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)
void WndDeckEditor::updateCardInfoRight(const QModelIndex &current, const QModelIndex &/*previous*/)
{
Q_UNUSED(previous);
cardInfo->setCard(current.sibling(current.row(), 1).data().toString());
}
@ -104,7 +125,7 @@ void WndDeckEditor::actLoadDeck()
if (l->loadDialog(this)) {
lastFileName = l->getLastFileName();
lastFileFormat = l->getLastFileFormat();
deckView->reset();
// deckView->reset();
nameEdit->setText(l->getName());
commentsEdit->setText(l->getComments());
}
@ -127,3 +148,52 @@ void WndDeckEditor::actSaveDeckAs()
lastFileFormat = l->getLastFileFormat();
}
}
void WndDeckEditor::actAddCard()
{
const QModelIndex currentIndex = databaseView->selectionModel()->currentIndex();
if (!currentIndex.isValid())
return;
const QString cardName = databaseModel->index(currentIndex.row(), 0).data().toString();
QModelIndexList matches = deckModel->match(deckModel->index(0, 1), Qt::EditRole, cardName);
if (matches.isEmpty()) {
int row = deckModel->rowCount();
deckModel->insertRow(row);
deckModel->setData(deckModel->index(row, 1), cardName, Qt::EditRole);
} else {
const QModelIndex numberIndex = deckModel->index(matches[0].row(), 0);
const int count = deckModel->data(numberIndex, Qt::EditRole).toInt();
deckModel->setData(numberIndex, count + 1, Qt::EditRole);
}
}
void WndDeckEditor::actRemoveCard()
{
const QModelIndex currentIndex = deckView->selectionModel()->currentIndex();
if (!currentIndex.isValid())
return;
deckModel->removeRow(currentIndex.row());
}
void WndDeckEditor::actIncrement()
{
const QModelIndex currentIndex = deckView->selectionModel()->currentIndex();
if (!currentIndex.isValid())
return;
const QModelIndex numberIndex = deckModel->index(currentIndex.row(), 0);
const int count = deckModel->data(numberIndex, Qt::EditRole).toInt();
deckModel->setData(numberIndex, count + 1, Qt::EditRole);
}
void WndDeckEditor::actDecrement()
{
const QModelIndex currentIndex = deckView->selectionModel()->currentIndex();
if (!currentIndex.isValid())
return;
const QModelIndex numberIndex = deckModel->index(currentIndex.row(), 0);
const int count = deckModel->data(numberIndex, Qt::EditRole).toInt();
if (count == 1)
deckModel->removeRow(currentIndex.row());
else
deckModel->setData(numberIndex, count - 1, Qt::EditRole);
}

View file

@ -9,6 +9,7 @@ class CardDatabase;
class CardDatabaseModel;
class DeckListModel;
class QTreeView;
class QTableView;
class CardInfoWidget;
class QLineEdit;
@ -22,6 +23,11 @@ private slots:
void actLoadDeck();
void actSaveDeck();
void actSaveDeckAs();
void actAddCard();
void actRemoveCard();
void actIncrement();
void actDecrement();
private:
QString lastFileName;
DeckList::FileFormat lastFileFormat;
@ -29,12 +35,14 @@ private:
CardDatabaseModel *databaseModel;
DeckListModel *deckModel;
QTreeView *databaseView, *deckView;
QTreeView *databaseView;
QTreeView *deckView;
CardInfoWidget *cardInfo;
QLineEdit *nameEdit, *commentsEdit;
QMenu *deckMenu;
QAction *aNewDeck, *aLoadDeck, *aSaveDeck, *aSaveDeckAs;
QAction *aAddCard, *aRemoveCard, *aIncrement, *aDecrement;
public:
WndDeckEditor(CardDatabase *_db, QWidget *parent = 0);
~WndDeckEditor();