Salome HOME
Merge branch 'Dev_1.1.1' of newgeom:newgeom into Dev_1.2.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 <Config_Keywords.h>
8 #include <Config_WidgetAPI.h>
9
10 #include <ModelAPI_AttributeDouble.h>
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_Object.h>
13 #include <ModelAPI_Events.h>
14
15 #include <ModuleBase_ParamSpinBox.h>
16 #include <ModuleBase_Tools.h>
17 #include <ModuleBase_WidgetDoubleValue.h>
18
19 #include <QFormLayout>
20 #include <QLabel>
21 #include <QList>
22 #include <QObject>
23 #include <QPixmap>
24 #include <QString>
25
26 #include <cfloat>
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_ParamSpinBox(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(const QString&)), this, SIGNAL(valuesChanged()));
90 }
91
92 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
93 {
94 }
95
96 void ModuleBase_WidgetDoubleValue::reset()
97 {
98   if (isComputedDefault() || mySpinBox->hasVariable()) {
99     return;
100     //if (myFeature->compute(myAttributeID))
101     //  restoreValue();
102   } else {
103     bool isOk;
104     double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
105     // reset the value just if there is a default value definition in the XML definition
106     // if the double value can not be found by the default value, do nothing
107     if (isOk) {
108       ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
109       storeValueCustom();
110     }
111   }
112 }
113
114 bool ModuleBase_WidgetDoubleValue::storeValueCustom() const
115 {
116   DataPtr aData = myFeature->data();
117   AttributeDoublePtr aReal = aData->real(attributeID());
118   if (!mySpinBox->hasVariable()) {
119     aReal->setValue(mySpinBox->value());
120     // In order to synchronize value and text
121     // If it is not synchronized sometimes it could take vale not as a digit but as a string
122     aReal->setText(mySpinBox->text().toStdString());
123   } else {
124     // Here is a text of a real value or an expression.
125     std::string aText = mySpinBox->text().toStdString();
126     aReal->setText(aText);
127     // Send it to evaluator to convert into the double and store in the attribute
128     static Events_ID anId = ModelAPI_AttributeEvalMessage::eventId();
129     std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
130       std::shared_ptr<ModelAPI_AttributeEvalMessage>(new ModelAPI_AttributeEvalMessage(anId, this));
131     aMessage->setAttribute(aData->attribute(attributeID()));
132     Events_Loop::loop()->send(aMessage);
133   }
134   updateObject(myFeature);
135   return true;
136 }
137
138 bool ModuleBase_WidgetDoubleValue::restoreValue()
139 {
140   DataPtr aData = myFeature->data();
141   AttributeDoublePtr aRef = aData->real(attributeID());
142   std::string aTextRepr = aRef->text();
143   if (!aTextRepr.empty()) {
144     ModuleBase_Tools::setSpinText(mySpinBox, QString::fromStdString(aTextRepr));
145   } else {
146     ModuleBase_Tools::setSpinValue(mySpinBox, aRef->value());
147   }
148   return true;
149 }
150
151 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
152 {
153   QList<QWidget*> aList;
154   aList.append(mySpinBox);
155   return aList;
156 }