Salome HOME
Sources of the application adopted to RHEL6 x64. The newest version of Eclipse IDE...
[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 #include <ModelAPI_Events.h>
15
16 #include <ModelAPI_Feature.h>
17 #include <ModelAPI_Data.h>
18 #include <ModelAPI_Object.h>
19 #include <ModelAPI_AttributeRefAttr.h>
20
21 #include <QWidget>
22 #include <QLineEdit>
23 #include <QHBoxLayout>
24 #include <QLabel>
25
26 ModuleBase_WidgetFeature::ModuleBase_WidgetFeature(QWidget* theParent,
27                                                    const Config_WidgetAPI* theData)
28 : ModuleBase_ModelWidget(theParent, theData)
29 {
30   QString aKinds = QString::fromStdString(theData->getProperty(FEATURE_KEYSEQUENCE));
31   myFeatureKinds = aKinds.split(" ");
32
33   myContainer = new QWidget(theParent);
34   QHBoxLayout* aControlLay = new QHBoxLayout(myContainer);
35   aControlLay->setContentsMargins(0, 0, 0, 0);
36
37   QString aLabelText = QString::fromStdString(theData->widgetLabel());
38   myLabel = new QLabel(aLabelText, myContainer);
39   aControlLay->addWidget(myLabel);
40
41   myEditor = new QLineEdit(myContainer);
42   QString anObjName = QString::fromStdString(attributeID());
43   myEditor->setObjectName(anObjName);
44   myEditor->setReadOnly(true);
45   aControlLay->addWidget(myEditor);
46
47   QString aTTip = QString::fromStdString(theData->widgetTooltip());
48   myEditor->setToolTip(aTTip);
49
50   aControlLay->addWidget(myEditor);
51   aControlLay->setStretch(1, 1);
52 }
53
54 ModuleBase_WidgetFeature::~ModuleBase_WidgetFeature()
55 {
56 }
57
58 bool ModuleBase_WidgetFeature::setValue(ModuleBase_WidgetValue* theValue)
59 {
60   bool isDone = false;
61
62   if (theValue) {
63     ModuleBase_WidgetValueFeature* aFeatureValue = 
64                          dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
65     // TODO
66 //    if (aFeatureValue)
67 //      isDone = setFeature(aFeatureValue->feature());
68   }
69   return isDone;
70 }
71
72 bool ModuleBase_WidgetFeature::setFeature(const FeaturePtr& theFeature)
73 {
74   if (!myFeatureKinds.contains(theFeature->getKind().c_str()))
75     return false;
76
77   myFeature = theFeature;
78   myEditor->setText(theFeature ? theFeature->data()->name().c_str() : "");
79   emit valuesChanged();
80   return true;
81 }
82
83 bool ModuleBase_WidgetFeature::storeValue(ObjectPtr theObject) const
84 {
85   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
86   if (!aFeature)
87     return false;
88   boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
89   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
90           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
91
92   ModuleBase_WidgetFeature* that = (ModuleBase_WidgetFeature*) this;
93   aRef->setObject(myFeature);
94   aFeature->execute();
95   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
96
97   return true;
98 }
99
100 bool ModuleBase_WidgetFeature::restoreValue(ObjectPtr theObject)
101 {
102   boost::shared_ptr<ModelAPI_Data> aData = theObject->data();
103   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
104           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
105
106   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aRef->object());
107   if (aFeature) {
108     myFeature = aFeature;
109     myEditor->setText(myFeature ? myFeature->data()->name().c_str() : "");
110     return true;
111   }
112   return false;
113 }
114
115 QWidget* ModuleBase_WidgetFeature::getControl() const
116 {
117   return myContainer;
118 }
119
120 QList<QWidget*> ModuleBase_WidgetFeature::getControls() const
121 {
122   QList<QWidget*> aList;
123   aList.append(myLabel);
124   aList.append(myEditor);
125   return aList;
126 }