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>
12 #include <ModuleBase_Operation.h>
13 #include <ModuleBase_OperationDescription.h>
14 //#include <ModuleBase_WidgetFeatureOrAttribute.h>
15 //#include <ModuleBase_WidgetFeature.h>
16 #include <ModuleBase_WidgetEditor.h>
17 #include <ModuleBase_WidgetSwitch.h>
18 #include <ModuleBase_WidgetShapeSelector.h>
19 #include <ModuleBase_WidgetDoubleValue.h>
20 #include <ModuleBase_WidgetBoolValue.h>
21 //#include <ModuleBase_WidgetPoint2dDistance.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_WidgetLabel.h>
31 #include <ModelAPI_Validator.h>
32 #include <ModelAPI_Session.h>
34 #include <Config_Keywords.h>
35 #include <Config_WidgetAPI.h>
38 #include <QHBoxLayout>
39 #include <QGridLayout>
41 #include <QMetaProperty>
54 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,
55 ModuleBase_IWorkshop* theWorkshop)
56 : myWorkshop(theWorkshop)
58 myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
61 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
66 void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
68 myParentId = myWidgetApi->widgetId();
69 if (!myWidgetApi->toChildWidget())
72 QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
73 do { //Iterate over each node
74 std::string aWdgType = myWidgetApi->widgetType();
75 //Create a widget (doublevalue, groupbox, toolbox, etc.
76 QWidget* aWidget = createWidgetByType(aWdgType, theParent);
78 if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {
79 aWidgetLay->addWidget(aWidget);
81 aWidget->setVisible(false);
84 if (myWidgetApi->isContainerWidget()) {
85 //if current widget is groupbox (container) process it's children recursively
86 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
87 createWidget(aWidget);
88 ModuleBase_Tools::adjustMargins(aWidget);
89 QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
90 aGrBox->setTitle(aGroupName);
92 if (myWidgetApi->isPagedWidget()) {
93 //If current widget is toolbox or switch-casebox then fetch all
94 //it's pages recursively and setup into the widget.
95 myWidgetApi->toChildWidget();
97 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
98 QWidget* aPage = new QWidget(aWidget);
99 ModuleBase_Tools::adjustMargins(aPage);
101 if (aWdgType == WDG_SWITCH) {
102 ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
103 aSwitch->addPage(aPage, aPageName);
104 } else if (aWdgType == WDG_TOOLBOX) {
105 QToolBox* aToolbox = qobject_cast<QToolBox*>(aWidget);
106 aToolbox->addItem(aPage, aPageName);
108 } while (myWidgetApi->toNextWidget());
110 } while (myWidgetApi->toNextWidget());
111 theParent->setLayout(aWidgetLay);
115 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
117 ModuleBase_WidgetLabel* aWgt =
118 new ModuleBase_WidgetLabel(theParent, myWidgetApi, myParentId);
119 myModelWidgets.append(aWgt);
120 return aWgt->getControl();
124 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
127 QWidget* result = NULL;
128 if (theType == WDG_DOUBLEVALUE) {
129 result = doubleSpinBoxControl(theParent);
131 } else if (theType == WDG_INFO) {
132 result = labelControl(theParent);
134 } else if (theType == WDG_SHAPE_SELECTOR) {
135 result = shapeSelectorControl(theParent);
137 } else if (theType == WDG_BOOLVALUE) {
138 result = booleanControl(theParent);
140 //} else if (theType == WDG_FEATURE_SELECTOR) {
141 // result = featureSelectorControl(theParent);
143 //} else if (theType == WDG_FEATURE_OR_ATTRIBUTE_SELECTOR) {
144 // result = featureOrAttributeSelectorControl(theParent);
146 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
147 result = doubleValueEditor(theParent);
149 } else if (theType == WDG_FILE_SELECTOR) {
150 result = fileSelectorControl(theParent);
152 } else if (theType == WDG_CHOICE) {
153 result = choiceControl(theParent);
155 } else if (theType == WDG_STRINGVALUE) {
156 result = lineEditControl(theParent);
158 } else if (theType == WDG_MULTISELECTOR) {
159 result = multiSelectorControl(theParent);
161 } else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
162 result = createContainer(theType, theParent);
164 result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi,
165 myParentId, myModelWidgets);
167 if (!result) {qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type");}
173 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
175 QWidget* result = NULL;
176 if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
177 QGroupBox* aGroupBox = new QGroupBox(theParent);
178 aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
180 } else if (theType == WDG_TOOLBOX) {
181 result = new QToolBox(theParent);
182 } else if (theType == WDG_SWITCH) {
183 result = new ModuleBase_WidgetSwitch(theParent);
184 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
188 else {qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type";}
193 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
195 ModuleBase_WidgetDoubleValue* aDblWgt =
196 new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi, myParentId);
197 myModelWidgets.append(aDblWgt);
198 return aDblWgt->getControl();
201 QWidget* ModuleBase_WidgetFactory::doubleValueEditor(QWidget* theParent)
203 ModuleBase_WidgetEditor* aWidget =
204 new ModuleBase_WidgetEditor(theParent, myWidgetApi, myParentId);
205 myModelWidgets.append(aWidget);
206 return aWidget->getControl();
209 QWidget* ModuleBase_WidgetFactory::shapeSelectorControl(QWidget* theParent)
211 ModuleBase_WidgetShapeSelector* aSelector =
212 new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi, myParentId);
213 myModelWidgets.append(aSelector);
214 return aSelector->getControl();
217 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
219 ModuleBase_WidgetBoolValue* aBoolWgt =
220 new ModuleBase_WidgetBoolValue(theParent, myWidgetApi, myParentId);
221 myModelWidgets.append(aBoolWgt);
222 return aBoolWgt->getControl();
225 QWidget* ModuleBase_WidgetFactory::fileSelectorControl(QWidget* theParent)
227 ModuleBase_WidgetFileSelector* aFileSelectorWgt =
228 new ModuleBase_WidgetFileSelector(theParent, myWidgetApi, myParentId);
229 myModelWidgets.append(aFileSelectorWgt);
230 return aFileSelectorWgt->getControl();
233 QWidget* ModuleBase_WidgetFactory::choiceControl(QWidget* theParent)
235 ModuleBase_WidgetChoice* aChoiceWgt =
236 new ModuleBase_WidgetChoice(theParent, myWidgetApi,myParentId);
237 myModelWidgets.append(aChoiceWgt);
238 return aChoiceWgt->getControl();
241 QWidget* ModuleBase_WidgetFactory::lineEditControl(QWidget* theParent)
243 ModuleBase_WidgetLineEdit* aLineEditWgt =
244 new ModuleBase_WidgetLineEdit(theParent, myWidgetApi,myParentId);
245 myModelWidgets.append(aLineEditWgt);
246 return aLineEditWgt->getControl();
249 QWidget* ModuleBase_WidgetFactory::multiSelectorControl(QWidget* theParent)
251 ModuleBase_WidgetMultiSelector* aMultiselectorWgt =
252 new ModuleBase_WidgetMultiSelector(theParent, myWorkshop, myWidgetApi,myParentId);
253 myModelWidgets.append(aMultiselectorWgt);
254 return aMultiselectorWgt->getControl();
257 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString)
259 return QString::fromStdString(theStdString);