diff --git a/cockatrice/src/carddatabase.cpp b/cockatrice/src/carddatabase.cpp index 5336b5b3..2d311900 100644 --- a/cockatrice/src/carddatabase.cpp +++ b/cockatrice/src/carddatabase.cpp @@ -16,19 +16,22 @@ #include 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 diff --git a/cockatrice/src/carddatabase.h b/cockatrice/src/carddatabase.h index af5a16f1..e9b693f8 100644 --- a/cockatrice/src/carddatabase.h +++ b/cockatrice/src/carddatabase.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -27,11 +28,15 @@ class CardSet : public QList { 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(); diff --git a/cockatrice/src/dlg_edit_tokens.cpp b/cockatrice/src/dlg_edit_tokens.cpp index e2de2cd4..2d272a6d 100644 --- a/cockatrice/src/dlg_edit_tokens.cpp +++ b/cockatrice/src/dlg_edit_tokens.cpp @@ -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); } diff --git a/cockatrice/src/setsmodel.cpp b/cockatrice/src/setsmodel.cpp index f11d73c4..206932c4 100644 --- a/cockatrice/src/setsmodel.cpp +++ b/cockatrice/src/setsmodel.cpp @@ -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)); +} diff --git a/cockatrice/src/setsmodel.h b/cockatrice/src/setsmodel.h index 74d29443..69f69343 100644 --- a/cockatrice/src/setsmodel.h +++ b/cockatrice/src/setsmodel.h @@ -2,9 +2,12 @@ #define SETSMODEL_H #include +#include #include #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 diff --git a/cockatrice/src/window_sets.cpp b/cockatrice/src/window_sets.cpp index 668b36e0..d37d370a 100644 --- a/cockatrice/src/window_sets.cpp +++ b/cockatrice/src/window_sets.cpp @@ -2,34 +2,63 @@ #include "setsmodel.h" #include "main.h" #include -#include +#include +#include +#include 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); +} diff --git a/cockatrice/src/window_sets.h b/cockatrice/src/window_sets.h index ce7cb622..46570519 100644 --- a/cockatrice/src/window_sets.h +++ b/cockatrice/src/window_sets.h @@ -4,17 +4,24 @@ #include 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 diff --git a/oracle/src/oracleimporter.cpp b/oracle/src/oracleimporter.cpp index 48172c48..273203c7 100644 --- a/oracle/src/oracleimporter.cpp +++ b/oracle/src/oracleimporter.cpp @@ -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 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); diff --git a/oracle/src/oracleimporter.h b/oracle/src/oracleimporter.h index 4c36a211..e8fe9e9d 100644 --- a/oracle/src/oracleimporter.h +++ b/oracle/src/oracleimporter.h @@ -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 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 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 &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 &getSets() { return allSets; } + const QString &getDataDir() const { return dataDir; } }; #endif