Salome HOME
Merge branch 'master' into Dev_1.1.0
[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 <QFormLayout>
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   QFormLayout* aControlLay = new QFormLayout(this);
41   ModuleBase_Tools::adjustMargins(aControlLay);
42
43   QString aLabelText = QString::fromStdString(theData->widgetLabel());
44   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
45   myLabel = new QLabel(aLabelText, this);
46   if (!aLabelIcon.isEmpty())
47     myLabel->setPixmap(QPixmap(aLabelIcon));
48
49   mySpinBox = new ModuleBase_DoubleSpinBox(this);
50   QString anObjName = QString::fromStdString(attributeID());
51   mySpinBox->setObjectName(anObjName);
52
53   bool isOk = false;
54   std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
55   double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
56   if (isOk) {
57     mySpinBox->setMinimum(aMinVal);
58   } else {
59     mySpinBox->setMinimum(-DBL_MAX);
60   }
61
62   aProp = theData->getProperty(DOUBLE_WDG_MAX);
63   double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
64   if (isOk) {
65     mySpinBox->setMaximum(aMaxVal);
66   } else {
67     mySpinBox->setMaximum(DBL_MAX);
68   }
69
70   aProp = theData->getProperty(DOUBLE_WDG_STEP);
71   double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
72   if (isOk) {
73     double aMinStep = pow(10, -1. * (double) mySpinBox->decimals());
74     if(aStepVal < aMinStep){
75       aStepVal = aMinStep;
76     }
77     mySpinBox->setSingleStep(aStepVal);
78   }
79
80   double aDefVal = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
81   if (isOk) {
82     mySpinBox->setValue(aDefVal);
83   }
84
85   QString aTTip = QString::fromStdString(theData->widgetTooltip());
86   mySpinBox->setToolTip(aTTip);
87
88   aControlLay->addRow(myLabel, mySpinBox);
89   connect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
90 }
91
92 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
93 {
94 }
95
96 void ModuleBase_WidgetDoubleValue::reset()
97 {
98   if (isComputedDefault()) {
99     return;
100     //if (myFeature->compute(myAttributeID))
101     //  restoreValue();
102   }
103   else {
104     bool isOk;
105     double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
106     ModuleBase_Tools::setSpinValue(mySpinBox, isOk ? aDefValue : 0.0);
107     storeValueCustom();
108   }
109 }
110
111 bool ModuleBase_WidgetDoubleValue::storeValueCustom() const
112 {
113   DataPtr aData = myFeature->data();
114   AttributeDoublePtr aReal = aData->real(attributeID());
115   aReal->setValue(mySpinBox->value());
116   updateObject(myFeature);
117   return true;
118 }
119
120 bool ModuleBase_WidgetDoubleValue::restoreValue()
121 {
122   DataPtr aData = myFeature->data();
123   AttributeDoublePtr aRef = aData->real(attributeID());
124
125   ModuleBase_Tools::setSpinValue(mySpinBox, aRef->value());
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 }