#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;
}
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));
// 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
}