Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetFactory.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * ModuleBase_WidgetFactory.cpp
5  *
6  *  Created on: Apr 3, 2014
7  *      Author: sbh
8  */
9
10 #include <ModuleBase_WidgetFactory.h>
11
12 #include <ModuleBase_Operation.h>
13 #include <ModuleBase_OperationDescription.h>
14 //#include <ModuleBase_WidgetFeatureOrAttribute.h>
15 //#include <ModuleBase_WidgetFeature.h>
16 #include <ModuleBase_WidgetEditor.h>
17 #include <ModuleBase_WidgetSwitch.h>
18 #include <ModuleBase_WidgetShapeSelector.h>
19 #include <ModuleBase_WidgetDoubleValue.h>
20 #include <ModuleBase_WidgetBoolValue.h>
21 //#include <ModuleBase_WidgetPoint2dDistance.h>
22 #include <ModuleBase_WidgetFileSelector.h>
23 #include <ModuleBase_WidgetChoice.h>
24 #include <ModuleBase_IWorkshop.h>
25 #include <ModuleBase_IModule.h>
26 #include <ModuleBase_Tools.h>
27 #include <ModuleBase_WidgetLineEdit.h>
28 #include <ModuleBase_WidgetMultiSelector.h>
29 #include <ModuleBase_WidgetLabel.h>
30 #include <ModuleBase_WidgetToolbox.h>
31 #include <ModuleBase_PageBase.h>
32 #include <ModuleBase_PageGroupBox.h>
33 #include <ModuleBase_PageWidget.h>
34
35 #include <ModelAPI_Validator.h>
36 #include <ModelAPI_Session.h>
37
38 #include <Config_Keywords.h>
39 #include <Config_WidgetAPI.h>
40
41 #include <QWidget>
42 #include <QHBoxLayout>
43 #include <QGridLayout>
44 #include <QSpinBox>
45 #include <QMetaProperty>
46 #include <QLabel>
47 #include <QPixmap>
48 #include <QGroupBox>
49 #include <QToolBox>
50
51 #ifdef _DEBUG
52 #include <QDebug>
53 #endif
54
55 #include <cfloat>
56 #include <climits>
57
58 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,
59                                                    ModuleBase_IWorkshop* theWorkshop)
60     : myWorkshop(theWorkshop)
61 {
62   myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
63 }
64
65 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
66 {
67   delete myWidgetApi;
68 }
69
70 void ModuleBase_WidgetFactory::createWidget(ModuleBase_PageBase* thePage)
71 {
72   myParentId = myWidgetApi->widgetId();
73   if (!myWidgetApi->toChildWidget())
74     return;
75
76   do {  //Iterate over each node
77     std::string aWdgType = myWidgetApi->widgetType();
78     // Create PageGroup TODO: extract
79     if (myWidgetApi->isGroupBoxWidget()) {
80       //if current widget is groupbox (container) process it's children recursively
81       QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
82       ModuleBase_PageGroupBox* aPage = new ModuleBase_PageGroupBox(thePage->pageWidget());
83       aPage->setTitle(aGroupName);
84       createWidget(aPage);
85       thePage->addPageWidget(aPage);
86     } else {
87       // Create a ModelWidget
88       ModuleBase_ModelWidget* aWidget = createWidgetByType(aWdgType, thePage->pageWidget());
89       if (aWidget) {
90         if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {
91           thePage->addModelWidget(aWidget);
92         } else {
93           aWidget->setVisible(false);
94         }
95       }
96       // Create PagedContainer TODO: extract
97       if (myWidgetApi->isPagedWidget()) {
98         //If current widget is toolbox or switch-casebox then fetch all
99         //it's pages recursively and setup into the widget.
100         myWidgetApi->toChildWidget();
101         do {
102           QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
103           QString aCaseId = qs(myWidgetApi->getProperty(_ID));
104           ModuleBase_PageBase* aPage = new ModuleBase_PageWidget(aWidget);
105           createWidget(aPage);
106           QWidget* aCasePageWidget = dynamic_cast<QWidget*>(aPage);
107           if (aWdgType == WDG_SWITCH) {
108             ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
109             aSwitch->addPage(aCasePageWidget, aPageName);
110           } else if (aWdgType == WDG_TOOLBOX) {
111             ModuleBase_WidgetToolbox* aToolbox = qobject_cast<ModuleBase_WidgetToolbox*>(aWidget);
112             aToolbox->addPage(aPage, aPageName, aCaseId);
113           }
114         } while (myWidgetApi->toNextWidget());
115       }
116     }
117   } while (myWidgetApi->toNextWidget());
118
119   thePage->alignToTop();
120 }
121
122 ModuleBase_ModelWidget* ModuleBase_WidgetFactory
123 ::createWidgetByType(const std::string& theType, QWidget* theParent)
124 {
125   ModuleBase_ModelWidget* result = NULL;
126
127   if (theType == WDG_INFO) {
128     result = new ModuleBase_WidgetLabel(theParent, myWidgetApi, myParentId);
129
130   } else if (theType == WDG_DOUBLEVALUE) {
131     result = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi, myParentId);
132
133   } else if (theType == WDG_SHAPE_SELECTOR) {
134     result =  new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi, myParentId);
135
136   } else if (theType == WDG_BOOLVALUE) {
137     result = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi, myParentId);
138
139   } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
140     result = new ModuleBase_WidgetEditor(theParent, myWidgetApi, myParentId);
141
142   } else if (theType == WDG_FILE_SELECTOR) {
143     result = new ModuleBase_WidgetFileSelector(theParent, myWidgetApi, myParentId);
144
145   } else if (theType == WDG_CHOICE) {
146     result = new ModuleBase_WidgetChoice(theParent, myWidgetApi,myParentId);
147
148   } else if (theType == WDG_STRINGVALUE) {
149     result = new ModuleBase_WidgetLineEdit(theParent, myWidgetApi,myParentId);
150
151   } else if (theType == WDG_MULTISELECTOR) {
152     result = new ModuleBase_WidgetMultiSelector(theParent, myWorkshop, myWidgetApi,myParentId);
153
154   } else if (theType == WDG_TOOLBOX) {
155     result = new ModuleBase_WidgetToolbox(theParent, myWidgetApi, myParentId);
156
157   } else if (theType == WDG_SWITCH) {
158     result = new ModuleBase_WidgetSwitch(theParent, myWidgetApi, myParentId);
159     return result;
160
161   } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
162     // Do nothing for "box" and "case"
163     result = NULL;
164   } else {
165     result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi,
166                                                       myParentId);
167 #ifdef _DEBUG
168     if (!result) {qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type");}
169 #endif
170   }
171   if (result) {
172     myModelWidgets.append(result);
173   }
174   return result;
175 }
176
177 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString)
178 {
179   return QString::fromStdString(theStdString);
180 }
181