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