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++
Initial implementation of a unittest for coordinates converter
Dmitry Kazakov9ea33d22ae5e
diff --git a/libs/ui/canvas/kis_coordinates_converter.cpp b/libs/ui/canvas/kis_coordinates_converter.cppindex b6cceaacfe..bec8ffdbde 100644--- a/libs/ui/canvas/kis_coordinates_converter.cpp+++ b/libs/ui/canvas/kis_coordinates_converter.cpp@@ -15,10 +15,13 @@#include <kis_config.h>#include <kis_image.h>+#include <kis_algebra_2d.h>struct KisCoordinatesConverter::Private {Private():+ imageXRes(1.0),+ imageYRes(1.0),isXAxisMirrored(false),isYAxisMirrored(false),isRotating(false),@@ -29,7 +32,9 @@ struct KisCoordinatesConverter::Private {{}- KisImageWSP image;+ QRect imageBounds;+ qreal imageXRes;+ qreal imageYRes;bool isXAxisMirrored;bool isYAxisMirrored;@@ -40,6 +45,8 @@ struct KisCoordinatesConverter::Private {QSizeF canvasWidgetSize;qreal devicePixelRatio;QPointF documentOffset;+ QPoint minimumOffset;+ QPoint maximumOffset;QTransform flakeToWidget;QTransform rotationBaseTransform;@@ -83,6 +90,42 @@ QPointF KisCoordinatesConverter::centeringCorrection() constreturn -range;}+void KisCoordinatesConverter::recalculateOffsetBoundsAndCrop()+{+ if (!m_d->canvasWidgetSize.isValid()) return;++ KisConfig cfg(true);++ // TODO: take references into account+ QSize documentSize = imageRectInWidgetPixels().toAlignedRect().size();+ QPointF dPoint(documentSize.width(), documentSize.height());+ QPointF wPoint(m_d->canvasWidgetSize.width(), m_d->canvasWidgetSize.height());++ QPointF minOffset = -cfg.vastScrolling() * wPoint;+ QPointF maxOffset = dPoint - wPoint + cfg.vastScrolling() * wPoint;++ m_d->minimumOffset = minOffset.toPoint();+ m_d->maximumOffset = maxOffset.toPoint();++ const QRectF limitRect(m_d->minimumOffset, m_d->maximumOffset);++ if (!limitRect.contains(m_d->documentOffset)) {+ m_d->documentOffset = KisAlgebra2D::clampPoint(m_d->documentOffset, limitRect);+ qDebug() << " corrected offset:" << m_d->documentOffset;+ correctTransformationToOffset();+ }+}++QPoint KisCoordinatesConverter::minimumOffset() const+{+ return m_d->minimumOffset;+}++QPoint KisCoordinatesConverter::maximumOffset() const+{+ return m_d->maximumOffset;+}+/*** The document offset and the position of the top left corner of the* image must always coincide, that is why we need to correct them to@@ -115,16 +158,15 @@ void KisCoordinatesConverter::correctTransformationToOffset()void KisCoordinatesConverter::recalculateTransformations(){- if(!m_d->image) return;-- m_d->imageToDocument = QTransform::fromScale(1 / m_d->image->xRes(),- 1 / m_d->image->yRes());+ m_d->imageToDocument = QTransform::fromScale(1 / m_d->imageXRes,+ 1 / m_d->imageYRes);qreal zoomX, zoomY;KoZoomHandler::zoom(&zoomX, &zoomY);m_d->documentToFlake = QTransform::fromScale(zoomX, zoomY);correctTransformationToOffset();+ recalculateOffsetBoundsAndCrop();QRectF irect = imageRectInWidgetPixels();QRectF wrect = QRectF(QPoint(0,0), m_d->canvasWidgetSize);@@ -164,7 +206,43 @@ void KisCoordinatesConverter::setDevicePixelRatio(qreal value)void KisCoordinatesConverter::setImage(KisImageWSP image){- m_d->image = image;+ m_d->imageXRes = image->xRes();+ m_d->imageYRes = image->yRes();++ // we should **not** call setResolution() here, since+ // it is a different kind of resolution that is used+ // to convert the image to the physical size of the display++ m_d->imageBounds = image->bounds();+ recalculateTransformations();+}++void KisCoordinatesConverter::setImageBounds(const QRect &rect)+{+ if (rect == m_d->imageBounds) return;+ m_d->imageBounds = rect;+ recalculateTransformations();+}++void KisCoordinatesConverter::setImageResolution(qreal xRes, qreal yRes)+{+ // we consiter the center of the image to be the still point+ // on the canvas++ if (qFuzzyCompare(xRes, m_d->imageXRes) && qFuzzyCompare(yRes, m_d->imageYRes)) return;++ const QPointF oldImageCenter = imageCenterInWidgetPixel();++ // we should **not** call setResolution() here, since+ // it is a different kind of resolution that is used+ // to convert the image to the physical size of the display++ m_d->imageXRes = xRes;+ m_d->imageYRes = yRes;+ recalculateTransformations();++ const QPointF newImageCenter = imageCenterInWidgetPixel();+ m_d->documentOffset += newImageCenter - oldImageCenter;recalculateTransformations();}@@ -198,6 +276,133 @@ void KisCoordinatesConverter::setZoom(qreal zoom)recalculateTransformations();}+void KisCoordinatesConverter::setZoom(KoZoomMode::Mode mode, qreal zoom, qreal resolutionX, qreal resolutionY, const QPointF &stillPoint)+{+ if (this->zoomMode() == mode &&+ qFuzzyCompare(this->zoom(), zoom) &&+ qFuzzyCompare(m_d->imageXRes, resolutionX) &&+ qFuzzyCompare(m_d->imageYRes, resolutionY)) {+ return; // no change+ }++ const int cfgMargin = zoomMarginSize();++ // TODO: add resolution change++ if(mode == KoZoomMode::ZOOM_CONSTANT) {+ if(qFuzzyIsNull(zoom)) return;+ const QPointF oldStillPointInImagePixels =+ widgetToImage(stillPoint);++ KoZoomHandler::setZoom(zoom);+ recalculateTransformations();++ const QPointF newStillPoint = imageToWidget(oldStillPointInImagePixels);+ setDocumentOffset(-newStillPoint + oldStillPointInImagePixels);+ } else if (mode == KoZoomMode::ZOOM_WIDTH || mode == KoZoomMode::ZOOM_HEIGHT) {+ // clang-format off+ struct DimensionWrapperHorizontal {+ qreal primaryLength(const QRectF &obj) { return obj.width(); }+ qreal primaryLength(const QSizeF &obj) { return obj.width(); }+ qreal primaryStart(const QRectF &obj) { return obj.left(); }+ qreal primaryEnd(const QRectF &obj) { return obj.right(); }+ qreal secondaryStart(const QRectF &obj) { return obj.top(); }+ qreal secondaryEnd(const QRectF &obj) { return obj.bottom(); }+ qreal& primaryRef(QPointF &pt) { return pt.rx(); }+ qreal& secondaryRef(QPointF &pt) { return pt.ry(); }+ qreal primary(const QPointF &pt) { return pt.x(); }+ qreal secondary(const QPointF &pt) { return pt.y(); }+ };++ struct DimensionWrapperVertical {+ qreal primaryLength(const QRectF &obj) { return obj.height(); }+ qreal primaryLength(const QSizeF &obj) { return obj.height(); }+ qreal primaryStart(const QRectF &obj) { return obj.top(); }+ qreal primaryEnd(const QRectF &obj) { return obj.bottom(); }+ qreal secondaryStart(const QRectF &obj) { return obj.left(); }+ qreal secondaryEnd(const QRectF &obj) { return obj.right(); }+ qreal& primaryRef(QPointF &pt) { return pt.ry(); }+ qreal& secondaryRef(QPointF &pt) { return pt.rx(); }+ qreal primary(const QPointF &pt) { return pt.y(); }+ qreal secondary(const QPointF &pt) { return pt.x(); }+ };+ // clang-format on++ auto zoomToDimension = [&](auto dim) {+ /**+ * We try not to move the image alond the secondary axis, we cleverly choose+ * a still point and pin it along the transformation+ */+ QPointF stillPointInImagePixels;+ QPointF stillPointInOldWidgetPixels;++ {+ /**+ * Depending on whether the image covers the center of the widget,+ * we either scale relative to the center of the widget or the center+ * of the image.+ */+ const QPointF widgetCenterInImagePixels = widgetToImage(widgetCenterPoint());++ if (dim.secondary(widgetCenterInImagePixels) >= dim.secondaryStart(imageRectInImagePixels())+ && dim.secondary(widgetCenterInImagePixels) <= dim.secondaryEnd(imageRectInImagePixels())) {+ stillPointInImagePixels = widgetCenterInImagePixels;+ stillPointInOldWidgetPixels = widgetCenterPoint();+ } else {+ stillPointInImagePixels = QRectF(imageRectInImagePixels()).center();+ stillPointInOldWidgetPixels = imageToWidget(stillPointInImagePixels);+ }+ }++ const QSize documentSize = imageRectInWidgetPixels().toAlignedRect().size();+ const qreal zoomCoeff =+ (dim.primaryLength(m_d->canvasWidgetSize) - 2 * cfgMargin) / dim.primaryLength(documentSize);++ KoZoomHandler::setZoom(this->zoom() * zoomCoeff);+ recalculateTransformations();++ const QPointF stillPointInNewWidgetPixels = imageToWidget(stillPointInImagePixels);+ const qreal verticalOffset =+ -dim.secondary(stillPointInOldWidgetPixels) + dim.secondary(stillPointInNewWidgetPixels);+ QPointF newDocumentOffset;+ dim.primaryRef(newDocumentOffset) = -cfgMargin;+ dim.secondaryRef(newDocumentOffset) = dim.secondary(m_d->documentOffset) + verticalOffset;++ setDocumentOffset(newDocumentOffset);+ };++ if (mode == KoZoomMode::ZOOM_WIDTH) {+ zoomToDimension(DimensionWrapperHorizontal{});+ } else {+ zoomToDimension(DimensionWrapperVertical{});+ }++ } else if (mode == KoZoomMode::ZOOM_PAGE) {+ const QSize documentSize = imageRectInWidgetPixels().toAlignedRect().size();+ const qreal zoomCoeffX =+ (m_d->canvasWidgetSize.width() - 2 * cfgMargin) / documentSize.width();+ const qreal zoomCoeffY =+ (m_d->canvasWidgetSize.height() - 2 * cfgMargin) / documentSize.height();++ KoZoomHandler::setZoom(this->zoom() * qMin(zoomCoeffX, zoomCoeffY));+ recalculateTransformations();++ const QPointF offset = imageCenterInWidgetPixel() - widgetCenterPoint();++ QPointF newDocumentOffset = m_d->documentOffset + offset;++ // just explicitly set minimal axis offset to zero to+ // avoid imperfections of floating point numbers+ if (zoomCoeffX < zoomCoeffY) {+ newDocumentOffset.setX(-cfgMargin);+ } else {+ newDocumentOffset.setY(-cfgMargin);+ }++ setDocumentOffset(newDocumentOffset);+ }+}+qreal KisCoordinatesConverter::effectiveZoom() const{qreal scaleX, scaleY;@@ -420,10 +625,7 @@ void KisCoordinatesConverter::getOpenGLCheckersInfo(const QRectF &viewportRect,QPointF KisCoordinatesConverter::imageCenterInWidgetPixel() const{- if(!m_d->image)- return QPointF();-- QPolygonF poly = imageToWidget(QPolygon(m_d->image->bounds()));+ QPolygonF poly = imageToWidget(QPolygon(m_d->imageBounds));return (poly[0] + poly[1] + poly[2] + poly[3]) / 4.0;}@@ -432,35 +634,29 @@ QPointF KisCoordinatesConverter::imageCenterInWidgetPixel() constQRectF KisCoordinatesConverter::imageRectInWidgetPixels() const{- if(!m_d->image) return QRectF();- return imageToWidget(m_d->image->bounds());+ return imageToWidget(m_d->imageBounds);}QRectF KisCoordinatesConverter::imageRectInViewportPixels() const{- if(!m_d->image) return QRectF();- return imageToViewport(m_d->image->bounds());+ return imageToViewport(m_d->imageBounds);}QRect KisCoordinatesConverter::imageRectInImagePixels() const{- if(!m_d->image) return QRect();- return m_d->image->bounds();+ return m_d->imageBounds;}QRectF KisCoordinatesConverter::imageRectInDocumentPixels() const{- if(!m_d->image) return QRectF();- return imageToDocument(m_d->image->bounds());+ return imageToDocument(m_d->imageBounds);}QSizeF KisCoordinatesConverter::imageSizeInFlakePixels() const{- if(!m_d->image) return QSizeF();-qreal scaleX, scaleY;imageScale(&scaleX, &scaleY);- QSize imageSize = m_d->image->size();+ QSize imageSize = m_d->imageBounds.size();return QSizeF(imageSize.width() * scaleX, imageSize.height() * scaleY);}@@ -489,19 +685,13 @@ QPointF KisCoordinatesConverter::widgetCenterPoint() constvoid KisCoordinatesConverter::imageScale(qreal *scaleX, qreal *scaleY) const{- if(!m_d->image) {- *scaleX = 1.0;- *scaleY = 1.0;- return;- }-// get the x and y zoom level of the canvasqreal zoomX, zoomY;KoZoomHandler::zoom(&zoomX, &zoomY);// Get the KisImage resolution- qreal resX = m_d->image->xRes();- qreal resY = m_d->image->yRes();+ qreal resX = m_d->imageXRes;+ qreal resY = m_d->imageYRes;// Compute the scale factors*scaleX = zoomX / resX;diff --git a/libs/ui/canvas/kis_coordinates_converter.h b/libs/ui/canvas/kis_coordinates_converter.hindex 7d1ef47166..1376a5045e 100644--- a/libs/ui/canvas/kis_coordinates_converter.h+++ b/libs/ui/canvas/kis_coordinates_converter.h@@ -52,6 +52,8 @@ public:void setCanvasWidgetSize(QSizeF size);void setDevicePixelRatio(qreal value);void setImage(KisImageWSP image);+ void setImageBounds(const QRect &rect);+ void setImageResolution(qreal xRes, qreal yRes);void setDocumentOffset(const QPointF &offset);qreal devicePixelRatio() const;@@ -73,6 +75,8 @@ public:void setZoom(qreal zoom) override;+ void setZoom(KoZoomMode::Mode mode, qreal zoom, qreal resolutionX, qreal resolutionY, const QPointF &stillPoint);+/*** A composition of to scale methods: zoom level + image resolution*/@@ -151,6 +155,9 @@ public:QPointF snapToDevicePixel(const QPointF &point) const;+ QPoint minimumOffset() const;+ QPoint maximumOffset() const;+public:// overrides from KoViewConverterQTransform viewToWidget() const override;@@ -158,11 +165,13 @@ public:private:friend class KisZoomAndPanTest;+ friend class KisCoordinatesConverterTest;QPointF centeringCorrection() const;void correctOffsetToTransformation();void correctTransformationToOffset();void recalculateTransformations();+ void recalculateOffsetBoundsAndCrop();private:struct Private;diff --git a/libs/ui/tests/kis_coordinates_converter_test.cpp b/libs/ui/tests/kis_coordinates_converter_test.cppindex a9547b3136..3122026a21 100644--- a/libs/ui/tests/kis_coordinates_converter_test.cpp+++ b/libs/ui/tests/kis_coordinates_converter_test.cpp@@ -14,6 +14,8 @@#include <kis_image.h>#include "kis_coordinates_converter.h"+#include "kis_filter_strategy.h"+#include <kis_algebra_2d.h>void initImage(KisImageSP *image, KoZoomHandler *zoomHandler)@@ -254,6 +256,486 @@ void KisCoordinatesConverterTest::testMirroringCanvasBiggerThanImage()QCOMPARE(viewportRect, QRectF(300,100,200,200));}+void KisCoordinatesConverterTest::testCanvasOffset()+{+ KisImageSP image;+ KisCoordinatesConverter converter;+ initImage(&image, &converter);++ converter.setImage(image);+ converter.setDocumentOffset(QPoint(0,0));+ converter.setCanvasWidgetSize(QSize(500,500));+ converter.setZoom(1.);++ qDebug() << ppVar(converter.imageToWidget(QPointF(0,0)));++ converter.setZoom(0.5);++ qDebug() << ppVar(converter.imageToWidget(QPointF(0,0)));++ {+ // move the image to the top and left direction (the offset is inverted)+ converter.setDocumentOffset(QPoint(0, 0));+ qDebug() << "before mirror" << ppVar(converter.documentOffset());++ qDebug() << " " << ppVar(converter.imageToWidget(QPointF(0, 0)));+ qDebug() << " " << ppVar(converter.imageToWidget(QPointF(1000, 0)));++ converter.mirror(converter.widgetCenterPoint(), true, false);+ qDebug() << "after mirror" << ppVar(converter.documentOffset());++ qDebug() << " " << ppVar(converter.imageToWidget(QPointF(0, 0)));+ qDebug() << " " << ppVar(converter.imageToWidget(QPointF(1000, 0)));++ converter.mirror(converter.widgetCenterPoint(), false, false);+ }++ {+ // move the image to the top and left direction (the offset is inverted)+ converter.setDocumentOffset(QPoint(100, 200));+ qDebug() << "before mirror" << ppVar(converter.documentOffset());++ qDebug() << " " << ppVar(converter.imageToWidget(QPointF(0, 0)));+ qDebug() << " " << ppVar(converter.imageToWidget(QPointF(1000, 0)));++ converter.mirror(converter.widgetCenterPoint(), true, false);+ qDebug() << "after mirror" << ppVar(converter.documentOffset());++ qDebug() << " " << ppVar(converter.imageToWidget(QPointF(0, 0)));+ qDebug() << " " << ppVar(converter.imageToWidget(QPointF(1000, 0)));++ converter.mirror(converter.widgetCenterPoint(), false, false);+ }++ {+ // move the image to the top and left direction (the offset is inverted)+ converter.setDocumentOffset(QPoint(100, 200));+ qDebug() << "before rotate" << ppVar(converter.documentOffset());++ qDebug() << " " << ppVar(converter.imageToWidget(QPointF(0, 0)));+ qDebug() << " " << ppVar(converter.imageToWidget(QPointF(1000, 0)));++ converter.rotate(converter.widgetCenterPoint(), 30);+ qDebug() << "after rotate" << ppVar(converter.documentOffset());++ qDebug() << " " << ppVar(converter.imageToWidget(QPointF(0, 0)));+ qDebug() << " " << ppVar(converter.imageToWidget(QPointF(1000, 0)));+ qDebug() << " " << ppVar(converter.imageToWidget(QPointF(1000, 1000)));+ qDebug() << " " << ppVar(converter.imageToWidget(QPointF(0, 1000)));++ converter.mirror(converter.widgetCenterPoint(), false, false);+ }+}++void KisCoordinatesConverterTest::testImageSmallerThanCanvas()+{+ KisImageSP image;+ KisCoordinatesConverter converter;+ initImage(&image, &converter);++ converter.setImage(image);+ converter.setDocumentOffset(QPoint(0,0));+ converter.setCanvasWidgetSize(QSize(500,500));+ converter.setZoom(1.);++ qDebug() << ppVar(converter.imageToWidget(QPointF(0,0)));++ converter.setZoom(0.5);++}++void KisCoordinatesConverterTest::testImageSizeChange()+{+ KisImageSP image;+ KisCoordinatesConverter converter;+ initImage(&image, &converter);++ converter.setImage(image);+ converter.setDocumentOffset(QPoint(0,0));+ converter.setCanvasWidgetSize(QSize(500,500));+ converter.setZoom(0.5);++ const QRect oldImageBounds = converter.imageRectInImagePixels();+ const QRect oldImageBoundsInWidget = converter.imageRectInWidgetPixels().toAlignedRect();++ image->resizeImage(QRect(0,0,500,500));+ image->waitForDone();+ QTest::qWait(100);++ /**+ * We do **not** directly link the converter to the image.+ * All updates to the converter should be propagated manually+ * by KisView+ */++ QCOMPARE(converter.imageRectInImagePixels(), oldImageBounds);+ QCOMPARE(converter.imageRectInWidgetPixels(), oldImageBoundsInWidget);++ converter.setImageBounds(image->bounds());++ QCOMPARE(converter.imageRectInImagePixels(), image->bounds());+ QCOMPARE(converter.imageRectInWidgetPixels(), QRect(0,0,250,250));+}++void KisCoordinatesConverterTest::testImageResolutionChange()+{+ KisImageSP image;+ KisCoordinatesConverter converter;+ initImage(&image, &converter);++ converter.setImage(image);+ converter.setDocumentOffset(QPoint(0,0));+ converter.setCanvasWidgetSize(QSize(500,500));+ converter.setZoom(0.5);++ qreal oldXScale = 0.0, oldYScale = 0.0;+ converter.imageScale(&oldXScale, &oldYScale);+ const QRectF oldImageRectInDocumentPixels = converter.imageRectInDocumentPixels();++ /**+ * The center of the image should be kept unchanged when resolution changes+ */+ const QPointF oldImageCenterInWidgetPixels = converter.imageCenterInWidgetPixel();+ qDebug() << "0" << ppVar(converter.imageCenterInWidgetPixel());++ KisFilterStrategy *strategy = new KisBilinearFilterStrategy();+ image->scaleImage(image->size(), 200, 200, strategy);+ image->waitForDone();+ QTest::qWait(100);++ /**+ * We do **not** directly link the converter to the image.+ * All updates to the converter should be propagated manually+ * by KisView+ */++ qreal newXScale = 0.0, newYScale = 0.0;++ converter.imageScale(&newXScale, &newYScale);+ QCOMPARE(newXScale, oldXScale);+ QCOMPARE(newYScale, oldYScale);+ QCOMPARE(converter.imageRectInDocumentPixels(), oldImageRectInDocumentPixels);+ QCOMPARE(converter.imageCenterInWidgetPixel(), oldImageCenterInWidgetPixels);++ converter.setImageResolution(image->xRes(), image->yRes());++ qDebug() << "1" << ppVar(converter.imageCenterInWidgetPixel());++ converter.imageScale(&newXScale, &newYScale);+ QCOMPARE(newXScale, 0.5 * oldXScale);+ QCOMPARE(newYScale, 0.5 * oldYScale);+ QCOMPARE(converter.imageRectInDocumentPixels(), QRectF(0,0,5,5));+ // the position of image center should be the same+ QCOMPARE(converter.imageCenterInWidgetPixel(), oldImageCenterInWidgetPixels);+}++void KisCoordinatesConverterTest::testOffsetLimits()+{+ KisImageSP image;+ KisCoordinatesConverter converter;+ initImage(&image, &converter);++ const QRect widgetRect(0,0,500,500);+ converter.setImage(image);+ converter.setDocumentOffset(QPoint(0,0));+ converter.setCanvasWidgetSize(widgetRect.size()); // should be initialized first+ converter.setZoom(0.5);++ const QPointF widgetSizeVector(widgetRect.width(), widgetRect.height());++ // initial offset is null, hence the topleft is at the origin of the widget+ QCOMPARE(converter.imageToWidget(QPointF(0,0)), QPointF(0,0));++ // when the scrollbars offset in minimal, the topleft of the image is moved+ // to the bottom-right corner of the widget+ converter.setDocumentOffset(converter.minimumOffset());+ QCOMPARE(converter.imageToWidget(QPointF(0,0)), 0.9 * widgetSizeVector);++ // when the scrollbars offset in maximal, the topleft of the image is moved+ // under the left side of the widget and we see only the bottom-right corner+ // of the image.+ converter.setDocumentOffset(converter.maximumOffset());+ QCOMPARE(converter.imageToWidget(QPointF(1000,1000)), 0.1 * widgetSizeVector);+}++void KisCoordinatesConverterTest::testOffsetLimitsCropping()+{+ KisImageSP image;+ KisCoordinatesConverter converter;+ initImage(&image, &converter);++ const QRect widgetRect(0,0,500,500);+ converter.setImage(image);+ converter.setDocumentOffset(QPoint(0,0));+ converter.setCanvasWidgetSize(widgetRect.size()); // should be initialized first+ converter.setZoom(0.5);++ QCOMPARE(converter.documentOffset(), QPoint(0,0));+ QCOMPARE(converter.imageToWidget(QPointF(0,0)), QPointF(0,0));+ QCOMPARE(converter.imageToWidget(QPointF(1000,1000)), QPointF(500,500));++ // This mirror operation cause clipping of the offset, since+ // the left edge of the document shoudl appear at position+ // 480, which is higer than the limit of 450.+ converter.mirror(QPointF(490, 0), true, false);++ QCOMPARE(converter.documentOffset(), QPoint(-450,0));+ QCOMPARE(converter.imageToWidget(QPointF(0,0)), QPointF(950,0));+ QCOMPARE(converter.imageToWidget(QPointF(1000,1000)), QPointF(450,500));++ // return the canvas back to the topleft (though mirrored)+ converter.setDocumentOffset(QPoint(0,0));+ QCOMPARE(converter.documentOffset(), QPoint(0,0));+ QCOMPARE(converter.imageToWidget(QPointF(0,0)), QPointF(500,0));+ QCOMPARE(converter.imageToWidget(QPointF(1000,1000)), QPointF(0,500));++ // Now mirror on the left side. The offset should be corrected+ // to the right+ converter.mirror(QPointF(10, 0), false, false);++ QCOMPARE(converter.documentOffset(), QPoint(450,0));+ QCOMPARE(converter.imageToWidget(QPointF(0,0)), QPointF(-450,0));+ QCOMPARE(converter.imageToWidget(QPointF(1000,1000)), QPointF(50,500));+}++void KisCoordinatesConverterTest::testZoomConstant()+{+ KisImageSP image;+ KisCoordinatesConverter converter;+ initImage(&image, &converter);++ const QRect widgetRect(0,0,700,500);+ converter.setImage(image);+ converter.setDocumentOffset(QPoint(0,0));+ converter.setCanvasWidgetSize(widgetRect.size()); // should be initialized first+ converter.setZoom(0.5);++ // still point is QPointF(50,50)+ QCOMPARE(converter.imageToWidget(QPointF(100,100)), QPointF(50,50)); // still point+ QCOMPARE(converter.imageToWidget(QPointF(0,0)), QPointF(0,0));++ converter.setZoom(KoZoomMode::ZOOM_CONSTANT, 0.75, image->xRes(), image->yRes(), QPointF(50,50));++ QCOMPARE(converter.imageToWidget(QPointF(100,100)), QPointF(50,50)); // still point+ QCOMPARE(converter.imageToWidget(QPointF(0,0)), QPointF(-25,-25));+}++using PointPairs = QVector<std::pair<QPointF, QPointF>>;++void KisCoordinatesConverterTest::testZoomMode_data()+{+ QTest::addColumn<int>("zoomMargin");+ QTest::addColumn<QSize>("widgetSize");+ QTest::addColumn<QPointF>("originalOffset");+ QTest::addColumn<qreal>("originalZoom");+ QTest::addColumn<PointPairs>("originalTestPoints");+ QTest::addColumn<KoZoomMode::Mode>("newZoomMode");+ QTest::addColumn<qreal>("expectedZoom");+ QTest::addColumn<PointPairs>("expectedTestPoints");++ QTest::newRow("width-shrink-relative-to-widget")+ << 0+ << QSize(700, 500)+ << QPointF(-100,-100)+ << 1.0+ << PointPairs{+ {{0,0}, {100,100}},+ {{100,100}, {200,200}},+ {{250,150}, {350,250}} // "still point" is the center of the widget(!)+ }+ << KoZoomMode::ZOOM_WIDTH+ << 0.7+ << PointPairs{+ {{0,0}, {0,145}}, // the left image border is aligned to the left+ {{100,100}, {70,215}},+ {{1000,0}, {700,145}}, // the right image border is aligned to the right+ {{250,150}, {175,250}}, // "still point" has Y coordinate unchanged+ };++ QTest::newRow("width-grow-relative-to-image")+ << 0+ << QSize(700, 500)+ << QPointF(-100,-100)+ << 0.1+ << PointPairs{+ {{0,0}, {100,100}},+ {{100,100}, {110,110}},+ {{1000,1000}, {200,200}},+ {{500,500}, {150,150}} // "still point" points to the center of the image(!)+ }+ << KoZoomMode::ZOOM_WIDTH+ << 0.7+ << PointPairs{+ {{0,0}, {0,-200}}, // the left image border is aligned to the left+ {{100,100}, {70,-130}},+ {{1000,0}, {700,-200}}, // the right image border is aligned to the right+ {{500,500}, {350,150}}, // "still point" has Y coordinate unchanged+ };++ QTest::newRow("width-shrink-width-margin")+ << 50+ << QSize(700, 500)+ << QPointF(-100,-100)+ << 1.0+ << PointPairs{+ {{0,0}, {100,100}},+ {{100,100}, {200,200}},+ {{250,150}, {350,250}} // "still point" is the center of the widget(!)+ }+ << KoZoomMode::ZOOM_WIDTH+ << 0.6+ << PointPairs{+ {{0,0}, {50,160}}, // the left image border is aligned to the left+ {{100,100}, {110,220}},+ {{1000,0}, {650,160}}, // the right image border is aligned to the right+ {{250,150}, {200,250}}, // "still point" has Y coordinate unchanged+ };++ QTest::newRow("height-shrink-relative-to-widget")+ << 0+ << QSize(700, 500)+ << QPointF(-100,-100)+ << 1.0+ << PointPairs{+ {{0,0}, {100,100}},+ {{100,100}, {200,200}},+ {{250,150}, {350,250}} // "still point" is the center of the widget(!)+ }+ << KoZoomMode::ZOOM_HEIGHT+ << 0.5+ << PointPairs{+ {{0,0}, {225,0}}, // the top image border is aligned to the top+ {{100,100}, {275,50}},+ {{0,1000}, {225,500}}, // the bottom image border is aligned to the bottom+ {{250,150}, {350,75}}, // "still point" has X coordinate unchanged+ };++ QTest::newRow("height-grow-relative-to-image")+ << 0+ << QSize(700, 500)+ << QPointF(-100,-100)+ << 0.1+ << PointPairs{+ {{0,0}, {100,100}},+ {{100,100}, {110,110}},+ {{1000,1000}, {200,200}},+ {{500,500}, {150,150}} // "still point" points to the center of the image(!)+ }+ << KoZoomMode::ZOOM_HEIGHT+ << 0.5+ << PointPairs{+ {{0,0}, {-100,0}}, // the top image border is aligned to the top+ {{100,100}, {-50,50}},+ {{0,1000}, {-100,500}}, // the bottom image border is aligned to the bottom+ {{500,500}, {150,250}}, // "still point" has X coordinate unchanged+ };++ QTest::newRow("height-shrink-width-margin")+ << 50+ << QSize(700, 500)+ << QPointF(-100,-100)+ << 1.0+ << PointPairs{+ {{0,0}, {100,100}},+ {{100,100}, {200,200}},+ {{250,150}, {350,250}} // "still point" is the center of the widget(!)+ }+ << KoZoomMode::ZOOM_HEIGHT+ << 0.4+ << PointPairs{+ {{0,0}, {250,50}}, // the top image border is aligned to the top+ {{100,100}, {290,90}},+ {{0,1000}, {250,450}}, // the bottom image border is aligned to the bottom+ {{250,150}, {350,110}}, // "still point" has X coordinate unchanged+ };++ QTest::newRow("page-vertically")+ << 0+ << QSize(700, 500)+ << QPointF(-100,-100)+ << 1.0+ << PointPairs{+ {{0,0}, {100,100}},+ {{100,100}, {200,200}},+ {{500,500}, {600,600}}, // center of the image should become in the center of the canvas+ }+ << KoZoomMode::ZOOM_PAGE+ << 0.5+ << PointPairs{+ {{0,0}, {100,0}}, // the top image border is aligned to the top+ {{100,100}, {150,50}},+ {{1000,1000}, {600,500}}, // the bottom image border is aligned to the bottom+ {{500,500}, {350,250}}, // center of the image should become in the center of the canvas+ };+ QTest::newRow("page-vertically-margin")+ << 50+ << QSize(700, 500)+ << QPointF(-100,-100)+ << 1.0+ << PointPairs{+ {{0,0}, {100,100}},+ {{100,100}, {200,200}},+ {{500,500}, {600,600}}, // center of the image should become in the center of the canvas+ }+ << KoZoomMode::ZOOM_PAGE+ << 0.4+ << PointPairs{+ {{0,0}, {150,50}}, // the top image border is aligned to the top+ {{100,100}, {190,90}},+ {{1000,1000}, {550,450}}, // the bottom image border is aligned to the bottom+ {{500,500}, {350,250}}, // center of the image should become in the center of the canvas+ };+}++void KisCoordinatesConverterTest::testZoomMode()+{+ QFETCH(int, zoomMargin);+ QFETCH(QSize, widgetSize);+ QFETCH(QPointF, originalOffset);+ QFETCH(qreal, originalZoom);+ QFETCH(PointPairs, originalTestPoints);+ QFETCH(KoZoomMode::Mode, newZoomMode);+ QFETCH(qreal, expectedZoom);+ QFETCH(PointPairs, expectedTestPoints);++ KisImageSP image;+ KisCoordinatesConverter converter;+ initImage(&image, &converter);++ const QRect widgetRect(QPoint(), widgetSize);+ converter.setZoomMarginSize(zoomMargin);+ converter.setImage(image);+ converter.setDocumentOffset(originalOffset);+ converter.setCanvasWidgetSize(widgetRect.size());+ converter.setZoom(originalZoom);++ for (const auto &pair : std::as_const(originalTestPoints)) {+ const QPointF realPointWidgetPos = converter.imageToWidget(pair.first);++ if (!KisAlgebra2D::fuzzyPointCompare(realPointWidgetPos, pair.second)) {+ qWarning() << "Failed to compare test points before transformation:";+ qWarning() << " image point: " << pair.first;+ qWarning() << " real widget point: " << realPointWidgetPos;+ qWarning() << " expected widget point:" << pair.second;+ QFAIL("faled to compare points before transforamation");+ }+ }++ converter.setZoom(newZoomMode, 777.7, image->xRes(), image->yRes(), QPointF(50,50));++ QCOMPARE(converter.zoom(), expectedZoom);+ for (const auto &pair : std::as_const(expectedTestPoints)) {+ const QPointF realPointWidgetPos = converter.imageToWidget(pair.first);++ if (!KisAlgebra2D::fuzzyPointCompare(realPointWidgetPos, pair.second)) {+ qWarning() << "Failed to compare test points after transformation:";+ qWarning() << " image point: " << pair.first;+ qWarning() << " real widget point: " << realPointWidgetPos;+ qWarning() << " expected widget point:" << pair.second;+ QFAIL("faled to compare points after transforamation");+ }+ }+}SIMPLE_TEST_MAIN(KisCoordinatesConverterTest)diff --git a/libs/ui/tests/kis_coordinates_converter_test.h b/libs/ui/tests/kis_coordinates_converter_test.hindex 56f83e2738..304183fe09 100644--- a/libs/ui/tests/kis_coordinates_converter_test.h+++ b/libs/ui/tests/kis_coordinates_converter_test.h@@ -21,6 +21,15 @@ private Q_SLOTS:void testRotation();void testMirroring();void testMirroringCanvasBiggerThanImage();+ void testCanvasOffset();+ void testImageSmallerThanCanvas();+ void testImageSizeChange();+ void testImageResolutionChange();+ void testOffsetLimits();+ void testOffsetLimitsCropping();+ void testZoomConstant();+ void testZoomMode_data();+ void testZoomMode();};#endif /* KIS_COORDINATES_CONVERTER_TEST_H */