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