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