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