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