diff options
author | flow <flowlnlnln@gmail.com> | 2022-08-09 12:56:38 -0300 |
---|---|---|
committer | flow <flowlnlnln@gmail.com> | 2022-08-20 10:46:33 -0300 |
commit | af2cf2734da211d443b3046fb0f9733f101d9d9d (patch) | |
tree | 2f9ff8707d781a8bbd1f30c5a5ccec3422481e8e /launcher/minecraft | |
parent | ec62d8e97334d3b5a30cea00858e7035468f3609 (diff) | |
download | PrismLauncher-af2cf2734da211d443b3046fb0f9733f101d9d9d.tar.gz PrismLauncher-af2cf2734da211d443b3046fb0f9733f101d9d9d.tar.bz2 PrismLauncher-af2cf2734da211d443b3046fb0f9733f101d9d9d.zip |
refactor: move things around in the mod model
Makes the method order in the cpp file the same as in the header file.
Signed-off-by: flow <flowlnlnln@gmail.com>
Diffstat (limited to 'launcher/minecraft')
-rw-r--r-- | launcher/minecraft/mod/ModFolderModel.cpp | 346 | ||||
-rw-r--r-- | launcher/minecraft/mod/ModFolderModel.h | 5 |
2 files changed, 175 insertions, 176 deletions
diff --git a/launcher/minecraft/mod/ModFolderModel.cpp b/launcher/minecraft/mod/ModFolderModel.cpp index 597f9807..8ab60413 100644 --- a/launcher/minecraft/mod/ModFolderModel.cpp +++ b/launcher/minecraft/mod/ModFolderModel.cpp @@ -54,6 +54,113 @@ ModFolderModel::ModFolderModel(const QString &dir, bool is_indexed) : ResourceFo FS::ensureFolderPathExists(m_dir.absolutePath()); } +QVariant ModFolderModel::data(const QModelIndex &index, int role) const +{ + if (!validateIndex(index)) + return {}; + + int row = index.row(); + int column = index.column(); + + switch (role) + { + case Qt::DisplayRole: + switch (column) + { + case NameColumn: + return m_resources[row]->name(); + case VersionColumn: { + switch(m_resources[row]->type()) { + case ResourceType::FOLDER: + return tr("Folder"); + case ResourceType::SINGLEFILE: + return tr("File"); + default: + break; + } + return at(row)->version(); + } + case DateColumn: + return m_resources[row]->dateTimeChanged(); + + default: + return QVariant(); + } + + case Qt::ToolTipRole: + return m_resources[row]->internal_id(); + + case Qt::CheckStateRole: + switch (column) + { + case ActiveColumn: + return at(row)->enabled() ? Qt::Checked : Qt::Unchecked; + default: + return QVariant(); + } + default: + return QVariant(); + } +} + +bool ModFolderModel::setData(const QModelIndex &index, const QVariant &value, int role) +{ + if (index.row() < 0 || index.row() >= rowCount(index) || !index.isValid()) + { + return false; + } + + if (role == Qt::CheckStateRole) + { + return setModStatus(index.row(), Toggle); + } + return false; +} + +QVariant ModFolderModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + switch (role) + { + case Qt::DisplayRole: + switch (section) + { + case ActiveColumn: + return QString(); + case NameColumn: + return tr("Name"); + case VersionColumn: + return tr("Version"); + case DateColumn: + return tr("Last changed"); + default: + return QVariant(); + } + + case Qt::ToolTipRole: + switch (section) + { + case ActiveColumn: + return tr("Is the mod enabled?"); + case NameColumn: + return tr("The name of the mod."); + case VersionColumn: + return tr("The version of the mod."); + case DateColumn: + return tr("The date and time this mod was last changed (or added)."); + default: + return QVariant(); + } + default: + return QVariant(); + } + return QVariant(); +} + +int ModFolderModel::columnCount(const QModelIndex &parent) const +{ + return NUM_COLUMNS; +} + Task* ModFolderModel::createUpdateTask() { auto index_dir = indexDir(); @@ -62,6 +169,50 @@ Task* ModFolderModel::createUpdateTask() return task; } +Task* ModFolderModel::createParseTask(Resource const& resource) +{ + return new LocalModParseTask(m_next_resolution_ticket, resource.type(), resource.fileinfo()); +} + +bool ModFolderModel::uninstallMod(const QString& filename, bool preserve_metadata) +{ + for(auto mod : allMods()){ + if(mod->fileinfo().fileName() == filename){ + auto index_dir = indexDir(); + mod->destroy(index_dir, preserve_metadata); + return true; + } + } + + return false; +} + +bool ModFolderModel::deleteMods(const QModelIndexList& indexes) +{ + if(!m_can_interact) { + return false; + } + + if(indexes.isEmpty()) + return true; + + for (auto i: indexes) + { + if(i.column() != 0) { + continue; + } + auto m = at(i.row()); + auto index_dir = indexDir(); + m->destroy(index_dir); + } + return true; +} + +bool ModFolderModel::isValid() +{ + return m_dir.exists() && m_dir.isReadable(); +} + void ModFolderModel::startWatching() { // Remove orphaned metadata next time @@ -74,6 +225,28 @@ void ModFolderModel::stopWatching() ResourceFolderModel::stopWatching({ m_dir.absolutePath(), indexDir().absolutePath() }); } +auto ModFolderModel::selectedMods(QModelIndexList& indexes) -> QList<Mod::Ptr> +{ + QList<Mod::Ptr> selected_resources; + for (auto i : indexes) { + if(i.column() != 0) + continue; + + selected_resources.push_back(at(i.row())); + } + return selected_resources; +} + +auto ModFolderModel::allMods() -> QList<Mod::Ptr> +{ + QList<Mod::Ptr> mods; + + for (auto res : m_resources) + mods.append(static_cast<Mod*>(res.get())); + + return mods; +} + void ModFolderModel::onUpdateSucceeded() { auto update_results = static_cast<ModFolderLoadTask*>(m_current_update_task.get())->result(); @@ -104,11 +277,6 @@ void ModFolderModel::onUpdateSucceeded() } } -Task* ModFolderModel::createParseTask(Resource const& resource) -{ - return new LocalModParseTask(m_next_resolution_ticket, resource.type(), resource.fileinfo()); -} - void ModFolderModel::onParseSucceeded(int ticket, QString mod_id) { auto iter = m_active_parse_tasks.constFind(ticket); @@ -135,46 +303,6 @@ void ModFolderModel::onParseSucceeded(int ticket, QString mod_id) } -bool ModFolderModel::isValid() -{ - return m_dir.exists() && m_dir.isReadable(); -} - -auto ModFolderModel::selectedMods(QModelIndexList& indexes) -> QList<Mod::Ptr> -{ - QList<Mod::Ptr> selected_resources; - for (auto i : indexes) { - if(i.column() != 0) - continue; - - selected_resources.push_back(at(i.row())); - } - return selected_resources; -} - -auto ModFolderModel::allMods() -> QList<Mod::Ptr> -{ - QList<Mod::Ptr> mods; - - for (auto res : m_resources) - mods.append(static_cast<Mod*>(res.get())); - - return mods; -} - -bool ModFolderModel::uninstallMod(const QString& filename, bool preserve_metadata) -{ - for(auto mod : allMods()){ - if(mod->fileinfo().fileName() == filename){ - auto index_dir = indexDir(); - mod->destroy(index_dir, preserve_metadata); - return true; - } - } - - return false; -} - bool ModFolderModel::setModStatus(const QModelIndexList& indexes, ModStatusAction enable) { if(!m_can_interact) { @@ -194,95 +322,6 @@ bool ModFolderModel::setModStatus(const QModelIndexList& indexes, ModStatusActio return true; } -bool ModFolderModel::deleteMods(const QModelIndexList& indexes) -{ - if(!m_can_interact) { - return false; - } - - if(indexes.isEmpty()) - return true; - - for (auto i: indexes) - { - if(i.column() != 0) { - continue; - } - auto m = at(i.row()); - auto index_dir = indexDir(); - m->destroy(index_dir); - } - return true; -} - -int ModFolderModel::columnCount(const QModelIndex &parent) const -{ - return NUM_COLUMNS; -} - -QVariant ModFolderModel::data(const QModelIndex &index, int role) const -{ - if (!validateIndex(index)) - return {}; - - int row = index.row(); - int column = index.column(); - - switch (role) - { - case Qt::DisplayRole: - switch (column) - { - case NameColumn: - return m_resources[row]->name(); - case VersionColumn: { - switch(m_resources[row]->type()) { - case ResourceType::FOLDER: - return tr("Folder"); - case ResourceType::SINGLEFILE: - return tr("File"); - default: - break; - } - return at(row)->version(); - } - case DateColumn: - return m_resources[row]->dateTimeChanged(); - - default: - return QVariant(); - } - - case Qt::ToolTipRole: - return m_resources[row]->internal_id(); - - case Qt::CheckStateRole: - switch (column) - { - case ActiveColumn: - return at(row)->enabled() ? Qt::Checked : Qt::Unchecked; - default: - return QVariant(); - } - default: - return QVariant(); - } -} - -bool ModFolderModel::setData(const QModelIndex &index, const QVariant &value, int role) -{ - if (index.row() < 0 || index.row() >= rowCount(index) || !index.isValid()) - { - return false; - } - - if (role == Qt::CheckStateRole) - { - return setModStatus(index.row(), Toggle); - } - return false; -} - bool ModFolderModel::setModStatus(int row, ModFolderModel::ModStatusAction action) { if(row < 0 || row >= m_resources.size()) { @@ -324,42 +363,3 @@ bool ModFolderModel::setModStatus(int row, ModFolderModel::ModStatusAction actio return true; } -QVariant ModFolderModel::headerData(int section, Qt::Orientation orientation, int role) const -{ - switch (role) - { - case Qt::DisplayRole: - switch (section) - { - case ActiveColumn: - return QString(); - case NameColumn: - return tr("Name"); - case VersionColumn: - return tr("Version"); - case DateColumn: - return tr("Last changed"); - default: - return QVariant(); - } - - case Qt::ToolTipRole: - switch (section) - { - case ActiveColumn: - return tr("Is the mod enabled?"); - case NameColumn: - return tr("The name of the mod."); - case VersionColumn: - return tr("The version of the mod."); - case DateColumn: - return tr("The date and time this mod was last changed (or added)."); - default: - return QVariant(); - } - default: - return QVariant(); - } - return QVariant(); -} - diff --git a/launcher/minecraft/mod/ModFolderModel.h b/launcher/minecraft/mod/ModFolderModel.h index a90457d5..ea9f0000 100644 --- a/launcher/minecraft/mod/ModFolderModel.h +++ b/launcher/minecraft/mod/ModFolderModel.h @@ -85,15 +85,14 @@ public: [[nodiscard]] Task* createUpdateTask() override; [[nodiscard]] Task* createParseTask(Resource const&) override; - // Alias for old code, consider those deprecated and don't use in new code :gun: bool installMod(QString file_path) { return ResourceFolderModel::installResource(file_path); } - void disableInteraction(bool disabled) { ResourceFolderModel::enableInteraction(!disabled); } - bool uninstallMod(const QString& filename, bool preserve_metadata = false); /// Deletes all the selected mods bool deleteMods(const QModelIndexList &indexes); + void disableInteraction(bool disabled) { ResourceFolderModel::enableInteraction(!disabled); } + /// Enable or disable listed mods bool setModStatus(const QModelIndexList &indexes, ModStatusAction action); |