Debian Codegraph @

src/Gui/Navigation/TouchpadNavigationStyle.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /***************************************************************************
3 * Copyright (c) 2012 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 <QApplication>
26
27
28 #include "Navigation/NavigationStyle.h"
29 #include "View3DInventorViewer.h"
30
31
32 using namespace Gui;
33
34 // ----------------------------------------------------------------------------------
35
36 /* TRANSLATOR Gui::TouchpadNavigationStyle */
37
38 TYPESYSTEM_SOURCE(Gui::TouchpadNavigationStyle, Gui::UserNavigationStyle)
39
40 TouchpadNavigationStyle::TouchpadNavigationStyle() = default;
41
42 TouchpadNavigationStyle::~TouchpadNavigationStyle() = default;
43
44 const char* TouchpadNavigationStyle::mouseButtons(ViewerMode mode)
45 {
46 switch (mode) {
47 case NavigationStyle::SELECTION:
48 return QT_TR_NOOP("Press left mouse button");
49 case NavigationStyle::PANNING:
50 return QT_TR_NOOP("Press Shift button");
51 case NavigationStyle::DRAGGING:
52 return QT_TR_NOOP("Press Alt button");
53 case NavigationStyle::ZOOMING:
54 return QT_TR_NOOP("Press Ctrl and Shift buttons");
55 default:
56 return "No description";
57 }
58 }
59
60 SbBool TouchpadNavigationStyle::processSoEvent(const SoEvent* const ev)
61 {
62 // Events when in "ready-to-seek" mode are ignored, except those
63 // which influence the seek mode itself -- these are handled further
64 // up the inheritance hierarchy.
65 if (this->isSeekMode()) {
66 return inherited::processSoEvent(ev);
67 }
68 // Switch off viewing mode (Bug #0000911)
69 if (!this->isSeekMode() && !this->isAnimating() && this->isViewing()) {
70 this->setViewing(false); // by default disable viewing mode to render the scene
71 }
72
73 const SoType type(ev->getTypeId());
74
75 const SbViewportRegion& vp = viewer->getSoRenderManager()->getViewportRegion();
76 const SbVec2s pos(ev->getPosition());
77 const SbVec2f posn = normalizePixelPos(pos);
78
79 const SbVec2f prevnormalized = this->lastmouseposition;
80 this->lastmouseposition = posn;
81
82 // Set to true if any event processing happened. Note that it is not
83 // necessary to restrict ourselves to only do one "action" for an
84 // event, we only need this flag to see if any processing happened
85 // at all.
86 SbBool processed = false;
87
88 const ViewerMode curmode = this->currentmode;
89 ViewerMode newmode = curmode;
90
91 // Mismatches in state of the modifier keys happens if the user
92 // presses or releases them outside the viewer window.
93 syncModifierKeys(ev);
94
95 // give the nodes in the foreground root the chance to handle events (e.g color bar)
96 if (!viewer->isEditing()) {
97 processed = handleEventInForeground(ev);
98 if (processed) {
99 return true;
100 }
101 }
102
103 // Keyboard handling
104 if (type.isDerivedFrom(SoKeyboardEvent::getClassTypeId())) {
105 const auto event = static_cast<const SoKeyboardEvent*>(ev);
106 processed = processKeyboardEvent(event);
107 }
108
109 // Mouse Button / Spaceball Button handling
110 if (type.isDerivedFrom(SoMouseButtonEvent::getClassTypeId())) {
111 const auto event = (const SoMouseButtonEvent*)ev;
112 const int button = event->getButton();
113 const SbBool press = event->getState() == SoButtonEvent::DOWN ? true : false;
114
115 // SoDebugError::postInfo("processSoEvent", "button = %d", button);
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
126 && (this->currentmode == NavigationStyle::PANNING
127 || this->currentmode == NavigationStyle::ZOOMING)) {
128 newmode = NavigationStyle::DRAGGING;
129 saveCursorPosition(ev);
130 this->centerTime = ev->getTime();
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 this->button2down = press;
168 break;
169 default:
170 break;
171 }
172 }
173
174 // Mouse Movement handling
175 if (type.isDerivedFrom(SoLocation2Event::getClassTypeId())) {
176 this->lockrecenter = true;
177 const auto event = (const SoLocation2Event*)ev;
178 if (this->currentmode == NavigationStyle::ZOOMING) {
179 this->zoomByCursor(posn, prevnormalized);
180 processed = true;
181 }
182 else if (this->currentmode == NavigationStyle::PANNING) {
183 if (!blockPan) {
184 float ratio = vp.getViewportAspectRatio();
185 panCamera(
186 viewer->getSoRenderManager()->getCamera(),
187 ratio,
188 this->panningplane,
189 posn,
190 prevnormalized
191 );
192 }
193 blockPan = false;
194 processed = true;
195 }
196 else if (this->currentmode == NavigationStyle::DRAGGING) {
197 this->addToLog(event->getPosition(), event->getTime());
198 this->spin(posn);
199 moveCursorPosition();
200 processed = true;
201 }
202 }
203
204 // Spaceball & Joystick handling
205 if (type.isDerivedFrom(SoMotion3Event::getClassTypeId())) {
206 const auto event = static_cast<const SoMotion3Event*>(ev);
207 if (event) {
208 this->processMotionEvent(event);
209 }
210 processed = true;
211 }
212
213 enum
214 {
215 BUTTON1DOWN = 1 << 0,
216 BUTTON2DOWN = 1 << 1,
217 CTRLDOWN = 1 << 2,
218 SHIFTDOWN = 1 << 3,
219 ALTDOWN = 1 << 4
220 };
221 unsigned int combo = (this->button1down ? BUTTON1DOWN : 0)
222 | (this->button2down ? BUTTON2DOWN : 0) | (this->ctrldown ? CTRLDOWN : 0)
223 | (this->shiftdown ? SHIFTDOWN : 0) | (this->altdown ? ALTDOWN : 0);
224
225 switch (combo) {
226 case 0:
227 if (curmode == NavigationStyle::SPINNING) {
228 break;
229 }
230 newmode = NavigationStyle::IDLE;
231 break;
232 case BUTTON1DOWN:
233 // make sure not to change the selection when stopping spinning
234 if (curmode == NavigationStyle::SPINNING) {
235 newmode = NavigationStyle::IDLE;
236 }
237 else {
238 newmode = NavigationStyle::SELECTION;
239 }
240 break;
241 case CTRLDOWN:
242 newmode = NavigationStyle::IDLE;
243 break;
244 case SHIFTDOWN:
245 // Shift + left mouse click enables dragging.
246 // If the mouse is released the event should not be forwarded to the base
247 // class that eventually performs a selection.
248 if (newmode == NavigationStyle::DRAGGING) {
249 processed = true;
250 }
251 newmode = NavigationStyle::PANNING;
252
253 if (currentmode != NavigationStyle::PANNING) {
254 blockPan = true;
255 }
256 break;
257 case ALTDOWN:
258 if (newmode != NavigationStyle::DRAGGING) {
259 saveCursorPosition(ev);
260 }
261 newmode = NavigationStyle::DRAGGING;
262 break;
263 case CTRLDOWN | SHIFTDOWN:
264 case CTRLDOWN | SHIFTDOWN | BUTTON1DOWN:
265 // Left mouse button doesn't change the zoom
266 // behaviour but when pressing or releasing it then do not forward the
267 // event to the base class that eventually performs a selection.
268 if (newmode == NavigationStyle::ZOOMING) {
269 processed = true;
270 }
271 newmode = NavigationStyle::ZOOMING;
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->altdown)) {
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 return processed;
307 }