Salome HOME
9cc69af893618c2f24851b02daa82603f147543e
[modules/shaper.git] / src / XGUI / XGUI_WidgetFactory.cpp
1 /*
2  * XGUI_WidgetFactory.cpp
3  *
4  *  Created on: Apr 3, 2014
5  *      Author: sbh
6  */
7
8 #include <XGUI_WidgetFactory.h>
9
10 #include <Config_WidgetAPI.h>
11
12 #include <QWidget>
13 #include <QHBoxLayout>
14 #include <QSpinBox>
15 #include <QMetaProperty>
16 #include <QLabel>
17
18 #ifdef _DEBUG
19 #include <QDebug>
20 #endif
21
22 XGUI_WidgetFactory::XGUI_WidgetFactory(const std::string& theXml)
23 {
24   myWidgetApi = new Config_WidgetAPI(theXml);
25 }
26
27 XGUI_WidgetFactory::~XGUI_WidgetFactory()
28 {
29 }
30
31 void XGUI_WidgetFactory::fillWidget(QWidget* theParent)
32 {
33   myWidgetApi->reset();
34   if (theParent->layout()) {
35     theParent->layout()->deleteLater();
36   }
37
38   QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
39   do {
40     std::string aWdgType = myWidgetApi->widgetType();
41     QWidget* aWidget = NULL;
42     if (aWdgType == "value") {
43       aWidget = valueWidget();
44     } else {
45       #ifdef _DEBUG
46       qDebug() << "XGUI_WidgetFactory::fillWidget: find bad widget type";
47       #endif
48     }
49     if (aWidget) {
50       aWidgetLay->addWidget(aWidget);
51     }
52   } while(myWidgetApi->nextWidget());
53   aWidgetLay->addStretch(1);
54   theParent->setLayout(aWidgetLay);
55 }
56
57 QWidget* XGUI_WidgetFactory::valueWidget()
58 {
59   QWidget* result = new QWidget();
60   QHBoxLayout* aControlLay = new QHBoxLayout(result);
61   QString aLabelVal = qs(myWidgetApi->getProperty("label"));
62   QLabel* aLabel = new QLabel(aLabelVal);
63   aControlLay->addWidget(aLabel);
64   QDoubleSpinBox* aBox = new QDoubleSpinBox(result);
65   bool isOk = false;
66   double aMinVal = qs(myWidgetApi->getProperty("min")).toDouble(&isOk);
67   if (isOk) {
68     aBox->setMinimum(aMinVal);
69   }
70   double aMaxVal = qs(myWidgetApi->getProperty("max")).toDouble(&isOk);
71   if (isOk) {
72     aBox->setMaximum(aMaxVal);
73   }
74   double aStepVal = qs(myWidgetApi->getProperty("step")).toDouble(&isOk);
75   if (isOk) {
76     aBox->setSingleStep(aStepVal);
77   }
78   double aDefVal = qs(myWidgetApi->getProperty("default")).toDouble(&isOk);
79   if (isOk) {
80     aBox->setValue(aDefVal);
81   }
82   aControlLay->addWidget(aBox);
83   aControlLay->addStretch();
84
85   result->setLayout(aControlLay);
86   return result;
87 }
88
89 QString XGUI_WidgetFactory::qs(const std::string& theStdString) const
90 {
91   return QString::fromStdString(theStdString);
92 }