1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #ifndef PartSet_WidgetPoint2D_H
22 #define PartSet_WidgetPoint2D_H
25 #include "PartSet_MouseProcessor.h"
27 #include <ModelAPI_CompositeFeature.h>
28 #include <ModuleBase_ModelWidget.h>
32 #include <TopoDS_Shape.hxx>
33 #include <V3d_View.hxx>
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;
44 class ModuleBase_IWorkshop;
49 /**\class PartSet_WidgetPoint2D
51 * \brief Implementation of model widget to provide widget to input point 2d
52 * In XML can be defined as folloung:
54 * <sketch-2dpoint_selector id="CircleCenter" title="Center" tooltip="Center coordinates"/>
57 class PARTSET_EXPORT PartSet_WidgetPoint2D : public ModuleBase_ModelWidget,
58 public PartSet_MouseProcessor
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);
69 virtual ~PartSet_WidgetPoint2D();
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);
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);
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);
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);
96 /// Select the internal content if it can be selected. It is empty in the default realization
97 virtual void selectContent();
99 /// Returns list of widget controls
100 /// \return a control list
101 virtual QList<QWidget*> getControls() const;
103 //bool initFromPrevious(ObjectPtr theObject);
105 /// The methiod called when widget is deactivated
106 virtual void deactivate();
108 /// \returns the sketch instance
109 CompositeFeaturePtr sketch() const { return mySketch; }
111 /// Set sketch instance
112 void setSketch(CompositeFeaturePtr theSketch) { mySketch = theSketch; }
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);
120 /// Returns coordinate X currently defined in the control
123 /// Returns coordinate Y currently defined in the control
126 /// Returns true if the event is processed.
127 virtual bool processEnter();
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;
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);
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);
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);
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);
158 /// Signal about selection of an existing vertex from an object
159 void vertexSelected();
162 /// Saves the internal parameters to the given feature
163 /// \return True in success
164 virtual bool storeValueCustom();
166 /// Restore value from attribute data to the widget's control
167 virtual bool restoreValueCustom();
169 /// Store current value in cashed value
170 void storeCurentValue();
172 /// Restore cashed value in the model attribute
173 /// \return boolean state if the restored feature shoud be hidden
174 bool restoreCurentValue();
176 /// Fills the widget with default values
177 /// \return true if the widget current value is reset
178 virtual bool resetCustom();
180 /// The methiod called when widget is activated
181 virtual void activateCustom();
183 //! Switch On/Off highlighting of the widget
184 virtual void setHighlighted(bool isHighlighted);
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);
195 /// Process value changed event
196 //void onValuesChanged();
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();
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;
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);
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);
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);
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);
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);
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);
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);
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;
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);
281 ObjectPtr getGeomSelection(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue);
284 ModuleBase_IWorkshop* myWorkshop; ///< workshop
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
294 /// value used as selection in mouse release method
295 std::shared_ptr<ModuleBase_ViewerPrs> myPreSelected;
297 /// it is important during restart operation
298 CompositeFeaturePtr mySketch;
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