From e48a815d258d981e1d9743bbdc37843d3d63fbf7 Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Sun, 30 Nov 2014 21:37:06 +0100 Subject: [PATCH 1/6] Add move buttons, misc improvements --- cockatrice/src/setsmodel.cpp | 55 ++++++++------- cockatrice/src/setsmodel.h | 9 +-- cockatrice/src/window_sets.cpp | 121 +++++++++++++++++++++++++++++---- cockatrice/src/window_sets.h | 15 ++-- 4 files changed, 148 insertions(+), 52 deletions(-) diff --git a/cockatrice/src/setsmodel.cpp b/cockatrice/src/setsmodel.cpp index 206932c4d0..d39e917d55 100644 --- a/cockatrice/src/setsmodel.cpp +++ b/cockatrice/src/setsmodel.cpp @@ -1,4 +1,5 @@ #include "setsmodel.h" +#include SetsModel::SetsModel(CardDatabase *_db, QObject *parent) : QAbstractTableModel(parent), sets(_db->getSetList()) @@ -78,13 +79,22 @@ bool SetsModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int r row = parent.row(); } int oldRow = qobject_cast(data)->getOldRow(); + if (oldRow < row) + row--; + + swapRows(oldRow, row); + + return true; +} + +void SetsModel::swapRows(int oldRow, int newRow) +{ beginRemoveRows(QModelIndex(), oldRow, oldRow); CardSet *temp = sets.takeAt(oldRow); endRemoveRows(); - if (oldRow < row) - row--; - beginInsertRows(QModelIndex(), row, row); - sets.insert(row, temp); + + beginInsertRows(QModelIndex(), newRow, newRow); + sets.insert(newRow, temp); endInsertRows(); for (int i = 0; i < sets.size(); i++) @@ -93,32 +103,27 @@ bool SetsModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int r sets.sortByKey(); emit dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1)); - - return true; } -QStringList SetsModel::mimeTypes() const +void SetsModel::sort(int column, Qt::SortOrder order) { - return QStringList() << "application/x-cockatricecardset"; -} + QMap setMap; + int numRows = rowCount(); + int row; -SetsProxyModel::SetsProxyModel(QObject *parent) - : QSortFilterProxyModel(parent) -{ - setDynamicSortFilter(true); + for(row = 0; row < numRows; ++row) + setMap.insertMulti(index(row, column).data().toString(), sets.at(row)); + + row = (order == Qt::AscendingOrder) ? 0 : numRows - 1; + + foreach(CardSet * set, setMap) + set->setSortKey((order == Qt::AscendingOrder) ? row++ : row--); + + sets.sortByKey(); + emit dataChanged(index(0, 0), index(numRows - 1, columnCount() - 1)); } -void SetsProxyModel::saveOrder() +QStringList SetsModel::mimeTypes() const { - 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)); + return QStringList() << "application/x-cockatricecardset"; } diff --git a/cockatrice/src/setsmodel.h b/cockatrice/src/setsmodel.h index 69f6934352..9ebdaccbb0 100644 --- a/cockatrice/src/setsmodel.h +++ b/cockatrice/src/setsmodel.h @@ -2,7 +2,6 @@ #define SETSMODEL_H #include -#include #include #include "carddatabase.h" @@ -39,12 +38,8 @@ class SetsModel : public QAbstractTableModel { QMimeData *mimeData(const QModelIndexList &indexes) const; bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent); QStringList mimeTypes() const; + void swapRows(int oldRow, int newRow); + void sort(int column, Qt::SortOrder order = Qt::AscendingOrder); }; -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 d37d370a29..c73e75e2f2 100644 --- a/cockatrice/src/window_sets.cpp +++ b/cockatrice/src/window_sets.cpp @@ -5,41 +5,62 @@ #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(proxyModel); + view->setModel(model); + view->sortByColumn(SetsModel::SortKeyCol, Qt::AscendingOrder); + view->setColumnHidden(SetsModel::SortKeyCol, true); + view->setAlternatingRowColors(true); view->setUniformRowHeights(true); view->setAllColumnsShowFocus(true); view->setSortingEnabled(true); - view->sortByColumn(SetsModel::SortKeyCol, Qt::AscendingOrder); + view->setSelectionMode(QAbstractItemView::SingleSelection); + view->setSelectionBehavior(QAbstractItemView::SelectRows); view->setDragEnabled(true); view->setAcceptDrops(true); view->setDropIndicatorShown(true); view->setDragDropMode(QAbstractItemView::InternalMove); + #if QT_VERSION < 0x050000 + view->header()->setResizeMode(QHeaderView::Stretch); view->header()->setResizeMode(SetsModel::LongNameCol, QHeaderView::ResizeToContents); #else + view->header()->setSectionResizeMode(QHeaderView::Stretch); view->header()->setSectionResizeMode(SetsModel::LongNameCol, QHeaderView::ResizeToContents); #endif - 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())); + upButton = new QPushButton(tr("Move selected set up")); + connect(upButton, SIGNAL(clicked()), this, SLOT(actUp())); + downButton = new QPushButton(tr("Move selected set down")); + connect(downButton, SIGNAL(clicked()), this, SLOT(actDown())); + topButton = new QPushButton(tr("Move selected set to top")); + connect(topButton, SIGNAL(clicked()), this, SLOT(actTop())); + bottomButton = new QPushButton(tr("Move selected set to bottom")); + connect(bottomButton, SIGNAL(clicked()), this, SLOT(actBottom())); + + upButton->setDisabled(true); + downButton->setDisabled(true); + topButton->setDisabled(true); + bottomButton->setDisabled(true); + + connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), + this, SLOT(actToggleButtons(const QItemSelection &, const QItemSelection &))); 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); + + mainLayout->addWidget(upButton, 1, 0, 1, 1); + mainLayout->addWidget(downButton, 2, 0, 1, 1); + + mainLayout->addWidget(topButton, 1, 1, 1, 1); + mainLayout->addWidget(bottomButton, 2, 1, 1, 1); QWidget *centralWidget = new QWidget; centralWidget->setLayout(mainLayout); @@ -53,12 +74,82 @@ WndSets::~WndSets() { } -void WndSets::actSave() +void WndSets::actToggleButtons(const QItemSelection & selected, const QItemSelection &) { - proxyModel->saveOrder(); + bool disabled = selected.empty(); + upButton->setDisabled(disabled); + downButton->setDisabled(disabled); + topButton->setDisabled(disabled); + bottomButton->setDisabled(disabled); } -void WndSets::actRestore() +void WndSets::selectRow(int row) { - view->sortByColumn(SetsModel::SortKeyCol, Qt::AscendingOrder); + QModelIndex idx = model->index(row, 0); + view->selectionModel()->select(idx, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows); + view->scrollTo(idx, QAbstractItemView::EnsureVisible); +} + +void WndSets::actUp() +{ + QModelIndexList rows = view->selectionModel()->selectedRows(); + if(rows.empty()) + return; + + QModelIndex selectedRow = rows.first(); + int oldRow = selectedRow.row(); + int newRow = oldRow - 1; + if(oldRow <= 0) + return; + + model->swapRows(oldRow, newRow); + selectRow(newRow); +} + +void WndSets::actDown() +{ + QModelIndexList rows = view->selectionModel()->selectedRows(); + if(rows.empty()) + return; + + QModelIndex selectedRow = rows.first(); + int oldRow = selectedRow.row(); + int newRow = oldRow + 1; + if(oldRow >= model->rowCount() - 1) + return; + + model->swapRows(oldRow, newRow); + selectRow(newRow); +} + +void WndSets::actTop() +{ + QModelIndexList rows = view->selectionModel()->selectedRows(); + if(rows.empty()) + return; + + QModelIndex selectedRow = rows.first(); + int oldRow = selectedRow.row(); + int newRow = 0; + if(oldRow <= 0) + return; + + model->swapRows(oldRow, newRow); + selectRow(newRow); +} + +void WndSets::actBottom() +{ + QModelIndexList rows = view->selectionModel()->selectedRows(); + if(rows.empty()) + return; + + QModelIndex selectedRow = rows.first(); + int oldRow = selectedRow.row(); + int newRow = model->rowCount() - 1; + if(oldRow >= newRow) + return; + + model->swapRows(oldRow, newRow); + selectRow(newRow); } diff --git a/cockatrice/src/window_sets.h b/cockatrice/src/window_sets.h index 4657051914..d9d96fae8b 100644 --- a/cockatrice/src/window_sets.h +++ b/cockatrice/src/window_sets.h @@ -5,23 +5,28 @@ class SetsModel; class SetsProxyModel; -class QTreeView; class QPushButton; class CardDatabase; +class QItemSelection; +class QTreeView; class WndSets : public QMainWindow { Q_OBJECT private: SetsModel *model; - SetsProxyModel *proxyModel; QTreeView *view; - QPushButton *saveButton, *restoreButton; + QPushButton *upButton, *downButton, *bottomButton, *topButton; public: WndSets(QWidget *parent = 0); ~WndSets(); +protected: + void selectRow(int row); private slots: - void actSave(); - void actRestore(); + void actUp(); + void actDown(); + void actTop(); + void actBottom(); + void actToggleButtons(const QItemSelection & selected, const QItemSelection & deselected); }; #endif From 36ed4480c2de2629ba8f724a25a37780dd9dcfa8 Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Tue, 2 Dec 2014 14:38:36 +0100 Subject: [PATCH 2/6] Re-added save and restore buttons --- cockatrice/src/setsmodel.cpp | 41 +++++++++++++++++++++++++--------- cockatrice/src/setsmodel.h | 2 ++ cockatrice/src/window_sets.cpp | 22 ++++++++++++++++-- cockatrice/src/window_sets.h | 4 +++- 4 files changed, 55 insertions(+), 14 deletions(-) diff --git a/cockatrice/src/setsmodel.cpp b/cockatrice/src/setsmodel.cpp index d39e917d55..85fbe85b23 100644 --- a/cockatrice/src/setsmodel.cpp +++ b/cockatrice/src/setsmodel.cpp @@ -97,30 +97,49 @@ void SetsModel::swapRows(int oldRow, int newRow) sets.insert(newRow, temp); endInsertRows(); - for (int i = 0; i < sets.size(); i++) - sets[i]->setSortKey(i); - - sets.sortByKey(); - emit dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1)); } void SetsModel::sort(int column, Qt::SortOrder order) { - QMap setMap; + QMap setMap; int numRows = rowCount(); int row; for(row = 0; row < numRows; ++row) - setMap.insertMulti(index(row, column).data().toString(), sets.at(row)); + setMap.insertMulti(index(row, column).data(), sets.at(row)); + + + QList tmp = setMap.values(); + sets.clear(); + if(order == Qt::AscendingOrder) + { + for(row = 0; row < tmp.size(); row++) { + sets.append(tmp.at(row)); + } + } else { + for(row = tmp.size() - 1; row >= 0; row--) { + sets.append(tmp.at(row)); + } + } + + emit dataChanged(index(0, 0), index(numRows - 1, columnCount() - 1)); +} - row = (order == Qt::AscendingOrder) ? 0 : numRows - 1; +void SetsModel::save() +{ + for (int i = 0; i < sets.size(); i++) + sets[i]->setSortKey(i); - foreach(CardSet * set, setMap) - set->setSortKey((order == Qt::AscendingOrder) ? row++ : row--); + sets.sortByKey(); +} +void SetsModel::restore(CardDatabase *db) +{ + sets = db->getSetList(); sets.sortByKey(); - emit dataChanged(index(0, 0), index(numRows - 1, columnCount() - 1)); + + emit dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1)); } QStringList SetsModel::mimeTypes() const diff --git a/cockatrice/src/setsmodel.h b/cockatrice/src/setsmodel.h index 9ebdaccbb0..88d3497c6a 100644 --- a/cockatrice/src/setsmodel.h +++ b/cockatrice/src/setsmodel.h @@ -40,6 +40,8 @@ class SetsModel : public QAbstractTableModel { QStringList mimeTypes() const; void swapRows(int oldRow, int newRow); void sort(int column, Qt::SortOrder order = Qt::AscendingOrder); + void save(); + void restore(CardDatabase *db); }; #endif diff --git a/cockatrice/src/window_sets.cpp b/cockatrice/src/window_sets.cpp index c73e75e2f2..15e1f0a385 100644 --- a/cockatrice/src/window_sets.cpp +++ b/cockatrice/src/window_sets.cpp @@ -13,8 +13,6 @@ WndSets::WndSets(QWidget *parent) model = new SetsModel(db, this); view = new QTreeView; view->setModel(model); - view->sortByColumn(SetsModel::SortKeyCol, Qt::AscendingOrder); - view->setColumnHidden(SetsModel::SortKeyCol, true); view->setAlternatingRowColors(true); view->setUniformRowHeights(true); @@ -36,6 +34,13 @@ WndSets::WndSets(QWidget *parent) view->header()->setSectionResizeMode(SetsModel::LongNameCol, QHeaderView::ResizeToContents); #endif + view->sortByColumn(SetsModel::SortKeyCol, Qt::AscendingOrder); + view->setColumnHidden(SetsModel::SortKeyCol, true); + + 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())); upButton = new QPushButton(tr("Move selected set up")); connect(upButton, SIGNAL(clicked()), this, SLOT(actUp())); downButton = new QPushButton(tr("Move selected set down")); @@ -62,6 +67,9 @@ WndSets::WndSets(QWidget *parent) mainLayout->addWidget(topButton, 1, 1, 1, 1); mainLayout->addWidget(bottomButton, 2, 1, 1, 1); + mainLayout->addWidget(saveButton, 3, 0, 1, 1); + mainLayout->addWidget(restoreButton, 3, 1, 1, 1); + QWidget *centralWidget = new QWidget; centralWidget->setLayout(mainLayout); setCentralWidget(centralWidget); @@ -74,6 +82,16 @@ WndSets::~WndSets() { } +void WndSets::actSave() +{ + model->save(); +} + +void WndSets::actRestore() +{ + model->restore(db); +} + void WndSets::actToggleButtons(const QItemSelection & selected, const QItemSelection &) { bool disabled = selected.empty(); diff --git a/cockatrice/src/window_sets.h b/cockatrice/src/window_sets.h index d9d96fae8b..d2c528232b 100644 --- a/cockatrice/src/window_sets.h +++ b/cockatrice/src/window_sets.h @@ -15,13 +15,15 @@ class WndSets : public QMainWindow { private: SetsModel *model; QTreeView *view; - QPushButton *upButton, *downButton, *bottomButton, *topButton; + QPushButton *saveButton, *restoreButton, *upButton, *downButton, *bottomButton, *topButton; public: WndSets(QWidget *parent = 0); ~WndSets(); protected: void selectRow(int row); private slots: + void actSave(); + void actRestore(); void actUp(); void actDown(); void actTop(); From 2745cb2c627877801bbc801f470e703b231c9d95 Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Tue, 2 Dec 2014 14:43:39 +0100 Subject: [PATCH 3/6] Removed debug --- cockatrice/src/setsmodel.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/cockatrice/src/setsmodel.cpp b/cockatrice/src/setsmodel.cpp index 85fbe85b23..5798404023 100644 --- a/cockatrice/src/setsmodel.cpp +++ b/cockatrice/src/setsmodel.cpp @@ -1,5 +1,4 @@ #include "setsmodel.h" -#include SetsModel::SetsModel(CardDatabase *_db, QObject *parent) : QAbstractTableModel(parent), sets(_db->getSetList()) From 00a5ed0b79c435004f55d67a87ba752df7190690 Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Thu, 4 Dec 2014 18:29:06 +0100 Subject: [PATCH 4/6] Fix compilation with qt <= 5.2 --- cockatrice/src/setsmodel.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cockatrice/src/setsmodel.cpp b/cockatrice/src/setsmodel.cpp index 5798404023..26542a6e45 100644 --- a/cockatrice/src/setsmodel.cpp +++ b/cockatrice/src/setsmodel.cpp @@ -101,13 +101,12 @@ void SetsModel::swapRows(int oldRow, int newRow) void SetsModel::sort(int column, Qt::SortOrder order) { - QMap setMap; + QMap setMap; int numRows = rowCount(); int row; for(row = 0; row < numRows; ++row) - setMap.insertMulti(index(row, column).data(), sets.at(row)); - + setMap.insertMulti(index(row, column).data().toString(), sets.at(row)); QList tmp = setMap.values(); sets.clear(); From 0ba351c9557df442280c414d192c4fcb700baf08 Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Thu, 4 Dec 2014 21:09:50 +0100 Subject: [PATCH 5/6] Add "saved" msgbox --- cockatrice/src/window_sets.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cockatrice/src/window_sets.cpp b/cockatrice/src/window_sets.cpp index 15e1f0a385..5fef67a63d 100644 --- a/cockatrice/src/window_sets.cpp +++ b/cockatrice/src/window_sets.cpp @@ -6,6 +6,7 @@ #include #include #include +#include WndSets::WndSets(QWidget *parent) : QMainWindow(parent) @@ -85,6 +86,7 @@ WndSets::~WndSets() void WndSets::actSave() { model->save(); + QMessageBox::information(this, tr("Success"), tr("The sets database has been saved successfully.")); } void WndSets::actRestore() From 9cc8d8b86c3da077b4737dbf06e35b2d1774b4bf Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Fri, 5 Dec 2014 08:48:33 +0100 Subject: [PATCH 6/6] Misc fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ucfirst(setCode): requires oracle re-run reorder columns rename the “short name“ column as “set code“ ensure proper casting for releaseDate and sortKey fields (refs 00a5ed0) --- cockatrice/src/setsmodel.cpp | 6 +++--- cockatrice/src/setsmodel.h | 2 +- oracle/src/oracleimporter.cpp | 5 ++++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/cockatrice/src/setsmodel.cpp b/cockatrice/src/setsmodel.cpp index 26542a6e45..bd6dc00693 100644 --- a/cockatrice/src/setsmodel.cpp +++ b/cockatrice/src/setsmodel.cpp @@ -25,11 +25,11 @@ QVariant SetsModel::data(const QModelIndex &index, int role) const CardSet *set = sets[index.row()]; switch (index.column()) { - case SortKeyCol: return set->getSortKey(); + case SortKeyCol: return QString("%1").arg(set->getSortKey(), 4, 10, QChar('0')); case SetTypeCol: return set->getSetType(); case ShortNameCol: return set->getShortName(); case LongNameCol: return set->getLongName(); - case ReleaseDateCol: return set->getReleaseDate(); + case ReleaseDateCol: return set->getReleaseDate().toString(Qt::ISODate); default: return QVariant(); } } @@ -41,7 +41,7 @@ QVariant SetsModel::headerData(int section, Qt::Orientation orientation, int rol switch (section) { case SortKeyCol: return tr("Key"); case SetTypeCol: return tr("Set type"); - case ShortNameCol: return tr("Short name"); + case ShortNameCol: return tr("Set code"); case LongNameCol: return tr("Long name"); case ReleaseDateCol: return tr("Release date"); default: return QVariant(); diff --git a/cockatrice/src/setsmodel.h b/cockatrice/src/setsmodel.h index 88d3497c6a..17911db183 100644 --- a/cockatrice/src/setsmodel.h +++ b/cockatrice/src/setsmodel.h @@ -24,7 +24,7 @@ class SetsModel : public QAbstractTableModel { static const int NUM_COLS = 5; SetList sets; public: - enum SetsColumns { SortKeyCol, SetTypeCol, ShortNameCol, LongNameCol, ReleaseDateCol }; + enum SetsColumns { SortKeyCol, LongNameCol, ShortNameCol, SetTypeCol, ReleaseDateCol }; SetsModel(CardDatabase *_db, QObject *parent = 0); ~SetsModel(); diff --git a/oracle/src/oracleimporter.cpp b/oracle/src/oracleimporter.cpp index 273203c7cd..54ff9958b2 100644 --- a/oracle/src/oracleimporter.cpp +++ b/oracle/src/oracleimporter.cpp @@ -40,6 +40,9 @@ bool OracleImporter::readSetsFromByteArray(const QByteArray &data) editionLong = map.value("name").toString(); editionCards = map.value("cards"); setType = map.value("type").toString(); + // capitalize set type + if(setType.length() > 0) + setType[0] = setType[0].toUpper(); releaseDate = map.value("releaseDate").toDate(); // core and expansion sets are marked to be imported by default @@ -236,7 +239,7 @@ int OracleImporter::startImport() const SetToDownload * curSet; // add an empty set for tokens - CardSet *tokenSet = new CardSet(TOKENS_SETNAME, tr("Dummy set containing tokens"), "tokens"); + CardSet *tokenSet = new CardSet(TOKENS_SETNAME, tr("Dummy set containing tokens"), "Tokens"); sets.insert(TOKENS_SETNAME, tokenSet); while (it.hasNext())