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