Debian Codegraph @

src/Gui/Navigation/OpenSCADNavigationStyle.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /***************************************************************************
3 * Copyright (c) 2021 Werner Mayer <wmayer[at]users.sourceforge.net> *
4 * *
5 * This file is part of the FreeCAD CAx development system. *
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 *
15 * GNU Library General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Library General Public *
18 * License along with this library; see the file COPYING.LIB. If not, *
19 * write to the Free Software Foundation, Inc., 59 Temple Place, *
20 * Suite 330, Boston, MA 02111-1307, USA *
21 * *
22 ***************************************************************************/
23
24
25 #include <Inventor/nodes/SoCamera.h>
26 #include <QApplication>
27
28
29 #include "Navigation/NavigationStyle.h"
30 #include "View3DInventorViewer.h"
31
32
33 using namespace Gui;
34
35 // ----------------------------------------------------------------------------------
36
37 /* TRANSLATOR Gui::OpenSCADNavigationStyle */
38
39 TYPESYSTEM_SOURCE(Gui::OpenSCADNavigationStyle, Gui::UserNavigationStyle)
40
41 OpenSCADNavigationStyle::OpenSCADNavigationStyle() = default;
42
43 OpenSCADNavigationStyle::~OpenSCADNavigationStyle() = default;
44
45 const char* OpenSCADNavigationStyle::mouseButtons(ViewerMode mode)
46 {
47 switch (mode) {
48 case NavigationStyle::SELECTION:
49 return QT_TR_NOOP("Press left mouse button");
50 case NavigationStyle::PANNING:
51 return QT_TR_NOOP("Press right mouse button and move mouse");
52 case NavigationStyle::DRAGGING:
53 return QT_TR_NOOP("Press left mouse button and move mouse");
54 case NavigationStyle::ZOOMING:
55 return QT_TR_NOOP("Press middle mouse button or SHIFT and right mouse button");
56 default:
57 return "No description";
58 }
59 }
60
61 SbBool OpenSCADNavigationStyle::processSoEvent(const SoEvent* const ev)
62 {
63 // Events when in "ready-to-seek" mode are ignored, except those
64 // which influence the seek mode itself -- these are handled further
65 // up the inheritance hierarchy.
66 if (this->isSeekMode()) {
67 return inherited::processSoEvent(ev);
68 }
69 // Switch off viewing mode
70 if (!this->isSeekMode() && !this->isAnimating() && this->isViewing()) {
71 this->setViewing(false); // by default disable viewing mode to render the scene
72 }
73
74 const SoType type(ev->getTypeId());
75
76 const SbViewportRegion& vp = viewer->getSoRenderManager()->getViewportRegion();
77 const SbVec2s pos(ev->getPosition());
78 const SbVec2f posn = normalizePixelPos(pos);
79
80 const SbVec2f prevnormalized = this->lastmouseposition;
81 this->lastmouseposition = posn;
82
83 // Set to true if any event processing happened. Note that it is not
84 // necessary to restrict ourselves to only do one "action" for an
85 // event, we only need this flag to see if any processing happened
86 // at all.
87 SbBool processed = false;
88
89 const ViewerMode curmode = this->currentmode;
90 ViewerMode newmode = curmode;
91
92 // Mismatches in state of the modifier keys happens if the user
93 // presses or releases them outside the viewer window.
94 syncModifierKeys(ev);
95
96 // give the nodes in the foreground root the chance to handle events (e.g color bar)
97 if (!viewer->isEditing()) {
98 processed = handleEventInForeground(ev);
99 if (processed) {
100 return true;
101 }
102 }
103
104 // Keyboard handling
105 if (type.isDerivedFrom(SoKeyboardEvent::getClassTypeId())) {
106 const auto event = static_cast<const SoKeyboardEvent*>(ev);
107 processed = processKeyboardEvent(event);
108 }
109
110 // Mouse Button / Spaceball Button handling
111 if (type.isDerivedFrom(SoMouseButtonEvent::getClassTypeId())) {
112 const auto event = (const SoMouseButtonEvent*)ev;
113 const int button = event->getButton();
114 const SbBool press = event->getState() == SoButtonEvent::DOWN ? true : false;
115
116 switch (button) {
117 case SoMouseButtonEvent::BUTTON1:
118 this->lockrecenter = true;
119 this->button1down = press;
120 if (press && (curmode == NavigationStyle::SEEK_WAIT_MODE)) {
121 newmode = NavigationStyle::SEEK_MODE;
122 this->seekToPoint(pos); // implicitly calls interactiveCountInc()
123 processed = true;
124 }
125 else if (!press && (curmode == NavigationStyle::ZOOMING)) {
126 newmode = NavigationStyle::IDLE;
127 processed = true;
128 }
129 else if (!press && (curmode == NavigationStyle::DRAGGING)) {
130 this->setViewing(false);
131 processed = true;
132 }
133 else if (viewer->isEditing() && (curmode == NavigationStyle::SPINNING)) {
134 processed = true;
135 }
136 else {
137 processed = processClickEvent(event);
138 }
139 break;
140 case SoMouseButtonEvent::BUTTON2:
141 // If we are in edit mode then simply ignore the RMB events
142 // to pass the event to the base class.
143 this->lockrecenter = true;
144 this->button2down = press;
145
146 // Don't show the context menu after dragging, panning or zooming
147 if (!press && (hasDragged || hasPanned || hasZoomed)) {
148 processed = true;
149 }
150 else if (!press && !viewer->isEditing()) {
151 if (this->currentmode != NavigationStyle::ZOOMING
152 && this->currentmode != NavigationStyle::DRAGGING) {
153 if (this->isPopupMenuEnabled()) {
154 this->openPopupMenu(event->getPosition());
155 }
156 }
157 }
158 // Alternative way of rotating & zooming
159 if (press
160 && (curmode == NavigationStyle::PANNING || curmode == NavigationStyle::ZOOMING)) {
161 newmode = NavigationStyle::DRAGGING;
162 saveCursorPosition(ev);
163 this->centerTime = ev->getTime();
164 processed = true;
165 }
166 else if (!press && (curmode == NavigationStyle::DRAGGING)) {
167 newmode = NavigationStyle::IDLE;
168 processed = true;
169 }
170 break;
171 case SoMouseButtonEvent::BUTTON3:
172 this->button3down = press;
173 if (press) {
174 this->centerTime = ev->getTime();
175 setupPanningPlane(getCamera());
176 this->lockrecenter = false;
177 }
178 else if (curmode == NavigationStyle::PANNING) {
179 newmode = NavigationStyle::IDLE;
180 processed = true;
181 }
182 break;
183 default:
184 break;
185 }
186 }
187
188 // Mouse Movement handling
189 if (type.isDerivedFrom(SoLocation2Event::getClassTypeId())) {
190 this->lockrecenter = true;
191 const auto event = (const SoLocation2Event*)ev;
192 if (!viewer->isEditing() && curmode == NavigationStyle::SELECTION) {
193 if (button1down && isDraggerUnderCursor(ev->getPosition())) {
194 newmode = NavigationStyle::INTERACT;
195 }
196 else {
197 newmode = NavigationStyle::DRAGGING;
198 saveCursorPosition(ev);
199 this->centerTime = ev->getTime();
200 }
201 }
202 else if (curmode == NavigationStyle::ZOOMING) {
203 // OpenSCAD uses vertical mouse position, not horizontal
204 // this->zoomByCursor(posn, prevnormalized);
205 float value = (posn[1] - prevnormalized[1]) * 10.0f;
206 if (this->invertZoom) {
207 value = -value;
208 }
209 zoom(viewer->getSoRenderManager()->getCamera(), value);
210 processed = true;
211 }
212 else if (curmode == NavigationStyle::PANNING) {
213 float ratio = vp.getViewportAspectRatio();
214 panCamera(
215 viewer->getSoRenderManager()->getCamera(),
216 ratio,
217 this->panningplane,
218 posn,
219 prevnormalized
220 );
221 processed = true;
222 }
223 else if (curmode == NavigationStyle::DRAGGING) {
224 this->addToLog(event->getPosition(), event->getTime());
225 this->spin(posn);
226 moveCursorPosition();
227 processed = true;
228 }
229 }
230
231 // Spaceball & Joystick handling
232 if (type.isDerivedFrom(SoMotion3Event::getClassTypeId())) {
233 const auto event = static_cast<const SoMotion3Event*>(ev);
234 if (event) {
235 this->processMotionEvent(event);
236 }
237 processed = true;
238 }
239
240 enum
241 {
242 BUTTON1DOWN = 1 << 0,
243 BUTTON3DOWN = 1 << 1,
244 CTRLDOWN = 1 << 2,
245 SHIFTDOWN = 1 << 3,
246 BUTTON2DOWN = 1 << 4
247 };
248 unsigned int combo = (this->button1down ? BUTTON1DOWN : 0)
249 | (this->button2down ? BUTTON2DOWN : 0) | (this->button3down ? BUTTON3DOWN : 0)
250 | (this->ctrldown ? CTRLDOWN : 0) | (this->shiftdown ? SHIFTDOWN : 0);
251
252 switch (combo) {
253 case 0:
254 if (curmode == NavigationStyle::SPINNING) {
255 break;
256 }
257 newmode = NavigationStyle::IDLE;
258 break;
259 case BUTTON1DOWN:
260 if (newmode != NavigationStyle::DRAGGING && newmode != NavigationStyle::INTERACT) {
261 newmode = NavigationStyle::SELECTION;
262 }
263 break;
264 case BUTTON2DOWN:
265 newmode = NavigationStyle::PANNING;
266 break;
267 case BUTTON3DOWN:
268 case SHIFTDOWN | BUTTON2DOWN:
269 case SHIFTDOWN | BUTTON3DOWN:
270 newmode = NavigationStyle::ZOOMING;
271 break;
272 default:
273 break;
274 }
275
276 // Process when selection button is pressed together with other buttons that could trigger
277 // different actions.
278 if (this->button1down && (this->button2down || this->button3down)) {
279 processed = true;
280 }
281
282 // Prevent interrupting rubber-band selection in sketcher
283 if (viewer->isEditing() && curmode == NavigationStyle::SELECTION
284 && newmode != NavigationStyle::IDLE) {
285 newmode = NavigationStyle::SELECTION;
286 processed = false;
287 }
288
289 // Reset flags when newmode is IDLE and the buttons are released
290 if (newmode == IDLE && !button1down && !button2down && !button3down) {
291 hasPanned = false;
292 hasDragged = false;
293 hasZoomed = false;
294 }
295
296 if (newmode != curmode) {
297 this->setViewingMode(newmode);
298 }
299
300 // If not handled in this class, pass on upwards in the inheritance
301 // hierarchy.
302 if (!processed) {
303 processed = inherited::processSoEvent(ev);
304 }
305 return processed;
306 }