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