]> git.sur5r.net Git - minitube/blobdiff - src/iconutils.cpp
New upstream version 2.9
[minitube] / src / iconutils.cpp
index b975411159ff0ef638b3d557c6086363e969c778..b09d400b2e4e024079ec776900d2f3f88586d578 100644 (file)
@@ -20,39 +20,55 @@ $END_LICENSE */
 
 #include "iconutils.h"
 #include <QAction>
+#include "mainwindow.h"
 
 QIcon IconUtils::fromTheme(const QString &name) {
     const QLatin1String symbolic("-symbolic");
     if (name.endsWith(symbolic)) return QIcon::fromTheme(name);
-    QIcon icon;
-    icon = QIcon::fromTheme(name + symbolic);
+    QIcon icon = QIcon::fromTheme(name + symbolic);
     if (icon.isNull()) return QIcon::fromTheme(name);
     return icon;
 }
 
 QIcon IconUtils::fromResources(const QString &name) {
-    QIcon icon = QIcon(QString(":/images/%1.png").arg(name));
+    QLatin1String path(":/images/");
+    QLatin1String ext(".png");
+    const QString pathAndName = path + name;
+    QIcon icon = QIcon(pathAndName + ext);
     if (!icon.isNull()) {
-        icon.addPixmap(IconUtils::pixmap(QString(":/images/%1_active.png").arg(name)), QIcon::Active);
-        icon.addPixmap(IconUtils::pixmap(QString(":/images/%1_selected.png").arg(name)), QIcon::Selected);
-        icon.addPixmap(IconUtils::pixmap(QString(":/images/%1_disabled.png").arg(name)), QIcon::Disabled);
+        QLatin1String active("_active");
+        QLatin1String selected("_selected");
+        QLatin1String disabled("_disabled");
+        QLatin1String checked("_checked");
+        QLatin1String twoX("@2x");
+
+        icon.addPixmap(QPixmap(pathAndName + active + ext), QIcon::Active);
+        icon.addPixmap(QPixmap(pathAndName + selected + ext), QIcon::Selected);
+        icon.addPixmap(QPixmap(pathAndName + disabled + ext), QIcon::Disabled);
+        icon.addPixmap(QPixmap(pathAndName + checked + ext), QIcon::Normal, QIcon::On);
+
+        const QString twoXAndExt = twoX + ext;
+        icon.addPixmap(QPixmap(pathAndName + active + twoXAndExt), QIcon::Active);
+        icon.addPixmap(QPixmap(pathAndName + selected + twoXAndExt), QIcon::Selected);
+        icon.addPixmap(QPixmap(pathAndName + disabled + twoXAndExt), QIcon::Disabled);
+        icon.addPixmap(QPixmap(pathAndName + checked + twoXAndExt), QIcon::Normal, QIcon::On);
     }
     return icon;
 }
 
 QIcon IconUtils::icon(const QString &name) {
-#if defined(APP_MAC) || defined(APP_WIN)
-    return fromResources(name);
-#else
+#ifdef APP_LINUX
     QIcon icon = fromTheme(name);
     if (icon.isNull()) icon = fromResources(name);
     return icon;
+#else
+    return fromResources(name);
 #endif
 }
 
 QIcon IconUtils::icon(const QStringList &names) {
     QIcon icon;
-    foreach (const QString &name, names) {
+    for (const QString &name : names) {
         icon = IconUtils::icon(name);
         if (!icon.availableSizes().isEmpty()) break;
     }
@@ -63,7 +79,7 @@ QIcon IconUtils::tintedIcon(const QString &name, const QColor &color, QList<QSiz
     QIcon i = IconUtils::icon(name);
     QIcon t;
     if (sizes.isEmpty()) sizes = i.availableSizes();
-    foreach (const QSize &size, sizes) {
+    for (const QSize &size : sizes) {
         QPixmap pixmap = i.pixmap(size);
         QImage tintedImage = tinted(pixmap.toImage(), color);
         t.addPixmap(QPixmap::fromImage(tintedImage));
@@ -106,23 +122,33 @@ void IconUtils::setupAction(QAction *action) {
     // show keyboard shortcuts in the status bar
     if (!action->shortcut().isEmpty())
         action->setStatusTip(action->statusTip() +
-                             " (" +
+                             QLatin1String(" (") +
                              action->shortcut().toString(QKeySequence::NativeText) +
-                             ")");
+                             QLatin1String(")"));
 }
 
 QPixmap IconUtils::pixmap(const QString &name) {
     // Check if a "@2x" file exists
-    QString fileName = name;
-    if (qApp->devicePixelRatio() > 1.0) {
-        int dotIndex = fileName.lastIndexOf(QLatin1Char('.'));
+    const qreal pixelRatio = IconUtils::pixelRatio();
+    if (pixelRatio > 1.0) {
+        int dotIndex = name.lastIndexOf(QLatin1Char('.'));
         if (dotIndex != -1) {
-            QString at2xfileName = fileName;
-            at2xfileName.insert(dotIndex, QStringLiteral("@2x"));
-            if (QFile::exists(at2xfileName))
-                fileName = at2xfileName;
+            QString at2xfileName = name;
+            at2xfileName.insert(dotIndex, QLatin1String("@2x"));
+            if (QFile::exists(at2xfileName)) {
+                QPixmap pixmap(at2xfileName);
+                pixmap.setDevicePixelRatio(pixelRatio);
+                return pixmap;
+            }
         }
     }
+    return QPixmap(name);
+}
 
-    return QPixmap(fileName);
+qreal IconUtils::pixelRatio() {
+#if QT_VERSION >= 0x050600
+    return MainWindow::instance()->devicePixelRatioF();
+#else
+    return MainWindow::instance()->devicePixelRatio();
+#endif
 }