Commit b00618c9 authored by Alvin Wong's avatar Alvin Wong Committed by Boudewijn Rempt

Fix 'Fit Page' under fractional DPI scaling

parent 7a109ac2
......@@ -36,7 +36,7 @@ public:
}
int margin;
QSize documentSize;
QSizeF documentSize;
QPoint documentOffset;
qreal preferredCenterFractionX;
qreal preferredCenterFractionY;
......@@ -72,12 +72,12 @@ KoCanvasBase* KoCanvasController::canvas() const
return 0;
}
void KoCanvasController::setDocumentSize(const QSize &sz)
void KoCanvasController::setDocumentSize(const QSizeF &sz)
{
d->documentSize = sz;
}
QSize KoCanvasController::documentSize() const
QSizeF KoCanvasController::documentSize() const
{
return d->documentSize;
}
......
......@@ -101,7 +101,7 @@ public:
/**
* @return the size of the viewport
*/
virtual QSize viewportSize() const = 0;
virtual QSizeF viewportSize() const = 0;
/**
* Set the new canvas to be shown as a child
......@@ -273,7 +273,7 @@ public:
* @param recalculateCenter if true the offset in the document we center on after calling
* recenterPreferred() will be recalculated for the new document size so the visual offset stays the same.
*/
virtual void updateDocumentSize(const QSize &sz, bool recalculateCenter) = 0;
virtual void updateDocumentSize(const QSizeF &sz, bool recalculateCenter) = 0;
/**
* Set mouse wheel to zoom behaviour
......@@ -307,8 +307,8 @@ public:
virtual QPointF currentCursorPosition() const = 0;
protected:
void setDocumentSize(const QSize &sz);
QSize documentSize() const;
void setDocumentSize(const QSizeF &sz);
QSizeF documentSize() const;
void setPreferredCenterFractionX(qreal);
qreal preferredCenterFractionX() const;
......@@ -449,7 +449,7 @@ public:
void scrollContentsBy(int /*dx*/, int /*dy*/) override {}
QSize viewportSize() const override { return QSize(); }
QSizeF viewportSize() const override { return QSizeF(); }
void setCanvas(KoCanvasBase *canvas) override {Q_UNUSED(canvas)}
KoCanvasBase *canvas() const override {return 0;}
int visibleHeight() const override {return 0;}
......@@ -473,7 +473,7 @@ public:
QPoint scrollBarValue() const override {return QPoint();}
void setScrollBarValue(const QPoint &/*value*/) override {}
void resetScrollBars() override {}
void updateDocumentSize(const QSize &/*sz*/, bool /*recalculateCenter*/) override {}
void updateDocumentSize(const QSizeF &/*sz*/, bool /*recalculateCenter*/) override {}
void setZoomWithWheel(bool /*zoom*/) override {}
void setVastScrolling(qreal /*factor*/) override {}
QPointF currentCursorPosition() const override { return QPointF(); }
......
......@@ -75,8 +75,8 @@ void KoCanvasControllerWidget::Private::resetScrollBars()
// The scrollbar value always points at the top-left corner of the
// bit of image we paint.
int docH = q->documentSize().height() + q->margin();
int docW = q->documentSize().width() + q->margin();
int docH = (int)q->documentSize().height() + q->margin();
int docW = (int)q->documentSize().width() + q->margin();
int drawH = viewportWidget->height();
int drawW = viewportWidget->width();
......@@ -222,9 +222,13 @@ void KoCanvasControllerWidget::scrollContentsBy(int dx, int dy)
d->setDocumentOffset();
}
QSize KoCanvasControllerWidget::viewportSize() const
QSizeF KoCanvasControllerWidget::viewportSize() const
{
return viewport()->size();
// Calculate viewport size aligned to device pixels to match KisOpenGLCanvas2.
qreal dpr = viewport()->devicePixelRatioF();
int viewportWidth = static_cast<int>(viewport()->width() * dpr);
int viewportHeight = static_cast<int>(viewport()->height() * dpr);
return QSizeF(viewportWidth / dpr, viewportHeight / dpr);
}
void KoCanvasControllerWidget::resizeEvent(QResizeEvent *resizeEvent)
......@@ -442,7 +446,7 @@ void KoCanvasControllerWidget::zoomTo(const QRect &viewRect)
zoomBy(viewRect.center(), scale);
}
void KoCanvasControllerWidget::updateDocumentSize(const QSize &sz, bool recalculateCenter)
void KoCanvasControllerWidget::updateDocumentSize(const QSizeF &sz, bool recalculateCenter)
{
// Don't update if the document-size didn't changed to prevent infinite loops and unneeded updates.
if (KoCanvasController::documentSize() == sz)
......
......@@ -54,7 +54,7 @@ public:
*/
void scrollContentsBy(int dx, int dy) override;
QSize viewportSize() const override;
QSizeF viewportSize() const override;
/// Reimplemented from KoCanvasController
......@@ -134,7 +134,7 @@ public:
*/
void setScrollBarValue(const QPoint &value) override;
void updateDocumentSize(const QSize &sz, bool recalculateCenter = true) override;
void updateDocumentSize(const QSizeF &sz, bool recalculateCenter = true) override;
/**
* Set mouse wheel to zoom behaviour
......
......@@ -78,7 +78,7 @@ void Viewport::setCanvas(QWidget *canvas)
resetLayout();
}
void Viewport::setDocumentSize(const QSize &size)
void Viewport::setDocumentSize(const QSizeF &size)
{
m_documentSize = size;
resetLayout();
......
......@@ -41,7 +41,7 @@ public:
QWidget *canvas() const {
return m_canvas;
}
void setDocumentSize(const QSize &size);
void setDocumentSize(const QSizeF &size);
public Q_SLOTS:
void documentOffsetMoved(const QPoint &);
......@@ -77,7 +77,7 @@ private:
KoShape *m_draggedShape;
QWidget *m_canvas;
QSize m_documentSize; // Size in pixels of the document
QSizeF m_documentSize; // Size in pixels of the document
QPoint m_documentOffset; // Place where the canvas widget should
int m_margin; // The viewport margin around the document
};
......
......@@ -815,7 +815,7 @@ void KisView::resetImageSizeAndScroll(bool changeCentering,
converter->imageToWidget(oldImageStillPoint) +
converter->documentOffset();
} else {
QSize oldDocumentSize = d->canvasController.documentSize();
QSizeF oldDocumentSize = d->canvasController.documentSize();
oldStillPoint = QPointF(0.5 * oldDocumentSize.width(), 0.5 * oldDocumentSize.height());
}
......@@ -841,7 +841,7 @@ void KisView::resetImageSizeAndScroll(bool changeCentering,
converter->imageToWidget(newImageStillPoint) +
converter->documentOffset();
} else {
QSize newDocumentSize = d->canvasController.documentSize();
QSizeF newDocumentSize = d->canvasController.documentSize();
newStillPoint = QPointF(0.5 * newDocumentSize.width(), 0.5 * newDocumentSize.height());
}
......
......@@ -177,7 +177,7 @@ bool KisCanvasController::eventFilter(QObject *watched, QEvent *event)
return false;
}
void KisCanvasController::updateDocumentSize(const QSize &sz, bool recalculateCenter)
void KisCanvasController::updateDocumentSize(const QSizeF &sz, bool recalculateCenter)
{
KoCanvasControllerWidget::updateDocumentSize(sz, recalculateCenter);
......
......@@ -39,7 +39,7 @@ public:
void keyPressEvent(QKeyEvent *event) override;
void wheelEvent(QWheelEvent *event) override;
bool eventFilter(QObject *watched, QEvent *event) override;
void updateDocumentSize(const QSize &sz, bool recalculateCenter) override;
void updateDocumentSize(const QSizeF &sz, bool recalculateCenter) override;
void activate() override;
QPointF currentCursorPosition() const override;
......
......@@ -106,8 +106,8 @@ QPointF KisCoordinatesConverter::centeringCorrection() const
void KisCoordinatesConverter::correctOffsetToTransformation()
{
m_d->documentOffset = -(imageRectInWidgetPixels().topLeft() -
centeringCorrection()).toPoint();
m_d->documentOffset = snapToDevicePixel(-(imageRectInWidgetPixels().topLeft() -
centeringCorrection()));
}
void KisCoordinatesConverter::correctTransformationToOffset()
......
......@@ -60,9 +60,11 @@ public:
}
protected:
QSize documentToViewport(const QSizeF &size) override {
QSizeF documentToViewport(const QSizeF &size) override {
QRectF docRect(QPointF(), size);
return m_converter->documentToWidget(docRect).toRect().size();
QSizeF viewport = m_converter->documentToWidget(docRect).size();
QPointF adjustedViewport = m_converter->snapToDevicePixel(QPointF(viewport.width(), viewport.height()));
return QSizeF(adjustedViewport.x(), adjustedViewport.y());
}
private:
......
......@@ -280,7 +280,7 @@ bool KisZoomAndPanTest::checkZoomWithAction(ZoomAndPanTester &t, qreal newZoom,
QPoint oldOffset = t.coordinatesConverter()->documentOffset();
QPointF oldPrefCenter = t.canvasController()->preferredCenter();
qreal oldZoom = t.zoomController()->zoomAction()->effectiveZoom();
QSize oldDocumentSize = t.canvasController()->documentSize();
QSize oldDocumentSize = t.canvasController()->documentSize().toSize();
t.zoomController()->setZoom(KoZoomMode::ZOOM_CONSTANT, newZoom);
......@@ -302,7 +302,7 @@ bool KisZoomAndPanTest::checkZoomWithWheel(ZoomAndPanTester &t, const QPoint &wi
QPoint oldOffset = t.coordinatesConverter()->documentOffset();
QPointF oldPrefCenter = t.canvasController()->preferredCenter();
qreal oldZoom = t.zoomController()->zoomAction()->effectiveZoom();
QSize oldDocumentSize = t.canvasController()->documentSize();
QSize oldDocumentSize = t.canvasController()->documentSize().toSize();
t.canvasController()->zoomRelativeToPoint(widgetPoint, zoomCoeff);
......@@ -332,13 +332,13 @@ void KisZoomAndPanTest::testZoom100ChangingWidgetSize()
t.canvasController()->setPreferredCenter(QPoint(320,220));
QCOMPARE(t.canvasWidget()->size(), QSize(983,983));
QCOMPARE(t.canvasWidget()->size(), t.canvasController()->viewportSize());
QCOMPARE(t.canvasWidget()->size(), t.canvasController()->viewportSize().toSize());
QVERIFY(verifyOffset(t, QPoint(-171,-271)));
t.canvasController()->resize(QSize(700,700));
QCOMPARE(t.canvasWidget()->size(), QSize(683,683));
QCOMPARE(t.canvasWidget()->size(), t.canvasController()->viewportSize());
QCOMPARE(t.canvasWidget()->size(), t.canvasController()->viewportSize().toSize());
QVERIFY(verifyOffset(t, QPoint(-171,-271)));
t.canvasController()->setPreferredCenter(QPoint(320,220));
......@@ -348,7 +348,7 @@ void KisZoomAndPanTest::testZoom100ChangingWidgetSize()
t.canvasController()->resize(QSize(400,400));
QCOMPARE(t.canvasWidget()->size(), QSize(383,383));
QCOMPARE(t.canvasWidget()->size(), t.canvasController()->viewportSize());
QCOMPARE(t.canvasWidget()->size(), t.canvasController()->viewportSize().toSize());
QVERIFY(verifyOffset(t, QPoint(-21,-121)));
t.canvasController()->setPreferredCenter(QPoint(320,220));
......@@ -371,7 +371,7 @@ void KisZoomAndPanTest::initializeViewport(ZoomAndPanTester &t, bool fullscreenM
t.canvasController()->setPreferredCenter(QPoint(320,220));
QCOMPARE(t.canvasWidget()->size(), QSize(483,483));
QCOMPARE(t.canvasWidget()->size(), t.canvasController()->viewportSize());
QCOMPARE(t.canvasWidget()->size(), t.canvasController()->viewportSize().toSize());
QVERIFY(verifyOffset(t, QPoint(79,-21)));
if (fullscreenMode) {
......@@ -386,7 +386,7 @@ void KisZoomAndPanTest::initializeViewport(ZoomAndPanTester &t, bool fullscreenM
t.canvasController()->resize(QSize(483,483));
QCOMPARE(t.canvasWidget()->size(), QSize(483,483));
QCOMPARE(t.canvasWidget()->size(), t.canvasController()->viewportSize());
QCOMPARE(t.canvasWidget()->size(), t.canvasController()->viewportSize().toSize());
QVERIFY(verifyOffset(t, QPoint(79,-21)));
......@@ -550,7 +550,7 @@ bool KisZoomAndPanTest::checkRotation(ZoomAndPanTester &t, qreal angle)
QPointF oldCenteringCorrection = t.coordinatesConverter()->centeringCorrection();
QPointF oldPreferredCenter = t.canvasController()->preferredCenter();
QPointF oldRealCenterPoint = t.coordinatesConverter()->widgetToImage(t.coordinatesConverter()->widgetCenterPoint());
QSize oldDocumentSize = t.canvasController()->documentSize();
QSize oldDocumentSize = t.canvasController()->documentSize().toSize();
qreal baseAngle = t.coordinatesConverter()->rotationAngle();
t.canvasController()->rotateCanvas(angle);
......@@ -560,7 +560,7 @@ bool KisZoomAndPanTest::checkRotation(ZoomAndPanTester &t, qreal angle)
QPointF newCenteringCorrection = t.coordinatesConverter()->centeringCorrection();
QPointF newPreferredCenter = t.canvasController()->preferredCenter();
QPointF newRealCenterPoint = t.coordinatesConverter()->widgetToImage(t.coordinatesConverter()->widgetCenterPoint());
QSize newDocumentSize = t.canvasController()->documentSize();
QSize newDocumentSize = t.canvasController()->documentSize().toSize();
// calculate theoretical preferred center
......@@ -648,7 +648,7 @@ void KisZoomAndPanTest::testRotation(qreal vastScrolling, qreal zoom)
t.canvasController()->setPreferredCenter(preferredCenter.toPoint());
QCOMPARE(t.canvasWidget()->size(), QSize(483,483));
QCOMPARE(t.canvasWidget()->size(), t.canvasController()->viewportSize());
QCOMPARE(t.canvasWidget()->size(), t.canvasController()->viewportSize().toSize());
QPointF realCenterPoint = t.coordinatesConverter()->widgetToImage(t.coordinatesConverter()->widgetCenterPoint());
QPointF expectedCenterPoint = QPointF(t.image()->bounds().center());
......
......@@ -31,6 +31,8 @@
#include <KoCanvasBase.h>
#include <KoCanvasController.h>
#include <QtMath>
void KoZoomController::Private::init(KoCanvasController *co,
KoZoomHandler *zh,
KActionCollection *actionCollection)
......@@ -136,7 +138,7 @@ void KoZoomController::setZoom(KoZoomMode::Mode mode, qreal zoom, qreal resoluti
}
qreal oldEffectiveZoom = d->action->effectiveZoom();
QSize oldPageViewportSize = documentToViewport(d->pageSize);
QSizeF oldPageViewportSize = documentToViewport(d->pageSize);
if(!qFuzzyCompare(d->zoomHandler->resolutionX(), resolutionX) ||
!qFuzzyCompare(d->zoomHandler->resolutionY(), resolutionY)) {
......@@ -175,7 +177,7 @@ void KoZoomController::setZoom(KoZoomMode::Mode mode, qreal zoom, qreal resoluti
d->pageSize << " > " << d->documentSize << ")\n";
#endif
QSize documentViewportSize = documentToViewport(d->documentSize);
QSizeF documentViewportSize = documentToViewport(d->documentSize);
// Tell the canvasController that the zoom has changed
// Actually canvasController doesn't know about zoom, but the document in pixels
......@@ -200,11 +202,17 @@ void KoZoomController::setZoom(KoZoomMode::Mode mode, qreal zoom, qreal resoluti
emit zoomChanged(mode, d->action->effectiveZoom());
}
QSize KoZoomController::documentToViewport(const QSizeF &size)
QSizeF KoZoomController::documentToViewport(const QSizeF &size)
{
return d->zoomHandler->documentToView(size).toSize();
}
QSize KoZoomController::documentToViewportCeil(const QSizeF &size)
{
QSizeF viewport = documentToViewport(size);
return QSize(qCeil(viewport.width()), qCeil(viewport.height()));
}
void KoZoomController::setAspectMode(bool status)
{
if (d->action) {
......
......@@ -188,7 +188,8 @@ Q_SIGNALS:
void zoomedToAll();
protected:
virtual QSize documentToViewport(const QSizeF &size);
virtual QSizeF documentToViewport(const QSizeF &size);
QSize documentToViewportCeil(const QSizeF &size);
private:
Q_PRIVATE_SLOT(d, void setAvailableSize())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment