Issue #207: Zoom to Cursor (Zoom Anchoring)

This commit is contained in:
Jakub Melka 2024-08-31 19:25:08 +02:00
parent 2a93ca5a1f
commit b97e36ce0d
4 changed files with 32 additions and 10 deletions

View File

@ -1231,21 +1231,41 @@ QPoint PDFDrawWidgetProxy::scrollByPixels(QPoint offset)
return QPoint(m_horizontalOffset - oldHorizontalOffset, m_verticalOffset - oldVerticalOffset); 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); const PDFReal clampedZoom = qBound(MIN_ZOOM, zoom, MAX_ZOOM);
if (m_zoom != clampedZoom) if (m_zoom != clampedZoom)
{ {
const PDFReal oldHorizontalOffsetMM = m_horizontalOffset * m_pixelToDeviceSpaceUnit; if (widgetPosition.has_value())
const PDFReal oldVerticalOffsetMM = m_verticalOffset * m_pixelToDeviceSpaceUnit; {
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;
// Try to restore offsets, so we are in the same place update();
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);
}
} }
} }

View File

@ -254,7 +254,8 @@ public:
/// Sets the zoom. Tries to preserve current offsets (so the current visible /// Sets the zoom. Tries to preserve current offsets (so the current visible
/// area will be visible after the zoom). /// area will be visible after the zoom).
/// \param zoom New 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 enum class ZoomHint
{ {

View File

@ -551,7 +551,7 @@ void PDFDrawWidget::wheelEvent(QWheelEvent* event)
const PDFReal zoom = m_widget->getDrawWidgetProxy()->getZoom(); 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 zoomStep = std::pow(PDFDrawWidgetProxy::ZOOM_STEP, static_cast<PDFReal>(angleDeltaY) / static_cast<PDFReal>(QWheelEvent::DefaultDeltasPerStep));
const PDFReal newZoom = zoom * zoomStep; const PDFReal newZoom = zoom * zoomStep;
proxy->zoom(newZoom); proxy->zoom(newZoom, event->position());
} }
else else
{ {

View File

@ -1,4 +1,5 @@
CURRENT: CURRENT:
- Issue #207: Zoom to Cursor (Zoom Anchoring)
- Issue #185: Latest git fails to build in linux - Issue #185: Latest git fails to build in linux
V: 1.4.0.0 4.7.2024 V: 1.4.0.0 4.7.2024