Salome HOME
Sources formated according to the codeing standards
[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(
34     QWidget* theParent, const Config_WidgetAPI* theData, 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, false);
55       }
56       if (aValuePoint) {
57         FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
58         if (aFeature) {
59           // find the given point in the feature attributes
60           std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttiributes = aFeature->data()
61               ->attributes(GeomDataAPI_Point2D::type());
62           std::list<boost::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes
63               .begin(), aLast = anAttiributes.end();
64           boost::shared_ptr<GeomDataAPI_Point2D> aFPoint;
65           for (; anIt != aLast && !aFPoint; anIt++) {
66             boost::shared_ptr<GeomDataAPI_Point2D> aCurPoint = boost::dynamic_pointer_cast<
67                 GeomDataAPI_Point2D>(*anIt);
68             if (aCurPoint && aCurPoint->pnt()->distance(aValuePoint) < Precision::Confusion())
69               aFPoint = aCurPoint;
70           }
71           if (aFPoint)
72             isDone = setAttribute(aFPoint, false);
73         }
74       }
75       if (isDone)
76         emit valuesChanged();
77     }
78   }
79   return isDone;
80 }
81
82 bool ModuleBase_WidgetFeatureOrAttribute::storeValue() const
83 {
84   //FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theFeature);
85   //if (!aFeature)
86   //  return false;
87
88   boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
89   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef = boost::dynamic_pointer_cast<
90       ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
91
92   if (myObject)
93     aRef->setObject(myObject);
94   if (myAttribute)
95     aRef->setAttr(myAttribute);
96
97   myFeature->execute();
98   updateObject(myFeature);
99
100   return true;
101 }
102
103 bool ModuleBase_WidgetFeatureOrAttribute::restoreValue()
104 {
105   boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
106   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef = boost::dynamic_pointer_cast<
107       ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
108
109   ObjectPtr aObj = aRef->object();
110   FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
111   if (aFeature) {
112     myObject = aFeature;
113     myAttribute = aRef->attr();
114
115     std::string aText = "";
116     if (aFeature)
117       aText = aFeature->data()->name().c_str();
118     if (myAttribute)
119       aText = myAttribute->attributeType().c_str();
120
121     editor()->setText(aText.c_str());
122     return true;
123   }
124   return false;
125 }
126
127 bool ModuleBase_WidgetFeatureOrAttribute::setAttribute(
128     const boost::shared_ptr<ModelAPI_Attribute>& theAttribute, bool theSendEvent)
129 {
130   if (!theAttribute)  // || !featureKinds().contains(theAttribute->attributeType().c_str()))
131     return false;
132
133   myAttribute = theAttribute;
134   editor()->setText(theAttribute ? theAttribute->attributeType().c_str() : "");
135   if (theSendEvent)
136     emit valuesChanged();
137   return true;
138 }
139