]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetDoubleValue.cpp
Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetDoubleValue.cpp
1 // File:        ModuleBase_Widgets.h
2 // Created:     04 June 2014
3 // Author:      Vitaly Smetannikov
4
5 #include <ModuleBase_WidgetDoubleValue.h>
6
7 #include <ModelAPI_AttributeDouble.h>
8 #include <ModelAPI_Data.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 <QWidget>
17 #include <QLayout>
18 #include <QLabel>
19 #include <QDoubleSpinBox>
20
21
22 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(QWidget* theParent, const Config_WidgetAPI* theData)
23   : ModuleBase_ModelWidget(theParent, theData)
24 {
25   myContainer = new QWidget(theParent);
26   QHBoxLayout* aControlLay = new QHBoxLayout(myContainer);
27   aControlLay->setContentsMargins(0, 0, 0, 0);
28
29   QString aLabelText = QString::fromStdString(theData->widgetLabel());
30   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
31   myLabel = new QLabel(aLabelText, myContainer);
32   myLabel->setPixmap(QPixmap(aLabelIcon));
33   aControlLay->addWidget(myLabel);
34
35   mySpinBox = new QDoubleSpinBox(myContainer);
36   QString anObjName = QString::fromStdString(attributeID());
37   mySpinBox->setObjectName(anObjName);
38
39   bool isOk = false;
40   std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
41   double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
42   if (isOk) {
43     mySpinBox->setMinimum(aMinVal);
44   } else {
45     mySpinBox->setMinimum(-DBL_MAX);
46   }
47
48   aProp = theData->getProperty(DOUBLE_WDG_MAX);
49   double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
50   if (isOk) {
51     mySpinBox->setMaximum(aMaxVal);
52   } else {
53     mySpinBox->setMaximum(DBL_MAX);
54   }
55
56   aProp = theData->getProperty(DOUBLE_WDG_STEP);
57   double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
58   if (isOk) {
59     mySpinBox->setSingleStep(aStepVal);
60   }
61
62   aProp = theData->getProperty(DOUBLE_WDG_DFLT);
63   double aDefVal = QString::fromStdString(aProp).toDouble(&isOk);
64   if (isOk) {
65     mySpinBox->setValue(aDefVal);
66   }
67
68   QString aTTip = QString::fromStdString(theData->widgetTooltip());
69   mySpinBox->setToolTip(aTTip);
70
71   aControlLay->addWidget(mySpinBox);
72   aControlLay->setStretch(1, 1);
73
74   connect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
75 }
76
77 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
78 {
79 }
80
81 bool ModuleBase_WidgetDoubleValue::storeValue(FeaturePtr theFeature) const
82 {
83   DataPtr aData = theFeature->data();
84   boost::shared_ptr<ModelAPI_AttributeDouble> aReal = aData->real(attributeID());
85   if (aReal->value() != mySpinBox->value()) {
86     aReal->setValue(mySpinBox->value());
87     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
88   }
89   return true;
90 }
91
92 bool ModuleBase_WidgetDoubleValue::restoreValue(FeaturePtr theFeature)
93 {
94   DataPtr aData = theFeature->data();
95   boost::shared_ptr<ModelAPI_AttributeDouble> aRef = aData->real(attributeID());
96
97   bool isBlocked = mySpinBox->blockSignals(true);
98   mySpinBox->setValue(aRef->value());
99   mySpinBox->blockSignals(isBlocked);
100
101   return true;
102 }
103
104 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
105 {
106   QList<QWidget*> aList;
107   aList.append(myLabel);
108   aList.append(mySpinBox);
109   return aList;
110 }