on 6ca9421 help sign in
124 lines 3.7 kB history raw
1/*
2 * Copyright (C) 2014 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#ifndef UBUNTU_TOUCH_GATE_H
18#define UBUNTU_TOUCH_GATE_H
20#include "UbuntuGesturesQmlGlobal.h"
21#include "TouchDispatcher.h"
23#include <QQuickItem>
24#include <QList>
25#include <QMap>
27#define TOUCHGATE_DEBUG 0
29class TouchOwnershipEvent;
31/*
32 Blocks the passage of events until ownership over the related touch points is granted.
34 Blocked touch events won't be discarded. Instead they will be buffered until ownership
35 is granted. If ownership is given to another item, the event buffer is cleared.
37 A TouchGate is useful as a mediator for items that do not understand, or gracefully handle,
38 touch canceling. By having a TouchGate in front of them you guarantee that only owned touches (i.e.,
39 touches that won't be canceled later) reaches them.
40 */
41class UBUNTUGESTURESQML_EXPORT TouchGate : public QQuickItem {
42 Q_OBJECT
44 // Item that's going to receive the touch events that make it through the gate.
45 Q_PROPERTY(QQuickItem* targetItem READ targetItem WRITE setTargetItem NOTIFY targetItemChanged)
47public:
48 TouchGate(QQuickItem *parent = nullptr);
50 bool event(QEvent *e) override;
52 QQuickItem *targetItem() { return m_dispatcher.targetItem(); }
53 void setTargetItem(QQuickItem *item);
55Q_SIGNALS:
56 void targetItemChanged(QQuickItem *item);
58protected:
59 void touchEvent(QTouchEvent *event) override;
60 void itemChange(ItemChange change, const ItemChangeData &value) override;
62private Q_SLOTS:
63 void onEnabledChanged();
65private:
66 void reset();
68 class TouchEvent {
69 public:
70 TouchEvent(QTouchDevice *device,
71 Qt::KeyboardModifiers modifiers,
72 const QList<QTouchEvent::TouchPoint> &touchPoints,
73 QWindow *window,
74 ulong timestamp);
76 bool removeTouch(int touchId);
78 QTouchDevice *device;
79 Qt::KeyboardModifiers modifiers;
80 QList<QTouchEvent::TouchPoint> touchPoints;
81 QWindow *window;
82 ulong timestamp;
83 };
85 void touchOwnershipEvent(TouchOwnershipEvent *event);
86 bool isTouchPointOwned(int touchId) const;
87 void storeTouchEvent(QTouchDevice *device,
88 Qt::KeyboardModifiers modifiers,
89 const QList<QTouchEvent::TouchPoint> &touchPoints,
90 QWindow *window,
91 ulong timestamp);
92 void removeTouchFromStoredEvents(int touchId);
93 void dispatchFullyOwnedEvents();
94 bool eventIsFullyOwned(const TouchEvent &event) const;
96 void dispatchTouchEventToTarget(const TouchEvent &event);
98 void removeTouchInfoForEndedTouches(const QList<QTouchEvent::TouchPoint> &touchPoints);
100 #if TOUCHGATE_DEBUG
101 QString oldestPendingTouchIdsString();
102 #endif
104 QList<TouchEvent> m_storedEvents;
106 enum {
107 OwnershipUndefined,
108 OwnershipRequested,
109 OwnershipGranted,
110 };
111 class TouchInfo {
112 public:
113 TouchInfo() {ownership = OwnershipUndefined; ended = false;}
114 int ownership;
115 bool ended;
116 };
117 QMap<int, TouchInfo> m_touchInfoMap;
119 TouchDispatcher m_dispatcher;
121 friend class tst_TouchGate;
122};
124#endif // UBUNTU_TOUCH_GATE_H