Salome HOME
Within Issue #143 highlight active inputs
[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 #include <ModuleBase_DoubleSpinBox.h>
7 #include <ModuleBase_Tools.h>
8
9 #include <ModelAPI_AttributeDouble.h>
10 #include <ModelAPI_Data.h>
11
12 #include <Config_Keywords.h>
13 #include <Config_WidgetAPI.h>
14
15 #include <Events_Loop.h>
16 #include <ModelAPI_Events.h>
17
18 #include <QWidget>
19 #include <QLayout>
20 #include <QLabel>
21 #include <QEvent>
22 #include <QKeyEvent>
23 #include <QTimer>
24
25 #include <math.h>
26
27 #ifndef DBL_MAX
28 #define DBL_MAX 1.7976931348623158e+308 
29 #endif
30 #ifdef _DEBUG
31 #include <iostream>
32 #endif
33
34 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(QWidget* theParent,
35                                                            const Config_WidgetAPI* theData,
36                                                            const std::string& theParentId)
37     : ModuleBase_ModelWidget(theParent, theData, theParentId)
38 {
39   myContainer = new QWidget(theParent);
40   QHBoxLayout* aControlLay = new QHBoxLayout(myContainer);
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, myContainer);
46   if (!aLabelIcon.isEmpty())
47     myLabel->setPixmap(QPixmap(aLabelIcon));
48   aControlLay->addWidget(myLabel);
49
50   mySpinBox = new ModuleBase_DoubleSpinBox(myContainer);
51   QString anObjName = QString::fromStdString(attributeID());
52   mySpinBox->setObjectName(anObjName);
53
54   bool isOk = false;
55   std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
56   double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
57   if (isOk) {
58     mySpinBox->setMinimum(aMinVal);
59   } else {
60     mySpinBox->setMinimum(-DBL_MAX);
61   }
62
63   aProp = theData->getProperty(DOUBLE_WDG_MAX);
64   double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
65   if (isOk) {
66     mySpinBox->setMaximum(aMaxVal);
67   } else {
68     mySpinBox->setMaximum(DBL_MAX);
69   }
70
71   aProp = theData->getProperty(DOUBLE_WDG_STEP);
72   double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
73   if (isOk) {
74     double aMinStep = pow(10, -1. * (double) mySpinBox->decimals());
75     if(aStepVal < aMinStep){
76       aStepVal = aMinStep;
77     }
78     mySpinBox->setSingleStep(aStepVal);
79   }
80
81   aProp = theData->getProperty(ANY_WDG_DEFAULT);
82   double aDefVal = QString::fromStdString(aProp).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 bool ModuleBase_WidgetDoubleValue::storeValue() const
103 {
104   DataPtr aData = myFeature->data();
105   AttributeDoublePtr aReal = aData->real(attributeID());
106   aReal->setValue(mySpinBox->value());
107   updateObject(myFeature);
108   return true;
109 }
110
111 bool ModuleBase_WidgetDoubleValue::restoreValue()
112 {
113   DataPtr aData = myFeature->data();
114   AttributeDoublePtr aRef = aData->real(attributeID());
115
116   bool isBlocked = mySpinBox->blockSignals(true);
117   mySpinBox->setValue(aRef->value());
118   mySpinBox->blockSignals(isBlocked);
119
120   return true;
121 }
122
123 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
124 {
125   QList<QWidget*> aList;
126   aList.append(myLabel);
127   aList.append(mySpinBox);
128   return aList;
129 }