Debian Codegraph @

src/Gui/Navigation/TinkerCADNavigationStyle.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::TinkerCADNavigationStyle */
38
39 TYPESYSTEM_SOURCE(Gui::TinkerCADNavigationStyle, Gui::UserNavigationStyle)
40
41 TinkerCADNavigationStyle::TinkerCADNavigationStyle() = default;
42
43 TinkerCADNavigationStyle::~TinkerCADNavigationStyle() = default;
44
45 const char* TinkerCADNavigationStyle::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 right mouse button");
54 case NavigationStyle::ZOOMING:
55 return QT_TR_NOOP("Scroll mouse wheel");
56 default:
57 return "No description";
58 }
59 }
60
61 SbBool TinkerCADNavigationStyle::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->button1down = press;
119 if (press && (curmode == NavigationStyle::SEEK_WAIT_MODE)) {
120 newmode = NavigationStyle::SEEK_MODE;
121 this->seekToPoint(pos); // implicitly calls interactiveCountInc()
122 processed = true;
123 }
124 else if (viewer->isEditing() && (curmode == NavigationStyle::SPINNING)) {
125 processed = true;
126 }
127 else {
128 processed = processClickEvent(event);
129 }
130 break;
131 case SoMouseButtonEvent::BUTTON2:
132 // If we are in edit mode then simply ignore the RMB events
133 // to pass the event to the base class.
134 this->button2down = press;
135 if (press) {
136 mouseDownConsumedEvent = *event;
137 mouseDownConsumedEvent.setTime(ev->getTime());
138 }
139
140 // About to start rotating
141 if (press && (curmode == NavigationStyle::IDLE)) {
142 // Use this variable to spot move events
143 saveCursorPosition(ev);
144 this->centerTime = ev->getTime();
145 processed = true;
146 }
147 // Don't show the context menu after dragging, panning or zooming
148 else if (!press && (hasDragged || hasPanned || hasZoomed)) {
149 processed = true;
150 }
151 else if (!press) {
152 newmode = NavigationStyle::IDLE;
153 if (!viewer->isEditing()) {
154 if (this->currentmode != NavigationStyle::ZOOMING
155 && this->currentmode != NavigationStyle::PANNING) {
156 if (this->isPopupMenuEnabled()) {
157 this->openPopupMenu(event->getPosition());
158 }
159 }
160 processed = true;
161 }
162 }
163 break;
164 case SoMouseButtonEvent::BUTTON3:
165 this->button3down = press;
166 if (press) {
167 this->centerTime = ev->getTime();
168 setupPanningPlane(getCamera());
169 }
170 else if (curmode == NavigationStyle::PANNING) {
171 newmode = NavigationStyle::IDLE;
172 processed = true;
173 }
174 break;
175 default:
176 break;
177 }
178 }
179
180 // Mouse Movement handling
181 if (type.isDerivedFrom(SoLocation2Event::getClassTypeId())) {
182 const auto event = (const SoLocation2Event*)ev;
183 if (curmode == NavigationStyle::PANNING) {
184 float ratio = vp.getViewportAspectRatio();
185 panCamera(
186 viewer->getSoRenderManager()->getCamera(),
187 ratio,
188 this->panningplane,
189 posn,
190 prevnormalized
191 );
192 processed = true;
193 }
194 else if (curmode == NavigationStyle::DRAGGING) {
195 this->addToLog(event->getPosition(), event->getTime());
196 this->spin(posn);
197 moveCursorPosition();
198 processed = true;
199 }
200 }
201
202 // Spaceball & Joystick handling
203 if (type.isDerivedFrom(SoMotion3Event::getClassTypeId())) {
204 const auto event = static_cast<const SoMotion3Event*>(ev);
205 if (event) {
206 this->processMotionEvent(event);
207 }
208 processed = true;
209 }
210
211 enum
212 {
213 BUTTON1DOWN = 1 << 0,
214 BUTTON3DOWN = 1 << 1,
215 CTRLDOWN = 1 << 2,
216 SHIFTDOWN = 1 << 3,
217 BUTTON2DOWN = 1 << 4
218 };
219 unsigned int combo = (this->button1down ? BUTTON1DOWN : 0)
220 | (this->button2down ? BUTTON2DOWN : 0) | (this->button3down ? BUTTON3DOWN : 0)
221 | (this->ctrldown ? CTRLDOWN : 0) | (this->shiftdown ? SHIFTDOWN : 0);
222
223 switch (combo) {
224 case 0:
225 if (curmode == NavigationStyle::SPINNING) {
226 break;
227 }
228 newmode = NavigationStyle::IDLE;
229 break;
230 case BUTTON1DOWN:
231 newmode = NavigationStyle::SELECTION;
232 break;
233 case BUTTON2DOWN:
234 if (newmode != NavigationStyle::DRAGGING) {
235 saveCursorPosition(ev);
236 }
237 newmode = NavigationStyle::DRAGGING;
238 break;
239 case BUTTON3DOWN:
240 newmode = NavigationStyle::PANNING;
241 break;
242 default:
243 break;
244 }
245
246 // Process when selection button is pressed together with other buttons that could trigger
247 // different actions.
248 if (this->button1down && (this->button2down || this->button3down)) {
249 processed = true;
250 }
251
252 // Prevent interrupting rubber-band selection in sketcher
253 if (viewer->isEditing() && curmode == NavigationStyle::SELECTION
254 && newmode != NavigationStyle::IDLE) {
255 newmode = NavigationStyle::SELECTION;
256 processed = false;
257 }
258
259 // Reset flags when newmode is IDLE and the buttons are released
260 if (newmode == IDLE && !button1down && !button2down && !button3down) {
261 hasPanned = false;
262 hasDragged = false;
263 hasZoomed = false;
264 }
265
266 if (newmode != curmode) {
267 this->setViewingMode(newmode);
268 }
269
270 // If not handled in this class, pass on upwards in the inheritance
271 // hierarchy.
272 if (!processed) {
273 processed = inherited::processSoEvent(ev);
274 }
275 return processed;
276 }