From 6b0007291bf8fd094300c21eb64cc119fb3a378d Mon Sep 17 00:00:00 2001 From: QazCetelic Date: Tue, 10 Sep 2024 22:30:29 +0200 Subject: [PATCH 01/14] Added search and support for subdirectories to icon picker Signed-off-by: QazCetelic --- launcher/icons/IconList.cpp | 142 ++++++++++++++++++----- launcher/icons/IconList.h | 4 + launcher/ui/dialogs/IconPickerDialog.cpp | 23 +++- launcher/ui/dialogs/IconPickerDialog.h | 5 + 4 files changed, 144 insertions(+), 30 deletions(-) diff --git a/launcher/icons/IconList.cpp b/launcher/icons/IconList.cpp index e4157ea2d..56ff10e51 100644 --- a/launcher/icons/IconList.cpp +++ b/launcher/icons/IconList.cpp @@ -56,7 +56,7 @@ IconList::IconList(const QStringList& builtinPaths, QString path, QObject* paren QDir instance_icons(builtinPath); auto file_info_list = instance_icons.entryInfoList(QDir::Files, QDir::Name); for (auto file_info : file_info_list) { - builtinNames.insert(file_info.completeBaseName()); + builtinNames.insert(file_info.baseName()); } } for (auto& builtinName : builtinNames) { @@ -77,10 +77,95 @@ IconList::IconList(const QStringList& builtinPaths, QString path, QObject* paren void IconList::sortIconList() { qDebug() << "Sorting icon list..."; - std::sort(icons.begin(), icons.end(), [](const MMCIcon& a, const MMCIcon& b) { return a.m_key.localeAwareCompare(b.m_key) < 0; }); + std::sort(icons.begin(), icons.end(), [](const MMCIcon& a, const MMCIcon& b) { + bool aIsSubdir = a.m_key.contains(std::filesystem::path::preferred_separator); + bool bIsSubdir = b.m_key.contains(std::filesystem::path::preferred_separator); + if (aIsSubdir != bIsSubdir) { + return !aIsSubdir; // root-level icons come first + } + return a.m_key.localeAwareCompare(b.m_key) < 0; + }); reindex(); } +// Helper function to add directories recursively +bool IconList::addPathRecursively(const QString& path) +{ + QDir dir(path); + if (!dir.exists()) + return false; + + bool watching = false; + + // Add the directory itself + watching = m_watcher->addPath(path); + + // Add all subdirectories + QFileInfoList entries = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot); + for (const QFileInfo& entry : entries) + { + if (addPathRecursively(entry.absoluteFilePath())) + { + watching = true; + } + } + return watching; +} + +void IconList::removePathRecursively(const QString& path) +{ + QFileInfo file_info(path); + if (file_info.isFile()) { + // Remove the icon belonging to the file + QString key = m_dir.relativeFilePath(file_info.absoluteFilePath()); + int idx = getIconIndex(key); + if (idx == -1) + return; + + } + else if (file_info.isDir()) { + // Remove the directory itself + m_watcher->removePath(path); + + const QDir dir(path); + // Remove all files within the directory + for (const QFileInfo& file : dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot)) + { + removePathRecursively(file.absoluteFilePath()); + } + } +} + +QStringList IconList::getIconFilePaths() const +{ + QStringList icon_files {}; + QStringList directories {m_dir.absolutePath()}; + while (!directories.isEmpty()) { + QString first = directories.takeFirst(); + QDir dir(first); + for (QString& file_name : dir.entryList(QDir::AllDirs | QDir::Files |QDir::NoDotAndDotDot, QDir::Name)) { + QString full_path = dir.filePath(file_name); // Convert to full path + QFileInfo file_info(full_path); + if (file_info.isDir()) + directories.push_back(full_path); + else + icon_files.push_back(full_path); + } + } + return icon_files; +} + +QString formatName(const QDir& icons_dir, const QFileInfo& file) +{ + if (file.dir() == icons_dir) { + return file.baseName(); + } + else { + const QString delimiter = " » "; + return (icons_dir.relativeFilePath(file.dir().path()) + std::filesystem::path::preferred_separator + file.baseName()).replace(std::filesystem::path::preferred_separator, delimiter); + } +} + void IconList::directoryChanged(const QString& path) { QDir new_dir(path); @@ -95,13 +180,9 @@ void IconList::directoryChanged(const QString& path) if (!FS::ensureFolderPathExists(m_dir.absolutePath())) return; m_dir.refresh(); - auto new_list = m_dir.entryList(QDir::Files, QDir::Name); - for (auto it = new_list.begin(); it != new_list.end(); it++) { - QString& foo = (*it); - foo = m_dir.filePath(foo); - } + QStringList new_file_names_list = getIconFilePaths(); #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) - QSet new_set(new_list.begin(), new_list.end()); + QSet new_set(new_file_names_list.begin(), new_file_names_list.end()); #else auto new_set = new_list.toSet(); #endif @@ -123,21 +204,16 @@ void IconList::directoryChanged(const QString& path) QSet to_add = new_set; to_add -= current_set; - for (auto remove : to_remove) { + for (const auto& remove : to_remove) { qDebug() << "Removing " << remove; - QFileInfo rmfile(remove); - QString key = rmfile.completeBaseName(); - - QString suffix = rmfile.suffix(); - // The icon doesnt have a suffix, but it can have other .s in the name, so we account for those as well - if (!IconUtils::isIconSuffix(suffix)) - key = rmfile.fileName(); + QFileInfo removed_file(remove); + QString key = m_dir.relativeFilePath(removed_file.absoluteFilePath()); int idx = getIconIndex(key); if (idx == -1) continue; - icons[idx].remove(IconType::FileBased); - if (icons[idx].type() == IconType::ToBeDeleted) { + icons[idx].remove(FileBased); + if (icons[idx].type() == ToBeDeleted) { beginRemoveRows(QModelIndex(), idx, idx); icons.remove(idx); reindex(); @@ -149,18 +225,14 @@ void IconList::directoryChanged(const QString& path) emit iconUpdated(key); } - for (auto add : to_add) { + for (const auto& add : to_add) { qDebug() << "Adding " << add; QFileInfo addfile(add); - QString key = addfile.completeBaseName(); + QString key = m_dir.relativeFilePath(addfile.absoluteFilePath()); + QString name = formatName(m_dir, addfile); - QString suffix = addfile.suffix(); - // The icon doesnt have a suffix, but it can have other .s in the name, so we account for those as well - if (!IconUtils::isIconSuffix(suffix)) - key = addfile.fileName(); - - if (addIcon(key, QString(), addfile.filePath(), IconType::FileBased)) { + if (addIcon(key, name, addfile.filePath(), IconType::FileBased)) { m_watcher->addPath(add); emit iconUpdated(key); } @@ -175,7 +247,7 @@ void IconList::fileChanged(const QString& path) QFileInfo checkfile(path); if (!checkfile.exists()) return; - QString key = checkfile.completeBaseName(); + QString key = m_dir.relativeFilePath(checkfile.absoluteFilePath()); int idx = getIconIndex(key); if (idx == -1) return; @@ -200,7 +272,7 @@ void IconList::startWatching() { auto abs_path = m_dir.absolutePath(); FS::ensureFolderPathExists(abs_path); - is_watching = m_watcher->addPath(abs_path); + is_watching = addPathRecursively(abs_path); if (is_watching) { qDebug() << "Started watching " << abs_path; } else { @@ -312,6 +384,7 @@ bool IconList::iconFileExists(const QString& key) const return iconEntry && iconEntry->has(IconType::FileBased); } +/// Returns the icon with the given key or nullptr if it doesn't exist. const MMCIcon* IconList::icon(const QString& key) const { int iconIdx = getIconIndex(key); @@ -394,6 +467,7 @@ void IconList::reindex() for (auto& iter : icons) { name_index[iter.m_key] = i; i++; + emit iconUpdated(iter.m_key); // prevents incorrect indices with proxy model } } @@ -425,3 +499,15 @@ QString IconList::getDirectory() const { return m_dir.absolutePath(); } + +/// Returns the directory of the icon with the given key or the default directory if it's a builtin icon. +QString IconList::iconDirectory(const QString& key) const +{ + for (auto mmc_icon : icons) { + if (mmc_icon.m_key == key && mmc_icon.has(IconType::FileBased)) { + QFileInfo icon_file(mmc_icon.getFilePath()); + return icon_file.dir().path(); + } + } + return getDirectory(); +} diff --git a/launcher/icons/IconList.h b/launcher/icons/IconList.h index 553946c42..fcd172561 100644 --- a/launcher/icons/IconList.h +++ b/launcher/icons/IconList.h @@ -72,6 +72,7 @@ class IconList : public QAbstractListModel { bool deleteIcon(const QString& key); bool trashIcon(const QString& key); bool iconFileExists(const QString& key) const; + QString iconDirectory(const QString& key) const; void installIcons(const QStringList& iconFiles); void installIcon(const QString& file, const QString& name); @@ -91,6 +92,9 @@ class IconList : public QAbstractListModel { IconList& operator=(const IconList&) = delete; void reindex(); void sortIconList(); + bool addPathRecursively(const QString& path); + void removePathRecursively(const QString& path); + QStringList getIconFilePaths() const; public slots: void directoryChanged(const QString& path); diff --git a/launcher/ui/dialogs/IconPickerDialog.cpp b/launcher/ui/dialogs/IconPickerDialog.cpp index a196fd587..a3f1d7ea4 100644 --- a/launcher/ui/dialogs/IconPickerDialog.cpp +++ b/launcher/ui/dialogs/IconPickerDialog.cpp @@ -16,6 +16,8 @@ #include #include #include +#include +#include #include "Application.h" @@ -33,6 +35,15 @@ IconPickerDialog::IconPickerDialog(QWidget* parent) : QDialog(parent), ui(new Ui ui->setupUi(this); setWindowModality(Qt::WindowModal); + searchBar = new QLineEdit(this); + searchBar->setPlaceholderText(tr("Search...")); + ui->verticalLayout->insertWidget(0, searchBar); + + proxyModel = new QSortFilterProxyModel(this); + proxyModel->setSourceModel(APPLICATION->icons().get()); + proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive); + ui->iconView->setModel(proxyModel); + auto contentsWidget = ui->iconView; contentsWidget->setViewMode(QListView::IconMode); contentsWidget->setFlow(QListView::LeftToRight); @@ -57,7 +68,7 @@ IconPickerDialog::IconPickerDialog(QWidget* parent) : QDialog(parent), ui(new Ui contentsWidget->installEventFilter(this); - contentsWidget->setModel(APPLICATION->icons().get()); + contentsWidget->setModel(proxyModel); // NOTE: ResetRole forces the button to be on the left, while the OK/Cancel ones are on the right. We win. auto buttonAdd = ui->buttonBox->addButton(tr("Add Icon"), QDialogButtonBox::ResetRole); @@ -73,6 +84,9 @@ IconPickerDialog::IconPickerDialog(QWidget* parent) : QDialog(parent), ui(new Ui auto buttonFolder = ui->buttonBox->addButton(tr("Open Folder"), QDialogButtonBox::ResetRole); connect(buttonFolder, &QPushButton::clicked, this, &IconPickerDialog::openFolder); + connect(searchBar, &QLineEdit::textChanged, this, &IconPickerDialog::filterIcons); + // Prevent incorrect indices from e.g. filesystem changes + connect(APPLICATION->icons().get(), &IconList::iconUpdated, this, [this]() { proxyModel->invalidate(); }); } bool IconPickerDialog::eventFilter(QObject* obj, QEvent* evt) @@ -159,5 +173,10 @@ IconPickerDialog::~IconPickerDialog() void IconPickerDialog::openFolder() { - DesktopServices::openPath(APPLICATION->icons()->getDirectory(), true); + DesktopServices::openPath(APPLICATION->icons()->iconDirectory(selectedIconKey), true); } + +void IconPickerDialog::filterIcons(const QString& query) +{ + proxyModel->setFilterFixedString(query); +} \ No newline at end of file diff --git a/launcher/ui/dialogs/IconPickerDialog.h b/launcher/ui/dialogs/IconPickerDialog.h index 37e53dcce..db1315338 100644 --- a/launcher/ui/dialogs/IconPickerDialog.h +++ b/launcher/ui/dialogs/IconPickerDialog.h @@ -16,6 +16,8 @@ #pragma once #include #include +#include +#include namespace Ui { class IconPickerDialog; @@ -36,6 +38,8 @@ class IconPickerDialog : public QDialog { private: Ui::IconPickerDialog* ui; QPushButton* buttonRemove; + QLineEdit* searchBar; + QSortFilterProxyModel* proxyModel; private slots: void selectionChanged(QItemSelection, QItemSelection); @@ -44,4 +48,5 @@ class IconPickerDialog : public QDialog { void addNewIcon(); void removeSelectedIcon(); void openFolder(); + void filterIcons(const QString& text); }; From 40c3866f635bcb0c084f9afc5c9bcec4a50fa036 Mon Sep 17 00:00:00 2001 From: QazCetelic Date: Wed, 11 Sep 2024 09:22:28 +0200 Subject: [PATCH 02/14] clang-tidy formatting Signed-off-by: QazCetelic --- launcher/icons/IconList.cpp | 54 +++++++++++------------- launcher/icons/IconList.h | 4 +- launcher/ui/dialogs/IconPickerDialog.cpp | 2 +- 3 files changed, 28 insertions(+), 32 deletions(-) diff --git a/launcher/icons/IconList.cpp b/launcher/icons/IconList.cpp index 56ff10e51..a9d5bda98 100644 --- a/launcher/icons/IconList.cpp +++ b/launcher/icons/IconList.cpp @@ -47,19 +47,19 @@ #define MAX_SIZE 1024 -IconList::IconList(const QStringList& builtinPaths, QString path, QObject* parent) : QAbstractListModel(parent) +IconList::IconList(const QStringList& builtinPaths, const QString& path, QObject* parent) : QAbstractListModel(parent) { QSet builtinNames; // add builtin icons - for (auto& builtinPath : builtinPaths) { + for (const auto& builtinPath : builtinPaths) { QDir instance_icons(builtinPath); auto file_info_list = instance_icons.entryInfoList(QDir::Files, QDir::Name); - for (auto file_info : file_info_list) { + for (const auto& file_info : file_info_list) { builtinNames.insert(file_info.baseName()); } } - for (auto& builtinName : builtinNames) { + for (const auto& builtinName : builtinNames) { addThemeIcon(builtinName); } @@ -81,7 +81,7 @@ void IconList::sortIconList() bool aIsSubdir = a.m_key.contains(std::filesystem::path::preferred_separator); bool bIsSubdir = b.m_key.contains(std::filesystem::path::preferred_separator); if (aIsSubdir != bIsSubdir) { - return !aIsSubdir; // root-level icons come first + return !aIsSubdir; // root-level icons come first } return a.m_key.localeAwareCompare(b.m_key) < 0; }); @@ -102,10 +102,8 @@ bool IconList::addPathRecursively(const QString& path) // Add all subdirectories QFileInfoList entries = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot); - for (const QFileInfo& entry : entries) - { - if (addPathRecursively(entry.absoluteFilePath())) - { + for (const QFileInfo& entry : entries) { + if (addPathRecursively(entry.absoluteFilePath())) { watching = true; } } @@ -122,15 +120,13 @@ void IconList::removePathRecursively(const QString& path) if (idx == -1) return; - } - else if (file_info.isDir()) { + } else if (file_info.isDir()) { // Remove the directory itself m_watcher->removePath(path); const QDir dir(path); // Remove all files within the directory - for (const QFileInfo& file : dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot)) - { + for (const QFileInfo& file : dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot)) { removePathRecursively(file.absoluteFilePath()); } } @@ -138,13 +134,13 @@ void IconList::removePathRecursively(const QString& path) QStringList IconList::getIconFilePaths() const { - QStringList icon_files {}; - QStringList directories {m_dir.absolutePath()}; + QStringList icon_files{}; + QStringList directories{ m_dir.absolutePath() }; while (!directories.isEmpty()) { QString first = directories.takeFirst(); QDir dir(first); - for (QString& file_name : dir.entryList(QDir::AllDirs | QDir::Files |QDir::NoDotAndDotDot, QDir::Name)) { - QString full_path = dir.filePath(file_name); // Convert to full path + for (QString& file_name : dir.entryList(QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot, QDir::Name)) { + QString full_path = dir.filePath(file_name); // Convert to full path QFileInfo file_info(full_path); if (file_info.isDir()) directories.push_back(full_path); @@ -159,10 +155,10 @@ QString formatName(const QDir& icons_dir, const QFileInfo& file) { if (file.dir() == icons_dir) { return file.baseName(); - } - else { + } else { const QString delimiter = " » "; - return (icons_dir.relativeFilePath(file.dir().path()) + std::filesystem::path::preferred_separator + file.baseName()).replace(std::filesystem::path::preferred_separator, delimiter); + return (icons_dir.relativeFilePath(file.dir().path()) + std::filesystem::path::preferred_separator + file.baseName()) + .replace(std::filesystem::path::preferred_separator, delimiter); } } @@ -252,7 +248,7 @@ void IconList::fileChanged(const QString& path) if (idx == -1) return; QIcon icon(path); - if (!icon.availableSizes().size()) + if (icon.availableSizes().empty()) return; icons[idx].m_images[IconType::FileBased].icon = icon; @@ -260,7 +256,7 @@ void IconList::fileChanged(const QString& path) emit iconUpdated(key); } -void IconList::SettingChanged(const Setting& setting, QVariant value) +void IconList::SettingChanged(const Setting& setting, const QVariant& value) { if (setting.id() != "IconsDir") return; @@ -335,12 +331,12 @@ Qt::ItemFlags IconList::flags(const QModelIndex& index) const QVariant IconList::data(const QModelIndex& index, int role) const { if (!index.isValid()) - return QVariant(); + return {}; int row = index.row(); if (row < 0 || row >= icons.size()) - return QVariant(); + return {}; switch (role) { case Qt::DecorationRole: @@ -350,7 +346,7 @@ QVariant IconList::data(const QModelIndex& index, int role) const case Qt::UserRole: return icons[row].m_key; default: - return QVariant(); + return {}; } } @@ -361,7 +357,7 @@ int IconList::rowCount(const QModelIndex& parent) const void IconList::installIcons(const QStringList& iconFiles) { - for (QString file : iconFiles) + for (const QString& file : iconFiles) installIcon(file, {}); } @@ -467,7 +463,7 @@ void IconList::reindex() for (auto& iter : icons) { name_index[iter.m_key] = i; i++; - emit iconUpdated(iter.m_key); // prevents incorrect indices with proxy model + emit iconUpdated(iter.m_key); // prevents incorrect indices with proxy model } } @@ -483,7 +479,7 @@ QIcon IconList::getIcon(const QString& key) const if (icon_index != -1) return icons[icon_index].icon(); - return QIcon(); + return {}; } int IconList::getIconIndex(const QString& key) const @@ -503,7 +499,7 @@ QString IconList::getDirectory() const /// Returns the directory of the icon with the given key or the default directory if it's a builtin icon. QString IconList::iconDirectory(const QString& key) const { - for (auto mmc_icon : icons) { + for (const auto& mmc_icon : icons) { if (mmc_icon.m_key == key && mmc_icon.has(IconType::FileBased)) { QFileInfo icon_file(mmc_icon.getFilePath()); return icon_file.dir().path(); diff --git a/launcher/icons/IconList.h b/launcher/icons/IconList.h index fcd172561..6213b7da3 100644 --- a/launcher/icons/IconList.h +++ b/launcher/icons/IconList.h @@ -51,7 +51,7 @@ class QFileSystemWatcher; class IconList : public QAbstractListModel { Q_OBJECT public: - explicit IconList(const QStringList& builtinPaths, QString path, QObject* parent = 0); + explicit IconList(const QStringList& builtinPaths, const QString& path, QObject* parent = 0); virtual ~IconList() {}; QIcon getIcon(const QString& key) const; @@ -101,7 +101,7 @@ class IconList : public QAbstractListModel { protected slots: void fileChanged(const QString& path); - void SettingChanged(const Setting& setting, QVariant value); + void SettingChanged(const Setting& setting, const QVariant& value); private: shared_qobject_ptr m_watcher; diff --git a/launcher/ui/dialogs/IconPickerDialog.cpp b/launcher/ui/dialogs/IconPickerDialog.cpp index a3f1d7ea4..80effec07 100644 --- a/launcher/ui/dialogs/IconPickerDialog.cpp +++ b/launcher/ui/dialogs/IconPickerDialog.cpp @@ -15,9 +15,9 @@ #include #include +#include #include #include -#include #include "Application.h" From 0a576a0f6725aa2db1cdad7c153bfac25f2caef7 Mon Sep 17 00:00:00 2001 From: QazCetelic Date: Wed, 11 Sep 2024 09:31:16 +0200 Subject: [PATCH 03/14] fixes for ci errors Signed-off-by: QazCetelic --- launcher/icons/IconList.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/launcher/icons/IconList.cpp b/launcher/icons/IconList.cpp index a9d5bda98..824bee617 100644 --- a/launcher/icons/IconList.cpp +++ b/launcher/icons/IconList.cpp @@ -35,6 +35,7 @@ */ #include "IconList.h" +#include #include #include #include @@ -180,7 +181,7 @@ void IconList::directoryChanged(const QString& path) #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) QSet new_set(new_file_names_list.begin(), new_file_names_list.end()); #else - auto new_set = new_list.toSet(); + auto new_set = new_file_names_list.toSet(); #endif QList current_list; for (auto& it : icons) { From 506ec642733e9c7c11ccb7c72619697c52307687 Mon Sep 17 00:00:00 2001 From: QazCetelic Date: Wed, 11 Sep 2024 09:43:45 +0200 Subject: [PATCH 04/14] fixes for ci errors II Signed-off-by: QazCetelic --- launcher/icons/IconList.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/launcher/icons/IconList.cpp b/launcher/icons/IconList.cpp index 824bee617..66f6ad1f3 100644 --- a/launcher/icons/IconList.cpp +++ b/launcher/icons/IconList.cpp @@ -35,7 +35,6 @@ */ #include "IconList.h" -#include #include #include #include @@ -154,13 +153,13 @@ QStringList IconList::getIconFilePaths() const QString formatName(const QDir& icons_dir, const QFileInfo& file) { - if (file.dir() == icons_dir) { + if (file.dir() == icons_dir) return file.baseName(); - } else { - const QString delimiter = " » "; - return (icons_dir.relativeFilePath(file.dir().path()) + std::filesystem::path::preferred_separator + file.baseName()) - .replace(std::filesystem::path::preferred_separator, delimiter); - } + + constexpr auto delimiter = " » "; + constexpr auto fs_separator = std::filesystem::path::preferred_separator; + QString relative_path_without_extension = icons_dir.relativeFilePath(file.dir().path()) + fs_separator + file.baseName(); + return relative_path_without_extension.replace(fs_separator, delimiter); } void IconList::directoryChanged(const QString& path) @@ -311,7 +310,7 @@ bool IconList::dropMimeData(const QMimeData* data, if (data->hasUrls()) { auto urls = data->urls(); QStringList iconFiles; - for (auto url : urls) { + for (const auto& url : urls) { // only local files may be dropped... if (!url.isLocalFile()) continue; From f7f7c4b43ef2871d71fb3214bcafbd32e77bf85d Mon Sep 17 00:00:00 2001 From: QazCetelic Date: Wed, 11 Sep 2024 09:55:27 +0200 Subject: [PATCH 05/14] fixes for ci errors III Signed-off-by: QazCetelic --- launcher/icons/IconList.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/launcher/icons/IconList.cpp b/launcher/icons/IconList.cpp index 66f6ad1f3..df1905502 100644 --- a/launcher/icons/IconList.cpp +++ b/launcher/icons/IconList.cpp @@ -35,6 +35,7 @@ */ #include "IconList.h" +#include #include #include #include From 7b49fd65085b662875ed673b5319d2155efce5bd Mon Sep 17 00:00:00 2001 From: QazCetelic Date: Wed, 11 Sep 2024 10:07:05 +0200 Subject: [PATCH 06/14] fixes for ci errors IV Signed-off-by: QazCetelic --- launcher/icons/IconList.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/launcher/icons/IconList.cpp b/launcher/icons/IconList.cpp index df1905502..e226ab471 100644 --- a/launcher/icons/IconList.cpp +++ b/launcher/icons/IconList.cpp @@ -35,7 +35,6 @@ */ #include "IconList.h" -#include #include #include #include @@ -79,8 +78,8 @@ void IconList::sortIconList() { qDebug() << "Sorting icon list..."; std::sort(icons.begin(), icons.end(), [](const MMCIcon& a, const MMCIcon& b) { - bool aIsSubdir = a.m_key.contains(std::filesystem::path::preferred_separator); - bool bIsSubdir = b.m_key.contains(std::filesystem::path::preferred_separator); + bool aIsSubdir = a.m_key.contains(QDir::separator()); + bool bIsSubdir = b.m_key.contains(QDir::separator()); if (aIsSubdir != bIsSubdir) { return !aIsSubdir; // root-level icons come first } @@ -158,9 +157,8 @@ QString formatName(const QDir& icons_dir, const QFileInfo& file) return file.baseName(); constexpr auto delimiter = " » "; - constexpr auto fs_separator = std::filesystem::path::preferred_separator; - QString relative_path_without_extension = icons_dir.relativeFilePath(file.dir().path()) + fs_separator + file.baseName(); - return relative_path_without_extension.replace(fs_separator, delimiter); + QString relative_path_without_extension = icons_dir.relativeFilePath(file.dir().path()) + QDir::separator() + file.baseName(); + return relative_path_without_extension.replace(QDir::separator(), delimiter); } void IconList::directoryChanged(const QString& path) From e35faa5522449a8ed65bd855274ce6e54492ba42 Mon Sep 17 00:00:00 2001 From: QazCetelic Date: Sun, 15 Sep 2024 09:04:23 +0200 Subject: [PATCH 07/14] Feedback I Signed-off-by: QazCetelic --- launcher/icons/IconList.cpp | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/launcher/icons/IconList.cpp b/launcher/icons/IconList.cpp index e226ab471..b60e67bec 100644 --- a/launcher/icons/IconList.cpp +++ b/launcher/icons/IconList.cpp @@ -95,10 +95,8 @@ bool IconList::addPathRecursively(const QString& path) if (!dir.exists()) return false; - bool watching = false; - // Add the directory itself - watching = m_watcher->addPath(path); + bool watching = m_watcher->addPath(path); // Add all subdirectories QFileInfoList entries = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot); @@ -134,21 +132,19 @@ void IconList::removePathRecursively(const QString& path) QStringList IconList::getIconFilePaths() const { - QStringList icon_files{}; + QStringList iconFiles{}; QStringList directories{ m_dir.absolutePath() }; while (!directories.isEmpty()) { QString first = directories.takeFirst(); QDir dir(first); - for (QString& file_name : dir.entryList(QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot, QDir::Name)) { - QString full_path = dir.filePath(file_name); // Convert to full path - QFileInfo file_info(full_path); - if (file_info.isDir()) - directories.push_back(full_path); + for (QFileInfo& fileInfo : dir.entryInfoList(QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot, QDir::Name)) { + if (fileInfo.isDir()) + directories.push_back(fileInfo.absoluteFilePath()); else - icon_files.push_back(full_path); + iconFiles.push_back(fileInfo.absoluteFilePath()); } } - return icon_files; + return iconFiles; } QString formatName(const QDir& icons_dir, const QFileInfo& file) @@ -157,8 +153,8 @@ QString formatName(const QDir& icons_dir, const QFileInfo& file) return file.baseName(); constexpr auto delimiter = " » "; - QString relative_path_without_extension = icons_dir.relativeFilePath(file.dir().path()) + QDir::separator() + file.baseName(); - return relative_path_without_extension.replace(QDir::separator(), delimiter); + QString relativePathWithoutExtension = icons_dir.relativeFilePath(file.dir().path()) + QDir::separator() + file.baseName(); + return relativePathWithoutExtension.replace(QDir::separator(), delimiter); } void IconList::directoryChanged(const QString& path) From a58e81d7441ad5887e0bb3d7f7b3ab1887115341 Mon Sep 17 00:00:00 2001 From: QazCetelic Date: Tue, 24 Sep 2024 09:58:47 +0200 Subject: [PATCH 08/14] snake_case to camelCase Signed-off-by: QazCetelic --- launcher/icons/IconList.cpp | 38 ++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/launcher/icons/IconList.cpp b/launcher/icons/IconList.cpp index b60e67bec..016affe29 100644 --- a/launcher/icons/IconList.cpp +++ b/launcher/icons/IconList.cpp @@ -147,13 +147,13 @@ QStringList IconList::getIconFilePaths() const return iconFiles; } -QString formatName(const QDir& icons_dir, const QFileInfo& file) +QString formatName(const QDir& iconsDir, const QFileInfo& file) { - if (file.dir() == icons_dir) + if (file.dir() == iconsDir) return file.baseName(); constexpr auto delimiter = " » "; - QString relativePathWithoutExtension = icons_dir.relativeFilePath(file.dir().path()) + QDir::separator() + file.baseName(); + QString relativePathWithoutExtension = iconsDir.relativeFilePath(file.dir().path()) + QDir::separator() + file.baseName(); return relativePathWithoutExtension.replace(QDir::separator(), delimiter); } @@ -171,9 +171,9 @@ void IconList::directoryChanged(const QString& path) if (!FS::ensureFolderPathExists(m_dir.absolutePath())) return; m_dir.refresh(); - QStringList new_file_names_list = getIconFilePaths(); + QStringList newFileNamesList = getIconFilePaths(); #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) - QSet new_set(new_file_names_list.begin(), new_file_names_list.end()); + QSet new_set(newFileNamesList.begin(), newFileNamesList.end()); #else auto new_set = new_file_names_list.toSet(); #endif @@ -192,13 +192,13 @@ void IconList::directoryChanged(const QString& path) QSet to_remove = current_set; to_remove -= new_set; - QSet to_add = new_set; - to_add -= current_set; + QSet toAdd = new_set; + toAdd -= current_set; for (const auto& remove : to_remove) { qDebug() << "Removing " << remove; - QFileInfo removed_file(remove); - QString key = m_dir.relativeFilePath(removed_file.absoluteFilePath()); + QFileInfo removedFile(remove); + QString key = m_dir.relativeFilePath(removedFile.absoluteFilePath()); int idx = getIconIndex(key); if (idx == -1) @@ -216,7 +216,7 @@ void IconList::directoryChanged(const QString& path) emit iconUpdated(key); } - for (const auto& add : to_add) { + for (const auto& add : toAdd) { qDebug() << "Adding " << add; QFileInfo addfile(add); @@ -464,16 +464,16 @@ void IconList::reindex() QIcon IconList::getIcon(const QString& key) const { - int icon_index = getIconIndex(key); + int iconIndex = getIconIndex(key); - if (icon_index != -1) - return icons[icon_index].icon(); + if (iconIndex != -1) + return icons[iconIndex].icon(); // Fallback for icons that don't exist. - icon_index = getIconIndex("grass"); + iconIndex = getIconIndex("grass"); - if (icon_index != -1) - return icons[icon_index].icon(); + if (iconIndex != -1) + return icons[iconIndex].icon(); return {}; } @@ -494,9 +494,9 @@ QString IconList::getDirectory() const /// Returns the directory of the icon with the given key or the default directory if it's a builtin icon. QString IconList::iconDirectory(const QString& key) const { - for (const auto& mmc_icon : icons) { - if (mmc_icon.m_key == key && mmc_icon.has(IconType::FileBased)) { - QFileInfo icon_file(mmc_icon.getFilePath()); + for (const auto& mmcIcon : icons) { + if (mmcIcon.m_key == key && mmcIcon.has(IconType::FileBased)) { + QFileInfo icon_file(mmcIcon.getFilePath()); return icon_file.dir().path(); } } From b675406b1ad3b56e67ee56605d2f85a8f0947246 Mon Sep 17 00:00:00 2001 From: QazCetelic Date: Tue, 24 Sep 2024 10:16:26 +0200 Subject: [PATCH 09/14] Split set creation into a separate function for readability Signed-off-by: QazCetelic --- launcher/icons/IconList.cpp | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/launcher/icons/IconList.cpp b/launcher/icons/IconList.cpp index 016affe29..0903131ce 100644 --- a/launcher/icons/IconList.cpp +++ b/launcher/icons/IconList.cpp @@ -157,6 +157,16 @@ QString formatName(const QDir& iconsDir, const QFileInfo& file) return relativePathWithoutExtension.replace(QDir::separator(), delimiter); } +QSet toStringSet(const QList& list) // Split into a separate function because the preprocessing impedes readability +{ +#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) + QSet set(list.begin(), list.end()); +#else + auto set = list.toSet(); +#endif + return set; +} + void IconList::directoryChanged(const QString& path) { QDir new_dir(path); @@ -171,31 +181,23 @@ void IconList::directoryChanged(const QString& path) if (!FS::ensureFolderPathExists(m_dir.absolutePath())) return; m_dir.refresh(); - QStringList newFileNamesList = getIconFilePaths(); -#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) - QSet new_set(newFileNamesList.begin(), newFileNamesList.end()); -#else - auto new_set = new_file_names_list.toSet(); -#endif + const QStringList newFileNamesList = getIconFilePaths(); + const QSet newSet = toStringSet(newFileNamesList); QList current_list; for (auto& it : icons) { if (!it.has(IconType::FileBased)) continue; current_list.push_back(it.m_images[IconType::FileBased].filename); } -#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) - QSet current_set(current_list.begin(), current_list.end()); -#else - QSet current_set = current_list.toSet(); -#endif + const QSet currentSet = toStringSet(current_list); - QSet to_remove = current_set; - to_remove -= new_set; + QSet toRemove = currentSet; + toRemove -= newSet; - QSet toAdd = new_set; - toAdd -= current_set; + QSet toAdd = newSet; + toAdd -= currentSet; - for (const auto& remove : to_remove) { + for (const auto& remove : toRemove) { qDebug() << "Removing " << remove; QFileInfo removedFile(remove); QString key = m_dir.relativeFilePath(removedFile.absoluteFilePath()); From f641f3acda6e96ad18dd24e16eee62e50e7bb29b Mon Sep 17 00:00:00 2001 From: QazCetelic Date: Tue, 24 Sep 2024 12:32:49 +0200 Subject: [PATCH 10/14] Refactoring Signed-off-by: QazCetelic --- launcher/icons/IconList.cpp | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/launcher/icons/IconList.cpp b/launcher/icons/IconList.cpp index 0903131ce..0edc97681 100644 --- a/launcher/icons/IconList.cpp +++ b/launcher/icons/IconList.cpp @@ -169,8 +169,8 @@ QSet toStringSet(const QList& list) // Split into a separate f void IconList::directoryChanged(const QString& path) { - QDir new_dir(path); - if (m_dir.absolutePath() != new_dir.absolutePath()) { + QDir newDir(path); + if (m_dir.absolutePath() != newDir.absolutePath()) { m_dir.setPath(path); m_dir.refresh(); if (is_watching) @@ -183,19 +183,14 @@ void IconList::directoryChanged(const QString& path) m_dir.refresh(); const QStringList newFileNamesList = getIconFilePaths(); const QSet newSet = toStringSet(newFileNamesList); - QList current_list; - for (auto& it : icons) { + QSet currentSet; + for (const MMCIcon& it : icons) { if (!it.has(IconType::FileBased)) continue; - current_list.push_back(it.m_images[IconType::FileBased].filename); + currentSet.insert(it.m_images[IconType::FileBased].filename); } - const QSet currentSet = toStringSet(current_list); - - QSet toRemove = currentSet; - toRemove -= newSet; - - QSet toAdd = newSet; - toAdd -= currentSet; + QSet toRemove = currentSet - newSet; + QSet toAdd = newSet - currentSet; for (const auto& remove : toRemove) { qDebug() << "Removing " << remove; @@ -456,11 +451,9 @@ void IconList::saveIcon(const QString& key, const QString& path, const char* for void IconList::reindex() { name_index.clear(); - int i = 0; - for (auto& iter : icons) { - name_index[iter.m_key] = i; - i++; - emit iconUpdated(iter.m_key); // prevents incorrect indices with proxy model + for (int i = 0; i < icons.size(); i++) { + name_index[icons[i].m_key] = i; + emit iconUpdated(icons[i].m_key); // prevents incorrect indices with proxy model } } @@ -471,7 +464,7 @@ QIcon IconList::getIcon(const QString& key) const if (iconIndex != -1) return icons[iconIndex].icon(); - // Fallback for icons that don't exist. + // Fallback for icons that don't exist.b iconIndex = getIconIndex("grass"); if (iconIndex != -1) From abbebff400a697023f159c2f00087031a470b577 Mon Sep 17 00:00:00 2001 From: QazCetelic Date: Sun, 17 Nov 2024 20:35:24 +0100 Subject: [PATCH 11/14] Removed bug when renaming icon file in nested folder while application is running Signed-off-by: QazCetelic --- launcher/icons/IconList.cpp | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/launcher/icons/IconList.cpp b/launcher/icons/IconList.cpp index 0edc97681..20a907ed3 100644 --- a/launcher/icons/IconList.cpp +++ b/launcher/icons/IconList.cpp @@ -147,22 +147,23 @@ QStringList IconList::getIconFilePaths() const return iconFiles; } -QString formatName(const QDir& iconsDir, const QFileInfo& file) +QString formatName(const QDir& iconsDir, const QFileInfo& iconFile) { - if (file.dir() == iconsDir) - return file.baseName(); + if (iconFile.dir() == iconsDir) + return iconFile.baseName(); constexpr auto delimiter = " » "; - QString relativePathWithoutExtension = iconsDir.relativeFilePath(file.dir().path()) + QDir::separator() + file.baseName(); + QString relativePathWithoutExtension = iconsDir.relativeFilePath(iconFile.dir().path()) + QDir::separator() + iconFile.baseName(); return relativePathWithoutExtension.replace(QDir::separator(), delimiter); } -QSet toStringSet(const QList& list) // Split into a separate function because the preprocessing impedes readability +/// Split into a separate function because the preprocessing impedes readability +QSet toStringSet(const QList& list) { #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) QSet set(list.begin(), list.end()); #else - auto set = list.toSet(); + QSet set = list.toSet(); #endif return set; } @@ -171,7 +172,8 @@ void IconList::directoryChanged(const QString& path) { QDir newDir(path); if (m_dir.absolutePath() != newDir.absolutePath()) { - m_dir.setPath(path); + if (!path.startsWith(m_dir.absolutePath())) + m_dir.setPath(path); m_dir.refresh(); if (is_watching) stopWatching(); @@ -192,9 +194,9 @@ void IconList::directoryChanged(const QString& path) QSet toRemove = currentSet - newSet; QSet toAdd = newSet - currentSet; - for (const auto& remove : toRemove) { - qDebug() << "Removing " << remove; - QFileInfo removedFile(remove); + for (const QString& removedPath : toRemove) { + qDebug() << "Removing icon " << removedPath; + QFileInfo removedFile(removedPath); QString key = m_dir.relativeFilePath(removedFile.absoluteFilePath()); int idx = getIconIndex(key); @@ -209,19 +211,19 @@ void IconList::directoryChanged(const QString& path) } else { dataChanged(index(idx), index(idx)); } - m_watcher->removePath(remove); + m_watcher->removePath(removedPath); emit iconUpdated(key); } - for (const auto& add : toAdd) { - qDebug() << "Adding " << add; + for (const QString& addedPath : toAdd) { + qDebug() << "Adding icon " << addedPath; - QFileInfo addfile(add); + QFileInfo addfile(addedPath); QString key = m_dir.relativeFilePath(addfile.absoluteFilePath()); QString name = formatName(m_dir, addfile); if (addIcon(key, name, addfile.filePath(), IconType::FileBased)) { - m_watcher->addPath(add); + m_watcher->addPath(addedPath); emit iconUpdated(key); } } @@ -231,7 +233,7 @@ void IconList::directoryChanged(const QString& path) void IconList::fileChanged(const QString& path) { - qDebug() << "Checking " << path; + qDebug() << "Checking icon " << path; QFileInfo checkfile(path); if (!checkfile.exists()) return; From e74592fa0968dce50e4bdc4cd6b06d0b3de06eca Mon Sep 17 00:00:00 2001 From: QazCetelic Date: Sun, 17 Nov 2024 20:43:09 +0100 Subject: [PATCH 12/14] Code style conventions (camelCase, m_ prefix) Signed-off-by: QazCetelic --- launcher/icons/IconList.cpp | 84 ++++++++++++++++++------------------- launcher/icons/IconList.h | 6 +-- 2 files changed, 45 insertions(+), 45 deletions(-) diff --git a/launcher/icons/IconList.cpp b/launcher/icons/IconList.cpp index 20a907ed3..9cfab903a 100644 --- a/launcher/icons/IconList.cpp +++ b/launcher/icons/IconList.cpp @@ -53,10 +53,10 @@ IconList::IconList(const QStringList& builtinPaths, const QString& path, QObject // add builtin icons for (const auto& builtinPath : builtinPaths) { - QDir instance_icons(builtinPath); - auto file_info_list = instance_icons.entryInfoList(QDir::Files, QDir::Name); - for (const auto& file_info : file_info_list) { - builtinNames.insert(file_info.baseName()); + QDir instanceIcons(builtinPath); + auto fileInfoList = instanceIcons.entryInfoList(QDir::Files, QDir::Name); + for (const auto& fileInfo : fileInfoList) { + builtinNames.insert(fileInfo.baseName()); } } for (const auto& builtinName : builtinNames) { @@ -64,7 +64,7 @@ IconList::IconList(const QStringList& builtinPaths, const QString& path, QObject } m_watcher.reset(new QFileSystemWatcher()); - is_watching = false; + m_is_watching = false; connect(m_watcher.get(), &QFileSystemWatcher::directoryChanged, this, &IconList::directoryChanged); connect(m_watcher.get(), &QFileSystemWatcher::fileChanged, this, &IconList::fileChanged); @@ -77,7 +77,7 @@ IconList::IconList(const QStringList& builtinPaths, const QString& path, QObject void IconList::sortIconList() { qDebug() << "Sorting icon list..."; - std::sort(icons.begin(), icons.end(), [](const MMCIcon& a, const MMCIcon& b) { + std::sort(m_icons.begin(), m_icons.end(), [](const MMCIcon& a, const MMCIcon& b) { bool aIsSubdir = a.m_key.contains(QDir::separator()); bool bIsSubdir = b.m_key.contains(QDir::separator()); if (aIsSubdir != bIsSubdir) { @@ -175,7 +175,7 @@ void IconList::directoryChanged(const QString& path) if (!path.startsWith(m_dir.absolutePath())) m_dir.setPath(path); m_dir.refresh(); - if (is_watching) + if (m_is_watching) stopWatching(); startWatching(); } @@ -186,7 +186,7 @@ void IconList::directoryChanged(const QString& path) const QStringList newFileNamesList = getIconFilePaths(); const QSet newSet = toStringSet(newFileNamesList); QSet currentSet; - for (const MMCIcon& it : icons) { + for (const MMCIcon& it : m_icons) { if (!it.has(IconType::FileBased)) continue; currentSet.insert(it.m_images[IconType::FileBased].filename); @@ -202,10 +202,10 @@ void IconList::directoryChanged(const QString& path) int idx = getIconIndex(key); if (idx == -1) continue; - icons[idx].remove(FileBased); - if (icons[idx].type() == ToBeDeleted) { + m_icons[idx].remove(FileBased); + if (m_icons[idx].type() == ToBeDeleted) { beginRemoveRows(QModelIndex(), idx, idx); - icons.remove(idx); + m_icons.remove(idx); reindex(); endRemoveRows(); } else { @@ -245,7 +245,7 @@ void IconList::fileChanged(const QString& path) if (icon.availableSizes().empty()) return; - icons[idx].m_images[IconType::FileBased].icon = icon; + m_icons[idx].m_images[IconType::FileBased].icon = icon; dataChanged(index(idx), index(idx)); emit iconUpdated(key); } @@ -262,8 +262,8 @@ void IconList::startWatching() { auto abs_path = m_dir.absolutePath(); FS::ensureFolderPathExists(abs_path); - is_watching = addPathRecursively(abs_path); - if (is_watching) { + m_is_watching = addPathRecursively(abs_path); + if (m_is_watching) { qDebug() << "Started watching " << abs_path; } else { qDebug() << "Failed to start watching " << abs_path; @@ -274,7 +274,7 @@ void IconList::stopWatching() { m_watcher->removePaths(m_watcher->files()); m_watcher->removePaths(m_watcher->directories()); - is_watching = false; + m_is_watching = false; } QStringList IconList::mimeTypes() const @@ -329,16 +329,16 @@ QVariant IconList::data(const QModelIndex& index, int role) const int row = index.row(); - if (row < 0 || row >= icons.size()) + if (row < 0 || row >= m_icons.size()) return {}; switch (role) { case Qt::DecorationRole: - return icons[row].icon(); + return m_icons[row].icon(); case Qt::DisplayRole: - return icons[row].name(); + return m_icons[row].name(); case Qt::UserRole: - return icons[row].m_key; + return m_icons[row].m_key; default: return {}; } @@ -346,7 +346,7 @@ QVariant IconList::data(const QModelIndex& index, int role) const int IconList::rowCount(const QModelIndex& parent) const { - return parent.isValid() ? 0 : icons.size(); + return parent.isValid() ? 0 : m_icons.size(); } void IconList::installIcons(const QStringList& iconFiles) @@ -380,7 +380,7 @@ const MMCIcon* IconList::icon(const QString& key) const int iconIdx = getIconIndex(key); if (iconIdx == -1) return nullptr; - return &icons[iconIdx]; + return &m_icons[iconIdx]; } bool IconList::deleteIcon(const QString& key) @@ -395,22 +395,22 @@ bool IconList::trashIcon(const QString& key) bool IconList::addThemeIcon(const QString& key) { - auto iter = name_index.find(key); - if (iter != name_index.end()) { - auto& oldOne = icons[*iter]; + auto iter = m_name_index.find(key); + if (iter != m_name_index.end()) { + auto& oldOne = m_icons[*iter]; oldOne.replace(Builtin, key); dataChanged(index(*iter), index(*iter)); return true; } // add a new icon - beginInsertRows(QModelIndex(), icons.size(), icons.size()); + beginInsertRows(QModelIndex(), m_icons.size(), m_icons.size()); { MMCIcon mmc_icon; mmc_icon.m_name = key; mmc_icon.m_key = key; mmc_icon.replace(Builtin, key); - icons.push_back(mmc_icon); - name_index[key] = icons.size() - 1; + m_icons.push_back(mmc_icon); + m_name_index[key] = m_icons.size() - 1; } endInsertRows(); return true; @@ -422,22 +422,22 @@ bool IconList::addIcon(const QString& key, const QString& name, const QString& p QIcon icon(path); if (icon.isNull()) return false; - auto iter = name_index.find(key); - if (iter != name_index.end()) { - auto& oldOne = icons[*iter]; + auto iter = m_name_index.find(key); + if (iter != m_name_index.end()) { + auto& oldOne = m_icons[*iter]; oldOne.replace(type, icon, path); dataChanged(index(*iter), index(*iter)); return true; } // add a new icon - beginInsertRows(QModelIndex(), icons.size(), icons.size()); + beginInsertRows(QModelIndex(), m_icons.size(), m_icons.size()); { MMCIcon mmc_icon; mmc_icon.m_name = name; mmc_icon.m_key = key; mmc_icon.replace(type, icon, path); - icons.push_back(mmc_icon); - name_index[key] = icons.size() - 1; + m_icons.push_back(mmc_icon); + m_name_index[key] = m_icons.size() - 1; } endInsertRows(); return true; @@ -452,10 +452,10 @@ void IconList::saveIcon(const QString& key, const QString& path, const char* for void IconList::reindex() { - name_index.clear(); - for (int i = 0; i < icons.size(); i++) { - name_index[icons[i].m_key] = i; - emit iconUpdated(icons[i].m_key); // prevents incorrect indices with proxy model + m_name_index.clear(); + for (int i = 0; i < m_icons.size(); i++) { + m_name_index[m_icons[i].m_key] = i; + emit iconUpdated(m_icons[i].m_key); // prevents incorrect indices with proxy model } } @@ -464,20 +464,20 @@ QIcon IconList::getIcon(const QString& key) const int iconIndex = getIconIndex(key); if (iconIndex != -1) - return icons[iconIndex].icon(); + return m_icons[iconIndex].icon(); // Fallback for icons that don't exist.b iconIndex = getIconIndex("grass"); if (iconIndex != -1) - return icons[iconIndex].icon(); + return m_icons[iconIndex].icon(); return {}; } int IconList::getIconIndex(const QString& key) const { - auto iter = name_index.find(key == "default" ? "grass" : key); - if (iter != name_index.end()) + auto iter = m_name_index.find(key == "default" ? "grass" : key); + if (iter != m_name_index.end()) return *iter; return -1; @@ -491,7 +491,7 @@ QString IconList::getDirectory() const /// Returns the directory of the icon with the given key or the default directory if it's a builtin icon. QString IconList::iconDirectory(const QString& key) const { - for (const auto& mmcIcon : icons) { + for (const auto& mmcIcon : m_icons) { if (mmcIcon.m_key == key && mmcIcon.has(IconType::FileBased)) { QFileInfo icon_file(mmcIcon.getFilePath()); return icon_file.dir().path(); diff --git a/launcher/icons/IconList.h b/launcher/icons/IconList.h index 6213b7da3..04c2738ef 100644 --- a/launcher/icons/IconList.h +++ b/launcher/icons/IconList.h @@ -105,8 +105,8 @@ class IconList : public QAbstractListModel { private: shared_qobject_ptr m_watcher; - bool is_watching; - QMap name_index; - QVector icons; + bool m_is_watching; + QMap m_name_index; + QVector m_icons; QDir m_dir; }; From 6ca18c62660baab20fe6a3e7fd7b6b05a837ce0d Mon Sep 17 00:00:00 2001 From: QazCetelic Date: Mon, 18 Nov 2024 19:19:02 +0100 Subject: [PATCH 13/14] Refactoring Signed-off-by: QazCetelic --- launcher/icons/IconList.cpp | 27 ++------------------------- launcher/icons/IconList.h | 1 - 2 files changed, 2 insertions(+), 26 deletions(-) diff --git a/launcher/icons/IconList.cpp b/launcher/icons/IconList.cpp index 9cfab903a..eb047b130 100644 --- a/launcher/icons/IconList.cpp +++ b/launcher/icons/IconList.cpp @@ -108,28 +108,6 @@ bool IconList::addPathRecursively(const QString& path) return watching; } -void IconList::removePathRecursively(const QString& path) -{ - QFileInfo file_info(path); - if (file_info.isFile()) { - // Remove the icon belonging to the file - QString key = m_dir.relativeFilePath(file_info.absoluteFilePath()); - int idx = getIconIndex(key); - if (idx == -1) - return; - - } else if (file_info.isDir()) { - // Remove the directory itself - m_watcher->removePath(path); - - const QDir dir(path); - // Remove all files within the directory - for (const QFileInfo& file : dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot)) { - removePathRecursively(file.absoluteFilePath()); - } - } -} - QStringList IconList::getIconFilePaths() const { QStringList iconFiles{}; @@ -179,9 +157,8 @@ void IconList::directoryChanged(const QString& path) stopWatching(); startWatching(); } - if (!m_dir.exists()) - if (!FS::ensureFolderPathExists(m_dir.absolutePath())) - return; + if (!m_dir.exists() && !FS::ensureFolderPathExists(m_dir.absolutePath())) + return; m_dir.refresh(); const QStringList newFileNamesList = getIconFilePaths(); const QSet newSet = toStringSet(newFileNamesList); diff --git a/launcher/icons/IconList.h b/launcher/icons/IconList.h index 04c2738ef..ec6b6d60e 100644 --- a/launcher/icons/IconList.h +++ b/launcher/icons/IconList.h @@ -93,7 +93,6 @@ class IconList : public QAbstractListModel { void reindex(); void sortIconList(); bool addPathRecursively(const QString& path); - void removePathRecursively(const QString& path); QStringList getIconFilePaths() const; public slots: From 33ff3b4f361df1429bd6490d0714e7c7bf90f143 Mon Sep 17 00:00:00 2001 From: QazCetelic Date: Sat, 23 Nov 2024 18:10:33 +0100 Subject: [PATCH 14/14] Fix naming Signed-off-by: QazCetelic --- launcher/icons/IconList.cpp | 34 +++++++++++++++++----------------- launcher/icons/IconList.h | 4 ++-- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/launcher/icons/IconList.cpp b/launcher/icons/IconList.cpp index eb047b130..faeb3f2c7 100644 --- a/launcher/icons/IconList.cpp +++ b/launcher/icons/IconList.cpp @@ -64,7 +64,7 @@ IconList::IconList(const QStringList& builtinPaths, const QString& path, QObject } m_watcher.reset(new QFileSystemWatcher()); - m_is_watching = false; + m_isWatching = false; connect(m_watcher.get(), &QFileSystemWatcher::directoryChanged, this, &IconList::directoryChanged); connect(m_watcher.get(), &QFileSystemWatcher::fileChanged, this, &IconList::fileChanged); @@ -153,7 +153,7 @@ void IconList::directoryChanged(const QString& path) if (!path.startsWith(m_dir.absolutePath())) m_dir.setPath(path); m_dir.refresh(); - if (m_is_watching) + if (m_isWatching) stopWatching(); startWatching(); } @@ -239,8 +239,8 @@ void IconList::startWatching() { auto abs_path = m_dir.absolutePath(); FS::ensureFolderPathExists(abs_path); - m_is_watching = addPathRecursively(abs_path); - if (m_is_watching) { + m_isWatching = addPathRecursively(abs_path); + if (m_isWatching) { qDebug() << "Started watching " << abs_path; } else { qDebug() << "Failed to start watching " << abs_path; @@ -251,7 +251,7 @@ void IconList::stopWatching() { m_watcher->removePaths(m_watcher->files()); m_watcher->removePaths(m_watcher->directories()); - m_is_watching = false; + m_isWatching = false; } QStringList IconList::mimeTypes() const @@ -372,8 +372,8 @@ bool IconList::trashIcon(const QString& key) bool IconList::addThemeIcon(const QString& key) { - auto iter = m_name_index.find(key); - if (iter != m_name_index.end()) { + auto iter = m_nameIndex.find(key); + if (iter != m_nameIndex.end()) { auto& oldOne = m_icons[*iter]; oldOne.replace(Builtin, key); dataChanged(index(*iter), index(*iter)); @@ -387,7 +387,7 @@ bool IconList::addThemeIcon(const QString& key) mmc_icon.m_key = key; mmc_icon.replace(Builtin, key); m_icons.push_back(mmc_icon); - m_name_index[key] = m_icons.size() - 1; + m_nameIndex[key] = m_icons.size() - 1; } endInsertRows(); return true; @@ -399,8 +399,8 @@ bool IconList::addIcon(const QString& key, const QString& name, const QString& p QIcon icon(path); if (icon.isNull()) return false; - auto iter = m_name_index.find(key); - if (iter != m_name_index.end()) { + auto iter = m_nameIndex.find(key); + if (iter != m_nameIndex.end()) { auto& oldOne = m_icons[*iter]; oldOne.replace(type, icon, path); dataChanged(index(*iter), index(*iter)); @@ -414,7 +414,7 @@ bool IconList::addIcon(const QString& key, const QString& name, const QString& p mmc_icon.m_key = key; mmc_icon.replace(type, icon, path); m_icons.push_back(mmc_icon); - m_name_index[key] = m_icons.size() - 1; + m_nameIndex[key] = m_icons.size() - 1; } endInsertRows(); return true; @@ -429,9 +429,9 @@ void IconList::saveIcon(const QString& key, const QString& path, const char* for void IconList::reindex() { - m_name_index.clear(); + m_nameIndex.clear(); for (int i = 0; i < m_icons.size(); i++) { - m_name_index[m_icons[i].m_key] = i; + m_nameIndex[m_icons[i].m_key] = i; emit iconUpdated(m_icons[i].m_key); // prevents incorrect indices with proxy model } } @@ -453,8 +453,8 @@ QIcon IconList::getIcon(const QString& key) const int IconList::getIconIndex(const QString& key) const { - auto iter = m_name_index.find(key == "default" ? "grass" : key); - if (iter != m_name_index.end()) + auto iter = m_nameIndex.find(key == "default" ? "grass" : key); + if (iter != m_nameIndex.end()) return *iter; return -1; @@ -470,8 +470,8 @@ QString IconList::iconDirectory(const QString& key) const { for (const auto& mmcIcon : m_icons) { if (mmcIcon.m_key == key && mmcIcon.has(IconType::FileBased)) { - QFileInfo icon_file(mmcIcon.getFilePath()); - return icon_file.dir().path(); + QFileInfo iconFile(mmcIcon.getFilePath()); + return iconFile.dir().path(); } } return getDirectory(); diff --git a/launcher/icons/IconList.h b/launcher/icons/IconList.h index ec6b6d60e..8936195c3 100644 --- a/launcher/icons/IconList.h +++ b/launcher/icons/IconList.h @@ -104,8 +104,8 @@ class IconList : public QAbstractListModel { private: shared_qobject_ptr m_watcher; - bool m_is_watching; - QMap m_name_index; + bool m_isWatching; + QMap m_nameIndex; QVector m_icons; QDir m_dir; };