fix toggle action

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2025-07-24 18:42:48 +03:00
parent 15a5e99a8a
commit 1346783c5e
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318

View File

@ -38,6 +38,7 @@
#include "ModFolderModel.h" #include "ModFolderModel.h"
#include <FileSystem.h> #include <FileSystem.h>
#include <qabstractitemmodel.h>
#include <QDebug> #include <QDebug>
#include <QFileSystemWatcher> #include <QFileSystemWatcher>
#include <QHeaderView> #include <QHeaderView>
@ -311,6 +312,29 @@ void ModFolderModel::onParseFinished()
emit dataChanged(index(row), index(row, columnCount(QModelIndex()) - 1)); emit dataChanged(index(row), index(row, columnCount(QModelIndex()) - 1));
} }
} }
QList<Mod*> collectMods(QList<Mod*> mods, QHash<QString, QSet<Mod*>> relation, std::set<QString>& seen)
{
QList<Mod*> affectedList = {};
for (auto mod : mods) {
auto id = mod->mod_id();
if (seen.count(id) == 0) {
seen.insert(id);
for (auto affected : relation[id]) {
auto affectedId = affected->mod_id();
if (findById(mods, affectedId) == nullptr && seen.count(affectedId) == 0) {
seen.insert(affectedId);
affectedList << affected;
}
}
}
}
// collect the affected mods until all of them are included in the list
if (!affectedList.isEmpty()) {
affectedList += collectMods(affectedList, relation, seen);
}
return affectedList;
}
QModelIndexList ModFolderModel::getAffectedMods(const QModelIndexList& indexes, EnableAction action) QModelIndexList ModFolderModel::getAffectedMods(const QModelIndexList& indexes, EnableAction action)
{ {
@ -318,54 +342,84 @@ QModelIndexList ModFolderModel::getAffectedMods(const QModelIndexList& indexes,
return {}; return {};
QModelIndexList affectedList = {}; QModelIndexList affectedList = {};
auto indexedMods = selectedMods(indexes); auto affectedMods = selectedMods(indexes);
if (action == EnableAction::TOGGLE) {
if (indexedMods.length() != 1) {
return {}; // not sure how to handle a bunch of rows that are toggled(not even sure it is posible)
}
action = indexedMods.first()->enabled() ? EnableAction::DISABLE : EnableAction::ENABLE;
}
std::set<QString> seen; std::set<QString> seen;
bool shouldBeEnabled = action == EnableAction::ENABLE;
for (auto mod : indexedMods) {
auto id = mod->mod_id();
QSet<Mod*> mods;
switch (action) {
case EnableAction::DISABLE: {
mods = m_requiredBy[id];
break;
}
case EnableAction::ENABLE: {
mods = m_requires[id];
break;
}
case EnableAction::TOGGLE:
break;
}
for (auto affected : mods) {
auto affectedId = affected->mod_id();
if (findById(indexedMods, affectedId) == nullptr && seen.count(affectedId) == 0) { switch (action) {
seen.insert(affectedId); case EnableAction::ENABLE: {
if (shouldBeEnabled != affected->enabled()) { affectedMods << collectMods(affectedMods, m_requires, seen);
auto row = m_resources_index[affected->internal_id()]; break;
affectedList << index(row, 0); }
} case EnableAction::DISABLE: {
} affectedMods << collectMods(affectedMods, m_requiredBy, seen);
break;
}
case EnableAction::TOGGLE: {
return {}; // this function should not be called with TOGGLE
} }
} }
// collect the affected mods until all of them are included in the list bool shouldBeEnabled = action == EnableAction::ENABLE;
if (!affectedList.isEmpty()) { for (auto affected : affectedMods) {
affectedList += getAffectedMods(indexes + affectedList, action); auto affectedId = affected->mod_id();
if (shouldBeEnabled != affected->enabled()) {
auto row = m_resources_index[affected->internal_id()];
affectedList << index(row, 0);
}
} }
return affectedList; return affectedList;
} }
bool ModFolderModel::setResourceEnabled(const QModelIndexList& indexes, EnableAction action) bool ModFolderModel::setResourceEnabled(const QModelIndexList& indexes, EnableAction action)
{ {
auto affected = getAffectedMods(indexes, action); if (indexes.isEmpty())
return ResourceFolderModel::setResourceEnabled(indexes + affected, action); return {};
QModelIndexList affectedList = {};
auto indexedMods = selectedMods(indexes);
QList<Mod*> toEnable = {};
QList<Mod*> toDisable = {};
std::set<QString> seen;
switch (action) {
case EnableAction::ENABLE: {
toEnable = indexedMods;
break;
}
case EnableAction::DISABLE: {
toDisable = indexedMods;
break;
}
case EnableAction::TOGGLE: {
for (auto mod : indexedMods) {
if (mod->enabled()) {
toDisable << mod;
} else {
toEnable << mod;
}
}
break;
}
}
toEnable << collectMods(toEnable, m_requires, seen);
toDisable << collectMods(toDisable, m_requiredBy, seen);
toDisable.removeIf([toEnable](Mod* m) { return toEnable.contains(m); });
auto toList = [this](QList<Mod*> mods, bool shouldBeEnabled) {
QModelIndexList list;
for (auto mod : mods) {
if (shouldBeEnabled != mod->enabled()) {
auto row = m_resources_index[mod->internal_id()];
list << index(row, 0);
}
}
return list;
};
auto disableStatus = ResourceFolderModel::setResourceEnabled(toList(toDisable, false), EnableAction::DISABLE);
auto enableStatus = ResourceFolderModel::setResourceEnabled(toList(toEnable, true), EnableAction::ENABLE);
return disableStatus && enableStatus;
} }
QStringList reqToList(QSet<Mod*> l) QStringList reqToList(QSet<Mod*> l)