From 32b3ed0a1362a4b0798ad71fac3450fb77cb7e41 Mon Sep 17 00:00:00 2001 From: Thomas Groman Date: Thu, 19 Sep 2019 00:41:48 -0700 Subject: merged from 0.6.7 codebase --- api/logic/icons/IIconList.h | 24 ++++++++--------- api/logic/icons/IconUtils.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++ api/logic/icons/IconUtils.h | 14 ++++++++++ 3 files changed, 88 insertions(+), 12 deletions(-) create mode 100644 api/logic/icons/IconUtils.cpp create mode 100644 api/logic/icons/IconUtils.h (limited to 'api/logic/icons') diff --git a/api/logic/icons/IIconList.h b/api/logic/icons/IIconList.h index e6c16d50..9a3fe022 100644 --- a/api/logic/icons/IIconList.h +++ b/api/logic/icons/IIconList.h @@ -6,21 +6,21 @@ enum IconType : unsigned { - Builtin, - Transient, - FileBased, - ICONS_TOTAL, - ToBeDeleted + Builtin, + Transient, + FileBased, + ICONS_TOTAL, + ToBeDeleted }; class MULTIMC_LOGIC_EXPORT IIconList { public: - virtual ~IIconList(); - virtual bool addIcon(const QString &key, const QString &name, const QString &path, const IconType type) = 0; - virtual bool deleteIcon(const QString &key) = 0; - virtual void saveIcon(const QString &key, const QString &path, const char * format) const = 0; - virtual bool iconFileExists(const QString &key) const = 0; - virtual void installIcons(const QStringList &iconFiles) = 0; - virtual void installIcon(const QString &file, const QString &name) = 0; + virtual ~IIconList(); + virtual bool addIcon(const QString &key, const QString &name, const QString &path, const IconType type) = 0; + virtual bool deleteIcon(const QString &key) = 0; + virtual void saveIcon(const QString &key, const QString &path, const char * format) const = 0; + virtual bool iconFileExists(const QString &key) const = 0; + virtual void installIcons(const QStringList &iconFiles) = 0; + virtual void installIcon(const QString &file, const QString &name) = 0; }; diff --git a/api/logic/icons/IconUtils.cpp b/api/logic/icons/IconUtils.cpp new file mode 100644 index 00000000..bf530c16 --- /dev/null +++ b/api/logic/icons/IconUtils.cpp @@ -0,0 +1,62 @@ +#include "IconUtils.h" + +#include "FileSystem.h" +#include + +#include + +namespace { +std::array validIconExtensions = {{ + "svg", + "png", + "ico", + "gif", + "jpg", + "jpeg" +}}; +} + +namespace IconUtils{ + +QString findBestIconIn(const QString &folder, const QString & iconKey) { + int best_found = validIconExtensions.size(); + QString best_filename; + + QDirIterator it(folder, QDir::NoDotAndDotDot | QDir::Files, QDirIterator::NoIteratorFlags); + while (it.hasNext()) { + it.next(); + auto fileInfo = it.fileInfo(); + + if(fileInfo.completeBaseName() != iconKey) + continue; + + auto extension = fileInfo.suffix(); + + for(int i = 0; i < best_found; i++) { + if(extension == validIconExtensions[i]) { + best_found = i; + qDebug() << i << " : " << fileInfo.fileName(); + best_filename = fileInfo.fileName(); + } + } + } + return FS::PathCombine(folder, best_filename); +} + +QString getIconFilter() { + QString out; + QTextStream stream(&out); + stream << '('; + for(size_t i = 0; i < validIconExtensions.size() - 1; i++) { + if(i > 0) { + stream << " "; + } + stream << "*." << validIconExtensions[i]; + } + stream << " *." << validIconExtensions[validIconExtensions.size() - 1]; + stream << ')'; + return out; +} + +} + diff --git a/api/logic/icons/IconUtils.h b/api/logic/icons/IconUtils.h new file mode 100644 index 00000000..ce236946 --- /dev/null +++ b/api/logic/icons/IconUtils.h @@ -0,0 +1,14 @@ +#pragma once + +#include +#include "multimc_logic_export.h" + +namespace IconUtils { + +// Given a folder and an icon key, find 'best' of the icons with the given key in there and return its path +MULTIMC_LOGIC_EXPORT QString findBestIconIn(const QString &folder, const QString & iconKey); + +// Get icon file type filter for file browser dialogs +MULTIMC_LOGIC_EXPORT QString getIconFilter(); + +} -- cgit v1.2.3