]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetDoubleValue.cpp
Salome HOME
Parameters creation error processing
[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_Expression.h>
29 #include <ModelAPI_AttributeString.h>
30 #include <ModelAPI_Session.h>
31 #include <ModelAPI_Document.h>
32 #include <ModelAPI_ResultParameter.h>
33 #include <ModelAPI_AttributeDouble.h>
34 #include <ModelAPI_Tools.h>
35 #include <ModelAPI_Events.h>
36
37 #include <ModuleBase_ParamSpinBox.h>
38 #include <ModuleBase_Tools.h>
39 #include <ModuleBase_WidgetDoubleValue.h>
40 #include <ModuleBase_IconFactory.h>
41
42 #include <QFormLayout>
43 #include <QLabel>
44 #include <QList>
45 #include <QObject>
46 #include <QPixmap>
47 #include <QString>
48
49 #include <cfloat>
50
51 #ifndef DBL_MAX
52 #define DBL_MAX 1.7976931348623158e+308
53 #endif
54 #ifdef _DEBUG
55 #include <iostream>
56 #endif
57
58 #define DEBUG_COMPLETE_WITH_PARAMETERS
59
60 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(QWidget* theParent,
61                                                            const Config_WidgetAPI* theData)
62     : ModuleBase_ModelWidget(theParent, theData)
63 {
64   QFormLayout* aControlLay = new QFormLayout(this);
65   ModuleBase_Tools::adjustMargins(aControlLay);
66
67   QString aLabelText = translate(theData->widgetLabel());
68   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
69   myLabel = new QLabel(aLabelText, this);
70   if (!aLabelIcon.isEmpty())
71     myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
72
73   bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
74
75   mySpinBox = new ModuleBase_ParamSpinBox(this);
76   mySpinBox->setAcceptVariables(aAcceptVariables);
77   QString anObjName = QString::fromStdString(attributeID());
78   mySpinBox->setObjectName(anObjName);
79
80   bool isOk = false;
81   std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
82   double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
83   if (isOk) {
84     mySpinBox->setMinimum(aMinVal);
85   } else {
86     mySpinBox->setMinimum(-DBL_MAX);
87   }
88
89   aProp = theData->getProperty(DOUBLE_WDG_MAX);
90   double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
91   if (isOk) {
92     mySpinBox->setMaximum(aMaxVal);
93   } else {
94     mySpinBox->setMaximum(DBL_MAX);
95   }
96
97   aProp = theData->getProperty(DOUBLE_WDG_STEP);
98   double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
99   if (isOk) {
100     double aMinStep = pow(10, -1. * (double) mySpinBox->decimals());
101     if(aStepVal < aMinStep){
102       aStepVal = aMinStep;
103     }
104     mySpinBox->setSingleStep(aStepVal);
105   }
106
107   double aDefVal = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
108   if (isOk) {
109     mySpinBox->setValue(aDefVal);
110   }
111
112   QString aTTip = translate(theData->widgetTooltip());
113   mySpinBox->setToolTip(aTTip);
114   myLabel->setToolTip(aTTip);
115
116   aControlLay->addRow(myLabel, mySpinBox);
117   // we should listen textChanged signal as valueChanged do not send when text is modified
118   connect(mySpinBox, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
119   mySpinBox->setValueEnabled(isValueEnabled());
120 }
121
122 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
123 {
124 }
125
126 void ModuleBase_WidgetDoubleValue::activateCustom()
127 {
128   ModuleBase_ModelWidget::activateCustom();
129 #ifdef DEBUG_COMPLETE_WITH_PARAMETERS
130   QStringList aParameters;
131   ModuleBase_Tools::getParameters(aParameters);
132   mySpinBox->setCompletionList(aParameters);
133 #endif
134 }
135
136 bool ModuleBase_WidgetDoubleValue::resetCustom()
137 {
138   bool aDone = false;
139   if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
140     aDone = false;
141   } else {
142     bool isOk;
143     double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
144     // reset the value just if there is a default value definition in the XML definition
145     // if the value can not be found by the default value, do nothing
146     if (isOk) {
147       ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
148       storeValue();
149       aDone = true;
150     }
151   }
152   return aDone;
153 }
154
155 bool ModuleBase_WidgetDoubleValue::storeValueCustom()
156 {
157   DataPtr aData = myFeature->data();
158   AttributeDoublePtr aReal = aData->real(attributeID());
159   if (mySpinBox->hasVariable()) {
160     // Here is a text of a real value or an expression.
161     QString aText = mySpinBox->text();
162     if (aText.contains('=')) {
163       if (!myParameter.get()) {
164         myParameter = createParameter(aText);
165         if (!myParameter.get()) {
166           aReal->setExpressionError("Parameter cannot be created");
167           aReal->setExpressionInvalid(true);
168           mySpinBox->setText(aReal->text().c_str());
169           return false;
170         } else if (aReal->expressionInvalid()) {
171           aReal->setExpressionError("");
172           aReal->setExpressionInvalid(false);
173         }
174       } else {
175         editParameter(aText);
176       }
177       aText = aText.split('=').at(0) + "=";
178     } else if (myParameter.get()){
179       // Nullyfy the parameter reference without deletion of the created
180       myParameter = FeaturePtr();
181     }
182     aReal->setText(aText.toStdString());
183   } else {
184     // it is important to set the empty text value to the attribute before set the value
185     // because setValue tries to calculate the attribute value according to the
186     // attribute current text
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 = 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   } else {
215     ModuleBase_Tools::setSpinValue(mySpinBox, aRef->isInitialized() ? aRef->value() : 0);
216   }
217   return true;
218 }
219
220 void ModuleBase_WidgetDoubleValue::selectContent()
221 {
222   mySpinBox->selectAll();
223 }
224
225 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
226 {
227   QList<QWidget*> aList;
228   aList.append(mySpinBox);
229   return aList;
230 }
231
232 bool ModuleBase_WidgetDoubleValue::processEnter()
233 {
234   bool isModified = getValueState() == ModifiedInPP;
235   if (isModified) {
236     emit valuesChanged();
237     mySpinBox->selectAll();
238   }
239   return isModified;
240 }
241
242
243 FeaturePtr ModuleBase_WidgetDoubleValue::createParameter(const QString& theText) const
244 {
245   FeaturePtr aParameter;
246   QStringList aList = theText.split("=");
247   if (aList.count() != 2) {
248     return aParameter;
249   }
250   QString aParamName = aList.at(0).trimmed();
251
252   if (isNameExist(aParamName)) {
253     return aParameter;
254   }
255
256   if (!ModelAPI_Expression::isVariable(aParamName.toStdString())) {
257     return aParameter;
258   }
259
260   QString aExpression = aList.at(1).trimmed();
261   if (aExpression.isEmpty()) {
262     return aParameter;
263   }
264
265   SessionPtr aMgr = ModelAPI_Session::get();
266   std::shared_ptr<ModelAPI_Document> aDoc = aMgr->activeDocument();
267
268   aParameter = aDoc->addFeature("Parameter");
269   if (aParameter.get()) {
270     AttributeStringPtr aNameAttr = aParameter->string("variable");
271     aNameAttr->setValue(aParamName.toStdString());
272
273     AttributeStringPtr aExprAttr = aParameter->string("expression");
274     aExprAttr->setValue(aExpression.toStdString());
275     aParameter->execute();
276
277     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
278     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
279   }
280   return aParameter;
281 }
282
283 bool ModuleBase_WidgetDoubleValue::isNameExist(const QString& theName) const
284 {
285   SessionPtr aMgr = ModelAPI_Session::get();
286   std::shared_ptr<ModelAPI_Document> aDoc = aMgr->activeDocument();
287   FeaturePtr aParamFeature;
288   int aNbFeatures = aDoc->numInternalFeatures();
289   std::string aName = theName.toStdString();
290   for (int i = 0; i < aNbFeatures; i++) {
291     aParamFeature = aDoc->internalFeature(i);
292     if (aParamFeature && aParamFeature->getKind() == "Parameter") {
293       if ((myParameter != aParamFeature) && (aParamFeature->name() == aName))
294         return true;
295     }
296   }
297   return false;
298 }
299
300 void ModuleBase_WidgetDoubleValue::editParameter(const QString& theText)
301 {
302   QStringList aList = theText.split("=");
303   QString aParamName = aList.at(0).trimmed();
304
305   QString aExpression = aList.at(1).trimmed();
306   if (aExpression.isEmpty()) {
307     return;
308   }
309
310   if (isNameExist(aParamName)) {
311     return;
312   }
313   AttributeStringPtr aNameAttr = myParameter->string("variable");
314   aNameAttr->setValue(aParamName.toStdString());
315
316   AttributeStringPtr aExprAttr = myParameter->string("expression");
317   aExprAttr->setValue(aExpression.toStdString());
318   myParameter->execute();
319
320   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
321 }
322
323 FeaturePtr ModuleBase_WidgetDoubleValue::findParameter(const QString& theName) const
324 {
325   SessionPtr aMgr = ModelAPI_Session::get();
326   std::shared_ptr<ModelAPI_Document> aDoc = aMgr->activeDocument();
327   FeaturePtr aParamFeature;
328   int aNbFeatures = aDoc->numInternalFeatures();
329   std::string aName = theName.toStdString();
330   for (int i = 0; i < aNbFeatures; i++) {
331     aParamFeature = aDoc->internalFeature(i);
332     if (aParamFeature && aParamFeature->getKind() == "Parameter") {
333       if (aParamFeature->name() == aName)
334         return aParamFeature;
335     }
336   }
337   return FeaturePtr();
338 }