Unity 8
 All Classes Functions
TouchGate.h
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  */
16 
17 #ifndef UBUNTU_TOUCH_GATE_H
18 #define UBUNTU_TOUCH_GATE_H
19 
20 #include "UbuntuGesturesQmlGlobal.h"
21 
22 #include <QQuickItem>
23 #include <QList>
24 #include <QPointer>
25 #include <QMap>
26 
27 #define TOUCHGATE_DEBUG 0
28 
29 class TouchOwnershipEvent;
30 
31 /*
32  Blocks the passage of events until ownership over the related touch points is granted.
33 
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.
36 
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  */
41 class UBUNTUGESTURESQML_EXPORT TouchGate : public QQuickItem {
42  Q_OBJECT
43 
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)
46 
47 public:
48  bool event(QEvent *e) override;
49 
50  QQuickItem *targetItem() { return m_targetItem; }
51  void setTargetItem(QQuickItem *item);
52 
53 Q_SIGNALS:
54  void targetItemChanged(QQuickItem *item);
55 
56 protected:
57  void touchEvent(QTouchEvent *event) override;
58 private:
59  class TouchEvent {
60  public:
61  TouchEvent(const QTouchEvent *event);
62 
63  bool removeTouch(int touchId);
64 
65  QEvent::Type eventType;
66  QTouchDevice *device;
67  Qt::KeyboardModifiers modifiers;
68  QList<QTouchEvent::TouchPoint> touchPoints;
69  QObject *target;
70  QWindow *window;
71  ulong timestamp;
72  };
73 
74  void touchOwnershipEvent(TouchOwnershipEvent *event);
75  bool isTouchPointOwned(int touchId) const;
76  void storeTouchEvent(const QTouchEvent *event);
77  void removeTouchFromStoredEvents(int touchId);
78  void dispatchFullyOwnedEvents();
79  bool eventIsFullyOwned(const TouchEvent &event) const;
80 
81  void dispatchTouchEventToTarget(const TouchEvent &event);
82  void dispatchTouchEventToTarget(QTouchEvent* event);
83  void dispatchTouchEventToTarget(
84  QEvent::Type eventType,
85  QTouchDevice *device,
86  Qt::KeyboardModifiers modifiers,
87  const QList<QTouchEvent::TouchPoint> &touchPoints,
88  QObject *target,
89  QWindow *window,
90  ulong timestamp);
91 
92  void transformTouchPoints(QList<QTouchEvent::TouchPoint> &touchPoints, const QTransform &transform);
93  static QTouchEvent *createQTouchEvent(QEvent::Type eventType,
94  QTouchDevice *device,
95  Qt::KeyboardModifiers modifiers,
96  const QList<QTouchEvent::TouchPoint> &touchPoints,
97  QObject *target,
98  QWindow *window,
99  ulong timestamp);
100  void removeTouchInfoForEndedTouches(const QList<QTouchEvent::TouchPoint> &touchPoints);
101 
102  #if TOUCHGATE_DEBUG
103  QString oldestPendingTouchIdsString();
104  #endif
105 
106  QList<TouchEvent> m_storedEvents;
107 
108  enum {
109  OwnershipUndefined,
110  OwnershipRequested,
111  OwnershipGranted,
112  };
113  class TouchInfo {
114  public:
115  TouchInfo() {ownership = OwnershipUndefined; ended = false;}
116  int ownership;
117  bool ended;
118  };
119  QMap<int, TouchInfo> m_touchInfoMap;
120 
121  QPointer<QQuickItem> m_targetItem;
122 
123  friend class tst_TouchGate;
124 };
125 
126 #endif // UBUNTU_TOUCH_GATE_H