Debian Codegraph @

src/Gui/Navigation/InventorNavigationStyle.cpp

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