Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include <ModuleBase_WidgetIntValue.h>
21 #include <ModuleBase_ParamSpinBox.h>
22 #include <ModuleBase_Tools.h>
23 #include <ModuleBase_ParamIntSpinBox.h>
24 #include <ModuleBase_IconFactory.h>
25
26 #include <ModelAPI_AttributeInteger.h>
27 #include <ModelAPI_Data.h>
28
29 #include <Config_Keywords.h>
30 #include <Config_WidgetAPI.h>
31
32 #include <Events_Loop.h>
33 #include <ModelAPI_Events.h>
34
35 #include <QWidget>
36 #include <QFormLayout>
37 #include <QLabel>
38 #include <QEvent>
39 #include <QTimer>
40
41 #include <math.h>
42
43 #ifndef INT_MAX
44 #define INT_MAX   2147483647
45 #endif
46
47 #ifdef _DEBUG
48 #include <iostream>
49 #endif
50
51 ModuleBase_WidgetIntValue::ModuleBase_WidgetIntValue(QWidget* theParent,
52                                                      const Config_WidgetAPI* theData)
53 : ModuleBase_ModelWidget(theParent, theData)
54 {
55   QFormLayout* aControlLay = new QFormLayout(this);
56   ModuleBase_Tools::adjustMargins(aControlLay);
57
58   QString aLabelText = translate(theData->widgetLabel());
59   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
60   myLabel = new QLabel(aLabelText, this);
61   if (!aLabelIcon.isEmpty())
62     myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
63
64   mySpinBox = new ModuleBase_ParamIntSpinBox(this);
65   QString anObjName = QString::fromStdString(attributeID());
66   mySpinBox->setObjectName(anObjName);
67
68   bool isOk = false;
69   std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
70   int aMinVal = QString::fromStdString(aProp).toInt(&isOk);
71   if (isOk) {
72     mySpinBox->setMinimum(aMinVal);
73   } else {
74     mySpinBox->setMinimum(-INT_MAX);
75   }
76
77   aProp = theData->getProperty(DOUBLE_WDG_MAX);
78   int aMaxVal = QString::fromStdString(aProp).toInt(&isOk);
79   if (isOk) {
80     mySpinBox->setMaximum(aMaxVal);
81   } else {
82     mySpinBox->setMaximum(INT_MAX);
83   }
84
85   aProp = theData->getProperty(DOUBLE_WDG_STEP);
86   int aStepVal = QString::fromStdString(aProp).toInt(&isOk);
87   if (isOk) {
88     mySpinBox->setSingleStep(aStepVal);
89   }
90
91   int aDefVal = QString::fromStdString(getDefaultValue()).toInt(&isOk);
92   if (isOk) {
93     mySpinBox->setValue(aDefVal);
94   }
95
96   QString aTTip = translate(theData->widgetTooltip());
97   mySpinBox->setToolTip(aTTip);
98   myLabel->setToolTip(aTTip);
99
100   aControlLay->addRow(myLabel, mySpinBox);
101   connect(mySpinBox, SIGNAL(valueChanged(int)), this, SIGNAL(valuesModified()));
102 }
103
104 ModuleBase_WidgetIntValue::~ModuleBase_WidgetIntValue()
105 {
106 }
107
108 bool ModuleBase_WidgetIntValue::resetCustom()
109 {
110   bool aDone = false;
111   if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
112     aDone = false;
113   } else {
114     bool isOk;
115     int aDefValue = QString::fromStdString(getDefaultValue()).toInt(&isOk);
116     // reset the value just if there is a default value definition in the XML definition
117     // if the value can not be found by the default value, do nothing
118     if (isOk) {
119       ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
120       storeValue();
121       aDone = true;
122     }
123   }
124   return aDone;
125 }
126
127 bool ModuleBase_WidgetIntValue::storeValueCustom()
128 {
129   DataPtr aData = myFeature->data();
130   AttributeIntegerPtr anAttribute = aData->integer(attributeID());
131   if (mySpinBox->hasVariable()) {
132     // Here is a text of a real value or an expression.
133     std::string aText = mySpinBox->text().toStdString();
134     anAttribute->setText(aText);
135   } else {
136     // it is important to set the empty text value to the attribute before set the value
137     // because setValue tries to calculate the attribute value according to the
138     // attribute current text
139     anAttribute->setText("");
140     anAttribute->setValue(mySpinBox->value());
141   }
142   updateObject(myFeature);
143   return true;
144 }
145
146 bool ModuleBase_WidgetIntValue::restoreValueCustom()
147 {
148   DataPtr aData = myFeature->data();
149   AttributeIntegerPtr anAttribute = aData->integer(attributeID());
150   std::string aTextRepr = anAttribute->text();
151   if (!aTextRepr.empty()) {
152     ModuleBase_Tools::setSpinText(mySpinBox, QString::fromStdString(aTextRepr));
153   } else {
154     ModuleBase_Tools::setSpinValue(mySpinBox, anAttribute->value());
155   }
156   return true;
157 }
158
159 void ModuleBase_WidgetIntValue::selectContent()
160 {
161   mySpinBox->selectAll();
162 }
163
164 QList<QWidget*> ModuleBase_WidgetIntValue::getControls() const
165 {
166   QList<QWidget*> aList;
167   aList.append(mySpinBox);
168   return aList;
169 }
170
171 bool ModuleBase_WidgetIntValue::processEnter()
172 {
173   bool isModified = getValueState() == ModifiedInPP;
174   if (isModified) {
175     emit valuesChanged();
176     mySpinBox->selectAll();
177   }
178   return isModified;
179 }