Debian Codegraph @

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