Debian Codegraph @

src/Gui/Navigation/NavigationStyle.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /***************************************************************************
3 * Copyright (c) 2008 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 #ifndef GUI_NAVIGATIONSTYLE_H
26 #define GUI_NAVIGATIONSTYLE_H
27
28 #include <Inventor/C/basic.h>
29 #include <Inventor/SbBox2s.h>
30 #include <Inventor/SbPlane.h>
31 #include <Inventor/SbRotation.h>
32 #include <Inventor/SbSphere.h>
33 #include <Inventor/SbTime.h>
34 #include <Inventor/SbVec2f.h>
35 #include <Inventor/SbVec2s.h>
36 #include <Inventor/SbVec3f.h>
37 #include <Inventor/events/SoEvents.h>
38
39 #include <QEvent>
40 #include <QAction>
41 #include <Base/BaseClass.h>
42 #include <Base/SmartPtrPy.h>
43 #include <Gui/Namespace.h>
44 #include <FCGlobal.h>
45 #include <memory>
46 #include <optional>
47
48 // forward declarations
49 class SoEvent;
50 class SoMouseWheelEvent;
51 class SoMotion3Event;
52 class SoQtViewer;
53 class SoCamera;
54 class SoSensor;
55 class SbSphereSheetProjector;
56
57 // NOLINTBEGIN(cppcoreguidelines-avoid*, readability-avoid-const-params-in-decls)
58 namespace Gui
59 {
60
61 class View3DInventorViewer;
62 class NavigationAnimator;
63 class AbstractMouseSelection;
64 class NavigationAnimation;
65
66 /**
67 * @author Werner Mayer
68 */
69 class GuiExport NavigationStyleEvent: public QEvent
70 {
71 public:
72 explicit NavigationStyleEvent(const Base::Type& s);
73 ~NavigationStyleEvent() override;
74 const Base::Type& style() const;
75
76 private:
77 Base::Type t;
78 };
79
80 /**
81 * The navigation style base class
82 * @author Werner Mayer
83 */
84 class GuiExport NavigationStyle: public Base::BaseClass
85 {
86 TYPESYSTEM_HEADER_WITH_OVERRIDE();
87
88 public:
89 enum ViewerMode
90 {
91 IDLE,
92 INTERACT,
93 ZOOMING,
94 BOXZOOM,
95 PANNING,
96 DRAGGING,
97 SPINNING,
98 SEEK_WAIT_MODE,
99 SEEK_MODE,
100 SELECTION
101 };
102
103 enum SelectionMode
104 {
105 Lasso = 0, /**< Select objects using a lasso. */
106 Rectangle = 1, /**< Select objects using a rectangle. */
107 Rubberband = 2, /**< Select objects using a rubberband. */
108 BoxZoom = 3, /**< Perform a box zoom. */
109 Clip = 4, /**< Clip objects using a lasso. */
110 };
111
112 enum class ClarifySelectionMode
113 {
114 Default, /**< Long press with LMB to trigger clarify selection */
115 Ctrl /**< Long press with Ctrl+LMB to trigger clarify selection */
116 };
117
118 enum OrbitStyle
119 {
120 Turntable,
121 Trackball,
122 FreeTurntable,
123 TrackballClassic,
124 RoundedArcball
125 };
126
127 enum class RotationCenterMode
128 {
129 WindowCenter = 0, /**< The center of the window */
130 ScenePointAtCursor = 1, /**< Find the point in the scene at the cursor position. If there is
131 no point then the focal plane is used */
132 FocalPointAtCursor = 2, /**< Find the point on the focal plane at the cursor position. */
133 BoundingBoxCenter = 4 /**< Find the center point of the bounding box of the scene. */
134 };
135 Q_DECLARE_FLAGS(RotationCenterModes, RotationCenterMode)
136
137 public:
138 NavigationStyle();
139 ~NavigationStyle() override;
140 NavigationStyle(const NavigationStyle&) = delete;
141
142 NavigationStyle& operator=(const NavigationStyle& ns);
143 void setViewer(View3DInventorViewer*);
144
145 void setAnimationEnabled(const SbBool enable);
146 void setSpinningAnimationEnabled(const SbBool enable);
147 SbBool isAnimationEnabled() const;
148 SbBool isSpinningAnimationEnabled() const;
149 SbBool isAnimating() const;
150 SbBool isSpinning() const;
151 void startAnimating(const std::shared_ptr<NavigationAnimation>& animation, bool wait = false) const;
152 void stopAnimating() const;
153
154 void setSensitivity(float);
155 float getSensitivity() const;
156
157 void setResetCursorPosition(SbBool);
158 SbBool isResetCursorPosition() const;
159
160 void setZoomInverted(SbBool);
161 SbBool isZoomInverted() const;
162 void setZoomStep(float);
163 void setZoomAtCursor(SbBool);
164 SbBool isZoomAtCursor() const;
165 void zoomIn();
166 void zoomOut();
167 void setRotationCenterMode(RotationCenterModes);
168 RotationCenterModes getRotationCenterMode() const;
169 void setRotationCenter(const SbVec3f& cnt);
170 SbVec3f getFocalPoint() const;
171
172 SoCamera* getCamera() const;
173 std::shared_ptr<NavigationAnimation> setCameraOrientation(
174 const SbRotation& orientation,
175 SbBool moveToCenter = false
176 ) const;
177 std::shared_ptr<NavigationAnimation> translateCamera(const SbVec3f& translation) const;
178
179 #if (COIN_MAJOR_VERSION * 100 + COIN_MINOR_VERSION * 10 + COIN_MICRO_VERSION < 403)
180 void findBoundingSphere();
181 #endif
182
183 void reorientCamera(SoCamera* camera, const SbRotation& rotation);
184 void reorientCamera(SoCamera* camera, const SbRotation& rotation, const SbVec3f& rotationCenter);
185
186 void boxZoom(const SbBox2s& box);
187 // Scale the camera inplace
188 void scale(float factor);
189 virtual void viewAll();
190
191 void setViewingMode(const ViewerMode newmode);
192 int getViewingMode() const;
193 virtual SbBool processEvent(const SoEvent* const ev);
194 virtual SbBool processMotionEvent(const SoMotion3Event* const ev);
195 virtual SbBool processKeyboardEvent(const SoKeyboardEvent* const event);
196 virtual SbBool processClickEvent(const SoMouseButtonEvent* const event);
197 virtual SbBool processWheelEvent(const SoMouseWheelEvent* const event);
198
199 void setPopupMenuEnabled(const SbBool on);
200 SbBool isPopupMenuEnabled() const;
201
202 void startSelection(AbstractMouseSelection*);
203 void startSelection(SelectionMode = Lasso);
204 void abortSelection();
205 void stopSelection();
206 void resetButtonState();
207 SbBool isSelecting() const;
208 const std::vector<SbVec2s>& getPolygon(SelectionRole* role = nullptr) const;
209
210 bool isDraggerUnderCursor(const SbVec2s pos) const;
211
212 virtual ClarifySelectionMode clarifySelectionMode() const
213 {
214 return ClarifySelectionMode::Default;
215 }
216
217
218 void setOrbitStyle(OrbitStyle style);
219 OrbitStyle getOrbitStyle() const;
220
221 SbBool isViewing() const;
222 void setViewing(SbBool);
223
224 SbVec3f getRotationCenter(SbBool&) const;
225
226 std::optional<SbVec2s>& getRightClickPosition();
227
228 PyObject* getPyObject() override;
229
230 protected:
231 void initialize();
232 void finalize();
233
234 void interactiveCountInc();
235 void interactiveCountDec();
236 int getInteractiveCount() const;
237
238 SbBool isSeekMode() const;
239 void setSeekMode(SbBool enable);
240 SbBool seekToPoint(const SbVec2s screenpos);
241 void seekToPoint(const SbVec3f& scenepos);
242 void lookAtPoint(const SbVec2s screenpos);
243 void lookAtPoint(const SbVec3f& position);
244
245 void panCamera(
246 SoCamera* camera,
247 float vpaspect,
248 const SbPlane& panplane,
249 const SbVec2f& previous,
250 const SbVec2f& current
251 );
252 void setupPanningPlane(const SoCamera* camera);
253 int getDelta() const;
254 void zoom(SoCamera* camera, float diffvalue);
255 virtual void zoomByCursor(const SbVec2f& thispos, const SbVec2f& prevpos);
256 void doZoom(SoCamera* camera, int wheeldelta, const SbVec2f& pos);
257 void doZoom(SoCamera* camera, float logzoomfactor, const SbVec2f& pos);
258 void doScale(SoCamera* camera, float factor);
259 void doRotate(SoCamera* camera, float angle, const SbVec2f& pos);
260 void spin(const SbVec2f& pointerpos);
261 SbBool doSpin();
262 void spin_simplified(SbVec2f curpos, SbVec2f prevpos);
263 void moveCursorPosition();
264 void saveCursorPosition(const SoEvent* const ev);
265
266 SbVec2f normalizePixelPos(SbVec2s pixpos);
267 SbVec2f normalizePixelPos(SbVec2f pixpos);
268
269 SbBool handleEventInForeground(const SoEvent* const e);
270 virtual SbBool processSoEvent(const SoEvent* const ev);
271 void syncWithEvent(const SoEvent* const ev);
272 virtual void openPopupMenu(const SbVec2s& position);
273
274 private:
275 void spinInternal(const SbVec2f& pointerpos, const SbVec2f& lastpos);
276 void spinSimplifiedInternal(const SbVec2f curpos, const SbVec2f prevpos);
277 bool isNavigationStyleAction(QAction* action, QActionGroup* navMenuGroup) const;
278 QWidget* findView3DInventorWidget() const;
279 void applyNavigationStyleChange(QAction* selectedAction);
280
281 protected:
282 void clearLog();
283 void addToLog(const SbVec2s pos, const SbTime time);
284
285 void syncModifierKeys(const SoEvent* const ev);
286
287 protected:
288 struct
289 { // tracking mouse movement in a log
290 short size;
291 short historysize;
292 SbVec2s* position;
293 SbTime* time;
294 } log;
295
296 View3DInventorViewer* viewer {nullptr};
297 NavigationAnimator* animator;
298 SbBool animationEnabled;
299 ViewerMode currentmode;
300 SoMouseButtonEvent mouseDownConsumedEvent;
301 SbVec2f lastmouseposition;
302 SbVec2s globalPos;
303 SbVec2s localPos;
304 SbPlane panningplane;
305 SbTime centerTime;
306 SbBool lockrecenter;
307 SbBool menuenabled;
308 SbBool ctrldown, shiftdown, altdown;
309 SbBool button1down, button2down, button3down;
310 SbBool invertZoom;
311 SbBool zoomAtCursor;
312 float zoomStep;
313 SbBool hasDragged;
314 SbBool hasPanned;
315 SbBool hasZoomed;
316
317 /** @name Mouse model */
318 //@{
319 AbstractMouseSelection* mouseSelection {nullptr};
320 std::vector<SbVec2s> pcPolygon;
321 SelectionRole selectedRole;
322 //@}
323
324 /** @name Spinning data */
325 //@{
326 SbBool spinningAnimationEnabled;
327 int spinsamplecounter;
328 SbRotation spinincrement;
329 SbSphereSheetProjector* spinprojector;
330 //@}
331
332 Py::SmartPtr pythonObject;
333
334 // store the position where right-click occurred just before
335 // the menu popped up
336 std::optional<SbVec2s> rightClickPosition;
337
338 private:
339 friend class NavigationAnimator;
340
341 SbVec3f rotationCenter;
342 SbBool rotationCenterFound;
343 SbBool rotationCenterIsScenePointAtCursor;
344 NavigationStyle::RotationCenterModes rotationCenterMode;
345 float sensitivity;
346 SbBool resetcursorpos;
347
348 #if (COIN_MAJOR_VERSION * 100 + COIN_MINOR_VERSION * 10 + COIN_MICRO_VERSION < 403)
349 SbSphere boundingSphere;
350 #endif
351 };
352
353 /** Sub-classes of this class appear in the preference dialog where users can
354 * choose their favorite navigation style.
355 * All other classes that inherit directly from NavigationStyle do not appear
356 * in the above dialog.
357 * This mechanism is useful to implement special navigation styles which are
358 * only needed for certain purposes. Thus, it should not be possible to be
359 * choosable by the user
360 * @author Werner Mayer
361 */
362 class GuiExport UserNavigationStyle: public NavigationStyle
363 {
364 TYPESYSTEM_HEADER_WITH_OVERRIDE();
365
366 public:
367 UserNavigationStyle() = default;
368 ~UserNavigationStyle() override = default;
369 virtual const char* mouseButtons(ViewerMode) = 0;
370 virtual std::string userFriendlyName() const;
371 static std::map<Base::Type, std::string> getUserFriendlyNames();
372 };
373
374 class GuiExport InventorNavigationStyle: public UserNavigationStyle
375 {
376 using inherited = UserNavigationStyle;
377
378 TYPESYSTEM_HEADER_WITH_OVERRIDE();
379
380 public:
381 InventorNavigationStyle();
382 ~InventorNavigationStyle() override;
383 const char* mouseButtons(ViewerMode) override;
384 std::string userFriendlyName() const override;
385 ClarifySelectionMode clarifySelectionMode() const override
386 {
387 return ClarifySelectionMode::Ctrl;
388 }
389
390 protected:
391 SbBool processSoEvent(const SoEvent* const ev) override;
392 };
393
394 class GuiExport CADNavigationStyle: public UserNavigationStyle
395 {
396 using inherited = UserNavigationStyle;
397
398 TYPESYSTEM_HEADER_WITH_OVERRIDE();
399
400 public:
401 CADNavigationStyle();
402 ~CADNavigationStyle() override;
403 const char* mouseButtons(ViewerMode) override;
404
405 protected:
406 SbBool processSoEvent(const SoEvent* const ev) override;
407
408 private:
409 SbBool lockButton1 {false};
410 };
411
412 class GuiExport RevitNavigationStyle: public UserNavigationStyle
413 {
414 using inherited = UserNavigationStyle;
415
416 TYPESYSTEM_HEADER_WITH_OVERRIDE();
417
418 public:
419 RevitNavigationStyle();
420 ~RevitNavigationStyle() override;
421 const char* mouseButtons(ViewerMode) override;
422
423 protected:
424 SbBool processSoEvent(const SoEvent* const ev) override;
425
426 private:
427 SbBool lockButton1 {false};
428 };
429
430 class GuiExport BlenderNavigationStyle: public UserNavigationStyle
431 {
432 using inherited = UserNavigationStyle;
433
434 TYPESYSTEM_HEADER_WITH_OVERRIDE();
435
436 public:
437 BlenderNavigationStyle();
438 ~BlenderNavigationStyle() override;
439 const char* mouseButtons(ViewerMode) override;
440
441 protected:
442 SbBool processSoEvent(const SoEvent* const ev) override;
443
444 private:
445 SbBool lockButton1 {false};
446 };
447
448 class GuiExport SolidWorksNavigationStyle: public UserNavigationStyle
449 {
450 using inherited = UserNavigationStyle;
451
452 TYPESYSTEM_HEADER_WITH_OVERRIDE();
453
454 public:
455 SolidWorksNavigationStyle();
456 ~SolidWorksNavigationStyle() override;
457 const char* mouseButtons(ViewerMode) override;
458
459 protected:
460 SbBool processSoEvent(const SoEvent* const ev) override;
461
462 private:
463 SbBool lockButton1 {false};
464 };
465
466 class GuiExport MayaGestureNavigationStyle: public UserNavigationStyle
467 {
468 using inherited = UserNavigationStyle;
469
470 TYPESYSTEM_HEADER_WITH_OVERRIDE();
471
472 public:
473 MayaGestureNavigationStyle();
474 ~MayaGestureNavigationStyle() override;
475 const char* mouseButtons(ViewerMode) override;
476
477 protected:
478 void zoomByCursor(const SbVec2f& thispos, const SbVec2f& prevpos) override;
479
480 SbBool processSoEvent(const SoEvent* const ev) override;
481
482 SbVec2s mousedownPos; // the position where some mouse button was pressed (local pixel coordinates).
483 short mouseMoveThreshold; // setting. Minimum move required to consider it a move (in pixels).
484 bool mouseMoveThresholdBroken; // a flag that the move threshold was surpassed since last mousedown.
485 int mousedownConsumedCount; // a flag for remembering that a mousedown of button1/button2 was
486 // consumed.
487 SoMouseButtonEvent mousedownConsumedEvents[5]; // the event that was consumed and is to be
488 // refired. 2 should be enough, but just for a
489 // case of the maximum 5 buttons...
490 bool testMoveThreshold(const SbVec2s currentPos) const;
491
492 bool thisClickIsComplex; // a flag that becomes set when a complex clicking pattern is detected
493 // (i.e., two or more mouse buttons were down at the same time).
494 bool inGesture; // a flag that is used to filter out mouse events during gestures.
495 };
496
497 class GuiExport TouchpadNavigationStyle: public UserNavigationStyle
498 {
499 using inherited = UserNavigationStyle;
500
501 TYPESYSTEM_HEADER_WITH_OVERRIDE();
502
503 public:
504 TouchpadNavigationStyle();
505 ~TouchpadNavigationStyle() override;
506 const char* mouseButtons(ViewerMode) override;
507
508 protected:
509 SbBool processSoEvent(const SoEvent* const ev) override;
510
511 private:
512 SbBool blockPan {false}; // Used to block the first pan in a mouse movement to prevent big jumps
513 };
514
515 class GuiExport OpenCascadeNavigationStyle: public UserNavigationStyle
516 {
517 using inherited = UserNavigationStyle;
518
519 TYPESYSTEM_HEADER_WITH_OVERRIDE();
520
521 public:
522 OpenCascadeNavigationStyle();
523 ~OpenCascadeNavigationStyle() override;
524 const char* mouseButtons(ViewerMode) override;
525
526 protected:
527 SbBool processSoEvent(const SoEvent* const ev) override;
528 };
529
530 class GuiExport OpenSCADNavigationStyle: public UserNavigationStyle
531 {
532 using inherited = UserNavigationStyle;
533
534 TYPESYSTEM_HEADER_WITH_OVERRIDE();
535
536 public:
537 OpenSCADNavigationStyle();
538 ~OpenSCADNavigationStyle() override;
539 const char* mouseButtons(ViewerMode) override;
540 ClarifySelectionMode clarifySelectionMode() const override
541 {
542 return ClarifySelectionMode::Ctrl;
543 }
544
545 protected:
546 SbBool processSoEvent(const SoEvent* const ev) override;
547 };
548
549 class GuiExport TinkerCADNavigationStyle: public UserNavigationStyle
550 {
551 using inherited = UserNavigationStyle;
552
553 TYPESYSTEM_HEADER_WITH_OVERRIDE();
554
555 public:
556 TinkerCADNavigationStyle();
557 ~TinkerCADNavigationStyle() override;
558 const char* mouseButtons(ViewerMode) override;
559
560 protected:
561 SbBool processSoEvent(const SoEvent* const ev) override;
562 };
563
564 } // namespace Gui
565 // NOLINTEND(cppcoreguidelines-avoid*, readability-avoid-const-params-in-decls)
566
567 Q_DECLARE_OPERATORS_FOR_FLAGS(Gui::NavigationStyle::RotationCenterModes)
568
569 #endif // GUI_NAVIGATIONSTYLE_H