]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_Widgets.cpp
Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[modules/shaper.git] / src / ModuleBase / ModuleBase_Widgets.cpp
1 // File:        ModuleBase_Widgets.h
2 // Created:     04 June 2014
3 // Author:      Vitaly Smetannikov
4
5 #include "ModuleBase_Widgets.h"
6
7 #include <ModelAPI_AttributeDouble.h>
8 #include <ModelAPI_AttributeBoolean.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 <Model_Events.h>
16
17 #include <QWidget>
18 #include <QLayout>
19 #include <QLabel>
20 #include <QDoubleSpinBox>
21 #include <QCheckBox>
22
23
24 ModuleBase_DoubleValueWidget::ModuleBase_DoubleValueWidget(QWidget* theParent, const Config_WidgetAPI* theData)
25   : ModuleBase_ModelWidget(theParent)
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   myAttributeID = theData->widgetId();
38   mySpinBox = new QDoubleSpinBox(myContainer);
39   QString anObjName = QString::fromStdString(myAttributeID);
40   mySpinBox->setObjectName(anObjName);
41
42   bool isOk = false;
43   std::string aProp = theData->getProperty(DOUBLE_WDG_MIN);
44   double aMinVal = QString::fromStdString(aProp).toDouble(&isOk);
45   if (isOk) {
46     mySpinBox->setMinimum(aMinVal);
47   } else {
48     mySpinBox->setMinimum(-DBL_MAX);
49   }
50
51   aProp = theData->getProperty(DOUBLE_WDG_MAX);
52   double aMaxVal = QString::fromStdString(aProp).toDouble(&isOk);
53   if (isOk) {
54     mySpinBox->setMaximum(aMaxVal);
55   } else {
56     mySpinBox->setMaximum(DBL_MAX);
57   }
58
59   aProp = theData->getProperty(DOUBLE_WDG_STEP);
60   double aStepVal = QString::fromStdString(aProp).toDouble(&isOk);
61   if (isOk) {
62     mySpinBox->setSingleStep(aStepVal);
63   }
64
65   aProp = theData->getProperty(DOUBLE_WDG_DFLT);
66   double aDefVal = QString::fromStdString(aProp).toDouble(&isOk);
67   if (isOk) {
68     mySpinBox->setValue(aDefVal);
69   }
70
71   QString aTTip = QString::fromStdString(theData->widgetTooltip());
72   mySpinBox->setToolTip(aTTip);
73
74   aControlLay->addWidget(mySpinBox);
75   aControlLay->setStretch(1, 1);
76
77   connect(mySpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
78 }
79
80 ModuleBase_DoubleValueWidget::~ModuleBase_DoubleValueWidget()
81 {
82 }
83
84 bool ModuleBase_DoubleValueWidget::storeValue(FeaturePtr theFeature) const
85 {
86   DataPtr aData = theFeature->data();
87   boost::shared_ptr<ModelAPI_AttributeDouble> aReal = aData->real(myAttributeID);
88   if (aReal->value() != mySpinBox->value()) {
89     aReal->setValue(mySpinBox->value());
90     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
91   }
92   return true;
93 }
94
95 bool ModuleBase_DoubleValueWidget::restoreValue(FeaturePtr theFeature)
96 {
97   DataPtr aData = theFeature->data();
98   boost::shared_ptr<ModelAPI_AttributeDouble> aRef = aData->real(myAttributeID);
99
100   bool isBlocked = mySpinBox->blockSignals(true);
101   mySpinBox->setValue(aRef->value());
102   mySpinBox->blockSignals(isBlocked);
103
104   return true;
105 }
106
107 QList<QWidget*> ModuleBase_DoubleValueWidget::getControls() const
108 {
109   QList<QWidget*> aList;
110   aList.append(myLabel);
111   aList.append(mySpinBox);
112   return aList;
113 }
114
115
116 //////////////////////////////////////////////////////////////////////////////////
117 ModuleBase_BoolValueWidget::ModuleBase_BoolValueWidget(QWidget* theParent, const Config_WidgetAPI* theData)
118   : ModuleBase_ModelWidget(theParent)
119 {
120   myAttributeID = theData->widgetId();
121   QString aText = QString::fromStdString(theData->widgetLabel());
122   QString aToolTip = QString::fromStdString(theData->widgetTooltip());
123   QString aDefault = QString::fromStdString(theData->getProperty("default"));
124
125   myCheckBox = new QCheckBox(aText, theParent);
126   myCheckBox->setToolTip(aToolTip);
127   myCheckBox->setChecked(aDefault == "true");
128
129   connect(myCheckBox, SIGNAL(toggled(bool)), this, SIGNAL(valuesChanged()));
130 }
131
132 ModuleBase_BoolValueWidget::~ModuleBase_BoolValueWidget()
133 {
134 }
135
136 QWidget* ModuleBase_BoolValueWidget::getControl() const 
137
138   return myCheckBox; 
139 }
140
141 bool ModuleBase_BoolValueWidget::storeValue(FeaturePtr theFeature) const
142 {
143   DataPtr aData = theFeature->data();
144   boost::shared_ptr<ModelAPI_AttributeBoolean> aBool = aData->boolean(myAttributeID);
145
146   if (aBool->value() != myCheckBox->isChecked()) {
147     aBool->setValue(myCheckBox->isChecked());
148     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
149   }
150   return true;
151 }
152
153 bool ModuleBase_BoolValueWidget::restoreValue(FeaturePtr theFeature)
154 {
155   DataPtr aData = theFeature->data();
156   boost::shared_ptr<ModelAPI_AttributeBoolean> aRef = aData->boolean(myAttributeID);
157
158   bool isBlocked = myCheckBox->blockSignals(true);
159   myCheckBox->setChecked(aRef->value());
160   myCheckBox->blockSignals(isBlocked);
161
162   return true;
163 }
164
165 QList<QWidget*> ModuleBase_BoolValueWidget::getControls() const
166 {
167   QList<QWidget*> aList;
168   aList.append(myCheckBox);
169   return aList;
170 }