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++
draft: Implement a shared strategy for touch-based actions
Dmitry Kazakovea8189578dc7
diff --git a/libs/ui/CMakeLists.txt b/libs/ui/CMakeLists.txtindex c414f1bb55..dc796aa485 100644--- a/libs/ui/CMakeLists.txt+++ b/libs/ui/CMakeLists.txt@@ -346,6 +346,7 @@ set(kritaui_LIB_SRCSinput/KisCanvasNavigationActionStrategy.cppinput/KisCanvasNavigationActionStrategyNativeGesture.cpp+ input/KisCanvasNavigationActionStrategyTouch.cppinput/kis_input_manager.cppinput/kis_input_manager_p.cppinput/kis_extended_modifiers_mapper.cppdiff --git a/libs/ui/input/KisCanvasNavigationActionStrategy.cpp b/libs/ui/input/KisCanvasNavigationActionStrategy.cppindex 1054df0c7e..8e1f7e4246 100644--- a/libs/ui/input/KisCanvasNavigationActionStrategy.cpp+++ b/libs/ui/input/KisCanvasNavigationActionStrategy.cpp@@ -71,4 +71,14 @@ qreal KisCanvasNavigationActionStrategy::canvasRotationAngleDescrete(qreal curre}}return rotationAngle;+}++KisCanvasNavigationActionStrategy::KisCanvasNavigationActionStrategy(const Flags &flags)+ : m_flags(flags)+{+}++KisCanvasNavigationActionStrategy::Flags KisCanvasNavigationActionStrategy::flags() const+{+ return m_flags;}\ No newline at end of filediff --git a/libs/ui/input/KisCanvasNavigationActionStrategy.h b/libs/ui/input/KisCanvasNavigationActionStrategy.hindex 13f1981990..8199e85690 100644--- a/libs/ui/input/KisCanvasNavigationActionStrategy.h+++ b/libs/ui/input/KisCanvasNavigationActionStrategy.h@@ -8,16 +8,32 @@#define KISCANVASNAVIGATIONACTIONSTRATEGY_H#include <QtGlobal>+#include <QFlags>+class QEvent;class KisCanvasNavigationActionStrategy{public:+ enum Flag {+ None = 0x0,+ PanEnabled = 0x1,+ ZoomEnabled = 0x2,+ RotationEnabled = 0x4,+ ZoomDescrete = 0x8,+ RotationDescrete = 0x10,+ };+ Q_DECLARE_FLAGS(Flags, Flag)++public:+ KisCanvasNavigationActionStrategy(const Flags &flags);virtual ~KisCanvasNavigationActionStrategy() = default;virtual bool supportsEvent(QEvent* event) const = 0;virtual void inputEvent(QEvent* event) = 0;+ Flags flags() const;+public:struct SnappedRotationData {qreal previousAngle {0.0};@@ -27,6 +43,8 @@ public:static qreal canvasRotationAngleContinuous(qreal currentRotationOffset, qreal currectCanvasRotation, SnappedRotationData &data);static qreal canvasRotationAngleDescrete(qreal currentRotationOffset, SnappedRotationData &data);+private:+ Flags m_flags {None};};diff --git a/libs/ui/input/KisCanvasNavigationActionStrategyNativeGesture.cpp b/libs/ui/input/KisCanvasNavigationActionStrategyNativeGesture.cppindex 10279db261..4fb8d83af6 100644--- a/libs/ui/input/KisCanvasNavigationActionStrategyNativeGesture.cpp+++ b/libs/ui/input/KisCanvasNavigationActionStrategyNativeGesture.cpp@@ -12,8 +12,8 @@#include <kis_canvas_controller.h>KisCanvasNavigationActionStrategyNativeGesture::KisCanvasNavigationActionStrategyNativeGesture(Flags flags, const QPointF &startViewPos, KisCanvas2 *canvas)- : m_canvas(canvas)- , m_flags(flags)+ : KisCanvasNavigationActionStrategy(flags)+ , m_canvas(canvas){m_actionStillPoint = canvas->coordinatesConverter()->makeWidgetStillPoint(startViewPos);m_nonRoundedZoom = canvas->viewConverter()->zoom();@@ -35,7 +35,7 @@ void KisCanvasNavigationActionStrategyNativeGesture::inputEvent(QEvent *event)} else {controller->setZoom(KoZoomMode::ZOOM_CONSTANT, 1.0);}- } else if (gevent->gestureType() == Qt::ZoomNativeGesture && m_flags.testFlag(ZoomEnabled)) {+ } else if (gevent->gestureType() == Qt::ZoomNativeGesture && flags().testFlag(ZoomEnabled)) {KisCanvasController *controller = static_cast<KisCanvasController *>(m_canvas->canvasController());const KisCoordinatesConverter *converter = m_canvas->coordinatesConverter();@@ -50,7 +50,7 @@ void KisCanvasNavigationActionStrategyNativeGesture::inputEvent(QEvent *event)const qreal newZoom = m_nonRoundedZoom * delta;- if (m_flags.testFlag(ZoomDescrete)) {+ if (flags().testFlag(ZoomDescrete)) {if (delta > 1.0) {const qreal nextExpectedZoom =converter->findNextZoom(converter->zoom(), converter->standardZoomLevels());@@ -71,13 +71,13 @@ void KisCanvasNavigationActionStrategyNativeGesture::inputEvent(QEvent *event)m_nonRoundedZoom = converter->clampZoom(newZoom);- } else if (gevent->gestureType() == Qt::RotateNativeGesture && m_flags.testFlag(RotationEnabled)) {+ } else if (gevent->gestureType() == Qt::RotateNativeGesture && flags().testFlag(RotationEnabled)) {KisCanvasController *controller = static_cast<KisCanvasController *>(m_canvas->canvasController());m_nonRoundedRelativeRotation += gevent->value();const qreal rotationAngle = [&]() {- if (m_flags.testFlag(RotationDescrete)) {+ if (flags().testFlag(RotationDescrete)) {return canvasRotationAngleDescrete(kisDegreesToRadians(m_nonRoundedRelativeRotation),m_rotationData);} else {@@ -91,7 +91,7 @@ void KisCanvasNavigationActionStrategyNativeGesture::inputEvent(QEvent *event)controller->rotateCanvas(rotationAngle, m_actionStillPoint, true);}}- if (gevent->gestureType() == Qt::PanNativeGesture && m_flags.testFlag(PanEnabled)) {+ if (gevent->gestureType() == Qt::PanNativeGesture && flags().testFlag(PanEnabled)) {#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)KisCanvasController *controller = static_cast<KisCanvasController *>(m_canvas->canvasController());diff --git a/libs/ui/input/KisCanvasNavigationActionStrategyNativeGesture.h b/libs/ui/input/KisCanvasNavigationActionStrategyNativeGesture.hindex 9e7958dbca..cbec77fdd8 100644--- a/libs/ui/input/KisCanvasNavigationActionStrategyNativeGesture.h+++ b/libs/ui/input/KisCanvasNavigationActionStrategyNativeGesture.h@@ -7,7 +7,6 @@#ifndef KISCANVASNAVIGATIONACTIONSTRATEGYNATIVEGESTURE_H#define KISCANVASNAVIGATIONACTIONSTRATEGYNATIVEGESTURE_H-#include <QFlags>#include <QPointF>#include <KoViewTransformStillPoint.h>#include "KisCanvasNavigationActionStrategy.h"@@ -17,16 +16,6 @@ class KisCanvas2;class KisCanvasNavigationActionStrategyNativeGesture : public KisCanvasNavigationActionStrategy{-public:- enum Flag {- None = 0x0,- PanEnabled = 0x1,- ZoomEnabled = 0x2,- RotationEnabled = 0x4,- ZoomDescrete = 0x8,- RotationDescrete = 0x10,- };- Q_DECLARE_FLAGS(Flags, Flag)public:KisCanvasNavigationActionStrategyNativeGesture(Flags flags, const QPointF &startViewPos, KisCanvas2 *canvas);@@ -35,7 +24,6 @@ public:private:KisCanvas2 *m_canvas;- Flags m_flags;QPointF m_accumulatedPan;qreal m_nonRoundedZoom {0.0};diff --git a/libs/ui/input/KisCanvasNavigationActionStrategyTouch.cpp b/libs/ui/input/KisCanvasNavigationActionStrategyTouch.cppnew file mode 100644index 0000000000..5da5d67274--- /dev/null+++ b/libs/ui/input/KisCanvasNavigationActionStrategyTouch.cpp@@ -0,0 +1,152 @@+/*+ * SPDX-FileCopyrightText: 2026 Dmitry Kazakov <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */++#include "KisCanvasNavigationActionStrategyTouch.h"++#include <QTouchEvent>++#include <kis_algebra_2d.h>+#include <kis_canvas2.h>+#include <kis_canvas_controller.h>++#include <input/kis_touch_shortcut.h>+++KisCanvasNavigationActionStrategyTouch::KisCanvasNavigationActionStrategyTouch(Flags flags, const QPointF &startViewPos, KisCanvas2 *canvas)+ : KisCanvasNavigationActionStrategy(flags)+ , m_canvas(canvas)+{+ m_actionStillPoint = canvas->coordinatesConverter()->makeWidgetStillPoint(startViewPos);+ m_nonRoundedZoom = canvas->viewConverter()->zoom();+}++bool KisCanvasNavigationActionStrategyTouch::supportsEvent(QEvent* event) const+{+ return event->type() == QEvent::TouchBegin || event->type() == QEvent::TouchUpdate+ || event->type() == QEvent::TouchEnd;+}++void KisCanvasNavigationActionStrategyTouch::inputEvent(QEvent* event)+{+ if (event->type() != QEvent::TouchUpdate) return;++ QTouchEvent *tevent = dynamic_cast<QTouchEvent *>(event);++#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)+ auto points = tevent->touchPoints();+ using TouchPoint = QTouchEvent::TouchPoint;+#else+ auto points = tevent->points();+ using TouchPoint = QTouchEvent::TouchPoint;+#endif++ auto testIfPressed = [](const TouchPoint &point) {+ auto state = static_cast<Qt::TouchPointState>(point.state());+ return KisTouchShortcut::pressedOnlyTouchStates().testFlag(state);+ };++ auto point0_it = std::find_if(points.begin(), points.end(), testIfPressed);+ if (point0_it == points.end())+ return;++ auto point1_it = std::find_if(point0_it, points.end(), testIfPressed);+ if (point1_it == points.end())+ return;++ const QPointF p0 = point0_it->position();+ const QPointF p1 = point1_it->position();++ const QPointF slope = p1 - p0;++ KisCanvasController *controller = static_cast<KisCanvasController *>(m_canvas->canvasController());++ KoViewTransformStillPoint adjustedStillPoint = m_actionStillPoint;++ /**+ * Zoom and Rotate actions will perform pan as part of+ * the post-action recentering stage, so we should track+ * if we really need to perform a separate pan action.+ */+ bool needsSeparatePanAction = true;++ if (flags().testFlag(PanEnabled)) {+ adjustedStillPoint.second = p0;+ }++ if (flags().testFlag(ZoomEnabled)) {+ const qreal dist = KisAlgebra2D::norm(slope);++ qreal scaleDelta = qFuzzyIsNull(m_lastDistance) ? 1.0 : dist / m_lastDistance;++ // Make sure none of the TouchPoints are too close together, which+ // throws off the zoom calculations. This also addresses a glitch+ // where a newly pressed TouchPoint can incorrectly report another+ // existing TouchPoint's coordinates instead of its own.++ if (dist < 10) {+ scaleDelta = 1.0;+ }++ // Workaround: only apply the zoom delta if it's not too+ // outlandish. TouchPoint coordinates are not always 100% reliable.++ if (qAbs(scaleDelta) < 0.8 || qAbs(scaleDelta) > 1.2) {+ // just skip the current zoom step+ scaleDelta = 1.0;+ }++ const qreal newNonRoundedZoom = m_nonRoundedZoom * scaleDelta;++ if (flags().testFlag(ZoomDescrete)) {+ const KisCoordinatesConverter *converter = m_canvas->coordinatesConverter();+ if (scaleDelta > 1.0) {+ const qreal nextExpectedZoom =+ converter->findNextZoom(converter->zoom(), converter->standardZoomLevels());+ if (newNonRoundedZoom >= nextExpectedZoom) {+ controller->setZoom(KoZoomMode::ZOOM_CONSTANT, nextExpectedZoom, adjustedStillPoint);+ needsSeparatePanAction = false;+ }+ } else if (scaleDelta < 1.0) {+ const qreal prevExpectedZoom =+ converter->findPrevZoom(converter->zoom(), converter->standardZoomLevels());+ if (newNonRoundedZoom <= prevExpectedZoom) {+ controller->setZoom(KoZoomMode::ZOOM_CONSTANT, prevExpectedZoom, adjustedStillPoint);+ needsSeparatePanAction = false;+ }+ }++ } else {+ controller->setZoom(KoZoomMode::ZOOM_CONSTANT, newNonRoundedZoom, adjustedStillPoint);+ needsSeparatePanAction = false;+ }++ m_lastDistance = dist;+ m_nonRoundedZoom = newNonRoundedZoom;+ }++ if (flags().testFlag(RotationEnabled)) {+ const qreal currentAngle = std::atan2(slope.y(), slope.x());++ const qreal rotationAngle = [this, currentAngle]() {+ if (flags().testFlag(RotationDescrete)) {+ return canvasRotationAngleDescrete(currentAngle, m_rotationData);+ } else {+ KisCanvasController *controller = static_cast<KisCanvasController *>(m_canvas->canvasController());+ return canvasRotationAngleContinuous(currentAngle, controller->rotation(), m_rotationData);+ }+ }();++ if (!qFuzzyIsNull(rotationAngle)) {+ controller->rotateCanvas(rotationAngle, adjustedStillPoint);+ needsSeparatePanAction = false;+ }+ }++ if (needsSeparatePanAction) {+ const QPoint canvasOffset((adjustedStillPoint.viewPoint() - m_actionStillPoint.viewPoint()).toPoint());+ controller->pan(-canvasOffset);+ }+}\ No newline at end of filediff --git a/libs/ui/input/KisCanvasNavigationActionStrategyTouch.h b/libs/ui/input/KisCanvasNavigationActionStrategyTouch.hnew file mode 100644index 0000000000..5fd7290b03--- /dev/null+++ b/libs/ui/input/KisCanvasNavigationActionStrategyTouch.h@@ -0,0 +1,43 @@+/*+ * SPDX-FileCopyrightText: 2026 Dmitry Kazakov <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */++#ifndef KISCANVASNAVIGATIONACTIONSTRATEGYTOUCH_H+#define KISCANVASNAVIGATIONACTIONSTRATEGYTOUCH_H++#include "KisCanvasNavigationActionStrategy.h"++#include <KoViewTransformStillPoint.h>++#include <QPointF>++class QEvent;+class KisCanvas2;++++class KisCanvasNavigationActionStrategyTouch : public KisCanvasNavigationActionStrategy+{+public:+ KisCanvasNavigationActionStrategyTouch(Flags flags, const QPointF &startViewPos, KisCanvas2 *canvas);++ bool supportsEvent(QEvent* event) const override;+ void inputEvent(QEvent* event) override;++private:+ KisCanvas2 *m_canvas;++ //QPointF m_accumulatedPan;+ qreal m_nonRoundedZoom {0.0};+ //qreal m_nonRoundedRelativeRotation {0.0};+ KoViewTransformStillPoint m_actionStillPoint;++ SnappedRotationData m_rotationData;+ qreal m_lastDistance {0.0};+};++++#endif // KISCANVASNAVIGATIONACTIONSTRATEGYTOUCH_Hdiff --git a/libs/ui/input/kis_zoom_and_rotate_action.cpp b/libs/ui/input/kis_zoom_and_rotate_action.cppindex 841373d4d2..ae12e2af43 100644--- a/libs/ui/input/kis_zoom_and_rotate_action.cpp+++ b/libs/ui/input/kis_zoom_and_rotate_action.cpp@@ -18,6 +18,7 @@#include "kis_input_manager.h"#include <KoViewTransformStillPoint.h>+#include "KisCanvasNavigationActionStrategyTouch.h"#include "KisCanvasNavigationActionStrategyNativeGesture.h"class KisZoomAndRotateAction::Private {@@ -95,9 +96,9 @@ void KisZoomAndRotateAction::begin(int shortcut, QEvent *event){if (!event) return;- QTouchEvent *touchEvent = dynamic_cast<QTouchEvent *>(event);+ //QTouchEvent *touchEvent = dynamic_cast<QTouchEvent *>(event);- if (touchEvent && touchEvent->touchPoints().size() > 0) {+ /*if (touchEvent && touchEvent->touchPoints().size() > 0) {d->shortcutIndex = shortcut;const QPointF lastPosition = touchEvent->touchPoints().at(0).pos();@@ -106,10 +107,12 @@ void KisZoomAndRotateAction::begin(int shortcut, QEvent *event)d->rotationData = {};d->actionStillPoint = inputManager()->canvas()->coordinatesConverter()->makeWidgetStillPoint(lastPosition);- } else if (event->type() == QEvent::NativeGesture) {+ } else*/ if (event->type() == QEvent::NativeGesture ||+ event->type() == QEvent::TouchBegin ||+ event->type() == QEvent::TouchUpdate) {- using Flag = KisCanvasNavigationActionStrategyNativeGesture::Flag;- using Flags = KisCanvasNavigationActionStrategyNativeGesture::Flags;+ using Flag = KisCanvasNavigationActionStrategy::Flag;+ using Flags = KisCanvasNavigationActionStrategy::Flags;Flags flags;flags.setFlag(Flag::PanEnabled, d->hasPanAction(shortcut));@@ -118,7 +121,11 @@ void KisZoomAndRotateAction::begin(int shortcut, QEvent *event)flags.setFlag(Flag::ZoomEnabled);flags.setFlag(Flag::ZoomDescrete, d->zoomIsDiscrete(shortcut));- d->actionStrategy.reset(new KisCanvasNavigationActionStrategyNativeGesture(flags, eventPosF(event), inputManager()->canvas()));+ if (event->type() == QEvent::NativeGesture) {+ d->actionStrategy.reset(new KisCanvasNavigationActionStrategyNativeGesture(flags, eventPosF(event), inputManager()->canvas()));+ } else {+ d->actionStrategy.reset(new KisCanvasNavigationActionStrategyTouch(flags, eventPosF(event), inputManager()->canvas()));+ }d->shortcutIndex = shortcut;}diff --git a/libs/ui/kis_png_converter.cpp b/libs/ui/kis_png_converter.cppindex 707c760220..80efaaf35d 100644--- a/libs/ui/kis_png_converter.cpp+++ b/libs/ui/kis_png_converter.cpp@@ -1352,6 +1352,7 @@ KisImportExportErrorCode KisPNGConverter::buildFile(QIODevice* iodevice, const Q&& colorProfile->getColorPrimaries() < 256 && colorProfile->getTransferCharacteristics() < 256);dbgFile << options.writeCicpIfPossible << cicpPossible << colorProfilePQ << colorProfile->name();bool wroteCICP = false;+#if defined(PNG_cICP_SUPPORTED)if (options.writeCicpIfPossible && (cicpPossible || colorProfilePQ)) {png_byte primaries = colorProfile->getColorPrimaries();png_byte transfer = colorProfile->getTransferCharacteristics();@@ -1372,6 +1373,7 @@ KisImportExportErrorCode KisPNGConverter::buildFile(QIODevice* iodevice, const Qpng_set_cICP(png_ptr, info_ptr, primaries, transfer, matrixCoef, fullRange);wroteCICP = true;}+#endifif (!wroteCICP) {#if PNG_LIBPNG_VER_MAJOR >= 1 && PNG_LIBPNG_VER_MINOR >= 5const char *typeString = "icc";