]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetFactory.cpp
Salome HOME
Implementation of "Import" BREP and STEP functionality
[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_WidgetFeatureOrAttribute.h>
14 #include <ModuleBase_WidgetFeature.h>
15 #include <ModuleBase_WidgetEditor.h>
16 #include <ModuleBase_WidgetSwitch.h>
17 #include <ModuleBase_WidgetSelector.h>
18 #include <ModuleBase_WidgetDoubleValue.h>
19 #include <ModuleBase_WidgetBoolValue.h>
20 #include <ModuleBase_WidgetPoint2dDistance.h>
21 #include <ModuleBase_WidgetFileSelector.h>
22 #include <ModuleBase_IWorkshop.h>
23 #include <ModuleBase_IModule.h>
24
25 #include <Config_Keywords.h>
26 #include <Config_WidgetAPI.h>
27
28 #include <QWidget>
29 #include <QHBoxLayout>
30 #include <QGridLayout>
31 #include <QSpinBox>
32 #include <QMetaProperty>
33 #include <QLabel>
34 #include <QPixmap>
35 #include <QGroupBox>
36 #include <QToolBox>
37
38 #ifdef _DEBUG
39 #include <QDebug>
40 #endif
41
42 #include <cfloat>
43 #include <climits>
44
45 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,
46                                                    ModuleBase_IWorkshop* theWorkshop)
47     : myWorkshop(theWorkshop)
48 {
49   myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
50 }
51
52 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
53 {
54 }
55
56 void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
57 {
58   myParentId = myWidgetApi->widgetId();
59   if (!myWidgetApi->toChildWidget())
60     return;
61
62   QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
63   aWidgetLay->setContentsMargins(2, 2, 2, 2);
64   do {  //Iterate over each node
65     std::string aWdgType = myWidgetApi->widgetType();
66     //Create a widget (doublevalue, groupbox, toolbox, etc.
67     QWidget* aWidget = createWidgetByType(aWdgType, theParent);
68     if (aWidget) {
69       if (!isInternalWidget(aWdgType)) {
70         aWidgetLay->addWidget(aWidget);
71       } else {
72         aWidget->setVisible(false);
73       }
74     }
75     if (myWidgetApi->isContainerWidget()) {
76       //if current widget is groupbox (container) process it's children recursively
77       QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
78       createWidget(aWidget);
79       QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
80       aGrBox->setTitle(aGroupName);
81     }
82     if (myWidgetApi->isPagedWidget()) {
83       //If current widget is toolbox or switch-casebox then fetch all
84       //it's pages recursively and setup into the widget.
85       myWidgetApi->toChildWidget();
86       do {
87         QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
88         QWidget* aPage = new QWidget(aWidget);
89         createWidget(aPage);
90         if (aWdgType == WDG_SWITCH) {
91           ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
92           aSwitch->addPage(aPage, aPageName);
93         } else if (aWdgType == WDG_TOOLBOX) {
94           QToolBox* aToolbox = qobject_cast<QToolBox*>(aWidget);
95           aToolbox->addItem(aPage, aPageName);
96         }
97       } while (myWidgetApi->toNextWidget());
98     }
99   } while (myWidgetApi->toNextWidget());
100   theParent->setLayout(aWidgetLay);
101 }
102
103 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
104 {
105   QWidget* result = new QWidget(theParent);
106   QVBoxLayout* aLabelLay = new QVBoxLayout(result);
107   QLabel* aLabel = new QLabel(result);
108   aLabel->setWordWrap(true);
109   aLabel->setText(qs(myWidgetApi->getProperty(INFO_WDG_TEXT)));
110   aLabel->setToolTip(qs(myWidgetApi->getProperty(INFO_WDG_TOOLTIP)));
111   aLabelLay->addWidget(aLabel);
112   aLabelLay->addStretch(1);
113   result->setLayout(aLabelLay);
114   return result;
115 }
116
117 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
118                                                       QWidget* theParent)
119 {
120   QWidget* result = NULL;
121   if (theType == WDG_DOUBLEVALUE) {
122     result = doubleSpinBoxControl(theParent);
123
124   } else if (theType == WDG_INFO) {
125     result = labelControl(theParent);
126
127   } else if (theType == WDG_SELECTOR) {
128     result = selectorControl(theParent);
129
130   } else if (theType == WDG_BOOLVALUE) {
131     result = booleanControl(theParent);
132
133   } else if (theType == WDG_POINT_SELECTOR) {
134     result = pointSelectorControl(theParent);
135
136   } else if (theType == WDG_FEATURE_SELECTOR) {
137     result = featureSelectorControl(theParent);
138
139   } else if (theType == WDG_FEATURE_OR_ATTRIBUTE_SELECTOR) {
140     result = featureOrAttributeSelectorControl(theParent);
141
142   } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
143     result = doubleValueEditor(theParent);
144
145   } else if (theType == WDG_POINT2D_DISTANCE) {
146     result = point2dDistanceControl(theParent);
147   } else if (theType == WDG_FILE_SELECTOR) {
148     result = fileSelectorControl(theParent);
149   } else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
150     result = createContainer(theType, theParent);
151   } else {
152     result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi,
153                                                       myModelWidgets);
154 #ifdef _DEBUG
155     if (!result) {qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type");}
156 #endif
157   }
158   return result;
159 }
160
161 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
162 {
163   QWidget* result = NULL;
164   if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
165     QGroupBox* aGroupBox = new QGroupBox(theParent);
166     aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
167     result = aGroupBox;
168   } else if (theType == WDG_TOOLBOX) {
169     result = new QToolBox(theParent);
170   } else if (theType == WDG_SWITCH) {
171     result = new ModuleBase_WidgetSwitch(theParent);
172   } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
173     result = NULL;
174   }
175 #ifdef _DEBUG
176   else {qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type";}
177 #endif
178   return result;
179 }
180
181 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
182 {
183   ModuleBase_WidgetDoubleValue* aDblWgt = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi,
184                                                                            myParentId);
185   myModelWidgets.append(aDblWgt);
186
187   return aDblWgt->getControl();
188 }
189
190 QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
191 {
192   ModuleBase_WidgetPoint2D* aWidget = new ModuleBase_WidgetPoint2D(theParent, myWidgetApi,
193                                                                    myParentId);
194   myModelWidgets.append(aWidget);
195   return aWidget->getControl();
196 }
197
198 QWidget* ModuleBase_WidgetFactory::featureSelectorControl(QWidget* theParent)
199 {
200   ModuleBase_WidgetFeature* aWidget = new ModuleBase_WidgetFeature(theParent, myWidgetApi,
201                                                                    myParentId);
202   myModelWidgets.append(aWidget);
203   return aWidget->getControl();
204 }
205
206 QWidget* ModuleBase_WidgetFactory::featureOrAttributeSelectorControl(QWidget* theParent)
207 {
208   ModuleBase_WidgetFeatureOrAttribute* aWidget = new ModuleBase_WidgetFeatureOrAttribute(
209       theParent, myWidgetApi, myParentId);
210   myModelWidgets.append(aWidget);
211   return aWidget->getControl();
212 }
213
214 QWidget* ModuleBase_WidgetFactory::doubleValueEditor(QWidget* theParent)
215 {
216   ModuleBase_WidgetEditor* aWidget = new ModuleBase_WidgetEditor(theParent, myWidgetApi,
217                                                                  myParentId);
218   myModelWidgets.append(aWidget);
219   return aWidget->getControl();
220 }
221
222 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
223 {
224   return QString::fromStdString(theStdString);
225 }
226
227 bool ModuleBase_WidgetFactory::isInternalWidget(const std::string& theType)
228 {
229   std::string prop = myWidgetApi->getProperty(FEATURE_INTERNAL);
230
231   std::transform(prop.begin(), prop.end(), prop.begin(), ::tolower);
232   if (prop.empty() || prop == "false" || prop == "0") {
233     return false;
234   }
235   return true;
236 }
237
238 QWidget* ModuleBase_WidgetFactory::selectorControl(QWidget* theParent)
239 {
240   ModuleBase_WidgetSelector* aSelector = new ModuleBase_WidgetSelector(theParent, myWorkshop,
241                                                                        myWidgetApi, myParentId);
242   myModelWidgets.append(aSelector);
243   return aSelector->getControl();
244 }
245
246 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
247 {
248   ModuleBase_WidgetBoolValue* aBoolWgt = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi,
249                                                                         myParentId);
250   myModelWidgets.append(aBoolWgt);
251
252   return aBoolWgt->getControl();
253 }
254
255 QWidget* ModuleBase_WidgetFactory::point2dDistanceControl(QWidget* theParent)
256 {
257   ModuleBase_WidgetPoint2dDistance* aDistWgt = new ModuleBase_WidgetPoint2dDistance(theParent,
258                                                                                     myWidgetApi,
259                                                                                     myParentId);
260   myModelWidgets.append(aDistWgt);
261
262   return aDistWgt->getControl();
263 }
264
265 QWidget* ModuleBase_WidgetFactory::fileSelectorControl(QWidget* theParent)
266 {
267   ModuleBase_WidgetFileSelector* aFileSelectorWgt =
268       new ModuleBase_WidgetFileSelector(theParent, myWidgetApi, myParentId);
269   myModelWidgets.append(aFileSelectorWgt);
270
271   return aFileSelectorWgt->getControl();
272 }