Salome HOME
Issue #2120 - Crash when creating an arc passing through the arc connected to both...
[modules/shaper.git] / src / PartSet / PartSet_WidgetPoint2d.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_WidgetPoint2d.h
4 // Created:     25 Apr 2014
5 // Author:      Natalia ERMOLAEVA
6
7 #ifndef PartSet_WidgetPoint2D_H
8 #define PartSet_WidgetPoint2D_H
9
10 #include "PartSet.h"
11 #include "PartSet_MouseProcessor.h"
12
13 #include <ModelAPI_CompositeFeature.h>
14 #include <ModuleBase_ModelWidget.h>
15
16 #include <QObject>
17
18 #include <TopoDS_Shape.hxx>
19 #include <V3d_View.hxx>
20
21 class ModelAPI_Feature;
22 class ModelAPI_AttributeRefAttr;
23 class GeomDataAPI_Point2D;
24 class ModuleBase_IWorkshop;
25 class ModuleBase_ParamSpinBox;
26 class ModuleBase_IViewWindow;
27 class ModuleBase_LabelValue;
28 class PartSet_ExternalObjectsMgr;
29 class GeomAPI_Pnt2d;
30 class ModuleBase_IWorkshop;
31
32 class QGroupBox;
33 class QMouseEvent;
34
35 /**\class PartSet_WidgetPoint2D
36  * \ingroup Modules
37  * \brief Implementation of model widget to provide widget to input point 2d
38  * In XML can be defined as folloung:
39  * \code
40  * <sketch-2dpoint_selector id="CircleCenter" title="Center" tooltip="Center coordinates"/>
41  * \endcode
42  */
43 class PARTSET_EXPORT PartSet_WidgetPoint2D : public ModuleBase_ModelWidget,
44                                              public PartSet_MouseProcessor
45 {
46 Q_OBJECT
47  public:
48   /// Constructor
49   /// \param theParent the parent object
50   /// \param theWorkshop a current workshop
51   /// \param theData the widget configuation. The attribute of the model widget is obtained from
52   PartSet_WidgetPoint2D(QWidget* theParent, ModuleBase_IWorkshop* theWorkshop,
53                         const Config_WidgetAPI* theData);
54   /// Destructor
55   virtual ~PartSet_WidgetPoint2D();
56
57   /// Checks if the selection presentation is valid in widget
58   /// \param theValue a selected presentation in the view
59   /// \return a boolean value
60   virtual bool isValidSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue);
61
62   /// Checks all attribute validators returns valid. It tries on the given selection
63   /// to current attribute by setting the value inside and calling validators. After this,
64   /// the previous attribute value is restored.The valid/invalid value is cashed.
65   /// \param theValue a selected presentation in the view
66   /// \param theAttribute the attribute
67   /// \return a boolean value
68   bool isValidSelectionForAttribute_(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue,
69                                      const std::shared_ptr<ModelAPI_Attribute>& theAttribute);
70
71   /// Fills the attribute with the value of the selected owner
72   /// \param thePrs a selected owner
73   bool setSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue);
74
75   /// Set the given wrapped value to the current widget
76   /// This value should be processed in the widget according to the needs
77   /// \param theValues the wrapped widget values
78   /// \param theToValidate a validation flag
79   virtual bool setSelection(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
80                             const bool theToValidate);
81
82   /// Select the internal content if it can be selected. It is empty in the default realization
83   virtual void selectContent();
84
85   /// Returns list of widget controls
86   /// \return a control list
87   virtual QList<QWidget*> getControls() const;
88
89   //bool initFromPrevious(ObjectPtr theObject);
90
91   /// The methiod called when widget is deactivated
92   virtual void deactivate();
93
94   /// \returns the sketch instance
95   CompositeFeaturePtr sketch() const { return mySketch; }
96
97   /// Set sketch instance
98   void setSketch(CompositeFeaturePtr theSketch) { mySketch = theSketch; }
99
100   /// Fill the widget values by given point
101   /// \param theX the X coordinate
102   /// \param theY the Y coordinate
103   /// \returns True in case of success
104   bool setPoint(double theX, double theY);
105
106   /// Returns coordinate X currently defined in the control
107   double x() const;
108
109   /// Returns coordinate Y currently defined in the control
110   double y() const;
111
112   /// Returns true if the event is processed.
113   virtual bool processEnter();
114
115   /// Returns true if the attribute can be changed using the selected shapes in the viewer
116   /// and creating a coincidence constraint to them. This control use them.
117   virtual bool useSelectedShapes() const;
118
119   /// Processing the mouse move event in the viewer
120   /// \param theWindow a view window
121   /// \param theEvent a mouse event
122   virtual void mouseMoved(ModuleBase_IViewWindow* theWindow, QMouseEvent* theEvent);
123
124   /// Processing the mouse release event in the viewer
125   /// \param theWindow a view window
126   /// \param theEvent a mouse event
127   virtual void mouseReleased(ModuleBase_IViewWindow* theWindow, QMouseEvent* theEvent);
128
129   /// Fill preselection used in mouseReleased
130   //virtual void setPreSelection(const std::shared_ptr<ModuleBase_ViewerPrs>& thePreSelected);
131   virtual void setPreSelection(const std::shared_ptr<ModuleBase_ViewerPrs>& thePreSelected,
132                                ModuleBase_IViewWindow* theWnd,
133                                QMouseEvent* theEvent);
134
135 signals:
136   /// Signal about selection of an existing vertex from an object
137   void vertexSelected();
138
139 protected:
140   /// Saves the internal parameters to the given feature
141   /// \return True in success
142   virtual bool storeValueCustom();
143
144   /// Restore value from attribute data to the widget's control
145   virtual bool restoreValueCustom();
146
147   /// Store current value in cashed value
148   void storeCurentValue();
149
150   /// Restore cashed value in the model attribute
151   /// \return boolean state if the restored feature shoud be hidden
152   bool restoreCurentValue();
153
154   /// Fills the widget with default values
155   /// \return true if the widget current value is reset
156   virtual bool resetCustom();
157
158   /// The methiod called when widget is activated
159   virtual void activateCustom();
160
161   //! Switch On/Off highlighting of the widget
162   virtual void setHighlighted(bool isHighlighted);
163
164   /// Returns true if the feature contains Point2D attribute with the same coordinates
165   /// The attribute of the widget is not processed.
166   /// \param theFeature a feature
167   /// \param theX the X coordinate
168   /// \param theY the Y coordinate
169   /// \return boolean result
170   bool isFeatureContainsPoint(const FeaturePtr& theFeature, double theX, double theY);
171
172 //private slots:
173   /// Process value changed event
174   //void onValuesChanged();
175
176   /// Compute the feature default value and fill the controls with it
177   /// or store the control value to the feature
178   /// The widget is not initialize the attribute value in order to avoid the
179   /// incorrect visualization in Sketch. E.g. by a line creation, the line should not
180   /// be visualized immediatelly when the end point widget is activated.
181   virtual void initializeValueByActivate();
182
183  private:
184    /// Returns point 2d from selected vertex
185    /// \param theView a view window
186    /// \param theShape a vertex shape
187    /// \param theX an output value of X coordinate
188    /// \param theY an output value of Y coordinate
189    bool getPoint2d(const Handle(V3d_View)& theView, const TopoDS_Shape& theShape,
190                    double& theX, double& theY) const;
191
192    /// Creates constrains of the clicked point
193    /// \param theClickedX the horizontal coordnate of the point
194    /// \param theClickedY the vertical coordnate of the point
195    bool setConstraintToPoint(double theClickedX, double theClickedY,
196                              const std::shared_ptr<ModuleBase_ViewerPrs>& theValue);
197
198    /// Create a coincidence constraint between the attribute and the parameter object
199    /// \theObject a result object
200    /// \return true if succed
201    bool setConstraintToObject(const ObjectPtr& theObject);
202
203    /// Returns if the feature is an orphan point, circle or an arc. Returns true if it
204    /// has no a coincident to other lines. It processes point, circle and arc features
205    /// In circle an arc features, only centers are processed, for other points, it returns
206    /// that the point is not an orphan.
207    /// \param theFeature a checked feature
208    /// \param theSketch a sketch
209    /// \param theX an X coordinate of the point
210    /// \param theY an Y coordinate of the point
211    /// \return boolean result
212    static bool isOrphanPoint(const FeaturePtr& theFeature, const CompositeFeaturePtr& theSketch,
213                              double theX, double theY);
214
215    /// Explode the given shape by vertices and found closed to the point vertes
216    /// \param theShape a shape to be exploded
217    /// \param thePoint a point
218    /// \return boolean value
219    static bool shapeExploreHasVertex(const std::shared_ptr<GeomAPI_Shape>& theShape,
220                                      const std::shared_ptr<GeomAPI_Pnt2d>& thePoint,
221                                      const CompositeFeaturePtr& theSketch);
222
223   /// Finds in the feature's sub-features first Point2D attribute with the given point coordinates
224   /// \param theFeature a feature with sub-feature attributes
225   /// \param thePoint a point to provided searched coordinates
226   /// \return found point or null
227   static std::shared_ptr<GeomDataAPI_Point2D> findFirstEqualPointInArgumentFeatures(
228                   const FeaturePtr& theFeature, const std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
229
230   /// Finds in the feature first Point2D attribute with the given point coordinates
231   /// \param theFeature a feature with point attributes
232   /// \param thePoint a point to provided searched coordinates
233   /// \return found point or null
234   static std::shared_ptr<GeomDataAPI_Point2D> findFirstEqualPoint(const FeaturePtr& theFeature,
235                                        const std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
236
237   /// Finds in the sketch attribute point of any feature that is nearest to the given point
238   /// \param theSketch a sketch, that is a container of features
239   /// \param theSkipFeature a feature that do not participate in the search
240   /// \param thePoint a point to provided searched coordinates
241   /// \return found point or null
242   static std::shared_ptr<GeomDataAPI_Point2D> findFirstEqualPointInSketch(
243                                        const CompositeFeaturePtr& theSketch,
244                                        const FeaturePtr& theSkipFeature,
245                                        const std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
246
247   /// Returns attribute reference if the key is defined in XML definition of this control
248   /// \return found attribute or null
249   std::shared_ptr<ModelAPI_AttributeRefAttr> attributeRefAttr() const;
250
251   /// Finds first equal point attribute in sketch and set it to reference attribute
252   /// \param theClickedX the horizontal coordnate of the point
253   /// \param theClickedY the vertical coordnate of the point
254   void fillRefAttribute(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue);
255   void fillRefAttribute(double theClickedX, double theClickedY,
256                         const std::shared_ptr<ModuleBase_ViewerPrs>& theValue);
257   void fillRefAttribute(const ObjectPtr& theObject);
258
259   ObjectPtr getGeomSelection(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue);
260
261 protected:
262   ModuleBase_IWorkshop* myWorkshop; ///< workshop
263
264 private:
265   QGroupBox* myGroupBox;  ///< the parent group box for all intenal widgets
266   //ModuleBase_ParamSpinBox* myXSpin;  ///< the spin box for the X coordinate
267   //ModuleBase_ParamSpinBox* myYSpin;  ///< the spin box for the Y coordinate
268   ModuleBase_LabelValue* myXSpin; ///< the label for the X coordinate
269   ModuleBase_LabelValue* myYSpin; ///< the label for the Y coordinate
270   PartSet_ExternalObjectsMgr* myExternalObjectMgr; ///< reference to external objects manager
271
272   /// value used as selection in mouse release method
273   std::shared_ptr<ModuleBase_ViewerPrs> myPreSelected;
274
275   /// it is important during restart operation
276   CompositeFeaturePtr mySketch;
277
278   std::string myRefAttribute; /// if not empty, coincidences are not set but attribute is filled
279   bool myValueIsCashed; /// boolean state if the value is cashed during value state change
280   bool myIsFeatureVisibleInCash; /// boolean value if the feature was visible when cash if filled
281   double myXValueInCash; /// the cashed X value during value state change
282   double myYValueInCash; /// the cashed Y value during value state change
283 };
284
285 #endif