diff options
| author | Kai Uwe Broulik <[email protected]> | 2023-11-20 14:16:18 +0100 |
|---|---|---|
| committer | Kai Uwe Broulik <[email protected]> | 2023-11-21 13:53:54 +0100 |
| commit | ecd3675aaa55b8224f1fa81b031471d834a18fad (patch) | |
| tree | 11722acc75ab4f5c2a345b0b7880c24957d3ee2d /src/kitemviews | |
| parent | 26adfe689a70af60933809c6106723ff5962b67e (diff) | |
views: Use scene devicePixelRatio rather than qApp
When possible, use the devicePixelRatio from the scene which on Wayland
unlike the QApplication will properly support fractional scaling.
A KItemViewsUtils class is introduced that can be reused elsewhere.
Diffstat (limited to 'src/kitemviews')
| -rw-r--r-- | src/kitemviews/kfileitemlistview.cpp | 5 | ||||
| -rw-r--r-- | src/kitemviews/kstandarditemlistwidget.cpp | 20 | ||||
| -rw-r--r-- | src/kitemviews/private/kitemviewsutils.cpp | 23 | ||||
| -rw-r--r-- | src/kitemviews/private/kitemviewsutils.h | 20 |
4 files changed, 57 insertions, 11 deletions
diff --git a/src/kitemviews/kfileitemlistview.cpp b/src/kitemviews/kfileitemlistview.cpp index 954e6ab88..d763fe287 100644 --- a/src/kitemviews/kfileitemlistview.cpp +++ b/src/kitemviews/kfileitemlistview.cpp @@ -9,6 +9,7 @@ #include "kfileitemlistwidget.h" #include "kfileitemmodel.h" #include "kfileitemmodelrolesupdater.h" +#include "private/kitemviewsutils.h" #include "private/kpixmapmodifier.h" #include <KIconLoader> @@ -157,7 +158,7 @@ QPixmap KFileItemListView::createDragPixmap(const KItemSet &indexes) const yCount = xCount; } - const qreal dpr = scene()->views()[0]->devicePixelRatio(); + const qreal dpr = KItemViewsUtils::devicePixelRatio(this); // Draw the selected items into the grid cells. QPixmap dragPixmap(QSize(xCount * size + xCount, yCount * size + yCount) * dpr); dragPixmap.setDevicePixelRatio(dpr); @@ -255,6 +256,7 @@ void KFileItemListView::onModelChanged(KItemModelBase *current, KItemModelBase * if (current) { m_modelRolesUpdater = new KFileItemModelRolesUpdater(static_cast<KFileItemModel *>(current), this); m_modelRolesUpdater->setIconSize(availableIconSize()); + m_modelRolesUpdater->setDevicePixelRatio(KItemViewsUtils::devicePixelRatio(this)); applyRolesToModel(); } @@ -414,6 +416,7 @@ void KFileItemListView::updateIconSize() } m_modelRolesUpdater->setIconSize(availableIconSize()); + m_modelRolesUpdater->setDevicePixelRatio(KItemViewsUtils::devicePixelRatio(this)); // Update the visible index range (which has most likely changed after the // icon size change) before unpausing m_modelRolesUpdater. diff --git a/src/kitemviews/kstandarditemlistwidget.cpp b/src/kitemviews/kstandarditemlistwidget.cpp index 54e1fa50b..b3721aed9 100644 --- a/src/kitemviews/kstandarditemlistwidget.cpp +++ b/src/kitemviews/kstandarditemlistwidget.cpp @@ -10,6 +10,7 @@ #include "kfileitemmodel.h" #include "private/kfileitemclipboard.h" #include "private/kitemlistroleeditor.h" +#include "private/kitemviewsutils.h" #include "private/kpixmapmodifier.h" #include <KIconEffect> @@ -965,6 +966,7 @@ void KStandardItemListWidget::updatePixmapCache() const bool iconOnTop = (m_layout == IconsLayout); const KItemListStyleOption &option = styleOption(); const qreal padding = option.padding; + const qreal dpr = KItemViewsUtils::devicePixelRatio(this); const int widgetIconSize = iconSize(); const int maxIconWidth = iconOnTop ? widgetSize.width() - 2 * padding : widgetIconSize; @@ -1025,7 +1027,7 @@ void KStandardItemListWidget::updatePixmapCache() } else if (m_pixmap.width() / m_pixmap.devicePixelRatio() != maxIconWidth || m_pixmap.height() / m_pixmap.devicePixelRatio() != maxIconHeight) { // A custom pixmap has been applied. Assure that the pixmap // is scaled to the maximum available size. - KPixmapModifier::scale(m_pixmap, QSize(maxIconWidth, maxIconHeight) * qApp->devicePixelRatio()); + KPixmapModifier::scale(m_pixmap, QSize(maxIconWidth, maxIconHeight) * dpr); } if (m_pixmap.isNull()) { @@ -1073,8 +1075,8 @@ void KStandardItemListWidget::updatePixmapCache() const int maxScaledIconHeight = scaledIconSize; m_scaledPixmapSize = m_pixmap.size(); - m_scaledPixmapSize.scale(maxScaledIconWidth * qApp->devicePixelRatio(), maxScaledIconHeight * qApp->devicePixelRatio(), Qt::KeepAspectRatio); - m_scaledPixmapSize = m_scaledPixmapSize / qApp->devicePixelRatio(); + m_scaledPixmapSize.scale(maxScaledIconWidth * dpr, maxScaledIconHeight * dpr, Qt::KeepAspectRatio); + m_scaledPixmapSize = m_scaledPixmapSize / dpr; if (iconOnTop) { // Center horizontally and align on bottom within the icon-area @@ -1171,7 +1173,7 @@ void KStandardItemListWidget::updateTextsCache() if (ratingSize.width() > availableWidth) { ratingSize.rwidth() = availableWidth; } - const qreal dpr = qApp->devicePixelRatio(); + const qreal dpr = KItemViewsUtils::devicePixelRatio(this); m_rating = QPixmap(ratingSize.toSize() * dpr); m_rating.setDevicePixelRatio(dpr); m_rating.fill(Qt::transparent); @@ -1473,9 +1475,10 @@ void KStandardItemListWidget::updateAdditionalInfoTextColor() void KStandardItemListWidget::drawPixmap(QPainter *painter, const QPixmap &pixmap) { if (m_scaledPixmapSize != pixmap.size() / pixmap.devicePixelRatio()) { + const qreal dpr = KItemViewsUtils::devicePixelRatio(this); QPixmap scaledPixmap = pixmap; - KPixmapModifier::scale(scaledPixmap, m_scaledPixmapSize * qApp->devicePixelRatio()); - scaledPixmap.setDevicePixelRatio(qApp->devicePixelRatio()); + KPixmapModifier::scale(scaledPixmap, m_scaledPixmapSize * dpr); + scaledPixmap.setDevicePixelRatio(dpr); painter->drawPixmap(m_pixmapPos, scaledPixmap); #ifdef KSTANDARDITEMLISTWIDGET_DEBUG @@ -1559,10 +1562,7 @@ void KStandardItemListWidget::closeRoleEditor() QPixmap KStandardItemListWidget::pixmapForIcon(const QString &name, const QStringList &overlays, int size, QIcon::Mode mode) const { static const QIcon fallbackIcon = QIcon::fromTheme(QStringLiteral("unknown")); - qreal dpr = qApp->devicePixelRatio(); - if (scene() && !scene()->views().isEmpty()) { - dpr = scene()->views().constFirst()->devicePixelRatioF(); - } + const qreal dpr = KItemViewsUtils::devicePixelRatio(this); size *= dpr; diff --git a/src/kitemviews/private/kitemviewsutils.cpp b/src/kitemviews/private/kitemviewsutils.cpp new file mode 100644 index 000000000..9e343b6d0 --- /dev/null +++ b/src/kitemviews/private/kitemviewsutils.cpp @@ -0,0 +1,23 @@ +/* + * SPDX-FileCopyrightText: 2023 Kai Uwe Broulik <[email protected]> + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "kitemviewsutils.h" + +#include <QApplication> +#include <QGraphicsItem> +#include <QGraphicsScene> +#include <QGraphicsView> + +qreal KItemViewsUtils::devicePixelRatio(const QGraphicsItem *item) +{ + qreal dpr = qApp->devicePixelRatio(); + if (item->scene()) { + if (const auto views = item->scene()->views(); !views.isEmpty()) { + dpr = views.first()->devicePixelRatioF(); + } + } + return dpr; +} diff --git a/src/kitemviews/private/kitemviewsutils.h b/src/kitemviews/private/kitemviewsutils.h new file mode 100644 index 000000000..e9c8bd3e8 --- /dev/null +++ b/src/kitemviews/private/kitemviewsutils.h @@ -0,0 +1,20 @@ +/* + * SPDX-FileCopyrightText: 2023 Kai Uwe Broulik <[email protected]> + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef KITEMVIEWSUTILS_H +#define KITEMVIEWSUTILS_H + +#include <qtypes.h> + +class QGraphicsItem; + +class KItemViewsUtils +{ +public: + static qreal devicePixelRatio(const QGraphicsItem *item); +}; + +#endif // KITEMVIEWSUTILS_H |
