2019-05-31 20:52:58 +01:00
|
|
|
#include "IconUtils.h"
|
|
|
|
|
|
|
|
#include <QDirIterator>
|
2023-08-02 17:35:35 +01:00
|
|
|
#include "FileSystem.h"
|
2019-05-31 20:52:58 +01:00
|
|
|
|
|
|
|
#include <array>
|
|
|
|
|
|
|
|
namespace {
|
2023-08-02 17:35:35 +01:00
|
|
|
std::array<const char*, 6> validIconExtensions = { { "svg", "png", "ico", "gif", "jpg", "jpeg" } };
|
2019-05-31 20:52:58 +01:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
namespace IconUtils {
|
2019-05-31 20:52:58 +01:00
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
QString findBestIconIn(const QString& folder, const QString& iconKey)
|
|
|
|
{
|
2019-05-31 20:52:58 +01:00
|
|
|
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();
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
if (fileInfo.completeBaseName() != iconKey)
|
2019-05-31 20:52:58 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
auto extension = fileInfo.suffix();
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
for (int i = 0; i < best_found; i++) {
|
|
|
|
if (extension == validIconExtensions[i]) {
|
2019-05-31 20:52:58 +01:00
|
|
|
best_found = i;
|
|
|
|
qDebug() << i << " : " << fileInfo.fileName();
|
|
|
|
best_filename = fileInfo.fileName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FS::PathCombine(folder, best_filename);
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
QString getIconFilter()
|
|
|
|
{
|
2019-05-31 20:52:58 +01:00
|
|
|
QString out;
|
|
|
|
QTextStream stream(&out);
|
|
|
|
stream << '(';
|
2023-08-02 17:35:35 +01:00
|
|
|
for (size_t i = 0; i < validIconExtensions.size() - 1; i++) {
|
|
|
|
if (i > 0) {
|
2019-05-31 20:52:58 +01:00
|
|
|
stream << " ";
|
|
|
|
}
|
|
|
|
stream << "*." << validIconExtensions[i];
|
|
|
|
}
|
|
|
|
stream << " *." << validIconExtensions[validIconExtensions.size() - 1];
|
|
|
|
stream << ')';
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
} // namespace IconUtils
|