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