]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetIntValue.cpp
Salome HOME
Undoes of the previous modifications.
[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
88   aControlLay->addRow(myLabel, mySpinBox);
89 #ifdef APPLY_BY_ENTER_OR_TAB
90   // Apply widget value change by enter/tab event.
91   connect(mySpinBox, SIGNAL(editingFinished()), this, SIGNAL(valuesChanged()));
92 #else
93   connect(mySpinBox, SIGNAL(valueChanged(int)), this, SIGNAL(valuesChanged()));
94 #endif
95 }
96
97 ModuleBase_WidgetIntValue::~ModuleBase_WidgetIntValue()
98 {
99 }
100
101 bool ModuleBase_WidgetIntValue::reset()
102 {
103   bool aDone = false;
104   if (!isUseReset() || isComputedDefault()) {
105     aDone = false;
106   } else {
107     bool isOk;
108     int aDefValue = QString::fromStdString(getDefaultValue()).toInt(&isOk);
109     // reset the value just if there is a default value definition in the XML definition
110     // if the double value can not be found by the default value, do nothing
111     if (isOk) {
112       bool isBlocked = mySpinBox->blockSignals(true);
113       mySpinBox->setValue(isOk ? aDefValue : 0);
114       mySpinBox->blockSignals(isBlocked);
115       storeValueCustom();
116       aDone = true;
117     }
118   }
119   return aDone;
120 }
121
122 bool ModuleBase_WidgetIntValue::storeValueCustom() const
123 {
124   DataPtr aData = myFeature->data();
125   AttributeIntegerPtr aIntVal = aData->integer(attributeID());
126   int aVal = mySpinBox->value();
127   aIntVal->setValue(mySpinBox->value());
128   updateObject(myFeature);
129   return true;
130 }
131
132 bool ModuleBase_WidgetIntValue::restoreValueCustom()
133 {
134   DataPtr aData = myFeature->data();
135   AttributeIntegerPtr aRef = aData->integer(attributeID());
136   bool isBlocked = mySpinBox->blockSignals(true);
137   mySpinBox->setValue(aRef->value());
138   mySpinBox->blockSignals(isBlocked);
139   return true;
140 }
141
142 QList<QWidget*> ModuleBase_WidgetIntValue::getControls() const
143 {
144   QList<QWidget*> aList;
145   aList.append(mySpinBox);
146   return aList;
147 }