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++
Fix offset snapping in the KisCoordinatesConverter
Dmitry Kazakov869bf75c2f44
diff --git a/libs/ui/canvas/kis_canvas_controller.cpp b/libs/ui/canvas/kis_canvas_controller.cppindex 3dda962049..6a5f43f07c 100644--- a/libs/ui/canvas/kis_canvas_controller.cpp+++ b/libs/ui/canvas/kis_canvas_controller.cpp@@ -503,8 +503,8 @@ void KisCanvasController::syncOnImageSizeChange(const QPointF &oldStillPoint, covoid KisCanvasController::updateCanvasOffsetInternal(const QPointF &newOffset){- m_d->coordinatesConverter->setDocumentOffset(- m_d->coordinatesConverter->snapToDevicePixel(newOffset));+ // not need to snap to device pixels, it is done by the converter automatically+ m_d->coordinatesConverter->setDocumentOffset(newOffset);}void KisCanvasController::updateCanvasWidgetSizeInternal(const QSize &newSize)diff --git a/libs/ui/canvas/kis_coordinates_converter.cpp b/libs/ui/canvas/kis_coordinates_converter.cppindex fdd7a980d4..81c8adb5a1 100644--- a/libs/ui/canvas/kis_coordinates_converter.cpp+++ b/libs/ui/canvas/kis_coordinates_converter.cpp@@ -130,7 +130,7 @@ void KisCoordinatesConverter::recalculateOffsetBoundsAndCrop()const QRectF limitRect(m_d->minimumOffset, m_d->maximumOffset);if (!limitRect.contains(m_d->documentOffset)) {- m_d->documentOffset = KisAlgebra2D::clampPoint(m_d->documentOffset, limitRect);+ m_d->documentOffset = snapToDevicePixel(KisAlgebra2D::clampPoint(m_d->documentOffset, limitRect));qDebug() << " corrected offset:" << m_d->documentOffset;correctTransformationToOffset();}@@ -162,7 +162,7 @@ QPoint KisCoordinatesConverter::maximumOffset() const* return its value to be set in the canvas controller explicitly.*/-void KisCoordinatesConverter::correctOffsetToTransformation()+void KisCoordinatesConverter::correctOffsetToTransformationAndSnap(){m_d->documentOffset = snapToDevicePixel(-(imageRectInWidgetPixels().topLeft() -centeringCorrection()));@@ -273,7 +273,7 @@ void KisCoordinatesConverter::setImageBounds(const QRect &rect, const QPointF olrecalculateTransformations();const QPointF newWidgetStillPoint = imageToWidget(newImageStillPoint);- m_d->documentOffset += newWidgetStillPoint - oldWidgetStillPoint;+ m_d->documentOffset = snapToDevicePixel(m_d->documentOffset + newWidgetStillPoint - oldWidgetStillPoint);recalculateTransformations();}@@ -300,27 +300,28 @@ void KisCoordinatesConverter::setImageResolution(qreal xRes, qreal yRes)recalculateTransformations();const QPointF newImageCenter = imageCenterInWidgetPixel();- m_d->documentOffset += newImageCenter - oldImageCenter;+ m_d->documentOffset = snapToDevicePixel(m_d->documentOffset + newImageCenter - oldImageCenter);recalculateTransformations();}-void KisCoordinatesConverter::setDocumentOffsetHiDPIUnaligned(const QPointF& offset)-{- // The given offset is in widget logical pixels. In order to prevent fuzzy- // canvas rendering at 100% pixel-perfect zoom level when devicePixelRatio- // is not integral, we adjusts the offset to map to whole device pixels.-- const QPointF offsetAdjusted = snapToDevicePixel(offset);- setDocumentOffset(offsetAdjusted);-}-void KisCoordinatesConverter::setDocumentOffset(const QPointF& offset){// when changing the offset manually, the mode is explicitly// reset to constantsetZoomMode(KoZoomMode::ZOOM_CONSTANT);- m_d->documentOffset = offset;+ // The given offset is in widget logical pixels. In order to prevent fuzzy+ // canvas rendering at 100% pixel-perfect zoom level when devicePixelRatio+ // is not integral, we adjusts the offset to map to whole device pixels.++ // Steps to reproduce the issue (when no snapping):+ // 1) Download an image with 1px vertical black and white stripes+ // 2) Enable fractional HiDPI support in Krita+ // 3) Set display scaling to 1.5 or 2.5+ // 4) Try to change offset of the image. If offset is unaligned, then+ // the image will disappear on the canvas.++ m_d->documentOffset = snapToDevicePixel(offset);recalculateTransformations();}@@ -427,7 +428,7 @@ void KisCoordinatesConverter::setZoom(KoZoomMode::Mode mode, qreal zoom, qreal rconst QPointF newStillPoint = imageToWidget(oldStillPointInImagePixels);const QPointF offset = newStillPoint - stillPoint;- m_d->documentOffset += offset;+ m_d->documentOffset = snapToDevicePixel(m_d->documentOffset + offset);recalculateTransformations();} else if (mode == KoZoomMode::ZOOM_PAGE || mode == KoZoomMode::ZOOM_WIDTH || mode == KoZoomMode::ZOOM_HEIGHT) {updateDisplayResolution();@@ -466,7 +467,7 @@ void KisCoordinatesConverter::setZoom(KoZoomMode::Mode mode, qreal zoom, qreal rnewDocumentOffset.setY(-cfgMargin);}- m_d->documentOffset = newDocumentOffset;+ m_d->documentOffset = snapToDevicePixel(newDocumentOffset);recalculateTransformations();}}@@ -489,7 +490,7 @@ void KisCoordinatesConverter::zoomTo(const QRectF &zoomRectWidget)const QPointF offset = imageToWidget(zoomPortionCenterInImagePixels) - widgetCenterPoint();QPointF newDocumentOffset = m_d->documentOffset + offset;- m_d->documentOffset = newDocumentOffset;+ m_d->documentOffset = snapToDevicePixel(newDocumentOffset);recalculateTransformations();}@@ -563,7 +564,7 @@ void KisCoordinatesConverter::rotate(QPointF center, qreal angle)m_d->flakeToWidget *= rot;m_d->flakeToWidget *= QTransform::fromTranslate(center.x(), center.y());- correctOffsetToTransformation();+ correctOffsetToTransformationAndSnap();recalculateTransformations();}@@ -602,7 +603,7 @@ void KisCoordinatesConverter::mirror(QPointF center, bool mirrorXAxis, bool mirrm_d->isXAxisMirrored = mirrorXAxis;m_d->isYAxisMirrored = mirrorYAxis;- correctOffsetToTransformation();+ correctOffsetToTransformationAndSnap();recalculateTransformations();}@@ -626,7 +627,7 @@ void KisCoordinatesConverter::resetRotation(QPointF center)m_d->flakeToWidget *= QTransform::fromTranslate(center.x(), center.y());m_d->rotationAngle = 0.0;- correctOffsetToTransformation();+ correctOffsetToTransformationAndSnap();recalculateTransformations();}diff --git a/libs/ui/canvas/kis_coordinates_converter.h b/libs/ui/canvas/kis_coordinates_converter.hindex b6319c7232..cd828d9bdd 100644--- a/libs/ui/canvas/kis_coordinates_converter.h+++ b/libs/ui/canvas/kis_coordinates_converter.h@@ -57,7 +57,6 @@ public:void setImageBounds(const QRect &rect, const QPointF oldImageStillPoint, const QPointF newImageStillPoint);void setImageResolution(qreal xRes, qreal yRes);void setDocumentOffset(const QPointF &offset);- void setDocumentOffsetHiDPIUnaligned(const QPointF& offset);qreal devicePixelRatio() const;QPoint documentOffset() const;@@ -184,7 +183,7 @@ private:friend class KisCoordinatesConverterTest;QPointF centeringCorrection() const;- void correctOffsetToTransformation();+ void correctOffsetToTransformationAndSnap();void correctTransformationToOffset();void recalculateTransformations();void recalculateZoomLevelLimits();diff --git a/libs/ui/tests/kis_coordinates_converter_test.cpp b/libs/ui/tests/kis_coordinates_converter_test.cppindex f7846da074..821273f6c1 100644--- a/libs/ui/tests/kis_coordinates_converter_test.cpp+++ b/libs/ui/tests/kis_coordinates_converter_test.cpp@@ -19,6 +19,9 @@#include "kis_filter_strategy.h"#include <kis_algebra_2d.h>+#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)+Q_DECLARE_METATYPE(KoZoomMode::Mode)+#endifvoid initImage(KisImageSP *image, KoZoomHandler *zoomHandler){@@ -1522,7 +1525,7 @@ void KisCoordinatesConverterTest::testHiDPICanvasSize()converter.setImage(image);converter.setCanvasWidgetSize(converter.snapWidgetSizeToDevicePixel(widgetSize));converter.setZoom(zoom);- converter.setDocumentOffset(converter.snapToDevicePixel(offset));+ converter.setDocumentOffset(offset);QCOMPARE(converter.getCanvasWidgetSize(), expectedWidgetSize);QCOMPARE(converter.documentOffsetF(), expectedOffsetF);@@ -1904,5 +1907,100 @@ void KisCoordinatesConverterTest::testZoomTo()}}+enum TestHiDPIMode+{+ ZoomPageTo7,+ ZoomPageToWidth,+ ScrollToFraction,+ ScrollAway,+ ImageResolution,+ ImageBounds,+ ZoomTo,+ Rotate+};++Q_DECLARE_METATYPE(TestHiDPIMode)++void KisCoordinatesConverterTest::testHiDPIOffsetSnapping_data()+{+ QTest::addColumn<TestHiDPIMode>("testingMode");++ QTest::addRow("page-to-const7") << ZoomPageTo7;+ QTest::addRow("page-to-width") << ZoomPageToWidth;+ QTest::addRow("scroll-to-fraction") << ScrollToFraction;+ QTest::addRow("scroll-away") << ScrollAway;+ QTest::addRow("image-resolution") << ImageResolution;+ QTest::addRow("image-bounds") << ImageBounds;+ QTest::addRow("zoom-to") << ZoomTo;+ QTest::addRow("rotate") << Rotate;++}++void KisCoordinatesConverterTest::testHiDPIOffsetSnapping()+{+ QFETCH(TestHiDPIMode, testingMode);++ KisImageSP image;+ KisCoordinatesConverter converter;+ initImage(&image, &converter);++ KisFilterStrategy *strategy = new KisBilinearFilterStrategy();+ image->scaleImage(QSize(77, 83), image->xRes(), image->yRes(), strategy);+ image->waitForDone();+ QTest::qWait(100);++ converter.setDevicePixelRatio(1.5);+ converter.setImage(image);+ converter.setCanvasWidgetSizeKeepZoom(converter.snapWidgetSizeToDevicePixel(QSize(700, 500)));++ auto roundTo5thDigit = [] (qreal x) {+ return qRound(x * 100000.0) / 100000.0;+ };++ {+ const QPointF imageTopLeftInDevicePixelsHW = converter.imageRectInWidgetPixels().topLeft() * converter.devicePixelRatio();+ QCOMPARE(roundTo5thDigit(imageTopLeftInDevicePixelsHW.x()), qRound(imageTopLeftInDevicePixelsHW.x()));+ QCOMPARE(roundTo5thDigit(imageTopLeftInDevicePixelsHW.y()), qRound(imageTopLeftInDevicePixelsHW.y()));+ }++++ switch (testingMode) {+ case ZoomPageTo7:+ converter.setZoom(KoZoomMode::ZOOM_CONSTANT, 0.17,+ converter.resolutionX(), converter.resolutionY(), converter.imageCenterInWidgetPixel());+ break;+ case ZoomPageToWidth:+ converter.setZoom(KoZoomMode::ZOOM_WIDTH, 1.0,+ converter.resolutionX(), converter.resolutionY(), converter.imageCenterInWidgetPixel());+ break;+ case ScrollToFraction:+ converter.setDocumentOffset(QPointF(-100.33, -200.77));+ break;+ case ScrollAway:+ converter.setCanvasWidgetSizeKeepZoom(converter.snapWidgetSizeToDevicePixel(QSize(701, 503)));+ converter.setDocumentOffset(QPointF(10000.33, 20000.22));+ break;+ case ImageResolution:+ converter.setImageResolution(7.77, 3.13);+ break;+ case ImageBounds:+ converter.setImageBounds(QRect(0,0,113, 117), QPointF(77,17), QPointF(111, 110));+ break;+ case ZoomTo:+ converter.zoomTo(QRectF(13.33, 17.77, 333.17, 234.13));+ break;+ case Rotate:+ converter.rotate(converter.imageCenterInWidgetPixel(), 17.3);+ break;+ }++ {+ const QPointF imageTopLeftInDevicePixelsHW = converter.imageRectInWidgetPixels().topLeft() * converter.devicePixelRatio();+ QCOMPARE(roundTo5thDigit(imageTopLeftInDevicePixelsHW.x()), qRound(imageTopLeftInDevicePixelsHW.x()));+ QCOMPARE(roundTo5thDigit(imageTopLeftInDevicePixelsHW.y()), qRound(imageTopLeftInDevicePixelsHW.y()));+ }+}+SIMPLE_TEST_MAIN(KisCoordinatesConverterTest)diff --git a/libs/ui/tests/kis_coordinates_converter_test.h b/libs/ui/tests/kis_coordinates_converter_test.hindex ba024cabc5..4bc57ee816 100644--- a/libs/ui/tests/kis_coordinates_converter_test.h+++ b/libs/ui/tests/kis_coordinates_converter_test.h@@ -53,6 +53,9 @@ private Q_SLOTS:void testZoomTo_data();void testZoomTo();++ void testHiDPIOffsetSnapping_data();+ void testHiDPIOffsetSnapping();};#endif /* KIS_COORDINATES_CONVERTER_TEST_H */