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