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