Salome HOME
983329b53ddf61b42dae50d72e1ad5bc23e0e2a2
[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
10 #include <Config_Keywords.h>
11 #include <Config_WidgetAPI.h>
12
13 #include <Events_Loop.h>
14
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_AttributeValidator.h>
23
24 #include <QWidget>
25 #include <QLineEdit>
26 #include <QHBoxLayout>
27 #include <QLabel>
28
29 ModuleBase_WidgetFeature::ModuleBase_WidgetFeature(QWidget* theParent,
30                                                    const Config_WidgetAPI* theData, 
31                                                    const std::string& theParentId)
32 : ModuleBase_ModelWidget(theParent, theData, theParentId)
33 {
34   myContainer = new QWidget(theParent);
35   QHBoxLayout* aControlLay = new QHBoxLayout(myContainer);
36   aControlLay->setContentsMargins(0, 0, 0, 0);
37
38   QString aLabelText = QString::fromStdString(theData->widgetLabel());
39   myLabel = new QLabel(aLabelText, myContainer);
40   aControlLay->addWidget(myLabel);
41
42   myEditor = new QLineEdit(myContainer);
43   QString anObjName = QString::fromStdString(attributeID());
44   myEditor->setObjectName(anObjName);
45   myEditor->setReadOnly(true);
46   aControlLay->addWidget(myEditor);
47
48   QString aTTip = QString::fromStdString(theData->widgetTooltip());
49   myEditor->setToolTip(aTTip);
50
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   PluginManagerPtr aMgr = ModelAPI_PluginManager::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_AttributeValidator* aAttrValidator = 
102       dynamic_cast<const ModelAPI_AttributeValidator*>(*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 (!aFeature)
121   //  return false;
122   boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
123   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
124           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
125
126   ModuleBase_WidgetFeature* that = (ModuleBase_WidgetFeature*) this;
127   aRef->setObject(myObject);
128   myFeature->execute();
129   updateObject(myFeature);
130   return true;
131 }
132
133 bool ModuleBase_WidgetFeature::restoreValue()
134 {
135   boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
136   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
137           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
138
139   ObjectPtr aObj = aRef->object();
140   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aRef->object());
141   if (aFeature) {
142     myObject = aFeature;
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 }