Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2023-07-28 01:09:21 +03:00
parent d960effb99
commit d460986de0
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318
6 changed files with 143 additions and 194 deletions

View File

@ -35,32 +35,29 @@
#include "IconList.h" #include "IconList.h"
#include <FileSystem.h> #include <FileSystem.h>
#include <QMap>
#include <QEventLoop>
#include <QMimeData>
#include <QUrl>
#include <QFileSystemWatcher>
#include <QSet>
#include <QDebug> #include <QDebug>
#include <QEventLoop>
#include <QFileSystemWatcher>
#include <QMap>
#include <QMimeData>
#include <QSet>
#include <QUrl>
#define MAX_SIZE 1024 #define MAX_SIZE 1024
IconList::IconList(const QStringList &builtinPaths, QString path, QObject *parent) : QAbstractListModel(parent) IconList::IconList(const QStringList& builtinPaths, QString path, QObject* parent) : QAbstractListModel(parent)
{ {
QSet<QString> builtinNames; QSet<QString> builtinNames;
// add builtin icons // add builtin icons
for(auto & builtinPath: builtinPaths) for (auto& builtinPath : builtinPaths) {
{
QDir instance_icons(builtinPath); QDir instance_icons(builtinPath);
auto file_info_list = instance_icons.entryInfoList(QDir::Files, QDir::Name); auto file_info_list = instance_icons.entryInfoList(QDir::Files, QDir::Name);
for (auto file_info : file_info_list) for (auto file_info : file_info_list) {
{
builtinNames.insert(file_info.completeBaseName()); builtinNames.insert(file_info.completeBaseName());
} }
} }
for(auto & builtinName : builtinNames) for (auto& builtinName : builtinNames) {
{
addThemeIcon(builtinName); addThemeIcon(builtinName);
} }
@ -78,31 +75,27 @@ IconList::IconList(const QStringList &builtinPaths, QString path, QObject *paren
void IconList::sortIconList() void IconList::sortIconList()
{ {
qDebug() << "Sorting icon list..."; qDebug() << "Sorting icon list...";
std::sort(icons.begin(), icons.end(), [](const MMCIcon& a, const MMCIcon& b) { std::sort(icons.begin(), icons.end(), [](const MMCIcon& a, const MMCIcon& b) { return a.m_key.localeAwareCompare(b.m_key) < 0; });
return a.m_key.localeAwareCompare(b.m_key) < 0;
});
reindex(); reindex();
} }
void IconList::directoryChanged(const QString &path) void IconList::directoryChanged(const QString& path)
{ {
QDir new_dir (path); QDir new_dir(path);
if(m_dir.absolutePath() != new_dir.absolutePath()) if (m_dir.absolutePath() != new_dir.absolutePath()) {
{
m_dir.setPath(path); m_dir.setPath(path);
m_dir.refresh(); m_dir.refresh();
if(is_watching) if (is_watching)
stopWatching(); stopWatching();
startWatching(); startWatching();
} }
if(!m_dir.exists()) if (!m_dir.exists())
if(!FS::ensureFolderPathExists(m_dir.absolutePath())) if (!FS::ensureFolderPathExists(m_dir.absolutePath()))
return; return;
m_dir.refresh(); m_dir.refresh();
auto new_list = m_dir.entryList(QDir::Files, QDir::Name); auto new_list = m_dir.entryList(QDir::Files, QDir::Name);
for (auto it = new_list.begin(); it != new_list.end(); it++) for (auto it = new_list.begin(); it != new_list.end(); it++) {
{ QString& foo = (*it);
QString &foo = (*it);
foo = m_dir.filePath(foo); foo = m_dir.filePath(foo);
} }
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
@ -111,8 +104,7 @@ void IconList::directoryChanged(const QString &path)
auto new_set = new_list.toSet(); auto new_set = new_list.toSet();
#endif #endif
QList<QString> current_list; QList<QString> current_list;
for (auto &it : icons) for (auto& it : icons) {
{
if (!it.has(IconType::FileBased)) if (!it.has(IconType::FileBased))
continue; continue;
current_list.push_back(it.m_images[IconType::FileBased].filename); current_list.push_back(it.m_images[IconType::FileBased].filename);
@ -129,8 +121,7 @@ void IconList::directoryChanged(const QString &path)
QSet<QString> to_add = new_set; QSet<QString> to_add = new_set;
to_add -= current_set; to_add -= current_set;
for (auto remove : to_remove) for (auto remove : to_remove) {
{
qDebug() << "Removing " << remove; qDebug() << "Removing " << remove;
QFileInfo rmfile(remove); QFileInfo rmfile(remove);
QString key = rmfile.completeBaseName(); QString key = rmfile.completeBaseName();
@ -144,23 +135,19 @@ void IconList::directoryChanged(const QString &path)
if (idx == -1) if (idx == -1)
continue; continue;
icons[idx].remove(IconType::FileBased); icons[idx].remove(IconType::FileBased);
if (icons[idx].type() == IconType::ToBeDeleted) if (icons[idx].type() == IconType::ToBeDeleted) {
{
beginRemoveRows(QModelIndex(), idx, idx); beginRemoveRows(QModelIndex(), idx, idx);
icons.remove(idx); icons.remove(idx);
reindex(); reindex();
endRemoveRows(); endRemoveRows();
} } else {
else
{
dataChanged(index(idx), index(idx)); dataChanged(index(idx), index(idx));
} }
m_watcher->removePath(remove); m_watcher->removePath(remove);
emit iconUpdated(key); emit iconUpdated(key);
} }
for (auto add : to_add) for (auto add : to_add) {
{
qDebug() << "Adding " << add; qDebug() << "Adding " << add;
QFileInfo addfile(add); QFileInfo addfile(add);
@ -171,8 +158,7 @@ void IconList::directoryChanged(const QString &path)
if (suffix != "jpeg" && suffix != "png" && suffix != "jpg" && suffix != "ico" && suffix != "svg" && suffix != "gif") if (suffix != "jpeg" && suffix != "png" && suffix != "jpg" && suffix != "ico" && suffix != "svg" && suffix != "gif")
key = addfile.fileName(); key = addfile.fileName();
if (addIcon(key, QString(), addfile.filePath(), IconType::FileBased)) if (addIcon(key, QString(), addfile.filePath(), IconType::FileBased)) {
{
m_watcher->addPath(add); m_watcher->addPath(add);
emit iconUpdated(key); emit iconUpdated(key);
} }
@ -181,7 +167,7 @@ void IconList::directoryChanged(const QString &path)
sortIconList(); sortIconList();
} }
void IconList::fileChanged(const QString &path) void IconList::fileChanged(const QString& path)
{ {
qDebug() << "Checking " << path; qDebug() << "Checking " << path;
QFileInfo checkfile(path); QFileInfo checkfile(path);
@ -200,9 +186,9 @@ void IconList::fileChanged(const QString &path)
emit iconUpdated(key); emit iconUpdated(key);
} }
void IconList::SettingChanged(const Setting &setting, QVariant value) void IconList::SettingChanged(const Setting& setting, QVariant value)
{ {
if(setting.id() != "IconsDir") if (setting.id() != "IconsDir")
return; return;
directoryChanged(value.toString()); directoryChanged(value.toString());
@ -213,12 +199,9 @@ void IconList::startWatching()
auto abs_path = m_dir.absolutePath(); auto abs_path = m_dir.absolutePath();
FS::ensureFolderPathExists(abs_path); FS::ensureFolderPathExists(abs_path);
is_watching = m_watcher->addPath(abs_path); is_watching = m_watcher->addPath(abs_path);
if (is_watching) if (is_watching) {
{
qDebug() << "Started watching " << abs_path; qDebug() << "Started watching " << abs_path;
} } else {
else
{
qDebug() << "Failed to start watching " << abs_path; qDebug() << "Failed to start watching " << abs_path;
} }
} }
@ -241,7 +224,11 @@ Qt::DropActions IconList::supportedDropActions() const
return Qt::CopyAction; return Qt::CopyAction;
} }
bool IconList::dropMimeData(const QMimeData *data, Qt::DropAction action, [[maybe_unused]] int row, [[maybe_unused]] int column, [[maybe_unused]] const QModelIndex &parent) bool IconList::dropMimeData(const QMimeData* data,
Qt::DropAction action,
[[maybe_unused]] int row,
[[maybe_unused]] int column,
[[maybe_unused]] const QModelIndex& parent)
{ {
if (action == Qt::IgnoreAction) if (action == Qt::IgnoreAction)
return true; return true;
@ -250,12 +237,10 @@ bool IconList::dropMimeData(const QMimeData *data, Qt::DropAction action, [[mayb
return false; return false;
// files dropped from outside? // files dropped from outside?
if (data->hasUrls()) if (data->hasUrls()) {
{
auto urls = data->urls(); auto urls = data->urls();
QStringList iconFiles; QStringList iconFiles;
for (auto url : urls) for (auto url : urls) {
{
// only local files may be dropped... // only local files may be dropped...
if (!url.isLocalFile()) if (!url.isLocalFile())
continue; continue;
@ -267,7 +252,7 @@ bool IconList::dropMimeData(const QMimeData *data, Qt::DropAction action, [[mayb
return false; return false;
} }
Qt::ItemFlags IconList::flags(const QModelIndex &index) const Qt::ItemFlags IconList::flags(const QModelIndex& index) const
{ {
Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index); Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index);
if (index.isValid()) if (index.isValid())
@ -276,7 +261,7 @@ Qt::ItemFlags IconList::flags(const QModelIndex &index) const
return Qt::ItemIsDropEnabled | defaultFlags; return Qt::ItemIsDropEnabled | defaultFlags;
} }
QVariant IconList::data(const QModelIndex &index, int role) const QVariant IconList::data(const QModelIndex& index, int role) const
{ {
if (!index.isValid()) if (!index.isValid())
return QVariant(); return QVariant();
@ -286,8 +271,7 @@ QVariant IconList::data(const QModelIndex &index, int role) const
if (row < 0 || row >= icons.size()) if (row < 0 || row >= icons.size())
return QVariant(); return QVariant();
switch (role) switch (role) {
{
case Qt::DecorationRole: case Qt::DecorationRole:
return icons[row].icon(); return icons[row].icon();
case Qt::DisplayRole: case Qt::DisplayRole:
@ -299,15 +283,14 @@ QVariant IconList::data(const QModelIndex &index, int role) const
} }
} }
int IconList::rowCount(const QModelIndex &parent) const int IconList::rowCount(const QModelIndex& parent) const
{ {
return parent.isValid() ? 0 : icons.size(); return parent.isValid() ? 0 : icons.size();
} }
void IconList::installIcons(const QStringList &iconFiles) void IconList::installIcons(const QStringList& iconFiles)
{ {
for (QString file : iconFiles) for (QString file : iconFiles) {
{
QFileInfo fileinfo(file); QFileInfo fileinfo(file);
if (!fileinfo.isReadable() || !fileinfo.isFile()) if (!fileinfo.isReadable() || !fileinfo.isFile())
continue; continue;
@ -322,10 +305,10 @@ void IconList::installIcons(const QStringList &iconFiles)
} }
} }
void IconList::installIcon(const QString &file, const QString &name) void IconList::installIcon(const QString& file, const QString& name)
{ {
QFileInfo fileinfo(file); QFileInfo fileinfo(file);
if(!fileinfo.isReadable() || !fileinfo.isFile()) if (!fileinfo.isReadable() || !fileinfo.isFile())
return; return;
QString target = FS::PathCombine(getDirectory(), name); QString target = FS::PathCombine(getDirectory(), name);
@ -333,17 +316,16 @@ void IconList::installIcon(const QString &file, const QString &name)
QFile::copy(file, target); QFile::copy(file, target);
} }
bool IconList::iconFileExists(const QString &key) const bool IconList::iconFileExists(const QString& key) const
{ {
auto iconEntry = icon(key); auto iconEntry = icon(key);
if(!iconEntry) if (!iconEntry) {
{
return false; return false;
} }
return iconEntry->has(IconType::FileBased); return iconEntry->has(IconType::FileBased);
} }
const MMCIcon *IconList::icon(const QString &key) const const MMCIcon* IconList::icon(const QString& key) const
{ {
int iconIdx = getIconIndex(key); int iconIdx = getIconIndex(key);
if (iconIdx == -1) if (iconIdx == -1)
@ -351,7 +333,7 @@ const MMCIcon *IconList::icon(const QString &key) const
return &icons[iconIdx]; return &icons[iconIdx];
} }
bool IconList::deleteIcon(const QString &key) bool IconList::deleteIcon(const QString& key)
{ {
if (!iconFileExists(key)) if (!iconFileExists(key))
return false; return false;
@ -359,7 +341,7 @@ bool IconList::deleteIcon(const QString &key)
return QFile::remove(icon(key)->getFilePath()); return QFile::remove(icon(key)->getFilePath());
} }
bool IconList::trashIcon(const QString &key) bool IconList::trashIcon(const QString& key)
{ {
if (!iconFileExists(key)) if (!iconFileExists(key))
return false; return false;
@ -370,15 +352,12 @@ bool IconList::trashIcon(const QString &key)
bool IconList::addThemeIcon(const QString& key) bool IconList::addThemeIcon(const QString& key)
{ {
auto iter = name_index.find(key); auto iter = name_index.find(key);
if (iter != name_index.end()) if (iter != name_index.end()) {
{ auto& oldOne = icons[*iter];
auto &oldOne = icons[*iter];
oldOne.replace(Builtin, key); oldOne.replace(Builtin, key);
dataChanged(index(*iter), index(*iter)); dataChanged(index(*iter), index(*iter));
return true; return true;
} } else {
else
{
// add a new icon // add a new icon
beginInsertRows(QModelIndex(), icons.size(), icons.size()); beginInsertRows(QModelIndex(), icons.size(), icons.size());
{ {
@ -394,22 +373,19 @@ bool IconList::addThemeIcon(const QString& key)
} }
} }
bool IconList::addIcon(const QString &key, const QString &name, const QString &path, const IconType type) bool IconList::addIcon(const QString& key, const QString& name, const QString& path, const IconType type)
{ {
// replace the icon even? is the input valid? // replace the icon even? is the input valid?
QIcon icon(path); QIcon icon(path);
if (icon.isNull()) if (icon.isNull())
return false; return false;
auto iter = name_index.find(key); auto iter = name_index.find(key);
if (iter != name_index.end()) if (iter != name_index.end()) {
{ auto& oldOne = icons[*iter];
auto &oldOne = icons[*iter];
oldOne.replace(type, icon, path); oldOne.replace(type, icon, path);
dataChanged(index(*iter), index(*iter)); dataChanged(index(*iter), index(*iter));
return true; return true;
} } else {
else
{
// add a new icon // add a new icon
beginInsertRows(QModelIndex(), icons.size(), icons.size()); beginInsertRows(QModelIndex(), icons.size(), icons.size());
{ {
@ -425,26 +401,24 @@ bool IconList::addIcon(const QString &key, const QString &name, const QString &p
} }
} }
void IconList::saveIcon(const QString &key, const QString &path, const char * format) const void IconList::saveIcon(const QString& key, const QString& path, const char* format) const
{ {
auto icon = getIcon(key); auto icon = getIcon(key);
auto pixmap = icon.pixmap(128, 128); auto pixmap = icon.pixmap(128, 128);
pixmap.save(path, format); pixmap.save(path, format);
} }
void IconList::reindex() void IconList::reindex()
{ {
name_index.clear(); name_index.clear();
int i = 0; int i = 0;
for (auto &iter : icons) for (auto& iter : icons) {
{
name_index[iter.m_key] = i; name_index[iter.m_key] = i;
i++; i++;
} }
} }
QIcon IconList::getIcon(const QString &key) const QIcon IconList::getIcon(const QString& key) const
{ {
int icon_index = getIconIndex(key); int icon_index = getIconIndex(key);
@ -459,7 +433,7 @@ QIcon IconList::getIcon(const QString &key) const
return QIcon(); return QIcon();
} }
int IconList::getIconIndex(const QString &key) const int IconList::getIconIndex(const QString& key) const
{ {
auto iter = name_index.find(key == "default" ? "grass" : key); auto iter = name_index.find(key == "default" ? "grass" : key);
if (iter != name_index.end()) if (iter != name_index.end())
@ -473,4 +447,4 @@ QString IconList::getDirectory() const
return m_dir.absolutePath(); return m_dir.absolutePath();
} }
//#include "IconList.moc" // #include "IconList.moc"

View File

@ -15,10 +15,10 @@
#pragma once #pragma once
#include <QMutex>
#include <QAbstractListModel> #include <QAbstractListModel>
#include <QFile>
#include <QDir> #include <QDir>
#include <QFile>
#include <QMutex>
#include <QtGui/QIcon> #include <QtGui/QIcon>
#include <memory> #include <memory>
@ -29,58 +29,58 @@
class QFileSystemWatcher; class QFileSystemWatcher;
class IconList : public QAbstractListModel class IconList : public QAbstractListModel {
{
Q_OBJECT Q_OBJECT
public: public:
explicit IconList(const QStringList &builtinPaths, QString path, QObject *parent = 0); explicit IconList(const QStringList& builtinPaths, QString path, QObject* parent = 0);
virtual ~IconList() {}; virtual ~IconList(){};
QIcon getIcon(const QString &key) const; QIcon getIcon(const QString& key) const;
int getIconIndex(const QString &key) const; int getIconIndex(const QString& key) const;
QString getDirectory() const; QString getDirectory() const;
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override; virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override;
virtual QStringList mimeTypes() const override; virtual QStringList mimeTypes() const override;
virtual Qt::DropActions supportedDropActions() const override; virtual Qt::DropActions supportedDropActions() const override;
virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override; virtual bool dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent) override;
virtual Qt::ItemFlags flags(const QModelIndex &index) const override; virtual Qt::ItemFlags flags(const QModelIndex& index) const override;
bool addThemeIcon(const QString &key); bool addThemeIcon(const QString& key);
bool addIcon(const QString &key, const QString &name, const QString &path, const IconType type); bool addIcon(const QString& key, const QString& name, const QString& path, const IconType type);
void saveIcon(const QString &key, const QString &path, const char * format) const; void saveIcon(const QString& key, const QString& path, const char* format) const;
bool deleteIcon(const QString &key); bool deleteIcon(const QString& key);
bool trashIcon(const QString &key); bool trashIcon(const QString& key);
bool iconFileExists(const QString &key) const; bool iconFileExists(const QString& key) const;
void installIcons(const QStringList &iconFiles); void installIcons(const QStringList& iconFiles);
void installIcon(const QString &file, const QString &name); void installIcon(const QString& file, const QString& name);
const MMCIcon * icon(const QString &key) const; const MMCIcon* icon(const QString& key) const;
void startWatching(); void startWatching();
void stopWatching(); void stopWatching();
signals: signals:
void iconUpdated(QString key); void iconUpdated(QString key);
private: private:
// hide copy constructor // hide copy constructor
IconList(const IconList &) = delete; IconList(const IconList&) = delete;
// hide assign op // hide assign op
IconList &operator=(const IconList &) = delete; IconList& operator=(const IconList&) = delete;
void reindex(); void reindex();
void sortIconList(); void sortIconList();
public slots: public slots:
void directoryChanged(const QString &path); void directoryChanged(const QString& path);
protected slots: protected slots:
void fileChanged(const QString &path); void fileChanged(const QString& path);
void SettingChanged(const Setting & setting, QVariant value); void SettingChanged(const Setting& setting, QVariant value);
private:
private:
shared_qobject_ptr<QFileSystemWatcher> m_watcher; shared_qobject_ptr<QFileSystemWatcher> m_watcher;
bool is_watching; bool is_watching;
QMap<QString, int> name_index; QMap<QString, int> name_index;

View File

@ -1,24 +1,18 @@
#include "IconUtils.h" #include "IconUtils.h"
#include "FileSystem.h"
#include <QDirIterator> #include <QDirIterator>
#include "FileSystem.h"
#include <array> #include <array>
namespace { namespace {
std::array<const char *, 6> validIconExtensions = {{ std::array<const char*, 6> validIconExtensions = { { "svg", "png", "ico", "gif", "jpg", "jpeg" } };
"svg",
"png",
"ico",
"gif",
"jpg",
"jpeg"
}};
} }
namespace IconUtils{ namespace IconUtils {
QString findBestIconIn(const QString &folder, const QString & iconKey) { QString findBestIconIn(const QString& folder, const QString& iconKey)
{
int best_found = validIconExtensions.size(); int best_found = validIconExtensions.size();
QString best_filename; QString best_filename;
@ -27,13 +21,13 @@ QString findBestIconIn(const QString &folder, const QString & iconKey) {
it.next(); it.next();
auto fileInfo = it.fileInfo(); auto fileInfo = it.fileInfo();
if(fileInfo.completeBaseName() != iconKey) if (fileInfo.completeBaseName() != iconKey)
continue; continue;
auto extension = fileInfo.suffix(); auto extension = fileInfo.suffix();
for(int i = 0; i < best_found; i++) { for (int i = 0; i < best_found; i++) {
if(extension == validIconExtensions[i]) { if (extension == validIconExtensions[i]) {
best_found = i; best_found = i;
qDebug() << i << " : " << fileInfo.fileName(); qDebug() << i << " : " << fileInfo.fileName();
best_filename = fileInfo.fileName(); best_filename = fileInfo.fileName();
@ -43,12 +37,13 @@ QString findBestIconIn(const QString &folder, const QString & iconKey) {
return FS::PathCombine(folder, best_filename); return FS::PathCombine(folder, best_filename);
} }
QString getIconFilter() { QString getIconFilter()
{
QString out; QString out;
QTextStream stream(&out); QTextStream stream(&out);
stream << '('; stream << '(';
for(size_t i = 0; i < validIconExtensions.size() - 1; i++) { for (size_t i = 0; i < validIconExtensions.size() - 1; i++) {
if(i > 0) { if (i > 0) {
stream << " "; stream << " ";
} }
stream << "*." << validIconExtensions[i]; stream << "*." << validIconExtensions[i];
@ -58,5 +53,4 @@ QString getIconFilter() {
return out; return out;
} }
} } // namespace IconUtils

View File

@ -5,9 +5,9 @@
namespace IconUtils { namespace IconUtils {
// Given a folder and an icon key, find 'best' of the icons with the given key in there and return its path // Given a folder and an icon key, find 'best' of the icons with the given key in there and return its path
QString findBestIconIn(const QString &folder, const QString & iconKey); QString findBestIconIn(const QString& folder, const QString& iconKey);
// Get icon file type filter for file browser dialogs // Get icon file type filter for file browser dialogs
QString getIconFilter(); QString getIconFilter();
} } // namespace IconUtils

View File

@ -37,11 +37,10 @@
#include <QFileInfo> #include <QFileInfo>
#include <QIcon> #include <QIcon>
IconType operator--(IconType &t, int) IconType operator--(IconType& t, int)
{ {
IconType temp = t; IconType temp = t;
switch (t) switch (t) {
{
case IconType::Builtin: case IconType::Builtin:
t = IconType::ToBeDeleted; t = IconType::ToBeDeleted;
break; break;
@ -51,8 +50,7 @@ IconType operator--(IconType &t, int)
case IconType::FileBased: case IconType::FileBased:
t = IconType::Transient; t = IconType::Transient;
break; break;
default: default: {
{
} }
} }
return temp; return temp;
@ -79,8 +77,8 @@ QIcon MMCIcon::icon() const
{ {
if (m_current_type == IconType::ToBeDeleted) if (m_current_type == IconType::ToBeDeleted)
return QIcon(); return QIcon();
auto & icon = m_images[m_current_type].icon; auto& icon = m_images[m_current_type].icon;
if(!icon.isNull()) if (!icon.isNull())
return icon; return icon;
// FIXME: inject this. // FIXME: inject this.
return QIcon::fromTheme(m_images[m_current_type].key); return QIcon::fromTheme(m_images[m_current_type].key);
@ -90,10 +88,8 @@ void MMCIcon::remove(IconType rm_type)
{ {
m_images[rm_type].filename = QString(); m_images[rm_type].filename = QString();
m_images[rm_type].icon = QIcon(); m_images[rm_type].icon = QIcon();
for (auto iter = rm_type; iter != IconType::ToBeDeleted; iter--) for (auto iter = rm_type; iter != IconType::ToBeDeleted; iter--) {
{ if (m_images[iter].present()) {
if (m_images[iter].present())
{
m_current_type = iter; m_current_type = iter;
return; return;
} }
@ -103,8 +99,7 @@ void MMCIcon::remove(IconType rm_type)
void MMCIcon::replace(IconType new_type, QIcon icon, QString path) void MMCIcon::replace(IconType new_type, QIcon icon, QString path)
{ {
if (new_type > m_current_type || m_current_type == IconType::ToBeDeleted) if (new_type > m_current_type || m_current_type == IconType::ToBeDeleted) {
{
m_current_type = new_type; m_current_type = new_type;
} }
m_images[new_type].icon = icon; m_images[new_type].icon = icon;
@ -114,8 +109,7 @@ void MMCIcon::replace(IconType new_type, QIcon icon, QString path)
void MMCIcon::replace(IconType new_type, const QString& key) void MMCIcon::replace(IconType new_type, const QString& key)
{ {
if (new_type > m_current_type || m_current_type == IconType::ToBeDeleted) if (new_type > m_current_type || m_current_type == IconType::ToBeDeleted) {
{
m_current_type = new_type; m_current_type = new_type;
} }
m_images[new_type].icon = QIcon(); m_images[new_type].icon = QIcon();
@ -125,13 +119,12 @@ void MMCIcon::replace(IconType new_type, const QString& key)
QString MMCIcon::getFilePath() const QString MMCIcon::getFilePath() const
{ {
if(m_current_type == IconType::ToBeDeleted){ if (m_current_type == IconType::ToBeDeleted) {
return QString(); return QString();
} }
return m_images[m_current_type].filename; return m_images[m_current_type].filename;
} }
bool MMCIcon::isBuiltIn() const bool MMCIcon::isBuiltIn() const
{ {
return m_current_type == IconType::Builtin; return m_current_type == IconType::Builtin;

View File

@ -14,32 +14,20 @@
*/ */
#pragma once #pragma once
#include <QString>
#include <QDateTime> #include <QDateTime>
#include <QIcon> #include <QIcon>
#include <QString>
enum IconType : unsigned enum IconType : unsigned { Builtin, Transient, FileBased, ICONS_TOTAL, ToBeDeleted };
{
Builtin,
Transient,
FileBased,
ICONS_TOTAL,
ToBeDeleted
};
struct MMCImage struct MMCImage {
{
QIcon icon; QIcon icon;
QString key; QString key;
QString filename; QString filename;
bool present() const bool present() const { return !icon.isNull() || !key.isEmpty(); }
{
return !icon.isNull() || !key.isEmpty();
}
}; };
struct MMCIcon struct MMCIcon {
{
QString m_key; QString m_key;
QString m_name; QString m_name;
MMCImage m_images[ICONS_TOTAL]; MMCImage m_images[ICONS_TOTAL];
@ -51,7 +39,7 @@ struct MMCIcon
QIcon icon() const; QIcon icon() const;
void remove(IconType rm_type); void remove(IconType rm_type);
void replace(IconType new_type, QIcon icon, QString path = QString()); void replace(IconType new_type, QIcon icon, QString path = QString());
void replace(IconType new_type, const QString &key); void replace(IconType new_type, const QString& key);
bool isBuiltIn() const; bool isBuiltIn() const;
QString getFilePath() const; QString getFilePath() const;
}; };