#include <QAction>
namespace {
+
void addIconFile(QIcon &icon,
const QString &filename,
int size,
icon.addFile(filename, QSize(size, size), mode, state);
}
}
+
+QVector<int> sizes;
+
} // namespace
+void IconUtils::setSizes(const QVector<int> &value) {
+ sizes = value;
+}
+
QIcon IconUtils::fromTheme(const QString &name) {
static const QLatin1String symbolic("-symbolic");
if (name.endsWith(symbolic)) return QIcon::fromTheme(name);
}
QIcon IconUtils::fromResources(const char *name) {
+ qDebug() << "Creating icon" << name;
+ static const QLatin1String normal("_normal");
static const QLatin1String active("_active");
static const QLatin1String selected("_selected");
static const QLatin1String disabled("_disabled");
static const QLatin1String checked("_checked");
static const QLatin1String ext(".png");
- QString path(":/icons/");
+ QString path = QStringLiteral(":/icons/");
if (MainWindow::instance()->palette().window().color().value() > 128)
path += QLatin1String("light/");
QIcon icon;
// WARN keep these sizes updated with what we really use
- for (int size : {16, 24, 32, 88}) {
- const QString pathAndName = path + QString::number(size) + '/' + name;
- QString iconFilename = pathAndName + ext;
+ for (int size : qAsConst(sizes)) {
+ const QString pathAndName =
+ path + QString::number(size) + QLatin1Char('/') + QLatin1String(name);
+ QString iconFilename = pathAndName + normal + ext;
if (QFile::exists(iconFilename)) {
addIconFile(icon, iconFilename, size);
addIconFile(icon, pathAndName + active + ext, size, QIcon::Active);
}
QIcon IconUtils::icon(const char *name) {
-#ifdef APP_LINUX
- QIcon icon = fromTheme(name);
+ static QMap<QByteArray, QIcon> cache = [] {
+ qDebug() << "Init icon cache";
+ QMap<QByteArray, QIcon> c;
+ QObject::connect(qApp, &QApplication::paletteChanged, qApp, [&c]() {
+ qDebug() << "Clearing icon cache";
+ c.clear();
+ });
+ return c;
+ }();
+
+ auto i = cache.constFind(QByteArray::fromRawData(name, strlen(name)));
+ if (i != cache.constEnd()) return i.value();
+
+ QIcon icon;
+#ifdef APP_UBUNTU_NO
+ icon = fromTheme(name);
if (icon.isNull()) icon = fromResources(name);
- return icon;
#else
- return fromResources(name);
+ icon = fromResources(name);
#endif
+
+ cache.insert(QByteArray(name), icon);
+ return icon;
}
QIcon IconUtils::icon(const QVector<const char *> &names) {
int size,
const QColor &background,
const qreal pixelRatio) {
- QString path(":/icons/");
+ QString path = QStringLiteral(":/icons/");
if (background.value() > 128)
- path += "light/";
+ path += QLatin1String("light/");
else
- path += "dark/";
- path += QString::number(size) + '/' + name + QLatin1String(".png");
+ path += QLatin1String("dark/");
+ path += QString::number(size) + QLatin1Char('/') + QLatin1String(name) +
+ QLatin1String("_normal.png");
return IconUtils::pixmap(path, pixelRatio);
}
painter.fillRect(pixmap.rect(), color);
}
+QPixmap IconUtils::pixmap(const char *name, const qreal pixelRatio) {
+ return pixmap(QString::fromLatin1(name), pixelRatio);
+}
+
QPixmap IconUtils::pixmap(const QString &filename, const qreal pixelRatio) {
// Check if a "@2x" file exists
if (pixelRatio > 1.0) {