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