1 // File: ModuleBase_WidgetFeatureOrAttribute.cpp
2 // Created: 25 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include <ModuleBase_WidgetFeatureOrAttribute.h>
7 #include <ModuleBase_WidgetValueFeature.h>
8 #include <ModuleBase_WidgetValue.h>
10 #include <ModelAPI_RefAttrValidator.h>
11 #include <ModelAPI_Session.h>
13 #include <Config_Keywords.h>
14 #include <Config_WidgetAPI.h>
16 #include <Events_Loop.h>
17 #include <ModelAPI_Events.h>
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>
25 #include <GeomAPI_Pnt2d.h>
27 #include <GeomDataAPI_Point2D.h>
29 #include <Precision.hxx>
33 #include <QHBoxLayout>
36 ModuleBase_WidgetFeatureOrAttribute::ModuleBase_WidgetFeatureOrAttribute(QWidget* theParent,
37 const Config_WidgetAPI* theData,
38 const std::string& theParentId)
39 : ModuleBase_WidgetFeature(theParent, theData, theParentId)
43 ModuleBase_WidgetFeatureOrAttribute::~ModuleBase_WidgetFeatureOrAttribute()
47 bool ModuleBase_WidgetFeatureOrAttribute::setValue(ModuleBase_WidgetValue* theValue)
52 ModuleBase_WidgetValueFeature* aFeatureValue =
53 dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
55 ObjectPtr aObject = aFeatureValue->object();
57 boost::shared_ptr<ModelAPI_Attribute> anAttribute = findAttribute(aFeatureValue);
59 isDone = setAttribute(anAttribute, false);
62 isDone = setObject(aObject, false);
72 bool ModuleBase_WidgetFeatureOrAttribute::storeValue() const
74 //FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theFeature);
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()));
83 aRef->setObject(myObject);
85 aRef->setAttr(myAttribute);
88 updateObject(myFeature);
93 bool ModuleBase_WidgetFeatureOrAttribute::restoreValue()
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()));
99 ObjectPtr aObj = aRef->object();
100 FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
103 myAttribute = aRef->attr();
105 std::string aText = "";
107 aText = aFeature->data()->name().c_str();
109 aText = myAttribute->attributeType().c_str();
111 editor()->setText(aText.c_str());
117 boost::shared_ptr<ModelAPI_Attribute> ModuleBase_WidgetFeatureOrAttribute::findAttribute(
118 ModuleBase_WidgetValue* theValue)
120 boost::shared_ptr<ModelAPI_Attribute> anAttribute;
121 ModuleBase_WidgetValueFeature* aFeatureValue =
122 dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
126 boost::shared_ptr<GeomAPI_Pnt2d> aValuePoint = aFeatureValue->point();
128 ObjectPtr aObject = aFeatureValue->object();
129 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
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;
147 bool ModuleBase_WidgetFeatureOrAttribute::setAttribute(
148 const boost::shared_ptr<ModelAPI_Attribute>& theAttribute, bool theSendEvent)
150 if (!theAttribute) // || !featureKinds().contains(theAttribute->attributeType().c_str()))
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);
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)) {
173 myAttribute = theAttribute;
174 editor()->setText(theAttribute ? theAttribute->attributeType().c_str() : "");
176 emit valuesChanged();