Themes: Cache icons instead of rebuilding a lot.
authorChristian Kamm <kamm@incasoftware.de>
Wed, 25 Mar 2015 13:57:23 +0000 (14:57 +0100)
committerChristian Kamm <kamm@incasoftware.de>
Wed, 25 Mar 2015 13:59:28 +0000 (14:59 +0100)
src/libsync/theme.cpp
src/libsync/theme.h

index 6483e904e38fa8c10d48f3504e02e50db715a8e1..3a7849773e535f78fbef6521557e6f30c22ac5ed 100644 (file)
@@ -122,11 +122,14 @@ QIcon Theme::themeIcon( const QString& name, bool sysTray ) const
         flavor = QLatin1String("colored");
     }
 
-    QIcon icon;
     if( QIcon::hasThemeIcon( name )) {
         // use from theme
-        icon = QIcon::fromTheme( name );
-    } else {
+        return QIcon::fromTheme( name );
+    }
+
+    QString key = name + "," + flavor;
+    QIcon & cached = _iconCache[key];
+    if (cached.isNull()) {
         QList<int> sizes;
         sizes <<16 << 22 << 32 << 48 << 64 << 128 << 256;
         foreach (int size, sizes) {
@@ -140,19 +143,20 @@ QIcon Theme::themeIcon( const QString& name, bool sysTray ) const
                     p.setPen(QColor("#dfdbd2"));
                     p.drawPixmap(px.rect(), mask, mask.rect());
                 }
-                icon.addPixmap(px);
+                cached.addPixmap(px);
             }
         }
-        if (icon.isNull()) {
+        if (cached.isNull()) {
             foreach (int size, sizes) {
                 QString pixmapName = QString::fromLatin1(":/client/resources/%1-%2.png").arg(name).arg(size);
                 if (QFile::exists(pixmapName)) {
-                    icon.addFile(pixmapName);
+                    cached.addFile(pixmapName);
                 }
             }
         }
     }
-    return icon;
+
+    return cached;
 }
 
 QString Theme::hidpiFileName(const QString &fileName, QPaintDevice *dev)
index 061060ec4ec2431edb0eafba52fd91a83f6b3c92..7974b76687d3597641188202ec3c4e34707f9ba0 100644 (file)
@@ -208,7 +208,7 @@ private:
 
     static Theme* _instance;
     bool _mono;
-
+    mutable QHash<QString, QIcon> _iconCache;
 };
 
 }