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