Salome HOME
Fix for the issue #2753 : error when dump/load script
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetIntValue.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 <ModuleBase_WidgetIntValue.h>
22 #include <ModuleBase_ParamSpinBox.h>
23 #include <ModuleBase_Tools.h>
24 #include <ModuleBase_ParamSpinBox.h>
25 #include <ModuleBase_IconFactory.h>
26
27 #include <ModelAPI_AttributeInteger.h>
28 #include <ModelAPI_Data.h>
29 #include <ModelAPI_AttributeString.h>
30
31 #include <Config_Keywords.h>
32 #include <Config_WidgetAPI.h>
33
34 #include <Events_Loop.h>
35 #include <ModelAPI_Events.h>
36
37 #include <QWidget>
38 #include <QFormLayout>
39 #include <QLabel>
40 #include <QEvent>
41 #include <QTimer>
42
43 #include <math.h>
44
45 #ifndef INT_MAX
46 #define INT_MAX   2147483647
47 #endif
48
49 #ifdef _DEBUG
50 #include <iostream>
51 #endif
52
53 ModuleBase_WidgetIntValue::ModuleBase_WidgetIntValue(QWidget* theParent,
54                                                      const Config_WidgetAPI* theData)
55 : ModuleBase_ModelWidget(theParent, theData), myHasDefault(false)
56 {
57   QFormLayout* aControlLay = new QFormLayout(this);
58   ModuleBase_Tools::adjustMargins(aControlLay);
59
60   QString aLabelText = translate(theData->widgetLabel());
61   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
62   myLabel = new QLabel(aLabelText, this);
63   if (!aLabelIcon.isEmpty())
64     myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
65
66   mySpinBox = new ModuleBase_ParamSpinBox(this);
67   mySpinBox->setDecimals(0);
68   QString anObjName = QString::fromStdString(attributeID());
69   mySpinBox->setObjectName(anObjName);
70
71   bool isOk = false;
72   std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
73   int aMinVal = QString::fromStdString(aProp).toInt(&isOk);
74   if (isOk) {
75     mySpinBox->setMinimum(aMinVal);
76   } else {
77     mySpinBox->setMinimum(-INT_MAX);
78   }
79
80   aProp = theData->getProperty(DOUBLE_WDG_MAX);
81   int aMaxVal = QString::fromStdString(aProp).toInt(&isOk);
82   if (isOk) {
83     mySpinBox->setMaximum(aMaxVal);
84   } else {
85     mySpinBox->setMaximum(INT_MAX);
86   }
87
88   aProp = theData->getProperty(DOUBLE_WDG_STEP);
89   int aStepVal = QString::fromStdString(aProp).toInt(&isOk);
90   if (isOk) {
91     mySpinBox->setSingleStep(aStepVal);
92   }
93
94   myDefVal = QString::fromStdString(getDefaultValue()).toInt(&myHasDefault);
95   if (myHasDefault)
96     mySpinBox->setValue(myDefVal);
97
98   QString aTTip = translate(theData->widgetTooltip());
99   mySpinBox->setToolTip(aTTip);
100   myLabel->setToolTip(aTTip);
101
102   aControlLay->addRow(myLabel, mySpinBox);
103   connect(mySpinBox, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
104 }
105
106 ModuleBase_WidgetIntValue::~ModuleBase_WidgetIntValue()
107 {
108 }
109
110 void ModuleBase_WidgetIntValue::activateCustom()
111 {
112   ModuleBase_ModelWidget::activateCustom();
113   QStringList aParameters;
114   ModuleBase_Tools::getParameters(aParameters);
115   mySpinBox->setCompletionList(aParameters);
116 }
117
118 bool ModuleBase_WidgetIntValue::resetCustom()
119 {
120   bool aDone = false;
121   if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
122     aDone = false;
123   } else {
124     bool isOk;
125     int aDefValue = QString::fromStdString(getDefaultValue()).toInt(&isOk);
126     // reset the value just if there is a default value definition in the XML definition
127     // if the value can not be found by the default value, do nothing
128     if (isOk) {
129       ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
130       storeValue();
131       aDone = true;
132     }
133   }
134   return aDone;
135 }
136
137 bool ModuleBase_WidgetIntValue::storeValueCustom()
138 {
139   DataPtr aData = myFeature->data();
140   AttributeIntegerPtr anAttribute = aData->integer(attributeID());
141   if (mySpinBox->hasVariable()) {
142     // Here is a text of a real value or an expression.
143     QString aText = mySpinBox->text();
144     if (aText.contains('=')) {
145       if (!myParameter.get()) {
146         myParameter = ModuleBase_Tools::createParameter(aText);
147         if (!myParameter.get()) {
148           anAttribute->setExpressionError("Parameter cannot be created");
149           anAttribute->setExpressionInvalid(true);
150           updateObject(myFeature);
151           return false;
152         } else if (anAttribute->expressionInvalid()) {
153           anAttribute->setExpressionError("");
154           anAttribute->setExpressionInvalid(false);
155         }
156       } else {
157         ModuleBase_Tools::editParameter(myParameter, aText);
158       }
159       aText = aText.split('=').at(0);
160     } else if (myParameter.get()) {
161       // Nullyfy the parameter reference without deletion of the created
162       myParameter = FeaturePtr();
163     }
164     anAttribute->setText(aText.toStdString());
165   } else {
166     // it is important to set the empty text value to the attribute before set the value
167     // because setValue tries to calculate the attribute value according to the
168     // attribute current text
169     if (anAttribute->expressionInvalid()) {
170       anAttribute->setExpressionError("");
171       anAttribute->setExpressionInvalid(false);
172     }
173     anAttribute->setText("");
174     anAttribute->setValue(mySpinBox->value());
175   }
176   updateObject(myFeature);
177   return true;
178 }
179
180 bool ModuleBase_WidgetIntValue::restoreValueCustom()
181 {
182   DataPtr aData = myFeature->data();
183   AttributeIntegerPtr anAttribute = aData->integer(attributeID());
184   std::string aTextRepr = anAttribute->text();
185   if (!aTextRepr.empty()) {
186     QString aText = QString::fromStdString(aTextRepr);
187     //if (aText.endsWith('=')) {
188     //  if (!myParameter.get()) {
189     //    QString aName = aText.left(aText.indexOf('=')).trimmed();
190     //    myParameter = ModuleBase_Tools::findParameter(aName);
191     //  }
192     //  /// If myParameter is empty then it was not created because of an error
193     //  if (!myParameter.get())
194     //    return false;
195
196     //  AttributeStringPtr aExprAttr = myParameter->string("expression");
197     //  aText += aExprAttr->value().c_str();
198     //}
199     ModuleBase_Tools::setSpinText(mySpinBox, aText);
200   } else {
201     ModuleBase_Tools::setSpinValue(mySpinBox,
202       anAttribute->isInitialized() ? anAttribute->value() : myDefVal);
203     if (anAttribute->isInitialized() && anAttribute->expressionInvalid()) {
204       anAttribute->setExpressionError("");
205       anAttribute->setExpressionInvalid(false);
206     }
207     if ((!anAttribute->isInitialized()) && myHasDefault)
208       anAttribute->setValue(myDefVal);
209   }
210   return true;
211 }
212
213 void ModuleBase_WidgetIntValue::selectContent()
214 {
215   mySpinBox->selectAll();
216 }
217
218 QList<QWidget*> ModuleBase_WidgetIntValue::getControls() const
219 {
220   QList<QWidget*> aList;
221   aList.append(mySpinBox);
222   return aList;
223 }
224
225 bool ModuleBase_WidgetIntValue::processEnter()
226 {
227   bool isModified = getValueState() == ModifiedInPP;
228   if (isModified) {
229     emit valuesChanged();
230     mySpinBox->selectAll();
231   }
232   return isModified;
233 }