Salome HOME
95045d0c20bd3857783ea1e1e5d6f71541409095
[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
11 #include <ModelAPI_AttributeInteger.h>
12 #include <ModelAPI_Data.h>
13
14 #include <Config_Keywords.h>
15 #include <Config_WidgetAPI.h>
16
17 #include <Events_Loop.h>
18 #include <ModelAPI_Events.h>
19
20 #include <QWidget>
21 #include <QFormLayout>
22 #include <QLabel>
23 #include <QEvent>
24 #include <QTimer>
25 #include <QSpinBox>
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 //#define APPLY_BY_ENTER_OR_TAB
38
39 ModuleBase_WidgetIntValue::ModuleBase_WidgetIntValue(QWidget* theParent,
40                                                            const Config_WidgetAPI* theData,
41                                                            const std::string& theParentId)
42     : ModuleBase_ModelWidget(theParent, theData, theParentId)
43 {
44   QFormLayout* aControlLay = new QFormLayout(this);
45   ModuleBase_Tools::adjustMargins(aControlLay);
46
47   QString aLabelText = QString::fromStdString(theData->widgetLabel());
48   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
49   myLabel = new QLabel(aLabelText, this);
50   if (!aLabelIcon.isEmpty())
51     myLabel->setPixmap(QPixmap(aLabelIcon));
52
53   mySpinBox = new QSpinBox(this);
54   QString anObjName = QString::fromStdString(attributeID());
55   mySpinBox->setObjectName(anObjName);
56
57   bool isOk = false;
58   std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
59   int aMinVal = QString::fromStdString(aProp).toInt(&isOk);
60   if (isOk) {
61     mySpinBox->setMinimum(aMinVal);
62   } else {
63     mySpinBox->setMinimum(-INT_MAX);
64   }
65
66   aProp = theData->getProperty(DOUBLE_WDG_MAX);
67   int aMaxVal = QString::fromStdString(aProp).toInt(&isOk);
68   if (isOk) {
69     mySpinBox->setMaximum(aMaxVal);
70   } else {
71     mySpinBox->setMaximum(INT_MAX);
72   }
73
74   aProp = theData->getProperty(DOUBLE_WDG_STEP);
75   int aStepVal = QString::fromStdString(aProp).toInt(&isOk);
76   if (isOk) {
77     mySpinBox->setSingleStep(aStepVal);
78   }
79
80   int aDefVal = QString::fromStdString(getDefaultValue()).toInt(&isOk);
81   if (isOk) {
82     mySpinBox->setValue(aDefVal);
83   }
84
85   QString aTTip = QString::fromStdString(theData->widgetTooltip());
86   mySpinBox->setToolTip(aTTip);
87   myLabel->setToolTip(aTTip);
88
89   aControlLay->addRow(myLabel, mySpinBox);
90 #ifdef APPLY_BY_ENTER_OR_TAB
91   // Apply widget value change by enter/tab event.
92   connect(mySpinBox, SIGNAL(editingFinished()), this, SIGNAL(valuesChanged()));
93 #else
94   connect(mySpinBox, SIGNAL(valueChanged(int)), this, SIGNAL(valuesChanged()));
95 #endif
96 }
97
98 ModuleBase_WidgetIntValue::~ModuleBase_WidgetIntValue()
99 {
100 }
101
102 bool ModuleBase_WidgetIntValue::reset()
103 {
104   bool aDone = false;
105   if (!isUseReset() || isComputedDefault()) {
106     aDone = false;
107   } else {
108     bool isOk;
109     int aDefValue = QString::fromStdString(getDefaultValue()).toInt(&isOk);
110     // reset the value just if there is a default value definition in the XML definition
111     // if the double value can not be found by the default value, do nothing
112     if (isOk) {
113       bool isBlocked = mySpinBox->blockSignals(true);
114       mySpinBox->setValue(isOk ? aDefValue : 0);
115       mySpinBox->blockSignals(isBlocked);
116       storeValueCustom();
117       aDone = true;
118     }
119   }
120   return aDone;
121 }
122
123 bool ModuleBase_WidgetIntValue::storeValueCustom() const
124 {
125   DataPtr aData = myFeature->data();
126   AttributeIntegerPtr aIntVal = aData->integer(attributeID());
127   int aVal = mySpinBox->value();
128   aIntVal->setValue(mySpinBox->value());
129   updateObject(myFeature);
130   return true;
131 }
132
133 bool ModuleBase_WidgetIntValue::restoreValueCustom()
134 {
135   DataPtr aData = myFeature->data();
136   AttributeIntegerPtr aRef = aData->integer(attributeID());
137   bool isBlocked = mySpinBox->blockSignals(true);
138   mySpinBox->setValue(aRef->value());
139   mySpinBox->blockSignals(isBlocked);
140   return true;
141 }
142
143 QList<QWidget*> ModuleBase_WidgetIntValue::getControls() const
144 {
145   QList<QWidget*> aList;
146   aList.append(mySpinBox);
147   return aList;
148 }