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>
30 #include <ModuleBase_WidgetToolbox.h>
32 #include <ModelAPI_Validator.h>
33 #include <ModelAPI_Session.h>
35 #include <Config_Keywords.h>
36 #include <Config_WidgetAPI.h>
39 #include <QHBoxLayout>
40 #include <QGridLayout>
42 #include <QMetaProperty>
55 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,
56 ModuleBase_IWorkshop* theWorkshop)
57 : myWorkshop(theWorkshop)
59 myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
62 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
67 void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
69 myParentId = myWidgetApi->widgetId();
70 if (!myWidgetApi->toChildWidget())
73 QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
74 bool isStretchLayout = false;
75 do { //Iterate over each node
76 std::string aWdgType = myWidgetApi->widgetType();
77 //Create a widget (doublevalue, groupbox, toolbox, etc.
78 QWidget* aWidget = createWidgetByType(aWdgType, theParent);
80 if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {
81 aWidgetLay->addWidget(aWidget);
83 aWidget->setVisible(false);
86 if (myWidgetApi->isContainerWidget()) {
87 //if current widget is groupbox (container) process it's children recursively
88 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
89 createWidget(aWidget);
90 ModuleBase_Tools::adjustMargins(aWidget);
91 QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
92 aGrBox->setTitle(aGroupName);
94 if (myWidgetApi->isPagedWidget()) {
95 //If current widget is toolbox or switch-casebox then fetch all
96 //it's pages recursively and setup into the widget.
97 myWidgetApi->toChildWidget();
99 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
100 QString aCaseId = qs(myWidgetApi->getProperty(_ID));
101 QWidget* aPage = new QWidget(aWidget);
103 ModuleBase_Tools::adjustMargins(aPage);
104 if (aWdgType == WDG_SWITCH) {
105 ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
106 aSwitch->addPage(aPage, aPageName);
107 } else if (aWdgType == WDG_TOOLBOX) {
108 ModuleBase_WidgetToolbox* aToolbox = qobject_cast<ModuleBase_WidgetToolbox*>(aWidget);
109 aToolbox->addPage(aPage, aPageName, aCaseId);
111 } while (myWidgetApi->toNextWidget());
113 if (aWidget && !isStretchLayout) {
114 isStretchLayout = !hasExpandingControls(aWidget);
116 } while (myWidgetApi->toNextWidget());
117 if (isStretchLayout) {
118 aWidgetLay->addStretch(1);
120 theParent->setLayout(aWidgetLay);
123 bool ModuleBase_WidgetFactory::hasExpandingControls(QWidget* theParent)
126 QList<QWidget *> aListToCheck;
127 aListToCheck << theParent;
128 ModuleBase_ModelWidget* aModelWidget = qobject_cast<ModuleBase_ModelWidget*>(theParent);
130 aListToCheck << aModelWidget->getControls();
132 foreach(QWidget* eachWidget, aListToCheck) {
133 QSizePolicy::Policy aVPolicy = eachWidget->sizePolicy().verticalPolicy();
134 if(aVPolicy & QSizePolicy::ExpandFlag) {
141 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
144 QWidget* result = NULL;
145 if (theType == WDG_DOUBLEVALUE) {
146 result = doubleSpinBoxControl(theParent);
148 } else if (theType == WDG_INFO) {
149 result = labelControl(theParent);
151 } else if (theType == WDG_SHAPE_SELECTOR) {
152 result = shapeSelectorControl(theParent);
154 } else if (theType == WDG_BOOLVALUE) {
155 result = booleanControl(theParent);
157 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
158 result = doubleValueEditor(theParent);
160 } else if (theType == WDG_FILE_SELECTOR) {
161 result = fileSelectorControl(theParent);
163 } else if (theType == WDG_CHOICE) {
164 result = choiceControl(theParent);
166 } else if (theType == WDG_STRINGVALUE) {
167 result = lineEditControl(theParent);
169 } else if (theType == WDG_MULTISELECTOR) {
170 result = multiSelectorControl(theParent);
172 } else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
173 result = createContainer(theType, theParent);
175 result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi,
176 myParentId, myModelWidgets);
178 if (!result) {qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type");}
184 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
186 QWidget* aResult = NULL;
187 if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
188 QGroupBox* aGroupBox = new QGroupBox(theParent);
189 aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
191 } else if (theType == WDG_TOOLBOX) {
192 ModuleBase_WidgetToolbox* aWdg = new ModuleBase_WidgetToolbox(theParent, myWidgetApi, myParentId);
193 myModelWidgets.append(aWdg);
195 } else if (theType == WDG_SWITCH) {
196 aResult = new ModuleBase_WidgetSwitch(theParent);
197 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
198 // Do nothing for "box" and "case"
202 else {qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type";}
207 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
209 ModuleBase_WidgetLabel* aWgt =
210 new ModuleBase_WidgetLabel(theParent, myWidgetApi, myParentId);
211 myModelWidgets.append(aWgt);
215 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
217 ModuleBase_WidgetDoubleValue* aDblWgt =
218 new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi, myParentId);
219 myModelWidgets.append(aDblWgt);
223 QWidget* ModuleBase_WidgetFactory::doubleValueEditor(QWidget* theParent)
225 ModuleBase_WidgetEditor* aWidget =
226 new ModuleBase_WidgetEditor(theParent, myWidgetApi, myParentId);
227 myModelWidgets.append(aWidget);
231 QWidget* ModuleBase_WidgetFactory::shapeSelectorControl(QWidget* theParent)
233 ModuleBase_WidgetShapeSelector* aSelector =
234 new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi, myParentId);
235 myModelWidgets.append(aSelector);
239 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
241 ModuleBase_WidgetBoolValue* aBoolWgt =
242 new ModuleBase_WidgetBoolValue(theParent, myWidgetApi, myParentId);
243 myModelWidgets.append(aBoolWgt);
247 QWidget* ModuleBase_WidgetFactory::fileSelectorControl(QWidget* theParent)
249 ModuleBase_WidgetFileSelector* aFileSelectorWgt =
250 new ModuleBase_WidgetFileSelector(theParent, myWidgetApi, myParentId);
251 myModelWidgets.append(aFileSelectorWgt);
252 return aFileSelectorWgt;
255 QWidget* ModuleBase_WidgetFactory::choiceControl(QWidget* theParent)
257 ModuleBase_WidgetChoice* aChoiceWgt =
258 new ModuleBase_WidgetChoice(theParent, myWidgetApi,myParentId);
259 myModelWidgets.append(aChoiceWgt);
263 QWidget* ModuleBase_WidgetFactory::lineEditControl(QWidget* theParent)
265 ModuleBase_WidgetLineEdit* aLineEditWgt =
266 new ModuleBase_WidgetLineEdit(theParent, myWidgetApi,myParentId);
267 myModelWidgets.append(aLineEditWgt);
271 QWidget* ModuleBase_WidgetFactory::multiSelectorControl(QWidget* theParent)
273 ModuleBase_WidgetMultiSelector* aMultiselectorWgt =
274 new ModuleBase_WidgetMultiSelector(theParent, myWorkshop, myWidgetApi,myParentId);
275 myModelWidgets.append(aMultiselectorWgt);
276 return aMultiselectorWgt;
279 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString)
281 return QString::fromStdString(theStdString);