Salome HOME
Merge with PythonAPI branch
[modules/shaper.git] / src / PartSet / PartSet_SketcherMgr.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_SketcherMgr.h
4 // Created:     19 Dec 2014
5 // Author:      Vitaly SMETANNIKOV
6
7 #ifndef PartSet_SketcherMgr_H
8 #define PartSet_SketcherMgr_H
9
10 #include "PartSet.h"
11
12 #include <ModelAPI_Feature.h>
13 #include <ModelAPI_Attribute.h>
14 #include <ModelAPI_CompositeFeature.h>
15 #include <ModelAPI_Result.h>
16
17 #include <ModuleBase_ViewerFilters.h>
18 #include <ModuleBase_Definitions.h>
19 #include <ModuleBase_ModelWidget.h>
20
21 #include <GeomAPI_Pln.h>
22 #include <SelectMgr_IndexedMapOfOwner.hxx>
23
24 #include <QObject>
25 #include <QList>
26 #include <QMap>
27
28 class PartSet_Module;
29 class ModuleBase_IViewWindow;
30 class ModuleBase_ModelWidget;
31 class ModuleBase_Operation;
32 class XGUI_OperationMgr;
33 class QMouseEvent;
34
35 /**
36 * \ingroup Modules
37 * A class for management of sketch operations
38   At the time of the sketcher operation active, only the sketch sub-feature results are
39   displayed in the viewer. After the sketch create/edit operation is finished, the sub-feature
40   are hidden, the sketch feature result is displayed
41 */
42 class PARTSET_EXPORT PartSet_SketcherMgr : public QObject
43 {
44   Q_OBJECT
45   /// Struct to define gp point, with the state is the point is initialized
46   struct Point
47   {
48     /// Constructor
49     Point()
50     {
51       myIsInitialized = false;
52     }
53     /// Destructor
54     ~Point()
55     {
56     }
57
58     /// clear the initialized flag.
59     void clear()
60     {
61       myIsInitialized = false;
62     }
63     /// set the point and switch on the initialized flag
64     /// \param thePoint the point
65     void setValue(const double theX, const double theY)
66     {
67       myIsInitialized = true;
68       myCurX = theX;
69       myCurY = theY;
70     }
71
72     bool myIsInitialized;  /// the state whether the point is set
73     double myCurX, myCurY; /// the point coordinates
74   };
75 public:
76   /// Constructor
77   /// \param theModule a pointer to PartSet module
78   PartSet_SketcherMgr(PartSet_Module* theModule);
79
80   virtual ~PartSet_SketcherMgr();
81
82   /// Returns true if the operation is the sketch
83   /// \param theOperation an operation
84   /// \return the boolean result
85   static bool isSketchOperation(ModuleBase_Operation* theOperation);
86
87   /// Returns true if the operation id is in the sketch operation id list
88   /// \param theOperation an operation
89   /// \return the boolean result
90   static bool isNestedSketchOperation(ModuleBase_Operation* theOperation);
91
92   /// Returns true if the operation is a create nested feature one
93   /// \param theOperation a checked operation
94   //// \return boolean value
95   static bool isNestedCreateOperation(ModuleBase_Operation* theOperation);
96
97   /// Returns true if the operation is an edit nested feature one
98   /// \param theOperation a checked operation
99   //// \return boolean value
100   static bool isNestedEditOperation(ModuleBase_Operation* theOperation);
101
102   /// Returns whether the current operation is a sketch entity - line, point, arc or circle
103   /// \param theId is an id of object
104   /// \return a boolean value
105   static bool isEntity(const std::string& theId);
106
107   /// Returns whether the current operation is a sketch distance - lenght, distance or radius
108   /// \param theOperation the operation
109   /// \return a boolean value
110   static bool isDistanceOperation(ModuleBase_Operation* theOperation);
111
112   /// Returns true if a mouse cursor is over viewer window
113   bool isMouseOverWindow() { return myIsMouseOverWindow; }
114
115   /// Returns current Sketch feature/ Returns NULL if there is no launched sketch operation
116   CompositeFeaturePtr activeSketch() const { return myCurrentSketch; }
117
118   /// Starts sketch operation
119   void startSketch(ModuleBase_Operation* );
120
121   /// Stops sketch operation
122   void stopSketch(ModuleBase_Operation* );
123
124   /// Starts sketch operation, connects to the opeation property panel
125   /// \param theOperation a committed operation
126   void startNestedSketch(ModuleBase_Operation* theOperation);
127
128   /// Stop sketch operation, disconnects from the opeation property panel
129   /// \param theOperation a stopped operation
130   void stopNestedSketch(ModuleBase_Operation* theOperation);
131
132   /// Visualizes the operation feature if it is a creation nested feature operation
133   /// \param theOperation a committed operation
134   void commitNestedSketch(ModuleBase_Operation* theOperation);
135
136   /// Returns True if there are available Undos and the sketch manager allows undo
137   /// \return the boolean result
138   bool canUndo() const;
139
140   //! Returns True if there are available Redos and the sketch manager allows redo
141   /// \return the boolean result
142   bool canRedo() const;
143
144   /// Returns False only if the sketch creating feature can not be visualized.
145   /// \return a boolean value
146   //bool canCommitOperation() const;
147
148   /// Returns whether the object can be erased at the bounds of the active operation.
149   /// Sketch sub-entities can not be erased during the sketch operation
150   /// \param theObject a model object
151   bool canEraseObject(const ObjectPtr& theObject) const;
152
153   /// Returns whether the object can be displayed at the bounds of the active operation.
154   /// Display only current operation results for usual operation and ask the sketcher manager
155   /// if it is a sketch operation
156   /// \param theObject a model object
157   bool canDisplayObject(const ObjectPtr& theObject) const;
158
159   /// Returns true if the mouse is over viewer or property panel value is changed
160   /// \return boolean result
161   bool canDisplayCurrentCreatedFeature() const;
162
163   /// Returns true if the current operation is nested creation or internal reentrant edit
164   /// \param theOperation an operation
165   bool canChangeCursor(ModuleBase_Operation* theOperation) const;
166
167   /// Returns state of constraints showing flag 
168   bool isConstraintsShown() const { return myIsConstraintsShown; }
169
170   /// Returns true if the object is a current sketch sub feature of a result of the feature
171   /// \param theObject an object
172   /// \return boolean value
173   bool isObjectOfSketch(const ObjectPtr& theObject) const;
174
175   /// Saves the current selection in the viewer into an internal container
176   /// It obtains the selected attributes. The highlighted objects can be processes as the selected ones
177   /// \param theHighlightedOnly a boolean flag
178   void storeSelection(const bool theHighlightedOnly = false);
179
180   /// Restores previously saved selection state
181   void restoreSelection();
182
183   /// Return error state of the sketch feature, true if the error has happened
184   /// \return boolean value
185   bool sketchSolverError();
186
187   //! Returns the feature error if the current state of the feature in the sketch is not correct
188   //! If the feature is correct, it returns an empty value
189   //! Incorrect states: the feature is sketch, the solver error value
190   //! The feature value is reset, this is the flag of sketch mgr
191   //! \return string value
192   QString getFeatureError(const FeaturePtr& theFeature);
193
194   /// It nullify internal flags concerned to clicked mouse event
195   void clearClickedFlags();
196
197   /// Returns list of strings which contains id's of sketch operations
198   static const QStringList& sketchOperationIdList();
199
200   /// Returns list of strings which contains id's of constraints operations
201   static const QStringList& constraintsIdList();
202
203   /// Returns a list of modes, where the AIS objects should be activated
204   /// \param theModes a list of modes
205   static void sketchSelectionModes(QIntList& theModes);
206
207   /// Connects or disconnects to the value changed signal of the property panel widgets
208   /// \param theWidget a property contol widget
209   /// \param isToConnect a boolean value whether connect or disconnect
210   void connectToPropertyPanel(ModuleBase_ModelWidget* theWidget, const bool isToConnect);
211
212   /// Visualize the operation feature if the previous state is modified value in property panel
213   /// \param thePreviousState the previous widget value state
214   void widgetStateChanged(int thePreviousState);
215
216 public slots:
217   /// Process sketch plane selected event
218   void onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln);
219
220   /// Toggle show constraints
221   void onShowConstraintsToggle(bool);
222
223 private slots:
224   /// Process the enter mouse to the view port. If the current operation is a create of
225   /// a nested sketch feature, it updates internal flags to display the feature on mouse move
226   void onEnterViewPort();
227   /// Process the leave mouse of the view port. If the current operation is a create of
228   /// a nested sketch feature, it hides the feature in the viewer
229   void onLeaveViewPort();
230   /// Listens to the value changed signal and display the current operation feature
231   void onBeforeValuesChangedInPropertyPanel();
232   /// Listens to the signal about the modification of the values have been done in the property panel
233   void onAfterValuesChangedInPropertyPanel();
234
235   void onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*);
236   void onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*);
237   void onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*);
238   void onMouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*);
239   void onApplicationStarted();
240   //void onBeforeWidgetActivated(ModuleBase_ModelWidget* theWidget);
241
242   void onBeforeContextMenu();
243   void onAfterContextMenu();
244
245 private:
246   /// Launches the operation from current highlighting
247   void launchEditing();
248
249   /// Converts mouse position to 2d coordinates. 
250   /// Member myCurrentSketch has to be correctly defined
251   void get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent, 
252                   Point& thePoint);
253
254   typedef QMap<FeaturePtr, std::pair<std::set<AttributePtr>, std::set<ResultPtr> > >
255                                                                        FeatureToSelectionMap;
256
257   /// Obtains the current selection of the object in the workshop viewer 
258   /// It includes the selection in all modes of activation, even local context - vertices, edges
259   /// It gets all results of the feature, find an AIS object in the viewer and takes all BRep
260   /// selection owners. If the owner is vertex, the corresponded attribute is seached in
261   /// the feature, if the owner is edge, the current result is added to the container of results.
262   /// \param theFeature a feature or result object
263   /// \param theSketch a current sketch feature
264   /// \param theWorkshop a workshop to have an access to AIS context and displayer
265   /// \param theSelection a container for the selection, to save results and attributres for a feature
266   static void getCurrentSelection(const FeaturePtr& theFeature,
267                                   const FeaturePtr& theSketch,
268                                   ModuleBase_IWorkshop* theWorkshop,
269                                   FeatureToSelectionMap& theSelection);
270
271   /// Applyes the current selection to the object in the workshop viewer 
272   /// It includes the selection in all modes of activation, even local context - vertexes, edges
273   /// It gets all results of the feature, find an AIS object in the viewer and takes all BRep
274   /// selection owners. If the owner is vertex, the corresponded attribute is seached in
275   /// the feature and if it is in the container of selected attributes, the owner is put in the
276   /// out container. If the owner is edge and the current result is in the container of selected
277   /// results, the owner is put in the out container.
278   /// \param theFeature a feature or result object
279   /// \param theSketch a current sketch feature
280   /// \param theWorkshop a workshop to have an access to AIS context and displayer
281   /// \param theSelection a container of the selection, it has results and attributres for a feature
282   /// \param theOwnersToSelect an out container of found owners
283   static void getSelectionOwners(const FeaturePtr& theFeature,
284                                   const FeaturePtr& theSketch,
285                                   ModuleBase_IWorkshop* theWorkshop,
286                                   const FeatureToSelectionMap& theSelection,
287                                   SelectMgr_IndexedMapOfOwner& anOwnersToSelect);
288
289   /// Returns true if the created feature is visible
290   /// \param 
291   bool isVisibleCreatedFeature() const;
292
293   /// Returns a current operation
294   /// \return an operation
295   ModuleBase_Operation* getCurrentOperation() const;
296
297   /// Get the active widget in the property panel
298   ModuleBase_ModelWidget* getActiveWidget() const;
299
300   /// Erase or display the feature of the current operation. If the mouse over the active view or
301   /// a current value is changed by property panel, the feature is displayed otherwise it is hidden
302   /// \param theOperation an operation which feature is to be displayed, it is nested create operation
303   /// \param isToDisplay a flag about the display or erase the feature
304   void visualizeFeature(const FeaturePtr& theFeature, const bool isEditOperation,
305                         const bool isToDisplay, const bool isFlushRedisplay = true);
306
307 private:
308   XGUI_OperationMgr* operationMgr() const;
309
310 private:
311   PartSet_Module* myModule;
312
313   bool myPreviousDrawModeEnabled; // the previous selection enabled state in the viewer
314   bool myIsDragging;
315   bool myDragDone;
316   bool myIsMouseOverWindow; /// the state that the mouse over the view
317   bool myIsMouseOverViewProcessed; /// the state whether the over view state is processed by mouseMove method
318   bool myIsPopupMenuActive; /// the state of the popup menu is shown
319   Point myCurrentPoint;
320   //Point myClickedPoint;
321
322   CompositeFeaturePtr myCurrentSketch;
323
324   Handle(ModuleBase_ShapeInPlaneFilter) myPlaneFilter;
325   FeatureToSelectionMap myCurrentSelection;
326   bool myPreviousUpdateViewerEnabled;
327
328   bool myIsConstraintsShown;
329 };
330
331
332 #endif