]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetDoubleValue.cpp
Salome HOME
Enter processing from object browser.
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetDoubleValue.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_WidgetDoubleValue.cpp
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
14 #include <ModuleBase_ParamSpinBox.h>
15 #include <ModuleBase_Tools.h>
16 #include <ModuleBase_WidgetDoubleValue.h>
17
18 #include <QFormLayout>
19 #include <QLabel>
20 #include <QList>
21 #include <QObject>
22 #include <QPixmap>
23 #include <QString>
24
25 #include <cfloat>
26
27 #ifndef DBL_MAX
28 #define DBL_MAX 1.7976931348623158e+308 
29 #endif
30 #ifdef _DEBUG
31 #include <iostream>
32 #endif
33
34 #define APPLY_BY_ENTER_OR_TAB
35
36 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(QWidget* theParent,
37                                                            const Config_WidgetAPI* theData,
38                                                            const std::string& theParentId)
39     : ModuleBase_ModelWidget(theParent, theData, theParentId)
40 {
41   QFormLayout* aControlLay = new QFormLayout(this);
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, this);
47   if (!aLabelIcon.isEmpty())
48     myLabel->setPixmap(QPixmap(aLabelIcon));
49
50   mySpinBox = new ModuleBase_ParamSpinBox(this);
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   double aDefVal = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
82   if (isOk) {
83     mySpinBox->setValue(aDefVal);
84   }
85
86   QString aTTip = QString::fromStdString(theData->widgetTooltip());
87   mySpinBox->setToolTip(aTTip);
88   myLabel->setToolTip(aTTip);
89
90   aControlLay->addRow(myLabel, mySpinBox);
91 #ifdef APPLY_BY_ENTER_OR_TAB
92   // Apply widget value change by enter/tab event.
93   //connect(mySpinBox, SIGNAL(editingFinished()), this, SIGNAL(valuesChanged()));
94   connect(mySpinBox, SIGNAL(valueStored()), this, SIGNAL(valuesChanged()));
95   connect(mySpinBox, SIGNAL(valueChanged(const QString&)), this, SIGNAL(valuesModified()));
96   connect(mySpinBox, SIGNAL(focusNextPrev()), this, SIGNAL(focusNextPrev()));
97
98 #else
99   connect(mySpinBox, SIGNAL(valueChanged(const QString&)), this, SIGNAL(valuesChanged()));
100 #endif
101 }
102
103 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
104 {
105 }
106
107 bool ModuleBase_WidgetDoubleValue::resetCustom()
108 {
109   bool aDone = false;
110   if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
111     aDone = false;
112   } else {
113     bool isOk;
114     double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
115     // reset the value just if there is a default value definition in the XML definition
116     // if the double value can not be found by the default value, do nothing
117     if (isOk) {
118       ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
119       storeValue();
120       aDone = true;
121     }
122   }
123   return aDone;
124 }
125
126 bool ModuleBase_WidgetDoubleValue::storeValueCustom() const
127 {
128   DataPtr aData = myFeature->data();
129   AttributeDoublePtr aReal = aData->real(attributeID());
130   if (mySpinBox->hasVariable()) {
131     // Here is a text of a real value or an expression.
132     std::string aText = mySpinBox->text().toStdString();
133     aReal->setText(aText);
134   } else {
135     // it is important to set the empty text value to the attribute before set the value
136     // because setValue tries to calculate the attribute value according to the
137     // attribute current text
138     aReal->setText("");
139     aReal->setValue(mySpinBox->value());
140   }
141   updateObject(myFeature);
142   return true;
143 }
144
145 bool ModuleBase_WidgetDoubleValue::restoreValueCustom()
146 {
147   DataPtr aData = myFeature->data();
148   AttributeDoublePtr aRef = aData->real(attributeID());
149   std::string aTextRepr = aRef->text();
150   if (!aTextRepr.empty()) {
151     ModuleBase_Tools::setSpinText(mySpinBox, QString::fromStdString(aTextRepr));
152   } else {
153     ModuleBase_Tools::setSpinValue(mySpinBox, aRef->value());
154   }
155   return true;
156 }
157
158 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
159 {
160   QList<QWidget*> aList;
161   aList.append(mySpinBox);
162   return aList;
163 }
164
165 bool ModuleBase_WidgetDoubleValue::processEnter()
166 {
167   bool isModified = mySpinBox->isModified();
168   if (isModified) {
169     emit valuesChanged();
170     mySpinBox->clearModified();
171     mySpinBox->selectAll();
172   }
173   return isModified;
174 }