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.
This commit is contained in:
Martchus 2024-02-22 00:59:22 +01:00
parent 44776c210e
commit bb2030c964
3 changed files with 40 additions and 7 deletions

View File

@ -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 *>(QApplication::instance());
if (auto *const style = app ? app->style() : nullptr) {

View File

@ -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<IconBaseType>(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<IconBaseType>(icon)), size, color);
return pixmap(QChar(static_cast<IconBaseType>(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)
{

View File

@ -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;