]> git.sur5r.net Git - minitube/blobdiff - src/iconutils.cpp
New upstream version 3.4
[minitube] / src / iconutils.cpp
index 6b536453ee37d1b926f012511c3e311290aec12d..b58368e453bb90ccaa2d80fdd1c04eff5827fd4c 100644 (file)
@@ -23,6 +23,7 @@ $END_LICENSE */
 #include <QAction>
 
 namespace {
+
 void addIconFile(QIcon &icon,
                  const QString &filename,
                  int size,
@@ -43,13 +44,15 @@ QIcon IconUtils::fromTheme(const QString &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/");
@@ -60,8 +63,9 @@ QIcon IconUtils::fromResources(const char *name) {
 
     // 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;
+        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);
@@ -74,13 +78,29 @@ QIcon IconUtils::fromResources(const char *name) {
 }
 
 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) {
@@ -96,12 +116,13 @@ QPixmap IconUtils::iconPixmap(const char *name,
                               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);
 }
 
@@ -149,6 +170,10 @@ void IconUtils::tint(QPixmap &pixmap, const QColor &color, QPainter::Composition
     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) {