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