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