Salome HOME
Merge branch 'master' of newgeom:newgeom
[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 #include <QPixmap>
18
19 #ifdef _DEBUG
20 #include <QDebug>
21 #endif
22
23 XGUI_WidgetFactory::XGUI_WidgetFactory(const std::string& theXml)
24 {
25   myWidgetApi = new Config_WidgetAPI(theXml);
26 }
27
28 XGUI_WidgetFactory::~XGUI_WidgetFactory()
29 {
30 }
31
32 void XGUI_WidgetFactory::fillWidget(QWidget* theParent)
33 {
34   myWidgetApi->reset();
35   if (theParent->layout()) {
36     theParent->layout()->deleteLater();
37   }
38
39   QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
40   aWidgetLay->setContentsMargins(0, 0, 0, 0);
41   do {
42     std::string aWdgType = myWidgetApi->widgetType();
43     QWidget* aWidget = NULL;
44     if (aWdgType == "value") {
45       aWidget = valueWidget();
46     } else {
47       #ifdef _DEBUG
48       qDebug() << "XGUI_WidgetFactory::fillWidget: find bad widget type";
49       #endif
50     }
51     if (aWidget) {
52       aWidgetLay->addWidget(aWidget);
53     }
54   } while(myWidgetApi->nextWidget());
55   theParent->setLayout(aWidgetLay);
56 }
57
58 QWidget* XGUI_WidgetFactory::valueWidget()
59 {
60   QWidget* result = new QWidget();
61   QHBoxLayout* aControlLay = new QHBoxLayout(result);
62   aControlLay->setContentsMargins(0, 0, 0, 0);
63   QString aLabelText = qs(myWidgetApi->widgetLabel());
64   QString aLabelIcon = qs(myWidgetApi->widgetIcon());
65   QLabel* aLabel = new QLabel(aLabelText);
66   aLabel->setPixmap(QPixmap(aLabelIcon));
67
68   aControlLay->addWidget(aLabel);
69   QDoubleSpinBox* aBox = new QDoubleSpinBox(result);
70   bool isOk = false;
71   double aMinVal = qs(myWidgetApi->getProperty("min")).toDouble(&isOk);
72   if (isOk) {
73     aBox->setMinimum(aMinVal);
74   }
75   double aMaxVal = qs(myWidgetApi->getProperty("max")).toDouble(&isOk);
76   if (isOk) {
77     aBox->setMaximum(aMaxVal);
78   }
79   double aStepVal = qs(myWidgetApi->getProperty("step")).toDouble(&isOk);
80   if (isOk) {
81     aBox->setSingleStep(aStepVal);
82   }
83   double aDefVal = qs(myWidgetApi->getProperty("default")).toDouble(&isOk);
84   if (isOk) {
85     aBox->setValue(aDefVal);
86   }
87   QString aTTip = qs(myWidgetApi->widgetTooltip());
88   aBox->setToolTip(aTTip);
89   aControlLay->addWidget(aBox);
90   aControlLay->setStretch(1, 1);
91
92   result->setLayout(aControlLay);
93   return result;
94 }
95
96 QString XGUI_WidgetFactory::qs(const std::string& theStdString) const
97 {
98   return QString::fromStdString(theStdString);
99 }