]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetFeature.cpp
Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[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 <Config_Keywords.h>
8 #include <Config_WidgetAPI.h>
9
10 #include <Events_Loop.h>
11 #include <Model_Events.h>
12
13 #include <ModelAPI_Feature.h>
14 #include <ModelAPI_Data.h>
15 #include <ModelAPI_Object.h>
16 #include <ModelAPI_AttributeRefAttr.h>
17
18 #include <QWidget>
19 #include <QLineEdit>
20 #include <QHBoxLayout>
21 #include <QLabel>
22
23 ModuleBase_WidgetFeature::ModuleBase_WidgetFeature(QWidget* theParent,
24                                                    const Config_WidgetAPI* theData)
25 : ModuleBase_ModelWidget(theParent, theData)
26 {
27   QString aKinds = QString::fromStdString(theData->getProperty(FEATURE_KEYSEQUENCE));
28   myFeatureKinds = aKinds.split(" ");
29
30   myContainer = new QWidget(theParent);
31   QHBoxLayout* aControlLay = new QHBoxLayout(myContainer);
32   aControlLay->setContentsMargins(0, 0, 0, 0);
33
34   QString aLabelText = QString::fromStdString(theData->widgetLabel());
35   myLabel = new QLabel(aLabelText, myContainer);
36   aControlLay->addWidget(myLabel);
37
38   myEditor = new QLineEdit(myContainer);
39   QString anObjName = QString::fromStdString(attributeID());
40   myEditor->setObjectName(anObjName);
41   myEditor->setReadOnly(true);
42   aControlLay->addWidget(myEditor);
43
44   QString aTTip = QString::fromStdString(theData->widgetTooltip());
45   myEditor->setToolTip(aTTip);
46
47   aControlLay->addWidget(myEditor);
48   aControlLay->setStretch(1, 1);
49 }
50
51 ModuleBase_WidgetFeature::~ModuleBase_WidgetFeature()
52 {
53 }
54
55 bool ModuleBase_WidgetFeature::setFeature(const FeaturePtr& theFeature)
56 {
57   if (!theFeature || !myFeatureKinds.contains(theFeature->getKind().c_str()))
58     return false;
59
60   myFeature = theFeature;
61   myEditor->setText(theFeature ? theFeature->data()->getName().c_str() : "");
62   emit valuesChanged();
63   return true;
64 }
65
66 bool ModuleBase_WidgetFeature::storeValue(FeaturePtr theFeature) const
67 {
68   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
69   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
70           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
71
72   ModuleBase_WidgetFeature* that = (ModuleBase_WidgetFeature*) this;
73   aRef->setFeature(myFeature);
74   theFeature->execute();
75   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
76
77   return true;
78 }
79
80 bool ModuleBase_WidgetFeature::restoreValue(FeaturePtr theFeature)
81 {
82   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
83   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
84           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
85
86   myFeature = aRef->feature();
87   myEditor->setText(myFeature ? myFeature->data()->getName().c_str() : "");
88   return true;
89 }
90
91 QWidget* ModuleBase_WidgetFeature::getControl() const
92 {
93   return myContainer;
94 }
95
96 QList<QWidget*> ModuleBase_WidgetFeature::getControls() const
97 {
98   QList<QWidget*> aList;
99   aList.append(myLabel);
100   aList.append(myEditor);
101   return aList;
102 }