Salome HOME
Apply modifications in the editors by Enter/Tab event only.
[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   connect(mySpinBox, SIGNAL(valueChanged(int)), this, SIGNAL(valuesModified()));
94 #else
95   connect(mySpinBox, SIGNAL(valueChanged(int)), this, SIGNAL(valuesChanged()));
96 #endif
97 }
98
99 ModuleBase_WidgetIntValue::~ModuleBase_WidgetIntValue()
100 {
101 }
102
103 bool ModuleBase_WidgetIntValue::reset()
104 {
105   bool aDone = false;
106   if (!isUseReset() || isComputedDefault()) {
107     aDone = false;
108   } else {
109     bool isOk;
110     int aDefValue = QString::fromStdString(getDefaultValue()).toInt(&isOk);
111     // reset the value just if there is a default value definition in the XML definition
112     // if the double value can not be found by the default value, do nothing
113     if (isOk) {
114       bool isBlocked = mySpinBox->blockSignals(true);
115       mySpinBox->setValue(isOk ? aDefValue : 0);
116       mySpinBox->blockSignals(isBlocked);
117       storeValueCustom();
118       aDone = true;
119     }
120   }
121   return aDone;
122 }
123
124 bool ModuleBase_WidgetIntValue::storeValueCustom() const
125 {
126   DataPtr aData = myFeature->data();
127   AttributeIntegerPtr aIntVal = aData->integer(attributeID());
128   int aVal = mySpinBox->value();
129   aIntVal->setValue(mySpinBox->value());
130   updateObject(myFeature);
131   return true;
132 }
133
134 bool ModuleBase_WidgetIntValue::restoreValueCustom()
135 {
136   DataPtr aData = myFeature->data();
137   AttributeIntegerPtr aRef = aData->integer(attributeID());
138   bool isBlocked = mySpinBox->blockSignals(true);
139   mySpinBox->setValue(aRef->value());
140   mySpinBox->blockSignals(isBlocked);
141   return true;
142 }
143
144 QList<QWidget*> ModuleBase_WidgetIntValue::getControls() const
145 {
146   QList<QWidget*> aList;
147   aList.append(mySpinBox);
148   return aList;
149 }