diff --git a/cockatrice/src/setsmodel.cpp b/cockatrice/src/setsmodel.cpp index f11d73c4..ca1be306 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(); } } @@ -91,3 +97,10 @@ QStringList SetsModel::mimeTypes() const { return QStringList() << "application/x-cockatricecardset"; } + +SetsProxyModel::SetsProxyModel(QObject *parent) + : QSortFilterProxyModel(parent) +{ + setDynamicSortFilter(true); +} + diff --git a/cockatrice/src/setsmodel.h b/cockatrice/src/setsmodel.h index 74d29443..eaf7d89b 100644 --- a/cockatrice/src/setsmodel.h +++ b/cockatrice/src/setsmodel.h @@ -2,6 +2,7 @@ #define SETSMODEL_H #include +#include #include #include "carddatabase.h" @@ -17,11 +18,16 @@ public: class SetsModel : public QAbstractTableModel { Q_OBJECT +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 +36,11 @@ 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); +}; #endif diff --git a/cockatrice/src/window_sets.cpp b/cockatrice/src/window_sets.cpp index 668b36e0..d56f3487 100644 --- a/cockatrice/src/window_sets.cpp +++ b/cockatrice/src/window_sets.cpp @@ -3,21 +3,32 @@ #include "main.h" #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); @@ -27,7 +38,7 @@ WndSets::WndSets(QWidget *parent) setCentralWidget(centralWidget); setWindowTitle(tr("Edit sets")); - resize(400, 400); + resize(700, 400); } WndSets::~WndSets() diff --git a/cockatrice/src/window_sets.h b/cockatrice/src/window_sets.h index ce7cb622..45317dd6 100644 --- a/cockatrice/src/window_sets.h +++ b/cockatrice/src/window_sets.h @@ -4,6 +4,7 @@ #include class SetsModel; +class SetsProxyModel; class QTreeView; class CardDatabase; @@ -11,6 +12,7 @@ class WndSets : public QMainWindow { Q_OBJECT private: SetsModel *model; + SetsProxyModel *proxyModel; QTreeView *view; public: WndSets(QWidget *parent = 0);