Salome HOME
Task 2.7: Completion for Int spin box
[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)
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   int aDefVal = QString::fromStdString(getDefaultValue()).toInt(&isOk);
95   if (isOk) {
96     mySpinBox->setValue(aDefVal);
97   }
98
99   QString aTTip = translate(theData->widgetTooltip());
100   mySpinBox->setToolTip(aTTip);
101   myLabel->setToolTip(aTTip);
102
103   aControlLay->addRow(myLabel, mySpinBox);
104   connect(mySpinBox, SIGNAL(valueChanged(int)), this, SIGNAL(valuesModified()));
105 }
106
107 ModuleBase_WidgetIntValue::~ModuleBase_WidgetIntValue()
108 {
109 }
110
111 void ModuleBase_WidgetIntValue::activateCustom()
112 {
113   ModuleBase_ModelWidget::activateCustom();
114   QStringList aParameters;
115   ModuleBase_Tools::getParameters(aParameters);
116   mySpinBox->setCompletionList(aParameters);
117 }
118
119 bool ModuleBase_WidgetIntValue::resetCustom()
120 {
121   bool aDone = false;
122   if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
123     aDone = false;
124   } else {
125     bool isOk;
126     int aDefValue = QString::fromStdString(getDefaultValue()).toInt(&isOk);
127     // reset the value just if there is a default value definition in the XML definition
128     // if the value can not be found by the default value, do nothing
129     if (isOk) {
130       ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
131       storeValue();
132       aDone = true;
133     }
134   }
135   return aDone;
136 }
137
138 bool ModuleBase_WidgetIntValue::storeValueCustom()
139 {
140   DataPtr aData = myFeature->data();
141   AttributeIntegerPtr anAttribute = aData->integer(attributeID());
142   if (mySpinBox->hasVariable()) {
143     // Here is a text of a real value or an expression.
144     QString aText = mySpinBox->text();
145     if (aText.contains('=')) {
146       if (!myParameter.get()) {
147         myParameter = ModuleBase_Tools::createParameter(aText);
148         if (!myParameter.get()) {
149           anAttribute->setExpressionError("Parameter cannot be created");
150           anAttribute->setExpressionInvalid(true);
151           updateObject(myFeature);
152           return false;
153         } else if (anAttribute->expressionInvalid()) {
154           anAttribute->setExpressionError("");
155           anAttribute->setExpressionInvalid(false);
156         }
157       } else {
158         ModuleBase_Tools::editParameter(myParameter, aText);
159       }
160       aText = aText.split('=').at(0) + "=";
161     } else if (myParameter.get()) {
162       // Nullyfy the parameter reference without deletion of the created
163       myParameter = FeaturePtr();
164     }
165     anAttribute->setText(aText.toStdString());
166   } else {
167     // it is important to set the empty text value to the attribute before set the value
168     // because setValue tries to calculate the attribute value according to the
169     // attribute current text
170     anAttribute->setText("");
171     anAttribute->setValue(mySpinBox->value());
172   }
173   updateObject(myFeature);
174   return true;
175 }
176
177 bool ModuleBase_WidgetIntValue::restoreValueCustom()
178 {
179   DataPtr aData = myFeature->data();
180   AttributeIntegerPtr anAttribute = aData->integer(attributeID());
181   std::string aTextRepr = anAttribute->text();
182   if (!aTextRepr.empty()) {
183     QString aText = QString::fromStdString(aTextRepr);
184     if (aText.endsWith('=')) {
185       if (!myParameter.get()) {
186         QString aName = aText.left(aText.indexOf('=')).trimmed();
187         myParameter = ModuleBase_Tools::findParameter(aName);
188       }
189       /// If myParameter is empty then it was not created because of an error
190       if (!myParameter.get())
191         return false;
192
193       AttributeStringPtr aExprAttr = myParameter->string("expression");
194       aText += aExprAttr->value().c_str();
195     }
196     ModuleBase_Tools::setSpinText(mySpinBox, aText);
197   } else {
198     ModuleBase_Tools::setSpinValue(mySpinBox, anAttribute->value());
199   }
200   return true;
201 }
202
203 void ModuleBase_WidgetIntValue::selectContent()
204 {
205   mySpinBox->selectAll();
206 }
207
208 QList<QWidget*> ModuleBase_WidgetIntValue::getControls() const
209 {
210   QList<QWidget*> aList;
211   aList.append(mySpinBox);
212   return aList;
213 }
214
215 bool ModuleBase_WidgetIntValue::processEnter()
216 {
217   bool isModified = getValueState() == ModifiedInPP;
218   if (isModified) {
219     emit valuesChanged();
220     mySpinBox->selectAll();
221   }
222   return isModified;
223 }