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