Merge pull request #449 from ctrlaltca/set_extinfo

Sets handling improvements
This commit is contained in:
Gavin Bisesi 2014-11-27 06:13:29 -05:00
commit 9e1f8a0892
9 changed files with 156 additions and 49 deletions

View file

@ -16,19 +16,22 @@
#include <QImageReader>
const int CardDatabase::versionNeeded = 3;
const char* CardDatabase::TOKENS_SETNAME = "TK";
static QXmlStreamWriter &operator<<(QXmlStreamWriter &xml, const CardSet *set)
{
xml.writeStartElement("set");
xml.writeTextElement("name", set->getShortName());
xml.writeTextElement("longname", set->getLongName());
xml.writeTextElement("settype", set->getSetType());
xml.writeTextElement("releasedate", set->getReleaseDate().toString(Qt::ISODate));
xml.writeEndElement();
return xml;
}
CardSet::CardSet(const QString &_shortName, const QString &_longName)
: shortName(_shortName), longName(_longName)
CardSet::CardSet(const QString &_shortName, const QString &_longName, const QString &_setType, const QDate &_releaseDate)
: shortName(_shortName), longName(_longName), setType(_setType), releaseDate(_releaseDate)
{
updateSortKey();
}
@ -705,7 +708,8 @@ void CardDatabase::loadSetsFromXml(QXmlStreamReader &xml)
if (xml.readNext() == QXmlStreamReader::EndElement)
break;
if (xml.name() == "set") {
QString shortName, longName;
QString shortName, longName, setType;
QDate releaseDate;
while (!xml.atEnd()) {
if (xml.readNext() == QXmlStreamReader::EndElement)
break;
@ -713,8 +717,12 @@ void CardDatabase::loadSetsFromXml(QXmlStreamReader &xml)
shortName = xml.readElementText();
else if (xml.name() == "longname")
longName = xml.readElementText();
else if (xml.name() == "settype")
setType = xml.readElementText();
else if (xml.name() == "releasedate")
releaseDate = QDate::fromString(xml.readElementText(), Qt::ISODate);
}
sets.insert(shortName, new CardSet(shortName, longName));
sets.insert(shortName, new CardSet(shortName, longName, setType, releaseDate));
}
}
}
@ -786,7 +794,7 @@ CardInfo *CardDatabase::getCardFromMap(CardNameMap &cardMap, const QString &card
return cardMap.value(cardName);
else if (createIfNotFound) {
CardInfo *newCard = new CardInfo(this, cardName, true);
newCard->addToSet(getSet("TK"));
newCard->addToSet(getSet(CardDatabase::TOKENS_SETNAME));
cardMap.insert(cardName, newCard);
return newCard;
} else

View file

@ -4,6 +4,7 @@
#include <QHash>
#include <QPixmap>
#include <QMap>
#include <QDate>
#include <QDataStream>
#include <QList>
#include <QXmlStreamReader>
@ -27,11 +28,15 @@ class CardSet : public QList<CardInfo *> {
private:
QString shortName, longName;
unsigned int sortKey;
QDate releaseDate;
QString setType;
public:
CardSet(const QString &_shortName = QString(), const QString &_longName = QString());
CardSet(const QString &_shortName = QString(), const QString &_longName = QString(), const QString &_setType = QString(), const QDate &_releaseDate = QDate());
QString getCorrectedShortName() const;
QString getShortName() const { return shortName; }
QString getLongName() const { return longName; }
QString getSetType() const { return setType; }
QDate getReleaseDate() const { return releaseDate; }
int getSortKey() const { return sortKey; }
void setSortKey(unsigned int _sortKey);
void updateSortKey();
@ -213,6 +218,8 @@ private:
CardInfo *getCardFromMap(CardNameMap &cardMap, const QString &cardName, bool createIfNotFound);
public:
static const char* TOKENS_SETNAME;
CardDatabase(QObject *parent = 0);
~CardDatabase();
void clear();

View file

@ -146,7 +146,7 @@ void DlgEditTokens::actAddToken()
return;
CardInfo *card = new CardInfo(cardDatabaseModel->getDatabase(), name, true);
card->addToSet(cardDatabaseModel->getDatabase()->getSet("TK"));
card->addToSet(cardDatabaseModel->getDatabase()->getSet(CardDatabase::TOKENS_SETNAME));
card->setCardType("Token");
cardDatabaseModel->getDatabase()->addCard(card);
}

View file

@ -20,13 +20,16 @@ int SetsModel::rowCount(const QModelIndex &parent) const
QVariant SetsModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || (index.column() >= 2) || (index.row() >= rowCount()) || (role != Qt::DisplayRole))
if (!index.isValid() || (index.column() >= NUM_COLS) || (index.row() >= rowCount()) || (role != Qt::DisplayRole))
return QVariant();
CardSet *set = sets[index.row()];
switch (index.column()) {
case 0: return set->getShortName();
case 1: return set->getLongName();
case SortKeyCol: return set->getSortKey();
case SetTypeCol: return set->getSetType();
case ShortNameCol: return set->getShortName();
case LongNameCol: return set->getLongName();
case ReleaseDateCol: return set->getReleaseDate();
default: return QVariant();
}
}
@ -36,8 +39,11 @@ QVariant SetsModel::headerData(int section, Qt::Orientation orientation, int rol
if ((role != Qt::DisplayRole) || (orientation != Qt::Horizontal))
return QVariant();
switch (section) {
case 0: return tr("Short name");
case 1: return tr("Long name");
case SortKeyCol: return tr("Key");
case SetTypeCol: return tr("Set type");
case ShortNameCol: return tr("Short name");
case LongNameCol: return tr("Long name");
case ReleaseDateCol: return tr("Release date");
default: return QVariant();
}
}
@ -84,6 +90,10 @@ bool SetsModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int r
for (int i = 0; i < sets.size(); i++)
sets[i]->setSortKey(i);
sets.sortByKey();
emit dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1));
return true;
}
@ -91,3 +101,24 @@ QStringList SetsModel::mimeTypes() const
{
return QStringList() << "application/x-cockatricecardset";
}
SetsProxyModel::SetsProxyModel(QObject *parent)
: QSortFilterProxyModel(parent)
{
setDynamicSortFilter(true);
}
void SetsProxyModel::saveOrder()
{
int numRows = rowCount();
SetsModel * model = (SetsModel*) sourceModel();
for(int row = 0; row < numRows; ++row) {
QModelIndex idx = index(row, 0);
int oldRow = data(idx, Qt::DisplayRole).toInt();
CardSet *temp = model->sets.at(oldRow);
temp->setSortKey(row);
}
model->sets.sortByKey();
emit model->dataChanged(model->index(0, 0), model->index(model->rowCount() - 1, model->columnCount() - 1));
}

View file

@ -2,9 +2,12 @@
#define SETSMODEL_H
#include <QAbstractTableModel>
#include <QSortFilterProxyModel>
#include <QMimeData>
#include "carddatabase.h"
class SetsProxyModel;
class SetsMimeData : public QMimeData {
Q_OBJECT
private:
@ -17,11 +20,17 @@ public:
class SetsModel : public QAbstractTableModel {
Q_OBJECT
friend class SetsProxyModel;
private:
static const int NUM_COLS = 5;
SetList sets;
public:
enum SetsColumns { SortKeyCol, SetTypeCol, ShortNameCol, LongNameCol, ReleaseDateCol };
SetsModel(CardDatabase *_db, QObject *parent = 0);
~SetsModel();
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &/*parent*/) const { return 2; }
int columnCount(const QModelIndex &parent = QModelIndex()) const { return NUM_COLS; }
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
@ -30,8 +39,12 @@ public:
QMimeData *mimeData(const QModelIndexList &indexes) const;
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent);
QStringList mimeTypes() const;
private:
SetList sets;
};
class SetsProxyModel : public QSortFilterProxyModel {
Q_OBJECT
public:
SetsProxyModel(QObject *parent = 0);
void saveOrder();
};
#endif

View file

@ -2,34 +2,63 @@
#include "setsmodel.h"
#include "main.h"
#include <QTreeView>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QHeaderView>
#include <QPushButton>
WndSets::WndSets(QWidget *parent)
: QMainWindow(parent)
{
model = new SetsModel(db, this);
proxyModel = new SetsProxyModel(this);
proxyModel->setSourceModel(model);
proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
view = new QTreeView;
view->setModel(model);
view->setModel(proxyModel);
view->setAlternatingRowColors(true);
view->setUniformRowHeights(true);
view->setAllColumnsShowFocus(true);
view->setSortingEnabled(true);
view->sortByColumn(SetsModel::SortKeyCol, Qt::AscendingOrder);
view->setDragEnabled(true);
view->setAcceptDrops(true);
view->setDropIndicatorShown(true);
view->setDragDropMode(QAbstractItemView::InternalMove);
#if QT_VERSION < 0x050000
view->header()->setResizeMode(SetsModel::LongNameCol, QHeaderView::ResizeToContents);
#else
view->header()->setSectionResizeMode(SetsModel::LongNameCol, QHeaderView::ResizeToContents);
#endif
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addWidget(view);
saveButton = new QPushButton(tr("Save set ordering"));
connect(saveButton, SIGNAL(clicked()), this, SLOT(actSave()));
restoreButton = new QPushButton(tr("Restore saved set ordering"));
connect(restoreButton, SIGNAL(clicked()), this, SLOT(actRestore()));
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(view, 0, 0, 1, 2);
mainLayout->addWidget(saveButton, 1, 0, 1, 1);
mainLayout->addWidget(restoreButton, 1, 1, 1, 1);
QWidget *centralWidget = new QWidget;
centralWidget->setLayout(mainLayout);
setCentralWidget(centralWidget);
setWindowTitle(tr("Edit sets"));
resize(400, 400);
resize(700, 400);
}
WndSets::~WndSets()
{
}
void WndSets::actSave()
{
proxyModel->saveOrder();
}
void WndSets::actRestore()
{
view->sortByColumn(SetsModel::SortKeyCol, Qt::AscendingOrder);
}

View file

@ -4,17 +4,24 @@
#include <QMainWindow>
class SetsModel;
class SetsProxyModel;
class QTreeView;
class QPushButton;
class CardDatabase;
class WndSets : public QMainWindow {
Q_OBJECT
private:
SetsModel *model;
SetsProxyModel *proxyModel;
QTreeView *view;
QPushButton *saveButton, *restoreButton;
public:
WndSets(QWidget *parent = 0);
~WndSets();
private slots:
void actSave();
void actRestore();
};
#endif

View file

@ -30,6 +30,8 @@ bool OracleImporter::readSetsFromByteArray(const QByteArray &data)
QString edition;
QString editionLong;
QVariant editionCards;
QString setType;
QDate releaseDate;
bool import;
while (it.hasNext()) {
@ -37,12 +39,14 @@ bool OracleImporter::readSetsFromByteArray(const QByteArray &data)
edition = map.value("code").toString();
editionLong = map.value("name").toString();
editionCards = map.value("cards");
setType = map.value("type").toString();
releaseDate = map.value("releaseDate").toDate();
// core and expansion sets are marked to be imported by default
import = (0 == QString::compare(map.value("type").toString(), QString("core"), Qt::CaseInsensitive) ||
0 == QString::compare(map.value("type").toString(), QString("expansion"), Qt::CaseInsensitive));
import = (0 == QString::compare(setType, QString("core"), Qt::CaseInsensitive) ||
0 == QString::compare(setType, QString("expansion"), Qt::CaseInsensitive));
newSetList.append(SetToDownload(edition, editionLong, editionCards, import));
newSetList.append(SetToDownload(edition, editionLong, editionCards, import, setType, releaseDate));
}
qSort(newSetList);
@ -231,13 +235,17 @@ int OracleImporter::startImport()
QListIterator<SetToDownload> it(allSets);
const SetToDownload * curSet;
// add an empty set for tokens
CardSet *tokenSet = new CardSet(TOKENS_SETNAME, tr("Dummy set containing tokens"), "tokens");
sets.insert(TOKENS_SETNAME, tokenSet);
while (it.hasNext())
{
curSet = & it.next();
if(!curSet->getImport())
continue;
CardSet *set = new CardSet(curSet->getShortName(), curSet->getLongName());
CardSet *set = new CardSet(curSet->getShortName(), curSet->getLongName(), curSet->getSetType(), curSet->getReleaseDate());
if (!sets.contains(set->getShortName()))
sets.insert(set->getShortName(), set);

View file

@ -7,38 +7,42 @@
class SetToDownload {
private:
QString shortName, longName;
bool import;
QVariant cards;
QString shortName, longName;
bool import;
QVariant cards;
QDate releaseDate;
QString setType;
public:
const QString &getShortName() const { return shortName; }
const QString &getLongName() const { return longName; }
const QVariant &getCards() const { return cards; }
bool getImport() const { return import; }
void setImport(bool _import) { import = _import; }
SetToDownload(const QString &_shortName, const QString &_longName, const QVariant &_cards, bool _import)
: shortName(_shortName), longName(_longName), import(_import), cards(_cards) { }
bool operator<(const SetToDownload &set) const { return longName.compare(set.longName, Qt::CaseInsensitive) < 0; }
const QString &getShortName() const { return shortName; }
const QString &getLongName() const { return longName; }
const QVariant &getCards() const { return cards; }
const QString &getSetType() const { return setType; }
const QDate &getReleaseDate() const { return releaseDate; }
bool getImport() const { return import; }
void setImport(bool _import) { import = _import; }
SetToDownload(const QString &_shortName, const QString &_longName, const QVariant &_cards, bool _import, const QString &_setType = QString(), const QDate &_releaseDate = QDate())
: shortName(_shortName), longName(_longName), import(_import), cards(_cards), setType(_setType), releaseDate(_releaseDate) { }
bool operator<(const SetToDownload &set) const { return longName.compare(set.longName, Qt::CaseInsensitive) < 0; }
};
class OracleImporter : public CardDatabase {
Q_OBJECT
Q_OBJECT
private:
QList<SetToDownload> allSets;
QVariantMap setsMap;
QString dataDir;
CardInfo *addCard(const QString &setName, QString cardName, bool isToken, int cardId, QString &cardCost, const QString &cardType, const QString &cardPT, int cardLoyalty, const QStringList &cardText);
QList<SetToDownload> allSets;
QVariantMap setsMap;
QString dataDir;
CardInfo *addCard(const QString &setName, QString cardName, bool isToken, int cardId, QString &cardCost, const QString &cardType, const QString &cardPT, int cardLoyalty, const QStringList &cardText);
signals:
void setIndexChanged(int cardsImported, int setIndex, const QString &setName);
void dataReadProgress(int bytesRead, int totalBytes);
void setIndexChanged(int cardsImported, int setIndex, const QString &setName);
void dataReadProgress(int bytesRead, int totalBytes);
public:
OracleImporter(const QString &_dataDir, QObject *parent = 0);
bool readSetsFromByteArray(const QByteArray &data);
int startImport();
int importTextSpoiler(CardSet *set, const QVariant &data);
QList<SetToDownload> &getSets() { return allSets; }
const QString &getDataDir() const { return dataDir; }
OracleImporter(const QString &_dataDir, QObject *parent = 0);
bool readSetsFromByteArray(const QByteArray &data);
int startImport();
int importTextSpoiler(CardSet *set, const QVariant &data);
QList<SetToDownload> &getSets() { return allSets; }
const QString &getDataDir() const { return dataDir; }
};
#endif