]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetDoubleValue.cpp
Salome HOME
904df9408166c2cabb180c6daa7e73cf0a2b2d32
[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 (!isUseReset())
99     return;
100
101   if (isComputedDefault() || mySpinBox->hasVariable()) {
102     return;
103     //if (myFeature->compute(myAttributeID))
104     //  restoreValue();
105   } else {
106     bool isOk;
107     double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
108     // reset the value just if there is a default value definition in the XML definition
109     // if the double value can not be found by the default value, do nothing
110     if (isOk) {
111       ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
112       storeValueCustom();
113     }
114   }
115 }
116
117 bool ModuleBase_WidgetDoubleValue::storeValueCustom() const
118 {
119   DataPtr aData = myFeature->data();
120   AttributeDoublePtr aReal = aData->real(attributeID());
121   if (!mySpinBox->hasVariable()) {
122     aReal->setValue(mySpinBox->value());
123     aReal->setText("");
124   } else {
125     // Here is a text of a real value or an expression.
126     std::string aText = mySpinBox->text().toStdString();
127     aReal->setText(aText);
128     // Send it to evaluator to convert into the double and store in the attribute
129     static Events_ID anId = ModelAPI_AttributeEvalMessage::eventId();
130     std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
131       std::shared_ptr<ModelAPI_AttributeEvalMessage>(new ModelAPI_AttributeEvalMessage(anId, this));
132     aMessage->setAttribute(aData->attribute(attributeID()));
133     Events_Loop::loop()->send(aMessage);
134   }
135   updateObject(myFeature);
136   return true;
137 }
138
139 bool ModuleBase_WidgetDoubleValue::restoreValue()
140 {
141   DataPtr aData = myFeature->data();
142   AttributeDoublePtr aRef = aData->real(attributeID());
143   std::string aTextRepr = aRef->text();
144   if (!aTextRepr.empty()) {
145     ModuleBase_Tools::setSpinText(mySpinBox, QString::fromStdString(aTextRepr));
146   } else {
147     ModuleBase_Tools::setSpinValue(mySpinBox, aRef->value());
148   }
149   return true;
150 }
151
152 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
153 {
154   QList<QWidget*> aList;
155   aList.append(mySpinBox);
156   return aList;
157 }