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   double aDefVal = QString::fromStdString(myDefaultValue).toDouble(&isOk);
83   if (isOk) {
84     mySpinBox->setValue(aDefVal);
85   } else if (aProp == DOUBLE_WDG_DEFAULT_COMPUTED){
86     myIsComputedDefault = true;
87   }
88
89   QString aTTip = QString::fromStdString(theData->widgetTooltip());
90   mySpinBox->setToolTip(aTTip);
91
92   aControlLay->addWidget(mySpinBox);
93   aControlLay->setStretch(1, 1);
94
95   connect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
96 }
97
98 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
99 {
100 }
101
102 void ModuleBase_WidgetDoubleValue::reset()
103 {
104   bool isOk;
105   double aDefValue = QString::fromStdString(myDefaultValue).toDouble(&isOk);
106   mySpinBox->setValue(isOk ? aDefValue : 0.0);
107 }
108
109 bool ModuleBase_WidgetDoubleValue::storeValue() const
110 {
111   DataPtr aData = myFeature->data();
112   AttributeDoublePtr aReal = aData->real(attributeID());
113   aReal->setValue(mySpinBox->value());
114   updateObject(myFeature);
115   return true;
116 }
117
118 bool ModuleBase_WidgetDoubleValue::restoreValue()
119 {
120   DataPtr aData = myFeature->data();
121   AttributeDoublePtr aRef = aData->real(attributeID());
122
123   bool isBlocked = mySpinBox->blockSignals(true);
124   mySpinBox->setValue(aRef->value());
125   mySpinBox->blockSignals(isBlocked);
126
127   return true;
128 }
129
130 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
131 {
132   QList<QWidget*> aList;
133   aList.append(mySpinBox);
134   return aList;
135 }