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