Salome HOME
Issue #394 Undo-ing a Sketch element
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetDoubleValue.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_Widgets.h
4 // Created:     04 June 2014
5 // Author:      Vitaly Smetannikov
6
7 #include <ModuleBase_WidgetDoubleValue.h>
8 #include <ModuleBase_DoubleSpinBox.h>
9 #include <ModuleBase_Tools.h>
10
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_Data.h>
13
14 #include <Config_Keywords.h>
15 #include <Config_WidgetAPI.h>
16
17 #include <Events_Loop.h>
18 #include <ModelAPI_Events.h>
19
20 #include <QWidget>
21 #include <QLayout>
22 #include <QLabel>
23 #include <QEvent>
24 #include <QTimer>
25
26 #include <math.h>
27
28 #ifndef DBL_MAX
29 #define DBL_MAX 1.7976931348623158e+308 
30 #endif
31 #ifdef _DEBUG
32 #include <iostream>
33 #endif
34
35 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(QWidget* theParent,
36                                                            const Config_WidgetAPI* theData,
37                                                            const std::string& theParentId)
38     : ModuleBase_ModelWidget(theParent, theData, theParentId)
39 {
40   myContainer = new QWidget(theParent);
41   QHBoxLayout* aControlLay = new QHBoxLayout(myContainer);
42   ModuleBase_Tools::adjustMargins(aControlLay);
43
44   QString aLabelText = QString::fromStdString(theData->widgetLabel());
45   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
46   myLabel = new QLabel(aLabelText, myContainer);
47   if (!aLabelIcon.isEmpty())
48     myLabel->setPixmap(QPixmap(aLabelIcon));
49   aControlLay->addWidget(myLabel);
50
51   mySpinBox = new ModuleBase_DoubleSpinBox(myContainer);
52   QString anObjName = QString::fromStdString(attributeID());
53   mySpinBox->setObjectName(anObjName);
54
55   bool isOk = false;
56   std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
57   double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
58   if (isOk) {
59     mySpinBox->setMinimum(aMinVal);
60   } else {
61     mySpinBox->setMinimum(-DBL_MAX);
62   }
63
64   aProp = theData->getProperty(DOUBLE_WDG_MAX);
65   double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
66   if (isOk) {
67     mySpinBox->setMaximum(aMaxVal);
68   } else {
69     mySpinBox->setMaximum(DBL_MAX);
70   }
71
72   aProp = theData->getProperty(DOUBLE_WDG_STEP);
73   double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
74   if (isOk) {
75     double aMinStep = pow(10, -1. * (double) mySpinBox->decimals());
76     if(aStepVal < aMinStep){
77       aStepVal = aMinStep;
78     }
79     mySpinBox->setSingleStep(aStepVal);
80   }
81
82   aProp = theData->getProperty(ATTR_DEFAULT);
83   double aDefVal = QString::fromStdString(aProp).toDouble(&isOk);
84   if (isOk) {
85     mySpinBox->setValue(aDefVal);
86   } else if (aProp == DOUBLE_WDG_DEFAULT_COMPUTED){
87     myIsComputedDefault = true;
88   }
89
90   QString aTTip = QString::fromStdString(theData->widgetTooltip());
91   mySpinBox->setToolTip(aTTip);
92
93   aControlLay->addWidget(mySpinBox);
94   aControlLay->setStretch(1, 1);
95
96   connect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
97   connect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(controlValuesChanged()));
98 }
99
100 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
101 {
102 }
103
104 bool ModuleBase_WidgetDoubleValue::storeValue() const
105 {
106   DataPtr aData = myFeature->data();
107   AttributeDoublePtr aReal = aData->real(attributeID());
108   aReal->setValue(mySpinBox->value());
109   updateObject(myFeature);
110   return true;
111 }
112
113 bool ModuleBase_WidgetDoubleValue::restoreValue()
114 {
115   DataPtr aData = myFeature->data();
116   AttributeDoublePtr aRef = aData->real(attributeID());
117
118   bool isBlocked = mySpinBox->blockSignals(true);
119   mySpinBox->setValue(aRef->value());
120   mySpinBox->blockSignals(isBlocked);
121
122   return true;
123 }
124
125 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
126 {
127   QList<QWidget*> aList;
128   aList.append(mySpinBox);
129   return aList;
130 }