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