From bb2030c964258c8ba6f54384b8b5654e09634b0d Mon Sep 17 00:00:00 2001 From: Martchus Date: Thu, 22 Feb 2024 00:59:22 +0100 Subject: [PATCH] Avoid too big icons in certain cases When setting the scale factor of the primary screen to e.g. 100 % and the scale factor of the second screen to something higher like 200 % the icons appear too big on buttons and tabs. This is because icons are apparently just rendered with the required size and the scale factor should therefore not be applied. This change adds overloads to specify a scale factor manually and uses a scale factor of 1.0 for icons. The icons do not look blurry afterwards but are also not too big anymore in the mentioned case. So this is probably how it is supposed to be done. --- iconengineplugin/iconengine.cpp | 2 +- qtforkawesome/renderer.cpp | 43 ++++++++++++++++++++++++++++----- qtforkawesome/renderer.h | 2 ++ 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/iconengineplugin/iconengine.cpp b/iconengineplugin/iconengine.cpp index 3a52e5f..aa0597c 100644 --- a/iconengineplugin/iconengine.cpp +++ b/iconengineplugin/iconengine.cpp @@ -63,7 +63,7 @@ QPixmap IconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State sta color = QGuiApplication::palette().color(group, role); } - auto pixmap = m_renderer.pixmap(QChar(m_char), size, color); + auto pixmap = m_renderer.pixmap(QChar(m_char), size, color, 1.0); #ifdef QT_FORK_AWESOME_ICON_ENGINE_ENABLE_STYLE_SUPPORT auto *const app = qobject_cast(QApplication::instance()); if (auto *const style = app ? app->style() : nullptr) { diff --git a/qtforkawesome/renderer.cpp b/qtforkawesome/renderer.cpp index 2cf5aa6..d1b1ef1 100644 --- a/qtforkawesome/renderer.cpp +++ b/qtforkawesome/renderer.cpp @@ -144,7 +144,7 @@ void QtForkAwesome::Renderer::render(QChar character, QPainter *painter, const Q /*! * \brief Renders the specified \a character as pixmap of the specified \a size. */ -QPixmap QtForkAwesome::Renderer::pixmap(QChar icon, const QSize &size, const QColor &color) const +QPixmap Renderer::pixmap(QChar icon, const QSize &size, const QColor &color, qreal scaleFactor) const { if (auto override = m_d->overrides.find(icon); override != m_d->overrides.end()) { if (const auto &overrideIcon = override->locateIcon(); !overrideIcon.isNull()) { @@ -152,11 +152,14 @@ QPixmap QtForkAwesome::Renderer::pixmap(QChar icon, const QSize &size, const QCo } } - const auto scaleFactor = + if (!scaleFactor) { + scaleFactor = #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) - !QCoreApplication::testAttribute(Qt::AA_UseHighDpiPixmaps) ? 1.0 : + !QCoreApplication::testAttribute(Qt::AA_UseHighDpiPixmaps) ? 1.0 : #endif - qGuiApp->devicePixelRatio(); + (m_d->paintDevice ? m_d->paintDevice->devicePixelRatioF() : qGuiApp->devicePixelRatio()); + } + const auto scaledSize = QSize(size * scaleFactor); auto pm = QPixmap(scaledSize); pm.fill(QColor(Qt::transparent)); @@ -171,9 +174,35 @@ QPixmap QtForkAwesome::Renderer::pixmap(QChar icon, const QSize &size, const QCo /*! * \brief Renders the specified \a icon as pixmap of the specified \a size. */ +QPixmap Renderer::pixmap(Icon icon, const QSize &size, const QColor &color, qreal scaleFactor) const +{ + return pixmap(QChar(static_cast(icon)), size, color, scaleFactor); +} + +/*! + * \brief Renders the specified \a character as pixmap of the specified \a size. + * \remarks + * - The pixmap will be scaled for the associated paint device or use the global device-dixel-ratio if not paint + * device has been associated. + * - When rendering a QPixmap for a QIcon, better the other overloads with the actual size (and a scale factor of + * one). + */ +QPixmap QtForkAwesome::Renderer::pixmap(QChar icon, const QSize &size, const QColor &color) const +{ + return pixmap(icon, size, color, 0.0); +} + +/*! + * \brief Renders the specified \a icon as pixmap of the specified \a size. + * \remarks + * - The pixmap will be scaled for the associated paint device or use the global device-dixel-ratio if not paint + * device has been associated. + * - When rendering a QPixmap for a QIcon, better the other overloads with the actual size (and a scale factor of + * one). + */ QPixmap Renderer::pixmap(Icon icon, const QSize &size, const QColor &color) const { - return pixmap(QChar(static_cast(icon)), size, color); + return pixmap(QChar(static_cast(icon)), size, color, 0.0); } /*! @@ -202,7 +231,9 @@ void Renderer::clearOverrides() /*! * \brief Sets the associated \a paintDevice. - * \remarks The device-pixel-ratio of the specified device will be used when rendering pixmaps. + * \remarks + * The device-pixel-ratio of the specified device will be used when rendering pixmaps using the overloads that + * do *not* take a scale factor. */ void Renderer::setAssociatedPaintDevice(QPaintDevice *paintDevice) { diff --git a/qtforkawesome/renderer.h b/qtforkawesome/renderer.h index 64529be..cdaa097 100644 --- a/qtforkawesome/renderer.h +++ b/qtforkawesome/renderer.h @@ -31,6 +31,8 @@ public: void render(QChar character, QPainter *painter, const QRect &rect, const QColor &color) const; void render(Icon icon, QPainter *painter, const QRect &rect, const QColor &color) const; + QPixmap pixmap(QChar icon, const QSize &size, const QColor &color, qreal scaleFactor) const; + QPixmap pixmap(Icon icon, const QSize &size, const QColor &color, qreal scaleFactor) const; QPixmap pixmap(QChar icon, const QSize &size, const QColor &color) const; QPixmap pixmap(Icon icon, const QSize &size, const QColor &color) const;