]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetCheckDoubleValue.cpp
Salome HOME
fcce1121fbee97520dcfc4ef5f85f25054da1547
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetCheckDoubleValue.cpp
1 // Copyright (C) 2014-2016 CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_WidgetCheckDoubleValue.cpp
4 // Created:     16 June 2016
5 // Author:      Clarisse Genrault (CEA)
6
7 #include <Config_Keywords.h>
8 #include <Config_WidgetAPI.h>
9
10 #include <ModelAPI_AttributeDouble.h>
11 #include <ModelAPI_AttributeBoolean.h>
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_Object.h>
14
15 #include <ModuleBase_ParamSpinBox.h>
16 #include <ModuleBase_Tools.h>
17 #include <ModuleBase_WidgetCheckDoubleValue.h>
18 #include <ModuleBase_IconFactory.h>
19
20 #include <QFormLayout>
21 #include <QCheckBox>
22 #include <QList>
23 #include <QObject>
24 #include <QPixmap>
25 #include <QString>
26
27 #include <cfloat>
28
29 #ifndef DBL_MAX
30 #define DBL_MAX 1.7976931348623158e+308 
31 #endif
32 #ifdef _DEBUG
33 #include <iostream>
34 #endif
35
36 //#define DEBUG_COMPLETE_WITH_PARAMETERS
37
38 ModuleBase_WidgetCheckDoubleValue::ModuleBase_WidgetCheckDoubleValue(QWidget* theParent,
39                                                                      const Config_WidgetAPI* theData)
40     : ModuleBase_ModelWidget(theParent, theData)
41 {
42   QFormLayout* aControlLay = new QFormLayout(this);
43   ModuleBase_Tools::adjustMargins(aControlLay);
44   
45   myCheckAttributeId = theData->getProperty("id_check");
46
47   QString aCheckText = QString::fromStdString(theData->widgetLabel());
48   myCheckBox = new QCheckBox(aCheckText, this);
49
50   bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
51
52   mySpinBox = new ModuleBase_ParamSpinBox(this);
53   mySpinBox->setAcceptVariables(aAcceptVariables);
54   QString anObjName = QString::fromStdString(attributeID());
55   mySpinBox->setObjectName(anObjName);
56
57   bool isOk = false;
58   std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
59   double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
60   if (isOk) {
61     mySpinBox->setMinimum(aMinVal);
62   } else {
63     mySpinBox->setMinimum(-DBL_MAX);
64   }
65
66   aProp = theData->getProperty(DOUBLE_WDG_MAX);
67   double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
68   if (isOk) {
69     mySpinBox->setMaximum(aMaxVal);
70   } else {
71     mySpinBox->setMaximum(DBL_MAX);
72   }
73
74   aProp = theData->getProperty(DOUBLE_WDG_STEP);
75   double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
76   if (isOk) {
77     double aMinStep = pow(10, -1. * (double) mySpinBox->decimals());
78     if(aStepVal < aMinStep){
79       aStepVal = aMinStep;
80     }
81     mySpinBox->setSingleStep(aStepVal);
82   }
83
84   double aDefVal = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
85   if (isOk) {
86     mySpinBox->setValue(aDefVal);
87   }
88   
89   QString aTTip = QString::fromStdString(theData->widgetTooltip());
90   mySpinBox->setToolTip(aTTip);
91   myCheckBox->setToolTip(aTTip);
92   
93   myCheckBox->setChecked(false);
94   mySpinBox->setEnabled(false);            
95
96   aControlLay->addRow(myCheckBox, mySpinBox);
97   connect(mySpinBox, SIGNAL(valueChanged(const QString&)), this, SIGNAL(valuesModified()));
98   connect(myCheckBox, SIGNAL(toggled(bool)), this, SIGNAL(valuesChanged()));
99 }
100
101 ModuleBase_WidgetCheckDoubleValue::~ModuleBase_WidgetCheckDoubleValue()
102 {
103 }
104
105 void ModuleBase_WidgetCheckDoubleValue::activateCustom()
106 {
107   ModuleBase_ModelWidget::activateCustom();
108 #ifdef DEBUG_COMPLETE_WITH_PARAMETERS
109   QStringList aParameters;
110   ModuleBase_Tools::getParameters(aParameters);
111   mySpinBox->setCompletionList(aParameters);
112 #endif
113 }
114
115 bool ModuleBase_WidgetCheckDoubleValue::resetCustom()
116 {
117   bool aDone = false;
118   if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
119     aDone = false;
120   } else {
121     bool isOk;
122     double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
123     // reset the value just if there is a default value definition in the XML definition
124     // if the value can not be found by the default value, do nothing
125     if (isOk) {
126       ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
127       storeValue();
128       aDone = true;
129     }
130   }
131   return aDone;
132 }
133
134 bool ModuleBase_WidgetCheckDoubleValue::storeValueCustom()
135 {
136   DataPtr aData = myFeature->data();
137   AttributeDoublePtr aReal = aData->real(attributeID());
138   AttributeBooleanPtr aBoolean = aData->boolean(myCheckAttributeId);
139   
140   if (mySpinBox->hasVariable()) {
141     // Here is a text of a real value or an expression.
142     std::string aText = mySpinBox->text().toStdString();
143     aReal->setText(aText);
144   } else {
145     // it is important to set the empty text value to the attribute before set the value
146     // because setValue tries to calculate the attribute value according to the
147     // attribute current text
148     aReal->setText("");
149     aReal->setValue(mySpinBox->value());
150   }
151   
152   // The spinbox is enabled/disabled only if the checkbox is checked/unchecked
153   aBoolean->setValue(myCheckBox->isChecked());
154   mySpinBox->setEnabled(myCheckBox->isChecked());
155   
156   updateObject(myFeature);
157   return true;
158 }
159
160 bool ModuleBase_WidgetCheckDoubleValue::restoreValueCustom()
161 {
162   DataPtr aData = myFeature->data();
163   AttributeDoublePtr aRef = aData->real(attributeID());
164   AttributeBooleanPtr aBoolean = aData->boolean(myCheckAttributeId);
165   std::string aTextRepr = aRef->text();
166   if (!aTextRepr.empty()) {
167     ModuleBase_Tools::setSpinText(mySpinBox, QString::fromStdString(aTextRepr));
168   } else {
169     ModuleBase_Tools::setSpinValue(mySpinBox, aRef->value());
170   }
171   
172   // The spinbox is enabled/disabled only if the checkbox is checked/unchecked
173   myCheckBox->setChecked(aBoolean->value());
174   mySpinBox->setEnabled(aBoolean->value());
175   
176   return true;
177 }
178
179 void ModuleBase_WidgetCheckDoubleValue::selectContent()
180 {
181   mySpinBox->selectAll();
182 }
183
184 QList<QWidget*> ModuleBase_WidgetCheckDoubleValue::getControls() const
185 {
186   QList<QWidget*> aList;
187   aList.append(mySpinBox);
188   aList.append(myCheckBox);
189   return aList;
190 }
191
192 bool ModuleBase_WidgetCheckDoubleValue::processEnter()
193 {
194   bool isModified = getValueState() == ModifiedInPP;
195   if (isModified) {
196     emit valuesChanged();
197     mySpinBox->selectAll();
198   }
199   return isModified;
200 }