]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetDoubleValue.cpp
Salome HOME
35641c2a50e8e6ba0ef8dd452465fa239db6b1bb
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetDoubleValue.cpp
1 // File:        ModuleBase_Widgets.h
2 // Created:     04 June 2014
3 // Author:      Vitaly Smetannikov
4
5 #include <ModuleBase_WidgetDoubleValue.h>
6
7 #include <ModelAPI_AttributeDouble.h>
8 #include <ModelAPI_Data.h>
9
10 #include <Config_Keywords.h>
11 #include <Config_WidgetAPI.h>
12
13 #include <Events_Loop.h>
14 #include <ModelAPI_Events.h>
15
16 #include <QWidget>
17 #include <QLayout>
18 #include <QLabel>
19 #include <QDoubleSpinBox>
20 #include <QEvent>
21 #include <QKeyEvent>
22
23 #ifndef DBL_MAX
24 #define DBL_MAX 1.7976931348623158e+308 
25 #endif
26
27 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(QWidget* theParent,
28                                                            const Config_WidgetAPI* theData,
29                                                            const std::string& theParentId)
30     : ModuleBase_ModelWidget(theParent, theData, theParentId)
31 {
32   myContainer = new QWidget(theParent);
33   QHBoxLayout* aControlLay = new QHBoxLayout(myContainer);
34   aControlLay->setContentsMargins(0, 0, 0, 0);
35
36   QString aLabelText = QString::fromStdString(theData->widgetLabel());
37   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
38   myLabel = new QLabel(aLabelText, myContainer);
39   if (!aLabelIcon.isEmpty())
40     myLabel->setPixmap(QPixmap(aLabelIcon));
41   aControlLay->addWidget(myLabel);
42
43   mySpinBox = new QDoubleSpinBox(myContainer);
44   QString anObjName = QString::fromStdString(attributeID());
45   mySpinBox->setObjectName(anObjName);
46
47   bool isOk = false;
48   std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
49   double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
50   if (isOk) {
51     mySpinBox->setMinimum(aMinVal);
52   } else {
53     mySpinBox->setMinimum(-DBL_MAX);
54   }
55
56   aProp = theData->getProperty(DOUBLE_WDG_MAX);
57   double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
58   if (isOk) {
59     mySpinBox->setMaximum(aMaxVal);
60   } else {
61     mySpinBox->setMaximum(DBL_MAX);
62   }
63
64   aProp = theData->getProperty(DOUBLE_WDG_STEP);
65   double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
66   if (isOk) {
67     mySpinBox->setSingleStep(aStepVal);
68   }
69
70   aProp = theData->getProperty(DOUBLE_WDG_DFLT);
71   double aDefVal = QString::fromStdString(aProp).toDouble(&isOk);
72   if (isOk) {
73     mySpinBox->setValue(aDefVal);
74   }
75
76   QString aTTip = QString::fromStdString(theData->widgetTooltip());
77   mySpinBox->setToolTip(aTTip);
78
79   aControlLay->addWidget(mySpinBox);
80   aControlLay->setStretch(1, 1);
81
82   connect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
83
84   mySpinBox->installEventFilter(this);
85 }
86
87 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
88 {
89 }
90
91 bool ModuleBase_WidgetDoubleValue::storeValue() const
92 {
93   DataPtr aData = myFeature->data();
94   AttributeDoublePtr aReal = aData->real(attributeID());
95   aReal->setValue(mySpinBox->value());
96   updateObject(myFeature);
97   return true;
98 }
99
100 bool ModuleBase_WidgetDoubleValue::restoreValue()
101 {
102   DataPtr aData = myFeature->data();
103   AttributeDoublePtr aRef = aData->real(attributeID());
104
105   bool isBlocked = mySpinBox->blockSignals(true);
106   mySpinBox->setValue(aRef->value());
107   mySpinBox->blockSignals(isBlocked);
108
109   return true;
110 }
111
112 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
113 {
114   QList<QWidget*> aList;
115   aList.append(myLabel);
116   aList.append(mySpinBox);
117   return aList;
118 }
119
120 bool ModuleBase_WidgetDoubleValue::eventFilter(QObject *theObject, QEvent *theEvent)
121 {
122   if (theObject == mySpinBox) {
123     if (theEvent->type() == QEvent::KeyRelease) {
124       QKeyEvent* aKeyEvent = (QKeyEvent*) theEvent;
125       if (aKeyEvent && (aKeyEvent->key() == Qt::Key_Return ||
126                         aKeyEvent->key() == Qt::Key_Enter)) {
127         emit focusOutWidget(this);
128       }
129       emit keyReleased((QKeyEvent*) theEvent);
130       return true;
131     }
132   }
133   return ModuleBase_ModelWidget::eventFilter(theObject, theEvent);
134 }