Debian Codegraph @

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