1 // File: ModuleBase_WidgetFeature.cpp
2 // Created: 25 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include <ModuleBase_WidgetFeature.h>
7 #include <ModuleBase_WidgetValueFeature.h>
8 #include <ModuleBase_WidgetValue.h>
9 #include <ModuleBase_Tools.h>
11 #include <Config_Keywords.h>
12 #include <Config_WidgetAPI.h>
14 #include <Events_Loop.h>
16 #include <ModelAPI_Events.h>
17 #include <ModelAPI_Feature.h>
18 #include <ModelAPI_Data.h>
19 #include <ModelAPI_Object.h>
20 #include <ModelAPI_AttributeRefAttr.h>
21 #include <ModelAPI_Validator.h>
22 #include <ModelAPI_ResultValidator.h>
23 #include <ModelAPI_RefAttrValidator.h>
24 #include <ModelAPI_Session.h>
28 #include <QHBoxLayout>
31 ModuleBase_WidgetFeature::ModuleBase_WidgetFeature(QWidget* theParent,
32 const Config_WidgetAPI* theData,
33 const std::string& theParentId)
34 : ModuleBase_ModelWidget(theParent, theData, theParentId)
36 myContainer = new QWidget(theParent);
37 QHBoxLayout* aControlLay = new QHBoxLayout(myContainer);
38 ModuleBase_Tools::adjustMargins(aControlLay);
40 QString aLabelText = QString::fromStdString(theData->widgetLabel());
41 myLabel = new QLabel(aLabelText, myContainer);
42 aControlLay->addWidget(myLabel);
44 myEditor = new QLineEdit(myContainer);
45 QString anObjName = QString::fromStdString(attributeID());
46 myEditor->setObjectName(anObjName);
47 myEditor->setReadOnly(true);
48 aControlLay->addWidget(myEditor);
50 QString aTTip = QString::fromStdString(theData->widgetTooltip());
51 myEditor->setToolTip(aTTip);
52 aControlLay->addWidget(myEditor);
53 aControlLay->setStretch(1, 1);
56 ModuleBase_WidgetFeature::~ModuleBase_WidgetFeature()
60 bool ModuleBase_WidgetFeature::setValue(ModuleBase_WidgetValue* theValue)
65 ModuleBase_WidgetValueFeature* aFeatureValue =
66 dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
68 isDone = setObject(aFeatureValue->object());
73 bool ModuleBase_WidgetFeature::setObject(const ObjectPtr& theObject, bool theSendEvent)
75 SessionPtr aMgr = ModelAPI_Session::get();
76 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
77 std::list<ModelAPI_Validator*> aValidators;
78 std::list<std::list<std::string> > anArguments;
79 aFactory->validators(parentID(), attributeID(), aValidators, anArguments);
81 // Check the type of selected object
82 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
84 for (; aValidator != aValidators.end(); aValidator++) {
85 const ModelAPI_ResultValidator* aResValidator =
86 dynamic_cast<const ModelAPI_ResultValidator*>(*aValidator);
89 if (aResValidator->isValid(theObject)) {
98 // Check the acceptability of the object as attribute
99 aValidator = aValidators.begin();
100 std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
101 for (; aValidator != aValidators.end(); aValidator++, aArgs++) {
102 const ModelAPI_RefAttrValidator* aAttrValidator =
103 dynamic_cast<const ModelAPI_RefAttrValidator*>(*aValidator);
104 if (aAttrValidator) {
105 if (!aAttrValidator->isValid(myFeature, *aArgs, theObject)) {
111 myObject = theObject;
112 myEditor->setText(theObject ? theObject->data()->name().c_str() : "");
114 emit valuesChanged();
118 bool ModuleBase_WidgetFeature::storeValue() const
120 //FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
124 boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
125 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef = boost::dynamic_pointer_cast<
126 ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
128 ModuleBase_WidgetFeature* that = (ModuleBase_WidgetFeature*) this;
129 aRef->setObject(myObject);
130 myFeature->execute();
131 updateObject(myFeature);
135 bool ModuleBase_WidgetFeature::restoreValue()
137 boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
138 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef = boost::dynamic_pointer_cast<
139 ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
141 ObjectPtr anObjPtr = aRef->object();
144 myEditor->setText(myObject ? myObject->data()->name().c_str() : "");
150 QWidget* ModuleBase_WidgetFeature::getControl() const
155 QList<QWidget*> ModuleBase_WidgetFeature::getControls() const
157 QList<QWidget*> aList;
158 aList.append(myLabel);
159 aList.append(myEditor);