| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/**************************************************************************** |
| 3 |
* * |
| 4 |
* Copyright (c) 2025 Sayantan Deb <sayantandebin[at]gmail.com> * |
| 5 |
* * |
| 6 |
* This file is part of FreeCAD. * |
| 7 |
* * |
| 8 |
* FreeCAD is free software: you can redistribute it and/or modify it * |
| 9 |
* under the terms of the GNU Lesser General Public License as * |
| 10 |
* published by the Free Software Foundation, either version 2.1 of the * |
| 11 |
* License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* FreeCAD is distributed in the hope that it will be useful, but * |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 16 |
* Lesser General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Lesser General Public * |
| 19 |
* License along with FreeCAD. If not, see * |
| 20 |
* <https://www.gnu.org/licenses/>. * |
| 21 |
* * |
| 22 |
***************************************************************************/ |
| 23 |
|
| 24 |
#ifndef GUI_GIZMO_H |
| 25 |
#define GUI_GIZMO_H |
| 26 |
|
| 27 |
#include <initializer_list> |
| 28 |
#include <memory> |
| 29 |
#include <vector> |
| 30 |
|
| 31 |
#include <Inventor/fields/SoSFBool.h> |
| 32 |
#include <Inventor/sensors/SoFieldSensor.h> |
| 33 |
#include <Inventor/nodekits/SoBaseKit.h> |
| 34 |
#include <Inventor/SbVec3f.h> |
| 35 |
#include <QMetaObject> |
| 36 |
|
| 37 |
#include <Base/Placement.h> |
| 38 |
#include <Gui/DocumentObserver.h> |
| 39 |
|
| 40 |
#include <FCGlobal.h> |
| 41 |
|
| 42 |
class SoDragger; |
| 43 |
class SoCamera; |
| 44 |
class SoInteractionKit; |
| 45 |
|
| 46 |
namespace Gui |
| 47 |
{ |
| 48 |
class QuantitySpinBox; |
| 49 |
class SoLinearDragger; |
| 50 |
class SoLinearDraggerContainer; |
| 51 |
class SoRotationDragger; |
| 52 |
class SoRotationDraggerContainer; |
| 53 |
class View3DInventorViewer; |
| 54 |
class ViewProviderDragger; |
| 55 |
|
| 56 |
struct GizmoPlacement |
| 57 |
{ |
| 58 |
SbVec3f pos; |
| 59 |
SbVec3f dir; |
| 60 |
}; |
| 61 |
|
| 62 |
class GuiExport Gizmo |
| 63 |
{ |
| 64 |
public: |
| 65 |
virtual ~Gizmo() = default; |
| 66 |
virtual SoInteractionKit* initDragger() = 0; |
| 67 |
virtual void uninitDragger() = 0; |
| 68 |
|
| 69 |
virtual GizmoPlacement getDraggerPlacement() = 0; |
| 70 |
virtual void setDraggerPlacement(const SbVec3f& pos, const SbVec3f& dir) = 0; |
| 71 |
void setDraggerPlacement(const Base::Vector3d& pos, const Base::Vector3d& dir); |
| 72 |
|
| 73 |
virtual void setGeometryScale(float scale) = 0; |
| 74 |
virtual void orientAlongCamera([[maybe_unused]] SoCamera* camera) {}; |
| 75 |
bool isDelayedUpdateEnabled(); |
| 76 |
|
| 77 |
double getMultFactor(); |
| 78 |
double getAddFactor(); |
| 79 |
|
| 80 |
bool getVisibility(); |
| 81 |
|
| 82 |
protected: |
| 83 |
double multFactor = 1.0f; |
| 84 |
double addFactor = 0.0f; |
| 85 |
|
| 86 |
QuantitySpinBox* property = nullptr; |
| 87 |
double initialValue; |
| 88 |
|
| 89 |
bool visible = true; |
| 90 |
}; |
| 91 |
|
| 92 |
class GuiExport LinearGizmo: public Gizmo |
| 93 |
{ |
| 94 |
public: |
| 95 |
LinearGizmo(QuantitySpinBox* property); |
| 96 |
~LinearGizmo() override = default; |
| 97 |
|
| 98 |
SoInteractionKit* initDragger() override; |
| 99 |
void uninitDragger() override; |
| 100 |
|
| 101 |
void updateColorTheme(); |
| 102 |
|
| 103 |
// Returns the position and rotation of the base of the dragger |
| 104 |
GizmoPlacement getDraggerPlacement() override; |
| 105 |
void setDraggerPlacement(const SbVec3f& pos, const SbVec3f& dir) override; |
| 106 |
void reverseDir(); |
| 107 |
// Returns the drag distance from the base of the feature |
| 108 |
double getDragLength(); |
| 109 |
void setDragLength(double dragLength); |
| 110 |
void setGeometryScale(float scale) override; |
| 111 |
SoLinearDraggerContainer* getDraggerContainer(); |
| 112 |
void setProperty(QuantitySpinBox* property); |
| 113 |
void setMultFactor(const double val); |
| 114 |
void setAddFactor(const double val); |
| 115 |
void setVisibility(bool visible); |
| 116 |
|
| 117 |
private: |
| 118 |
SoLinearDragger* dragger = nullptr; |
| 119 |
SoLinearDraggerContainer* draggerContainer = nullptr; |
| 120 |
QMetaObject::Connection quantityChangedConnection; |
| 121 |
QMetaObject::Connection formulaDialogConnection; |
| 122 |
|
| 123 |
void draggingStarted(); |
| 124 |
void draggingFinished(); |
| 125 |
void draggingContinued(); |
| 126 |
|
| 127 |
using inherited = Gizmo; |
| 128 |
}; |
| 129 |
|
| 130 |
class GuiExport RotationGizmo: public Gizmo |
| 131 |
{ |
| 132 |
public: |
| 133 |
RotationGizmo(QuantitySpinBox* property); |
| 134 |
~RotationGizmo() override; |
| 135 |
|
| 136 |
SoInteractionKit* initDragger() override; |
| 137 |
void uninitDragger() override; |
| 138 |
|
| 139 |
void updateColorTheme(); |
| 140 |
|
| 141 |
// Distance between the linear gizmo base and rotation gizmo |
| 142 |
double sepDistance = 0; |
| 143 |
|
| 144 |
// Returns the position and rotation of the base of the dragger |
| 145 |
GizmoPlacement getDraggerPlacement() override; |
| 146 |
void setDraggerPlacement(const SbVec3f& pos, const SbVec3f& dir) override; |
| 147 |
void reverseDir(); |
| 148 |
// The two gizmos are separated by sepDistance units |
| 149 |
void placeOverLinearGizmo(LinearGizmo* gizmo); |
| 150 |
void placeBelowLinearGizmo(LinearGizmo* gizmo); |
| 151 |
// Returns the rotation angle wrt the normal axis |
| 152 |
double getRotAngle(); |
| 153 |
void setRotAngle(double angle); |
| 154 |
void setGeometryScale(float scale) override; |
| 155 |
SoRotationDraggerContainer* getDraggerContainer(); |
| 156 |
void orientAlongCamera(SoCamera* camera) override; |
| 157 |
void setProperty(QuantitySpinBox* property); |
| 158 |
void setMultFactor(const double val); |
| 159 |
void setAddFactor(const double val); |
| 160 |
void setVisibility(bool visible); |
| 161 |
|
| 162 |
private: |
| 163 |
SoRotationDragger* dragger = nullptr; |
| 164 |
SoRotationDraggerContainer* draggerContainer = nullptr; |
| 165 |
SoFieldSensor translationSensor; |
| 166 |
LinearGizmo* linearGizmo = nullptr; |
| 167 |
bool automaticOrientation = false; |
| 168 |
QMetaObject::Connection quantityChangedConnection; |
| 169 |
QMetaObject::Connection formulaDialogConnection; |
| 170 |
|
| 171 |
void draggingStarted(); |
| 172 |
void draggingFinished(); |
| 173 |
void draggingContinued(); |
| 174 |
static void translationSensorCB(void* data, SoSensor* sensor); |
| 175 |
|
| 176 |
using inherited = Gizmo; |
| 177 |
}; |
| 178 |
|
| 179 |
class GuiExport DirectedRotationGizmo: public RotationGizmo |
| 180 |
{ |
| 181 |
public: |
| 182 |
DirectedRotationGizmo(QuantitySpinBox* property); |
| 183 |
|
| 184 |
SoInteractionKit* initDragger() override; |
| 185 |
|
| 186 |
void flipArrow(); |
| 187 |
|
| 188 |
private: |
| 189 |
using inherited = RotationGizmo; |
| 190 |
}; |
| 191 |
|
| 192 |
class GuiExport RadialGizmo: public RotationGizmo |
| 193 |
{ |
| 194 |
public: |
| 195 |
RadialGizmo(QuantitySpinBox* property); |
| 196 |
|
| 197 |
SoInteractionKit* initDragger() override; |
| 198 |
|
| 199 |
void updateColorTheme(); |
| 200 |
|
| 201 |
void setRadius(float radius); |
| 202 |
void flipArrow(); |
| 203 |
|
| 204 |
private: |
| 205 |
using inherited = RotationGizmo; |
| 206 |
}; |
| 207 |
|
| 208 |
class GuiExport GizmoContainer: public SoBaseKit |
| 209 |
{ |
| 210 |
SO_KIT_HEADER(GizmoContainer); |
| 211 |
SO_KIT_CATALOG_ENTRY_HEADER(annotation); |
| 212 |
SO_KIT_CATALOG_ENTRY_HEADER(pickStyle); |
| 213 |
SO_KIT_CATALOG_ENTRY_HEADER(toggleSwitch); |
| 214 |
SO_KIT_CATALOG_ENTRY_HEADER(geometry); |
| 215 |
|
| 216 |
public: |
| 217 |
static void initClass(); |
| 218 |
GizmoContainer(); |
| 219 |
~GizmoContainer() override; |
| 220 |
|
| 221 |
SoSFBool visible; |
| 222 |
|
| 223 |
void initGizmos(); |
| 224 |
void uninitGizmos(); |
| 225 |
|
| 226 |
template<typename T = Gizmo> |
| 227 |
T* getGizmo(int index) |
| 228 |
{ |
| 229 |
assert(index >= 0 && index < static_cast<int>(gizmos.size()) && "index out of range!"); |
| 230 |
return dynamic_cast<T*>(gizmos[index]); |
| 231 |
} |
| 232 |
// This should be called only once after construction |
| 233 |
void addGizmos(std::initializer_list<Gui::Gizmo*> gizmos); |
| 234 |
void attachViewer(Gui::View3DInventorViewer* viewer, Base::Placement& origin); |
| 235 |
void setUpAutoScale(SoCamera* cameraIn); |
| 236 |
void calculateScaleAndOrientation(); |
| 237 |
|
| 238 |
// Checks if the gizmos are enabled in the preferences |
| 239 |
static bool isEnabled(); |
| 240 |
static std::unique_ptr<GizmoContainer> create( |
| 241 |
std::initializer_list<Gui::Gizmo*> gizmos, |
| 242 |
ViewProviderDragger* vp |
| 243 |
); |
| 244 |
|
| 245 |
private: |
| 246 |
std::vector<Gizmo*> gizmos; |
| 247 |
SoFieldSensor cameraSensor; |
| 248 |
SoFieldSensor cameraPositionSensor; |
| 249 |
WeakPtrT<ViewProviderDragger> viewProvider; |
| 250 |
|
| 251 |
void addGizmo(Gizmo* gizmo); |
| 252 |
|
| 253 |
static void cameraChangeCallback(void* data, SoSensor*); |
| 254 |
static void cameraPositionChangeCallback(void* data, SoSensor*); |
| 255 |
}; |
| 256 |
|
| 257 |
} // namespace Gui |
| 258 |
|
| 259 |
#endif /* GUI_GIZMO_H */ |