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