Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetDoubleValue.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 <Config_Keywords.h>
22 #include <Config_WidgetAPI.h>
23
24 #include <ModelAPI_AttributeDouble.h>
25 #include <ModelAPI_Data.h>
26 #include <ModelAPI_Object.h>
27
28 #include <ModuleBase_ParamSpinBox.h>
29 #include <ModuleBase_Tools.h>
30 #include <ModuleBase_WidgetDoubleValue.h>
31 #include <ModuleBase_IconFactory.h>
32
33 #include <QFormLayout>
34 #include <QLabel>
35 #include <QList>
36 #include <QObject>
37 #include <QPixmap>
38 #include <QString>
39
40 #include <cfloat>
41
42 #ifndef DBL_MAX
43 #define DBL_MAX 1.7976931348623158e+308
44 #endif
45 #ifdef _DEBUG
46 #include <iostream>
47 #endif
48
49 //#define DEBUG_COMPLETE_WITH_PARAMETERS
50
51 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(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   bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
65
66   mySpinBox = new ModuleBase_ParamSpinBox(this);
67   mySpinBox->setAcceptVariables(aAcceptVariables);
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   double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
74   if (isOk) {
75     mySpinBox->setMinimum(aMinVal);
76   } else {
77     mySpinBox->setMinimum(-DBL_MAX);
78   }
79
80   aProp = theData->getProperty(DOUBLE_WDG_MAX);
81   double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
82   if (isOk) {
83     mySpinBox->setMaximum(aMaxVal);
84   } else {
85     mySpinBox->setMaximum(DBL_MAX);
86   }
87
88   aProp = theData->getProperty(DOUBLE_WDG_STEP);
89   double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
90   if (isOk) {
91     double aMinStep = pow(10, -1. * (double) mySpinBox->decimals());
92     if(aStepVal < aMinStep){
93       aStepVal = aMinStep;
94     }
95     mySpinBox->setSingleStep(aStepVal);
96   }
97
98   double aDefVal = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
99   if (isOk) {
100     mySpinBox->setValue(aDefVal);
101   }
102
103   QString aTTip = translate(theData->widgetTooltip());
104   mySpinBox->setToolTip(aTTip);
105   myLabel->setToolTip(aTTip);
106
107   aControlLay->addRow(myLabel, mySpinBox);
108   // we should listen textChanged signal as valueChanged do not send when text is modified
109   connect(mySpinBox, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
110   mySpinBox->setValueEnabled(isValueEnabled());
111 }
112
113 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
114 {
115 }
116
117 void ModuleBase_WidgetDoubleValue::activateCustom()
118 {
119   ModuleBase_ModelWidget::activateCustom();
120 #ifdef DEBUG_COMPLETE_WITH_PARAMETERS
121   QStringList aParameters;
122   ModuleBase_Tools::getParameters(aParameters);
123   mySpinBox->setCompletionList(aParameters);
124 #endif
125 }
126
127 bool ModuleBase_WidgetDoubleValue::resetCustom()
128 {
129   bool aDone = false;
130   if (!isUseReset() || isComputedDefault() || mySpinBox->hasVariable()) {
131     aDone = false;
132   } else {
133     bool isOk;
134     double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
135     // reset the value just if there is a default value definition in the XML definition
136     // if the value can not be found by the default value, do nothing
137     if (isOk) {
138       ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
139       storeValue();
140       aDone = true;
141     }
142   }
143   return aDone;
144 }
145
146 bool ModuleBase_WidgetDoubleValue::storeValueCustom()
147 {
148   DataPtr aData = myFeature->data();
149   AttributeDoublePtr aReal = aData->real(attributeID());
150   if (mySpinBox->hasVariable()) {
151     // Here is a text of a real value or an expression.
152     std::string aText = mySpinBox->text().toStdString();
153     aReal->setText(aText);
154   } else {
155     // it is important to set the empty text value to the attribute before set the value
156     // because setValue tries to calculate the attribute value according to the
157     // attribute current text
158     aReal->setText("");
159     aReal->setValue(mySpinBox->value());
160   }
161   updateObject(myFeature);
162   return true;
163 }
164
165 bool ModuleBase_WidgetDoubleValue::restoreValueCustom()
166 {
167   DataPtr aData = myFeature->data();
168   AttributeDoublePtr aRef = aData->real(attributeID());
169   std::string aTextRepr = aRef->text();
170   if (!aTextRepr.empty()) {
171     ModuleBase_Tools::setSpinText(mySpinBox, QString::fromStdString(aTextRepr));
172   } else {
173     ModuleBase_Tools::setSpinValue(mySpinBox, aRef->isInitialized() ? aRef->value() : 0);
174   }
175   return true;
176 }
177
178 void ModuleBase_WidgetDoubleValue::selectContent()
179 {
180   mySpinBox->selectAll();
181 }
182
183 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
184 {
185   QList<QWidget*> aList;
186   aList.append(mySpinBox);
187   return aList;
188 }
189
190 bool ModuleBase_WidgetDoubleValue::processEnter()
191 {
192   bool isModified = getValueState() == ModifiedInPP;
193   if (isModified) {
194     emit valuesChanged();
195     mySpinBox->selectAll();
196   }
197   return isModified;
198 }