diff options
| author | Mikaƫl Capelle <capelle.mikael@gmail.com> | 2021-01-18 21:17:46 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-18 21:17:46 +0100 |
| commit | 0a683c0a5adda925e7dce5c9e0df5222b5ce3462 (patch) | |
| tree | fdf38d77f9ab5615161141d9df314551f696870e /src | |
| parent | 1f9a1da7f450937810a7ac8ee317cd28bdac0bdd (diff) | |
| parent | 3c0201a79909a878e6ae572b28bc7fc9588ff1c2 (diff) | |
Merge pull request #1372 from Holt59/install-mod-at
Modify 'Install mod... ' in context menu to install above current mod.
Diffstat (limited to 'src')
| -rw-r--r-- | src/modlistcontextmenu.cpp | 19 | ||||
| -rw-r--r-- | src/modlistviewactions.cpp | 87 | ||||
| -rw-r--r-- | src/modlistviewactions.h | 22 | ||||
| -rw-r--r-- | src/organizercore.cpp | 3 | ||||
| -rw-r--r-- | src/organizercore.h | 2 | ||||
| -rw-r--r-- | src/organizerproxy.cpp | 2 |
6 files changed, 81 insertions, 54 deletions
diff --git a/src/modlistcontextmenu.cpp b/src/modlistcontextmenu.cpp index f4aea109..2d9577c5 100644 --- a/src/modlistcontextmenu.cpp +++ b/src/modlistcontextmenu.cpp @@ -24,24 +24,31 @@ void ModListGlobalContextMenu::populate(OrganizerCore& core, ModListView* view, { clear(); - addAction(tr("Install Mod..."), [=]() { view->actions().installMod(); }); - auto modIndex = index.data(ModList::IndexRole); if (modIndex.isValid() && view->sortColumn() == ModList::COL_PRIORITY) { auto info = ModInfo::getByIndex(modIndex.toInt()); if (!info->isBackup()) { - QString text = tr("Create empty mod above"); + + // the mod are not created/installed at the same position depending + // on the clicked mod and the sort order + QString installText = tr("Install mod above... "); + QString createText = tr("Create empty mod above"); if (info->isSeparator()) { - text = tr("Create empty mod inside"); + installText = tr("Install mod inside... "); + createText = tr("Create empty mod inside"); } else if (view->sortOrder() == Qt::DescendingOrder) { - text = tr("Create empty mod below"); + installText = tr("Install mod below... "); + createText = tr("Create empty mod below"); } - addAction(text, [=]() { view->actions().createEmptyMod(index); }); + + addAction(installText, [=]() { view->actions().installMod("", index); }); + addAction(createText, [=]() { view->actions().createEmptyMod(index); }); addAction(tr("Create separator above"), [=]() { view->actions().createSeparator(index); }); } } else { + addAction(tr("Install mod..."), [=]() { view->actions().installMod(); }); addAction(tr("Create empty mod"), [=]() { view->actions().createEmptyMod(); }); addAction(tr("Create separator"), [=]() { view->actions().createSeparator(); }); } diff --git a/src/modlistviewactions.cpp b/src/modlistviewactions.cpp index 123fbf75..e7e74b16 100644 --- a/src/modlistviewactions.cpp +++ b/src/modlistviewactions.cpp @@ -50,7 +50,50 @@ ModListViewActions::ModListViewActions( } -void ModListViewActions::installMod(const QString& archivePath) const +int ModListViewActions::findInstallPriority(const QModelIndex& index) const +{ + int newPriority = -1; + if (index.isValid() && index.data(ModList::IndexRole).isValid() + && m_view->sortColumn() == ModList::COL_PRIORITY) { + auto mIndex = index.data(ModList::IndexRole).toInt(); + auto info = ModInfo::getByIndex(mIndex); + newPriority = m_core.currentProfile()->getModPriority(mIndex); + if (info->isSeparator()) { + + auto isSeparator = [](const auto& p) { + return ModInfo::getByIndex(p.second)->isSeparator(); + }; + + auto& ibp = m_core.currentProfile()->getAllIndexesByPriority(); + + // start right after/before the current priority and look for the next + // separator + if (m_view->sortOrder() == Qt::AscendingOrder) { + auto it = std::find_if(ibp.find(newPriority + 1), ibp.end(), isSeparator); + if (it != ibp.end()) { + newPriority = it->first; + } + else { + newPriority = -1; + } + } + else { + auto it = std::find_if(std::reverse_iterator{ ibp.find(newPriority - 1) }, ibp.rend(), isSeparator); + if (it != ibp.rend()) { + newPriority = it->first + 1; + } + else { + // create "before" priority 0, i.e. at the end in descending priority. + newPriority = 0; + } + } + } + } + + return newPriority; +} + +void ModListViewActions::installMod(const QString& archivePath, const QModelIndex& index) const { try { QString path = archivePath; @@ -68,7 +111,7 @@ void ModListViewActions::installMod(const QString& archivePath) const return; } else { - m_core.installMod(path, false, nullptr, QString()); + m_core.installMod(path, findInstallPriority(index), false, nullptr, QString()); } } catch (const std::exception& e) { @@ -97,43 +140,6 @@ void ModListViewActions::createEmptyMod(const QModelIndex& index) const return; } - int newPriority = -1; - if (index.isValid() && index.data(ModList::IndexRole).isValid() - && m_view->sortColumn() == ModList::COL_PRIORITY) { - auto mIndex = index.data(ModList::IndexRole).toInt(); - auto info = ModInfo::getByIndex(mIndex); - newPriority = m_core.currentProfile()->getModPriority(mIndex); - if (info->isSeparator()) { - - auto isSeparator = [](const auto& p) { - return ModInfo::getByIndex(p.second)->isSeparator(); - }; - - auto& ibp = m_core.currentProfile()->getAllIndexesByPriority(); - - // start right after/before the current priority and look for the next - // separator - if (m_view->sortOrder() == Qt::AscendingOrder) { - auto it = std::find_if(ibp.find(newPriority + 1), ibp.end(), isSeparator); - if (it != ibp.end()) { - newPriority = it->first; - } - else { - newPriority = -1; - } - } - else { - auto it = std::find_if(std::reverse_iterator{ ibp.find(newPriority - 1) }, ibp.rend(), isSeparator); - if (it != ibp.rend()) { - newPriority = it->first + 1; - } - else { - // create "before" priority 0, i.e. at the end in descending priority. - newPriority = 0; - } - } - } - } if (m_core.createMod(name) == nullptr) { return; @@ -141,6 +147,7 @@ void ModListViewActions::createEmptyMod(const QModelIndex& index) const m_core.refresh(); + const int newPriority = findInstallPriority(index); const auto mIndex = ModInfo::getIndex(name); if (newPriority >= 0) { m_core.modList()->changeModPriority(mIndex, newPriority); @@ -857,7 +864,7 @@ void ModListViewActions::reinstallMod(const QModelIndex& index) const fullInstallationFile = m_core.downloadManager()->getOutputDirectory() + "/" + installationFile; } if (QFile::exists(fullInstallationFile)) { - m_core.installMod(fullInstallationFile, true, modInfo, modInfo->name()); + m_core.installMod(fullInstallationFile, -1, true, modInfo, modInfo->name()); } else { QMessageBox::information(m_parent, tr("Failed"), tr("Installation file no longer exists")); diff --git a/src/modlistviewactions.h b/src/modlistviewactions.h index 1914a8aa..8de9ad38 100644 --- a/src/modlistviewactions.h +++ b/src/modlistviewactions.h @@ -31,13 +31,20 @@ public: PluginListView* pluginView, QObject* nxmReceiver); - // install the mod from the given archive + // install mod, create an empty mod or a separator with at priority based on the + // index and the current sorting of the view + // - if the list is not sorted by priority or the index is invalid, use the + // highest priority + // - otherwise + // - create separators above the given index (the priority is not the + // same depending on the current sorting) + // - create/install mods above or below other mods depending on the sort order, or + // at the end of the separator if index corresponds to the separator // - void installMod(const QString& archivePath = "") const; - - // create an empty mod/a separator before the given mod or at - // the end of the list if the index is invalid + // if the archivePath is empty for installMod, ask the user for the archive + // to install // + void installMod(const QString& archivePath = "", const QModelIndex& index = QModelIndex()) const; void createEmptyMod(const QModelIndex& index = QModelIndex()) const; void createSeparator(const QModelIndex& index = QModelIndex()) const; @@ -133,6 +140,11 @@ signals: private: + // find the priority where to install or create a mod for the + // given index (e.g. above, below, inside separator or at the end) + // + int findInstallPriority(const QModelIndex& current) const; + // move the contents of the overwrite to the given path // void moveOverwriteContentsTo(const QString& absolutePath) const; diff --git a/src/organizercore.cpp b/src/organizercore.cpp index d3e817a5..d5c341a9 100644 --- a/src/organizercore.cpp +++ b/src/organizercore.cpp @@ -718,11 +718,12 @@ QString OrganizerCore::pluginDataPath() } MOBase::IModInterface *OrganizerCore::installMod(const QString & archivePath, + int priority, bool reinstallation, ModInfo::Ptr currentMod, const QString &initModName) { - return installArchive(archivePath, -1, reinstallation, currentMod, initModName).get(); + return installArchive(archivePath, reinstallation ? -1 : priority, reinstallation, currentMod, initModName).get(); } ModInfo::Ptr OrganizerCore::installDownload(int index, int priority) diff --git a/src/organizercore.h b/src/organizercore.h index 39a2c3cb..53686c8a 100644 --- a/src/organizercore.h +++ b/src/organizercore.h @@ -318,7 +318,7 @@ public: QVariant persistent(const QString &pluginName, const QString &key, const QVariant &def) const;
void setPersistent(const QString &pluginName, const QString &key, const QVariant &value, bool sync);
static QString pluginDataPath();
- virtual MOBase::IModInterface *installMod(const QString &fileName, bool reinstallation, ModInfo::Ptr currentMod, const QString &initModName);
+ virtual MOBase::IModInterface *installMod(const QString &fileName, int priority, bool reinstallation, ModInfo::Ptr currentMod, const QString &initModName);
QString resolvePath(const QString &fileName) const;
QStringList listDirectories(const QString &directoryName) const;
QStringList findFiles(const QString &path, const std::function<bool (const QString &)> &filter) const;
diff --git a/src/organizerproxy.cpp b/src/organizerproxy.cpp index 6a19986f..b416153b 100644 --- a/src/organizerproxy.cpp +++ b/src/organizerproxy.cpp @@ -228,7 +228,7 @@ void OrganizerProxy::refresh(bool saveChanges) IModInterface *OrganizerProxy::installMod(const QString &fileName, const QString &nameSuggestion)
{
- return m_Proxied->installMod(fileName, false, nullptr, nameSuggestion);
+ return m_Proxied->installMod(fileName, -1, false, nullptr, nameSuggestion);
}
QString OrganizerProxy::resolvePath(const QString &fileName) const
|
