X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Ficonutils.cpp;h=52462721320aa7cb6d8e8a88f8c847458f0309e8;hb=fe9d17324e88a65d4f28afccf21857c5a5e60649;hp=6b536453ee37d1b926f012511c3e311290aec12d;hpb=434d88418722fd7717038e44bd74271ca1d92771;p=minitube diff --git a/src/iconutils.cpp b/src/iconutils.cpp index 6b53645..5246272 100644 --- a/src/iconutils.cpp +++ b/src/iconutils.cpp @@ -23,6 +23,7 @@ $END_LICENSE */ #include namespace { + void addIconFile(QIcon &icon, const QString &filename, int size, @@ -32,8 +33,15 @@ void addIconFile(QIcon &icon, icon.addFile(filename, QSize(size, size), mode, state); } } + +QVector sizes; + } // namespace +void IconUtils::setSizes(const QVector &value) { + sizes = value; +} + QIcon IconUtils::fromTheme(const QString &name) { static const QLatin1String symbolic("-symbolic"); if (name.endsWith(symbolic)) return QIcon::fromTheme(name); @@ -43,13 +51,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/"); @@ -59,9 +69,10 @@ QIcon IconUtils::fromResources(const char *name) { QIcon icon; // 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; + for (int size : qAsConst(sizes)) { + 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 +85,29 @@ QIcon IconUtils::fromResources(const char *name) { } QIcon IconUtils::icon(const char *name) { -#ifdef APP_LINUX - QIcon icon = fromTheme(name); + static QMap cache = [] { + qDebug() << "Init icon cache"; + QMap 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 &names) { @@ -96,12 +123,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 +177,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) {