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