Krita Source Code Documentation
Loading...
Searching...
No Matches
KisTouchHoldEventsPostponer Class Reference

#include <KisTouchHoldEventsPostponer.h>

+ Inheritance diagram for KisTouchHoldEventsPostponer:

Public Types

enum  State { WaitingForHold , HoldCompleted , HoldCancelled }
 

Signals

void sigHoldCompleted ()
 

Public Member Functions

void cancelHoldWait ()
 
 KisTouchHoldEventsPostponer (const KisTouchHoldEventsPostponer &rhs)=delete
 
 KisTouchHoldEventsPostponer (qreal maxHoldDistance, int holdTimeout)
 
QVector< QTouchEvent * > postponedEvents ()
 
void pushThrough (QTouchEvent *event)
 
State state () const
 
 ~KisTouchHoldEventsPostponer ()
 

Private Slots

void slotHoldCompletionTimeout ()
 

Private Member Functions

void enqueueEvent (QTouchEvent *event)
 
bool qualifyForPostponing (QTouchEvent *event)
 

Private Attributes

qreal m_maxHoldDistance {20.0}
 
QVector< QTouchEvent * > m_postponedEvents
 
State m_state { WaitingForHold }
 
QTimer m_timer
 

Detailed Description

The object to store and manage postponed events while waiting for a touch-hold shortcut to trigger. Currently, it supports single- finger touch actions only.

Usage:

// in the class
std::optional<KisTouchHoldEventsPostponer> postponer;
// on action start
postponer.emplace(20, 1000);
// on any touch event (including the first)
if (postponer) {
postponer->pushThrough(event);
if (postponer->state() == KisTouchHoldEventsPostponer::HoldCancelled) {
Q_FOREACH (QTouchEvent *event, postponer->postponedEvents()) {
// ... deliver the event ...
}
postponer.reset();
}
}
// on sigHoldCompleted()
QTouchEvent *tiggeringEvent = postponer->postponedEvents().front();
// ... start operation using the very first as the rule ...
@ HoldCancelled
the hold action has been detected, the accumulated events should be dropped and the hold action shoul...

Definition at line 47 of file KisTouchHoldEventsPostponer.h.

Member Enumeration Documentation

◆ State

Enumerator
WaitingForHold 
HoldCompleted 

the postponer is still accumulating the events

HoldCancelled 

the hold action has been detected, the accumulated events should be dropped and the hold action should be started

hold action has been cancelled, the accumulated events should be fired to the consumers

Definition at line 51 of file KisTouchHoldEventsPostponer.h.

51 {
55 };
@ HoldCompleted
the postponer is still accumulating the events

Constructor & Destructor Documentation

◆ KisTouchHoldEventsPostponer() [1/2]

KisTouchHoldEventsPostponer::KisTouchHoldEventsPostponer ( qreal maxHoldDistance,
int holdTimeout )

Createa touch-hold postponer with maxHoldDistance used as a threshold for hold/drag and holdTimeout as a timeout to consider the hold operation completed

Definition at line 25 of file KisTouchHoldEventsPostponer.cpp.

26 : m_maxHoldDistance(maxHoldDistance)
27{
28 connect(&m_timer, &QTimer::timeout, this, &KisTouchHoldEventsPostponer::slotHoldCompletionTimeout);
29
30 m_timer.setSingleShot(true);
31#ifdef Q_OS_WIN
32 // on Windows, the coarse timers are too coarse for running is a unitest, so we should
33 // switch to a precise one inside unittests; for normal user-facing runs the precision
34 // is not necessary
35 m_timer.setTimerType(QStandardPaths::isTestModeEnabled() ? Qt::PreciseTimer : Qt::CoarseTimer);
36#else
37 m_timer.setTimerType(Qt::CoarseTimer);
38#endif
39 m_timer.setInterval(holdTimeout);
40 m_timer.start();
41}

References m_timer, and slotHoldCompletionTimeout().

◆ KisTouchHoldEventsPostponer() [2/2]

KisTouchHoldEventsPostponer::KisTouchHoldEventsPostponer ( const KisTouchHoldEventsPostponer & rhs)
delete

◆ ~KisTouchHoldEventsPostponer()

KisTouchHoldEventsPostponer::~KisTouchHoldEventsPostponer ( )

Definition at line 43 of file KisTouchHoldEventsPostponer.cpp.

44{
45 qDeleteAll(m_postponedEvents);
46}
QVector< QTouchEvent * > m_postponedEvents

References m_postponedEvents.

Member Function Documentation

◆ cancelHoldWait()

void KisTouchHoldEventsPostponer::cancelHoldWait ( )

Cancel waiting for a touch hold. The state will transition into HoldCancelled

Definition at line 67 of file KisTouchHoldEventsPostponer.cpp.

References HoldCancelled, m_state, and m_timer.

◆ enqueueEvent()

void KisTouchHoldEventsPostponer::enqueueEvent ( QTouchEvent * event)
private

Make a clone of the event and add to the queue. Can be removed after we stop supporting Qt5.

Definition at line 107 of file KisTouchHoldEventsPostponer.cpp.

108{
109#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
110 // TODO: change QScopedPointer -> std::unique_ptr
111 QScopedPointer<QEvent> clonedTouchEvent;
112 KoPointerEvent::copyQtPointerEvent(event, clonedTouchEvent);
113 m_postponedEvents.append(static_cast<QTouchEvent *>(clonedTouchEvent.take()));
114#else
115 m_postponedEvents.append(static_cast<QTouchEvent *>(event->clone()));
116#endif
117}

References m_postponedEvents.

◆ postponedEvents()

QVector< QTouchEvent * > KisTouchHoldEventsPostponer::postponedEvents ( )
Returns
the clones of all the postponed events, potentially including the one that caused the cancellation of the hold-wait. The postponer owns all the postponed events.

Definition at line 73 of file KisTouchHoldEventsPostponer.cpp.

74{
76 return m_postponedEvents;
77}
#define KIS_SAFE_ASSERT_RECOVER_NOOP(cond)
Definition kis_assert.h:130

References KIS_SAFE_ASSERT_RECOVER_NOOP, m_postponedEvents, m_state, and WaitingForHold.

◆ pushThrough()

void KisTouchHoldEventsPostponer::pushThrough ( QTouchEvent * event)

Push a new event through the postponer. The method should be called in WaitingForHold state only. In all other states the postponer is not considered as active anymore.

If event does not qualify as an event for touch-hold, then the state of the postponer will be switched into HoldCancelled, which should be handled by the called on exiting pushThrough().

Note: the event that caused the state transition into HoldCancelled state will still be added to the queue, so that it could be managed uniformely with the rest of the postponed events.

Definition at line 53 of file KisTouchHoldEventsPostponer.cpp.

54{
56
57 if (qualifyForPostponing(event)) {
58 enqueueEvent(event);
59 } else {
60 // enqueue the event to make sure it is delivered via
61 // the "deliver all the postponed events" mechanism
62 enqueueEvent(event);
64 }
65}
bool qualifyForPostponing(QTouchEvent *event)
#define KIS_SAFE_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:128

References cancelHoldWait(), enqueueEvent(), KIS_SAFE_ASSERT_RECOVER_RETURN, m_state, qualifyForPostponing(), and WaitingForHold.

◆ qualifyForPostponing()

bool KisTouchHoldEventsPostponer::qualifyForPostponing ( QTouchEvent * event)
private
Returns
if event can be considered as a touch-hold event

It is currently hardcoded to support only single-finger touch-holds.

Definition at line 94 of file KisTouchHoldEventsPostponer.cpp.

95{
97 if (numPressedPoints > 1)
98 return false;
99
101 if (pressDistance > m_maxHoldDistance)
102 return false;
103
104 return true;
105}
static Qt::TouchPointStates pressedOnlyTouchStates()
static qreal touchDragDistance(const QTouchEvent *event, Qt::TouchPointStates allowedStates)
static int countTouchPoints(const QTouchEvent *event, Qt::TouchPointStates allowedStates)

References KisTouchShortcut::countTouchPoints(), m_maxHoldDistance, KisTouchShortcut::pressedOnlyTouchStates(), and KisTouchShortcut::touchDragDistance().

◆ sigHoldCompleted

void KisTouchHoldEventsPostponer::sigHoldCompleted ( )
signal

Emitted when the postponed has detected a touch-and-hold action

◆ slotHoldCompletionTimeout

void KisTouchHoldEventsPostponer::slotHoldCompletionTimeout ( )
privateslot

Definition at line 79 of file KisTouchHoldEventsPostponer.cpp.

80{
81 // it might happen that the timer event has been emitted before
82 // a cancellation touch event was delivered, then the timer event
83 // may come "late"
84 if (m_state != WaitingForHold) {
85 // the caller should have processed all the postponed events in this case
87 return;
88 }
89
91 Q_EMIT sigHoldCompleted();
92}

References HoldCompleted, KIS_SAFE_ASSERT_RECOVER_NOOP, m_postponedEvents, m_state, sigHoldCompleted(), and WaitingForHold.

◆ state()

KisTouchHoldEventsPostponer::State KisTouchHoldEventsPostponer::state ( ) const
Returns
the current state of the postponer

Definition at line 48 of file KisTouchHoldEventsPostponer.cpp.

49{
50 return m_state;
51}

References m_state.

Member Data Documentation

◆ m_maxHoldDistance

qreal KisTouchHoldEventsPostponer::m_maxHoldDistance {20.0}
private

Definition at line 125 of file KisTouchHoldEventsPostponer.h.

125{20.0} ;

◆ m_postponedEvents

QVector<QTouchEvent*> KisTouchHoldEventsPostponer::m_postponedEvents
private

Definition at line 124 of file KisTouchHoldEventsPostponer.h.

◆ m_state

State KisTouchHoldEventsPostponer::m_state { WaitingForHold }
private

Definition at line 123 of file KisTouchHoldEventsPostponer.h.

◆ m_timer

QTimer KisTouchHoldEventsPostponer::m_timer
private

Definition at line 122 of file KisTouchHoldEventsPostponer.h.


The documentation for this class was generated from the following files: