Salome HOME
fcc226b3ea4cc67660f9edd0dc78992a95c30f01
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetFeature.cpp
1 // File:        ModuleBase_WidgetFeature.cpp
2 // Created:     25 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <ModuleBase_WidgetFeature.h>
6
7 #include <ModuleBase_WidgetValueFeature.h>
8 #include <ModuleBase_WidgetValue.h>
9 #include <ModuleBase_Tools.h>
10
11 #include <Config_Keywords.h>
12 #include <Config_WidgetAPI.h>
13
14 #include <Events_Loop.h>
15
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
25 #include <QWidget>
26 #include <QLineEdit>
27 #include <QHBoxLayout>
28 #include <QLabel>
29
30 ModuleBase_WidgetFeature::ModuleBase_WidgetFeature(QWidget* theParent,
31                                                    const Config_WidgetAPI* theData,
32                                                    const std::string& theParentId)
33     : ModuleBase_ModelWidget(theParent, theData, theParentId)
34 {
35   myContainer = new QWidget(theParent);
36   QHBoxLayout* aControlLay = new QHBoxLayout(myContainer);
37   ModuleBase_Tools::adjustMargins(aControlLay);
38
39   QString aLabelText = QString::fromStdString(theData->widgetLabel());
40   myLabel = new QLabel(aLabelText, myContainer);
41   aControlLay->addWidget(myLabel);
42
43   myEditor = new QLineEdit(myContainer);
44   QString anObjName = QString::fromStdString(attributeID());
45   myEditor->setObjectName(anObjName);
46   myEditor->setReadOnly(true);
47   aControlLay->addWidget(myEditor);
48
49   QString aTTip = QString::fromStdString(theData->widgetTooltip());
50   myEditor->setToolTip(aTTip);
51   aControlLay->addWidget(myEditor);
52   aControlLay->setStretch(1, 1);
53 }
54
55 ModuleBase_WidgetFeature::~ModuleBase_WidgetFeature()
56 {
57 }
58
59 bool ModuleBase_WidgetFeature::setValue(ModuleBase_WidgetValue* theValue)
60 {
61   bool isDone = false;
62
63   if (theValue) {
64     ModuleBase_WidgetValueFeature* aFeatureValue =
65         dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
66     if (aFeatureValue)
67       isDone = setObject(aFeatureValue->object());
68   }
69   return isDone;
70 }
71
72 bool ModuleBase_WidgetFeature::setObject(const ObjectPtr& theObject, bool theSendEvent)
73 {
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);
79
80   // Check the type of selected object
81   std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
82   bool isValid = true;
83   for (; aValidator != aValidators.end(); aValidator++) {
84     const ModelAPI_ResultValidator* aResValidator =
85         dynamic_cast<const ModelAPI_ResultValidator*>(*aValidator);
86     if (aResValidator) {
87       isValid = false;
88       if (aResValidator->isValid(theObject)) {
89         isValid = true;
90         break;
91       }
92     }
93   }
94   if (!isValid)
95     return false;
96
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)) {
105         return false;
106       }
107     }
108   }
109
110   myObject = theObject;
111   myEditor->setText(theObject ? theObject->data()->name().c_str() : "");
112   if (theSendEvent)
113     emit valuesChanged();
114   return true;
115 }
116
117 bool ModuleBase_WidgetFeature::storeValue() const
118 {
119   //FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
120   if (!myObject)
121     return false;
122
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()));
126
127   ModuleBase_WidgetFeature* that = (ModuleBase_WidgetFeature*) this;
128   aRef->setObject(myObject);
129   myFeature->execute();
130   updateObject(myFeature);
131   return true;
132 }
133
134 bool ModuleBase_WidgetFeature::restoreValue()
135 {
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()));
139
140   ObjectPtr anObjPtr = aRef->object();
141   if (anObjPtr) {
142     myObject = anObjPtr;
143     myEditor->setText(myObject ? myObject->data()->name().c_str() : "");
144     return true;
145   }
146   return false;
147 }
148
149 QWidget* ModuleBase_WidgetFeature::getControl() const
150 {
151   return myContainer;
152 }
153
154 QList<QWidget*> ModuleBase_WidgetFeature::getControls() const
155 {
156   QList<QWidget*> aList;
157   aList.append(myLabel);
158   aList.append(myEditor);
159   return aList;
160 }