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