Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_touch_shortcut.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the KDE project
3 * SPDX-FileCopyrightText: 2012 Arjen Hiemstra <ahiemstra@heimr.nl>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 *
7 */
8
10
11#include <kis_algebra_2d.h>
13#include "kis_config.h"
14
15#include <QTouchEvent>
16
34
36 : KisAbstractShortcut(action, index)
37 , d(new Private(type))
38{
39
40}
41
46
48{
49 return d->minDragThreshold;
50}
51
56
58{
59 return d->isTouchPainting ? std::numeric_limits<int>::max() : action()->priority();
60}
61
66
71
76
81
82bool KisTouchShortcut::isAvailable(KisInputActionGroupsMask mask) const
83{
84 if (d->isTouchPainting && KisConfig(true).disableTouchOnCanvas()) {
85 return false;
86 }
87
89}
90
91bool KisTouchShortcut::matchTapType(const QTouchEvent *event, Qt::TouchPointStates allowedStates)
92{
93 return matchTouchPoint(event, allowedStates)
95}
96
97bool KisTouchShortcut::matchDragType(const QTouchEvent *event, Qt::TouchPointStates allowedStates)
98{
99 return touchDragDistance(event, allowedStates) > d->minDragThreshold &&
100 matchTouchPoint(event, allowedStates)
102}
103
104bool KisTouchShortcut::matchHoldType(const QTouchEvent *event, Qt::TouchPointStates allowedStates)
105{
106 return isHoldType() && matchTouchPoint(event, allowedStates);
107}
108
109int KisTouchShortcut::countTouchPoints(const QTouchEvent *event, Qt::TouchPointStates allowedStates)
110{
111#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
112 auto points = event->touchPoints();
113 using TouchPoint = QTouchEvent::TouchPoint;
114#else
115 auto points = event->points();
116 using TouchPoint = QEventPoint;
117#endif
118
119 const int count =
120 std::count_if(points.begin(), points.end(), [=] (const TouchPoint &point) {
121 auto state = static_cast<Qt::TouchPointState>(point.state());
122 return allowedStates.testFlag(state);
123 });
124
125 return count;
126}
127
128qreal KisTouchShortcut::touchDragDistance(const QTouchEvent *event, Qt::TouchPointStates allowedStates)
129{
130#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
131 auto points = event->touchPoints();
132 using TouchPoint = QTouchEvent::TouchPoint;
133#else
134 auto points = event->points();
135 using TouchPoint = QEventPoint;
136#endif
137
138 return std::sqrt(std::accumulate(points.begin(), points.end(), qreal(0),
139 [&] (qreal maxOffsetSq, const TouchPoint &point) {
140 auto state = static_cast<Qt::TouchPointState>(point.state());
141 if (!allowedStates.testFlag(state)) {
142 return maxOffsetSq;
143 }
144
145 return std::max(maxOffsetSq, KisAlgebra2D::normSquared(point.pos() - point.startPos()));
146 }));
147}
148
149bool KisTouchShortcut::matchTouchPoint(const QTouchEvent *event, Qt::TouchPointStates allowedStates)
150{
151 const int numStillActivePoints = countTouchPoints(event, allowedStates);
152
153 return numStillActivePoints >= d->minTouchPoints && numStillActivePoints <= d->maxTouchPoints;
154}
float value(const T *src, size_t ch)
Abstract base class for input actions.
KisAbstractInputAction * action
virtual bool isAvailable(KisInputActionGroupsMask mask) const
Private(TouchGestureAction type)
bool isAvailable(KisInputActionGroupsMask mask) const override
void setIsTouchPainting(bool value)
bool matchTapType(const QTouchEvent *event, Qt::TouchPointStates allowedStates)
bool matchDragType(const QTouchEvent *event, Qt::TouchPointStates allowedStates)
static qreal touchDragDistance(const QTouchEvent *event, Qt::TouchPointStates allowedStates)
qreal minDragThreshold() const
void setMinDragThreshold(qreal value)
KisTouchShortcut(KisAbstractInputAction *action, int index, TouchGestureAction type)
bool matchHoldType(const QTouchEvent *event, Qt::TouchPointStates allowedStates)
static int countTouchPoints(const QTouchEvent *event, Qt::TouchPointStates allowedStates)
bool matchTouchPoint(const QTouchEvent *event, Qt::TouchPointStates allowedStates)
void setMaximumTouchPoints(int max)
int priority() const override
void setMinimumTouchPoints(int min)
qreal normSquared(const T &a)