Salome HOME
#1186 misprint “Origin/PointCoordindates”. The correction: using 'title' or 'label...
[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_WidgetCheckGroupBox.h>
33 #include <ModuleBase_PageWidget.h>
34 #include <ModuleBase_WidgetExprEditor.h>
35 #include <ModuleBase_WidgetCreatorFactory.h>
36
37 #include <ModelAPI_Validator.h>
38 #include <ModelAPI_Session.h>
39
40 #include <Config_Keywords.h>
41 #include <Config_WidgetAPI.h>
42
43 #include <QWidget>
44 #include <QHBoxLayout>
45 #include <QGridLayout>
46 #include <QSpinBox>
47 #include <QMetaProperty>
48 #include <QLabel>
49 #include <QPixmap>
50 #include <QGroupBox>
51 #include <QToolBox>
52
53 #ifdef _DEBUG
54 #include <QDebug>
55 #endif
56
57 #include <cfloat>
58 #include <climits>
59
60 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,
61                                                    ModuleBase_IWorkshop* theWorkshop)
62     : myWorkshop(theWorkshop)
63 {
64   myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
65 }
66
67 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
68 {
69   delete myWidgetApi;
70 }
71
72 void ModuleBase_WidgetFactory::createWidget(ModuleBase_PageBase* thePage)
73 {
74   myParentId = myWidgetApi->widgetId();
75   if (!myWidgetApi->toChildWidget())
76     return;
77
78   do {  //Iterate over each node
79     std::string aWdgType = myWidgetApi->widgetType();
80     // Create PageGroup TODO: extract
81     if (myWidgetApi->isGroupBoxWidget() ||
82         ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
83
84       //if current widget is groupbox (container) process it's children recursively
85       ModuleBase_PageBase* aPage = createPageByType(aWdgType, thePage->pageWidget());
86
87       createWidget(aPage);
88       thePage->addPageWidget(aPage);
89     } else {
90       // Create a ModelWidget
91       ModuleBase_ModelWidget* aWidget = createWidgetByType(aWdgType, thePage->pageWidget());
92       if (aWidget) {
93         if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {
94           thePage->addModelWidget(aWidget);
95         } else {
96           aWidget->setVisible(false);
97         }
98       }
99       // Create PagedContainer TODO: extract
100       if (myWidgetApi->isPagedWidget()) {
101         //If current widget is toolbox or switch-casebox then fetch all
102         //it's pages recursively and setup into the widget.
103         myWidgetApi->toChildWidget();
104         do {
105           QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
106           QString aCaseId = qs(myWidgetApi->getProperty(_ID));
107           ModuleBase_PageBase* aPage = new ModuleBase_PageWidget(aWidget);
108           createWidget(aPage);
109           if (aWdgType == WDG_SWITCH || aWdgType == WDG_TOOLBOX) {
110             ModuleBase_PagedContainer* aContainer = qobject_cast<ModuleBase_PagedContainer*>(aWidget);
111
112             QString anIconPath = qs( myWidgetApi->getProperty( CONTAINER_PAGE_ICON ) );
113             QPixmap anIcon( anIconPath );
114             aContainer->addPage( aPage, aPageName, aCaseId, anIcon );
115           }
116         } while (myWidgetApi->toNextWidget());
117       }
118     }
119   } while (myWidgetApi->toNextWidget());
120
121   thePage->alignToTop();
122 }
123
124 void ModuleBase_WidgetFactory::getAttributeTitle(const std::string& theFeatureKind,
125                                                  const std::string& theAttributeId,
126                                                  std::string& theTitle)
127 {
128   if (!theTitle.empty())
129     return;
130
131   myParentId = myWidgetApi->widgetId();
132   if (!myWidgetApi->toChildWidget())
133     return;
134
135   do {  //Iterate over each node
136     std::string aWdgType = myWidgetApi->widgetType();
137     // Find title under PageGroup
138     if (myWidgetApi->isGroupBoxWidget() ||
139         ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
140
141       getAttributeTitle(theFeatureKind, theAttributeId, theTitle);
142     } else {
143       // Find title here
144       std::string anAttributeId = myWidgetApi->widgetId();
145       if (anAttributeId == theAttributeId) {
146         theTitle = QString::fromStdString(myWidgetApi->widgetLabel()).toStdString().c_str();
147         if (theTitle.empty())
148           theTitle = QString::fromStdString(myWidgetApi->getProperty(CONTAINER_PAGE_NAME)).toStdString().c_str();
149
150       }
151       if (myWidgetApi->isPagedWidget()) {
152         //If current widget is toolbox or switch-casebox then fetch all
153         //it's pages recursively and setup into the widget.
154         myWidgetApi->toChildWidget();
155         do {
156           getAttributeTitle(theFeatureKind, theAttributeId, theTitle);
157         } while (myWidgetApi->toNextWidget() && theTitle.empty());
158       }
159     }
160   } while (myWidgetApi->toNextWidget() && theTitle.empty());
161 }
162
163 ModuleBase_PageBase* ModuleBase_WidgetFactory::createPageByType(const std::string& theType,
164                                                                 QWidget* theParent)
165 {
166   ModuleBase_PageBase* aResult = NULL;
167
168   if (theType == WDG_GROUP) {
169     QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
170     ModuleBase_PageGroupBox* aPage = new ModuleBase_PageGroupBox(theParent);
171     aPage->setTitle(aGroupName);
172     aResult = aPage;
173   }
174   else if (theType == WDG_CHECK_GROUP) {
175     QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
176     ModuleBase_WidgetCheckGroupBox* aPage = new ModuleBase_WidgetCheckGroupBox(theParent,
177                                                                 myWidgetApi, myParentId);
178     aPage->setTitle(aGroupName);
179     aResult = aPage;
180   }
181   if (!aResult)
182     aResult = ModuleBase_WidgetCreatorFactory::get()->createPageByType(theType, theParent,
183                                                                        myWidgetApi, myParentId);
184
185   ModuleBase_ModelWidget* aWidget = dynamic_cast<ModuleBase_ModelWidget*>(aResult);
186   if (aWidget)
187     myModelWidgets.append(aWidget);
188
189   return aResult;
190 }
191
192 ModuleBase_ModelWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
193                                                                      QWidget* theParent)
194 {
195   ModuleBase_ModelWidget* result = NULL;
196
197   if (theType == WDG_INFO) {
198     result = new ModuleBase_WidgetLabel(theParent, myWidgetApi, myParentId);
199   } else if (theType == WDG_ERRORINFO) {
200     result = new ModuleBase_WidgetErrorLabel(theParent, myWidgetApi, myParentId);
201   } else if (theType == WDG_DOUBLEVALUE) {
202     result = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi, myParentId);
203   } else if (theType == WDG_INTEGERVALUE) {
204     result = new ModuleBase_WidgetIntValue(theParent, myWidgetApi, myParentId);
205   } else if (theType == WDG_SHAPE_SELECTOR) {
206     result = new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi, myParentId);
207   } else if (theType == WDG_BOOLVALUE) {
208     result = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi, myParentId);
209   //} else if (theType == WDG_DOUBLEVALUE_EDITOR) {
210   //  result = new ModuleBase_WidgetEditor(theParent, myWidgetApi, myParentId);
211   } else if (theType == WDG_FILE_SELECTOR) {
212     result = new ModuleBase_WidgetFileSelector(theParent, myWidgetApi, myParentId);
213   } else if (theType == WDG_CHOICE) {
214     result = new ModuleBase_WidgetChoice(theParent, myWidgetApi, myParentId);
215   } else if (theType == WDG_STRINGVALUE) {
216     std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
217     result = new ModuleBase_WidgetLineEdit( theParent, myWidgetApi, myParentId, aPlaceHolder );
218   } else if (theType == WDG_EXPR_EDITOR) {
219     std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
220     result = new ModuleBase_WidgetExprEditor( theParent, myWidgetApi, myParentId, aPlaceHolder );
221   } else if (theType == WDG_MULTISELECTOR) {
222     result = new ModuleBase_WidgetMultiSelector(theParent, myWorkshop, myWidgetApi, myParentId);
223   } else if (theType == WDG_TOOLBOX) {
224     result = new ModuleBase_WidgetToolbox(theParent, myWidgetApi, myParentId);
225   } else if (theType == WDG_SWITCH) {
226     result = new ModuleBase_WidgetSwitch(theParent, myWidgetApi, myParentId);
227   } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE ||
228              theType == NODE_VALIDATOR) {
229     // Do nothing for "box" and "case"
230     result = NULL;
231   } else {
232     result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi, myParentId);
233     if (!result)
234       result = ModuleBase_WidgetCreatorFactory::get()->createWidgetByType(theType, theParent,
235                                                               myWidgetApi, myParentId, myWorkshop);
236     #ifdef _DEBUG
237     if (!result) {
238       qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type %s", theType.c_str());
239     }
240     #endif
241   }
242   if (result)
243     myModelWidgets.append(result);
244   return result;
245 }
246
247 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString)
248 {
249   return QString::fromStdString(theStdString);
250 }
251