Improved context menu, added context menu for masters and added multi-selection support

This commit is contained in:
Pieter van der Kloet 2013-02-24 03:10:27 +01:00
parent 150d4a7a72
commit 9198afeefb
2 changed files with 119 additions and 76 deletions

View file

@ -99,9 +99,10 @@ DataFilesPage::DataFilesPage(Files::ConfigurationManager &cfg, GameSettings &gam
mMastersTable = new QTableView(this); mMastersTable = new QTableView(this);
mMastersTable->setModel(mMastersProxyModel); mMastersTable->setModel(mMastersProxyModel);
mMastersTable->setObjectName("MastersTable"); mMastersTable->setObjectName("MastersTable");
mMastersTable->setContextMenuPolicy(Qt::CustomContextMenu);
mMastersTable->setSortingEnabled(false); mMastersTable->setSortingEnabled(false);
mMastersTable->setSelectionBehavior(QAbstractItemView::SelectRows); mMastersTable->setSelectionBehavior(QAbstractItemView::SelectRows);
mMastersTable->setSelectionMode(QAbstractItemView::SingleSelection); mMastersTable->setSelectionMode(QAbstractItemView::ExtendedSelection);
mMastersTable->setEditTriggers(QAbstractItemView::NoEditTriggers); mMastersTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
mMastersTable->setAlternatingRowColors(true); mMastersTable->setAlternatingRowColors(true);
mMastersTable->horizontalHeader()->setStretchLastSection(true); mMastersTable->horizontalHeader()->setStretchLastSection(true);
@ -118,7 +119,7 @@ DataFilesPage::DataFilesPage(Files::ConfigurationManager &cfg, GameSettings &gam
mPluginsTable->setContextMenuPolicy(Qt::CustomContextMenu); mPluginsTable->setContextMenuPolicy(Qt::CustomContextMenu);
mPluginsTable->setSortingEnabled(false); mPluginsTable->setSortingEnabled(false);
mPluginsTable->setSelectionBehavior(QAbstractItemView::SelectRows); mPluginsTable->setSelectionBehavior(QAbstractItemView::SelectRows);
mPluginsTable->setSelectionMode(QAbstractItemView::SingleSelection); mPluginsTable->setSelectionMode(QAbstractItemView::ExtendedSelection);
mPluginsTable->setEditTriggers(QAbstractItemView::NoEditTriggers); mPluginsTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
mPluginsTable->setAlternatingRowColors(true); mPluginsTable->setAlternatingRowColors(true);
mPluginsTable->setVerticalScrollMode(QAbstractItemView::ScrollPerItem); mPluginsTable->setVerticalScrollMode(QAbstractItemView::ScrollPerItem);
@ -173,6 +174,7 @@ DataFilesPage::DataFilesPage(Files::ConfigurationManager &cfg, GameSettings &gam
connect(mMastersTable, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(setCheckState(QModelIndex))); connect(mMastersTable, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(setCheckState(QModelIndex)));
connect(mPluginsTable, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showContextMenu(QPoint))); connect(mPluginsTable, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showContextMenu(QPoint)));
connect(mMastersTable, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showContextMenu(QPoint)));
connect(mDataFilesModel, SIGNAL(layoutChanged()), this, SLOT(updateViews())); connect(mDataFilesModel, SIGNAL(layoutChanged()), this, SLOT(updateViews()));
@ -208,15 +210,13 @@ void DataFilesPage::createActions()
mProfileToolBar->addAction(mDeleteProfileAction); mProfileToolBar->addAction(mDeleteProfileAction);
// Context menu actions // Context menu actions
mCheckAction = new QAction(tr("Check selected"), this); mCheckAction = new QAction(tr("Check Selection"), this);
connect(mCheckAction, SIGNAL(triggered()), this, SLOT(check())); connect(mCheckAction, SIGNAL(triggered()), this, SLOT(check()));
mUncheckAction = new QAction(tr("Uncheck selected"), this); mUncheckAction = new QAction(tr("Uncheck Selection"), this);
connect(mUncheckAction, SIGNAL(triggered()), this, SLOT(uncheck())); connect(mUncheckAction, SIGNAL(triggered()), this, SLOT(uncheck()));
// Context menu for the plugins table
mContextMenu = new QMenu(this); mContextMenu = new QMenu(this);
mContextMenu->addAction(mCheckAction); mContextMenu->addAction(mCheckAction);
mContextMenu->addAction(mUncheckAction); mContextMenu->addAction(mUncheckAction);
} }
@ -296,6 +296,9 @@ void DataFilesPage::loadSettings()
void DataFilesPage::saveSettings() void DataFilesPage::saveSettings()
{ {
if (mDataFilesModel->rowCount() < 1)
return;
QString profile = mLauncherSettings.value(QString("Profiles/CurrentProfile")); QString profile = mLauncherSettings.value(QString("Profiles/CurrentProfile"));
if (profile.isEmpty()) if (profile.isEmpty())
@ -406,54 +409,21 @@ void DataFilesPage::deleteProfile()
void DataFilesPage::check() void DataFilesPage::check()
{ {
// Check the current selection if (mPluginsTable->hasFocus())
if (!mPluginsTable->selectionModel()->hasSelection()) { setPluginsCheckstates(Qt::Checked);
return;
}
QModelIndexList indexes = mPluginsTable->selectionModel()->selectedIndexes(); if (mMastersTable->hasFocus())
setMastersCheckstates(Qt::Checked);
//sort selection ascending because selectedIndexes returns an unsorted list
//qSort(indexes.begin(), indexes.end(), rowSmallerThan);
foreach (const QModelIndex &index, indexes) {
if (!index.isValid())
return;
QModelIndex sourceIndex = mPluginsProxyModel->mapToSource(
mFilterProxyModel->mapToSource(index));
if (!sourceIndex.isValid())
return;
mDataFilesModel->setCheckState(sourceIndex, Qt::Checked);
}
} }
void DataFilesPage::uncheck() void DataFilesPage::uncheck()
{ {
// uncheck the current selection if (mPluginsTable->hasFocus())
if (!mPluginsTable->selectionModel()->hasSelection()) { setPluginsCheckstates(Qt::Unchecked);
return;
}
QModelIndexList indexes = mPluginsTable->selectionModel()->selectedIndexes(); if (mMastersTable->hasFocus())
setMastersCheckstates(Qt::Unchecked);
//sort selection ascending because selectedIndexes returns an unsorted list
//qSort(indexes.begin(), indexes.end(), rowSmallerThan);
foreach (const QModelIndex &index, indexes) {
if (!index.isValid())
return;
QModelIndex sourceIndex = mPluginsProxyModel->mapToSource(
mFilterProxyModel->mapToSource(index));
if (!sourceIndex.isValid())
return;
mDataFilesModel->setCheckState(sourceIndex, Qt::Unchecked);
}
} }
void DataFilesPage::refresh() void DataFilesPage::refresh()
@ -464,6 +434,50 @@ void DataFilesPage::refresh()
mPluginsTable->scrollToTop(); mPluginsTable->scrollToTop();
} }
void DataFilesPage::setMastersCheckstates(Qt::CheckState state)
{
if (!mMastersTable->selectionModel()->hasSelection()) {
return;
}
QModelIndexList indexes = mMastersTable->selectionModel()->selectedIndexes();
foreach (const QModelIndex &index, indexes)
{
if (!index.isValid())
return;
QModelIndex sourceIndex = mMastersProxyModel->mapToSource(index);
if (!sourceIndex.isValid())
return;
mDataFilesModel->setCheckState(sourceIndex, state);
}
}
void DataFilesPage::setPluginsCheckstates(Qt::CheckState state)
{
if (!mPluginsTable->selectionModel()->hasSelection()) {
return;
}
QModelIndexList indexes = mPluginsTable->selectionModel()->selectedIndexes();
foreach (const QModelIndex &index, indexes)
{
if (!index.isValid())
return;
QModelIndex sourceIndex = mPluginsProxyModel->mapToSource(
mFilterProxyModel->mapToSource(index));
if (!sourceIndex.isValid())
return;
mDataFilesModel->setCheckState(sourceIndex, state);
}
}
void DataFilesPage::setCheckState(QModelIndex index) void DataFilesPage::setCheckState(QModelIndex index)
{ {
@ -554,35 +568,69 @@ void DataFilesPage::profileRenamed(const QString &previous, const QString &curre
void DataFilesPage::showContextMenu(const QPoint &point) void DataFilesPage::showContextMenu(const QPoint &point)
{ {
// Make sure there are plugins in the view QObject *object = QObject::sender();
if (!mPluginsTable->selectionModel()->hasSelection()) {
// Not a signal-slot call
if (!object)
return; return;
}
QPoint globalPos = mPluginsTable->mapToGlobal(point); if (object->objectName() == QLatin1String("PluginsTable")) {
if (!mPluginsTable->selectionModel()->hasSelection())
QModelIndexList indexes = mPluginsTable->selectionModel()->selectedIndexes();
// Show the check/uncheck actions depending on the state of the selected items
mUncheckAction->setEnabled(false);
mCheckAction->setEnabled(false);
foreach (const QModelIndex &index, indexes) {
if (!index.isValid())
return; return;
QModelIndex sourceIndex = mPluginsProxyModel->mapToSource( QPoint globalPos = mPluginsTable->mapToGlobal(point);
mFilterProxyModel->mapToSource(index)); QModelIndexList indexes = mPluginsTable->selectionModel()->selectedIndexes();
if (!sourceIndex.isValid()) // Show the check/uncheck actions depending on the state of the selected items
return; mUncheckAction->setEnabled(false);
mCheckAction->setEnabled(false);
(mDataFilesModel->checkState(sourceIndex) == Qt::Checked) foreach (const QModelIndex &index, indexes)
? mUncheckAction->setEnabled(true) {
: mCheckAction->setEnabled(true); if (!index.isValid())
return;
QModelIndex sourceIndex = mPluginsProxyModel->mapToSource(
mFilterProxyModel->mapToSource(index));
if (!sourceIndex.isValid())
return;
(mDataFilesModel->checkState(sourceIndex) == Qt::Checked)
? mUncheckAction->setEnabled(true)
: mCheckAction->setEnabled(true);
}
// Show menu
mContextMenu->exec(globalPos);
} }
// Show menu if (object->objectName() == QLatin1String("MastersTable")) {
mContextMenu->exec(globalPos); if (!mMastersTable->selectionModel()->hasSelection())
return;
QPoint globalPos = mMastersTable->mapToGlobal(point);
QModelIndexList indexes = mMastersTable->selectionModel()->selectedIndexes();
// Show the check/uncheck actions depending on the state of the selected items
mUncheckAction->setEnabled(false);
mCheckAction->setEnabled(false);
foreach (const QModelIndex &index, indexes)
{
if (!index.isValid())
return;
QModelIndex sourceIndex = mMastersProxyModel->mapToSource(index);
if (!sourceIndex.isValid())
return;
(mDataFilesModel->checkState(sourceIndex) == Qt::Checked)
? mUncheckAction->setEnabled(true)
: mCheckAction->setEnabled(true);
}
mContextMenu->exec(globalPos);
}
} }

View file

@ -37,7 +37,6 @@ public:
public slots: public slots:
void setCheckState(QModelIndex index); void setCheckState(QModelIndex index);
void filterChanged(const QString filter); void filterChanged(const QString filter);
void showContextMenu(const QPoint &point); void showContextMenu(const QPoint &point);
void profileChanged(const QString &previous, const QString &current); void profileChanged(const QString &previous, const QString &current);
@ -49,10 +48,6 @@ public slots:
// Action slots // Action slots
void newProfile(); void newProfile();
void deleteProfile(); void deleteProfile();
// void moveUp();
// void moveDown();
// void moveTop();
// void moveBottom();
void check(); void check();
void uncheck(); void uncheck();
void refresh(); void refresh();
@ -90,8 +85,8 @@ private:
TextInputDialog *mNewProfileDialog; TextInputDialog *mNewProfileDialog;
// const QStringList checkedPlugins(); void setMastersCheckstates(Qt::CheckState state);
// const QStringList selectedMasters(); void setPluginsCheckstates(Qt::CheckState state);
void createActions(); void createActions();
void setupDataFiles(); void setupDataFiles();