Sets dialog: new columns and sorting

Added "order key", "set type" and "release date" columns
Use a proxy model to sort the table
made the dialog wider accordingly
This commit is contained in:
Fabio Bas 2014-11-19 18:09:37 +01:00
parent cb37073828
commit 8542d875d3
4 changed files with 45 additions and 10 deletions

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();
} }
} }
@ -91,3 +97,10 @@ QStringList SetsModel::mimeTypes() const
{ {
return QStringList() << "application/x-cockatricecardset"; return QStringList() << "application/x-cockatricecardset";
} }
SetsProxyModel::SetsProxyModel(QObject *parent)
: QSortFilterProxyModel(parent)
{
setDynamicSortFilter(true);
}

View file

@ -2,6 +2,7 @@
#define SETSMODEL_H #define SETSMODEL_H
#include <QAbstractTableModel> #include <QAbstractTableModel>
#include <QSortFilterProxyModel>
#include <QMimeData> #include <QMimeData>
#include "carddatabase.h" #include "carddatabase.h"
@ -17,11 +18,16 @@ public:
class SetsModel : public QAbstractTableModel { class SetsModel : public QAbstractTableModel {
Q_OBJECT Q_OBJECT
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 +36,11 @@ 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);
};
#endif #endif

View file

@ -3,21 +3,32 @@
#include "main.h" #include "main.h"
#include <QTreeView> #include <QTreeView>
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QHeaderView>
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; QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addWidget(view); mainLayout->addWidget(view);
@ -27,7 +38,7 @@ WndSets::WndSets(QWidget *parent)
setCentralWidget(centralWidget); setCentralWidget(centralWidget);
setWindowTitle(tr("Edit sets")); setWindowTitle(tr("Edit sets"));
resize(400, 400); resize(700, 400);
} }
WndSets::~WndSets() WndSets::~WndSets()

View file

@ -4,6 +4,7 @@
#include <QMainWindow> #include <QMainWindow>
class SetsModel; class SetsModel;
class SetsProxyModel;
class QTreeView; class QTreeView;
class CardDatabase; class CardDatabase;
@ -11,6 +12,7 @@ class WndSets : public QMainWindow {
Q_OBJECT Q_OBJECT
private: private:
SetsModel *model; SetsModel *model;
SetsProxyModel *proxyModel;
QTreeView *view; QTreeView *view;
public: public:
WndSets(QWidget *parent = 0); WndSets(QWidget *parent = 0);