Browse Source

Provide scaled pixmaps by UIThemeManager

Avoid leaking the paths of the theme resource files outside of the theme support implementation.

PR #18269.
adaptive-webui-19844
Vladimir Golovnev 2 years ago committed by GitHub
parent
commit
53cec6db09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      src/gui/aboutdialog.cpp
  2. 12
      src/gui/optionsdialog.cpp
  3. 8
      src/gui/speedlimitdialog.cpp
  4. 42
      src/gui/uithememanager.cpp
  5. 3
      src/gui/uithememanager.h
  6. 20
      src/gui/utils.cpp
  7. 1
      src/gui/utils.h

2
src/gui/aboutdialog.cpp

@ -51,7 +51,7 @@ AboutDialog::AboutDialog(QWidget *parent)
// Title // Title
m_ui->labelName->setText(QStringLiteral("<b><h2>qBittorrent " QBT_VERSION " (%1-bit)</h2></b>").arg(QT_POINTER_SIZE * 8)); m_ui->labelName->setText(QStringLiteral("<b><h2>qBittorrent " QBT_VERSION " (%1-bit)</h2></b>").arg(QT_POINTER_SIZE * 8));
m_ui->logo->setPixmap(Utils::Gui::scaledPixmapSvg(UIThemeManager::instance()->getIconPath(u"qbittorrent-tray"_qs), this, 32)); m_ui->logo->setPixmap(UIThemeManager::instance()->getScaledPixmap(u"qbittorrent-tray"_qs, 32));
// About // About
const QString aboutText = const QString aboutText =

12
src/gui/optionsdialog.cpp

@ -884,11 +884,11 @@ void OptionsDialog::loadSpeedTabOptions()
const auto *pref = Preferences::instance(); const auto *pref = Preferences::instance();
const auto *session = BitTorrent::Session::instance(); const auto *session = BitTorrent::Session::instance();
m_ui->labelGlobalRate->setPixmap(Utils::Gui::scaledPixmapSvg(UIThemeManager::instance()->getIconPath(u"slow_off"_qs), this, Utils::Gui::mediumIconSize(this).height())); m_ui->labelGlobalRate->setPixmap(UIThemeManager::instance()->getScaledPixmap(u"slow_off"_qs, Utils::Gui::mediumIconSize(this).height()));
m_ui->spinUploadLimit->setValue(session->globalUploadSpeedLimit() / 1024); m_ui->spinUploadLimit->setValue(session->globalUploadSpeedLimit() / 1024);
m_ui->spinDownloadLimit->setValue(session->globalDownloadSpeedLimit() / 1024); m_ui->spinDownloadLimit->setValue(session->globalDownloadSpeedLimit() / 1024);
m_ui->labelAltRate->setPixmap(Utils::Gui::scaledPixmapSvg(UIThemeManager::instance()->getIconPath(u"slow"_qs), this, Utils::Gui::mediumIconSize(this).height())); m_ui->labelAltRate->setPixmap(UIThemeManager::instance()->getScaledPixmap(u"slow"_qs, Utils::Gui::mediumIconSize(this).height()));
m_ui->spinUploadLimitAlt->setValue(session->altGlobalUploadSpeedLimit() / 1024); m_ui->spinUploadLimitAlt->setValue(session->altGlobalUploadSpeedLimit() / 1024);
m_ui->spinDownloadLimitAlt->setValue(session->altGlobalDownloadSpeedLimit() / 1024); m_ui->spinDownloadLimitAlt->setValue(session->altGlobalDownloadSpeedLimit() / 1024);
@ -1769,8 +1769,8 @@ void OptionsDialog::webUIHttpsCertChanged(const Path &path)
}; };
m_ui->textWebUIHttpsCert->setSelectedPath(path); m_ui->textWebUIHttpsCert->setSelectedPath(path);
m_ui->lblSslCertStatus->setPixmap(Utils::Gui::scaledPixmapSvg(UIThemeManager::instance()->getIconPath( m_ui->lblSslCertStatus->setPixmap(UIThemeManager::instance()->getScaledPixmap(
isCertFileValid() ? u"security-high"_qs : u"security-low"_qs), this, 24)); (isCertFileValid() ? u"security-high"_qs : u"security-low"_qs), 24));
} }
void OptionsDialog::webUIHttpsKeyChanged(const Path &path) void OptionsDialog::webUIHttpsKeyChanged(const Path &path)
@ -1791,8 +1791,8 @@ void OptionsDialog::webUIHttpsKeyChanged(const Path &path)
}; };
m_ui->textWebUIHttpsKey->setSelectedPath(path); m_ui->textWebUIHttpsKey->setSelectedPath(path);
m_ui->lblSslKeyStatus->setPixmap(Utils::Gui::scaledPixmapSvg(UIThemeManager::instance()->getIconPath( m_ui->lblSslKeyStatus->setPixmap(UIThemeManager::instance()->getScaledPixmap(
isKeyFileValid() ? u"security-high"_qs : u"security-low"_qs), this, 24)); (isKeyFileValid() ? u"security-high"_qs : u"security-low"_qs), 24));
} }
bool OptionsDialog::isWebUiEnabled() const bool OptionsDialog::isWebUiEnabled() const

8
src/gui/speedlimitdialog.cpp

@ -56,10 +56,10 @@ SpeedLimitDialog::SpeedLimitDialog(QWidget *parent)
{ {
m_ui->setupUi(this); m_ui->setupUi(this);
m_ui->labelGlobalSpeedIcon->setPixmap(Utils::Gui::scaledPixmapSvg(UIThemeManager::instance()->getIconPath(u"slow_off"_qs) m_ui->labelGlobalSpeedIcon->setPixmap(
, this, Utils::Gui::mediumIconSize(this).height())); UIThemeManager::instance()->getScaledPixmap(u"slow_off"_qs, Utils::Gui::mediumIconSize(this).height()));
m_ui->labelAltGlobalSpeedIcon->setPixmap(Utils::Gui::scaledPixmapSvg(UIThemeManager::instance()->getIconPath(u"slow"_qs) m_ui->labelAltGlobalSpeedIcon->setPixmap(
, this, Utils::Gui::mediumIconSize(this).height())); UIThemeManager::instance()->getScaledPixmap(u"slow"_qs, Utils::Gui::mediumIconSize(this).height()));
const auto initSlider = [](QSlider *slider, const int value, const int maximum) const auto initSlider = [](QSlider *slider, const int value, const int maximum)
{ {

42
src/gui/uithememanager.cpp

@ -35,6 +35,7 @@
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonObject> #include <QJsonObject>
#include <QPalette> #include <QPalette>
#include <QPixmapCache>
#include <QResource> #include <QResource>
#include "base/global.h" #include "base/global.h"
@ -237,6 +238,25 @@ QIcon UIThemeManager::getFlagIcon(const QString &countryIsoCode) const
return icon; return icon;
} }
QPixmap UIThemeManager::getScaledPixmap(const QString &iconId, const int height) const
{
// (workaround) svg images require the use of `QIcon()` to load and scale losslessly,
// otherwise other image classes will convert it to pixmap first and follow-up scaling will become lossy.
Q_ASSERT(height > 0);
const QString cacheKey = iconId + u'@' + QString::number(height);
QPixmap pixmap;
if (!QPixmapCache::find(cacheKey, &pixmap))
{
pixmap = getIcon(iconId).pixmap(height);
QPixmapCache::insert(cacheKey, pixmap);
}
return pixmap;
}
QColor UIThemeManager::getColor(const QString &id, const QColor &defaultColor) const QColor UIThemeManager::getColor(const QString &id, const QColor &defaultColor) const
{ {
return m_colors.value(id, defaultColor); return m_colors.value(id, defaultColor);
@ -272,28 +292,6 @@ QIcon UIThemeManager::getSystrayIcon() const
} }
#endif #endif
Path UIThemeManager::getIconPath(const QString &iconId) const
{
#if (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS))
if (m_useSystemIcons)
{
Path path = Utils::Fs::tempPath() / Path(iconId + u".png");
if (!path.exists())
{
const QIcon icon = QIcon::fromTheme(iconId);
if (!icon.isNull())
icon.pixmap(32).save(path.toString());
else
path = getIconPathFromResources(iconId);
}
return path;
}
#endif
return getIconPathFromResources(iconId);
}
Path UIThemeManager::getIconPathFromResources(const QString &iconId) const Path UIThemeManager::getIconPathFromResources(const QString &iconId) const
{ {
if (m_themeSource) if (m_themeSource)

3
src/gui/uithememanager.h

@ -34,6 +34,7 @@
#include <QHash> #include <QHash>
#include <QIcon> #include <QIcon>
#include <QObject> #include <QObject>
#include <QPixmap>
#include <QString> #include <QString>
#include "base/pathfwd.h" #include "base/pathfwd.h"
@ -58,9 +59,9 @@ public:
static void freeInstance(); static void freeInstance();
static UIThemeManager *instance(); static UIThemeManager *instance();
Path getIconPath(const QString &iconId) const;
QIcon getIcon(const QString &iconId, const QString &fallback = {}) const; QIcon getIcon(const QString &iconId, const QString &fallback = {}) const;
QIcon getFlagIcon(const QString &countryIsoCode) const; QIcon getFlagIcon(const QString &countryIsoCode) const;
QPixmap getScaledPixmap(const QString &iconId, int height) const;
QColor getColor(const QString &id, const QColor &defaultColor) const; QColor getColor(const QString &id, const QColor &defaultColor) const;

20
src/gui/utils.cpp

@ -81,26 +81,6 @@ QPixmap Utils::Gui::scaledPixmap(const Path &path, const QWidget *widget, const
return (height == 0) ? pixmap : pixmap.scaledToHeight(height, Qt::SmoothTransformation); return (height == 0) ? pixmap : pixmap.scaledToHeight(height, Qt::SmoothTransformation);
} }
QPixmap Utils::Gui::scaledPixmapSvg(const Path &path, const QWidget *widget, const int height)
{
// (workaround) svg images require the use of `QIcon()` to load and scale losslessly,
// otherwise other image classes will convert it to pixmap first and follow-up scaling will become lossy.
Q_UNUSED(widget);
Q_ASSERT(height > 0);
const QString cacheKey = path.data() + u'@' + QString::number(height);
QPixmap pixmap;
QPixmapCache cache;
if (!cache.find(cacheKey, &pixmap))
{
pixmap = QIcon(path.data()).pixmap(height);
cache.insert(cacheKey, pixmap);
}
return pixmap;
}
QSize Utils::Gui::smallIconSize(const QWidget *widget) QSize Utils::Gui::smallIconSize(const QWidget *widget)
{ {
// Get DPI scaled icon size (device-dependent), see QT source // Get DPI scaled icon size (device-dependent), see QT source

1
src/gui/utils.h

@ -42,7 +42,6 @@ namespace Utils::Gui
QPixmap scaledPixmap(const QIcon &icon, const QWidget *widget, int height); QPixmap scaledPixmap(const QIcon &icon, const QWidget *widget, int height);
QPixmap scaledPixmap(const Path &path, const QWidget *widget, int height = 0); QPixmap scaledPixmap(const Path &path, const QWidget *widget, int height = 0);
QPixmap scaledPixmapSvg(const Path &path, const QWidget *widget, int height);
QSize smallIconSize(const QWidget *widget = nullptr); QSize smallIconSize(const QWidget *widget = nullptr);
QSize mediumIconSize(const QWidget *widget = nullptr); QSize mediumIconSize(const QWidget *widget = nullptr);

Loading…
Cancel
Save