From bd34b532230d92bd6232ceb1ab2b0092cac79d22 Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Thu, 31 Dec 2020 22:18:11 +0100 Subject: Move plugin list context menu to its own class and to PluginListView. --- src/pluginlistcontextmenu.cpp | 132 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 src/pluginlistcontextmenu.cpp (limited to 'src/pluginlistcontextmenu.cpp') diff --git a/src/pluginlistcontextmenu.cpp b/src/pluginlistcontextmenu.cpp new file mode 100644 index 00000000..787e5c0c --- /dev/null +++ b/src/pluginlistcontextmenu.cpp @@ -0,0 +1,132 @@ +#include "pluginlistcontextmenu.h" + +#include +#include + +#include "pluginlistview.h" +#include "organizercore.h" + +using namespace MOBase; + +PluginListContextMenu::PluginListContextMenu( + const QModelIndex& index, OrganizerCore& core, PluginListView* view) : + QMenu(view) + , m_core(core) + , m_index(index.model() == view->model() ? view->indexViewToModel(index) : index) + , m_view(view) +{ + if (view->selectionModel()->hasSelection()) { + m_selected = view->indexViewToModel(view->selectionModel()->selectedRows()); + } + else { + m_selected = { index }; + } + + addAction(tr("Enable selected"), [=]() { m_core.pluginList()->setEnabled(m_selected, true); }); + addAction(tr("Disable selected"), [=]() { m_core.pluginList()->setEnabled(m_selected, false); }); + + addSeparator(); + + addAction(tr("Enable all"), m_core.pluginList(), &PluginList::enableAll); + addAction(tr("Disable all"), m_core.pluginList(), &PluginList::disableAll); + + addSeparator(); + + addMenu(createSendToContextMenu()); + addSeparator(); + + bool hasLocked = false; + bool hasUnlocked = false; + for (auto& idx : m_selected) { + if (m_core.pluginList()->isEnabled(idx.row())) { + if (m_core.pluginList()->isESPLocked(idx.row())) { + hasLocked = true; + } + else { + hasUnlocked = true; + } + } + } + + if (hasLocked) { + addAction(tr("Unlock load order"), [=]() { setESPLock(m_selected, false); }); + } + if (hasUnlocked) { + addAction(tr("Lock load order"), [=]() { setESPLock(m_selected, true); }); + } + + addSeparator(); + + unsigned int modInfoIndex = ModInfo::getIndex(m_core.pluginList()->origin(m_index.data().toString())); + // this is to avoid showing the option on game files like skyrim.esm + if (modInfoIndex != UINT_MAX) { + addAction(tr("Open Origin in Explorer"), [=]() { openOriginExplorer(m_selected); }); + ModInfo::Ptr modInfo = ModInfo::getByIndex(modInfoIndex); + std::vector flags = modInfo->getFlags(); + + if (!modInfo->isForeign() && m_selected.size() == 1) { + QAction* infoAction = addAction(tr("Open Origin Info..."), [=]() { openOriginInformation(index); }); + setDefaultAction(infoAction); + } + } + +} + +QMenu* PluginListContextMenu::createSendToContextMenu() +{ + QMenu* menu = new QMenu(m_view); + menu->setTitle(tr("Send to... ")); + menu->addAction(tr("Top"), [=]() { m_core.pluginList()->sendToPriority(m_selected, 0); }); + menu->addAction(tr("Bottom"), [=]() { m_core.pluginList()->sendToPriority(m_selected, INT_MAX); }); + menu->addAction(tr("Priority..."), [=]() { sendPluginsToPriority(m_selected); }); + return menu; +} + +void PluginListContextMenu::sendPluginsToPriority(const QModelIndexList& indices) +{ + bool ok; + int newPriority = QInputDialog::getInt(m_view->topLevelWidget(), + tr("Set Priority"), tr("Set the priority of the selected plugins"), + 0, 0, INT_MAX, 1, &ok); + if (!ok) return; + + m_core.pluginList()->sendToPriority(m_selected, newPriority); +} + +void PluginListContextMenu::setESPLock(const QModelIndexList& indices, bool locked) +{ + for (auto& idx : indices) { + if (m_core.pluginList()->isEnabled(idx.row())) { + m_core.pluginList()->lockESPIndex(idx.row(), locked); + } + } +} + +void PluginListContextMenu::openOriginExplorer(const QModelIndexList& indices) +{ + for (auto& idx : indices) { + QString fileName = idx.data().toString(); + unsigned int modIndex = ModInfo::getIndex(m_core.pluginList()->origin(fileName)); + if (modIndex == UINT_MAX) { + continue; + } + ModInfo::Ptr modInfo = ModInfo::getByIndex(modIndex); + shell::Explore(modInfo->absolutePath()); + } +} + +void PluginListContextMenu::openOriginInformation(const QModelIndex& index) +{ + try { + QString fileName = index.data().toString(); + unsigned int modIndex = ModInfo::getIndex(m_core.pluginList()->origin(fileName)); + ModInfo::Ptr modInfo = ModInfo::getByIndex(modIndex); + + if (modInfo->isRegular() || modInfo->isOverwrite()) { + emit openModInformation(modIndex); + } + } + catch (const std::exception& e) { + reportError(e.what()); + } +} -- cgit v1.3.1 From 399ff3fcf7920adec128d30f3c97a7636a6f10be Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Thu, 31 Dec 2020 23:03:03 +0100 Subject: Minor clean for plugin list. --- src/pluginlist.cpp | 45 +++++++++++++------------------------------ src/pluginlist.h | 14 ++++---------- src/pluginlistcontextmenu.cpp | 16 +++++++++++++-- 3 files changed, 31 insertions(+), 44 deletions(-) (limited to 'src/pluginlistcontextmenu.cpp') diff --git a/src/pluginlist.cpp b/src/pluginlist.cpp index f6a52a80..a2e485ee 100644 --- a/src/pluginlist.cpp +++ b/src/pluginlist.cpp @@ -349,43 +349,24 @@ void PluginList::setEnabled(const QModelIndexList& indices, bool enabled) } if (!dirty.isEmpty()) { emit writePluginsList(); - pluginStatesChanged(dirty, IPluginList::PluginState::STATE_ACTIVE); + pluginStatesChanged(dirty, + enabled ? IPluginList::PluginState::STATE_ACTIVE : IPluginList::PluginState::STATE_INACTIVE); } } -void PluginList::enableAll() +void PluginList::setEnabledAll(bool enabled) { - if (QMessageBox::question(nullptr, tr("Confirm"), tr("Really enable all plugins?"), - QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { - QStringList dirty; - for (ESPInfo &info : m_ESPs) { - if (!info.enabled) { - info.enabled = true; - dirty.append(info.name); - } - } - if (!dirty.isEmpty()) { - emit writePluginsList(); - pluginStatesChanged(dirty, IPluginList::PluginState::STATE_ACTIVE); + QStringList dirty; + for (ESPInfo &info : m_ESPs) { + if (info.enabled != enabled) { + info.enabled = enabled; + dirty.append(info.name); } } -} - -void PluginList::disableAll() -{ - if (QMessageBox::question(nullptr, tr("Confirm"), tr("Really disable all plugins?"), - QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { - QStringList dirty; - for (ESPInfo &info : m_ESPs) { - if (!info.forceEnabled && info.enabled) { - info.enabled = false; - dirty.append(info.name); - } - } - if (!dirty.isEmpty()) { - emit writePluginsList(); - pluginStatesChanged(dirty, IPluginList::PluginState::STATE_INACTIVE); - } + if (!dirty.isEmpty()) { + emit writePluginsList(); + pluginStatesChanged(dirty, + enabled ? IPluginList::PluginState::STATE_ACTIVE : IPluginList::PluginState::STATE_INACTIVE); } } @@ -404,7 +385,7 @@ void PluginList::sendToPriority(const QModelIndexList& indices, int newPriority) void PluginList::shiftPluginsPriority(const QModelIndexList& indices, int offset) { - // retrieve the mod index and sort them by priority to avoid issue + // retrieve the plugin index and sort them by priority to avoid issue // when moving them std::vector allIndex; for (auto& idx : indices) { diff --git a/src/pluginlist.h b/src/pluginlist.h index 6b0f584c..c93ce5cb 100644 --- a/src/pluginlist.h +++ b/src/pluginlist.h @@ -257,15 +257,9 @@ public: // implementation of the QAbstractTableModel interface public slots: - /** - * @brief enables ALL plugins - **/ - void enableAll(); - - /** - * @brief disables ALL plugins - **/ - void disableAll(); + // enable/disable all plugins + // + void setEnabledAll(bool enabled); // enable/disable plugins at the given indices. // @@ -273,7 +267,7 @@ public slots: // send plugins to the given priority // - void sendToPriority(const QModelIndexList& selectionModel, int priority); + void sendToPriority(const QModelIndexList& indices, int priority); // shift the priority of mods at the given indices by the given offset // diff --git a/src/pluginlistcontextmenu.cpp b/src/pluginlistcontextmenu.cpp index 787e5c0c..e6afd996 100644 --- a/src/pluginlistcontextmenu.cpp +++ b/src/pluginlistcontextmenu.cpp @@ -27,8 +27,20 @@ PluginListContextMenu::PluginListContextMenu( addSeparator(); - addAction(tr("Enable all"), m_core.pluginList(), &PluginList::enableAll); - addAction(tr("Disable all"), m_core.pluginList(), &PluginList::disableAll); + addAction(tr("Enable all"), [=]() { + if (QMessageBox::question( + m_view->topLevelWidget(), tr("Confirm"), tr("Really enable all plugins?"), + QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { + m_core.pluginList()->setEnabledAll(true); + } + }); + addAction(tr("Disable all"), [=]() { + if (QMessageBox::question( + m_view->topLevelWidget(), tr("Confirm"), tr("Really disable all plugins?"), + QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { + m_core.pluginList()->setEnabledAll(false); + } + }); addSeparator(); -- cgit v1.3.1