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,13 +35,13 @@
#include "IconList.h"
#include <FileSystem.h>
#include <QMap>
#include <QEventLoop>
#include <QMimeData>
#include <QUrl>
#include <QFileSystemWatcher>
#include <QSet>
#include <QDebug>
#include <QEventLoop>
#include <QFileSystemWatcher>
#include <QMap>
#include <QMimeData>
#include <QSet>
#include <QUrl>
#define MAX_SIZE 1024
@ -50,17 +50,14 @@ IconList::IconList(const QStringList &builtinPaths, QString path, QObject *paren
QSet<QString> builtinNames;
// add builtin icons
for(auto & builtinPath: builtinPaths)
{
for (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 (auto file_info : file_info_list) {
builtinNames.insert(file_info.completeBaseName());
}
}
for(auto & builtinName : builtinNames)
{
for (auto& builtinName : builtinNames) {
addThemeIcon(builtinName);
}
@ -78,17 +75,14 @@ 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) { return a.m_key.localeAwareCompare(b.m_key) < 0; });
reindex();
}
void IconList::directoryChanged(const QString& 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.refresh();
if (is_watching)
@ -100,8 +94,7 @@ void IconList::directoryChanged(const QString &path)
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++)
{
for (auto it = new_list.begin(); it != new_list.end(); it++) {
QString& foo = (*it);
foo = m_dir.filePath(foo);
}
@ -111,8 +104,7 @@ void IconList::directoryChanged(const QString &path)
auto new_set = new_list.toSet();
#endif
QList<QString> current_list;
for (auto &it : icons)
{
for (auto& it : icons) {
if (!it.has(IconType::FileBased))
continue;
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;
to_add -= current_set;
for (auto remove : to_remove)
{
for (auto remove : to_remove) {
qDebug() << "Removing " << remove;
QFileInfo rmfile(remove);
QString key = rmfile.completeBaseName();
@ -144,23 +135,19 @@ void IconList::directoryChanged(const QString &path)
if (idx == -1)
continue;
icons[idx].remove(IconType::FileBased);
if (icons[idx].type() == IconType::ToBeDeleted)
{
if (icons[idx].type() == IconType::ToBeDeleted) {
beginRemoveRows(QModelIndex(), idx, idx);
icons.remove(idx);
reindex();
endRemoveRows();
}
else
{
} else {
dataChanged(index(idx), index(idx));
}
m_watcher->removePath(remove);
emit iconUpdated(key);
}
for (auto add : to_add)
{
for (auto add : to_add) {
qDebug() << "Adding " << 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")
key = addfile.fileName();
if (addIcon(key, QString(), addfile.filePath(), IconType::FileBased))
{
if (addIcon(key, QString(), addfile.filePath(), IconType::FileBased)) {
m_watcher->addPath(add);
emit iconUpdated(key);
}
@ -213,12 +199,9 @@ void IconList::startWatching()
auto abs_path = m_dir.absolutePath();
FS::ensureFolderPathExists(abs_path);
is_watching = m_watcher->addPath(abs_path);
if (is_watching)
{
if (is_watching) {
qDebug() << "Started watching " << abs_path;
}
else
{
} else {
qDebug() << "Failed to start watching " << abs_path;
}
}
@ -241,7 +224,11 @@ Qt::DropActions IconList::supportedDropActions() const
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)
return true;
@ -250,12 +237,10 @@ bool IconList::dropMimeData(const QMimeData *data, Qt::DropAction action, [[mayb
return false;
// files dropped from outside?
if (data->hasUrls())
{
if (data->hasUrls()) {
auto urls = data->urls();
QStringList iconFiles;
for (auto url : urls)
{
for (auto url : urls) {
// only local files may be dropped...
if (!url.isLocalFile())
continue;
@ -286,8 +271,7 @@ QVariant IconList::data(const QModelIndex &index, int role) const
if (row < 0 || row >= icons.size())
return QVariant();
switch (role)
{
switch (role) {
case Qt::DecorationRole:
return icons[row].icon();
case Qt::DisplayRole:
@ -306,8 +290,7 @@ int IconList::rowCount(const QModelIndex &parent) const
void IconList::installIcons(const QStringList& iconFiles)
{
for (QString file : iconFiles)
{
for (QString file : iconFiles) {
QFileInfo fileinfo(file);
if (!fileinfo.isReadable() || !fileinfo.isFile())
continue;
@ -336,8 +319,7 @@ void IconList::installIcon(const QString &file, const QString &name)
bool IconList::iconFileExists(const QString& key) const
{
auto iconEntry = icon(key);
if(!iconEntry)
{
if (!iconEntry) {
return false;
}
return iconEntry->has(IconType::FileBased);
@ -370,15 +352,12 @@ bool IconList::trashIcon(const QString &key)
bool IconList::addThemeIcon(const QString& key)
{
auto iter = name_index.find(key);
if (iter != name_index.end())
{
if (iter != name_index.end()) {
auto& oldOne = icons[*iter];
oldOne.replace(Builtin, key);
dataChanged(index(*iter), index(*iter));
return true;
}
else
{
} else {
// add a new icon
beginInsertRows(QModelIndex(), icons.size(), icons.size());
{
@ -401,15 +380,12 @@ bool IconList::addIcon(const QString &key, const QString &name, const QString &p
if (icon.isNull())
return false;
auto iter = name_index.find(key);
if (iter != name_index.end())
{
if (iter != name_index.end()) {
auto& oldOne = icons[*iter];
oldOne.replace(type, icon, path);
dataChanged(index(*iter), index(*iter));
return true;
}
else
{
} else {
// add a new icon
beginInsertRows(QModelIndex(), icons.size(), icons.size());
{
@ -432,13 +408,11 @@ void IconList::saveIcon(const QString &key, const QString &path, const char * fo
pixmap.save(path, format);
}
void IconList::reindex()
{
name_index.clear();
int i = 0;
for (auto &iter : icons)
{
for (auto& iter : icons) {
name_index[iter.m_key] = i;
i++;
}

View File

@ -15,10 +15,10 @@
#pragma once
#include <QMutex>
#include <QAbstractListModel>
#include <QFile>
#include <QDir>
#include <QFile>
#include <QMutex>
#include <QtGui/QIcon>
#include <memory>
@ -29,8 +29,7 @@
class QFileSystemWatcher;
class IconList : public QAbstractListModel
{
class IconList : public QAbstractListModel {
Q_OBJECT
public:
explicit IconList(const QStringList& builtinPaths, QString path, QObject* parent = 0);
@ -80,6 +79,7 @@ public slots:
protected slots:
void fileChanged(const QString& path);
void SettingChanged(const Setting& setting, QVariant value);
private:
shared_qobject_ptr<QFileSystemWatcher> m_watcher;
bool is_watching;

View File

@ -1,24 +1,18 @@
#include "IconUtils.h"
#include "FileSystem.h"
#include <QDirIterator>
#include "FileSystem.h"
#include <array>
namespace {
std::array<const char *, 6> validIconExtensions = {{
"svg",
"png",
"ico",
"gif",
"jpg",
"jpeg"
}};
std::array<const char*, 6> validIconExtensions = { { "svg", "png", "ico", "gif", "jpg", "jpeg" } };
}
namespace IconUtils {
QString findBestIconIn(const QString &folder, const QString & iconKey) {
QString findBestIconIn(const QString& folder, const QString& iconKey)
{
int best_found = validIconExtensions.size();
QString best_filename;
@ -43,7 +37,8 @@ QString findBestIconIn(const QString &folder, const QString & iconKey) {
return FS::PathCombine(folder, best_filename);
}
QString getIconFilter() {
QString getIconFilter()
{
QString out;
QTextStream stream(&out);
stream << '(';
@ -58,5 +53,4 @@ QString getIconFilter() {
return out;
}
}
} // namespace IconUtils

View File

@ -10,4 +10,4 @@ QString findBestIconIn(const QString &folder, const QString & iconKey);
// Get icon file type filter for file browser dialogs
QString getIconFilter();
}
} // namespace IconUtils

View File

@ -40,8 +40,7 @@
IconType operator--(IconType& t, int)
{
IconType temp = t;
switch (t)
{
switch (t) {
case IconType::Builtin:
t = IconType::ToBeDeleted;
break;
@ -51,8 +50,7 @@ IconType operator--(IconType &t, int)
case IconType::FileBased:
t = IconType::Transient;
break;
default:
{
default: {
}
}
return temp;
@ -90,10 +88,8 @@ void MMCIcon::remove(IconType rm_type)
{
m_images[rm_type].filename = QString();
m_images[rm_type].icon = QIcon();
for (auto iter = rm_type; iter != IconType::ToBeDeleted; iter--)
{
if (m_images[iter].present())
{
for (auto iter = rm_type; iter != IconType::ToBeDeleted; iter--) {
if (m_images[iter].present()) {
m_current_type = iter;
return;
}
@ -103,8 +99,7 @@ void MMCIcon::remove(IconType rm_type)
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_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)
{
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_images[new_type].icon = QIcon();
@ -131,7 +125,6 @@ QString MMCIcon::getFilePath() const
return m_images[m_current_type].filename;
}
bool MMCIcon::isBuiltIn() const
{
return m_current_type == IconType::Builtin;

View File

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