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

View file

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

View file

@ -146,7 +146,7 @@ void DlgEditTokens::actAddToken()
return; return;
CardInfo *card = new CardInfo(cardDatabaseModel->getDatabase(), name, true); 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"); card->setCardType("Token");
cardDatabaseModel->getDatabase()->addCard(card); 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 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(); return QVariant();
CardSet *set = sets[index.row()]; CardSet *set = sets[index.row()];
switch (index.column()) { switch (index.column()) {
case 0: return set->getShortName(); case SortKeyCol: return set->getSortKey();
case 1: return set->getLongName(); case SetTypeCol: return set->getSetType();
case ShortNameCol: return set->getShortName();
case LongNameCol: return set->getLongName();
case ReleaseDateCol: return set->getReleaseDate();
default: return QVariant(); default: return QVariant();
} }
} }
@ -36,8 +39,11 @@ QVariant SetsModel::headerData(int section, Qt::Orientation orientation, int rol
if ((role != Qt::DisplayRole) || (orientation != Qt::Horizontal)) if ((role != Qt::DisplayRole) || (orientation != Qt::Horizontal))
return QVariant(); return QVariant();
switch (section) { switch (section) {
case 0: return tr("Short name"); case SortKeyCol: return tr("Key");
case 1: return tr("Long name"); 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(); 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++) for (int i = 0; i < sets.size(); i++)
sets[i]->setSortKey(i); sets[i]->setSortKey(i);
sets.sortByKey();
emit dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1));
return true; return true;
} }
@ -91,3 +101,24 @@ QStringList SetsModel::mimeTypes() const
{ {
return QStringList() << "application/x-cockatricecardset"; 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 #define SETSMODEL_H
#include <QAbstractTableModel> #include <QAbstractTableModel>
#include <QSortFilterProxyModel>
#include <QMimeData> #include <QMimeData>
#include "carddatabase.h" #include "carddatabase.h"
class SetsProxyModel;
class SetsMimeData : public QMimeData { class SetsMimeData : public QMimeData {
Q_OBJECT Q_OBJECT
private: private:
@ -17,11 +20,17 @@ public:
class SetsModel : public QAbstractTableModel { class SetsModel : public QAbstractTableModel {
Q_OBJECT Q_OBJECT
friend class SetsProxyModel;
private:
static const int NUM_COLS = 5;
SetList sets;
public: public:
enum SetsColumns { SortKeyCol, SetTypeCol, ShortNameCol, LongNameCol, ReleaseDateCol };
SetsModel(CardDatabase *_db, QObject *parent = 0); SetsModel(CardDatabase *_db, QObject *parent = 0);
~SetsModel(); ~SetsModel();
int rowCount(const QModelIndex &parent = QModelIndex()) const; 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 data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
Qt::ItemFlags flags(const QModelIndex &index) const; Qt::ItemFlags flags(const QModelIndex &index) const;
@ -30,8 +39,12 @@ public:
QMimeData *mimeData(const QModelIndexList &indexes) const; QMimeData *mimeData(const QModelIndexList &indexes) const;
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent); bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent);
QStringList mimeTypes() const; QStringList mimeTypes() const;
private:
SetList sets;
}; };
class SetsProxyModel : public QSortFilterProxyModel {
Q_OBJECT
public:
SetsProxyModel(QObject *parent = 0);
void saveOrder();
};
#endif #endif

View file

@ -2,34 +2,63 @@
#include "setsmodel.h" #include "setsmodel.h"
#include "main.h" #include "main.h"
#include <QTreeView> #include <QTreeView>
#include <QHBoxLayout> #include <QGridLayout>
#include <QHeaderView>
#include <QPushButton>
WndSets::WndSets(QWidget *parent) WndSets::WndSets(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
{ {
model = new SetsModel(db, this); model = new SetsModel(db, this);
proxyModel = new SetsProxyModel(this);
proxyModel->setSourceModel(model);
proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
view = new QTreeView; view = new QTreeView;
view->setModel(model); view->setModel(proxyModel);
view->setAlternatingRowColors(true); view->setAlternatingRowColors(true);
view->setUniformRowHeights(true); view->setUniformRowHeights(true);
view->setAllColumnsShowFocus(true); view->setAllColumnsShowFocus(true);
view->setSortingEnabled(true);
view->sortByColumn(SetsModel::SortKeyCol, Qt::AscendingOrder);
view->setDragEnabled(true); view->setDragEnabled(true);
view->setAcceptDrops(true); view->setAcceptDrops(true);
view->setDropIndicatorShown(true); view->setDropIndicatorShown(true);
view->setDragDropMode(QAbstractItemView::InternalMove); 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; saveButton = new QPushButton(tr("Save set ordering"));
mainLayout->addWidget(view); 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; QWidget *centralWidget = new QWidget;
centralWidget->setLayout(mainLayout); centralWidget->setLayout(mainLayout);
setCentralWidget(centralWidget); setCentralWidget(centralWidget);
setWindowTitle(tr("Edit sets")); setWindowTitle(tr("Edit sets"));
resize(400, 400); resize(700, 400);
} }
WndSets::~WndSets() WndSets::~WndSets()
{ {
} }
void WndSets::actSave()
{
proxyModel->saveOrder();
}
void WndSets::actRestore()
{
view->sortByColumn(SetsModel::SortKeyCol, Qt::AscendingOrder);
}

View file

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

View file

@ -30,6 +30,8 @@ bool OracleImporter::readSetsFromByteArray(const QByteArray &data)
QString edition; QString edition;
QString editionLong; QString editionLong;
QVariant editionCards; QVariant editionCards;
QString setType;
QDate releaseDate;
bool import; bool import;
while (it.hasNext()) { while (it.hasNext()) {
@ -37,12 +39,14 @@ bool OracleImporter::readSetsFromByteArray(const QByteArray &data)
edition = map.value("code").toString(); edition = map.value("code").toString();
editionLong = map.value("name").toString(); editionLong = map.value("name").toString();
editionCards = map.value("cards"); 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 // core and expansion sets are marked to be imported by default
import = (0 == QString::compare(map.value("type").toString(), QString("core"), Qt::CaseInsensitive) || import = (0 == QString::compare(setType, QString("core"), Qt::CaseInsensitive) ||
0 == QString::compare(map.value("type").toString(), QString("expansion"), 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); qSort(newSetList);
@ -231,13 +235,17 @@ int OracleImporter::startImport()
QListIterator<SetToDownload> it(allSets); QListIterator<SetToDownload> it(allSets);
const SetToDownload * curSet; 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()) while (it.hasNext())
{ {
curSet = & it.next(); curSet = & it.next();
if(!curSet->getImport()) if(!curSet->getImport())
continue; 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())) if (!sets.contains(set->getShortName()))
sets.insert(set->getShortName(), set); sets.insert(set->getShortName(), set);

View file

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