Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Improve rendering of pattern thumbnails
Freya Lupen349eb68d7293
diff --git a/libs/resourcewidgets/KisResourceThumbnailPainter.cpp b/libs/resourcewidgets/KisResourceThumbnailPainter.cppindex 7663db2e2d..1b7d8f9ed3 100644--- a/libs/resourcewidgets/KisResourceThumbnailPainter.cpp+++ b/libs/resourcewidgets/KisResourceThumbnailPainter.cpp@@ -50,6 +50,7 @@ void KisResourceThumbnailPainter::paint(QPainter *painter, const QModelIndex& in}QRect innerRect = addMargin ? rect.adjusted(2, 2, -2, -2) : rect;+ QSize innerRectSizeDPI = innerRect.size() * devicePixelRatioF;painter->setRenderHint(QPainter::SmoothPixmapTransform, true);@@ -57,7 +58,7 @@ void KisResourceThumbnailPainter::paint(QPainter *painter, const QModelIndex& inm_checkerPainter.paint(*painter, innerRect, innerRect.topLeft());if (!thumbnail.isNull()) {thumbnail = KisResourceThumbnailCache::instance()->getImage(index,- innerRect.size() * devicePixelRatioF,+ innerRectSizeDPI,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);thumbnail.setDevicePixelRatio(devicePixelRatioF);@@ -65,16 +66,21 @@ void KisResourceThumbnailPainter::paint(QPainter *painter, const QModelIndex& in}} else if (resourceType == ResourceType::Patterns) {painter->fillRect(innerRect, Qt::white); // no checkers, they are confusing with patterns.- if (!thumbnail.isNull() && (thumbnail.height() > innerRect.height() || thumbnail.width() > innerRect.width())) {- thumbnail = KisResourceThumbnailCache::instance()->getImage(index,- innerRect.size() * devicePixelRatioF,- Qt::KeepAspectRatio,- Qt::SmoothTransformation);- thumbnail.setDevicePixelRatio(devicePixelRatioF);+ // for large patterns, scale down to no less than 50% zoom and crop it,+ // to see distinguishing details without being too zoomed in+ double scale = qMin(double(thumbnail.height()) / innerRectSizeDPI.height(), double(thumbnail.width()) / innerRectSizeDPI.width());+ if (!thumbnail.isNull() && (scale >= 1.0)) {+ scale = qMin(scale, 2.0);+ QRect sourceRect = QRect(0, 0, innerRectSizeDPI.width() * scale, innerRectSizeDPI.height() * scale);+ painter->drawImage(innerRect, thumbnail, sourceRect);+ } else {+ // crisply transform small patterns, which get scaled up to user-space,+ // preventing them from being too tiny to see on hi-dpi+ painter->setRenderHint(QPainter::SmoothPixmapTransform, false);+ QBrush patternBrush(thumbnail);+ painter->setBrushOrigin(innerRect.topLeft());+ painter->fillRect(innerRect, patternBrush);}- QBrush patternBrush(thumbnail);- patternBrush.setTransform(QTransform::fromTranslate(innerRect.x(), innerRect.y()));- painter->fillRect(innerRect, patternBrush);} else if (resourceType == ResourceType::WindowLayouts) {// TODO: thumbnails for window layouts?painter->fillRect(innerRect, Qt::white);@@ -84,13 +90,13 @@ void KisResourceThumbnailPainter::paint(QPainter *painter, const QModelIndex& inpainter->setPen(before);} else {painter->fillRect(innerRect, Qt::white); // no checkers, they are confusing with patterns.- if (!thumbnail.isNull() && !(thumbnail.height() == innerRect.height() * devicePixelRatioF &&- thumbnail.width() == innerRect.width() * devicePixelRatioF)) {- bool needsUpscaling = thumbnail.height() < innerRect.height() * devicePixelRatioF- || thumbnail.width() < innerRect.width() * devicePixelRatioF;+ if (!thumbnail.isNull() && !(thumbnail.height() == innerRectSizeDPI.height() &&+ thumbnail.width() == innerRectSizeDPI.width())) {+ bool needsUpscaling = thumbnail.height() < innerRectSizeDPI.height()+ || thumbnail.width() < innerRectSizeDPI.width();thumbnail = KisResourceThumbnailCache::instance()->getImage(index,- innerRect.size() * devicePixelRatioF,+ innerRectSizeDPI,Qt::KeepAspectRatio,needsUpscaling ? Qt::FastTransformation: Qt::SmoothTransformation);@@ -98,10 +104,10 @@ void KisResourceThumbnailPainter::paint(QPainter *painter, const QModelIndex& in}QPoint topleft(innerRect.topLeft());- if (thumbnail.width() < innerRect.width()*devicePixelRatioF) {+ if (thumbnail.width() < innerRectSizeDPI.width()) {topleft.setX(topleft.x() + (innerRect.width() - thumbnail.width()/devicePixelRatioF) / 2);}- if (thumbnail.height() < innerRect.height()*devicePixelRatioF) {+ if (thumbnail.height() < innerRectSizeDPI.height()) {topleft.setY(topleft.y() + (innerRect.height() - thumbnail.height()/devicePixelRatioF) / 2);}painter->drawImage(topleft, thumbnail);