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