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