Salome HOME
Issue #2581: Initialize attributes if they are not initialized. It can be in case...
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetDoubleValue.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <Config_Keywords.h>
22 #include <Config_WidgetAPI.h>
23 #include <Events_Loop.h>
24
25 #include <ModelAPI_AttributeDouble.h>
26 #include <ModelAPI_Data.h>
27 #include <ModelAPI_Object.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Document.h>
30 #include <ModelAPI_ResultParameter.h>
31 #include <ModelAPI_AttributeDouble.h>
32 #include <ModelAPI_Tools.h>
33 #include <ModelAPI_Events.h>
34 #include <ModelAPI_AttributeString.h>
35
36 #include <ModuleBase_ParamSpinBox.h>
37 #include <ModuleBase_Tools.h>
38 #include <ModuleBase_WidgetDoubleValue.h>
39 #include <ModuleBase_IconFactory.h>
40
41 #include <QFormLayout>
42 #include <QLabel>
43 #include <QList>
44 #include <QObject>
45 #include <QPixmap>
46 #include <QString>
47
48 #include <cfloat>
49
50 #ifndef DBL_MAX
51 #define DBL_MAX 1.7976931348623158e+308
52 #endif
53 #ifdef _DEBUG
54 #include <iostream>
55 #endif
56
57 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(QWidget* theParent,
58                                                            const Config_WidgetAPI* theData)
59     : ModuleBase_ModelWidget(theParent, theData)
60 {
61   QFormLayout* aControlLay = new QFormLayout(this);
62   ModuleBase_Tools::adjustMargins(aControlLay);
63
64   QString aLabelText = translate(theData->widgetLabel());
65   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
66   myLabel = new QLabel(aLabelText, this);
67   if (!aLabelIcon.isEmpty())
68     myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
69
70   bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
71
72   mySpinBox = new ModuleBase_ParamSpinBox(this);
73   mySpinBox->setAcceptVariables(aAcceptVariables);
74   QString anObjName = QString::fromStdString(attributeID());
75   mySpinBox->setObjectName(anObjName);
76
77   bool isOk = false;
78   std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
79   double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
80   if (isOk) {
81     mySpinBox->setMinimum(aMinVal);
82   } else {
83     mySpinBox->setMinimum(-DBL_MAX);
84   }
85
86   aProp = theData->getProperty(DOUBLE_WDG_MAX);
87   double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
88   if (isOk) {
89     mySpinBox->setMaximum(aMaxVal);
90   } else {
91     mySpinBox->setMaximum(DBL_MAX);
92   }
93
94   aProp = theData->getProperty(DOUBLE_WDG_STEP);
95   double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
96   if (isOk) {
97     double aMinStep = pow(10, -1. * (double) mySpinBox->decimals());
98     if(aStepVal < aMinStep){
99       aStepVal = aMinStep;
100     }
101     mySpinBox->setSingleStep(aStepVal);
102   }
103
104   myDefaultVal = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
105   if (isOk)
106     mySpinBox->setValue(myDefaultVal);
107   else
108     myDefaultVal = 0;
109
110   QString aTTip = translate(theData->widgetTooltip());
111   mySpinBox->setToolTip(aTTip);
112   myLabel->setToolTip(aTTip);
113
114   aControlLay->addRow(myLabel, mySpinBox);
115   // we should listen textChanged signal as valueChanged do not send when text is modified
116   connect(mySpinBox, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
117   mySpinBox->setValueEnabled(isValueEnabled());
118 }
119
120 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
121 {
122 }
123
124 void ModuleBase_WidgetDoubleValue::activateCustom()
125 {
126   ModuleBase_ModelWidget::activateCustom();
127   QStringList aParameters;
128   ModuleBase_Tools::getParameters(aParameters);
129   mySpinBox->setCompletionList(aParameters);
130 }
131
132 bool ModuleBase_WidgetDoubleValue::resetCustom()
133 {
134   bool aDone = false;
135   if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
136     aDone = false;
137   } else {
138     bool isOk;
139     double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
140     // reset the value just if there is a default value definition in the XML definition
141     // if the value can not be found by the default value, do nothing
142     if (isOk) {
143       ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
144       storeValue();
145       aDone = true;
146     }
147   }
148   return aDone;
149 }
150
151 bool ModuleBase_WidgetDoubleValue::storeValueCustom()
152 {
153   DataPtr aData = myFeature->data();
154   AttributeDoublePtr aReal = aData->real(attributeID());
155   if (mySpinBox->hasVariable()) {
156     // Here is a text of a real value or an expression.
157     QString aText = mySpinBox->text();
158     if (aText.contains('=')) {
159       if (!myParameter.get()) {
160         myParameter = ModuleBase_Tools::createParameter(aText);
161         if (!myParameter.get()) {
162           aReal->setExpressionError("Parameter cannot be created");
163           aReal->setExpressionInvalid(true);
164           updateObject(myFeature);
165           return false;
166         } else if (aReal->expressionInvalid()) {
167           aReal->setExpressionError("");
168           aReal->setExpressionInvalid(false);
169         }
170       } else {
171         ModuleBase_Tools::editParameter(myParameter, aText);
172       }
173       aText = aText.split('=').at(0);
174     } else if (myParameter.get()){
175       // Nullyfy the parameter reference without deletion of the created
176       myParameter = FeaturePtr();
177     }
178     aReal->setText(aText.toStdString());
179   } else {
180     // it is important to set the empty text value to the attribute before set the value
181     // because setValue tries to calculate the attribute value according to the
182     // attribute current text
183     if (aReal->expressionInvalid()) {
184       aReal->setExpressionError("");
185       aReal->setExpressionInvalid(false);
186     }
187     aReal->setText("");
188     aReal->setValue(mySpinBox->value());
189   }
190   updateObject(myFeature);
191   return true;
192 }
193
194 bool ModuleBase_WidgetDoubleValue::restoreValueCustom()
195 {
196   DataPtr aData = myFeature->data();
197   AttributeDoublePtr aRef = aData->real(attributeID());
198   std::string aTextRepr = aRef->text();
199   if (!aTextRepr.empty()) {
200     QString aText = QString::fromStdString(aTextRepr);
201     //if (aText.endsWith('=')) {
202     //  if (!myParameter.get()) {
203     //    QString aName = aText.left(aText.indexOf('=')).trimmed();
204     //    myParameter = ModuleBase_Tools::findParameter(aName);
205     //  }
206     //  /// If myParameter is empty then it was not created because of an error
207     //  if (!myParameter.get())
208     //    return false;
209
210     //  AttributeStringPtr aExprAttr = myParameter->string("expression");
211     //  aText += aExprAttr->value().c_str();
212     //}
213     ModuleBase_Tools::setSpinText(mySpinBox, aText);
214   }
215   else {
216     ModuleBase_Tools::setSpinValue(mySpinBox, aRef->isInitialized() ? aRef->value() : myDefaultVal);
217     if (aRef->isInitialized() && aRef->expressionInvalid()) {
218       aRef->setExpressionError("");
219       aRef->setExpressionInvalid(false);
220     }
221     if (!aRef->isInitialized())
222       aRef->setValue(myDefaultVal);
223   }
224   return true;
225 }
226
227 void ModuleBase_WidgetDoubleValue::selectContent()
228 {
229   mySpinBox->selectAll();
230 }
231
232 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
233 {
234   QList<QWidget*> aList;
235   aList.append(mySpinBox);
236   return aList;
237 }
238
239 bool ModuleBase_WidgetDoubleValue::processEnter()
240 {
241   bool isModified = getValueState() == ModifiedInPP;
242   if (isModified) {
243     emit valuesChanged();
244     mySpinBox->selectAll();
245   }
246   return isModified;
247 }