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