refactor icon list
Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
parent
1ca7e5efe9
commit
7e58b965b7
@ -43,6 +43,7 @@
|
||||
#include <QMimeData>
|
||||
#include <QSet>
|
||||
#include <QUrl>
|
||||
#include "icons/IconUtils.h"
|
||||
|
||||
#define MAX_SIZE 1024
|
||||
|
||||
@ -129,7 +130,7 @@ void IconList::directoryChanged(const QString& path)
|
||||
|
||||
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 (suffix != "jpeg" && suffix != "png" && suffix != "jpg" && suffix != "ico" && suffix != "svg" && suffix != "gif")
|
||||
if (!IconUtils::isIconSuffix(suffix))
|
||||
key = rmfile.fileName();
|
||||
|
||||
int idx = getIconIndex(key);
|
||||
@ -156,7 +157,7 @@ void IconList::directoryChanged(const QString& path)
|
||||
|
||||
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 (suffix != "jpeg" && suffix != "png" && suffix != "jpg" && suffix != "ico" && suffix != "svg" && suffix != "gif")
|
||||
if (!IconUtils::isIconSuffix(suffix))
|
||||
key = addfile.fileName();
|
||||
|
||||
if (addIcon(key, QString(), addfile.filePath(), IconType::FileBased)) {
|
||||
@ -258,7 +259,6 @@ Qt::ItemFlags IconList::flags(const QModelIndex& index) const
|
||||
Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index);
|
||||
if (index.isValid())
|
||||
return Qt::ItemIsDropEnabled | defaultFlags;
|
||||
else
|
||||
return Qt::ItemIsDropEnabled | defaultFlags;
|
||||
}
|
||||
|
||||
@ -291,19 +291,8 @@ int IconList::rowCount(const QModelIndex& parent) const
|
||||
|
||||
void IconList::installIcons(const QStringList& iconFiles)
|
||||
{
|
||||
for (QString file : iconFiles) {
|
||||
QFileInfo fileinfo(file);
|
||||
if (!fileinfo.isReadable() || !fileinfo.isFile())
|
||||
continue;
|
||||
QString target = FS::PathCombine(getDirectory(), fileinfo.fileName());
|
||||
|
||||
QString suffix = fileinfo.suffix();
|
||||
if (suffix != "jpeg" && suffix != "png" && suffix != "jpg" && suffix != "ico" && suffix != "svg" && suffix != "gif")
|
||||
continue;
|
||||
|
||||
if (!QFile::copy(file, target))
|
||||
continue;
|
||||
}
|
||||
for (QString file : iconFiles)
|
||||
installIcon(file, {});
|
||||
}
|
||||
|
||||
void IconList::installIcon(const QString& file, const QString& name)
|
||||
@ -312,18 +301,17 @@ void IconList::installIcon(const QString& file, const QString& name)
|
||||
if (!fileinfo.isReadable() || !fileinfo.isFile())
|
||||
return;
|
||||
|
||||
QString target = FS::PathCombine(getDirectory(), name);
|
||||
if (!IconUtils::isIconSuffix(fileinfo.suffix()))
|
||||
return;
|
||||
|
||||
QString target = FS::PathCombine(getDirectory(), name.isEmpty() ? fileinfo.fileName() : name);
|
||||
QFile::copy(file, target);
|
||||
}
|
||||
|
||||
bool IconList::iconFileExists(const QString& key) const
|
||||
{
|
||||
auto iconEntry = icon(key);
|
||||
if (!iconEntry) {
|
||||
return false;
|
||||
}
|
||||
return iconEntry->has(IconType::FileBased);
|
||||
return iconEntry && iconEntry->has(IconType::FileBased);
|
||||
}
|
||||
|
||||
const MMCIcon* IconList::icon(const QString& key) const
|
||||
@ -336,18 +324,12 @@ const MMCIcon* IconList::icon(const QString& key) const
|
||||
|
||||
bool IconList::deleteIcon(const QString& key)
|
||||
{
|
||||
if (!iconFileExists(key))
|
||||
return false;
|
||||
|
||||
return QFile::remove(icon(key)->getFilePath());
|
||||
return iconFileExists(key) && QFile::remove(icon(key)->getFilePath());
|
||||
}
|
||||
|
||||
bool IconList::trashIcon(const QString& key)
|
||||
{
|
||||
if (!iconFileExists(key))
|
||||
return false;
|
||||
|
||||
return FS::trash(icon(key)->getFilePath(), nullptr);
|
||||
return iconFileExists(key) && FS::trash(icon(key)->getFilePath(), nullptr);
|
||||
}
|
||||
|
||||
bool IconList::addThemeIcon(const QString& key)
|
||||
@ -358,7 +340,7 @@ bool IconList::addThemeIcon(const QString& key)
|
||||
oldOne.replace(Builtin, key);
|
||||
dataChanged(index(*iter), index(*iter));
|
||||
return true;
|
||||
} else {
|
||||
}
|
||||
// add a new icon
|
||||
beginInsertRows(QModelIndex(), icons.size(), icons.size());
|
||||
{
|
||||
@ -371,7 +353,6 @@ bool IconList::addThemeIcon(const QString& key)
|
||||
}
|
||||
endInsertRows();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool IconList::addIcon(const QString& key, const QString& name, const QString& path, const IconType type)
|
||||
@ -386,7 +367,7 @@ bool IconList::addIcon(const QString& key, const QString& name, const QString& p
|
||||
oldOne.replace(type, icon, path);
|
||||
dataChanged(index(*iter), index(*iter));
|
||||
return true;
|
||||
} else {
|
||||
}
|
||||
// add a new icon
|
||||
beginInsertRows(QModelIndex(), icons.size(), icons.size());
|
||||
{
|
||||
@ -399,7 +380,6 @@ bool IconList::addIcon(const QString& key, const QString& name, const QString& p
|
||||
}
|
||||
endInsertRows();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void IconList::saveIcon(const QString& key, const QString& path, const char* format) const
|
||||
|
@ -38,17 +38,14 @@
|
||||
#include <QDirIterator>
|
||||
#include "FileSystem.h"
|
||||
|
||||
#include <array>
|
||||
|
||||
namespace {
|
||||
std::array<const char*, 6> validIconExtensions = { { "svg", "png", "ico", "gif", "jpg", "jpeg" } };
|
||||
static const QStringList 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);
|
||||
@ -56,36 +53,20 @@ QString findBestIconIn(const QString& folder, const QString& iconKey)
|
||||
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();
|
||||
if (fileInfo.completeBaseName() == iconKey && isIconSuffix(fileInfo.suffix()))
|
||||
return fileInfo.absoluteFilePath();
|
||||
}
|
||||
}
|
||||
}
|
||||
return FS::PathCombine(folder, best_filename);
|
||||
return {};
|
||||
}
|
||||
|
||||
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;
|
||||
return "(*." + validIconExtensions.join(" *.") + ")";
|
||||
}
|
||||
|
||||
bool isIconSuffix(QString suffix)
|
||||
{
|
||||
return validIconExtensions.contains(suffix);
|
||||
}
|
||||
|
||||
} // namespace IconUtils
|
||||
|
@ -45,4 +45,5 @@ QString findBestIconIn(const QString& folder, const QString& iconKey);
|
||||
// Get icon file type filter for file browser dialogs
|
||||
QString getIconFilter();
|
||||
|
||||
bool isIconSuffix(QString suffix);
|
||||
} // namespace IconUtils
|
||||
|
@ -51,8 +51,8 @@ IconType operator--(IconType& t, int)
|
||||
case IconType::FileBased:
|
||||
t = IconType::Transient;
|
||||
break;
|
||||
default: {
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user