Debian Codegraph @

src/Gui/SoTouchEvents.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2015 Victor Titov (DeepSOIC) <[email protected])> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
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. *
10 * *
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. *
15 * *
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 *
20 * *
21 ***************************************************************************/
22
23
24 #include <numbers>
25
26 #include <QApplication>
27 #include <QGestureEvent>
28 #include <QWidget>
29
30 #include <Base/Exception.h>
31 #include <Base/Tools.h>
32
33 #include "SoTouchEvents.h"
34
35
36 SO_EVENT_SOURCE(SoGestureEvent);
37
38 SbBool SoGestureEvent::isSoGestureEvent(const SoEvent* ev) const
39 {
40 return ev->isOfType(SoGestureEvent::getClassTypeId());
41 }
42
43 //----------------------------SoGesturePanEvent--------------------------------
44
45 SO_EVENT_SOURCE(SoGesturePanEvent);
46
47 SoGesturePanEvent::SoGesturePanEvent(QPanGesture* qpan, QWidget* widget)
48 {
49 Q_UNUSED(widget);
50 totalOffset = SbVec2f(qpan->offset().x(), -qpan->offset().y());
51 deltaOffset = SbVec2f(qpan->delta().x(), -qpan->delta().y());
52 state = SbGestureState(qpan->state());
53
54 Qt::KeyboardModifiers mods = QApplication::keyboardModifiers();
55 this->setAltDown(mods.testFlag(Qt::AltModifier));
56 this->setCtrlDown(mods.testFlag(Qt::ControlModifier));
57 this->setShiftDown(mods.testFlag(Qt::ShiftModifier));
58 this->setTime(SbTime::getTimeOfDay());
59 }
60
61 SbBool SoGesturePanEvent::isSoGesturePanEvent(const SoEvent* ev) const
62 {
63 return ev->isOfType(SoGesturePanEvent::getClassTypeId());
64 }
65
66 //----------------------------SoGesturePinchEvent--------------------------------
67
68 SO_EVENT_SOURCE(SoGesturePinchEvent);
69
70 SoGesturePinchEvent::SoGesturePinchEvent(QPinchGesture* qpinch, QWidget* widget)
71 {
72 int h = widget->height();
73 QPointF widgetCorner = QPointF(widget->mapToGlobal(QPoint(0, 0)));
74 qreal scaleToWidget
75 = (widget->mapFromGlobal(QPoint(800, 800)) - widget->mapFromGlobal(QPoint(0, 0))).x() / 800.0;
76 QPointF pnt; // temporary
77 pnt = qpinch->startCenterPoint();
78 pnt = (pnt - widgetCorner) * scaleToWidget; // translate screen coord. into widget coord.
79 startCenter = SbVec2f(pnt.x(), h - pnt.y());
80
81 pnt = qpinch->centerPoint();
82 pnt = (pnt - widgetCorner) * scaleToWidget;
83 curCenter = SbVec2f(pnt.x(), h - pnt.y());
84
85 pnt = qpinch->lastCenterPoint();
86 pnt = (pnt - widgetCorner) * scaleToWidget;
87 deltaCenter = curCenter - SbVec2f(pnt.x(), h - pnt.y());
88
89 deltaZoom = qpinch->scaleFactor();
90 totalZoom = qpinch->totalScaleFactor();
91
92 deltaAngle = -unbranchAngle(Base::toRadians(qpinch->rotationAngle() - qpinch->lastRotationAngle()));
93 totalAngle = Base::toRadians(-qpinch->totalRotationAngle());
94
95 state = SbGestureState(qpinch->state());
96
97 this->setPosition(SbVec2s(curCenter));
98 Qt::KeyboardModifiers mods = QApplication::keyboardModifiers();
99 this->setAltDown(mods.testFlag(Qt::AltModifier));
100 this->setCtrlDown(mods.testFlag(Qt::ControlModifier));
101 this->setShiftDown(mods.testFlag(Qt::ShiftModifier));
102 this->setTime(SbTime::getTimeOfDay());
103 }
104
105 SbBool SoGesturePinchEvent::isSoGesturePinchEvent(const SoEvent* ev) const
106 {
107 return ev->isOfType(SoGesturePinchEvent::getClassTypeId());
108 }
109
110 /*!
111 * \brief SoGesturePinchEvent::unbranchAngle : utility function to bring an angle into -pi..pi region.
112 * \param ang - in radians
113 * \return
114 */
115 double SoGesturePinchEvent::unbranchAngle(double ang)
116 {
117 using std::numbers::pi;
118
119 return ang - 2.0 * pi * floor((ang + pi) / (2.0 * pi));
120 }
121
122
123 //----------------------------SoGestureSwipeEvent--------------------------------
124
125 SO_EVENT_SOURCE(SoGestureSwipeEvent);
126
127 SoGestureSwipeEvent::SoGestureSwipeEvent(QSwipeGesture* qwsipe, QWidget* widget)
128 {
129 Q_UNUSED(widget);
130 angle = qwsipe->swipeAngle();
131 switch (qwsipe->verticalDirection()) {
132 case QSwipeGesture::Up:
133 vertDir = +1;
134 break;
135 case QSwipeGesture::Down:
136 vertDir = -1;
137 break;
138 default:
139 vertDir = 0;
140 break;
141 }
142 switch (qwsipe->horizontalDirection()) {
143 case QSwipeGesture::Right:
144 horzDir = +1;
145 break;
146 case QSwipeGesture::Left:
147 horzDir = -1;
148 break;
149 default:
150 horzDir = 0;
151 break;
152 }
153
154 state = SbGestureState(qwsipe->state());
155
156 Qt::KeyboardModifiers mods = QApplication::keyboardModifiers();
157 this->setAltDown(mods.testFlag(Qt::AltModifier));
158 this->setCtrlDown(mods.testFlag(Qt::ControlModifier));
159 this->setShiftDown(mods.testFlag(Qt::ShiftModifier));
160 this->setTime(SbTime::getTimeOfDay());
161 }
162
163 SbBool SoGestureSwipeEvent::isSoGestureSwipeEvent(const SoEvent* ev) const
164 {
165 return ev->isOfType(SoGestureSwipeEvent::getClassTypeId());
166 }
167
168
169 //----------------------------GesturesDevice-------------------------------
170
171 GesturesDevice::GesturesDevice(QWidget* widget)
172 : InputDevice(nullptr)
173 {
174 if (SoGestureEvent::getClassTypeId().isBad()) {
175 SoGestureEvent::initClass();
176 SoGesturePanEvent::initClass();
177 SoGesturePinchEvent::initClass();
178 SoGestureSwipeEvent::initClass();
179 }
180 if (!widget) {
181 throw Base::ValueError(
182 "Can't create a gestures quarter input device without widget (null pointer was passed)."
183 );
184 }
185 this->widget = widget;
186 }
187
188 const SoEvent* GesturesDevice::translateEvent(QEvent* event)
189 {
190 if (event->type() == QEvent::Gesture || event->type() == QEvent::GestureOverride) {
191 auto gevent = static_cast<QGestureEvent*>(event);
192
193 auto zg = static_cast<QPinchGesture*>(gevent->gesture(Qt::PinchGesture));
194 if (zg) {
195 gevent->setAccepted(Qt::PinchGesture, true); // prefer it over pan
196 return new SoGesturePinchEvent(zg, this->widget);
197 }
198
199 auto pg = static_cast<QPanGesture*>(gevent->gesture(Qt::PanGesture));
200 if (pg) {
201 gevent->setAccepted(Qt::PanGesture, true);
202 return new SoGesturePanEvent(pg, this->widget);
203 }
204
205 auto sg = static_cast<QSwipeGesture*>(gevent->gesture(Qt::SwipeGesture));
206 if (sg) {
207 gevent->setAccepted(Qt::SwipeGesture, true);
208 return new SoGesturePanEvent(pg, this->widget);
209 }
210 }
211 return nullptr;
212 }