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 <QMimeData>
|
||||||
#include <QSet>
|
#include <QSet>
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
|
#include "icons/IconUtils.h"
|
||||||
|
|
||||||
#define MAX_SIZE 1024
|
#define MAX_SIZE 1024
|
||||||
|
|
||||||
@ -129,7 +130,7 @@ void IconList::directoryChanged(const QString& path)
|
|||||||
|
|
||||||
QString suffix = rmfile.suffix();
|
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
|
// 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();
|
key = rmfile.fileName();
|
||||||
|
|
||||||
int idx = getIconIndex(key);
|
int idx = getIconIndex(key);
|
||||||
@ -156,7 +157,7 @@ void IconList::directoryChanged(const QString& path)
|
|||||||
|
|
||||||
QString suffix = addfile.suffix();
|
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
|
// 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();
|
key = addfile.fileName();
|
||||||
|
|
||||||
if (addIcon(key, QString(), addfile.filePath(), IconType::FileBased)) {
|
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);
|
Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index);
|
||||||
if (index.isValid())
|
if (index.isValid())
|
||||||
return Qt::ItemIsDropEnabled | defaultFlags;
|
return Qt::ItemIsDropEnabled | defaultFlags;
|
||||||
else
|
|
||||||
return Qt::ItemIsDropEnabled | defaultFlags;
|
return Qt::ItemIsDropEnabled | defaultFlags;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -291,19 +291,8 @@ int IconList::rowCount(const QModelIndex& parent) const
|
|||||||
|
|
||||||
void IconList::installIcons(const QStringList& iconFiles)
|
void IconList::installIcons(const QStringList& iconFiles)
|
||||||
{
|
{
|
||||||
for (QString file : iconFiles) {
|
for (QString file : iconFiles)
|
||||||
QFileInfo fileinfo(file);
|
installIcon(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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void IconList::installIcon(const QString& file, const QString& name)
|
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())
|
if (!fileinfo.isReadable() || !fileinfo.isFile())
|
||||||
return;
|
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);
|
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) {
|
return iconEntry && iconEntry->has(IconType::FileBased);
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return iconEntry->has(IconType::FileBased);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const MMCIcon* IconList::icon(const QString& key) const
|
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)
|
bool IconList::deleteIcon(const QString& key)
|
||||||
{
|
{
|
||||||
if (!iconFileExists(key))
|
return iconFileExists(key) && QFile::remove(icon(key)->getFilePath());
|
||||||
return false;
|
|
||||||
|
|
||||||
return QFile::remove(icon(key)->getFilePath());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IconList::trashIcon(const QString& key)
|
bool IconList::trashIcon(const QString& key)
|
||||||
{
|
{
|
||||||
if (!iconFileExists(key))
|
return iconFileExists(key) && FS::trash(icon(key)->getFilePath(), nullptr);
|
||||||
return false;
|
|
||||||
|
|
||||||
return FS::trash(icon(key)->getFilePath(), nullptr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IconList::addThemeIcon(const QString& key)
|
bool IconList::addThemeIcon(const QString& key)
|
||||||
@ -358,7 +340,7 @@ bool IconList::addThemeIcon(const QString& key)
|
|||||||
oldOne.replace(Builtin, key);
|
oldOne.replace(Builtin, key);
|
||||||
dataChanged(index(*iter), index(*iter));
|
dataChanged(index(*iter), index(*iter));
|
||||||
return true;
|
return true;
|
||||||
} else {
|
}
|
||||||
// add a new icon
|
// add a new icon
|
||||||
beginInsertRows(QModelIndex(), icons.size(), icons.size());
|
beginInsertRows(QModelIndex(), icons.size(), icons.size());
|
||||||
{
|
{
|
||||||
@ -372,7 +354,6 @@ bool IconList::addThemeIcon(const QString& key)
|
|||||||
endInsertRows();
|
endInsertRows();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
@ -386,7 +367,7 @@ bool IconList::addIcon(const QString& key, const QString& name, const QString& p
|
|||||||
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 {
|
}
|
||||||
// add a new icon
|
// add a new icon
|
||||||
beginInsertRows(QModelIndex(), icons.size(), icons.size());
|
beginInsertRows(QModelIndex(), icons.size(), icons.size());
|
||||||
{
|
{
|
||||||
@ -400,7 +381,6 @@ bool IconList::addIcon(const QString& key, const QString& name, const QString& p
|
|||||||
endInsertRows();
|
endInsertRows();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
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
|
||||||
{
|
{
|
||||||
|
@ -38,17 +38,14 @@
|
|||||||
#include <QDirIterator>
|
#include <QDirIterator>
|
||||||
#include "FileSystem.h"
|
#include "FileSystem.h"
|
||||||
|
|
||||||
#include <array>
|
|
||||||
|
|
||||||
namespace {
|
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 {
|
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;
|
QString best_filename;
|
||||||
|
|
||||||
QDirIterator it(folder, QDir::NoDotAndDotDot | QDir::Files, QDirIterator::NoIteratorFlags);
|
QDirIterator it(folder, QDir::NoDotAndDotDot | QDir::Files, QDirIterator::NoIteratorFlags);
|
||||||
@ -56,36 +53,20 @@ 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 && isIconSuffix(fileInfo.suffix()))
|
||||||
continue;
|
return fileInfo.absoluteFilePath();
|
||||||
|
|
||||||
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 {};
|
||||||
}
|
|
||||||
return FS::PathCombine(folder, best_filename);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString getIconFilter()
|
QString getIconFilter()
|
||||||
{
|
{
|
||||||
QString out;
|
return "(*." + validIconExtensions.join(" *.") + ")";
|
||||||
QTextStream stream(&out);
|
|
||||||
stream << '(';
|
|
||||||
for (size_t i = 0; i < validIconExtensions.size() - 1; i++) {
|
|
||||||
if (i > 0) {
|
|
||||||
stream << " ";
|
|
||||||
}
|
}
|
||||||
stream << "*." << validIconExtensions[i];
|
|
||||||
}
|
bool isIconSuffix(QString suffix)
|
||||||
stream << " *." << validIconExtensions[validIconExtensions.size() - 1];
|
{
|
||||||
stream << ')';
|
return validIconExtensions.contains(suffix);
|
||||||
return out;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace IconUtils
|
} // namespace IconUtils
|
||||||
|
@ -45,4 +45,5 @@ 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();
|
||||||
|
|
||||||
|
bool isIconSuffix(QString suffix);
|
||||||
} // namespace IconUtils
|
} // namespace IconUtils
|
||||||
|
@ -51,8 +51,8 @@ IconType operator--(IconType& t, int)
|
|||||||
case IconType::FileBased:
|
case IconType::FileBased:
|
||||||
t = IconType::Transient;
|
t = IconType::Transient;
|
||||||
break;
|
break;
|
||||||
default: {
|
default:
|
||||||
}
|
break;
|
||||||
}
|
}
|
||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user