Salome HOME
Provide distance between points of lines
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetFeatureOrAttribute.cpp
1 // File:        ModuleBase_WidgetFeatureOrAttribute.cpp
2 // Created:     25 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <ModuleBase_WidgetFeatureOrAttribute.h>
6
7 #include <ModuleBase_WidgetValueFeature.h>
8 #include <ModuleBase_WidgetValue.h>
9 #include <ModuleBase_Tools.h>
10
11 #include <Config_Keywords.h>
12 #include <Config_WidgetAPI.h>
13
14 #include <Events_Loop.h>
15 #include <ModelAPI_Events.h>
16
17 #include <ModelAPI_Feature.h>
18 #include <ModelAPI_Data.h>
19 #include <ModelAPI_Object.h>
20 #include <ModelAPI_AttributeRefAttr.h>
21 #include <GeomAPI_Pnt2d.h>
22
23 #include <GeomDataAPI_Point2D.h>
24
25 #include <Precision.hxx>
26
27 #include <QWidget>
28 #include <QLineEdit>
29 #include <QHBoxLayout>
30 #include <QLabel>
31
32 ModuleBase_WidgetFeatureOrAttribute::ModuleBase_WidgetFeatureOrAttribute(QWidget* theParent,
33                                                    const Config_WidgetAPI* theData, 
34                                                    const std::string& theParentId)
35 : ModuleBase_WidgetFeature(theParent, theData, theParentId)
36 {
37 }
38
39 ModuleBase_WidgetFeatureOrAttribute::~ModuleBase_WidgetFeatureOrAttribute()
40 {
41 }
42
43 bool ModuleBase_WidgetFeatureOrAttribute::setValue(ModuleBase_WidgetValue* theValue)
44 {
45   bool isDone = false;
46
47   if (theValue) {
48     ModuleBase_WidgetValueFeature* aFeatureValue = 
49                          dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
50     if (aFeatureValue) {
51       boost::shared_ptr<GeomAPI_Pnt2d> aValuePoint = aFeatureValue->point();
52       ObjectPtr aObject = aFeatureValue->object();
53       if (aObject) {
54         isDone = setObject(aObject);
55       }
56       if (!isDone && aValuePoint) {
57         FeaturePtr aFeature = ModuleBase_Tools::feature(aObject);
58         if (aFeature) {
59           // find the given point in the feature attributes
60           std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttiributes =
61                                         aFeature->data()->attributes(GeomDataAPI_Point2D::type());
62           std::list<boost::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
63                                                                             aLast = anAttiributes.end();
64           boost::shared_ptr<GeomDataAPI_Point2D> aFPoint;
65           for (;anIt!=aLast && !aFPoint; anIt++) {
66             boost::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
67                                                 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
68             if (aCurPoint && aCurPoint->pnt()->distance(aValuePoint) < Precision::Confusion())
69               aFPoint = aCurPoint;
70           }
71           if (aFPoint)
72             isDone = setAttribute(aFPoint);
73         }
74       }
75     }
76   }
77   return isDone;
78 }
79
80 bool ModuleBase_WidgetFeatureOrAttribute::storeValue(ObjectPtr theFeature) const
81 {
82   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theFeature);
83   if (!aFeature)
84     return false;
85   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
86   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
87           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
88
89   ModuleBase_WidgetFeatureOrAttribute* that = (ModuleBase_WidgetFeatureOrAttribute*) this;
90   if (object())
91     aRef->setObject(object());
92   else if (myAttribute)
93     aRef->setAttr(myAttribute);
94
95   aFeature->execute();
96   updateObject(theFeature);
97
98   return true;
99 }
100
101 bool ModuleBase_WidgetFeatureOrAttribute::restoreValue(ObjectPtr theFeature)
102 {
103   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
104   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
105           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
106
107   FeaturePtr aFeature = ModuleBase_Tools::feature(aRef->object());
108   if (aFeature) {
109     setObject(aFeature);
110     myAttribute = aRef->attr();
111
112     std::string aText = "";
113     if (aFeature)
114       aText = aFeature->data()->name().c_str();
115     else if (myAttribute)
116       aText = myAttribute->attributeType().c_str();
117
118     editor()->setText(aText.c_str());
119     return true;
120   }
121   return false;
122 }
123
124 bool ModuleBase_WidgetFeatureOrAttribute::setAttribute(const boost::shared_ptr<ModelAPI_Attribute>& theAttribute)
125 {
126   if (!theAttribute)// || !featureKinds().contains(theAttribute->attributeType().c_str()))
127     return false;
128
129   myAttribute = theAttribute;
130   editor()->setText(theAttribute ? theAttribute->attributeType().c_str() : "");
131   emit valuesChanged();
132   return true;
133 }
134