Salome HOME
ModuleBase_WidgetChoice is extended to use in a combo box a list of values read from...
[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_ParamIntSpinBox.h>
25 #include <ModuleBase_IconFactory.h>
26
27 #include <ModelAPI_AttributeInteger.h>
28 #include <ModelAPI_Data.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)
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_ParamIntSpinBox(this);
66   QString anObjName = QString::fromStdString(attributeID());
67   mySpinBox->setObjectName(anObjName);
68
69   bool isOk = false;
70   std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
71   int aMinVal = QString::fromStdString(aProp).toInt(&isOk);
72   if (isOk) {
73     mySpinBox->setMinimum(aMinVal);
74   } else {
75     mySpinBox->setMinimum(-INT_MAX);
76   }
77
78   aProp = theData->getProperty(DOUBLE_WDG_MAX);
79   int aMaxVal = QString::fromStdString(aProp).toInt(&isOk);
80   if (isOk) {
81     mySpinBox->setMaximum(aMaxVal);
82   } else {
83     mySpinBox->setMaximum(INT_MAX);
84   }
85
86   aProp = theData->getProperty(DOUBLE_WDG_STEP);
87   int aStepVal = QString::fromStdString(aProp).toInt(&isOk);
88   if (isOk) {
89     mySpinBox->setSingleStep(aStepVal);
90   }
91
92   int aDefVal = QString::fromStdString(getDefaultValue()).toInt(&isOk);
93   if (isOk) {
94     mySpinBox->setValue(aDefVal);
95   }
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(valueChanged(int)), this, SIGNAL(valuesModified()));
103 }
104
105 ModuleBase_WidgetIntValue::~ModuleBase_WidgetIntValue()
106 {
107 }
108
109 bool ModuleBase_WidgetIntValue::resetCustom()
110 {
111   bool aDone = false;
112   if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
113     aDone = false;
114   } else {
115     bool isOk;
116     int aDefValue = QString::fromStdString(getDefaultValue()).toInt(&isOk);
117     // reset the value just if there is a default value definition in the XML definition
118     // if the value can not be found by the default value, do nothing
119     if (isOk) {
120       ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
121       storeValue();
122       aDone = true;
123     }
124   }
125   return aDone;
126 }
127
128 bool ModuleBase_WidgetIntValue::storeValueCustom()
129 {
130   DataPtr aData = myFeature->data();
131   AttributeIntegerPtr anAttribute = aData->integer(attributeID());
132   if (mySpinBox->hasVariable()) {
133     // Here is a text of a real value or an expression.
134     std::string aText = mySpinBox->text().toStdString();
135     anAttribute->setText(aText);
136   } else {
137     // it is important to set the empty text value to the attribute before set the value
138     // because setValue tries to calculate the attribute value according to the
139     // attribute current text
140     anAttribute->setText("");
141     anAttribute->setValue(mySpinBox->value());
142   }
143   updateObject(myFeature);
144   return true;
145 }
146
147 bool ModuleBase_WidgetIntValue::restoreValueCustom()
148 {
149   DataPtr aData = myFeature->data();
150   AttributeIntegerPtr anAttribute = aData->integer(attributeID());
151   std::string aTextRepr = anAttribute->text();
152   if (!aTextRepr.empty()) {
153     ModuleBase_Tools::setSpinText(mySpinBox, QString::fromStdString(aTextRepr));
154   } else {
155     ModuleBase_Tools::setSpinValue(mySpinBox, anAttribute->value());
156   }
157   return true;
158 }
159
160 void ModuleBase_WidgetIntValue::selectContent()
161 {
162   mySpinBox->selectAll();
163 }
164
165 QList<QWidget*> ModuleBase_WidgetIntValue::getControls() const
166 {
167   QList<QWidget*> aList;
168   aList.append(mySpinBox);
169   return aList;
170 }
171
172 bool ModuleBase_WidgetIntValue::processEnter()
173 {
174   bool isModified = getValueState() == ModifiedInPP;
175   if (isModified) {
176     emit valuesChanged();
177     mySpinBox->selectAll();
178   }
179   return isModified;
180 }