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 /// 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);
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);
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);
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);
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);
101 /// Select the internal content if it can be selected. It is empty in the default realization
102 virtual void selectContent();
104 /// Returns list of widget controls
105 /// \return a control list
106 virtual QList<QWidget*> getControls() const;
108 //bool initFromPrevious(ObjectPtr theObject);
110 /// The methiod called when widget is deactivated
111 virtual void deactivate();
113 /// \returns the sketch instance
114 CompositeFeaturePtr sketch() const { return mySketch; }
116 /// Set sketch instance
117 void setSketch(CompositeFeaturePtr theSketch) { mySketch = theSketch; }
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);
125 /// Returns coordinate X currently defined in the control
128 /// Returns coordinate Y currently defined in the control
131 /// Returns true if the event is processed.
132 virtual bool processEnter();
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;
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);
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);
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);
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);
162 /// Return reference to external objects manager
163 /// \return manager instance
164 PartSet_ExternalObjectsMgr* getExternalObjectMgr() const { return myExternalObjectMgr; }
167 /// Signal about selection of an existing vertex from an object
168 void vertexSelected();
171 /// Saves the internal parameters to the given feature
172 /// \return True in success
173 virtual bool storeValueCustom();
175 /// Restore value from attribute data to the widget's control
176 virtual bool restoreValueCustom();
178 /// Store current value in cashed value
179 void storeCurentValue();
181 /// Restore cashed value in the model attribute
182 /// \return boolean state if the restored feature shoud be hidden
183 bool restoreCurentValue();
185 /// Fills the widget with default values
186 /// \return true if the widget current value is reset
187 virtual bool resetCustom();
189 /// The methiod called when widget is activated
190 virtual void activateCustom();
192 //! Switch On/Off highlighting of the widget
193 virtual void setHighlighted(bool isHighlighted);
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);
204 /// Process value changed event
205 //void onValuesChanged();
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();
215 /// Returns point 2d from selected vertex
216 /// \param theView a view window
217 /// \param theShape a vertex shape
218 /// \param theX an output value of X coordinate
219 /// \param theY an output value of Y coordinate
220 bool getPoint2d(const Handle(V3d_View)& theView, const TopoDS_Shape& theShape,
221 double& theX, double& theY) const;
223 /// Creates constrains of the clicked point
224 /// \param theClickedX the horizontal coordnate of the point
225 /// \param theClickedY the vertical coordnate of the point
226 bool setConstraintToPoint(double theClickedX, double theClickedY,
227 const std::shared_ptr<ModuleBase_ViewerPrs>& theValue);
229 /// Create a coincidence constraint between the attribute and the parameter object
230 /// \theObject a result object
231 /// \return true if succed
232 bool setConstraintToObject(const ObjectPtr& theObject);
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);
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);
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);
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);
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);
278 /// Returns attribute reference if the key is defined in XML definition of this control
279 /// \return found attribute or null
280 std::shared_ptr<ModelAPI_AttributeRefAttr> attributeRefAttr() const;
282 /// Finds first equal point attribute in sketch and set it to reference attribute
283 /// \param theClickedX the horizontal coordnate of the point
284 /// \param theClickedY the vertical coordnate of the point
285 void fillRefAttribute(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue);
286 void fillRefAttribute(double theClickedX, double theClickedY,
287 const std::shared_ptr<ModuleBase_ViewerPrs>& theValue);
288 void fillRefAttribute(const ObjectPtr& theObject);
290 ObjectPtr getGeomSelection(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue);
293 ModuleBase_IWorkshop* myWorkshop; ///< workshop
296 QGroupBox* myGroupBox; ///< the parent group box for all intenal widgets
297 //ModuleBase_ParamSpinBox* myXSpin; ///< the spin box for the X coordinate
298 //ModuleBase_ParamSpinBox* myYSpin; ///< the spin box for the Y coordinate
299 ModuleBase_LabelValue* myXSpin; ///< the label for the X coordinate
300 ModuleBase_LabelValue* myYSpin; ///< the label for the Y coordinate
301 PartSet_ExternalObjectsMgr* myExternalObjectMgr; ///< reference to external objects manager
303 /// value used as selection in mouse release method
304 std::shared_ptr<ModuleBase_ViewerPrs> myPreSelected;
306 /// it is important during restart operation
307 CompositeFeaturePtr mySketch;
309 std::string myRefAttribute; /// if not empty, coincidences are not set but attribute is filled
310 bool myValueIsCashed; /// boolean state if the value is cashed during value state change
311 bool myIsFeatureVisibleInCash; /// boolean value if the feature was visible when cash if filled
312 double myXValueInCash; /// the cashed X value during value state change
313 double myYValueInCash; /// the cashed Y value during value state change