Salome HOME
Processing of 'selection_filter' xml nodes added.
[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 <QTimer>
23
24 #include <math.h>
25
26 #ifndef DBL_MAX
27 #define DBL_MAX 1.7976931348623158e+308 
28 #endif
29 #ifdef _DEBUG
30 #include <iostream>
31 #endif
32
33 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(QWidget* theParent,
34                                                            const Config_WidgetAPI* theData,
35                                                            const std::string& theParentId)
36     : ModuleBase_ModelWidget(theParent, theData, theParentId)
37 {
38   myContainer = new QWidget(theParent);
39   QHBoxLayout* aControlLay = new QHBoxLayout(myContainer);
40   ModuleBase_Tools::adjustMargins(aControlLay);
41
42   QString aLabelText = QString::fromStdString(theData->widgetLabel());
43   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
44   myLabel = new QLabel(aLabelText, myContainer);
45   if (!aLabelIcon.isEmpty())
46     myLabel->setPixmap(QPixmap(aLabelIcon));
47   aControlLay->addWidget(myLabel);
48
49   mySpinBox = new ModuleBase_DoubleSpinBox(myContainer);
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   aProp = theData->getProperty(ATTR_DEFAULT);
81   double aDefVal = QString::fromStdString(aProp).toDouble(&isOk);
82   if (isOk) {
83     mySpinBox->setValue(aDefVal);
84   } else if (aProp == DOUBLE_WDG_DEFAULT_COMPUTED){
85     myIsComputedDefault = true;
86   }
87
88   QString aTTip = QString::fromStdString(theData->widgetTooltip());
89   mySpinBox->setToolTip(aTTip);
90
91   aControlLay->addWidget(mySpinBox);
92   aControlLay->setStretch(1, 1);
93
94   connect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
95 }
96
97 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
98 {
99 }
100
101 bool ModuleBase_WidgetDoubleValue::storeValue() const
102 {
103   DataPtr aData = myFeature->data();
104   AttributeDoublePtr aReal = aData->real(attributeID());
105   aReal->setValue(mySpinBox->value());
106   updateObject(myFeature);
107   return true;
108 }
109
110 bool ModuleBase_WidgetDoubleValue::restoreValue()
111 {
112   DataPtr aData = myFeature->data();
113   AttributeDoublePtr aRef = aData->real(attributeID());
114
115   bool isBlocked = mySpinBox->blockSignals(true);
116   mySpinBox->setValue(aRef->value());
117   mySpinBox->blockSignals(isBlocked);
118
119   return true;
120 }
121
122 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
123 {
124   QList<QWidget*> aList;
125   aList.append(mySpinBox);
126   return aList;
127 }