|
8 | 8 |
|
9 | 9 | #include <DPaletteHelper> |
10 | 10 |
|
| 11 | +#include <private/qhexstring_p.h> |
11 | 12 | #include <private/qlabel_p.h> |
12 | 13 |
|
13 | 14 | DWIDGET_BEGIN_NAMESPACE |
@@ -257,11 +258,16 @@ void DLabel::paintEvent(QPaintEvent *event) |
257 | 258 | } |
258 | 259 | } else |
259 | 260 | #endif |
260 | | - if (d->pixmap && !d->pixmap->isNull()) { |
261 | | - QPixmap pix; |
262 | | - if (d->scaledcontents) { |
263 | | - QSize scaledSize = cr.size() * devicePixelRatioF(); |
264 | | - if (!d->scaledpixmap || d->scaledpixmap->size() != scaledSize) { |
| 261 | +#if QT_VERSION < QT_VERSION_CHECK(6, 9, 0) |
| 262 | + if (d->pixmap && !d->pixmap->isNull()) { |
| 263 | +#else |
| 264 | + if (d->icon && !d->icon->isNull()) { |
| 265 | +#endif |
| 266 | +#if QT_VERSION < QT_VERSION_CHECK(6, 9, 0) |
| 267 | + QPixmap pix; |
| 268 | + if (d->scaledcontents) { |
| 269 | + QSize scaledSize = cr.size() * devicePixelRatioF(); |
| 270 | + if (!d->scaledpixmap || d->scaledpixmap->size() != scaledSize) { |
265 | 271 | #if QT_VERSION < QT_VERSION_CHECK(6, 8, 0) |
266 | 272 | if (!d->cachedimage) |
267 | 273 | #endif |
@@ -290,15 +296,45 @@ void DLabel::paintEvent(QPaintEvent *event) |
290 | 296 | #endif |
291 | 297 | #endif |
292 | 298 | d->scaledpixmap->setDevicePixelRatio(devicePixelRatioF()); |
293 | | - } |
| 299 | + } |
294 | 300 | pix = *d->scaledpixmap; |
295 | | - } else |
296 | | - pix = *d->pixmap; |
297 | | - QStyleOption opt; |
298 | | - opt.initFrom(this); |
299 | | - if (!isEnabled()) |
300 | | - pix = style->generatedIconPixmap(QIcon::Disabled, pix, &opt); |
301 | | - style->drawItemPixmap(&painter, cr, align, pix); |
| 301 | + } else |
| 302 | + pix = *d->pixmap; |
| 303 | + QStyleOption opt; |
| 304 | + opt.initFrom(this); |
| 305 | + if (!isEnabled()) |
| 306 | + pix = style->generatedIconPixmap(QIcon::Disabled, pix, &opt); |
| 307 | + style->drawItemPixmap(&painter, cr, align, pix); |
| 308 | +#else |
| 309 | + // pick up from |
| 310 | + // https://github.com/qt/qtbase/blob/25986746947798e1a22d0830d3bcb11a55fcd3ae/src/widgets/widgets/qlabel.cpp#L1052 |
| 311 | + const qreal dpr = devicePixelRatio(); |
| 312 | + const QSize size = d->scaledcontents ? cr.size() : d->pixmapSize; |
| 313 | + const auto mode = isEnabled() ? QIcon::Normal : QIcon::Disabled; |
| 314 | + QPixmap pix = d->icon->pixmap(size, dpr, mode); |
| 315 | + if (d->scaledcontents && pix.size() != size * dpr) { |
| 316 | + using namespace Qt::StringLiterals; |
| 317 | + const QString key = |
| 318 | + "qt_label_"_L1 % HexString<quint64>(pix.cacheKey()) % |
| 319 | + HexString<quint8>(mode) % HexString<uint>(size.width()) % |
| 320 | + HexString<uint>(size.height()) % |
| 321 | + HexString<quint16>(qRound(dpr * 1000)); |
| 322 | + if (!QPixmapCache::find(key, &pix)) { |
| 323 | + pix = pix.scaled(size * dpr, Qt::IgnoreAspectRatio, |
| 324 | + Qt::SmoothTransformation); |
| 325 | + pix.setDevicePixelRatio(dpr); |
| 326 | + // using QIcon to cache the newly create pixmap is not possible |
| 327 | + // because QIcon does not clear this cache (so we grow indefinitely) |
| 328 | + // and also uses the newly added pixmap as starting point for new |
| 329 | + // scaled pixmap which makes it very blurry. |
| 330 | + // Therefore use QPixmapCache here. |
| 331 | + QPixmapCache::insert(key, pix); |
| 332 | + } |
| 333 | + } |
| 334 | + QStyleOption opt; |
| 335 | + opt.initFrom(this); |
| 336 | + style->drawItemPixmap(&painter, cr, align, pix); |
| 337 | +#endif |
302 | 338 | } |
303 | 339 | } |
304 | 340 |
|
|
0 commit comments