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