diff options
| author | Al <26797547+Al12rs@users.noreply.github.com> | 2020-09-01 03:59:22 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-01 03:59:22 -0700 |
| commit | fa6136c6711a0b651e2d8b652b78b055f0343c9b (patch) | |
| tree | b03b2d98dfdda0c19de1626d06dbe34c5407600f /src/modlist.cpp | |
| parent | f0e94c26aebae6f86a59ed974e433f96e77faec7 (diff) | |
| parent | 3729f18af1ee2a84756eb729e2f2b8343c51c24a (diff) | |
Merge pull request #1220 from Holt59/modlist-setactive-bulk
Add a bulk-version of ModList::setActive.
Diffstat (limited to 'src/modlist.cpp')
| -rw-r--r-- | src/modlist.cpp | 57 |
1 files changed, 40 insertions, 17 deletions
diff --git a/src/modlist.cpp b/src/modlist.cpp index cb08c6f4..cbd9f30c 100644 --- a/src/modlist.cpp +++ b/src/modlist.cpp @@ -637,18 +637,6 @@ bool ModList::setData(const QModelIndex &index, const QVariant &value, int role) } emit postDataChanged(); - - IModList::ModStates newState = state(modID); - if (oldState != newState) { - try { - m_ModStateChanged(info->name(), newState); - } catch (const std::exception &e) { - log::error("failed to invoke state changed notification: {}", e.what()); - } catch (...) { - log::error("failed to invoke state changed notification: unknown exception"); - } - } - return result; } @@ -834,7 +822,7 @@ void ModList::modInfoChanged(ModInfo::Ptr info) if (info->name() == m_ChangeInfo.name) { IModList::ModStates newState = state(info->name()); if (m_ChangeInfo.state != newState) { - m_ModStateChanged(info->name(), newState); + m_ModStateChanged({ {info->name(), newState} }); } int row = ModInfo::getIndex(info->name()); @@ -952,16 +940,41 @@ bool ModList::setActive(const QString &name, bool active) { unsigned int modIndex = ModInfo::getIndex(name); if (modIndex == UINT_MAX) { + log::debug("Trying to {} mod {} which does not exist.", + active ? "enable" : "disable", name); return false; } else { m_Profile->setModEnabled(modIndex, active); - - IModList::ModStates newState = state(modIndex); - m_ModStateChanged(name, newState); return true; } } +int ModList::setActive(const QStringList& names, bool active) { + + // We only add indices for mods that exist (modIndex != UINT_MAX) + // and that can be enabled / disabled. + QList<unsigned int> indices; + for (const auto& name : names) { + auto modIndex = ModInfo::getIndex(name); + if (modIndex != UINT_MAX) { + indices.append(modIndex); + } + else { + log::debug("Trying to {} mod {} which does not exist.", + active ? "enable" : "disable", name); + } + } + + if (active) { + m_Profile->setModsEnabled(indices, {}); + } + else { + m_Profile->setModsEnabled({}, indices); + } + + return indices.size(); +} + int ModList::priority(const QString &name) const { unsigned int modIndex = ModInfo::getIndex(name); @@ -988,12 +1001,22 @@ bool ModList::setPriority(const QString &name, int newPriority) } } -bool ModList::onModStateChanged(const std::function<void (const QString &, IModList::ModStates)> &func) +bool ModList::onModStateChanged(const std::function<void(const std::map<QString, ModStates>&)>& func) { auto conn = m_ModStateChanged.connect(func); return conn.connected(); } +void ModList::notifyModStateChanged(QList<unsigned int> modIndices) const +{ + std::map<QString, ModStates> mods; + for (auto modIndex : modIndices) { + ModInfo::Ptr modInfo = ModInfo::getByIndex(modIndex); + mods.emplace(modInfo->name(), state(modIndex)); + } + m_ModStateChanged(mods); +} + bool ModList::onModMoved(const std::function<void (const QString &, int, int)> &func) { auto conn = m_ModMoved.connect(func); |
