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