1 /* This file is part of the KDE project
2 
3    Copyright (C) 2006 Thorsten Zachmann <zachmann@kde.org>
4    Copyright (C) 2006 C. Boemann Rasmussen <cbo@boemann.dk>
5    Copyright (C) 2006-2007 Thomas Zander <zander@kde.org>
6 
7    This library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Library General Public
9    License as published by the Free Software Foundation; either
10    version 2 of the License, or (at your option) any later version.
11 
12    This library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Library General Public License for more details.
16 
17    You should have received a copy of the GNU Library General Public License
18    along with this library; see the file COPYING.LIB.  If not, write to
19    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21 */
22 
23 #include "KoPointerEvent.h"
24 #include "KoInputDeviceHandlerEvent.h"
25 #include <QTabletEvent>
26 #include <QMouseEvent>
27 #include <QWheelEvent>
28 #include <QTouchEvent>
29 #include <QGraphicsSceneMouseEvent>
30 
31 class Q_DECL_HIDDEN KoPointerEvent::Private
32 {
33 public:
Private()34     Private()
35         : tabletEvent(0), mouseEvent(0), wheelEvent(0), touchEvent(0), gsMouseEvent(0)
36         , gsWheelEvent(0), deviceEvent(0), tabletButton(Qt::NoButton)
37         , globalPos(0, 0), pos(0, 0), posZ(0), rotationX(0), rotationY(0)
38         , rotationZ(0) {}
39     QTabletEvent *tabletEvent;
40     QMouseEvent *mouseEvent;
41     QWheelEvent *wheelEvent;
42     QTouchEvent *touchEvent;
43     QGraphicsSceneMouseEvent *gsMouseEvent;
44     QGraphicsSceneWheelEvent *gsWheelEvent;
45     KoInputDeviceHandlerEvent *deviceEvent;
46     Qt::MouseButton tabletButton;
47     QPoint globalPos, pos;
48     int posZ;
49     int rotationX, rotationY, rotationZ;
50 };
51 
KoPointerEvent(QMouseEvent * ev,const QPointF & pnt)52 KoPointerEvent::KoPointerEvent(QMouseEvent *ev, const QPointF &pnt)
53     : point(pnt),
54       m_event(ev),
55       d(new Private())
56 {
57     Q_ASSERT(m_event);
58     d->mouseEvent = ev;
59 }
60 
KoPointerEvent(QGraphicsSceneMouseEvent * ev,const QPointF & pnt)61 KoPointerEvent::KoPointerEvent(QGraphicsSceneMouseEvent *ev, const QPointF &pnt)
62     : point(pnt),
63       m_event(ev),
64       d(new Private())
65 {
66     Q_ASSERT(m_event);
67     d->gsMouseEvent = ev;
68 }
69 
KoPointerEvent(QGraphicsSceneWheelEvent * ev,const QPointF & pnt)70 KoPointerEvent::KoPointerEvent(QGraphicsSceneWheelEvent *ev, const QPointF &pnt)
71     : point(pnt),
72       m_event(ev),
73       d(new Private())
74 {
75     Q_ASSERT(m_event);
76     d->gsWheelEvent = ev;
77 }
78 
KoPointerEvent(QTabletEvent * ev,const QPointF & pnt)79 KoPointerEvent::KoPointerEvent(QTabletEvent *ev, const QPointF &pnt)
80     : point(pnt),
81       m_event(ev),
82       d(new Private())
83 {
84     Q_ASSERT(m_event);
85     d->tabletEvent = ev;
86 }
87 
KoPointerEvent(QTouchEvent * ev,const QPointF & pnt,const QVector<KoTouchPoint> & _touchPoints)88 KoPointerEvent::KoPointerEvent(QTouchEvent *ev, const QPointF &pnt, const QVector<KoTouchPoint> &_touchPoints)
89     : point (pnt)
90     , touchPoints(_touchPoints)
91     , m_event(ev)
92     , d(new Private())
93 {
94     Q_ASSERT(m_event);
95     d->touchEvent = ev;
96 }
97 
KoPointerEvent(QWheelEvent * ev,const QPointF & pnt)98 KoPointerEvent::KoPointerEvent(QWheelEvent *ev, const QPointF &pnt)
99     : point(pnt),
100       m_event(ev),
101       d(new Private())
102 {
103     Q_ASSERT(m_event);
104     d->wheelEvent = ev;
105 }
106 
KoPointerEvent(KoInputDeviceHandlerEvent * ev,int x,int y,int z,int rx,int ry,int rz)107 KoPointerEvent::KoPointerEvent(KoInputDeviceHandlerEvent * ev, int x, int y, int z, int rx, int ry, int rz)
108     : m_event(ev)
109     , d(new Private())
110 {
111     Q_ASSERT(m_event);
112     d->deviceEvent = ev;
113     d->pos = QPoint(x, y);
114     d->posZ = z;
115     d->rotationX = rx;
116     d->rotationY = ry;
117     d->rotationZ = rz;
118 }
119 
KoPointerEvent(KoPointerEvent * event,const QPointF & point)120 KoPointerEvent::KoPointerEvent(KoPointerEvent *event, const QPointF &point)
121     : point(point)
122     , touchPoints(event->touchPoints)
123     , m_event(event->m_event)
124     , d(new Private(*(event->d)))
125 {
126     Q_ASSERT(m_event);
127 }
128 
KoPointerEvent(const KoPointerEvent & rhs)129 KoPointerEvent::KoPointerEvent(const KoPointerEvent &rhs)
130     : point(rhs.point)
131     , touchPoints(rhs.touchPoints)
132     , m_event(rhs.m_event)
133     , d(new Private(*rhs.d))
134 {
135 }
136 
~KoPointerEvent()137 KoPointerEvent::~KoPointerEvent()
138 {
139     delete d;
140 }
141 
button() const142 Qt::MouseButton KoPointerEvent::button() const
143 {
144     if (d->mouseEvent)
145         return d->mouseEvent->button();
146     else if (d->tabletEvent || d->touchEvent)
147         return d->tabletButton;
148     else if (d->deviceEvent)
149         return d->deviceEvent->button();
150     else if (d->gsMouseEvent)
151         return d->gsMouseEvent->button();
152     else
153         return Qt::NoButton;
154 }
155 
buttons() const156 Qt::MouseButtons KoPointerEvent::buttons() const
157 {
158     if (d->mouseEvent)
159         return d->mouseEvent->buttons();
160     else if (d->wheelEvent)
161         return d->wheelEvent->buttons();
162     else if (d->tabletEvent || d->touchEvent)
163         return d->tabletButton;
164     else if (d->deviceEvent)
165         return d->deviceEvent->buttons();
166     else if (d->gsMouseEvent)
167         return d->gsMouseEvent->buttons();
168     else if (d->gsWheelEvent)
169         return d->gsWheelEvent->buttons();
170     return Qt::NoButton;
171 }
172 
globalPos() const173 QPoint KoPointerEvent::globalPos() const
174 {
175     if (d->mouseEvent)
176         return d->mouseEvent->globalPos();
177     else if (d->wheelEvent)
178         return d->wheelEvent->globalPos();
179     else if (d->tabletEvent)
180         return d->tabletEvent->globalPos();
181     else if (d->gsMouseEvent)
182         return d->gsMouseEvent->screenPos();
183     else if (d->gsWheelEvent)
184         return d->gsWheelEvent->screenPos();
185     else
186         return d->globalPos;
187 }
188 
pos() const189 QPoint KoPointerEvent::pos() const
190 {
191     if (d->mouseEvent)
192         return d->mouseEvent->pos();
193     else if (d->wheelEvent)
194         return d->wheelEvent->pos();
195     else if (d->tabletEvent)
196         return d->tabletEvent->pos();
197     else if (d->gsMouseEvent)
198         return d->gsMouseEvent->pos().toPoint();
199     else if (d->gsWheelEvent)
200         return d->gsWheelEvent->pos().toPoint();
201     else
202         return d->pos;
203 }
204 
pressure() const205 qreal KoPointerEvent::pressure() const
206 {
207     if (d->tabletEvent)
208         return d->tabletEvent->pressure();
209     else
210         return 1.0;
211 }
212 
rotation() const213 qreal KoPointerEvent::rotation() const
214 {
215     if (d->tabletEvent)
216         return d->tabletEvent->rotation();
217     else
218         return 0.0;
219 }
220 
tangentialPressure() const221 qreal KoPointerEvent::tangentialPressure() const
222 {
223     if (d->tabletEvent)
224         return d->tabletEvent->tangentialPressure();
225     else
226         return 0.0;
227 }
228 
x() const229 int KoPointerEvent::x() const
230 {
231     if (d->tabletEvent)
232         return d->tabletEvent->x();
233     if (d->wheelEvent)
234         return d->wheelEvent->x();
235     else if (d->mouseEvent)
236         return d->mouseEvent->x();
237     else
238         return pos().x();
239 }
240 
xTilt() const241 int KoPointerEvent::xTilt() const
242 {
243     if (d->tabletEvent)
244         return d->tabletEvent->xTilt();
245     else
246         return 0;
247 }
248 
y() const249 int KoPointerEvent::y() const
250 {
251     if (d->tabletEvent)
252         return d->tabletEvent->y();
253     if (d->wheelEvent)
254         return d->wheelEvent->y();
255     else if (d->mouseEvent)
256         return d->mouseEvent->y();
257     else
258         return pos().y();
259 }
260 
yTilt() const261 int KoPointerEvent::yTilt() const
262 {
263     if (d->tabletEvent)
264         return d->tabletEvent->yTilt();
265     else
266         return 0;
267 }
268 
z() const269 int KoPointerEvent::z() const
270 {
271     if (d->tabletEvent)
272         return d->tabletEvent->z();
273     else if (d->deviceEvent)
274         return d->posZ;
275     else
276         return 0;
277 }
278 
delta() const279 int KoPointerEvent::delta() const
280 {
281     if (d->wheelEvent)
282         return d->wheelEvent->delta();
283     else if (d->gsWheelEvent)
284         return d->gsWheelEvent->delta();
285     else
286         return 0;
287 }
288 
rotationX() const289 int KoPointerEvent::rotationX() const
290 {
291     return d->rotationX;
292 }
293 
rotationY() const294 int KoPointerEvent::rotationY() const
295 {
296     return d->rotationY;
297 }
298 
rotationZ() const299 int KoPointerEvent::rotationZ() const
300 {
301     return d->rotationZ;
302 }
303 
orientation() const304 Qt::Orientation KoPointerEvent::orientation() const
305 {
306     if (d->wheelEvent)
307         return d->wheelEvent->orientation();
308     else if (d->gsWheelEvent)
309         return d->gsWheelEvent->orientation();
310     else
311         return Qt::Horizontal;
312 }
313 
isTabletEvent()314 bool KoPointerEvent::isTabletEvent()
315 {
316     return dynamic_cast<QTabletEvent*>(m_event) != 0;
317 }
318 
setTabletButton(Qt::MouseButton button)319 void KoPointerEvent::setTabletButton(Qt::MouseButton button)
320 {
321     d->tabletButton = button;
322 }
323 
modifiers() const324 Qt::KeyboardModifiers KoPointerEvent::modifiers() const
325 {
326     if (d->tabletEvent)
327         return d->tabletEvent->modifiers();
328     else if (d->mouseEvent)
329         return d->mouseEvent->modifiers();
330     else if (d->wheelEvent)
331         return d->wheelEvent->modifiers();
332     else if (d->deviceEvent)
333         return d->deviceEvent->modifiers();
334     else if (d->gsMouseEvent)
335         return d->gsMouseEvent->modifiers();
336     else if (d->gsWheelEvent)
337         return d->gsWheelEvent->modifiers();
338     else
339         return Qt::NoModifier;
340 }
341