Krita Source Code Documentation
Loading...
Searching...
No Matches
KisCanvasNavigationActionStrategyNativeGesture.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
8
9#include <QNativeGestureEvent>
10
11#include <kis_canvas2.h>
13
21
23{
24 return event->type() == QEvent::NativeGesture;
25}
26
28{
29 QNativeGestureEvent *gevent = static_cast<QNativeGestureEvent *>(event);
30 if (gevent->gestureType() == Qt::SmartZoomNativeGesture) {
32
33 if (controller->zoomState().mode != KoZoomMode::ZOOM_WIDTH) {
34 controller->setZoom(KoZoomMode::ZOOM_WIDTH, 1.0);
35 } else {
36 controller->setZoom(KoZoomMode::ZOOM_CONSTANT, 1.0);
37 }
38 } else if (gevent->gestureType() == Qt::ZoomNativeGesture && flags().testFlag(ZoomEnabled)) {
41
42 const qreal delta = 1.0 + gevent->value();
43
44 // Workaround: only apply the zoom delta if it's not too
45 // outlandish. TouchPoint coordinates are not always 100% reliable.
46 if (qAbs(delta) > 1.2 || qAbs(delta) < 0.8) {
47 // just skip the current zoom step
48 return;
49 }
50
51 const qreal newZoom = m_nonRoundedZoom * delta;
52
53 if (flags().testFlag(ZoomDescrete)) {
54 if (delta > 1.0) {
55 const qreal nextExpectedZoom =
56 converter->findNextZoom(converter->zoom(), converter->standardZoomLevels());
57 if (newZoom >= nextExpectedZoom) {
58 controller->setZoom(KoZoomMode::ZOOM_CONSTANT, nextExpectedZoom, m_actionStillPoint);
59 }
60 } else if (delta < 1.0) {
61 const qreal prevExpectedZoom =
62 converter->findPrevZoom(converter->zoom(), converter->standardZoomLevels());
63 if (newZoom <= prevExpectedZoom) {
64 controller->setZoom(KoZoomMode::ZOOM_CONSTANT, prevExpectedZoom, m_actionStillPoint);
65 }
66 }
67
68 } else {
70 }
71
72 m_nonRoundedZoom = converter->clampZoom(newZoom);
73
74 } else if (gevent->gestureType() == Qt::RotateNativeGesture && flags().testFlag(RotationEnabled)) {
76
77 m_nonRoundedRelativeRotation += gevent->value();
78
79 const qreal rotationAngle = [&]() {
80 if (flags().testFlag(RotationDescrete)) {
83 } else {
85 controller->rotation(),
87 }
88 }();
89
90 if (!qFuzzyIsNull(rotationAngle)) {
91 controller->rotateCanvas(rotationAngle, m_actionStillPoint, true);
92 }
93 }
94 if (gevent->gestureType() == Qt::PanNativeGesture && flags().testFlag(PanEnabled)) {
95#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
97
98 m_accumulatedPan += gevent->delta();
99
100 // we take the integral part of the accumulated offset and pan
101 // using it; the fractional part is left in the accumulator for
102 // the following pan actions
103 const QPoint canvasOffset(std::trunc(m_accumulatedPan.x()),
104 std::trunc(m_accumulatedPan.y()));
105 m_accumulatedPan -= canvasOffset;
106
107 // do the pan and adjust the still point so that zoom and rotate actions
108 // could also take that into account
109 m_actionStillPoint.second += canvasOffset;
110 controller->pan(-canvasOffset);
111#endif
112 }
113}
KisCoordinatesConverter * coordinatesConverter
const KoViewConverter * viewConverter() const override
void rotateCanvas(qreal angle, const std::optional< KoViewTransformStillPoint > &stillPoint, bool isNativeGesture=false)
KisCanvasNavigationActionStrategyNativeGesture(Flags flags, const QPointF &startViewPos, KisCanvas2 *canvas)
static qreal canvasRotationAngleContinuous(qreal currentRotationOffset, qreal currectCanvasRotation, SnappedRotationData &data)
static qreal canvasRotationAngleDescrete(qreal currentRotationOffset, SnappedRotationData &data)
static qreal findNextZoom(qreal currentZoom, const QVector< qreal > &zoomLevels)
QVector< qreal > standardZoomLevels() const
KoViewTransformStillPoint makeWidgetStillPoint(const QPointF &viewPoint) const override
Creates a still point that links the viewPoint of the widget to the corresponding point of the image.
static qreal findPrevZoom(qreal currentZoom, const QVector< qreal > &zoomLevels)
KoCanvasController * canvasController() const
void pan(const QPoint &distance) override
void setZoom(KoZoomMode::Mode mode, qreal zoom) override
virtual KoZoomState zoomState() const =0
virtual void setZoom(KoZoomMode::Mode mode, qreal zoom)=0
virtual void zoom(qreal *zoomX, qreal *zoomY) const
void zoom(qreal *zoomX, qreal *zoomY) const override
@ ZOOM_CONSTANT
zoom x %
Definition KoZoomMode.h:24
@ ZOOM_WIDTH
zoom pagewidth
Definition KoZoomMode.h:26
KoZoomMode::Mode mode
Definition KoZoomState.h:24
static bool qFuzzyIsNull(half h)
T kisDegreesToRadians(T degrees)
Definition kis_global.h:176