Salome HOME
Fix sketcher bugs
[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   const ModelAPI_Validator* aValidator = aFactory->validator(parentID(), attributeID());
80   if (aValidator) {
81     const ModuleBase_ResultValidator* aResValidator = 
82       dynamic_cast<const ModuleBase_ResultValidator*>(aValidator);
83     if (aResValidator) {
84       if (!aResValidator->isValid(theObject))
85         return false;
86     }
87   }
88
89   myObject = theObject;
90   myEditor->setText(theObject ? theObject->data()->name().c_str() : "");
91   emit valuesChanged();
92   return true;
93 }
94
95 bool ModuleBase_WidgetFeature::storeValue(ObjectPtr theObject) const
96 {
97   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
98   if (!aFeature)
99     return false;
100   boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
101   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
102           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
103
104   ModuleBase_WidgetFeature* that = (ModuleBase_WidgetFeature*) this;
105   aRef->setObject(myObject);
106   aFeature->execute();
107   updateObject(theObject);
108   return true;
109 }
110
111 bool ModuleBase_WidgetFeature::restoreValue(ObjectPtr theObject)
112 {
113   boost::shared_ptr<ModelAPI_Data> aData = theObject->data();
114   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
115           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
116
117   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aRef->object());
118   if (aFeature) {
119     myObject = aFeature;
120     myEditor->setText(myObject ? myObject->data()->name().c_str() : "");
121     return true;
122   }
123   return false;
124 }
125
126 QWidget* ModuleBase_WidgetFeature::getControl() const
127 {
128   return myContainer;
129 }
130
131 QList<QWidget*> ModuleBase_WidgetFeature::getControls() const
132 {
133   QList<QWidget*> aList;
134   aList.append(myLabel);
135   aList.append(myEditor);
136   return aList;
137 }