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