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