Add move buttons, misc improvements

This commit is contained in:
Fabio Bas 2014-11-30 21:37:06 +01:00
parent 9e1f8a0892
commit e48a815d25
4 changed files with 149 additions and 53 deletions

View file

@ -1,4 +1,5 @@
#include "setsmodel.h" #include "setsmodel.h"
#include <QDebug>
SetsModel::SetsModel(CardDatabase *_db, QObject *parent) SetsModel::SetsModel(CardDatabase *_db, QObject *parent)
: QAbstractTableModel(parent), sets(_db->getSetList()) : QAbstractTableModel(parent), sets(_db->getSetList())
@ -78,13 +79,22 @@ bool SetsModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int r
row = parent.row(); row = parent.row();
} }
int oldRow = qobject_cast<const SetsMimeData *>(data)->getOldRow(); int oldRow = qobject_cast<const SetsMimeData *>(data)->getOldRow();
if (oldRow < row)
row--;
swapRows(oldRow, row);
return true;
}
void SetsModel::swapRows(int oldRow, int newRow)
{
beginRemoveRows(QModelIndex(), oldRow, oldRow); beginRemoveRows(QModelIndex(), oldRow, oldRow);
CardSet *temp = sets.takeAt(oldRow); CardSet *temp = sets.takeAt(oldRow);
endRemoveRows(); endRemoveRows();
if (oldRow < row)
row--; beginInsertRows(QModelIndex(), newRow, newRow);
beginInsertRows(QModelIndex(), row, row); sets.insert(newRow, temp);
sets.insert(row, temp);
endInsertRows(); endInsertRows();
for (int i = 0; i < sets.size(); i++) 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(); sets.sortByKey();
emit dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1)); emit dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1));
}
return true; void SetsModel::sort(int column, Qt::SortOrder order)
{
QMap<QString, CardSet *> setMap;
int numRows = rowCount();
int row;
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));
} }
QStringList SetsModel::mimeTypes() const QStringList SetsModel::mimeTypes() const
{ {
return QStringList() << "application/x-cockatricecardset"; 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));
}

View file

@ -2,7 +2,6 @@
#define SETSMODEL_H #define SETSMODEL_H
#include <QAbstractTableModel> #include <QAbstractTableModel>
#include <QSortFilterProxyModel>
#include <QMimeData> #include <QMimeData>
#include "carddatabase.h" #include "carddatabase.h"
@ -39,12 +38,8 @@ 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;
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 #endif

View file

@ -5,41 +5,62 @@
#include <QGridLayout> #include <QGridLayout>
#include <QHeaderView> #include <QHeaderView>
#include <QPushButton> #include <QPushButton>
#include <QItemSelection>
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(proxyModel); view->setModel(model);
view->sortByColumn(SetsModel::SortKeyCol, Qt::AscendingOrder);
view->setColumnHidden(SetsModel::SortKeyCol, true);
view->setAlternatingRowColors(true); view->setAlternatingRowColors(true);
view->setUniformRowHeights(true); view->setUniformRowHeights(true);
view->setAllColumnsShowFocus(true); view->setAllColumnsShowFocus(true);
view->setSortingEnabled(true); view->setSortingEnabled(true);
view->sortByColumn(SetsModel::SortKeyCol, Qt::AscendingOrder); view->setSelectionMode(QAbstractItemView::SingleSelection);
view->setSelectionBehavior(QAbstractItemView::SelectRows);
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 #if QT_VERSION < 0x050000
view->header()->setResizeMode(QHeaderView::Stretch);
view->header()->setResizeMode(SetsModel::LongNameCol, QHeaderView::ResizeToContents); view->header()->setResizeMode(SetsModel::LongNameCol, QHeaderView::ResizeToContents);
#else #else
view->header()->setSectionResizeMode(QHeaderView::Stretch);
view->header()->setSectionResizeMode(SetsModel::LongNameCol, QHeaderView::ResizeToContents); view->header()->setSectionResizeMode(SetsModel::LongNameCol, QHeaderView::ResizeToContents);
#endif #endif
saveButton = new QPushButton(tr("Save set ordering")); upButton = new QPushButton(tr("Move selected set up"));
connect(saveButton, SIGNAL(clicked()), this, SLOT(actSave())); connect(upButton, SIGNAL(clicked()), this, SLOT(actUp()));
restoreButton = new QPushButton(tr("Restore saved set ordering")); downButton = new QPushButton(tr("Move selected set down"));
connect(restoreButton, SIGNAL(clicked()), this, SLOT(actRestore())); 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; QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(view, 0, 0, 1, 2); 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; QWidget *centralWidget = new QWidget;
centralWidget->setLayout(mainLayout); 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);
} }

View file

@ -5,23 +5,28 @@
class SetsModel; class SetsModel;
class SetsProxyModel; class SetsProxyModel;
class QTreeView;
class QPushButton; class QPushButton;
class CardDatabase; class CardDatabase;
class QItemSelection;
class QTreeView;
class WndSets : public QMainWindow { class WndSets : public QMainWindow {
Q_OBJECT Q_OBJECT
private: private:
SetsModel *model; SetsModel *model;
SetsProxyModel *proxyModel;
QTreeView *view; QTreeView *view;
QPushButton *saveButton, *restoreButton; QPushButton *upButton, *downButton, *bottomButton, *topButton;
public: public:
WndSets(QWidget *parent = 0); WndSets(QWidget *parent = 0);
~WndSets(); ~WndSets();
protected:
void selectRow(int row);
private slots: private slots:
void actSave(); void actUp();
void actRestore(); void actDown();
void actTop();
void actBottom();
void actToggleButtons(const QItemSelection & selected, const QItemSelection & deselected);
}; };
#endif #endif