@@ -303,7 +303,7 @@ bool ModFolderModel::installMod(const QString &filename)
303303 return false ;
304304}
305305
306- bool ModFolderModel::enableMods (const QModelIndexList& indexes, bool enable)
306+ bool ModFolderModel::setModStatus (const QModelIndexList& indexes, ModStatusAction enable)
307307{
308308 if (interaction_disabled) {
309309 return false ;
@@ -314,27 +314,14 @@ bool ModFolderModel::enableMods(const QModelIndexList& indexes, bool enable)
314314
315315 for (auto index: indexes)
316316 {
317- Mod &m = mods[index.row ()];
318- m.enable (enable);
319- emit dataChanged (index, index);
317+ if (index.column () != 0 ) {
318+ continue ;
319+ }
320+ setModStatus (index.row (), enable);
320321 }
321322 return true ;
322323}
323324
324- void ModFolderModel::toggleEnabled (const QModelIndex& index)
325- {
326- if (interaction_disabled) {
327- return ;
328- }
329- if (!index.isValid ()) {
330- return ;
331- }
332-
333- Mod &m = mods[index.row ()];
334- m.enable (!m.enabled ());
335- emit dataChanged (index, index);
336- }
337-
338325bool ModFolderModel::deleteMods (const QModelIndexList& indexes)
339326{
340327 if (interaction_disabled) {
@@ -418,16 +405,52 @@ bool ModFolderModel::setData(const QModelIndex &index, const QVariant &value, in
418405
419406 if (role == Qt::CheckStateRole)
420407 {
421- auto &mod = mods[index.row ()];
422- if (mod.enable (!mod.enabled ()))
423- {
424- emit dataChanged (index, index);
425- return true ;
426- }
408+ return setModStatus (index.row (), Toggle);
427409 }
428410 return false ;
429411}
430412
413+ bool ModFolderModel::setModStatus (int row, ModFolderModel::ModStatusAction action)
414+ {
415+ if (row < 0 || row >= mods.size ()) {
416+ return false ;
417+ }
418+
419+ auto &mod = mods[row];
420+ bool desiredStatus;
421+ switch (action) {
422+ case Enable:
423+ desiredStatus = true ;
424+ break ;
425+ case Disable:
426+ desiredStatus = false ;
427+ break ;
428+ case Toggle:
429+ default :
430+ desiredStatus = !mod.enabled ();
431+ break ;
432+ }
433+
434+ if (desiredStatus == mod.enabled ()) {
435+ return true ;
436+ }
437+
438+ // preserve the row, but change its ID
439+ auto oldId = mod.mmc_id ();
440+ if (!mod.enable (!mod.enabled ())) {
441+ return false ;
442+ }
443+ auto newId = mod.mmc_id ();
444+ if (modsIndex.contains (newId)) {
445+ // NOTE: this could handle a corner case, where we are overwriting a file, because the same 'mod' exists both enabled and disabled
446+ // But is it necessary?
447+ }
448+ modsIndex.remove (oldId);
449+ modsIndex[newId] = row;
450+ emit dataChanged (index (row, 0 ), index (row, columnCount (QModelIndex ()) - 1 ));
451+ return true ;
452+ }
453+
431454QVariant ModFolderModel::headerData (int section, Qt::Orientation orientation, int role) const
432455{
433456 switch (role)
0 commit comments