Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_input_manager_p.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2015 Michael Abrahams <miabraha@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
8
9#include <QMap>
10#include <QApplication>
11#include <QScopedPointer>
12#include <QTimer>
13#include <QtGlobal>
14
15#include <boost/preprocessor/repeat_from_to.hpp>
16
17#include "kis_input_manager.h"
18#include "kis_config.h"
21#include "kis_stroke_shortcut.h"
22#include "kis_touch_shortcut.h"
27
28#include "config-qt-patches-present.h"
29
30#include <memory>
31
33{
34 Q_UNUSED(value)
35 // TODO: remove
36}
37
39 : q(qq)
40 , toolManager(toolManagerArg)
41 , moveEventCompressor(10 /* ms */,
42 KisSignalCompressor::FIRST_ACTIVE,
43 KisSignalCompressor::ADDITIVE_INTERVAL)
44 , priorityEventFilterSeqNo(0)
45 , popupWidget(nullptr)
46 , canvasSwitcher(this, qq)
47{
48 KisConfig cfg(true);
49
53
54 if (cfg.trackTabletEventLatency()) {
56 }
57
59 [this] () {
60 return this->canvas ? this->canvas->inputActionGroupsMaskInterface()->inputActionGroupsMask() : AllActionGroup;
61 });
62
63#ifdef Q_OS_ANDROID
64 // a workaround for Xiaomi tablets
66#endif
67
73#ifdef Q_OS_MACOS
75#endif
76
81#if defined Q_OS_LINUX && !KRITA_QT_HAS_UNBALANCED_KEY_PRESS_RELEASE_PATCH
83#endif
84
85 if (qEnvironmentVariableIsSet("KRITA_FIX_UNBALANCED_KEY_EVENTS")) {
86 useUnbalancedKeyPressEventWorkaround = qEnvironmentVariableIntValue("KRITA_FIX_UNBALANCED_KEY_EVENTS");
87 }
88}
89
90static const int InputWidgetsThreshold = 2000;
91static const int OtherWidgetsThreshold = 400;
92
94 : QObject(p),
95 d(_d),
96 eatOneMouseStroke(false),
97 focusSwitchThreshold(InputWidgetsThreshold)
98{
99}
100
102{
103 QWidget *widget = qobject_cast<QWidget*>(object);
105
106 thresholdConnections.clear();
107 thresholdConnections.addConnection(&focusSwitchThreshold, SIGNAL(timeout()), widget, SLOT(setFocus()));
108}
109
111{
112 if (!canvas) return;
113
114 QObject *canvasWidget = canvas->canvasWidget();
115
116 if (!canvasResolver.contains(canvasWidget)) {
117 canvasResolver.insert(canvasWidget, canvas);
118 } else {
119 // just a sanity cheek to find out if we are
120 // trying to add two canvases concurrently.
122 }
123
124 if (canvas != d->canvas) {
125 d->q->setupAsEventFilter(canvasWidget);
126 canvasWidget->installEventFilter(this);
127
128 setupFocusThreshold(canvasWidget);
129 focusSwitchThreshold.setEnabled(false);
130
131 d->canvas = canvas;
132 d->toolProxy = qobject_cast<KisToolProxy*>(canvas->toolProxy());
133 }
134}
135
137{
138 QObject *widget = canvas->canvasWidget();
139
140 canvasResolver.remove(widget);
141
142 if (d->eventsReceiver == widget) {
144 }
145
146 widget->removeEventFilter(this);
147
148 if (d->canvas == canvas) {
149 d->canvas = 0;
150 d->toolProxy = 0;
151 }
152}
153
154bool isInputWidget(QWidget *w)
155{
156 if (!w) return false;
157
158
160 types << QLatin1String("QAbstractSlider");
161 types << QLatin1String("QAbstractSpinBox");
162 types << QLatin1String("QLineEdit");
163 types << QLatin1String("QTextEdit");
164 types << QLatin1String("QPlainTextEdit");
165 types << QLatin1String("QComboBox");
166 types << QLatin1String("QKeySequenceEdit");
167
168 Q_FOREACH (const QLatin1String &type, types) {
169 if (w->inherits(type.data())) {
170 return true;
171 }
172 }
173
174 return false;
175}
176
177bool KisInputManager::Private::CanvasSwitcher::eventFilter(QObject* object, QEvent* event )
178{
179 if (canvasResolver.contains(object)) {
180 switch (event->type()) {
181 case QEvent::FocusIn: {
182 QFocusEvent *fevent = static_cast<QFocusEvent*>(event);
183 KisCanvas2 *canvas = canvasResolver.value(object);
184
185 // only relevant canvases from the same main window should be
186 // registered in the switcher
188
189 if (canvas != d->canvas) {
190 eatOneMouseStroke = 2 * (fevent->reason() == Qt::MouseFocusReason);
191 }
192
193 d->canvas = canvas;
194 d->toolProxy = qobject_cast<KisToolProxy*>(canvas->toolProxy());
195
196 d->q->setupAsEventFilter(object);
197
198 object->removeEventFilter(this);
199 object->installEventFilter(this);
200
201 setupFocusThreshold(object);
202 focusSwitchThreshold.setEnabled(false);
203
204 const QPoint globalPos = QCursor::pos();
205 const QPoint localPos = d->canvas->canvasWidget()->mapFromGlobal(globalPos);
206 QWidget *canvasWindow = d->canvas->canvasWidget()->window();
207 const QPoint windowsPos = canvasWindow ? canvasWindow->mapFromGlobal(globalPos) : localPos;
208
209 QEnterEvent event(localPos, windowsPos, globalPos);
210 d->q->eventFilter(object, &event);
211 break;
212 }
213 case QEvent::FocusOut: {
214 focusSwitchThreshold.setEnabled(true);
215 break;
216 }
217 case QEvent::Enter: {
218 break;
219 }
220 case QEvent::Leave: {
221 focusSwitchThreshold.stop();
222 break;
223 }
224 case QEvent::Wheel: {
225 QWidget *widget = static_cast<QWidget*>(object);
226 widget->setFocus();
227 break;
228 }
229 case QEvent::MouseButtonPress:
230 case QEvent::MouseButtonRelease:
231 case QEvent::TabletPress:
232 case QEvent::TabletRelease:
233 focusSwitchThreshold.forceDone();
234
235 if (eatOneMouseStroke) {
236 eatOneMouseStroke--;
237 return true;
238 }
239 break;
240 case QEvent::MouseButtonDblClick:
241 focusSwitchThreshold.forceDone();
242 if (eatOneMouseStroke) {
243 return true;
244 }
245 break;
246 case QEvent::MouseMove:
247 case QEvent::TabletMove: {
248 QWidget *widget = static_cast<QWidget*>(object);
249
250 if (!widget->hasFocus()) {
251 const int delay =
252 isInputWidget(QApplication::focusWidget()) ?
254
255 focusSwitchThreshold.setDelayThreshold(delay);
256 focusSwitchThreshold.start();
257 }
258 }
259 break;
260 default:
261 break;
262 }
263 }
264 return QObject::eventFilter(object, event);
265}
266
270
272{
273 switch (event->type()) {
274 case QEvent::TabletEnterProximity:
275 KisInputEventsEater::debugEvent<QEvent>(event);
277 break;
278 case QEvent::TabletLeaveProximity:
279 KisInputEventsEater::debugEvent<QEvent>(event);
281 break;
282#ifdef Q_OS_WIN
283 case QEvent::KeyPress:
284 case QEvent::KeyRelease:
285 case QEvent::ShortcutOverride:
286 if (d->ignoreHighFunctionKeys) {
287 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
288 int key = keyEvent->key();
289
290 if (key >= Qt::Key_F13 && key <= Qt::Key_F35) {
292 const QString pre = "[BLOCKED HIGH F-KEY]";
294 }
295 return true;
296 }
297 break;
298 }
299#endif /* Q_OS_WIN */
300 default:
301 break;
302 }
303 return QObject::eventFilter(object, event);
304}
305
306#define EXTRA_BUTTON(z, n, _) \
307 if(buttons & Qt::ExtraButton##n) { \
308 buttonSet << Qt::ExtraButton##n; \
309 }
310
312 const QList<Qt::Key> &modifiers,
313 Qt::MouseButtons buttons)
314{
315 KisStrokeShortcut *strokeShortcut =
316 new KisStrokeShortcut(action, index);
317
318 QSet<Qt::MouseButton> buttonSet;
319 if(buttons & Qt::LeftButton) {
320 buttonSet << Qt::LeftButton;
321 }
322 if(buttons & Qt::RightButton) {
323 buttonSet << Qt::RightButton;
324 }
325 if(buttons & Qt::MiddleButton) {
326 buttonSet << Qt::MiddleButton;
327 }
328
329BOOST_PP_REPEAT_FROM_TO(1, 25, EXTRA_BUTTON, _)
330
331 if (!buttonSet.empty()) {
332 strokeShortcut->setButtons(QSet<Qt::Key>(modifiers.cbegin(), modifiers.cend()), buttonSet);
333 matcher.addShortcut(strokeShortcut);
334 }
335 else {
336 delete strokeShortcut;
337 }
338}
339
341 const QList<Qt::Key> &keys)
342{
343 if (keys.size() == 0) return;
344
345 KisSingleActionShortcut *keyShortcut =
346 new KisSingleActionShortcut(action, index);
347
348 //Note: Ordering is important here, Shift + V is different from V + Shift,
349 //which is the reason we use the last key here since most users will enter
350 //shortcuts as "Shift + V". Ideally this should not happen, but this is
351 //the way the shortcut matcher is currently implemented.
352 QList<Qt::Key> allKeys = keys;
353 Qt::Key key = allKeys.takeLast();
354 QSet<Qt::Key> modifiers = QSet<Qt::Key>(allKeys.begin(), allKeys.end());
355 keyShortcut->setKey(modifiers, key);
356 matcher.addShortcut(keyShortcut);
357}
358
360 const QList<Qt::Key> &modifiers,
362{
363
364
365 auto addSingleActionShortcut = [&] (KisSingleActionShortcut::WheelAction a) {
366 std::unique_ptr<KisSingleActionShortcut> keyShortcut(
367 new KisSingleActionShortcut(action, index));
368 keyShortcut->setWheel(QSet<Qt::Key>(modifiers.begin(), modifiers.end()), a);
369 matcher.addShortcut(keyShortcut.release());
370 };
371
372 switch(wheelAction) {
374 addSingleActionShortcut(KisSingleActionShortcut::WheelUp);
375 break;
377 addSingleActionShortcut(KisSingleActionShortcut::WheelDown);
378 break;
380 addSingleActionShortcut(KisSingleActionShortcut::WheelLeft);
381 break;
383 addSingleActionShortcut(KisSingleActionShortcut::WheelRight);
384 break;
387 break;
388 }
389}
390
392{
393 KisTouchShortcut *shortcut = new KisTouchShortcut(action, index, gesture);
394 if (isTouchPainting) {
395 shortcut->setIsTouchPainting(true);
396 shortcut->setMinDragThreshold(1.5); // min threshold for touch painting is 1.5 px
397 }
398 dbgKrita << "TouchAction:" << action->name() << (isTouchPainting ? "touch-painting" : "");
399 switch(gesture) {
403 shortcut->setMinimumTouchPoints(1);
404 shortcut->setMaximumTouchPoints(1);
405 break;
408 shortcut->setMinimumTouchPoints(2);
409 shortcut->setMaximumTouchPoints(2);
410 break;
413 shortcut->setMinimumTouchPoints(3);
414 shortcut->setMaximumTouchPoints(3);
415 break;
418 shortcut->setMinimumTouchPoints(4);
419 shortcut->setMaximumTouchPoints(4);
420 break;
423 shortcut->setMinimumTouchPoints(5);
424 shortcut->setMaximumTouchPoints(5);
425 default:
426 break;
427 }
428 matcher.addShortcut(shortcut);
429}
430
432{
433 auto addNativeGesture = [&] (KisNativeGestureShortcut::Type type) {
434 KisNativeGestureShortcut *shortcut = new KisNativeGestureShortcut(action, index, type);
435 matcher.addShortcut(shortcut);
436 };
437
438 // Native gestures:
439 // Qt5 only implements QNativeGestureEvent for macOS
440 // Qt6 implements QNativeGestureEvent for macOS and Wayland
441 //
442 // Touchpad pixel scrolling:
443 // Qt5 only implements touchpad pixel scrolling for macOS
444 // Qt6 implements touchpad pixel scrolling for macOS and Wayland
445
446 switch (gesture) {
449 break;
452 break;
455 break;
456 default:
457 return false;
458 }
459
460 return true;
461}
462
464{
466 Q_FOREACH (KisAbstractInputAction *action, actions) {
467 KisToolInvocationAction *toolAction =
468 dynamic_cast<KisToolInvocationAction*>(action);
469
470 if(toolAction) {
471 defaultInputAction = toolAction;
472 }
473 }
474
475 connect(KisInputProfileManager::instance(), SIGNAL(currentProfileChanged()), q, SLOT(profileChanged()));
476 if(KisInputProfileManager::instance()->currentProfile()) {
477 q->profileChanged();
478 }
479}
480
482{
483 bool retval = false;
484
486 event->type() == QEvent::KeyPress ||
487 event->type() == QEvent::KeyRelease) {
488
490 retval = true;
491 }
492
493 return retval && !forwardAllEventsToTool;
494}
495
500
502{
503 bool retval = false;
504
505 if (!matcher.pointerMoved(event) && toolProxy) {
506 toolProxy->forwardHoverEvent(event);
507 }
508 retval = true;
509 event->setAccepted(true);
510
511 return retval;
512}
513
515{
517
518 QVector<Qt::Key> newKeys;
519 Qt::KeyboardModifiers modifiers = mapper.queryStandardModifiers();
520 Q_FOREACH (Qt::Key key, mapper.queryExtendedModifiers()) {
521 QKeyEvent kevent(QEvent::ShortcutOverride, key, modifiers);
523 }
524
525 fixShortcutMatcherModifiersState(newKeys, modifiers);
526}
527
529{
531
532 matcher.handlePolledKeys(newKeys);
533
534 for (auto it = danglingKeys.begin(); it != danglingKeys.end();) {
535 if (newKeys.contains(*it)) {
536 newKeys.removeOne(*it);
537 it = danglingKeys.erase(it);
538 } else {
539 ++it;
540 }
541 }
542
550
551 Q_FOREACH (Qt::Key key, danglingKeys) {
552 QKeyEvent kevent(QEvent::KeyRelease, key, modifiers);
553 processUnhandledEvent(&kevent);
554 }
555
556 Q_FOREACH (Qt::Key key, newKeys) {
557 // just replay the whole sequence
558 {
559 QKeyEvent kevent(QEvent::ShortcutOverride, key, modifiers);
560 processUnhandledEvent(&kevent);
561 }
562 {
563 QKeyEvent kevent(QEvent::KeyPress, key, modifiers);
564 processUnhandledEvent(&kevent);
565 }
566 }
567}
568
570{
571 // on OS X, we need to compute the timestamp that compares correctly against the native event timestamp,
572 // which seems to be the msecs since system startup. On Linux with WinTab, we produce the timestamp that
573 // we compare against ourselves in QWindowSystemInterface.
574
575 QElapsedTimer elapsed;
576 elapsed.start();
577 return elapsed.msecsSinceReference();
578}
579
581{
582 dbgTablet << qUtf8Printable(message);
583}
float value(const T *src, size_t ch)
const Params2D p
@ AllActionGroup
Abstract base class for input actions.
static void setInputManager(KisInputManager *manager)
bool testingAcceptCompressedTabletEvents(bool defaultValue=false) const
int tabletEventsDelay(bool defaultValue=false) const
bool trackTabletEventLatency(bool defaultValue=false) const
bool testingCompressBrushEvents(bool defaultValue=false) const
static Qt::Key workaroundShiftAltMetaHell(const QKeyEvent *keyEvent)
bool eventFilter(QObject *object, QEvent *event) override
bool eventFilter(QObject *object, QEvent *event) override
virtual void print(const QString &message) override
void addTouchShortcut(KisAbstractInputAction *action, int index, KisShortcutConfiguration::TouchGestureAction gesture, bool isTouchPainting=false)
void setMaskSyntheticEvents(bool value)
KisInputEventsEater eventEater
bool handleCompressedTabletEvent(QEvent *event)
bool addNativeGestureShortcut(KisAbstractInputAction *action, int index, KisShortcutConfiguration::NativeGestureAction gesture)
void addKeyShortcut(KisAbstractInputAction *action, int index, const QList< Qt::Key > &keys)
void addWheelShortcut(KisAbstractInputAction *action, int index, const QList< Qt::Key > &modifiers, KisShortcutConfiguration::MouseWheelMovement wheelAction)
KisToolInvocationAction * defaultInputAction
Private(KisInputManager *qq, KoToolManager *toolManagerArg)
QPointer< KisToolProxy > toolProxy
KisSignalCompressor moveEventCompressor
void addStrokeShortcut(KisAbstractInputAction *action, int index, const QList< Qt::Key > &modifiers, Qt::MouseButtons buttons)
QScopedPointer< QEvent > compressedMoveEvent
QPointer< KisCanvas2 > canvas
bool processUnhandledEvent(QEvent *event)
KisSharedPtr< TabletLatencyTracker > tabletLatencyTracker
Central object to manage canvas input.
void setupAsEventFilter(QObject *receiver)
bool eventFilter(QObject *object, QEvent *event) override
QList< KisAbstractInputAction * > actions
static KisInputProfileManager * instance()
@ TouchpadScroll
Two-dimensional pixel scrolling on a touchpad, supported on MacOS and Wayland only.
@ SmartZoomGesture
Smart zoom gesture, typically a double tap that is a boolean zoom/unzoom.
@ PinchGesture
Pinch gesture, a two finger gesture used for navigating the canvas.
@ WheelDown
Downwards movement, toward the user.
@ WheelUp
Upwards movement, away from the user.
@ WheelReserved_0
Do not use! Previously was used for TouchpadScroll shortcut.
void addShortcut(KisSingleActionShortcut *shortcut)
void setInputActionGroupsMaskCallback(std::function< KisInputActionGroupsMask()> func)
void setIgnoreMultiFingerCancelWorkaroundEnalbed(bool value)
QVector< Qt::Key > debugPressedKeys() const
void handlePolledKeys(const QVector< Qt::Key > &keys)
bool pointerMoved(QEvent *event)
void setDelay(std::function< bool()> idleCallback, int idleDelay, int timeout)
@ WheelDown
Mouse wheel moves down.
@ WheelRight
Mouse wheel moves right.
@ WheelLeft
Mouse wheel moves left.
void setKey(const QSet< Qt::Key > &modifiers, Qt::Key key)
void setButtons(const QSet< Qt::Key > &modifiers, const QSet< Qt::MouseButton > &buttons)
QString eventToString(const QMouseEvent &ev, const QString &prefix)
static KisTabletDebugger * instance()
Tool Invocation action of KisAbstractInputAction.
The KisTouchShortcut class only handles touch gestures it does not handle tool invocation i....
void setIsTouchPainting(bool value)
void setMinDragThreshold(qreal value)
void setMaximumTouchPoints(int max)
void setMinimumTouchPoints(int min)
#define KIS_SAFE_ASSERT_RECOVER_BREAK(cond)
Definition kis_assert.h:127
#define KIS_SAFE_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:128
#define KIS_SAFE_ASSERT_RECOVER_NOOP(cond)
Definition kis_assert.h:130
#define dbgKrita
Definition kis_debug.h:49
#define dbgTablet
Definition kis_debug.h:62
static const int OtherWidgetsThreshold
static const int InputWidgetsThreshold
bool isInputWidget(QWidget *w)
#define EXTRA_BUTTON(z, n, _)
QString buttons(const T &ev)