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