Salome HOME
Initial version of redesign of working with results
[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 <Model_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     if (aFeatureValue)
66       isDone = setFeature(aFeatureValue->feature());
67   }
68   return isDone;
69 }
70
71 bool ModuleBase_WidgetFeature::setFeature(const FeaturePtr& theFeature)
72 {
73   if (!theFeature || !myFeatureKinds.contains(theFeature->getKind().c_str()))
74     return false;
75
76   myFeature = theFeature;
77   myEditor->setText(theFeature ? theFeature->data()->getName().c_str() : "");
78   emit valuesChanged();
79   return true;
80 }
81
82 bool ModuleBase_WidgetFeature::storeValue(FeaturePtr theFeature) const
83 {
84   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
85   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
86           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
87
88   ModuleBase_WidgetFeature* that = (ModuleBase_WidgetFeature*) this;
89   aRef->setFeature(myFeature);
90   theFeature->execute();
91   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
92
93   return true;
94 }
95
96 bool ModuleBase_WidgetFeature::restoreValue(FeaturePtr theFeature)
97 {
98   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
99   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
100           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
101
102   myFeature = aRef->feature();
103   myEditor->setText(myFeature ? myFeature->data()->getName().c_str() : "");
104   return true;
105 }
106
107 QWidget* ModuleBase_WidgetFeature::getControl() const
108 {
109   return myContainer;
110 }
111
112 QList<QWidget*> ModuleBase_WidgetFeature::getControls() const
113 {
114   QList<QWidget*> aList;
115   aList.append(myLabel);
116   aList.append(myEditor);
117   return aList;
118 }