Skip to content

Commit

Permalink
Issue #207: Zoom to Cursor (Zoom Anchoring)
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubMelka committed Aug 31, 2024
1 parent 2a93ca5 commit b97e36c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
36 changes: 28 additions & 8 deletions Pdf4QtLibWidgets/sources/pdfdrawspacecontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1231,21 +1231,41 @@ QPoint PDFDrawWidgetProxy::scrollByPixels(QPoint offset)
return QPoint(m_horizontalOffset - oldHorizontalOffset, m_verticalOffset - oldVerticalOffset);
}

void PDFDrawWidgetProxy::zoom(PDFReal zoom)
void PDFDrawWidgetProxy::zoom(PDFReal zoom, std::optional<QPointF> widgetPosition)
{
const PDFReal clampedZoom = qBound(MIN_ZOOM, zoom, MAX_ZOOM);
if (m_zoom != clampedZoom)
{
const PDFReal oldHorizontalOffsetMM = m_horizontalOffset * m_pixelToDeviceSpaceUnit;
const PDFReal oldVerticalOffsetMM = m_verticalOffset * m_pixelToDeviceSpaceUnit;
if (widgetPosition.has_value())
{
QPointF point = widgetPosition.value();

m_zoom = clampedZoom;
const PDFReal posX = (m_horizontalOffset - point.x()) * m_pixelToDeviceSpaceUnit;
const PDFReal posY = (m_verticalOffset - point.y()) * m_pixelToDeviceSpaceUnit;

update();
m_zoom = clampedZoom;

update();

// Try to restore offsets, so we are in the same place
setHorizontalOffset(oldHorizontalOffsetMM * m_deviceSpaceUnitToPixel);
setVerticalOffset(oldVerticalOffsetMM * m_deviceSpaceUnitToPixel);
const PDFReal hp = posX / m_pixelToDeviceSpaceUnit + point.x();
const PDFReal vp = posY / m_pixelToDeviceSpaceUnit + point.y();

setHorizontalOffset(hp);
setVerticalOffset(vp);
}
else
{
const PDFReal oldHorizontalOffsetMM = m_horizontalOffset * m_pixelToDeviceSpaceUnit;
const PDFReal oldVerticalOffsetMM = m_verticalOffset * m_pixelToDeviceSpaceUnit;

m_zoom = clampedZoom;

update();

// Try to restore offsets, so we are in the same place
setHorizontalOffset(oldHorizontalOffsetMM * m_deviceSpaceUnitToPixel);
setVerticalOffset(oldVerticalOffsetMM * m_deviceSpaceUnitToPixel);
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion Pdf4QtLibWidgets/sources/pdfdrawspacecontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ class PDF4QTLIBWIDGETSSHARED_EXPORT PDFDrawWidgetProxy : public QObject
/// Sets the zoom. Tries to preserve current offsets (so the current visible
/// area will be visible after the zoom).
/// \param zoom New zoom
void zoom(PDFReal zoom);
/// \param widgetPosition Position of the mouse during zooming
void zoom(PDFReal zoom, std::optional<QPointF> widgetPosition = std::nullopt);

enum class ZoomHint
{
Expand Down
2 changes: 1 addition & 1 deletion Pdf4QtLibWidgets/sources/pdfdrawwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ void PDFDrawWidget::wheelEvent(QWheelEvent* event)
const PDFReal zoom = m_widget->getDrawWidgetProxy()->getZoom();
const PDFReal zoomStep = std::pow(PDFDrawWidgetProxy::ZOOM_STEP, static_cast<PDFReal>(angleDeltaY) / static_cast<PDFReal>(QWheelEvent::DefaultDeltasPerStep));
const PDFReal newZoom = zoom * zoomStep;
proxy->zoom(newZoom);
proxy->zoom(newZoom, event->position());
}
else
{
Expand Down
1 change: 1 addition & 0 deletions RELEASES.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
CURRENT:
- Issue #207: Zoom to Cursor (Zoom Anchoring)
- Issue #185: Latest git fails to build in linux

V: 1.4.0.0 4.7.2024
Expand Down

0 comments on commit b97e36c

Please sign in to comment.