Salome HOME
6fd65bf96e58d58f63e7795d58013913d6ee14e4
[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 <Model_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 : ModuleBase_WidgetFeature(theParent, theData)
34 {
35 }
36
37 ModuleBase_WidgetFeatureOrAttribute::~ModuleBase_WidgetFeatureOrAttribute()
38 {
39 }
40
41 bool ModuleBase_WidgetFeatureOrAttribute::setValue(ModuleBase_WidgetValue* theValue)
42 {
43   bool isDone = false;
44
45   if (theValue) {
46     ModuleBase_WidgetValueFeature* aFeatureValue = 
47                          dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
48     if (aFeatureValue) {
49       boost::shared_ptr<GeomAPI_Pnt2d> aValuePoint = aFeatureValue->point();
50       FeaturePtr aValueFeature = aFeatureValue->feature();
51       if (aValueFeature) {
52         isDone = setFeature(aValueFeature);
53       }
54       if (!isDone && aValuePoint) {
55         // find the given point in the feature attributes
56         std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttiributes =
57                                       aValueFeature->data()->attributes(GeomDataAPI_Point2D::type());
58         std::list<boost::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
59                                                                           aLast = anAttiributes.end();
60         boost::shared_ptr<GeomDataAPI_Point2D> aFPoint;
61         for (;anIt!=aLast && !aFPoint; anIt++) {
62           boost::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
63                                               boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
64           if (aCurPoint && aCurPoint->pnt()->distance(aValuePoint) < Precision::Confusion())
65             aFPoint = aCurPoint;
66         }
67         if (aFPoint)
68           isDone = setAttribute(aFPoint);
69       }
70     }
71   }
72   return isDone;
73 }
74
75 bool ModuleBase_WidgetFeatureOrAttribute::storeValue(FeaturePtr theFeature) const
76 {
77   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
78   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
79           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
80
81   ModuleBase_WidgetFeatureOrAttribute* that = (ModuleBase_WidgetFeatureOrAttribute*) this;
82   if (feature())
83     aRef->setFeature(feature());
84   else if (myAttribute)
85     aRef->setAttr(myAttribute);
86
87   theFeature->execute();
88   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
89
90   return true;
91 }
92
93 bool ModuleBase_WidgetFeatureOrAttribute::restoreValue(FeaturePtr theFeature)
94 {
95   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
96   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
97           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
98
99   FeaturePtr aFeature = aRef->feature();
100   setFeature(aFeature);
101   myAttribute = aRef->attr();
102
103   std::string aText = "";
104   if (aFeature)
105     aText = aFeature->data()->getName().c_str();
106   else if (myAttribute)
107     aText = myAttribute->attributeType().c_str();
108
109   editor()->setText(aText.c_str());
110   return true;
111 }
112
113 bool ModuleBase_WidgetFeatureOrAttribute::setAttribute(const boost::shared_ptr<ModelAPI_Attribute>& theAttribute)
114 {
115   if (!theAttribute || !featureKinds().contains(theAttribute->attributeType().c_str()))
116     return false;
117
118   myAttribute = theAttribute;
119   editor()->setText(theAttribute ? theAttribute->attributeType().c_str() : "");
120   emit valuesChanged();
121   return true;
122 }
123