Debian Codegraph @

src/Gui/GuiApplicationNativeEventAware.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2010 Thomas Anderson <ta@nextgenengineering> *
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 <iomanip>
25 #include <sstream>
26
27 #include <QMainWindow>
28 #include <FCConfig.h>
29 #include "Application.h"
30 #include "GuiApplicationNativeEventAware.h"
31 #include "SpaceballEvent.h"
32
33
34 #if defined(_USE_3DCONNEXION_SDK) || defined(SPNAV_FOUND)
35 # if defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD)
36 # if defined(SPNAV_USE_X11)
37 # include "3Dconnexion/GuiNativeEventLinuxX11.h"
38 # else
39 # include "3Dconnexion/GuiNativeEventLinux.h"
40 # endif
41 # elif defined(Q_OS_WIN)
42 # include "3Dconnexion/GuiNativeEventWin32.h"
43 # elif defined(Q_OS_MACOS)
44 # include "3Dconnexion/GuiNativeEventMac.h"
45 # endif // Platform switch
46 #endif // Spacemice
47
48 Gui::GUIApplicationNativeEventAware::GUIApplicationNativeEventAware(int& argc, char* argv[])
49 : QApplication(argc, argv)
50 , spaceballPresent(false)
51 {
52 #if defined(_USE_3DCONNEXION_SDK) || defined(SPNAV_FOUND)
53 nativeEvent = new Gui::GuiNativeEvent(this);
54 #endif
55 }
56
57 Gui::GUIApplicationNativeEventAware::~GUIApplicationNativeEventAware() = default;
58
59 void Gui::GUIApplicationNativeEventAware::initSpaceball(QMainWindow* window)
60 {
61 #if defined(_USE_3DCONNEXION_SDK) || defined(SPNAV_FOUND)
62 # if defined(USE_3DCONNEXION_NAVLIB)
63 ParameterGrp::handle hViewGrp = App::GetApplication().GetParameterGroupByPath(
64 "User parameter:BaseApp/Preferences/View"
65 );
66 if (nativeEvent && hViewGrp->GetBool("LegacySpaceMouseDevices", false)) {
67 nativeEvent->initSpaceball(window);
68 }
69 # else
70 nativeEvent->initSpaceball(window);
71 # endif
72 #else
73 Q_UNUSED(window);
74 #endif
75 Spaceball::MotionEvent::MotionEventType = QEvent::registerEventType();
76 Spaceball::ButtonEvent::ButtonEventType = QEvent::registerEventType();
77 }
78
79 bool Gui::GUIApplicationNativeEventAware::processSpaceballEvent(QObject* object, QEvent* event)
80 {
81 if (!activeWindow()) {
82 qDebug("No active window\n");
83 return true;
84 }
85
86 QApplication::notify(object, event);
87 if (event->type() == Spaceball::MotionEvent::MotionEventType) {
88 auto motionEvent = dynamic_cast<Spaceball::MotionEvent*>(event);
89 if (!motionEvent) {
90 return true;
91 }
92 if (!motionEvent->isHandled()) {
93 // make a new event and post to parent.
94 auto newEvent = new Spaceball::MotionEvent(*motionEvent);
95 postEvent(object->parent(), newEvent);
96 }
97 }
98
99 if (event->type() == Spaceball::ButtonEvent::ButtonEventType) {
100 auto buttonEvent = dynamic_cast<Spaceball::ButtonEvent*>(event);
101 if (!buttonEvent) {
102 return true;
103 }
104 if (!buttonEvent->isHandled()) {
105 // make a new event and post to parent.
106 auto newEvent = new Spaceball::ButtonEvent(*buttonEvent);
107 postEvent(object->parent(), newEvent);
108 }
109 }
110 return true;
111 }
112
113 void Gui::GUIApplicationNativeEventAware::postMotionEvent(std::vector<int> motionDataArray)
114 {
115 auto currentWidget(focusWidget());
116 if (!currentWidget) {
117 return;
118 }
119 importSettings(motionDataArray);
120
121 auto motionEvent = new Spaceball::MotionEvent();
122 motionEvent->setTranslations(motionDataArray[0], motionDataArray[1], motionDataArray[2]);
123 motionEvent->setRotations(motionDataArray[3], motionDataArray[4], motionDataArray[5]);
124 this->postEvent(currentWidget, motionEvent);
125 }
126
127 void Gui::GUIApplicationNativeEventAware::postButtonEvent(int buttonNumber, int buttonPress)
128 {
129 auto currentWidget(focusWidget());
130 if (!currentWidget) {
131 return;
132 }
133
134 auto buttonEvent = new Spaceball::ButtonEvent();
135 buttonEvent->setButtonNumber(buttonNumber);
136 if (buttonPress) {
137 buttonEvent->setButtonStatus(Spaceball::BUTTON_PRESSED);
138 }
139 else {
140 buttonEvent->setButtonStatus(Spaceball::BUTTON_RELEASED);
141 }
142 this->postEvent(currentWidget, buttonEvent);
143 }
144
145 float Gui::GUIApplicationNativeEventAware::convertPrefToSensitivity(int value)
146 {
147 if (value < 0) {
148 return ((0.9 / 50) * float(value) + 1);
149 }
150 else {
151 return ((2.5 / 50) * float(value) + 1);
152 }
153 }
154
155 void Gui::GUIApplicationNativeEventAware::importSettings(std::vector<int>& motionDataArray)
156 {
157 ParameterGrp::handle group = App::GetApplication()
158 .GetUserParameter()
159 .GetGroup("BaseApp")
160 ->GetGroup("Spaceball")
161 ->GetGroup("Motion");
162
163 // Remapping of motion data
164 long remap = group->GetInt("Remapping", 12345);
165 if (remap != 12345) {
166 std::stringstream s;
167 s << std::setfill('0') << std::setw(6) << remap;
168
169 std::string str;
170 s >> str;
171
172 // the string must have a length of 6 and it must contain all digits 0,...,5
173 std::string::size_type pos1 = str.find_first_not_of("012345");
174 std::string::size_type pos2 = std::string("012345").find_first_not_of(str);
175 if (pos1 == std::string::npos && pos2 == std::string::npos) {
176 std::vector<int> vec(str.size());
177 std::transform(str.begin(), str.end(), vec.begin(), [](char c) -> int { return c - '0'; });
178
179 std::vector<int> copy = motionDataArray;
180 for (int i = 0; i < 6; i++) {
181 motionDataArray[i] = copy[vec[i]];
182 }
183 }
184 }
185
186 // here I import settings from a dialog. For now they are set as is
187 bool dominant = group->GetBool("Dominant"); // Is dominant checked
188 bool flipXY = group->GetBool("FlipYZ");
189 ; // Is Flip X/Y checked
190 float generalSensitivity = convertPrefToSensitivity(group->GetInt("GlobalSensitivity"));
191
192 // array that has stored info about "Enabled" checkboxes of all axes
193 bool enabled[6];
194 enabled[0] = group->GetBool("Translations", true) && group->GetBool("PanLREnable", true);
195 enabled[1] = group->GetBool("Translations", true) && group->GetBool("PanUDEnable", true);
196 enabled[2] = group->GetBool("Translations", true) && group->GetBool("ZoomEnable", true);
197 enabled[3] = group->GetBool("Rotations", true) && group->GetBool("TiltEnable", true);
198 enabled[4] = group->GetBool("Rotations", true) && group->GetBool("RollEnable", true);
199 enabled[5] = group->GetBool("Rotations", true) && group->GetBool("SpinEnable", true);
200
201 // array that has stored info about "Reversed" checkboxes of all axes
202 bool reversed[6];
203 reversed[0] = group->GetBool("PanLRReverse");
204 reversed[1] = group->GetBool("PanUDReverse");
205 reversed[2] = group->GetBool("ZoomReverse");
206 reversed[3] = group->GetBool("TiltReverse");
207 reversed[4] = group->GetBool("RollReverse");
208 reversed[5] = group->GetBool("SpinReverse");
209
210 // array that has stored info about sliders - on each slider you need to use method
211 // DlgSpaceballSettings::GetValuefromSlider which will convert <-50, 50> linear integers from
212 // slider to <0.1, 10> exponential floating values
213 float sensitivity[6];
214 sensitivity[0] = convertPrefToSensitivity(group->GetInt("PanLRSensitivity"));
215 sensitivity[1] = convertPrefToSensitivity(group->GetInt("PanUDSensitivity"));
216 sensitivity[2] = convertPrefToSensitivity(group->GetInt("ZoomSensitivity"));
217 sensitivity[3] = convertPrefToSensitivity(group->GetInt("TiltSensitivity"));
218 sensitivity[4] = convertPrefToSensitivity(group->GetInt("RollSensitivity"));
219 sensitivity[5] = convertPrefToSensitivity(group->GetInt("SpinSensitivity"));
220
221 if (group->GetBool("Calibrate")) {
222 group->SetInt("CalibrationX", motionDataArray[0]);
223 group->SetInt("CalibrationY", motionDataArray[1]);
224 group->SetInt("CalibrationZ", motionDataArray[2]);
225 group->SetInt("CalibrationXr", motionDataArray[3]);
226 group->SetInt("CalibrationYr", motionDataArray[4]);
227 group->SetInt("CalibrationZr", motionDataArray[5]);
228
229 group->RemoveBool("Calibrate");
230
231 return;
232 }
233 else {
234 motionDataArray[0] = motionDataArray[0] - group->GetInt("CalibrationX");
235 motionDataArray[1] = motionDataArray[1] - group->GetInt("CalibrationY");
236 motionDataArray[2] = motionDataArray[2] - group->GetInt("CalibrationZ");
237 motionDataArray[3] = motionDataArray[3] - group->GetInt("CalibrationXr");
238 motionDataArray[4] = motionDataArray[4] - group->GetInt("CalibrationYr");
239 motionDataArray[5] = motionDataArray[5] - group->GetInt("CalibrationZr");
240 }
241
242 int i;
243
244 if (flipXY) {
245 bool tempBool;
246 float tempFloat;
247
248 tempBool = enabled[1];
249 enabled[1] = enabled[2];
250 enabled[2] = tempBool;
251
252 tempBool = enabled[4];
253 enabled[4] = enabled[5];
254 enabled[5] = tempBool;
255
256
257 tempBool = reversed[1];
258 reversed[1] = reversed[2];
259 reversed[2] = tempBool;
260
261 tempBool = reversed[4];
262 reversed[4] = reversed[5];
263 reversed[5] = tempBool;
264
265
266 tempFloat = sensitivity[1];
267 sensitivity[1] = sensitivity[2];
268 sensitivity[2] = tempFloat;
269
270 tempFloat = sensitivity[4];
271 sensitivity[4] = sensitivity[5];
272 sensitivity[5] = tempFloat;
273
274
275 i = motionDataArray[1];
276 motionDataArray[1] = motionDataArray[2];
277 motionDataArray[2] = -i;
278
279 i = motionDataArray[4];
280 motionDataArray[4] = motionDataArray[5];
281 motionDataArray[5] = -i;
282 }
283
284 if (dominant) { // if dominant is checked
285 int max = 0;
286 bool flag = false;
287 for (i = 0; i < 6; ++i) {
288 if (abs(motionDataArray[i]) > abs(max)) {
289 max = motionDataArray[i];
290 }
291 }
292 for (i = 0; i < 6; ++i) {
293 if ((motionDataArray[i] != max) || (flag)) {
294 motionDataArray[i] = 0;
295 }
296 else if (motionDataArray[i] == max) {
297 flag = true;
298 }
299 }
300 }
301
302 for (i = 0; i < 6; ++i) {
303 if (motionDataArray[i] != 0) {
304 if (!enabled[i]) {
305 motionDataArray[i] = 0;
306 }
307 else {
308 if (reversed[i]) {
309 motionDataArray[i] = -motionDataArray[i];
310 }
311 motionDataArray[i] = (int)((float)(motionDataArray[i]) * sensitivity[i]
312 * generalSensitivity);
313 }
314 }
315 }
316 }
317
318 #include "moc_GuiApplicationNativeEventAware.cpp"