Debian Codegraph @

src/Gui/Navigation/OpenCascadeNavigationStyle.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /***************************************************************************
3 * Copyright (c) 2015 Kirill Gavrilov <[email protected]> *
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::OpenCascadeNavigationStyle */
38
39 TYPESYSTEM_SOURCE(Gui::OpenCascadeNavigationStyle, Gui::UserNavigationStyle)
40
41 OpenCascadeNavigationStyle::OpenCascadeNavigationStyle() = default;
42
43 OpenCascadeNavigationStyle::~OpenCascadeNavigationStyle() = default;
44
45 const char* OpenCascadeNavigationStyle::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 Ctrl and middle mouse button");
52 case NavigationStyle::DRAGGING:
53 return QT_TR_NOOP("Press Ctrl and right mouse button");
54 case NavigationStyle::ZOOMING:
55 return QT_TR_NOOP("Press Ctrl and left mouse button");
56 default:
57 return "No description";
58 }
59 }
60
61 SbBool OpenCascadeNavigationStyle::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 auto const 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 && (this->currentmode == NavigationStyle::SEEK_WAIT_MODE)) {
121 newmode = NavigationStyle::SEEK_MODE;
122 this->seekToPoint(pos); // implicitly calls interactiveCountInc()
123 processed = true;
124 }
125 else if (!press && (this->currentmode == NavigationStyle::ZOOMING)) {
126 newmode = NavigationStyle::IDLE;
127 processed = true;
128 }
129 else if (!press && (this->currentmode == NavigationStyle::DRAGGING)) {
130 this->setViewing(false);
131 processed = true;
132 }
133 else if (viewer->isEditing() && (this->currentmode == 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
145 // Don't show the context menu after dragging, panning or zooming
146 if (!press && (hasDragged || hasPanned || hasZoomed)) {
147 processed = true;
148 }
149 else if (!press && !viewer->isEditing()) {
150 if (this->currentmode != NavigationStyle::ZOOMING
151 && this->currentmode != NavigationStyle::PANNING
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 && (this->currentmode == NavigationStyle::PANNING
161 || this->currentmode == NavigationStyle::ZOOMING)) {
162 newmode = NavigationStyle::DRAGGING;
163 saveCursorPosition(ev);
164 this->centerTime = ev->getTime();
165 processed = true;
166 }
167 else if (!press && (this->currentmode == NavigationStyle::DRAGGING)) {
168 newmode = NavigationStyle::IDLE;
169 processed = true;
170 }
171 this->button2down = press;
172 break;
173 case SoMouseButtonEvent::BUTTON3:
174 if (press) {
175 this->centerTime = ev->getTime();
176 setupPanningPlane(getCamera());
177 this->lockrecenter = false;
178 }
179 else if (this->currentmode == NavigationStyle::PANNING) {
180 newmode = NavigationStyle::IDLE;
181 processed = true;
182 }
183 this->button3down = press;
184 break;
185 default:
186 break;
187 }
188 }
189
190 enum
191 {
192 BUTTON1DOWN = 1 << 0,
193 BUTTON3DOWN = 1 << 1,
194 CTRLDOWN = 1 << 2,
195 SHIFTDOWN = 1 << 3,
196 BUTTON2DOWN = 1 << 4
197 };
198 unsigned int combo = (this->button1down ? BUTTON1DOWN : 0)
199 | (this->button2down ? BUTTON2DOWN : 0) | (this->button3down ? BUTTON3DOWN : 0)
200 | (this->ctrldown ? CTRLDOWN : 0) | (this->shiftdown ? SHIFTDOWN : 0);
201
202 // Mouse Movement handling
203 if (type.isDerivedFrom(SoLocation2Event::getClassTypeId())) {
204 this->lockrecenter = true;
205 const auto event = (const SoLocation2Event*)ev;
206 if (this->currentmode == NavigationStyle::ZOOMING) {
207 // OCCT uses horizontal mouse position, not vertical
208 // this->zoomByCursor(posn, prevnormalized);
209 float value = (posn[0] - prevnormalized[0]) * 10.0f;
210 if (this->invertZoom) {
211 value = -value;
212 }
213 zoom(viewer->getSoRenderManager()->getCamera(), value);
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 else if (combo == (CTRLDOWN | BUTTON1DOWN)) {
234 newmode = NavigationStyle::ZOOMING;
235 }
236 }
237
238 // Spaceball & Joystick handling
239 if (type.isDerivedFrom(SoMotion3Event::getClassTypeId())) {
240 const auto event = static_cast<const SoMotion3Event*>(ev);
241 if (event) {
242 this->processMotionEvent(event);
243 }
244 processed = true;
245 }
246
247 switch (combo) {
248 case 0:
249 if (curmode == NavigationStyle::SPINNING) {
250 break;
251 }
252 newmode = NavigationStyle::IDLE;
253 break;
254 case CTRLDOWN | BUTTON1DOWN:
255 case BUTTON1DOWN:
256 if (newmode != NavigationStyle::ZOOMING) {
257 newmode = NavigationStyle::SELECTION;
258 }
259 break;
260 case CTRLDOWN | BUTTON3DOWN:
261 case BUTTON3DOWN:
262 newmode = NavigationStyle::PANNING;
263 break;
264 case CTRLDOWN | BUTTON2DOWN:
265 if (newmode != NavigationStyle::DRAGGING) {
266 saveCursorPosition(ev);
267 }
268 newmode = NavigationStyle::DRAGGING;
269 break;
270 case BUTTON2DOWN:
271 newmode = NavigationStyle::IDLE;
272 break;
273 default:
274 break;
275 }
276
277 // Process when selection button is pressed together with other buttons that could trigger
278 // different actions.
279 if (this->button1down && (this->button2down || this->button3down || this->ctrldown)) {
280 processed = true;
281 }
282
283 // Prevent interrupting rubber-band selection in sketcher
284 if (viewer->isEditing() && curmode == NavigationStyle::SELECTION
285 && newmode != NavigationStyle::IDLE) {
286 newmode = NavigationStyle::SELECTION;
287 processed = false;
288 }
289
290 // Reset flags when newmode is IDLE and the buttons are released
291 if (newmode == IDLE && !button1down && !button2down && !button3down) {
292 hasPanned = false;
293 hasDragged = false;
294 hasZoomed = false;
295 }
296
297 if (newmode != curmode) {
298 this->setViewingMode(newmode);
299 }
300
301 // If not handled in this class, pass on upwards in the inheritance
302 // hierarchy.
303 if (!processed) {
304 processed = inherited::processSoEvent(ev);
305 }
306
307 return processed;
308 }