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