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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
// SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************

 *   Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net>     *

 *                                                                         *

 *   This file is part of the FreeCAD CAx development system.              *

 *                                                                         *

 *   This library is free software; you can redistribute it and/or         *

 *   modify it under the terms of the GNU Library General Public           *

 *   License as published by the Free Software Foundation; either          *

 *   version 2 of the License, or (at your option) any later version.      *

 *                                                                         *

 *   This library  is distributed in the hope that it will be useful,      *

 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *

 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *

 *   GNU Library General Public License for more details.                  *

 *                                                                         *

 *   You should have received a copy of the GNU Library General Public     *

 *   License along with this library; see the file COPYING.LIB. If not,    *

 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *

 *   Suite 330, Boston, MA  02111-1307, USA                                *

 *                                                                         *

 ***************************************************************************/


#ifndef GUI_NAVIGATIONSTYLE_H
#define GUI_NAVIGATIONSTYLE_H

#include <Inventor/C/basic.h>
#include <Inventor/SbBox2s.h>
#include <Inventor/SbPlane.h>
#include <Inventor/SbRotation.h>
#include <Inventor/SbSphere.h>
#include <Inventor/SbTime.h>
#include <Inventor/SbVec2f.h>
#include <Inventor/SbVec2s.h>
#include <Inventor/SbVec3f.h>
#include <Inventor/events/SoEvents.h>

#include <QEvent>
#include <QAction>
#include <Base/BaseClass.h>
#include <Base/SmartPtrPy.h>
#include <Gui/Namespace.h>
#include <FCGlobal.h>
#include <memory>
#include <optional>

// forward declarations
class SoEvent;
class SoMouseWheelEvent;
class SoMotion3Event;
class SoQtViewer;
class SoCamera;
class SoSensor;
class SbSphereSheetProjector;

// NOLINTBEGIN(cppcoreguidelines-avoid*, readability-avoid-const-params-in-decls)
namespace Gui
{

class View3DInventorViewer;
class NavigationAnimator;
class AbstractMouseSelection;
class NavigationAnimation;

/**

 * @author Werner Mayer

 */
class GuiExport NavigationStyleEvent: public QEvent
{
public:
    explicit NavigationStyleEvent(const Base::Type& s);
    ~NavigationStyleEvent() override;
    const Base::Type& style() const;

private:
    Base::Type t;
};

/**

 * The navigation style base class

 * @author Werner Mayer

 */
class GuiExport NavigationStyle: public Base::BaseClass
{
    TYPESYSTEM_HEADER_WITH_OVERRIDE();

public:
    enum ViewerMode

    {
        IDLE,
        INTERACT,
        ZOOMING,
        BOXZOOM,
        PANNING,
        DRAGGING,
        SPINNING,
        SEEK_WAIT_MODE,
        SEEK_MODE,
        SELECTION
    };

    enum SelectionMode

    {
        Lasso = 0,      /**< Select objects using a lasso. */
        Rectangle = 1,  /**< Select objects using a rectangle. */
        Rubberband = 2, /**< Select objects using a rubberband. */
        BoxZoom = 3,    /**< Perform a box zoom. */
        Clip = 4,       /**< Clip objects using a lasso. */
    };

    enum class ClarifySelectionMode

    {
        Default, /**< Long press with LMB to trigger clarify selection */
        Ctrl     /**< Long press with Ctrl+LMB to trigger clarify selection */
    };

    enum OrbitStyle

    {
        Turntable,
        Trackball,
        FreeTurntable,
        TrackballClassic,
        RoundedArcball
    };

    enum class RotationCenterMode

    {
        WindowCenter = 0,       /**< The center of the window */
        ScenePointAtCursor = 1, /**< Find the point in the scene at the cursor position. If there is

                                   no point then the focal plane is used */
        FocalPointAtCursor = 2, /**< Find the point on the focal plane at the cursor position. */
        BoundingBoxCenter = 4   /**< Find the center point of the bounding box of the scene. */
    };
    Q_DECLARE_FLAGS(RotationCenterModes, RotationCenterMode)

public:
    NavigationStyle();
    ~NavigationStyle() override;
    NavigationStyle(const NavigationStyle&) = delete;

    NavigationStyle& operator=(const NavigationStyle& ns);
    void setViewer(View3DInventorViewer*);

    void setAnimationEnabled(const SbBool enable);
    void setSpinningAnimationEnabled(const SbBool enable);
    SbBool isAnimationEnabled() const;
    SbBool isSpinningAnimationEnabled() const;
    SbBool isAnimating() const;
    SbBool isSpinning() const;
    void startAnimating(const std::shared_ptr<NavigationAnimation>& animation, bool wait = false) const;
    void stopAnimating() const;

    void setSensitivity(float);
    float getSensitivity() const;

    void setResetCursorPosition(SbBool);
    SbBool isResetCursorPosition() const;

    void setZoomInverted(SbBool);
    SbBool isZoomInverted() const;
    void setZoomStep(float);
    void setZoomAtCursor(SbBool);
    SbBool isZoomAtCursor() const;
    void zoomIn();
    void zoomOut();
    void setRotationCenterMode(RotationCenterModes);
    RotationCenterModes getRotationCenterMode() const;
    void setRotationCenter(const SbVec3f& cnt);
    SbVec3f getFocalPoint() const;

    SoCamera* getCamera() const;
    std::shared_ptr<NavigationAnimation> setCameraOrientation(

        const SbRotation& orientation,

        SbBool moveToCenter = false

    ) const;
    std::shared_ptr<NavigationAnimation> translateCamera(const SbVec3f& translation) const;

#if (COIN_MAJOR_VERSION * 100 + COIN_MINOR_VERSION * 10 + COIN_MICRO_VERSION < 403)
    void findBoundingSphere();
#endif

    void reorientCamera(SoCamera* camera, const SbRotation& rotation);
    void reorientCamera(SoCamera* camera, const SbRotation& rotation, const SbVec3f& rotationCenter);

    void boxZoom(const SbBox2s& box);
    // Scale the camera inplace
    void scale(float factor);
    virtual void viewAll();

    void setViewingMode(const ViewerMode newmode);
    int getViewingMode() const;
    virtual SbBool processEvent(const SoEvent* const ev);
    virtual SbBool processMotionEvent(const SoMotion3Event* const ev);
    virtual SbBool processKeyboardEvent(const SoKeyboardEvent* const event);
    virtual SbBool processClickEvent(const SoMouseButtonEvent* const event);
    virtual SbBool processWheelEvent(const SoMouseWheelEvent* const event);

    void setPopupMenuEnabled(const SbBool on);
    SbBool isPopupMenuEnabled() const;

    void startSelection(AbstractMouseSelection*);
    void startSelection(SelectionMode = Lasso);
    void abortSelection();
    void stopSelection();
    SbBool isSelecting() const;
    const std::vector<SbVec2s>& getPolygon(SelectionRole* role = nullptr) const;

    bool isDraggerUnderCursor(const SbVec2s pos) const;

    virtual ClarifySelectionMode clarifySelectionMode() const
    {
        return ClarifySelectionMode::Default;
    }

    void setOrbitStyle(OrbitStyle style);
    OrbitStyle getOrbitStyle() const;

    SbBool isViewing() const;
    void setViewing(SbBool);

    SbVec3f getRotationCenter(SbBool&) const;

    std::optional<SbVec2s>& getRightClickPosition();

    PyObject* getPyObject() override;

protected:
    void initialize();
    void finalize();

    void interactiveCountInc();
    void interactiveCountDec();
    int getInteractiveCount() const;

    SbBool isSeekMode() const;
    void setSeekMode(SbBool enable);
    SbBool seekToPoint(const SbVec2s screenpos);
    void seekToPoint(const SbVec3f& scenepos);
    void lookAtPoint(const SbVec2s screenpos);
    void lookAtPoint(const SbVec3f& position);

    void panCamera(

        SoCamera* camera,

        float vpaspect,

        const SbPlane& panplane,

        const SbVec2f& previous,

        const SbVec2f& current

    );
    void setupPanningPlane(const SoCamera* camera);
    int getDelta() const;
    void zoom(SoCamera* camera, float diffvalue);
    virtual void zoomByCursor(const SbVec2f& thispos, const SbVec2f& prevpos);
    void doZoom(SoCamera* camera, int wheeldelta, const SbVec2f& pos);
    void doZoom(SoCamera* camera, float logzoomfactor, const SbVec2f& pos);
    void doScale(SoCamera* camera, float factor);
    void doRotate(SoCamera* camera, float angle, const SbVec2f& pos);
    void spin(const SbVec2f& pointerpos);
    SbBool doSpin();
    void spin_simplified(SbVec2f curpos, SbVec2f prevpos);
    void moveCursorPosition();
    void saveCursorPosition(const SoEvent* const ev);

    SbVec2f normalizePixelPos(SbVec2s pixpos);
    SbVec2f normalizePixelPos(SbVec2f pixpos);

    SbBool handleEventInForeground(const SoEvent* const e);
    virtual SbBool processSoEvent(const SoEvent* const ev);
    void syncWithEvent(const SoEvent* const ev);
    virtual void openPopupMenu(const SbVec2s& position);

private:
    void spinInternal(const SbVec2f& pointerpos, const SbVec2f& lastpos);
    void spinSimplifiedInternal(const SbVec2f curpos, const SbVec2f prevpos);
    bool isNavigationStyleAction(QAction* action, QActionGroup* navMenuGroup) const;
    QWidget* findView3DInventorWidget() const;
    void applyNavigationStyleChange(QAction* selectedAction);

protected:
    void clearLog();
    void addToLog(const SbVec2s pos, const SbTime time);

    void syncModifierKeys(const SoEvent* const ev);

protected:
    struct

    {  // tracking mouse movement in a log
        short size;
        short historysize;
        SbVec2s* position;
        SbTime* time;
    } log;

    View3DInventorViewer* viewer {nullptr};
    NavigationAnimator* animator;
    SbBool animationEnabled;
    ViewerMode currentmode;
    SoMouseButtonEvent mouseDownConsumedEvent;
    SbVec2f lastmouseposition;
    SbVec2s globalPos;
    SbVec2s localPos;
    SbPlane panningplane;
    SbTime centerTime;
    SbBool lockrecenter;
    SbBool menuenabled;
    SbBool ctrldown, shiftdown, altdown;
    SbBool button1down, button2down, button3down;
    SbBool invertZoom;
    SbBool zoomAtCursor;
    float zoomStep;
    SbBool hasDragged;
    SbBool hasPanned;
    SbBool hasZoomed;

    /** @name Mouse model */
    //@{
    AbstractMouseSelection* mouseSelection {nullptr};
    std::vector<SbVec2s> pcPolygon;
    SelectionRole selectedRole;
    //@}

    /** @name Spinning data */
    //@{
    SbBool spinningAnimationEnabled;
    int spinsamplecounter;
    SbRotation spinincrement;
    SbSphereSheetProjector* spinprojector;
    //@}

    Py::SmartPtr pythonObject;

    // store the position where right-click occurred just before
    // the menu popped up
    std::optional<SbVec2s> rightClickPosition;

private:
    friend class NavigationAnimator;

    SbVec3f rotationCenter;
    SbBool rotationCenterFound;
    SbBool rotationCenterIsScenePointAtCursor;
    NavigationStyle::RotationCenterModes rotationCenterMode;
    float sensitivity;
    SbBool resetcursorpos;

#if (COIN_MAJOR_VERSION * 100 + COIN_MINOR_VERSION * 10 + COIN_MICRO_VERSION < 403)
    SbSphere boundingSphere;
#endif
};

/** Sub-classes of this class appear in the preference dialog where users can

 * choose their favorite navigation style.

 * All other classes that inherit directly from NavigationStyle do not appear

 * in the above dialog.

 * This mechanism is useful to implement special navigation styles which are

 * only needed for certain purposes. Thus, it should not be possible to be

 * choosable by the user

 * @author Werner Mayer

 */
class GuiExport UserNavigationStyle: public NavigationStyle
{
    TYPESYSTEM_HEADER_WITH_OVERRIDE();

public:
    UserNavigationStyle() = default;
    ~UserNavigationStyle() override = default;
    virtual const char* mouseButtons(ViewerMode) = 0;
    virtual std::string userFriendlyName() const;
    static std::map<Base::Type, std::string> getUserFriendlyNames();
};

class GuiExport InventorNavigationStyle: public UserNavigationStyle
{
    using inherited = UserNavigationStyle;

    TYPESYSTEM_HEADER_WITH_OVERRIDE();

public:
    InventorNavigationStyle();
    ~InventorNavigationStyle() override;
    const char* mouseButtons(ViewerMode) override;
    std::string userFriendlyName() const override;
    ClarifySelectionMode clarifySelectionMode() const override
    {
        return ClarifySelectionMode::Ctrl;
    }

protected:
    SbBool processSoEvent(const SoEvent* const ev) override;
};

class GuiExport CADNavigationStyle: public UserNavigationStyle
{
    using inherited = UserNavigationStyle;

    TYPESYSTEM_HEADER_WITH_OVERRIDE();

public:
    CADNavigationStyle();
    ~CADNavigationStyle() override;
    const char* mouseButtons(ViewerMode) override;

protected:
    SbBool processSoEvent(const SoEvent* const ev) override;

private:
    SbBool lockButton1 {false};
};

class GuiExport RevitNavigationStyle: public UserNavigationStyle
{
    using inherited = UserNavigationStyle;

    TYPESYSTEM_HEADER_WITH_OVERRIDE();

public:
    RevitNavigationStyle();
    ~RevitNavigationStyle() override;
    const char* mouseButtons(ViewerMode) override;

protected:
    SbBool processSoEvent(const SoEvent* const ev) override;

private:
    SbBool lockButton1 {false};
};

class GuiExport BlenderNavigationStyle: public UserNavigationStyle
{
    using inherited = UserNavigationStyle;

    TYPESYSTEM_HEADER_WITH_OVERRIDE();

public:
    BlenderNavigationStyle();
    ~BlenderNavigationStyle() override;
    const char* mouseButtons(ViewerMode) override;

protected:
    SbBool processSoEvent(const SoEvent* const ev) override;

private:
    SbBool lockButton1 {false};
};

class GuiExport SolidWorksNavigationStyle: public UserNavigationStyle
{
    using inherited = UserNavigationStyle;

    TYPESYSTEM_HEADER_WITH_OVERRIDE();

public:
    SolidWorksNavigationStyle();
    ~SolidWorksNavigationStyle() override;
    const char* mouseButtons(ViewerMode) override;

protected:
    SbBool processSoEvent(const SoEvent* const ev) override;

private:
    SbBool lockButton1 {false};
};

class GuiExport MayaGestureNavigationStyle: public UserNavigationStyle
{
    using inherited = UserNavigationStyle;

    TYPESYSTEM_HEADER_WITH_OVERRIDE();

public:
    MayaGestureNavigationStyle();
    ~MayaGestureNavigationStyle() override;
    const char* mouseButtons(ViewerMode) override;

protected:
    void zoomByCursor(const SbVec2f& thispos, const SbVec2f& prevpos) override;

    SbBool processSoEvent(const SoEvent* const ev) override;

    SbVec2s mousedownPos;  // the position where some mouse button was pressed (local pixel coordinates).
    short mouseMoveThreshold;  // setting. Minimum move required to consider it a move (in pixels).
    bool mouseMoveThresholdBroken;  // a flag that the move threshold was surpassed since last mousedown.
    int mousedownConsumedCount;  // a flag for remembering that a mousedown of button1/button2 was
                                 // consumed.
    SoMouseButtonEvent mousedownConsumedEvents[5];  // the event that was consumed and is to be
                                                    // refired. 2 should be enough, but just for a
                                                    // case of the maximum 5 buttons...
    bool testMoveThreshold(const SbVec2s currentPos) const;

    bool thisClickIsComplex;  // a flag that becomes set when a complex clicking pattern is detected
                              // (i.e., two or more mouse buttons were down at the same time).
    bool inGesture;           // a flag that is used to filter out mouse events during gestures.
};

class GuiExport TouchpadNavigationStyle: public UserNavigationStyle
{
    using inherited = UserNavigationStyle;

    TYPESYSTEM_HEADER_WITH_OVERRIDE();

public:
    TouchpadNavigationStyle();
    ~TouchpadNavigationStyle() override;
    const char* mouseButtons(ViewerMode) override;

protected:
    SbBool processSoEvent(const SoEvent* const ev) override;

private:
    SbBool blockPan {false};  // Used to block the first pan in a mouse movement to prevent big jumps
};

class GuiExport OpenCascadeNavigationStyle: public UserNavigationStyle
{
    using inherited = UserNavigationStyle;

    TYPESYSTEM_HEADER_WITH_OVERRIDE();

public:
    OpenCascadeNavigationStyle();
    ~OpenCascadeNavigationStyle() override;
    const char* mouseButtons(ViewerMode) override;

protected:
    SbBool processSoEvent(const SoEvent* const ev) override;
};

class GuiExport OpenSCADNavigationStyle: public UserNavigationStyle
{
    using inherited = UserNavigationStyle;

    TYPESYSTEM_HEADER_WITH_OVERRIDE();

public:
    OpenSCADNavigationStyle();
    ~OpenSCADNavigationStyle() override;
    const char* mouseButtons(ViewerMode) override;
    ClarifySelectionMode clarifySelectionMode() const override
    {
        return ClarifySelectionMode::Ctrl;
    }

protected:
    SbBool processSoEvent(const SoEvent* const ev) override;
};

class GuiExport TinkerCADNavigationStyle: public UserNavigationStyle
{
    using inherited = UserNavigationStyle;

    TYPESYSTEM_HEADER_WITH_OVERRIDE();

public:
    TinkerCADNavigationStyle();
    ~TinkerCADNavigationStyle() override;
    const char* mouseButtons(ViewerMode) override;

protected:
    SbBool processSoEvent(const SoEvent* const ev) override;
};

}  // namespace Gui
// NOLINTEND(cppcoreguidelines-avoid*, readability-avoid-const-params-in-decls)

Q_DECLARE_OPERATORS_FOR_FLAGS(Gui::NavigationStyle::RotationCenterModes)

#endif  // GUI_NAVIGATIONSTYLE_H