Salome HOME
47e9960be06285ea6e882961c283926b1011b5d8
[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_ResultValidators.h>
10 #include <ModuleBase_Tools.h>
11
12 #include <Config_Keywords.h>
13 #include <Config_WidgetAPI.h>
14
15 #include <Events_Loop.h>
16 #include <ModelAPI_Events.h>
17
18 #include <ModelAPI_Feature.h>
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_Object.h>
21 #include <ModelAPI_AttributeRefAttr.h>
22 #include <ModelAPI_Validator.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   //QString aKinds = QString::fromStdString(theData->getProperty(FEATURE_KEYSEQUENCE));
35   //myObjectKinds = aKinds.split(" ");
36   //theData->
37
38   myContainer = new QWidget(theParent);
39   QHBoxLayout* aControlLay = new QHBoxLayout(myContainer);
40   aControlLay->setContentsMargins(0, 0, 0, 0);
41
42   QString aLabelText = QString::fromStdString(theData->widgetLabel());
43   myLabel = new QLabel(aLabelText, myContainer);
44   aControlLay->addWidget(myLabel);
45
46   myEditor = new QLineEdit(myContainer);
47   QString anObjName = QString::fromStdString(attributeID());
48   myEditor->setObjectName(anObjName);
49   myEditor->setReadOnly(true);
50   aControlLay->addWidget(myEditor);
51
52   QString aTTip = QString::fromStdString(theData->widgetTooltip());
53   myEditor->setToolTip(aTTip);
54
55   aControlLay->addWidget(myEditor);
56   aControlLay->setStretch(1, 1);
57 }
58
59 ModuleBase_WidgetFeature::~ModuleBase_WidgetFeature()
60 {
61 }
62
63 bool ModuleBase_WidgetFeature::setValue(ModuleBase_WidgetValue* theValue)
64 {
65   bool isDone = false;
66
67   if (theValue) {
68     ModuleBase_WidgetValueFeature* aFeatureValue = 
69                          dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
70     if (aFeatureValue)
71       isDone = setObject(aFeatureValue->object());
72   }
73   return isDone;
74 }
75
76 bool ModuleBase_WidgetFeature::setObject(const ObjectPtr& theObject, bool theSendEvent)
77 {
78   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
79   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
80   std::list<ModelAPI_Validator*> aValidators;
81   std::list<std::list<std::string> > anArguments;
82   aFactory->validators(parentID(), attributeID(), aValidators, anArguments);
83   std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
84   bool isValid = true;
85   for(; aValidator != aValidators.end(); aValidator++) {
86     const ModuleBase_ResultValidator* aResValidator = 
87       dynamic_cast<const ModuleBase_ResultValidator*>(*aValidator);
88     if (aResValidator) {
89       isValid = false;
90       if (aResValidator->isValid(theObject)) {
91         isValid = true;
92         break;
93       }
94     }
95   }
96   if (!isValid)
97     return false;
98
99   myObject = ModuleBase_Tools::feature(theObject);
100   myEditor->setText(theObject ? theObject->data()->name().c_str() : "");
101   if (theSendEvent)
102     emit valuesChanged();
103   return true;
104 }
105
106 bool ModuleBase_WidgetFeature::storeValue(ObjectPtr theObject) const
107 {
108   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
109   if (!aFeature)
110     return false;
111   boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
112   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
113           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
114
115   ModuleBase_WidgetFeature* that = (ModuleBase_WidgetFeature*) this;
116   aRef->setObject(myObject);
117   aFeature->execute();
118   updateObject(theObject);
119   return true;
120 }
121
122 bool ModuleBase_WidgetFeature::restoreValue(ObjectPtr theObject)
123 {
124   boost::shared_ptr<ModelAPI_Data> aData = theObject->data();
125   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
126           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
127
128   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aRef->object());
129   if (aFeature) {
130     myObject = aFeature;
131     myEditor->setText(myObject ? myObject->data()->name().c_str() : "");
132     return true;
133   }
134   return false;
135 }
136
137 QWidget* ModuleBase_WidgetFeature::getControl() const
138 {
139   return myContainer;
140 }
141
142 QList<QWidget*> ModuleBase_WidgetFeature::getControls() const
143 {
144   QList<QWidget*> aList;
145   aList.append(myLabel);
146   aList.append(myEditor);
147   return aList;
148 }