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