1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
\r
4 * ModuleBase_WidgetFactory.cpp
\r
6 * Created on: Apr 3, 2014
\r
10 #include <ModuleBase_WidgetFactory.h>
\r
11 #include <ModuleBase_IconFactory.h>
\r
13 #include <ModuleBase_Operation.h>
\r
14 #include <ModuleBase_OperationDescription.h>
\r
15 #include <ModuleBase_WidgetEditor.h>
\r
16 #include <ModuleBase_WidgetSwitch.h>
\r
17 #include <ModuleBase_WidgetShapeSelector.h>
\r
18 #include <ModuleBase_WidgetFeatureSelector.h>
\r
19 #include <ModuleBase_WidgetDoubleValue.h>
\r
20 #include <ModuleBase_WidgetIntValue.h>
\r
21 #include <ModuleBase_WidgetBoolValue.h>
\r
22 #include <ModuleBase_WidgetFileSelector.h>
\r
23 #include <ModuleBase_WidgetChoice.h>
\r
24 #include <ModuleBase_IWorkshop.h>
\r
25 #include <ModuleBase_IModule.h>
\r
26 #include <ModuleBase_Tools.h>
\r
27 #include <ModuleBase_WidgetLineEdit.h>
\r
28 #include <ModuleBase_WidgetMultiSelector.h>
\r
29 #include <ModuleBase_WidgetConcealedObjects.h>
\r
30 #include <ModuleBase_WidgetLabel.h>
\r
31 #include <ModuleBase_WidgetToolbox.h>
\r
32 #include <ModuleBase_PageBase.h>
\r
33 #include <ModuleBase_PageGroupBox.h>
\r
34 #include <ModuleBase_WidgetOptionalBox.h>
\r
35 #include <ModuleBase_PageWidget.h>
\r
36 #include <ModuleBase_WidgetExprEditor.h>
\r
37 #include <ModuleBase_WidgetCreatorFactory.h>
\r
38 #include <ModuleBase_WidgetAction.h>
\r
40 #include <ModelAPI_Validator.h>
\r
41 #include <ModelAPI_Session.h>
\r
43 #include <Config_Keywords.h>
\r
44 #include <Config_WidgetAPI.h>
\r
47 #include <QHBoxLayout>
\r
48 #include <QGridLayout>
\r
50 #include <QMetaProperty>
\r
53 #include <QGroupBox>
\r
63 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,
\r
64 ModuleBase_IWorkshop* theWorkshop)
\r
65 : myWorkshop(theWorkshop)
\r
67 myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
\r
70 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
\r
75 void ModuleBase_WidgetFactory::createWidget(ModuleBase_PageBase* thePage, bool alignToTop)
\r
77 std::string aWType = myWidgetApi->widgetType();
\r
78 if (aWType == NODE_FEATURE) {
\r
79 // if XML definition of the feature contains the next key, the widgets should not be created,
\r
80 // but a specific panel should be made. However, to provide persistent of the panel values,
\r
81 // we need to get into the panel the feature of the operation. As a result this panel should
\r
82 // be created after the feature creating(create operation). The method setPanel() of this
\r
83 // class is used for this. Here, we just return to avoid the widgets creation.
\r
84 std::string aPanelName = myWidgetApi->getProperty(PROPERTY_PANEL_ID);
\r
85 if (!aPanelName.empty())
\r
89 if (!myWidgetApi->toChildWidget())
\r
92 do { //Iterate over each node
\r
93 std::string aWdgType = myWidgetApi->widgetType();
\r
94 // Create PageGroup TODO: extract
\r
95 if (myWidgetApi->isGroupBoxWidget() ||
\r
96 ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
\r
98 //if current widget is groupbox (container) process it's children recursively
\r
99 ModuleBase_PageBase* aPage = createPageByType(aWdgType, thePage->pageWidget());
\r
101 createWidget(aPage);
\r
102 thePage->addPageWidget(aPage);
\r
104 // Create a ModelWidget
\r
105 ModuleBase_ModelWidget* aWidget = createWidgetByType(aWdgType, thePage->pageWidget());
\r
107 if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {
\r
108 thePage->addModelWidget(aWidget);
\r
110 aWidget->setVisible(false);
\r
113 // Create PagedContainer TODO: extract
\r
114 if (myWidgetApi->isPagedWidget()) {
\r
115 //If current widget is toolbox or switch-casebox then fetch all
\r
116 //it's pages recursively and setup into the widget.
\r
117 if (myWidgetApi->toChildWidget()) {
\r
119 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
\r
120 QString aCaseId = qs(myWidgetApi->getProperty(_ID));
\r
121 ModuleBase_PageBase* aPage = new ModuleBase_PageWidget(aWidget);
\r
122 createWidget(aPage);
\r
123 if (aWdgType == WDG_SWITCH || aWdgType == WDG_TOOLBOX) {
\r
124 ModuleBase_PagedContainer* aContainer = qobject_cast<ModuleBase_PagedContainer*>(aWidget);
\r
126 QString anIconPath = qs( myWidgetApi->getProperty( CONTAINER_PAGE_ICON ) );
\r
127 QPixmap anIcon = ModuleBase_IconFactory::loadPixmap( anIconPath );
\r
128 aContainer->addPage( aPage, aPageName, aCaseId, anIcon );
\r
130 } while (myWidgetApi->toNextWidget());
\r
134 } while (myWidgetApi->toNextWidget());
\r
137 thePage->alignToTop();
\r
140 void ModuleBase_WidgetFactory::createPanel(ModuleBase_PageBase* thePage,
\r
141 const FeaturePtr& theFeature)
\r
143 std::string aPanelName = myWidgetApi->getProperty(PROPERTY_PANEL_ID);
\r
144 if (!aPanelName.empty() && ModuleBase_WidgetCreatorFactory::get()->hasPanelWidget(aPanelName)) {
\r
145 QWidget* aPanel = ModuleBase_WidgetCreatorFactory::get()->createPanelByType(aPanelName,
\r
146 thePage->pageWidget(), theFeature);
\r
147 thePage->addWidget(aPanel);
\r
148 thePage->alignToTop();
\r
152 void ModuleBase_WidgetFactory::createWidget(ModuleBase_PageBase* thePage,
\r
153 const std::string& theWidgetId)
\r
155 bool aFound = false;
\r
156 moveToWidgetId(theWidgetId, aFound);
\r
158 std::string aWdgType = myWidgetApi->widgetType();
\r
160 // Create a ModelWidget
\r
161 ModuleBase_ModelWidget* aWidget = createWidgetByType(aWdgType, thePage->pageWidget());
\r
163 if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {
\r
164 thePage->addModelWidget(aWidget);
\r
167 aWidget->setVisible(false);
\r
171 thePage->alignToTop();
\r
174 void ModuleBase_WidgetFactory::getAttributeTitle(const std::string& theAttributeId,
\r
175 std::string& theTitle)
\r
177 bool aFound = false;
\r
178 moveToWidgetId(theAttributeId, aFound);
\r
180 theTitle = QString::fromStdString(myWidgetApi->widgetLabel()).toStdString().c_str();
\r
181 if (theTitle.empty())
\r
182 theTitle = QString::fromStdString(myWidgetApi->getProperty(CONTAINER_PAGE_NAME)).toStdString().c_str();
\r
186 void ModuleBase_WidgetFactory::getGreedAttribute(std::string& theAttributeId)
\r
188 if (!theAttributeId.empty())
\r
191 if (!myWidgetApi->toChildWidget())
\r
194 do { //Iterate over each node
\r
195 std::string aWdgType = myWidgetApi->widgetType();
\r
196 // Find title under PageGroup
\r
197 if (myWidgetApi->isGroupBoxWidget() ||
\r
198 ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
\r
199 getGreedAttribute(theAttributeId);
\r
203 std::string anAttributeId = myWidgetApi->widgetId();
\r
204 if (myWidgetApi->getBooleanAttribute(ATTR_GREED, false))
\r
205 theAttributeId = anAttributeId;
\r
206 if (theAttributeId.empty() && myWidgetApi->isPagedWidget()) {
\r
207 //If current widget is toolbox or switch-casebox then fetch all
\r
208 //it's pages recursively and setup into the widget.
\r
209 if (myWidgetApi->toChildWidget()) {
\r
211 getGreedAttribute(theAttributeId);
\r
212 } while (theAttributeId.empty() && myWidgetApi->toNextWidget());
\r
216 } while (theAttributeId.empty() && myWidgetApi->toNextWidget());
\r
219 void ModuleBase_WidgetFactory::moveToWidgetId(const std::string& theWidgetId, bool& theFound)
\r
224 if (!myWidgetApi->toChildWidget())
\r
227 do { //Iterate over each node
\r
228 std::string aWdgType = myWidgetApi->widgetType();
\r
229 // Find title under PageGroup
\r
230 if (myWidgetApi->isGroupBoxWidget() ||
\r
231 ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
\r
232 moveToWidgetId(theWidgetId, theFound);
\r
236 std::string anAttributeId = myWidgetApi->widgetId();
\r
237 theFound = anAttributeId == theWidgetId;
\r
238 if (!theFound && myWidgetApi->isPagedWidget()) {
\r
239 //If current widget is toolbox or switch-casebox then fetch all
\r
240 //it's pages recursively and setup into the widget.
\r
241 if (myWidgetApi->toChildWidget()) {
\r
243 moveToWidgetId(theWidgetId, theFound);
\r
244 } while (!theFound && myWidgetApi->toNextWidget());
\r
248 } while (!theFound && myWidgetApi->toNextWidget());
\r
251 ModuleBase_PageBase* ModuleBase_WidgetFactory::createPageByType(const std::string& theType,
\r
252 QWidget* theParent)
\r
254 ModuleBase_PageBase* aResult = NULL;
\r
256 if (theType == WDG_GROUP) {
\r
257 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
\r
258 ModuleBase_PageGroupBox* aPage = new ModuleBase_PageGroupBox(theParent);
\r
259 aPage->setTitle(aGroupName);
\r
262 else if (theType == WDG_OPTIONALBOX) {
\r
263 ModuleBase_WidgetOptionalBox* aPage = new ModuleBase_WidgetOptionalBox(theParent,
\r
268 aResult = ModuleBase_WidgetCreatorFactory::get()->createPageByType(theType, theParent,
\r
271 ModuleBase_ModelWidget* aWidget = dynamic_cast<ModuleBase_ModelWidget*>(aResult);
\r
273 myModelWidgets.append(aWidget);
\r
278 ModuleBase_ModelWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
\r
279 QWidget* theParent)
\r
281 ModuleBase_ModelWidget* result = NULL;
\r
283 if (theType == WDG_INFO) {
\r
284 result = new ModuleBase_WidgetLabel(theParent, myWidgetApi);
\r
285 } else if (theType == WDG_DOUBLEVALUE) {
\r
286 result = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi);
\r
287 } else if (theType == WDG_INTEGERVALUE) {
\r
288 result = new ModuleBase_WidgetIntValue(theParent, myWidgetApi);
\r
289 } else if (theType == WDG_SHAPE_SELECTOR) {
\r
290 result = new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi);
\r
291 } else if (theType == WDG_FEATURE_SELECTOR) {
\r
292 result = new ModuleBase_WidgetFeatureSelector(theParent, myWorkshop, myWidgetApi);
\r
293 } else if (theType == WDG_BOOLVALUE) {
\r
294 result = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi);
\r
295 //} else if (theType == WDG_DOUBLEVALUE_EDITOR) {
\r
296 // result = new ModuleBase_WidgetEditor(theParent, myWidgetApi);
\r
297 } else if (theType == WDG_FILE_SELECTOR) {
\r
298 result = new ModuleBase_WidgetFileSelector(theParent, myWidgetApi);
\r
299 } else if (theType == WDG_CHOICE) {
\r
300 result = new ModuleBase_WidgetChoice(theParent, myWidgetApi);
\r
301 } else if (theType == WDG_STRINGVALUE) {
\r
302 std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
\r
303 result = new ModuleBase_WidgetLineEdit( theParent, myWidgetApi, aPlaceHolder );
\r
304 } else if (theType == WDG_EXPR_EDITOR) {
\r
305 std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
\r
306 result = new ModuleBase_WidgetExprEditor( theParent, myWidgetApi, aPlaceHolder );
\r
307 } else if (theType == WDG_MULTISELECTOR) {
\r
308 result = new ModuleBase_WidgetMultiSelector(theParent, myWorkshop, myWidgetApi);
\r
309 } else if (theType == WDG_CONCEALED_OBJECTS_VIEW) {
\r
310 result = new ModuleBase_WidgetConcealedObjects(theParent, myWidgetApi);
\r
311 } else if (theType == WDG_TOOLBOX) {
\r
312 result = new ModuleBase_WidgetToolbox(theParent, myWidgetApi);
\r
313 } else if (theType == WDG_SWITCH) {
\r
314 result = new ModuleBase_WidgetSwitch(theParent, myWidgetApi);
\r
315 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE ||
\r
316 theType == NODE_VALIDATOR) {
\r
317 // Do nothing for "box" and "case"
\r
319 } else if (theType == WDG_ACTION) {
\r
320 result = new ModuleBase_WidgetAction(theParent, myWidgetApi);
\r
322 result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi);
\r
324 result = ModuleBase_WidgetCreatorFactory::get()->createWidgetByType(theType, theParent,
\r
325 myWidgetApi, myWorkshop);
\r
328 qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type %s", theType.c_str());
\r
333 myModelWidgets.append(result);
\r
337 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString)
\r
339 return QString::fromStdString(theStdString);
\r