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