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>
10 #include <Config_Keywords.h>
11 #include <Config_WidgetAPI.h>
13 #include <Events_Loop.h>
15 #include <ModelAPI_Events.h>
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 #include <ModelAPI_ResultValidator.h>
22 #include <ModelAPI_RefAttrValidator.h>
26 #include <QHBoxLayout>
29 ModuleBase_WidgetFeature::ModuleBase_WidgetFeature(QWidget* theParent,
30 const Config_WidgetAPI* theData,
31 const std::string& theParentId)
32 : ModuleBase_ModelWidget(theParent, theData, theParentId)
34 myContainer = new QWidget(theParent);
35 QHBoxLayout* aControlLay = new QHBoxLayout(myContainer);
36 aControlLay->setContentsMargins(0, 0, 0, 0);
38 QString aLabelText = QString::fromStdString(theData->widgetLabel());
39 myLabel = new QLabel(aLabelText, myContainer);
40 aControlLay->addWidget(myLabel);
42 myEditor = new QLineEdit(myContainer);
43 QString anObjName = QString::fromStdString(attributeID());
44 myEditor->setObjectName(anObjName);
45 myEditor->setReadOnly(true);
46 aControlLay->addWidget(myEditor);
48 QString aTTip = QString::fromStdString(theData->widgetTooltip());
49 myEditor->setToolTip(aTTip);
51 aControlLay->addWidget(myEditor);
52 aControlLay->setStretch(1, 1);
55 ModuleBase_WidgetFeature::~ModuleBase_WidgetFeature()
59 bool ModuleBase_WidgetFeature::setValue(ModuleBase_WidgetValue* theValue)
64 ModuleBase_WidgetValueFeature* aFeatureValue =
65 dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
67 isDone = setObject(aFeatureValue->object());
72 bool ModuleBase_WidgetFeature::setObject(const ObjectPtr& theObject, bool theSendEvent)
74 SessionPtr aMgr = ModelAPI_Session::get();
75 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
76 std::list<ModelAPI_Validator*> aValidators;
77 std::list<std::list<std::string> > anArguments;
78 aFactory->validators(parentID(), attributeID(), aValidators, anArguments);
80 // Check the type of selected object
81 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
83 for (; aValidator != aValidators.end(); aValidator++) {
84 const ModelAPI_ResultValidator* aResValidator =
85 dynamic_cast<const ModelAPI_ResultValidator*>(*aValidator);
88 if (aResValidator->isValid(theObject)) {
97 // Check the acceptability of the object as attribute
98 aValidator = aValidators.begin();
99 std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
100 for (; aValidator != aValidators.end(); aValidator++, aArgs++) {
101 const ModelAPI_RefAttrValidator* aAttrValidator =
102 dynamic_cast<const ModelAPI_RefAttrValidator*>(*aValidator);
103 if (aAttrValidator) {
104 if (!aAttrValidator->isValid(myFeature, *aArgs, theObject)) {
110 myObject = theObject;
111 myEditor->setText(theObject ? theObject->data()->name().c_str() : "");
113 emit valuesChanged();
117 bool ModuleBase_WidgetFeature::storeValue() const
119 //FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
123 boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
124 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef = boost::dynamic_pointer_cast<
125 ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
127 ModuleBase_WidgetFeature* that = (ModuleBase_WidgetFeature*) this;
128 aRef->setObject(myObject);
129 myFeature->execute();
130 updateObject(myFeature);
134 bool ModuleBase_WidgetFeature::restoreValue()
136 boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
137 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef = boost::dynamic_pointer_cast<
138 ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
140 ObjectPtr anObjPtr = aRef->object();
143 myEditor->setText(myObject ? myObject->data()->name().c_str() : "");
149 QWidget* ModuleBase_WidgetFeature::getControl() const
154 QList<QWidget*> ModuleBase_WidgetFeature::getControls() const
156 QList<QWidget*> aList;
157 aList.append(myLabel);
158 aList.append(myEditor);