1
/***************************************************************************
2
* Copyright (c) 2015 Victor Titov (DeepSOIC) <vv.titov@gmail.com)> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
23
#include "PreCompiled.h"
25
#include <QApplication>
26
#include <QGestureEvent>
29
#include <Base/Exception.h>
31
#include "SoTouchEvents.h"
34
SO_EVENT_SOURCE(SoGestureEvent);
36
SbBool SoGestureEvent::isSoGestureEvent(const SoEvent *ev) const
38
return ev->isOfType(SoGestureEvent::getClassTypeId());
41
//----------------------------SoGesturePanEvent--------------------------------
43
SO_EVENT_SOURCE(SoGesturePanEvent);
45
SoGesturePanEvent::SoGesturePanEvent(QPanGesture* qpan, QWidget *widget)
48
totalOffset = SbVec2f(qpan->offset().x(), -qpan->offset().y());
49
deltaOffset = SbVec2f(qpan->delta().x(), -qpan->delta().y());
50
state = SbGestureState(qpan->state());
52
Qt::KeyboardModifiers mods = QApplication::keyboardModifiers();
53
this->setAltDown(mods.testFlag(Qt::AltModifier));
54
this->setCtrlDown(mods.testFlag(Qt::ControlModifier));
55
this->setShiftDown(mods.testFlag(Qt::ShiftModifier));
56
this->setTime(SbTime::getTimeOfDay());
59
SbBool SoGesturePanEvent::isSoGesturePanEvent(const SoEvent *ev) const
61
return ev->isOfType(SoGesturePanEvent::getClassTypeId());
64
//----------------------------SoGesturePinchEvent--------------------------------
66
SO_EVENT_SOURCE(SoGesturePinchEvent);
68
SoGesturePinchEvent::SoGesturePinchEvent(QPinchGesture* qpinch, QWidget *widget)
70
int h = widget->height();
71
QPointF widgetCorner = QPointF(widget->mapToGlobal(QPoint(0,0)));
72
qreal scaleToWidget = (widget->mapFromGlobal(QPoint(800,800))-widget->mapFromGlobal(QPoint(0,0))).x()/800.0;
73
QPointF pnt;//temporary
74
pnt = qpinch->startCenterPoint();
75
pnt = (pnt-widgetCorner)*scaleToWidget;//translate screen coord. into widget coord.
76
startCenter = SbVec2f(pnt.x(), h - pnt.y());
78
pnt = qpinch->centerPoint();
79
pnt = (pnt-widgetCorner)*scaleToWidget;
80
curCenter = SbVec2f(pnt.x(), h - pnt.y());
82
pnt = qpinch->lastCenterPoint();
83
pnt = (pnt-widgetCorner)*scaleToWidget;
84
deltaCenter = curCenter - SbVec2f(pnt.x(), h - pnt.y());
86
deltaZoom = qpinch->scaleFactor();
87
totalZoom = qpinch->totalScaleFactor();
89
deltaAngle = -unbranchAngle((qpinch->rotationAngle()-qpinch->lastRotationAngle()) / 180.0 * M_PI);
90
totalAngle = -qpinch->totalRotationAngle() / 180 * M_PI;
92
state = SbGestureState(qpinch->state());
94
this->setPosition(SbVec2s(curCenter));
95
Qt::KeyboardModifiers mods = QApplication::keyboardModifiers();
96
this->setAltDown(mods.testFlag(Qt::AltModifier));
97
this->setCtrlDown(mods.testFlag(Qt::ControlModifier));
98
this->setShiftDown(mods.testFlag(Qt::ShiftModifier));
99
this->setTime(SbTime::getTimeOfDay());
102
SbBool SoGesturePinchEvent::isSoGesturePinchEvent(const SoEvent *ev) const
104
return ev->isOfType(SoGesturePinchEvent::getClassTypeId());
108
* \brief SoGesturePinchEvent::unbranchAngle : utility function to bring an angle into -pi..pi region.
109
* \param ang - in radians
112
double SoGesturePinchEvent::unbranchAngle(double ang)
114
return ang - 2.0 * M_PI * floor((ang + M_PI) / (2.0 * M_PI));
118
//----------------------------SoGestureSwipeEvent--------------------------------
120
SO_EVENT_SOURCE(SoGestureSwipeEvent);
122
SoGestureSwipeEvent::SoGestureSwipeEvent(QSwipeGesture *qwsipe, QWidget *widget)
125
angle = qwsipe->swipeAngle();
126
switch (qwsipe->verticalDirection()){
127
case QSwipeGesture::Up :
130
case QSwipeGesture::Down :
137
switch (qwsipe->horizontalDirection()){
138
case QSwipeGesture::Right :
141
case QSwipeGesture::Left :
149
state = SbGestureState(qwsipe->state());
151
Qt::KeyboardModifiers mods = QApplication::keyboardModifiers();
152
this->setAltDown(mods.testFlag(Qt::AltModifier));
153
this->setCtrlDown(mods.testFlag(Qt::ControlModifier));
154
this->setShiftDown(mods.testFlag(Qt::ShiftModifier));
155
this->setTime(SbTime::getTimeOfDay());
158
SbBool SoGestureSwipeEvent::isSoGestureSwipeEvent(const SoEvent *ev) const
160
return ev->isOfType(SoGestureSwipeEvent::getClassTypeId());
164
//----------------------------GesturesDevice-------------------------------
166
GesturesDevice::GesturesDevice(QWidget* widget)
167
: InputDevice(nullptr)
169
if (SoGestureEvent::getClassTypeId().isBad()){
170
SoGestureEvent::initClass();
171
SoGesturePanEvent::initClass();
172
SoGesturePinchEvent::initClass();
173
SoGestureSwipeEvent::initClass();
176
throw Base::ValueError("Can't create a gestures quarter input device without widget (null pointer was passed).");
177
this->widget = widget;
180
const SoEvent* GesturesDevice::translateEvent(QEvent* event)
182
if (event->type() == QEvent::Gesture
183
|| event->type() == QEvent::GestureOverride) {
184
auto gevent = static_cast<QGestureEvent*>(event);
186
auto zg = static_cast<QPinchGesture*>(gevent->gesture(Qt::PinchGesture));
188
gevent->setAccepted(Qt::PinchGesture,true);//prefer it over pan
189
return new SoGesturePinchEvent(zg,this->widget);
192
auto pg = static_cast<QPanGesture*>(gevent->gesture(Qt::PanGesture));
194
gevent->setAccepted(Qt::PanGesture,true);
195
return new SoGesturePanEvent(pg,this->widget);
198
auto sg = static_cast<QSwipeGesture*>(gevent->gesture(Qt::SwipeGesture));
200
gevent->setAccepted(Qt::SwipeGesture,true);
201
return new SoGesturePanEvent(pg,this->widget);