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