Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetPoint2dDistance.cpp
1 // File:        ModuleBase_WidgetPoint2dDistance.h
2 // Created:     23 June 2014
3 // Author:      Vitaly Smetannikov
4
5 #include <ModuleBase_WidgetPoint2dDistance.h>
6 #include <ModuleBase_WidgetValueFeature.h>
7 #include <ModuleBase_DoubleSpinBox.h>
8
9 #include <GeomAPI_Pnt2d.h>
10 #include <Config_WidgetAPI.h>
11 #include <GeomDataAPI_Point2D.h>
12
13 #include <ModelAPI_Data.h>
14 #include <ModelAPI_AttributeDouble.h>
15
16 ModuleBase_WidgetPoint2dDistance::ModuleBase_WidgetPoint2dDistance(QWidget* theParent,
17                                                                    const Config_WidgetAPI* theData,
18                                                                    const std::string& theParentId)
19     : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId)
20 {
21   myFirstPntName = theData->getProperty("first_point");
22 }
23
24 ModuleBase_WidgetPoint2dDistance::~ModuleBase_WidgetPoint2dDistance()
25 {
26 }
27
28 bool ModuleBase_WidgetPoint2dDistance::setValue(ModuleBase_WidgetValue* theValue)
29 {
30   bool isDone = false;
31
32   if (theValue) {
33     ModuleBase_WidgetValueFeature* aFeatureValue =
34         dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
35     if (aFeatureValue) {
36       boost::shared_ptr<GeomAPI_Pnt2d> aPnt = aFeatureValue->point();
37       ObjectPtr aObject = aFeatureValue->object();
38       FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
39       if (aFeature && aPnt) {
40         setPoint(aFeature, aPnt);
41         isDone = true;
42       }
43     }
44   }
45   return isDone;
46 }
47
48 void ModuleBase_WidgetPoint2dDistance::setPoint(FeaturePtr theFeature,
49                                                 const boost::shared_ptr<GeomAPI_Pnt2d>& thePnt)
50 {
51   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
52   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
53       aData->attribute(myFirstPntName));
54   if (!aPoint)
55     return;
56
57   double aRadius = thePnt->distance(aPoint->pnt());
58   AttributeDoublePtr aReal = aData->real(attributeID());
59   if (aReal && (aReal->value() != aRadius)) {
60     aReal->setValue(aRadius);
61     mySpinBox->setValue(aRadius);
62   }
63 }