Salome HOME
Issue #326 Distance constraint on 2 preselected segments problem
[modules/shaper.git] / src / PartSet / PartSet_SketcherMgr.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_SketcherMgr.h
4 // Created:     19 Dec 2014
5 // Author:      Vitaly SMETANNIKOV
6
7 #ifndef PartSet_SketcherMgr_H
8 #define PartSet_SketcherMgr_H
9
10 #include "PartSet.h"
11
12 #include <ModelAPI_Feature.h>
13 #include <ModelAPI_Attribute.h>
14 #include <ModelAPI_CompositeFeature.h>
15 #include <ModelAPI_Result.h>
16
17 #include <ModuleBase_ViewerFilters.h>
18 #include <ModuleBase_Definitions.h>
19
20 #include <GeomAPI_Pln.h>
21 #include <SelectMgr_IndexedMapOfOwner.hxx>
22
23 #include <QObject>
24 #include <QList>
25 #include <QMap>
26
27 class PartSet_Module;
28 class ModuleBase_IViewWindow;
29 class ModuleBase_ModelWidget;
30 class ModuleBase_Operation;
31 class QMouseEvent;
32
33 /**
34 * A class for management of sketch operations
35 */
36 class PARTSET_EXPORT PartSet_SketcherMgr : public QObject
37 {
38   Q_OBJECT
39   /// Struct to define gp point, with the state is the point is initialized
40   struct Point
41   {
42     /// Constructor
43     Point()
44     {
45       myIsInitialized = false;
46     }
47     /// Destructor
48     ~Point()
49     {
50     }
51
52     /// clear the initialized flag.
53     void clear()
54     {
55       myIsInitialized = false;
56     }
57     /// set the point and switch on the initialized flag
58     /// \param thePoint the point
59     void setValue(const double theX, const double theY)
60     {
61       myIsInitialized = true;
62       myCurX = theX;
63       myCurY = theY;
64     }
65
66     bool myIsInitialized;  /// the state whether the point is set
67     double myCurX, myCurY; /// the point coordinates
68   };
69 public:
70   PartSet_SketcherMgr(PartSet_Module* theModule);
71
72   virtual ~PartSet_SketcherMgr();
73
74   static QStringList sketchOperationIdList();
75
76   /// Launches the operation from current highlighting
77   void launchEditing();
78
79   // Returns current Sketch feature/ Returns NULL if there is no launched sketch operation
80   CompositeFeaturePtr activeSketch() const { return myCurrentSketch; }
81
82   /// Starts sketch operation
83   void startSketch(ModuleBase_Operation* theOperation);
84
85   /// Stops sketch operation
86   void stopSketch(ModuleBase_Operation* theOperation);
87
88 public slots:
89   void onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln);
90
91
92 private slots:
93   void onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*);
94   void onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*);
95   void onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*);
96   void onMouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*);
97   void onApplicationStarted();
98   void onBeforeWidgetActivated(ModuleBase_ModelWidget* theWidget);
99
100 private:
101   /// Returns whethe the current operation is a sketch distance - lenght, distance or radius
102   /// \param the operation
103   /// \return a boolean value
104   bool isDistanceOperation(ModuleBase_Operation* theOperation) const;
105
106   /// Converts mouse position to 2d coordinates. 
107   /// Member myCurrentSketch has to be correctly defined
108   void get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent, 
109                   Point& thePoint);
110
111   typedef QList<AttributePtr> AttributeList;
112   typedef QMap<FeaturePtr, AttributeList> FeatureToAttributesMap;
113   typedef std::map<FeaturePtr, std::pair<std::set<AttributePtr>, std::set<ResultPtr> > >
114                                                                        FeatureToSelectionMap;
115   /// Obtains the current selection of the object in the workshop viewer by a map of feature to attributes
116   /// It calls the next method for each feature
117   /// \param theFeatureToAttributes a map of feature to attributes
118   /// \param theSketch a current sketch feature
119   /// \param theWorkshop a workshop to have an access to AIS context and displayer
120   /// \param theSelection a container for the selection, to save results and attributres for a feature
121   static void getCurrentSelection(const FeatureToAttributesMap& theFeatureToAttributes,
122                                   const FeaturePtr& theSketch,
123                                   ModuleBase_IWorkshop* theWorkshop,
124                                   FeatureToSelectionMap& theSelection);
125
126   /// Obtains the current selection of the object in the workshop viewer 
127   /// It includes the selection in all modes of activation, even local context - vertices, edges
128   /// It gets all results of the feature, find an AIS object in the viewer and takes all BRep
129   /// selection owners. If the owner is vertex, the corresponded attribute is seached in
130   /// the feature, if the owner is edge, the current result is added to the container of results.
131   /// \param theFeature a feature or result object
132   /// \param theSketch a current sketch feature
133   /// \param theWorkshop a workshop to have an access to AIS context and displayer
134   /// \param theSelection a container for the selection, to save results and attributres for a feature
135   static void getCurrentSelection(const FeaturePtr& theFeature,
136                                   const FeaturePtr& theSketch,
137                                   ModuleBase_IWorkshop* theWorkshop,
138                                   FeatureToSelectionMap& theSelection);
139
140   /// Applyes the current selection to the object in the workshop viewer 
141   /// It includes the selection in all modes of activation, even local context - vertexes, edges
142   /// It gets all results of the feature, find an AIS object in the viewer and takes all BRep
143   /// selection owners. If the owner is vertex, the corresponded attribute is seached in
144   /// the feature and if it is in the container of selected attributes, the owner is put in the
145   /// out container. If the owner is edge and the current result is in the container of selected
146   /// results, the owner is put in the out container.
147   /// \param theFeature a feature or result object
148   /// \param theSketch a current sketch feature
149   /// \param theWorkshop a workshop to have an access to AIS context and displayer
150   /// \param theSelection a container of the selection, it has results and attributres for a feature
151   /// \param theOwnersToSelect an out container of found owners
152   static void getSelectionOwners(const FeaturePtr& theFeature,
153                                   const FeaturePtr& theSketch,
154                                   ModuleBase_IWorkshop* theWorkshop,
155                                   const FeatureToSelectionMap& theSelection,
156                                   SelectMgr_IndexedMapOfOwner& anOwnersToSelect);
157
158 private:
159   PartSet_Module* myModule;
160
161   bool myPreviousSelectionEnabled; // the previous selection enabled state in the viewer
162   bool myIsDragging;
163   bool myDragDone;
164   Point myCurrentPoint;
165   Point myClickedPoint;
166
167   CompositeFeaturePtr myCurrentSketch;
168
169   FeatureToAttributesMap myFeature2AttributeMap; /// a map of a feature to attributes
170
171   Handle(ModuleBase_ShapeInPlaneFilter) myPlaneFilter;
172 };
173
174
175 #endif