Skip to content

armgit / krita

Public Imported from GitHub
0

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++

fixup: actually fix the touch strategies

Dmitry Kazakov5a9f2f87da81
diff --git a/libs/ui/input/KisCanvasNavigationActionStrategy.h b/libs/ui/input/KisCanvasNavigationActionStrategy.h
index 8199e85690..4ac1e54a78 100644
--- a/libs/ui/input/KisCanvasNavigationActionStrategy.h
+++ b/libs/ui/input/KisCanvasNavigationActionStrategy.h
@@ -47,6 +47,6 @@ private:
Flags m_flags {None};
};
-
+Q_DECLARE_OPERATORS_FOR_FLAGS(KisCanvasNavigationActionStrategy::Flags);
#endif // KISCANVASNAVIGATIONACTIONSTRATEGY_H
diff --git a/libs/ui/input/KisCanvasNavigationActionStrategyNativeGesture.h b/libs/ui/input/KisCanvasNavigationActionStrategyNativeGesture.h
index cbec77fdd8..95b3242d93 100644
--- a/libs/ui/input/KisCanvasNavigationActionStrategyNativeGesture.h
+++ b/libs/ui/input/KisCanvasNavigationActionStrategyNativeGesture.h
@@ -33,6 +33,4 @@ private:
SnappedRotationData m_rotationData;
};
-Q_DECLARE_OPERATORS_FOR_FLAGS(KisCanvasNavigationActionStrategyNativeGesture::Flags)
-
#endif // KISCANVASNAVIGATIONACTIONSTRATEGYNATIVEGESTURE_H
diff --git a/libs/ui/input/KisCanvasNavigationActionStrategyTouch.cpp b/libs/ui/input/KisCanvasNavigationActionStrategyTouch.cpp
index 5da5d67274..c9adaaf66b 100644
--- a/libs/ui/input/KisCanvasNavigationActionStrategyTouch.cpp
+++ b/libs/ui/input/KisCanvasNavigationActionStrategyTouch.cpp
@@ -14,12 +14,103 @@
#include <input/kis_touch_shortcut.h>
+namespace
+{
+
+template <typename PointFunctor>
+QPointF calcAverageForEachPressedPoint(const QTouchEvent *tevent, PointFunctor pointFunctor)
+{
+ QPointF result;
+
+#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 [sum, count] =
+ std::accumulate(points.begin(),
+ points.end(),
+ std::pair<QPointF, int>{},
+ [&](std::pair<QPointF, int> result, const TouchPoint &point) {
+ auto allowedStates = KisTouchShortcut::pressedOnlyTouchStates();
+ auto state = static_cast<Qt::TouchPointState>(point.state());
+ if (!allowedStates.testFlag(state)) {
+ return result;
+ }
+ result.first += pointFunctor(point);
+ result.second++;
+
+ return result;
+ });
+
+ if (count > 0) {
+ result = sum / qreal(count);
+ }
+
+ return result;
+}
+
+template <typename PointFunctor>
+QPointF calcForTheFirstPressedPoint(const QTouchEvent *tevent, PointFunctor pointFunctor)
+{
+ QPointF result;
+
+#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
-KisCanvasNavigationActionStrategyTouch::KisCanvasNavigationActionStrategyTouch(Flags flags, const QPointF &startViewPos, KisCanvas2 *canvas)
+ auto testIfPressed = [](const TouchPoint &point) {
+ auto state = static_cast<Qt::TouchPointState>(point.state());
+ return KisTouchShortcut::pressedOnlyTouchStates().testFlag(state);
+ };
+
+ auto it = std::find_if(points.begin(), points.end(), testIfPressed);
+ if (it != points.end()) {
+ result = pointFunctor(*it);
+ }
+
+ return result;
+}
+
+QPointF calculateBasePointFromEvent(const QTouchEvent *tevent, bool aroundTheFirstFinger)
+{
+ auto functor = [] (const auto &point) { return point.position(); };
+
+ if (aroundTheFirstFinger) {
+ return calcForTheFirstPressedPoint(tevent, functor);
+ } else {
+ return calcAverageForEachPressedPoint(tevent, functor);
+ }
+}
+
+bool effectiveTransformAroundTheFirstFinger(KisCanvasNavigationActionStrategy::Flags flags) {
+ using Flag = KisCanvasNavigationActionStrategy::Flag;
+
+ // for pan-mode we force the average calculation mode, for all
+ // the rest the value from the config
+ return !flags.testAnyFlags(Flag::ZoomEnabled | Flag::RotationEnabled)
+ ? false
+ : KisConfig(true).readEntry("touchGestureAroundTheFirstFinger", true);
+}
+
+} // namespace
+
+KisCanvasNavigationActionStrategyTouch::KisCanvasNavigationActionStrategyTouch(Flags flags,
+ const QTouchEvent *startEvent,
+ KisCanvas2 *canvas)
: KisCanvasNavigationActionStrategy(flags)
, m_canvas(canvas)
+ , m_transformAroundTheFirstFinger(effectiveTransformAroundTheFirstFinger(flags))
{
- m_actionStillPoint = canvas->coordinatesConverter()->makeWidgetStillPoint(startViewPos);
+ m_actionStillPoint = canvas->coordinatesConverter()->makeWidgetStillPoint(
+ calculateBasePointFromEvent(startEvent, m_transformAroundTheFirstFinger));
m_nonRoundedZoom = canvas->viewConverter()->zoom();
}
@@ -52,9 +143,16 @@ void KisCanvasNavigationActionStrategyTouch::inputEvent(QEvent* event)
if (point0_it == points.end())
return;
- auto point1_it = std::find_if(point0_it, points.end(), testIfPressed);
- if (point1_it == points.end())
- return;
+ auto point1_it = std::find_if(std::next(point0_it), points.end(), testIfPressed);
+ if (point1_it == points.end()) {
+ if (flags().testAnyFlags(ZoomEnabled | RotationEnabled)) {
+ return;
+ } else {
+ // we are doing pan-only, this action can be connected to
+ // a single-finger gesture
+ point1_it = point0_it;
+ }
+ }
const QPointF p0 = point0_it->position();
const QPointF p1 = point1_it->position();
@@ -73,7 +171,7 @@ void KisCanvasNavigationActionStrategyTouch::inputEvent(QEvent* event)
bool needsSeparatePanAction = true;
if (flags().testFlag(PanEnabled)) {
- adjustedStillPoint.second = p0;
+ adjustedStillPoint.second = calculateBasePointFromEvent(tevent, m_transformAroundTheFirstFinger);
}
if (flags().testFlag(ZoomEnabled)) {
@@ -129,15 +227,14 @@ void KisCanvasNavigationActionStrategyTouch::inputEvent(QEvent* event)
if (flags().testFlag(RotationEnabled)) {
const qreal currentAngle = std::atan2(slope.y(), slope.x());
+ qreal rotationAngle = 0.0;
- 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 (flags().testFlag(RotationDescrete)) {
+ rotationAngle = canvasRotationAngleDescrete(currentAngle, m_rotationData);
+ } else {
+ KisCanvasController *controller = static_cast<KisCanvasController *>(m_canvas->canvasController());
+ rotationAngle = canvasRotationAngleContinuous(currentAngle, controller->rotation(), m_rotationData);
+ }
if (!qFuzzyIsNull(rotationAngle)) {
controller->rotateCanvas(rotationAngle, adjustedStillPoint);
@@ -146,7 +243,9 @@ void KisCanvasNavigationActionStrategyTouch::inputEvent(QEvent* event)
}
if (needsSeparatePanAction) {
- const QPoint canvasOffset((adjustedStillPoint.viewPoint() - m_actionStillPoint.viewPoint()).toPoint());
+ const KisCoordinatesConverter *converter = m_canvas->coordinatesConverter();
+ const QPointF currentStillPointViewPosition = converter->documentToWidget(m_actionStillPoint.docPoint());
+ const QPoint canvasOffset((adjustedStillPoint.viewPoint() - currentStillPointViewPosition).toPoint());
controller->pan(-canvasOffset);
}
}
\ No newline at end of file
diff --git a/libs/ui/input/KisCanvasNavigationActionStrategyTouch.h b/libs/ui/input/KisCanvasNavigationActionStrategyTouch.h
index 5fd7290b03..a139ebdb27 100644
--- a/libs/ui/input/KisCanvasNavigationActionStrategyTouch.h
+++ b/libs/ui/input/KisCanvasNavigationActionStrategyTouch.h
@@ -15,13 +15,12 @@
class QEvent;
class KisCanvas2;
-
-
+class QTouchEvent;
class KisCanvasNavigationActionStrategyTouch : public KisCanvasNavigationActionStrategy
{
public:
- KisCanvasNavigationActionStrategyTouch(Flags flags, const QPointF &startViewPos, KisCanvas2 *canvas);
+ KisCanvasNavigationActionStrategyTouch(Flags flags, const QTouchEvent *startEvent, KisCanvas2 *canvas);
bool supportsEvent(QEvent* event) const override;
void inputEvent(QEvent* event) override;
@@ -29,13 +28,13 @@ public:
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};
+
+ const bool m_transformAroundTheFirstFinger = {false};
};
diff --git a/libs/ui/input/kis_pan_action.cpp b/libs/ui/input/kis_pan_action.cpp
index 3bf51c105f..5725bf49ca 100644
--- a/libs/ui/input/kis_pan_action.cpp
+++ b/libs/ui/input/kis_pan_action.cpp
@@ -19,6 +19,7 @@
#include "kis_input_manager.h"
+#include "KisCanvasNavigationActionStrategyTouch.h"
#include "KisCanvasNavigationActionStrategyNativeGesture.h"
@@ -27,13 +28,9 @@ class KisPanAction::Private
public:
Private() : panDistance(10) { }
- QPointF averagePoint( QTouchEvent* event, int *outCount = nullptr );
-
const int panDistance;
- QPointF lastPosition;
QPointF originalPreferredCenter;
- int touchPointsCount { 0 };
std::unique_ptr<KisCanvasNavigationActionStrategy> actionStrategy;
};
@@ -83,15 +80,23 @@ void KisPanAction::begin(int shortcut, QEvent *event)
/**
* Firstly, try to handle native gestures and touch events
*/
- if (event && event->type() == QEvent::NativeGesture && shortcut == PanModeShortcut) {
+ if (event
+ && (event->type() == QEvent::NativeGesture || event->type() == QEvent::TouchBegin
+ || event->type() == QEvent::TouchUpdate)
+ && shortcut == PanModeShortcut) {
using Flag = KisCanvasNavigationActionStrategyNativeGesture::Flag;
using Flags = KisCanvasNavigationActionStrategyNativeGesture::Flags;
Flags flags;
flags.setFlag(Flag::PanEnabled);
- 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 {
+ const QTouchEvent *tevent = static_cast<const QTouchEvent*>(event);
+ d->actionStrategy.reset(new KisCanvasNavigationActionStrategyTouch(flags, tevent, inputManager()->canvas()));
+ }
// native gestures don't have cursor tracking by the OS, so they shouldn't show any cursor
QApplication::restoreOverrideCursor();
@@ -102,12 +107,6 @@ void KisPanAction::begin(int shortcut, QEvent *event)
switch (shortcut) {
case PanModeShortcut: {
- QTouchEvent *tevent = dynamic_cast<QTouchEvent*>(event);
- if (tevent) {
- d->lastPosition = d->averagePoint(tevent, &d->touchPointsCount);
- break;
- }
-
// Some QT wheel events are actually be touch pad pan events. From the QT docs:
// "Wheel events are generated for both mouse wheels and trackpad scroll gestures."
QWheelEvent *wheelEvent = dynamic_cast<QWheelEvent*>(event);
@@ -155,31 +154,9 @@ void KisPanAction::inputEvent(QEvent *event)
if (d->actionStrategy && d->actionStrategy->supportsEvent(event)) {
d->actionStrategy->inputEvent(event);
- return;
- }
-
- switch (event->type()) {
- case QEvent::TouchUpdate: {
- QTouchEvent *tevent = static_cast<QTouchEvent*>(event);
- int newTouchPointsCount;
- QPointF newPos = d->averagePoint(tevent, &newTouchPointsCount);
- // When the number of touch points have changed, the average point
- // of the touch points will produce a huge jump which we don't want
- // to happen when panning. This can happen when ending a 3-finger
- // pan gesture. So we only pan the canvas if the number of touch
- // points have not changed.
- if (newTouchPointsCount == d->touchPointsCount) {
- QPointF delta = newPos - d->lastPosition;
- inputManager()->canvas()->canvasController()->pan(-delta.toPoint());
- }
- d->lastPosition = newPos;
- d->touchPointsCount = newTouchPointsCount;
- return;
- }
- default:
- break;
+ } else {
+ KisAbstractInputAction::inputEvent(event);
}
- KisAbstractInputAction::inputEvent(event);
}
void KisPanAction::cursorMovedAbsolute(const QPointF &startPos, const QPointF &pos)
@@ -187,33 +164,6 @@ void KisPanAction::cursorMovedAbsolute(const QPointF &startPos, const QPointF &p
inputManager()->canvas()->canvasController()->setPreferredCenter(-pos + startPos + d->originalPreferredCenter);
}
-QPointF KisPanAction::Private::averagePoint( QTouchEvent* event, int *outCount )
-{
- if(!event) {
- return QPointF();
- }
-
- QPointF result;
- int count = 0;
-
- Q_FOREACH ( QTouchEvent::TouchPoint point, event->touchPoints() ) {
- if( point.state() != Qt::TouchPointReleased ) {
- result += point.screenPos();
- count++;
- }
- }
-
- if (outCount) {
- *outCount = count;
- }
-
- if( count > 0 ) {
- return result / count;
- } else {
- return QPointF();
- }
-}
-
bool KisPanAction::isShortcutRequired(int shortcut) const
{
return shortcut == PanModeShortcut;
diff --git a/libs/ui/input/kis_rotate_canvas_action.cpp b/libs/ui/input/kis_rotate_canvas_action.cpp
index 572bf1c5a7..334b2b3df1 100644
--- a/libs/ui/input/kis_rotate_canvas_action.cpp
+++ b/libs/ui/input/kis_rotate_canvas_action.cpp
@@ -18,6 +18,7 @@
#include <math.h>
+#include "KisCanvasNavigationActionStrategyTouch.h"
#include "KisCanvasNavigationActionStrategyNativeGesture.h"
constexpr qreal DISCRETE_ANGLE_STEP = 15.0; // discrete rotation snapping angle
@@ -30,11 +31,8 @@ public:
// Coverity requires sane defaults for all variables (CID 36429)
Shortcut mode {RotateModeShortcut};
- qreal previousAngle {0.0};
qreal snapRotation {0.0};
- qreal touchRotation {0.0};
bool allowRotation {false};
- KoViewTransformStillPoint actionStillPoint;
std::unique_ptr<KisCanvasNavigationActionStrategy> actionStrategy;
};
@@ -89,19 +87,26 @@ void KisRotateCanvasAction::begin(int shortcut, QEvent *event)
dynamic_cast<KisCanvasController*>(inputManager()->canvas()->canvasController());
KIS_SAFE_ASSERT_RECOVER_RETURN(canvasController);
- if (event && event->type() == QEvent::NativeGesture
+ if (event
+ && (event->type() == QEvent::NativeGesture || event->type() == QEvent::TouchBegin
+ || event->type() == QEvent::TouchUpdate)
&& (shortcut == RotateModeShortcut || shortcut == DiscreteRotateModeShortcut)) {
using Flag = KisCanvasNavigationActionStrategyNativeGesture::Flag;
using Flags = KisCanvasNavigationActionStrategyNativeGesture::Flags;
- canvasController->beginCanvasRotation();
+ //canvasController->beginCanvasRotation();
Flags flags;
flags.setFlag(Flag::RotationEnabled);
flags.setFlag(Flag::RotationDescrete, shortcut == DiscreteRotateModeShortcut);
- 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 {
+ const QTouchEvent *tevent = static_cast<const QTouchEvent*>(event);
+ d->actionStrategy.reset(new KisCanvasNavigationActionStrategyTouch(flags, tevent, inputManager()->canvas()));
+ }
// native gestures don't have cursor tracking by the OS, so they shouldn't show any cursor
QApplication::restoreOverrideCursor();
@@ -109,9 +114,7 @@ void KisRotateCanvasAction::begin(int shortcut, QEvent *event)
}
d->allowRotation = false;
- d->previousAngle = 0;
d->snapRotation = 0;
- d->touchRotation = 0;
d->mode = (Shortcut)shortcut;
@@ -124,7 +127,6 @@ void KisRotateCanvasAction::begin(int shortcut, QEvent *event)
const qreal startRotation = inputManager()->canvas()->rotationAngle();
d->snapRotation = startRotation - std::trunc(startRotation / DISCRETE_ANGLE_STEP) * DISCRETE_ANGLE_STEP;
canvasController->beginCanvasRotation();
- d->actionStillPoint = inputManager()->canvas()->coordinatesConverter()->makeWidgetStillPoint(eventPosF(event));
break;
}
case RotateLeftShortcut:
@@ -143,19 +145,21 @@ void KisRotateCanvasAction::end(QEvent *event)
{
Q_UNUSED(event);
- d->actionStrategy.reset();
+ if (d->actionStrategy) {
+ d->actionStrategy.reset();
+ } else {
+ KisCanvasController *canvasController =
+ dynamic_cast<KisCanvasController *>(inputManager()->canvas()->canvasController());
+ KIS_SAFE_ASSERT_RECOVER_RETURN(canvasController);
- KisCanvasController *canvasController =
- dynamic_cast<KisCanvasController*>(inputManager()->canvas()->canvasController());
- KIS_SAFE_ASSERT_RECOVER_RETURN(canvasController);
-
- switch(d->mode) {
- case RotateModeShortcut:
- case DiscreteRotateModeShortcut:
- canvasController->endCanvasRotation();
- break;
- default:
- break;
+ switch (d->mode) {
+ case RotateModeShortcut:
+ case DiscreteRotateModeShortcut:
+ canvasController->endCanvasRotation();
+ break;
+ default:
+ break;
+ }
}
}
@@ -199,70 +203,9 @@ void KisRotateCanvasAction::inputEvent(QEvent* event)
if (d->actionStrategy && d->actionStrategy->supportsEvent(event)) {
d->actionStrategy->inputEvent(event);
- return;
- }
-
- switch (event->type()) {
- case QEvent::TouchUpdate: {
- QTouchEvent *touchEvent = static_cast<QTouchEvent*>(event);
-
- if (touchEvent->touchPoints().count() != 2)
- break;
-
- QTouchEvent::TouchPoint tp0 = touchEvent->touchPoints().at(0);
- QTouchEvent::TouchPoint tp1 = touchEvent->touchPoints().at(1);
-
- if (tp0.state() == Qt::TouchPointReleased ||
- tp1.state() == Qt::TouchPointReleased)
- {
- // Workaround: on some devices, the coordinates of TouchPoints
- // in state TouchPointReleased are not reliable, and can
- // "jump" by a significant distance. So we just stop handling
- // the rotation as soon as the user's finger leaves the tablet.
- break;
- }
-
- QPointF p0 = tp0.pos();
- QPointF p1 = tp1.pos();
-
- if ((p0-p1).manhattanLength() < 10)
- {
- // The TouchPoints are too close together. Don't update the
- // rotation as the angle will likely be off. This also deals
- // with a glitch where a newly pressed TouchPoint incorrectly
- // reports the existing TouchPoint's coordinates instead of its
- // own.
- break;
- }
-
- // high school (y2 - y1) / (x2 - x1)
- QPointF slope = p1 - p0;
- qreal newAngle = atan2(slope.y(), slope.x());
-
- // We must have the previous angle measurement to calculate the delta.
- if (d->allowRotation)
- {
- qreal delta = (180 / M_PI) * (newAngle - d->previousAngle);
-
- // Rotate by the effective angle from the beginning of the action.
- d->touchRotation += delta;
-
- KisCanvas2 *canvas = inputManager()->canvas();
- KisCanvasController *controller = static_cast<KisCanvasController*>(canvas->canvasController());
- controller->rotateCanvas(d->touchRotation);
- }
- else
- {
- d->allowRotation = true;
- }
-
- d->previousAngle = newAngle;
- return;
- }
- default:
- break;
+ } else {
+ KisAbstractInputAction::inputEvent(event);
}
- KisAbstractInputAction::inputEvent(event);
}
KisInputActionGroup KisRotateCanvasAction::inputActionGroup(int shortcut) const
diff --git a/libs/ui/input/kis_zoom_action.cpp b/libs/ui/input/kis_zoom_action.cpp
index 70b912ea8b..865f70252d 100644
--- a/libs/ui/input/kis_zoom_action.cpp
+++ b/libs/ui/input/kis_zoom_action.cpp
@@ -21,13 +21,14 @@
#include "kis_input_manager.h"
#include "kis_config.h"
+#include "KisCanvasNavigationActionStrategyTouch.h"
#include "KisCanvasNavigationActionStrategyNativeGesture.h"
class KisZoomAction::Private
{
public:
- Private(KisZoomAction *qq) : q(qq), lastDistance(0.f) {}
+ Private(KisZoomAction *qq) : q(qq) {}
QPointF centerPoint(QTouchEvent* event);
@@ -35,9 +36,6 @@ public:
// Coverity requires sane defaults for all variables (CID 36380)
Shortcuts mode {ZoomModeShortcut};
- QPointF lastPosition;
- float lastDistance {0.0};
-
KoViewTransformStillPoint actionStillPoint;
qreal startZoom {1.0};
@@ -121,10 +119,11 @@ void KisZoomAction::begin(int shortcut, QEvent *event)
/**
* Firstly, try to handle native gestures and touch events
*/
- if (event && event->type() == QEvent::NativeGesture
+ if (event
+ && (event->type() == QEvent::NativeGesture || event->type() == QEvent::TouchBegin
+ || event->type() == QEvent::TouchUpdate)
&& (shortcut == ZoomModeShortcut || shortcut == RelativeZoomModeShortcut || shortcut == DiscreteZoomModeShortcut
|| shortcut == RelativeDiscreteZoomModeShortcut)) {
-
using Flag = KisCanvasNavigationActionStrategyNativeGesture::Flag;
using Flags = KisCanvasNavigationActionStrategyNativeGesture::Flags;
@@ -133,21 +132,23 @@ void KisZoomAction::begin(int shortcut, QEvent *event)
flags.setFlag(Flag::ZoomDescrete,
shortcut == DiscreteZoomModeShortcut || shortcut == RelativeDiscreteZoomModeShortcut);
- 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 {
+ const QTouchEvent *tevent = static_cast<const QTouchEvent*>(event);
+ d->actionStrategy.reset(new KisCanvasNavigationActionStrategyTouch(flags, tevent, inputManager()->canvas()));
+ }
// native gestures don't have cursor tracking by the OS, so they shouldn't show any cursor
QApplication::restoreOverrideCursor();
return;
}
- d->lastDistance = 0.f;
-
switch(shortcut) {
case ZoomModeShortcut:
case RelativeZoomModeShortcut: {
d->startZoom = inputManager()->canvas()->coordinatesConverter()->zoom();
d->mode = (Shortcuts)shortcut;
- d->lastPosition = QPoint();
d->actionStillPoint = inputManager()->canvas()->coordinatesConverter()->makeWidgetStillPoint(eventPosF(event));
break;
}
@@ -231,84 +232,9 @@ void KisZoomAction::inputEvent( QEvent* event )
if (d->actionStrategy && d->actionStrategy->supportsEvent(event)) {
d->actionStrategy->inputEvent(event);
return;
+ } else {
+ KisAbstractInputAction::inputEvent(event);
}
-
- switch (event->type()) {
- case QEvent::TouchUpdate: {
- QTouchEvent *tevent = static_cast<QTouchEvent*>(event);
-
- if (tevent->touchPoints().count() != 2) {
- // Sanity check. The input state machine should only invoke
- // this action if there are 2 TouchPoints in the event.
- return;
- }
-
- // First, let's determine if we want to handle this event. Sadly
- // the coordinates of TouchPoints reported by Qt are not always
- // dependable. TouchPoints that are just getting released can be
- // off by a significant amount. So we stop the zoom as soon as the
- // user lifts a finger.
-
- QTouchEvent::TouchPoint tp0 = tevent->touchPoints().at(0);
- QTouchEvent::TouchPoint tp1 = tevent->touchPoints().at(1);
- if (tp0.state() == Qt::TouchPointReleased ||
- tp1.state() == Qt::TouchPointReleased) {
- // Force a recomputation of the position on the next event.
- d->lastPosition = QPoint();
- return;
- }
-
- QPointF p0 = tp0.pos();
- QPointF p1 = tp1.pos();
-
- // 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 ((p0-p1).manhattanLength() < 10) {
- d->lastPosition = QPointF();
- return;
- }
-
- // If this is the first valid set of points that we are getting,
- // then use that as the reference for the zoom.
-
- if (d->lastPosition.isNull()) {
- d->lastPosition = p0;
- d->lastDistance = 0;
- return;
- }
-
- float dist = QLineF(p0, p1).length();
- float delta = qFuzzyCompare(1.0f, 1.0f + d->lastDistance) ? 1.f : dist / d->lastDistance;
-
- // Workaround: only apply the zoom delta if it's not too
- // outlandish. As explained above, TouchPoint coordinates are not
- // always 100% reliable.
-
- if(qAbs(delta) < 0.8f || qAbs(delta) > 1.2f) {
- // TouchPoint coordinates tend to converge toward correct
- // values over time, so assume that the new position is
- // likelier to be correct than the last and use that as the new
- // reference.
- d->lastPosition = p0;
- return;
- }
-
- KisCanvasController *controller = static_cast<KisCanvasController *>(inputManager()->canvas()->canvasController());
- const qreal newZoom = controller->canvas()->viewConverter()->zoom() * delta;
- KoViewTransformStillPoint adjustedStillPoint = d->actionStillPoint;
- adjustedStillPoint.second = p0;
- controller->setZoom(KoZoomMode::ZOOM_CONSTANT, newZoom, adjustedStillPoint);
- d->lastDistance = dist;
- d->lastPosition = p0;
- return; // Don't try to update the cursor during a pinch-zoom
- }
- default:
- break;
- }
- KisAbstractInputAction::inputEvent(event);
}
void KisZoomAction::cursorMovedAbsolute(const QPointF &startPos, const QPointF &pos)
diff --git a/libs/ui/input/kis_zoom_and_rotate_action.cpp b/libs/ui/input/kis_zoom_and_rotate_action.cpp
index ae12e2af43..e2ea491342 100644
--- a/libs/ui/input/kis_zoom_and_rotate_action.cpp
+++ b/libs/ui/input/kis_zoom_and_rotate_action.cpp
@@ -25,13 +25,6 @@ class KisZoomAndRotateAction::Private {
public:
Private() {}
- int shortcutIndex {0};
- float lastDistance {0.0};
-
- KisCanvasNavigationActionStrategy::SnappedRotationData rotationData;
-
- KoViewTransformStillPoint actionStillPoint;
-
std::unique_ptr<KisCanvasNavigationActionStrategy> actionStrategy;
static bool zoomIsDiscrete(int shortcutIndex) {
@@ -96,20 +89,8 @@ void KisZoomAndRotateAction::begin(int shortcut, QEvent *event)
{
if (!event) return;
- //QTouchEvent *touchEvent = dynamic_cast<QTouchEvent *>(event);
-
- /*if (touchEvent && touchEvent->touchPoints().size() > 0) {
- d->shortcutIndex = shortcut;
- const QPointF lastPosition = touchEvent->touchPoints().at(0).pos();
-
- d->lastDistance = 0;
-
- d->rotationData = {};
-
- d->actionStillPoint = inputManager()->canvas()->coordinatesConverter()->makeWidgetStillPoint(lastPosition);
- } else*/ if (event->type() == QEvent::NativeGesture ||
- event->type() == QEvent::TouchBegin ||
- event->type() == QEvent::TouchUpdate) {
+ if (event->type() == QEvent::NativeGesture || event->type() == QEvent::TouchBegin
+ || event->type() == QEvent::TouchUpdate) {
using Flag = KisCanvasNavigationActionStrategy::Flag;
using Flags = KisCanvasNavigationActionStrategy::Flags;
@@ -124,10 +105,9 @@ void KisZoomAndRotateAction::begin(int shortcut, QEvent *event)
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()));
+ const QTouchEvent *tevent = static_cast<const QTouchEvent*>(event);
+ d->actionStrategy.reset(new KisCanvasNavigationActionStrategyTouch(flags, tevent, inputManager()->canvas()));
}
-
- d->shortcutIndex = shortcut;
}
}
@@ -146,48 +126,9 @@ void KisZoomAndRotateAction::inputEvent(QEvent *event)
{
if (d->actionStrategy && d->actionStrategy->supportsEvent(event)) {
d->actionStrategy->inputEvent(event);
- return;
- }
-
- switch (event->type()) {
- case QEvent::TouchUpdate: {
- QTouchEvent *tevent = dynamic_cast<QTouchEvent *>(event);
- if (tevent && tevent->touchPoints().size() > 1) {
-
- const QPointF p0 = tevent->touchPoints().at(0).pos();
- const QPointF p1 = tevent->touchPoints().at(1).pos();
-
- const QPointF slope = p1 - p0;
- const qreal currentAngle = std::atan2(slope.y(), slope.x());
-
- const qreal rotationAngle = canvasRotationAngle(currentAngle);
- const qreal dist = QLineF(p0, p1).length();
- qreal scaleDelta = qFuzzyCompare(1.0, 1.0 + d->lastDistance) ? 1.0 : dist / d->lastDistance;
-
- // 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
- d->lastDistance = dist;
- scaleDelta = 1.0;
- }
-
- KisCanvas2 *canvas = inputManager()->canvas();
- KisCanvasController *controller = static_cast<KisCanvasController *>(canvas->canvasController());
- const qreal newZoom = canvas->viewConverter()->zoom() * scaleDelta;
- KoViewTransformStillPoint adjustedStillPoint = d->actionStillPoint;
- adjustedStillPoint.second = p0;
- controller->setZoom(KoZoomMode::ZOOM_CONSTANT, newZoom, adjustedStillPoint);
- controller->rotateCanvas(rotationAngle, adjustedStillPoint);
-
- return;
- }
- }
- default:
- break;
+ } else {
+ KisAbstractInputAction::inputEvent(event);
}
- KisAbstractInputAction::inputEvent(event);
}
KisInputActionGroup KisZoomAndRotateAction::inputActionGroup(int shortcut) const
@@ -195,15 +136,3 @@ KisInputActionGroup KisZoomAndRotateAction::inputActionGroup(int shortcut) const
Q_UNUSED(shortcut);
return ViewTransformActionGroup;
}
-
-qreal KisZoomAndRotateAction::canvasRotationAngle(qreal currentAngle)
-{
- if (d->rotationIsDiscrete(d->shortcutIndex)) {
- return KisCanvasNavigationActionStrategy::canvasRotationAngleDescrete(currentAngle, d->rotationData);
- } else {
- KisCanvas2 *canvas = inputManager()->canvas();
- KisCanvasController *controller = static_cast<KisCanvasController*>(canvas->canvasController());
- return KisCanvasNavigationActionStrategy::canvasRotationAngleContinuous(currentAngle, controller->rotation(), d->rotationData);
- }
-}
-
diff --git a/libs/ui/input/kis_zoom_and_rotate_action.h b/libs/ui/input/kis_zoom_and_rotate_action.h
index b096dff043..e4e084a310 100644
--- a/libs/ui/input/kis_zoom_and_rotate_action.h
+++ b/libs/ui/input/kis_zoom_and_rotate_action.h
@@ -43,17 +43,6 @@ public:
KisInputActionGroup inputActionGroup(int shortcut) const override;
-private:
- struct SnappedRotationData {
- qreal previousAngle {0.0};
- qreal initialReferenceAngle {0.0};
- qreal accumRotationAngle {0.0};
- };
-
- static qreal canvasRotationAngleContinuous(qreal currentRotationOffset, qreal currectCanvasRotation, SnappedRotationData &data);
- static qreal canvasRotationAngleDescrete(qreal currentRotationOffset, SnappedRotationData &data);
- qreal canvasRotationAngle(qreal currentAngle);
-
private:
class Private;
const QScopedPointer<Private> d;