Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[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 #include <ModuleBase_Operation.h>
10
11 #include <Config_Keywords.h>
12 #include <Config_WidgetAPI.h>
13
14 #include <QWidget>
15 #include <QHBoxLayout>
16 #include <QSpinBox>
17 #include <QMetaProperty>
18 #include <QLabel>
19 #include <QPixmap>
20
21 #ifdef _DEBUG
22 #include <QDebug>
23 #endif
24
25 XGUI_WidgetFactory::XGUI_WidgetFactory(ModuleBase_Operation* theOperation)
26     : myOperation(theOperation)
27 {
28   QString aXml = myOperation->xmlRepresentation();
29   myWidgetApi = new Config_WidgetAPI(aXml.toStdString());
30 }
31
32 XGUI_WidgetFactory::~XGUI_WidgetFactory()
33 {
34 }
35
36 void XGUI_WidgetFactory::fillWidget(QWidget* theParent)
37 {
38   myWidgetApi->reset();
39   if (theParent->layout()) {
40     theParent->layout()->deleteLater();
41   }
42
43   QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
44   aWidgetLay->setContentsMargins(0, 0, 0, 0);
45   do {
46     std::string aWdgType = myWidgetApi->widgetType();
47     QWidget* aWidget = NULL;
48     if (aWdgType == NODE_DOUBLE_WDG) {
49       aWidget = doubleSpinBoxWidget();
50     } else {
51       #ifdef _DEBUG
52       qDebug() << "XGUI_WidgetFactory::fillWidget: find bad widget type";
53       #endif
54     }
55     if (aWidget) {
56       aWidgetLay->addWidget(aWidget);
57     }
58   } while(myWidgetApi->nextWidget());
59   theParent->setLayout(aWidgetLay);
60 }
61
62 QWidget* XGUI_WidgetFactory::doubleSpinBoxWidget()
63 {
64   QWidget* result = new QWidget();
65   QHBoxLayout* aControlLay = new QHBoxLayout(result);
66   aControlLay->setContentsMargins(0, 0, 0, 0);
67   QString aLabelText = qs(myWidgetApi->widgetLabel());
68   QString aLabelIcon = qs(myWidgetApi->widgetIcon());
69   QLabel* aLabel = new QLabel(aLabelText);
70   aLabel->setPixmap(QPixmap(aLabelIcon));
71
72   aControlLay->addWidget(aLabel);
73   QDoubleSpinBox* aBox = new QDoubleSpinBox(result);
74   QString anObjName = QString::fromStdString(myWidgetApi->widgetId());
75   aBox->setObjectName(anObjName);
76   bool isOk = false;
77   std::string aProp = myWidgetApi->getProperty(DOUBLE_WDG_MIN);
78   double aMinVal = qs(aProp).toDouble(&isOk);
79   if (isOk) {
80     aBox->setMinimum(aMinVal);
81   }
82   aProp = myWidgetApi->getProperty(DOUBLE_WDG_MAX);
83   double aMaxVal = qs(aProp).toDouble(&isOk);
84   if (isOk) {
85     aBox->setMaximum(aMaxVal);
86   }
87   aProp = myWidgetApi->getProperty(DOUBLE_WDG_STEP);
88   double aStepVal = qs(aProp).toDouble(&isOk);
89   if (isOk) {
90     aBox->setSingleStep(aStepVal);
91   }
92   aProp = myWidgetApi->getProperty(DOUBLE_WDG_DFLT);
93   double aDefVal = qs(aProp).toDouble(&isOk);
94   if (isOk) {
95     aBox->setValue(aDefVal);
96   }
97   QString aTTip = qs(myWidgetApi->widgetTooltip());
98   aBox->setToolTip(aTTip);
99   aControlLay->addWidget(aBox);
100   aControlLay->setStretch(1, 1);
101   result->setLayout(aControlLay);
102   connectWidget(aBox, NODE_DOUBLE_WDG);
103   return result;
104 }
105
106 bool XGUI_WidgetFactory::connectWidget(QWidget* theWidget, const QString& theType)
107 {
108   bool result = false;
109   if (theType == NODE_DOUBLE_WDG) {
110     result = QObject::connect(theWidget, SIGNAL(valueChanged(double)), 
111                               myOperation, SLOT(storeReal(double)));
112   }
113   return result;
114 }
115
116 QString XGUI_WidgetFactory::qs(const std::string& theStdString) const
117 {
118   return QString::fromStdString(theStdString);
119 }