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