Salome HOME
cc8b8d4c2d7c536a8a50c96392bcc08140e10b66
[modules/shaper.git] / src / PartSet / PartSet_WidgetPoint2d.h
1 // Copyright (C) 2014-2023  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 #include <ModuleBase_ViewerPrs.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   /// Return reference to external objects manager
163   /// \return manager instance
164   PartSet_ExternalObjectsMgr* getExternalObjectMgr() const { return myExternalObjectMgr; }
165
166 signals:
167   /// Signal about selection of an existing vertex from an object
168   void vertexSelected();
169
170 protected:
171   /// Saves the internal parameters to the given feature
172   /// \return True in success
173   virtual bool storeValueCustom();
174
175   /// Restore value from attribute data to the widget's control
176   virtual bool restoreValueCustom();
177
178   /// Store current value in cashed value
179   void storeCurentValue();
180
181   /// Restore cashed value in the model attribute
182   /// \return boolean state if the restored feature shoud be hidden
183   bool restoreCurentValue();
184
185   /// Fills the widget with default values
186   /// \return true if the widget current value is reset
187   virtual bool resetCustom();
188
189   /// The methiod called when widget is activated
190   virtual void activateCustom();
191
192   //! Switch On/Off highlighting of the widget
193   virtual void setHighlighted(bool isHighlighted);
194
195   /// Returns true if the feature contains Point2D attribute with the same coordinates
196   /// The attribute of the widget is not processed.
197   /// \param theFeature a feature
198   /// \param theX the X coordinate
199   /// \param theY the Y coordinate
200   /// \return boolean result
201   bool isFeatureContainsPoint(const FeaturePtr& theFeature, double theX, double theY);
202
203 //private slots:
204   /// Process value changed event
205   //void onValuesChanged();
206
207   /// Compute the feature default value and fill the controls with it
208   /// or store the control value to the feature
209   /// The widget is not initialize the attribute value in order to avoid the
210   /// incorrect visualization in Sketch. E.g. by a line creation, the line should not
211   /// be visualized immediatelly when the end point widget is activated.
212   virtual void initializeValueByActivate();
213
214  private:
215    /// Creates constrains of the clicked point
216    /// \param theClickedX the horizontal coordnate of the point
217    /// \param theClickedY the vertical coordnate of the point
218    bool setConstraintToPoint(double theClickedX, double theClickedY,
219                              const std::shared_ptr<ModuleBase_ViewerPrs>& theValue);
220
221    /// Create a coincidence constraint between the attribute and the parameter object
222    /// \theObject a result object
223    /// \return true if succed
224    bool setConstraintToObject(const ObjectPtr& theObject);
225
226    /// Process selected value
227    /// \param theValue the selected object
228    /// \param theX X coordinate of clicked point
229    /// \param theY Y coordinate of clicked point
230    void processSelection(const ModuleBase_ViewerPrsPtr& theValue, double theX, double theY);
231
232
233 public:
234    /// Returns if the feature is an orphan point, circle or an arc. Returns true if it
235    /// has no a coincident to other lines. It processes point, circle and arc features
236    /// In circle an arc features, only centers are processed, for other points, it returns
237    /// that the point is not an orphan.
238    /// \param theFeature a checked feature
239    /// \param theSketch a sketch
240    /// \param theX an X coordinate of the point
241    /// \param theY an Y coordinate of the point
242    /// \return boolean result
243    static bool isOrphanPoint(const FeaturePtr& theFeature, const CompositeFeaturePtr& theSketch,
244                              double theX, double theY);
245
246    /// Explode the given shape by vertices and found closed to the point vertes
247    /// \param theShape a shape to be exploded
248    /// \param thePoint a point
249    /// \return boolean value
250    static bool shapeExploreHasVertex(const std::shared_ptr<GeomAPI_Shape>& theShape,
251                                      const std::shared_ptr<GeomAPI_Pnt2d>& thePoint,
252                                      const CompositeFeaturePtr& theSketch);
253
254   /// Finds in the feature's sub-features first Point2D attribute with the given point coordinates
255   /// \param theFeature a feature with sub-feature attributes
256   /// \param thePoint a point to provided searched coordinates
257   /// \return found point or null
258   static std::shared_ptr<GeomDataAPI_Point2D> findFirstEqualPointInArgumentFeatures(
259                   const FeaturePtr& theFeature, const std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
260
261   /// Finds in the feature first Point2D attribute with the given point coordinates
262   /// \param theFeature a feature with point attributes
263   /// \param thePoint a point to provided searched coordinates
264   /// \return found point or null
265   static std::shared_ptr<GeomDataAPI_Point2D> findFirstEqualPoint(const FeaturePtr& theFeature,
266                                        const std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
267
268   /// Finds in the sketch attribute point of any feature that is nearest to the given point
269   /// \param theSketch a sketch, that is a container of features
270   /// \param theSkipFeature a feature that do not participate in the search
271   /// \param thePoint a point to provided searched coordinates
272   /// \return found point or null
273   static std::shared_ptr<GeomDataAPI_Point2D> findFirstEqualPointInSketch(
274                                        const CompositeFeaturePtr& theSketch,
275                                        const FeaturePtr& theSkipFeature,
276                                        const std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
277
278 private:
279   /// Returns attribute reference if the key is defined in XML definition of this control
280   /// \return found attribute or null
281   std::shared_ptr<ModelAPI_AttributeRefAttr> attributeRefAttr() const;
282
283   /// Finds first equal point attribute in sketch and set it to reference attribute
284   /// \param theClickedX the horizontal coordnate of the point
285   /// \param theClickedY the vertical coordnate of the point
286   void fillRefAttribute(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue);
287   void fillRefAttribute(double theClickedX, double theClickedY,
288                         const std::shared_ptr<ModuleBase_ViewerPrs>& theValue);
289   void fillRefAttribute(const ObjectPtr& theObject);
290
291   ObjectPtr getGeomSelection(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue);
292
293 protected:
294   ModuleBase_IWorkshop* myWorkshop; ///< workshop
295
296 private:
297   QGroupBox* myGroupBox;  ///< the parent group box for all intenal widgets
298   //ModuleBase_ParamSpinBox* myXSpin;  ///< the spin box for the X coordinate
299   //ModuleBase_ParamSpinBox* myYSpin;  ///< the spin box for the Y coordinate
300   ModuleBase_LabelValue* myXSpin; ///< the label for the X coordinate
301   ModuleBase_LabelValue* myYSpin; ///< the label for the Y coordinate
302   PartSet_ExternalObjectsMgr* myExternalObjectMgr; ///< reference to external objects manager
303
304   /// value used as selection in mouse release method
305   std::shared_ptr<ModuleBase_ViewerPrs> myPreSelected;
306
307   /// it is important during restart operation
308   CompositeFeaturePtr mySketch;
309
310   std::string myRefAttribute; /// if not empty, coincidences are not set but attribute is filled
311   bool myValueIsCashed; /// boolean state if the value is cashed during value state change
312   bool myIsFeatureVisibleInCash; /// boolean value if the feature was visible when cash if filled
313   double myXValueInCash; /// the cashed X value during value state change
314   double myYValueInCash; /// the cashed Y value during value state change
315 };
316
317 #endif