X-Git-Url: https://git.sur5r.net/?p=minitube;a=blobdiff_plain;f=src%2Ficonutils.cpp;h=b09d400b2e4e024079ec776900d2f3f88586d578;hp=b975411159ff0ef638b3d557c6086363e969c778;hb=533489a63a9716c645a11a99ca446978b20eedd0;hpb=994e6e5e95196b0e36c680b1fd496f12d71739c9 diff --git a/src/iconutils.cpp b/src/iconutils.cpp index b975411..b09d400 100644 --- a/src/iconutils.cpp +++ b/src/iconutils.cpp @@ -20,39 +20,55 @@ $END_LICENSE */ #include "iconutils.h" #include +#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, QListshortcut().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 }