Salome HOME
Issue #244 : do not allow to select null objects (coming from other documents) yet
[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 <ModelAPI_RefAttrValidator.h>
11 #include <ModelAPI_Session.h>
12
13 #include <Config_Keywords.h>
14 #include <Config_WidgetAPI.h>
15
16 #include <Events_Loop.h>
17 #include <ModelAPI_Events.h>
18
19 #include <ModelAPI_Feature.h>
20 #include <ModelAPI_Data.h>
21 #include <ModelAPI_Object.h>
22 #include <ModelAPI_AttributeRefAttr.h>
23 #include <ModelAPI_Validator.h>
24
25 #include <GeomAPI_Pnt2d.h>
26
27 #include <GeomDataAPI_Point2D.h>
28
29 #include <Precision.hxx>
30
31 #include <QWidget>
32 #include <QLineEdit>
33 #include <QHBoxLayout>
34 #include <QLabel>
35
36 ModuleBase_WidgetFeatureOrAttribute::ModuleBase_WidgetFeatureOrAttribute(QWidget* theParent, 
37                                                                          const Config_WidgetAPI* theData, 
38                                                                          const std::string& theParentId)
39     : ModuleBase_WidgetFeature(theParent, theData, theParentId)
40 {
41 }
42
43 ModuleBase_WidgetFeatureOrAttribute::~ModuleBase_WidgetFeatureOrAttribute()
44 {
45 }
46
47 bool ModuleBase_WidgetFeatureOrAttribute::setValue(ModuleBase_WidgetValue* theValue)
48 {
49   bool isDone = false;
50
51   if (theValue) {
52     ModuleBase_WidgetValueFeature* aFeatureValue =
53         dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
54     if (aFeatureValue) {
55       ObjectPtr aObject = aFeatureValue->object();
56
57       boost::shared_ptr<ModelAPI_Attribute> anAttribute = findAttribute(aFeatureValue);
58       if (anAttribute) {
59         isDone = setAttribute(anAttribute, false);
60       }
61       else if (aObject) {
62         isDone = setObject(aObject, false);
63       }
64
65       if (isDone)
66         emit valuesChanged();
67     }
68   }
69   return isDone;
70 }
71
72 bool ModuleBase_WidgetFeatureOrAttribute::storeValue() const
73 {
74   //FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theFeature);
75   //if (!aFeature)
76   //  return false;
77
78   boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
79   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef = boost::dynamic_pointer_cast<
80       ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
81
82   if (myObject)
83     aRef->setObject(myObject);
84   if (myAttribute)
85     aRef->setAttr(myAttribute);
86
87   myFeature->execute();
88   updateObject(myFeature);
89
90   return true;
91 }
92
93 bool ModuleBase_WidgetFeatureOrAttribute::restoreValue()
94 {
95   boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
96   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef = boost::dynamic_pointer_cast<
97       ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
98
99   ObjectPtr aObj = aRef->object();
100   FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
101   if (aFeature) {
102     myObject = aFeature;
103     myAttribute = aRef->attr();
104
105     std::string aText = "";
106     if (aFeature)
107       aText = aFeature->data()->name().c_str();
108     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 boost::shared_ptr<ModelAPI_Attribute> ModuleBase_WidgetFeatureOrAttribute::findAttribute(
118                                                         ModuleBase_WidgetValue* theValue)
119 {
120   boost::shared_ptr<ModelAPI_Attribute> anAttribute;
121   ModuleBase_WidgetValueFeature* aFeatureValue =
122                                   dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
123   if (!aFeatureValue)
124     return anAttribute;
125
126   boost::shared_ptr<GeomAPI_Pnt2d> aValuePoint = aFeatureValue->point();
127   if (aValuePoint) {
128     ObjectPtr aObject = aFeatureValue->object();
129     FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
130     if (aFeature) {
131       // find the given point in the feature attributes
132       std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttiributes = aFeature->data()
133           ->attributes(GeomDataAPI_Point2D::type());
134       std::list<boost::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes
135           .begin(), aLast = anAttiributes.end();
136       for (; anIt != aLast && !anAttribute; anIt++) {
137         boost::shared_ptr<GeomDataAPI_Point2D> aCurPoint = boost::dynamic_pointer_cast<
138             GeomDataAPI_Point2D>(*anIt);
139         if (aCurPoint && aCurPoint->pnt()->distance(aValuePoint) < Precision::Confusion())
140           anAttribute = aCurPoint;
141       }
142     }
143   }
144   return anAttribute;
145 }
146
147 bool ModuleBase_WidgetFeatureOrAttribute::setAttribute(
148     const boost::shared_ptr<ModelAPI_Attribute>& theAttribute, bool theSendEvent)
149 {
150   if (!theAttribute)  // || !featureKinds().contains(theAttribute->attributeType().c_str()))
151     return false;
152
153   SessionPtr aMgr = ModelAPI_Session::get();
154   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
155   std::list<ModelAPI_Validator*> aValidators;
156   std::list<std::list<std::string> > anArguments;
157   aFactory->validators(parentID(), attributeID(), aValidators, anArguments);
158
159   // Check the acceptability of the attribute
160   std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
161   int aSize = aValidators.size();
162   std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
163   for (; aValidator != aValidators.end(); aValidator++, aArgs++) {
164     const ModelAPI_RefAttrValidator* aAttrValidator =
165         dynamic_cast<const ModelAPI_RefAttrValidator*>(*aValidator);
166     if (aAttrValidator) {
167       if (!aAttrValidator->isValid(myFeature, *aArgs, theAttribute)) {
168         return false;
169       }
170     }
171   }
172
173   myAttribute = theAttribute;
174   editor()->setText(theAttribute ? theAttribute->attributeType().c_str() : "");
175   if (theSendEvent)
176     emit valuesChanged();
177   return true;
178 }
179