From c291946d2a8c3e03940da4070533e124283d303c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Mon, 5 Aug 2019 00:44:56 +0200 Subject: NOISSUE do not lose selection on mod enable/disable toggle --- api/logic/FileSystem.cpp | 5 +++ api/logic/minecraft/mod/Mod.cpp | 26 ++--------- api/logic/minecraft/mod/ModFolderModel.cpp | 71 ++++++++++++++++++++---------- api/logic/minecraft/mod/ModFolderModel.h | 9 +++- 4 files changed, 63 insertions(+), 48 deletions(-) (limited to 'api') diff --git a/api/logic/FileSystem.cpp b/api/logic/FileSystem.cpp index 192d868b..49af2927 100644 --- a/api/logic/FileSystem.cpp +++ b/api/logic/FileSystem.cpp @@ -174,6 +174,11 @@ bool copy::operator()(const QString &offset) bool deletePath(QString path) { bool OK = true; + QFileInfo finfo(path); + if(finfo.isFile()) { + return QFile::remove(path); + } + QDir dir(path); if (!dir.exists()) diff --git a/api/logic/minecraft/mod/Mod.cpp b/api/logic/minecraft/mod/Mod.cpp index aa2496c2..df8b406d 100644 --- a/api/logic/minecraft/mod/Mod.cpp +++ b/api/logic/minecraft/mod/Mod.cpp @@ -18,6 +18,7 @@ #include "Mod.h" #include +#include namespace { @@ -100,34 +101,15 @@ bool Mod::enable(bool value) if (!foo.rename(path)) return false; } - m_file = QFileInfo(path); + repath(QFileInfo(path)); m_enabled = value; return true; } bool Mod::destroy() { - if (m_type == MOD_FOLDER) - { - QDir d(m_file.filePath()); - if (d.removeRecursively()) - { - m_type = MOD_UNKNOWN; - return true; - } - return false; - } - else if (m_type == MOD_SINGLEFILE || m_type == MOD_ZIPFILE || m_type == MOD_LITEMOD) - { - QFile f(m_file.filePath()); - if (f.remove()) - { - m_type = MOD_UNKNOWN; - return true; - } - return false; - } - return true; + m_type = MOD_UNKNOWN; + return FS::deletePath(m_file.filePath()); } diff --git a/api/logic/minecraft/mod/ModFolderModel.cpp b/api/logic/minecraft/mod/ModFolderModel.cpp index 14907dea..59ccaaba 100644 --- a/api/logic/minecraft/mod/ModFolderModel.cpp +++ b/api/logic/minecraft/mod/ModFolderModel.cpp @@ -303,7 +303,7 @@ bool ModFolderModel::installMod(const QString &filename) return false; } -bool ModFolderModel::enableMods(const QModelIndexList& indexes, bool enable) +bool ModFolderModel::setModStatus(const QModelIndexList& indexes, ModStatusAction enable) { if(interaction_disabled) { return false; @@ -314,27 +314,14 @@ bool ModFolderModel::enableMods(const QModelIndexList& indexes, bool enable) for (auto index: indexes) { - Mod &m = mods[index.row()]; - m.enable(enable); - emit dataChanged(index, index); + if(index.column() != 0) { + continue; + } + setModStatus(index.row(), enable); } return true; } -void ModFolderModel::toggleEnabled(const QModelIndex& index) -{ - if(interaction_disabled) { - return; - } - if(!index.isValid()) { - return; - } - - Mod &m = mods[index.row()]; - m.enable(!m.enabled()); - emit dataChanged(index, index); -} - bool ModFolderModel::deleteMods(const QModelIndexList& indexes) { if(interaction_disabled) { @@ -418,16 +405,52 @@ bool ModFolderModel::setData(const QModelIndex &index, const QVariant &value, in if (role == Qt::CheckStateRole) { - auto &mod = mods[index.row()]; - if (mod.enable(!mod.enabled())) - { - emit dataChanged(index, index); - return true; - } + return setModStatus(index.row(), Toggle); } return false; } +bool ModFolderModel::setModStatus(int row, ModFolderModel::ModStatusAction action) +{ + if(row < 0 || row >= mods.size()) { + return false; + } + + auto &mod = mods[row]; + bool desiredStatus; + switch(action) { + case Enable: + desiredStatus = true; + break; + case Disable: + desiredStatus = false; + break; + case Toggle: + default: + desiredStatus = !mod.enabled(); + break; + } + + if(desiredStatus == mod.enabled()) { + return true; + } + + // preserve the row, but change its ID + auto oldId = mod.mmc_id(); + if(!mod.enable(!mod.enabled())) { + return false; + } + auto newId = mod.mmc_id(); + if(modsIndex.contains(newId)) { + // NOTE: this could handle a corner case, where we are overwriting a file, because the same 'mod' exists both enabled and disabled + // But is it necessary? + } + modsIndex.remove(oldId); + modsIndex[newId] = row; + emit dataChanged(index(row, 0), index(row, columnCount(QModelIndex()) - 1)); + return true; +} + QVariant ModFolderModel::headerData(int section, Qt::Orientation orientation, int role) const { switch (role) diff --git a/api/logic/minecraft/mod/ModFolderModel.h b/api/logic/minecraft/mod/ModFolderModel.h index 624345be..8394e405 100644 --- a/api/logic/minecraft/mod/ModFolderModel.h +++ b/api/logic/minecraft/mod/ModFolderModel.h @@ -48,6 +48,11 @@ public: DateColumn, NUM_COLUMNS }; + enum ModStatusAction { + Disable, + Enable, + Toggle + }; ModFolderModel(const QString &dir); virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; @@ -93,8 +98,7 @@ public: bool deleteMods(const QModelIndexList &indexes); /// Enable or disable listed mods - bool enableMods(const QModelIndexList &indexes, bool enable = true); - void toggleEnabled(const QModelIndex &index); + bool setModStatus(const QModelIndexList &indexes, ModStatusAction action); void startWatching(); void stopWatching(); @@ -125,6 +129,7 @@ signals: private: void resolveMod(Mod& m); + bool setModStatus(int index, ModStatusAction action); protected: QFileSystemWatcher *m_watcher; -- cgit v1.2.3