]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetFactory.cpp
Salome HOME
89b82bef47bef6dc332a5c99967d79680a301d7e
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetFactory.cpp
1 /*
2  * ModuleBase_WidgetFactory.cpp
3  *
4  *  Created on: Apr 3, 2014
5  *      Author: sbh
6  */
7
8 #include <ModuleBase_WidgetFactory.h>
9
10 #include <ModuleBase_Operation.h>
11 #include <ModuleBase_OperationDescription.h>
12 #include <ModuleBase_WidgetPoint2D.h>
13 #include <ModuleBase_WidgetSwitch.h>
14 #include <ModuleBase_WidgetSelector.h>
15 #include <ModuleBase_WidgetDoubleValue.h>
16 #include <ModuleBase_WidgetBoolValue.h>
17 #include <ModuleBase_WidgetPoint2dDistance.h>
18
19 #include <Config_Keywords.h>
20 #include <Config_WidgetAPI.h>
21
22 #include <QWidget>
23 #include <QHBoxLayout>
24 #include <QGridLayout>
25 #include <QSpinBox>
26 #include <QMetaProperty>
27 #include <QLabel>
28 #include <QPixmap>
29 #include <QGroupBox>
30 #include <QToolBox>
31
32 #ifdef _DEBUG
33 #include <QDebug>
34 #endif
35
36 #include <cfloat>
37 #include <climits>
38
39 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,
40                                                    ModuleBase_IWorkshop* theWorkshop)
41  : myWorkshop(theWorkshop)
42 {
43   myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
44 }
45
46 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
47 {
48 }
49
50 void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
51 {
52   if (!myWidgetApi->toChildWidget())
53     return;
54
55   QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
56   aWidgetLay->setContentsMargins(2, 2, 2, 2);
57   do { //Iterate over each node
58     std::string aWdgType = myWidgetApi->widgetType();
59     //Create a widget (doublevalue, groupbox, toolbox, etc.
60     QWidget* aWidget = createWidgetByType(aWdgType, theParent);
61     if (aWidget) {
62       aWidgetLay->addWidget(aWidget);
63     }
64     if (myWidgetApi->isContainerWidget()) {
65       //if current widget is groupbox (container) process it's children recursively
66       QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
67       createWidget(aWidget);
68       QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
69       aGrBox->setTitle(aGroupName);
70     }
71     if (myWidgetApi->isPagedWidget()) {
72       //If current widget is toolbox or switch-casebox then fetch all
73       //it's pages recursively and setup into the widget.
74       myWidgetApi->toChildWidget();
75       do {
76         QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
77         QWidget* aPage = new QWidget(aWidget);
78         createWidget(aPage);
79         if (aWdgType == WDG_SWITCH) {
80           ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
81           aSwitch->addPage(aPage, aPageName);
82         } else if (aWdgType == WDG_TOOLBOX){
83           QToolBox* aToolbox = qobject_cast<QToolBox*>(aWidget);
84           aToolbox->addItem(aPage, aPageName);
85         }
86       } while(myWidgetApi->toNextWidget());
87     }
88   } while(myWidgetApi->toNextWidget());
89   theParent->setLayout(aWidgetLay);
90 }
91
92 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
93 {
94   QWidget* result = new QWidget(theParent);
95   QVBoxLayout* aLabelLay = new QVBoxLayout(result);
96   QLabel* aLabel = new QLabel(result);
97   aLabel->setText(qs(myWidgetApi->getProperty(INFO_WDG_TEXT)));
98   aLabel->setToolTip(qs(myWidgetApi->getProperty(INFO_WDG_TOOLTIP)));
99   aLabelLay->addWidget(aLabel);
100   aLabelLay->addStretch(1);
101   result->setLayout(aLabelLay);
102   return result;
103 }
104
105 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType, QWidget* theParent)
106 {
107   QWidget* result = NULL;
108   if (theType == WDG_DOUBLEVALUE) {
109     result = doubleSpinBoxControl(theParent);
110
111   } else if (theType == WDG_INFO) {
112     result = labelControl(theParent);
113
114   } else if (theType == WDG_SELECTOR) {
115     result = selectorControl(theParent);
116
117   } else if (theType == WDG_BOOLVALUE) {
118     result = booleanControl(theParent);
119
120   } else if (theType == WDG_POINT_SELECTOR) {
121     result = pointSelectorControl(theParent);
122
123   } else if (theType == WDG_POINT2D_DISTANCE) {
124     result = point2dDistanceControl(theParent);
125
126   } else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
127     result = createContainer(theType, theParent);
128   }
129 #ifdef _DEBUG
130   else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad widget type"; }
131 #endif
132   return result;
133 }
134
135 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
136 {
137   QWidget* result = NULL;
138   if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
139     QGroupBox* aGroupBox = new QGroupBox(theParent);
140     aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
141     result = aGroupBox;
142   } else if (theType == WDG_TOOLBOX) {
143     result = new QToolBox(theParent);
144   } else if (theType == WDG_SWITCH) {
145     result = new ModuleBase_WidgetSwitch(theParent);
146   } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
147     result = NULL;
148   }
149 #ifdef _DEBUG
150   else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type"; }
151 #endif
152   return result;
153 }
154
155 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
156 {
157   ModuleBase_WidgetDoubleValue* aDblWgt = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi);
158   myModelWidgets.append(aDblWgt);
159
160   return aDblWgt->getControl();
161 }
162
163 QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
164 {
165   ModuleBase_WidgetPoint2D* aWidget = new ModuleBase_WidgetPoint2D(theParent, myWidgetApi);
166   myModelWidgets.append(aWidget);
167   return aWidget->getControl();
168 }
169
170 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
171 {
172   return QString::fromStdString(theStdString);
173 }
174
175
176 QWidget* ModuleBase_WidgetFactory::selectorControl(QWidget* theParent)
177 {
178   ModuleBase_WidgetSelector* aSelector = new ModuleBase_WidgetSelector(theParent, myWorkshop, myWidgetApi);
179   myModelWidgets.append(aSelector);
180   return aSelector->getControl();
181 }
182
183
184 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
185 {
186   ModuleBase_WidgetBoolValue* aBoolWgt = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi);
187   myModelWidgets.append(aBoolWgt);
188
189   return aBoolWgt->getControl();
190 }
191
192
193 QWidget* ModuleBase_WidgetFactory::point2dDistanceControl(QWidget* theParent)
194 {
195   ModuleBase_WidgetPoint2dDistance* aDistWgt = new ModuleBase_WidgetPoint2dDistance(theParent, myWidgetApi);
196   myModelWidgets.append(aDistWgt);
197
198   return aDistWgt->getControl();
199 }