Salome HOME
Merge branch 'Dev_0.7.1' of newgeom:newgeom into Dev_0.7.1
[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 }
98
99 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
100 {
101 }
102
103 bool ModuleBase_WidgetDoubleValue::storeValue() const
104 {
105   DataPtr aData = myFeature->data();
106   AttributeDoublePtr aReal = aData->real(attributeID());
107   aReal->setValue(mySpinBox->value());
108   updateObject(myFeature);
109   return true;
110 }
111
112 bool ModuleBase_WidgetDoubleValue::restoreValue()
113 {
114   DataPtr aData = myFeature->data();
115   AttributeDoublePtr aRef = aData->real(attributeID());
116
117   bool isBlocked = mySpinBox->blockSignals(true);
118   mySpinBox->setValue(aRef->value());
119   mySpinBox->blockSignals(isBlocked);
120
121   return true;
122 }
123
124 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
125 {
126   QList<QWidget*> aList;
127   aList.append(mySpinBox);
128   return aList;
129 }