Salome HOME
Fix pyconfig redefined declarations for _XOPEN_SOURCE and _POSIX_C_SOURCE
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetIntValue.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_Widgets.h
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 ModuleBase_WidgetIntValue::ModuleBase_WidgetIntValue(QWidget* theParent,
38                                                            const Config_WidgetAPI* theData,
39                                                            const std::string& theParentId)
40     : ModuleBase_ModelWidget(theParent, theData, theParentId)
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(QPixmap(aLabelIcon));
50
51   mySpinBox = new QSpinBox(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
86   aControlLay->addRow(myLabel, mySpinBox);
87   connect(mySpinBox, SIGNAL(valueChanged(int)), this, SIGNAL(valuesChanged()));
88 }
89
90 ModuleBase_WidgetIntValue::~ModuleBase_WidgetIntValue()
91 {
92 }
93
94 void ModuleBase_WidgetIntValue::reset()
95 {
96   if (!isUseReset())
97     return;
98
99   if (isComputedDefault()) {
100     return;
101     //if (myFeature->compute(myAttributeID))
102     //  restoreValue();
103   } else {
104     bool isOk;
105     int aDefValue = QString::fromStdString(getDefaultValue()).toInt(&isOk);
106     // reset the value just if there is a default value definition in the XML definition
107     // if the double value can not be found by the default value, do nothing
108     if (isOk) {
109       bool isBlocked = mySpinBox->blockSignals(true);
110       mySpinBox->setValue(isOk ? aDefValue : 0);
111       mySpinBox->blockSignals(isBlocked);
112       storeValueCustom();
113     }
114   }
115 }
116
117 bool ModuleBase_WidgetIntValue::storeValueCustom() const
118 {
119   DataPtr aData = myFeature->data();
120   AttributeIntegerPtr aIntVal = aData->integer(attributeID());
121   int aVal = mySpinBox->value();
122   aIntVal->setValue(mySpinBox->value());
123   updateObject(myFeature);
124   return true;
125 }
126
127 bool ModuleBase_WidgetIntValue::restoreValue()
128 {
129   DataPtr aData = myFeature->data();
130   AttributeIntegerPtr aRef = aData->integer(attributeID());
131   bool isBlocked = mySpinBox->blockSignals(true);
132   mySpinBox->setValue(aRef->value());
133   mySpinBox->blockSignals(isBlocked);
134   return true;
135 }
136
137 QList<QWidget*> ModuleBase_WidgetIntValue::getControls() const
138 {
139   QList<QWidget*> aList;
140   aList.append(mySpinBox);
141   return aList;
142 }