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