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