diff --git a/cockatrice/src/setsmodel.cpp b/cockatrice/src/setsmodel.cpp index d39e917d..85fbe85b 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)); - row = (order == Qt::AscendingOrder) ? 0 : numRows - 1; + + 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)); + } + } - foreach(CardSet * set, setMap) - set->setSortKey((order == Qt::AscendingOrder) ? row++ : row--); + emit dataChanged(index(0, 0), index(numRows - 1, columnCount() - 1)); +} + +void SetsModel::save() +{ + for (int i = 0; i < sets.size(); i++) + sets[i]->setSortKey(i); sets.sortByKey(); - emit dataChanged(index(0, 0), index(numRows - 1, columnCount() - 1)); +} + +void SetsModel::restore(CardDatabase *db) +{ + sets = db->getSetList(); + sets.sortByKey(); + + 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 9ebdaccb..88d3497c 100644 --- a/cockatrice/src/setsmodel.h +++ b/cockatrice/src/setsmodel.h @@ -40,6 +40,8 @@ public: 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 c73e75e2..15e1f0a3 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 d9d96fae..d2c52823 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();