Add support for multi-selection in set editor (#2441)

This commit is contained in:
Zach H 2017-03-22 15:49:38 -04:00 committed by GitHub
parent 33e8a2ea95
commit 85985a9433
4 changed files with 175 additions and 74 deletions

View file

@ -124,7 +124,7 @@ void SetsModel::toggleRow(int row, bool enable)
{ {
CardSet *temp = sets.at(row); CardSet *temp = sets.at(row);
if(enable) if (enable)
enabledSets.insert(temp); enabledSets.insert(temp);
else else
enabledSets.remove(temp); enabledSets.remove(temp);
@ -132,14 +132,28 @@ void SetsModel::toggleRow(int row, bool enable)
emit dataChanged(index(row, EnabledCol), index(row, EnabledCol)); emit dataChanged(index(row, EnabledCol), index(row, EnabledCol));
} }
void SetsModel::toggleAll(bool enable) void SetsModel::toggleRow(int row)
{
CardSet *tmp = sets.at(row);
if (tmp == nullptr)
return;
if (enabledSets.contains(tmp))
enabledSets.remove(tmp);
else
enabledSets.insert(tmp);
emit dataChanged(index(row, EnabledCol), index(row, EnabledCol));
}
void SetsModel::toggleAll(bool enabled)
{ {
enabledSets.clear(); enabledSets.clear();
if(enable)
{ if (enabled)
foreach(CardSet *set, sets) foreach(CardSet *set, sets)
enabledSets.insert(set); enabledSets.insert(set);
}
emit dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1)); emit dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1));
} }

View file

@ -43,7 +43,8 @@ public:
QStringList mimeTypes() const; QStringList mimeTypes() const;
void swapRows(int oldRow, int newRow); void swapRows(int oldRow, int newRow);
void toggleRow(int row, bool enable); void toggleRow(int row, bool enable);
void toggleAll(bool enable); void toggleRow(int row);
void toggleAll(bool);
void sort(int column, Qt::SortOrder order = Qt::AscendingOrder); void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);
void save(CardDatabase *db); void save(CardDatabase *db);
void restore(CardDatabase *db); void restore(CardDatabase *db);

View file

@ -15,12 +15,13 @@
#include <QToolBar> #include <QToolBar>
#include <QAction> #include <QAction>
#include <QLabel> #include <QLabel>
#include <QDebug>
WndSets::WndSets(QWidget *parent) WndSets::WndSets(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
{ {
// left toolbar // left toolbar
QToolBar *setsEditToolBar = new QToolBar; setsEditToolBar = new QToolBar;
setsEditToolBar->setOrientation(Qt::Vertical); setsEditToolBar->setOrientation(Qt::Vertical);
setsEditToolBar->setIconSize(QSize(24, 24)); setsEditToolBar->setIconSize(QSize(24, 24));
setsEditToolBar->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); setsEditToolBar->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
@ -64,6 +65,7 @@ WndSets::WndSets(QWidget *parent)
view->setSortingEnabled(true); view->setSortingEnabled(true);
view->setSelectionMode(QAbstractItemView::SingleSelection); view->setSelectionMode(QAbstractItemView::SingleSelection);
view->setSelectionBehavior(QAbstractItemView::SelectRows); view->setSelectionBehavior(QAbstractItemView::SelectRows);
view->setSelectionMode(QAbstractItemView::ExtendedSelection);
view->setDragEnabled(true); view->setDragEnabled(true);
view->setAcceptDrops(true); view->setAcceptDrops(true);
@ -78,33 +80,41 @@ WndSets::WndSets(QWidget *parent)
view->setColumnHidden(SetsModel::IsKnownCol, true); view->setColumnHidden(SetsModel::IsKnownCol, true);
view->setRootIsDecorated(false); view->setRootIsDecorated(false);
connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
this, SLOT(actToggleButtons(const QItemSelection &, const QItemSelection &)));
// bottom buttons // bottom buttons
enableAllButton = new QPushButton(tr("Enable all sets")); enableAllButton = new QPushButton(tr("Enable all sets"));
connect(enableAllButton, SIGNAL(clicked()), this, SLOT(actEnableAll()));
disableAllButton = new QPushButton(tr("Disable all sets")); disableAllButton = new QPushButton(tr("Disable all sets"));
enableSomeButton = new QPushButton(tr("Enable selected set(s)"));
disableSomeButton = new QPushButton(tr("Disable selected set(s)"));
connect(enableAllButton, SIGNAL(clicked()), this, SLOT(actEnableAll()));
connect(disableAllButton, SIGNAL(clicked()), this, SLOT(actDisableAll())); connect(disableAllButton, SIGNAL(clicked()), this, SLOT(actDisableAll()));
connect(enableSomeButton, SIGNAL(clicked()), this, SLOT(actEnableSome()));
connect(disableSomeButton, SIGNAL(clicked()), this, SLOT(actDisableSome()));
connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
this, SLOT(actToggleButtons(const QItemSelection &, const QItemSelection &)));
labNotes = new QLabel;
QLabel *labNotes = new QLabel;
labNotes->setText("<b>" + tr("hints:") + "</b>" + "<ul><li>" + tr("Enable the sets that you want to have available in the deck editor") + "</li><li>" + tr("Move sets around to change their order, or click on a column header to sort sets on that field") + "</li><li>" + tr("Sets order decides the source that will be used when loading images for a specific card") + "</li><li>" + tr("Disabled sets will be used for loading images only if all the enabled sets failed") + "</li></ul>"); labNotes->setText("<b>" + tr("hints:") + "</b>" + "<ul><li>" + tr("Enable the sets that you want to have available in the deck editor") + "</li><li>" + tr("Move sets around to change their order, or click on a column header to sort sets on that field") + "</li><li>" + tr("Sets order decides the source that will be used when loading images for a specific card") + "</li><li>" + tr("Disabled sets will be used for loading images only if all the enabled sets failed") + "</li></ul>");
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(buttonBox, SIGNAL(accepted()), this, SLOT(actSave())); connect(buttonBox, SIGNAL(accepted()), this, SLOT(actSave()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(actRestore())); connect(buttonBox, SIGNAL(rejected()), this, SLOT(actRestore()));
QGridLayout *mainLayout = new QGridLayout; mainLayout = new QGridLayout;
mainLayout->addWidget(setsEditToolBar, 0, 0, 1, 1); mainLayout->addWidget(setsEditToolBar, 0, 0, 1, 1);
mainLayout->addWidget(view, 0, 1, 1, 2); mainLayout->addWidget(view, 0, 1, 1, 2);
mainLayout->addWidget(enableAllButton, 1, 1, 1, 1); mainLayout->addWidget(enableAllButton, 1, 1);
mainLayout->addWidget(disableAllButton, 1, 2, 1, 1); mainLayout->addWidget(disableAllButton, 1, 2);
mainLayout->addWidget(enableSomeButton, 1, 1);
mainLayout->addWidget(disableSomeButton, 1, 2);
mainLayout->addWidget(labNotes, 2, 1, 1, 2); mainLayout->addWidget(labNotes, 2, 1, 1, 2);
mainLayout->addWidget(buttonBox, 3, 1, 1, 2); mainLayout->addWidget(buttonBox, 3, 1, 1, 2);
mainLayout->setColumnStretch(1, 1); mainLayout->setColumnStretch(1, 1);
mainLayout->setColumnStretch(2, 1); mainLayout->setColumnStretch(2, 1);
enableSomeButton->hide();
disableSomeButton->hide();
QWidget *centralWidget = new QWidget; QWidget *centralWidget = new QWidget;
centralWidget->setLayout(mainLayout); centralWidget->setLayout(mainLayout);
setCentralWidget(centralWidget); setCentralWidget(centralWidget);
@ -117,6 +127,29 @@ WndSets::~WndSets()
{ {
} }
void WndSets::rebuildMainLayout(int actionToTake)
{
if (mainLayout == nullptr)
return;
switch (actionToTake)
{
case NO_SETS_SELECTED:
enableAllButton->show();
disableAllButton->show();
enableSomeButton->hide();
disableSomeButton->hide();
break;
case SOME_SETS_SELECTED:
enableAllButton->hide();
disableAllButton->hide();
enableSomeButton->show();
disableSomeButton->show();
break;
}
}
void WndSets::actSave() void WndSets::actSave()
{ {
model->save(db); model->save(db);
@ -138,36 +171,27 @@ void WndSets::actToggleButtons(const QItemSelection & selected, const QItemSelec
aUp->setDisabled(disabled); aUp->setDisabled(disabled);
aDown->setDisabled(disabled); aDown->setDisabled(disabled);
aBottom->setDisabled(disabled); aBottom->setDisabled(disabled);
int rows = view->selectionModel()->selectedRows().size();
rebuildMainLayout((rows > 1) ? SOME_SETS_SELECTED : NO_SETS_SELECTED);
} }
void WndSets::selectRow(int row) void WndSets::selectRows(QSet<int> rows)
{ {
QModelIndex idx = model->index(row, 0); foreach (int i, rows)
view->selectionModel()->select(idx, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows); {
view->scrollTo(idx, QAbstractItemView::EnsureVisible); QModelIndex idx = model->index(i, 0);
view->selectionModel()->select(idx, QItemSelectionModel::Select | QItemSelectionModel::Rows);
view->scrollTo(idx, QAbstractItemView::EnsureVisible);
}
} }
void WndSets::actEnable()
{
QModelIndexList rows = view->selectionModel()->selectedRows();
if(rows.empty())
return;
model->toggleRow(rows.first().row(), true);
}
void WndSets::actDisable()
{
QModelIndexList rows = view->selectionModel()->selectedRows();
if(rows.empty())
return;
model->toggleRow(rows.first().row(), false);
}
void WndSets::actEnableAll() void WndSets::actEnableAll()
{ {
model->toggleAll(true); model->toggleAll(true);
} }
void WndSets::actDisableAll() void WndSets::actDisableAll()
@ -175,66 +199,117 @@ void WndSets::actDisableAll()
model->toggleAll(false); model->toggleAll(false);
} }
void WndSets::actEnableSome()
{
QModelIndexList rows = view->selectionModel()->selectedRows();
foreach(QModelIndex i, rows)
model->toggleRow(i.row(), true);
}
void WndSets::actDisableSome()
{
QModelIndexList rows = view->selectionModel()->selectedRows();
foreach(QModelIndex i, rows)
model->toggleRow(i.row(), false);
}
void WndSets::actUp() void WndSets::actUp()
{ {
QModelIndexList rows = view->selectionModel()->selectedRows(); QModelIndexList rows = view->selectionModel()->selectedRows();
if(rows.empty()) qSort(rows.begin(), rows.end(), qLess<QModelIndex>());
QSet<int> newRows;
if (rows.empty())
return; return;
QModelIndex selectedRow = rows.first(); foreach (QModelIndex i, rows)
int oldRow = selectedRow.row(); {
int newRow = oldRow - 1; int oldRow = i.row();
if(oldRow <= 0) int newRow = oldRow - 1;
return; if (oldRow <= 0)
continue;
model->swapRows(oldRow, newRow); model->swapRows(oldRow, newRow);
selectRow(newRow); newRows.insert(newRow);
}
selectRows(newRows);
} }
void WndSets::actDown() void WndSets::actDown()
{ {
QModelIndexList rows = view->selectionModel()->selectedRows(); QModelIndexList rows = view->selectionModel()->selectedRows();
if(rows.empty()) qSort(rows.begin(), rows.end(), qGreater<QModelIndex>());
QSet<int> newRows;
if (rows.empty())
return; return;
QModelIndex selectedRow = rows.first(); foreach (QModelIndex i, rows)
int oldRow = selectedRow.row(); {
int newRow = oldRow + 1; int oldRow = i.row();
if(oldRow >= model->rowCount() - 1) int newRow = oldRow + 1;
return; if (oldRow >= model->rowCount() - 1)
continue;
model->swapRows(oldRow, newRow); model->swapRows(oldRow, newRow);
selectRow(newRow); newRows.insert(newRow);
}
selectRows(newRows);
} }
void WndSets::actTop() void WndSets::actTop()
{ {
QModelIndexList rows = view->selectionModel()->selectedRows(); QModelIndexList rows = view->selectionModel()->selectedRows();
if(rows.empty()) qSort(rows.begin(), rows.end(), qLess<QModelIndex>());
return; QSet<int> newRows;
QModelIndex selectedRow = rows.first();
int oldRow = selectedRow.row();
int newRow = 0; int newRow = 0;
if(oldRow <= 0)
if (rows.empty())
return; return;
model->swapRows(oldRow, newRow); for (int i = 0; i < rows.length(); i++)
selectRow(newRow); {
int oldRow = rows.at(i).row();
if (oldRow <= 0) {
newRow++;
continue;
}
newRows.insert(newRow);
model->swapRows(oldRow, newRow++);
}
selectRows(newRows);
} }
void WndSets::actBottom() void WndSets::actBottom()
{ {
QModelIndexList rows = view->selectionModel()->selectedRows(); QModelIndexList rows = view->selectionModel()->selectedRows();
if(rows.empty()) qSort(rows.begin(), rows.end(), qGreater<QModelIndex>());
return; QSet<int> newRows;
QModelIndex selectedRow = rows.first();
int oldRow = selectedRow.row();
int newRow = model->rowCount() - 1; int newRow = model->rowCount() - 1;
if(oldRow >= newRow)
if (rows.empty())
return; return;
model->swapRows(oldRow, newRow); for (int i = 0; i < rows.length(); i++)
selectRow(newRow); {
int oldRow = rows.at(i).row();
if (oldRow >= newRow) {
newRow--;
continue;
}
newRows.insert(newRow);
model->swapRows(oldRow, newRow--);
}
selectRows(newRows);
} }

View file

@ -2,6 +2,10 @@
#define WINDOW_SETS_H #define WINDOW_SETS_H
#include <QMainWindow> #include <QMainWindow>
#include <QSet>
#include <QDialogButtonBox>
#include <QLabel>
#include <QGridLayout>
class SetsModel; class SetsModel;
class SetsProxyModel; class SetsProxyModel;
@ -15,18 +19,25 @@ class WndSets : public QMainWindow {
private: private:
SetsModel *model; SetsModel *model;
QTreeView *view; QTreeView *view;
QPushButton *enableAllButton, *disableAllButton; QPushButton *toggleAllButton, *toggleSelectedButton;
QPushButton *enableAllButton, *disableAllButton, *enableSomeButton, *disableSomeButton;
QAction *aUp, *aDown, *aBottom, *aTop; QAction *aUp, *aDown, *aBottom, *aTop;
QToolBar *setsEditToolBar;
QDialogButtonBox *buttonBox;
QLabel *labNotes;
QGridLayout *mainLayout;
void rebuildMainLayout(int actionToTake);
enum {NO_SETS_SELECTED, SOME_SETS_SELECTED};
public: public:
WndSets(QWidget *parent = 0); WndSets(QWidget *parent = 0);
~WndSets(); ~WndSets();
protected: protected:
void selectRow(int row); void selectRows(QSet<int> rows);
private slots: private slots:
void actEnable();
void actDisable();
void actEnableAll(); void actEnableAll();
void actDisableAll(); void actDisableAll();
void actEnableSome();
void actDisableSome();
void actSave(); void actSave();
void actRestore(); void actRestore();
void actUp(); void actUp();