Salome HOME
04f80d8792ac2d21bcbf9e9a36e5dfb658d12610
[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
28 ModuleBase_WidgetDoubleValue::ModuleBase_WidgetDoubleValue(QWidget* theParent, 
29   const Config_WidgetAPI* theData, 
30   const std::string& theParentId)
31   : ModuleBase_ModelWidget(theParent, theData, theParentId)
32 {
33   myContainer = new QWidget(theParent);
34   QHBoxLayout* aControlLay = new QHBoxLayout(myContainer);
35   aControlLay->setContentsMargins(0, 0, 0, 0);
36
37   QString aLabelText = QString::fromStdString(theData->widgetLabel());
38   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
39   myLabel = new QLabel(aLabelText, myContainer);
40   if (!aLabelIcon.isEmpty())
41     myLabel->setPixmap(QPixmap(aLabelIcon));
42   aControlLay->addWidget(myLabel);
43
44   mySpinBox = new QDoubleSpinBox(myContainer);
45   QString anObjName = QString::fromStdString(attributeID());
46   mySpinBox->setObjectName(anObjName);
47
48   bool isOk = false;
49   std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
50   double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
51   if (isOk) {
52     mySpinBox->setMinimum(aMinVal);
53   } else {
54     mySpinBox->setMinimum(-DBL_MAX);
55   }
56
57   aProp = theData->getProperty(DOUBLE_WDG_MAX);
58   double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
59   if (isOk) {
60     mySpinBox->setMaximum(aMaxVal);
61   } else {
62     mySpinBox->setMaximum(DBL_MAX);
63   }
64
65   aProp = theData->getProperty(DOUBLE_WDG_STEP);
66   double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
67   if (isOk) {
68     mySpinBox->setSingleStep(aStepVal);
69   }
70
71   aProp = theData->getProperty(DOUBLE_WDG_DFLT);
72   double aDefVal = QString::fromStdString(aProp).toDouble(&isOk);
73   myHasDefaultValue = isOk;
74   if (isOk) {
75     mySpinBox->setValue(aDefVal);
76   }
77
78   QString aTTip = QString::fromStdString(theData->widgetTooltip());
79   mySpinBox->setToolTip(aTTip);
80
81   aControlLay->addWidget(mySpinBox);
82   aControlLay->setStretch(1, 1);
83
84   connect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
85
86   mySpinBox->installEventFilter(this);
87 }
88
89 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
90 {
91 }
92
93 bool ModuleBase_WidgetDoubleValue::storeValue(ObjectPtr theObject) const
94 {
95   DataPtr aData = theObject->data();
96   AttributeDoublePtr aReal = aData->real(attributeID());
97   if (aReal->value() != mySpinBox->value()) {
98     aReal->setValue(mySpinBox->value());
99     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
100   }
101   return true;
102 }
103
104 bool ModuleBase_WidgetDoubleValue::restoreValue(ObjectPtr theObject)
105 {
106   DataPtr aData = theObject->data();
107   AttributeDoublePtr aRef = aData->real(attributeID());
108
109   bool isBlocked = mySpinBox->blockSignals(true);
110   mySpinBox->setValue(aRef->value());
111   mySpinBox->blockSignals(isBlocked);
112
113   return true;
114 }
115
116 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
117 {
118   QList<QWidget*> aList;
119   aList.append(myLabel);
120   aList.append(mySpinBox);
121   return aList;
122 }
123
124 bool ModuleBase_WidgetDoubleValue::eventFilter(QObject *theObject, QEvent *theEvent)
125 {
126   if (theObject == mySpinBox) {
127     if (theEvent->type() == QEvent::KeyRelease) {
128       QKeyEvent* aKeyEvent = (QKeyEvent*)theEvent;
129       if (aKeyEvent && aKeyEvent->key() == Qt::Key_Return) {
130         emit focusOutWidget(this);
131       }
132       emit keyReleased(attributeID(), (QKeyEvent*) theEvent);
133       return true;
134     }
135   }
136   return ModuleBase_ModelWidget::eventFilter(theObject, theEvent);
137 }