Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[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 <Model_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   myLabel->setPixmap(QPixmap(aLabelIcon));
39   aControlLay->addWidget(myLabel);
40
41   mySpinBox = new QDoubleSpinBox(myContainer);
42   QString anObjName = QString::fromStdString(attributeID());
43   mySpinBox->setObjectName(anObjName);
44
45   bool isOk = false;
46   std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
47   double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
48   if (isOk) {
49     mySpinBox->setMinimum(aMinVal);
50   } else {
51     mySpinBox->setMinimum(-DBL_MAX);
52   }
53
54   aProp = theData->getProperty(DOUBLE_WDG_MAX);
55   double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
56   if (isOk) {
57     mySpinBox->setMaximum(aMaxVal);
58   } else {
59     mySpinBox->setMaximum(DBL_MAX);
60   }
61
62   aProp = theData->getProperty(DOUBLE_WDG_STEP);
63   double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
64   if (isOk) {
65     mySpinBox->setSingleStep(aStepVal);
66   }
67
68   aProp = theData->getProperty(DOUBLE_WDG_DFLT);
69   double aDefVal = QString::fromStdString(aProp).toDouble(&isOk);
70   myHasDefaultValue = isOk;
71   if (isOk) {
72     mySpinBox->setValue(aDefVal);
73   }
74
75   QString aTTip = QString::fromStdString(theData->widgetTooltip());
76   mySpinBox->setToolTip(aTTip);
77
78   aControlLay->addWidget(mySpinBox);
79   aControlLay->setStretch(1, 1);
80
81   connect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
82
83   mySpinBox->installEventFilter(this);
84 }
85
86 ModuleBase_WidgetDoubleValue::~ModuleBase_WidgetDoubleValue()
87 {
88 }
89
90 bool ModuleBase_WidgetDoubleValue::storeValue(FeaturePtr theFeature) const
91 {
92   DataPtr aData = theFeature->data();
93   AttributeDoublePtr aReal = aData->real(attributeID());
94   if (aReal->value() != mySpinBox->value()) {
95     aReal->setValue(mySpinBox->value());
96     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
97   }
98   return true;
99 }
100
101 bool ModuleBase_WidgetDoubleValue::restoreValue(FeaturePtr theFeature)
102 {
103   DataPtr aData = theFeature->data();
104   AttributeDoublePtr aRef = aData->real(attributeID());
105
106   bool isBlocked = mySpinBox->blockSignals(true);
107   mySpinBox->setValue(aRef->value());
108   mySpinBox->blockSignals(isBlocked);
109
110   return true;
111 }
112
113 QList<QWidget*> ModuleBase_WidgetDoubleValue::getControls() const
114 {
115   QList<QWidget*> aList;
116   aList.append(myLabel);
117   aList.append(mySpinBox);
118   return aList;
119 }
120
121 bool ModuleBase_WidgetDoubleValue::eventFilter(QObject *theObject, QEvent *theEvent)
122 {
123   if (theObject == mySpinBox) {
124     if (theEvent->type() == QEvent::KeyRelease) {
125       emit keyReleased(attributeID(), (QKeyEvent*) theEvent);
126       return true;
127     }
128   }
129   return ModuleBase_ModelWidget::eventFilter(theObject, theEvent);
130 }