From 600a4b3b14db8d7610757c852876641c49612864 Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Sat, 29 Aug 2020 12:48:17 +0200 Subject: Add a bulk-version of ModList::setActive. --- src/modlist.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'src/modlist.cpp') diff --git a/src/modlist.cpp b/src/modlist.cpp index cb08c6f4..b093fd88 100644 --- a/src/modlist.cpp +++ b/src/modlist.cpp @@ -962,6 +962,40 @@ bool ModList::setActive(const QString &name, bool active) } } +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 indices; + for (const auto& name : names) { + auto modIndex = ModInfo::getIndex(name); + if (modIndex != UINT_MAX) { + + // This check is not done by the bulk Profile::setModsEnabled, so we + // do it here. + ModInfo::Ptr modInfo = ModInfo::getByIndex(modIndex); + if (!modInfo->alwaysEnabled()) { + indices.append(modIndex); + } + } + } + + if (active) { + m_Profile->setModsEnabled(indices, {}); + } + else { + m_Profile->setModsEnabled({}, indices); + } + + // Notify callbacks: + for (auto modIndex : indices) { + IModList::ModStates newState = state(modIndex); + m_ModStateChanged(ModInfo::getByIndex(modIndex)->name(), newState); + } + + return indices.size(); +} + int ModList::priority(const QString &name) const { unsigned int modIndex = ModInfo::getIndex(name); -- cgit v1.3.1 From e421837ba00b7a6cbe9ace477836f067dca58828 Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Sat, 29 Aug 2020 13:35:58 +0200 Subject: Remove check for alwaysEnabled() and log missing mods in ModList::setActive(). --- src/modlist.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/modlist.cpp') diff --git a/src/modlist.cpp b/src/modlist.cpp index b093fd88..218604b2 100644 --- a/src/modlist.cpp +++ b/src/modlist.cpp @@ -952,6 +952,8 @@ 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); @@ -970,13 +972,11 @@ int ModList::setActive(const QStringList& names, bool active) { for (const auto& name : names) { auto modIndex = ModInfo::getIndex(name); if (modIndex != UINT_MAX) { - - // This check is not done by the bulk Profile::setModsEnabled, so we - // do it here. - ModInfo::Ptr modInfo = ModInfo::getByIndex(modIndex); - if (!modInfo->alwaysEnabled()) { - indices.append(modIndex); - } + indices.append(modIndex); + } + else { + log::debug("Trying to {} mod {} which does not exist.", + active ? "enable" : "disable", name); } } -- cgit v1.3.1 From 97e9e7cf2be5f8f24f62aa25d4669eda885e8567 Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Sat, 29 Aug 2020 15:20:46 +0200 Subject: Refactor the way m_ModStateChanged works. --- src/modlist.cpp | 28 +++++++--------------------- src/modlist.h | 8 ++++++++ src/organizercore.cpp | 5 +++++ 3 files changed, 20 insertions(+), 21 deletions(-) (limited to 'src/modlist.cpp') diff --git a/src/modlist.cpp b/src/modlist.cpp index 218604b2..c772f5bd 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; } @@ -957,9 +945,6 @@ bool ModList::setActive(const QString &name, bool active) return false; } else { m_Profile->setModEnabled(modIndex, active); - - IModList::ModStates newState = state(modIndex); - m_ModStateChanged(name, newState); return true; } } @@ -987,12 +972,6 @@ int ModList::setActive(const QStringList& names, bool active) { m_Profile->setModsEnabled({}, indices); } - // Notify callbacks: - for (auto modIndex : indices) { - IModList::ModStates newState = state(modIndex); - m_ModStateChanged(ModInfo::getByIndex(modIndex)->name(), newState); - } - return indices.size(); } @@ -1028,6 +1007,13 @@ bool ModList::onModStateChanged(const std::function modIndices) const { + for (auto modIndex : modIndices) { + ModInfo::Ptr modInfo = ModInfo::getByIndex(modIndex); + m_ModStateChanged(modInfo->name(), state(modIndex)); + } +} + bool ModList::onModMoved(const std::function &func) { auto conn = m_ModMoved.connect(func); diff --git a/src/modlist.h b/src/modlist.h index 22eb7962..1d16339f 100644 --- a/src/modlist.h +++ b/src/modlist.h @@ -124,6 +124,14 @@ public: void highlightMods(const QItemSelectionModel *selection, const MOShared::DirectoryEntry &directoryEntry); + /** + * @brief Notify the most list that the state of the specified mods has changed. This is used + * to notify the plugin that registered through onModStateChanged(). + * + * @param modIndices Indices of the mods that changed. + */ + void notifyModStateChanged(QList modIndices) const; + public: /// \copydoc MOBase::IModList::displayName diff --git a/src/organizercore.cpp b/src/organizercore.cpp index 28bb06db..815c80c2 100644 --- a/src/organizercore.cpp +++ b/src/organizercore.cpp @@ -1552,6 +1552,9 @@ void OrganizerCore::modStatusChanged(unsigned int index) m_DirectoryStructure->getFileRegister()->sortOrigins(); refreshLists(); + + m_ModList.notifyModStateChanged({ index }); + } catch (const std::exception &e) { reportError(tr("failed to update mod list: %1").arg(e.what())); } @@ -1601,6 +1604,8 @@ void OrganizerCore::modStatusChanged(QList index) { m_DirectoryStructure->getFileRegister()->sortOrigins(); refreshLists(); + + m_ModList.notifyModStateChanged(index); } catch (const std::exception &e) { reportError(tr("failed to update mod list: %1").arg(e.what())); } -- cgit v1.3.1 From 3729f18af1ee2a84756eb729e2f2b8343c51c24a Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Sat, 29 Aug 2020 19:35:41 +0200 Subject: Update for change to onModStateChanged. --- src/modlist.cpp | 11 +++++++---- src/modlist.h | 4 ++-- 2 files changed, 9 insertions(+), 6 deletions(-) (limited to 'src/modlist.cpp') diff --git a/src/modlist.cpp b/src/modlist.cpp index c772f5bd..cbd9f30c 100644 --- a/src/modlist.cpp +++ b/src/modlist.cpp @@ -822,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()); @@ -1001,17 +1001,20 @@ bool ModList::setPriority(const QString &name, int newPriority) } } -bool ModList::onModStateChanged(const std::function &func) +bool ModList::onModStateChanged(const std::function&)>& func) { auto conn = m_ModStateChanged.connect(func); return conn.connected(); } -void ModList::notifyModStateChanged(QList modIndices) const { +void ModList::notifyModStateChanged(QList modIndices) const +{ + std::map mods; for (auto modIndex : modIndices) { ModInfo::Ptr modInfo = ModInfo::getByIndex(modIndex); - m_ModStateChanged(modInfo->name(), state(modIndex)); + mods.emplace(modInfo->name(), state(modIndex)); } + m_ModStateChanged(mods); } bool ModList::onModMoved(const std::function &func) diff --git a/src/modlist.h b/src/modlist.h index a054677b..5312db34 100644 --- a/src/modlist.h +++ b/src/modlist.h @@ -70,7 +70,7 @@ public: COL_LASTCOLUMN = COL_NOTES, }; - typedef boost::signals2::signal SignalModStateChanged; + typedef boost::signals2::signal&)> SignalModStateChanged; typedef boost::signals2::signal SignalModMoved; public: @@ -156,7 +156,7 @@ public: virtual bool setPriority(const QString &name, int newPriority) override; /// \copydoc MOBase::IModList::onModStateChanged - virtual bool onModStateChanged(const std::function &func) override; + virtual bool onModStateChanged(const std::function&)>& func) override; /// \copydoc MOBase::IModList::onModMoved virtual bool onModMoved(const std::function &func) override; -- cgit v1.3.1