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