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