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 bool isStretchLayout = false;
74 do { //Iterate over each node
75 std::string aWdgType = myWidgetApi->widgetType();
76 //Create a widget (doublevalue, groupbox, toolbox, etc.
77 QWidget* aWidget = createWidgetByType(aWdgType, theParent);
79 if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {
80 aWidgetLay->addWidget(aWidget);
82 aWidget->setVisible(false);
85 if (myWidgetApi->isContainerWidget()) {
86 //if current widget is groupbox (container) process it's children recursively
87 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
88 createWidget(aWidget);
89 ModuleBase_Tools::adjustMargins(aWidget);
90 QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
91 aGrBox->setTitle(aGroupName);
93 if (myWidgetApi->isPagedWidget()) {
94 //If current widget is toolbox or switch-casebox then fetch all
95 //it's pages recursively and setup into the widget.
96 myWidgetApi->toChildWidget();
98 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
99 QWidget* aPage = new QWidget(aWidget);
101 ModuleBase_Tools::adjustMargins(aPage);
102 if (aWdgType == WDG_SWITCH) {
103 ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
104 aSwitch->addPage(aPage, aPageName);
105 } else if (aWdgType == WDG_TOOLBOX) {
106 QToolBox* aToolbox = qobject_cast<QToolBox*>(aWidget);
107 aToolbox->addItem(aPage, aPageName);
110 } while (myWidgetApi->toNextWidget());
112 if (aWidget && !isStretchLayout) {
113 isStretchLayout = !hasExpandingControls(aWidget);
115 } while (myWidgetApi->toNextWidget());
116 if (isStretchLayout) {
117 aWidgetLay->addStretch(1);
119 theParent->setLayout(aWidgetLay);
122 bool ModuleBase_WidgetFactory::hasExpandingControls(QWidget* theParent)
125 QList<QWidget *> aListToCheck;
126 aListToCheck << theParent;
127 ModuleBase_ModelWidget* aModelWidget = qobject_cast<ModuleBase_ModelWidget*>(theParent);
129 aListToCheck << aModelWidget->getControls();
131 foreach(QWidget* eachWidget, aListToCheck) {
132 QSizePolicy::Policy aVPolicy = eachWidget->sizePolicy().verticalPolicy();
133 if(aVPolicy & QSizePolicy::ExpandFlag) {
140 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
143 QWidget* result = NULL;
144 if (theType == WDG_DOUBLEVALUE) {
145 result = doubleSpinBoxControl(theParent);
147 } else if (theType == WDG_INFO) {
148 result = labelControl(theParent);
150 } else if (theType == WDG_SHAPE_SELECTOR) {
151 result = shapeSelectorControl(theParent);
153 } else if (theType == WDG_BOOLVALUE) {
154 result = booleanControl(theParent);
156 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
157 result = doubleValueEditor(theParent);
159 } else if (theType == WDG_FILE_SELECTOR) {
160 result = fileSelectorControl(theParent);
162 } else if (theType == WDG_CHOICE) {
163 result = choiceControl(theParent);
165 } else if (theType == WDG_STRINGVALUE) {
166 result = lineEditControl(theParent);
168 } else if (theType == WDG_MULTISELECTOR) {
169 result = multiSelectorControl(theParent);
171 } else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
172 result = createContainer(theType, theParent);
174 result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi,
175 myParentId, myModelWidgets);
177 if (!result) {qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type");}
183 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
185 QWidget* result = NULL;
186 if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
187 QGroupBox* aGroupBox = new QGroupBox(theParent);
188 aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
190 } else if (theType == WDG_TOOLBOX) {
191 result = new QToolBox(theParent);
192 // Dark-grey rounded tabs with button-like border #and bold font
193 QString css = "QToolBox::tab{background-color:#c8c8c8;"
197 "border-color:#fff #505050 #505050 #fff;}";
198 result->setStyleSheet(css);
199 // default vertical size policy is preferred
200 QSizePolicy aSizePolicy = result->sizePolicy();
201 aSizePolicy.setVerticalPolicy(QSizePolicy::MinimumExpanding);
202 result->setSizePolicy(aSizePolicy);
203 } else if (theType == WDG_SWITCH) {
204 result = new ModuleBase_WidgetSwitch(theParent);
205 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
206 // Do nothing for "box" and "case"
210 else {qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type";}
215 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
217 ModuleBase_WidgetLabel* aWgt =
218 new ModuleBase_WidgetLabel(theParent, myWidgetApi, myParentId);
219 myModelWidgets.append(aWgt);
223 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
225 ModuleBase_WidgetDoubleValue* aDblWgt =
226 new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi, myParentId);
227 myModelWidgets.append(aDblWgt);
231 QWidget* ModuleBase_WidgetFactory::doubleValueEditor(QWidget* theParent)
233 ModuleBase_WidgetEditor* aWidget =
234 new ModuleBase_WidgetEditor(theParent, myWidgetApi, myParentId);
235 myModelWidgets.append(aWidget);
239 QWidget* ModuleBase_WidgetFactory::shapeSelectorControl(QWidget* theParent)
241 ModuleBase_WidgetShapeSelector* aSelector =
242 new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi, myParentId);
243 myModelWidgets.append(aSelector);
247 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
249 ModuleBase_WidgetBoolValue* aBoolWgt =
250 new ModuleBase_WidgetBoolValue(theParent, myWidgetApi, myParentId);
251 myModelWidgets.append(aBoolWgt);
255 QWidget* ModuleBase_WidgetFactory::fileSelectorControl(QWidget* theParent)
257 ModuleBase_WidgetFileSelector* aFileSelectorWgt =
258 new ModuleBase_WidgetFileSelector(theParent, myWidgetApi, myParentId);
259 myModelWidgets.append(aFileSelectorWgt);
260 return aFileSelectorWgt;
263 QWidget* ModuleBase_WidgetFactory::choiceControl(QWidget* theParent)
265 ModuleBase_WidgetChoice* aChoiceWgt =
266 new ModuleBase_WidgetChoice(theParent, myWidgetApi,myParentId);
267 myModelWidgets.append(aChoiceWgt);
271 QWidget* ModuleBase_WidgetFactory::lineEditControl(QWidget* theParent)
273 ModuleBase_WidgetLineEdit* aLineEditWgt =
274 new ModuleBase_WidgetLineEdit(theParent, myWidgetApi,myParentId);
275 myModelWidgets.append(aLineEditWgt);
279 QWidget* ModuleBase_WidgetFactory::multiSelectorControl(QWidget* theParent)
281 ModuleBase_WidgetMultiSelector* aMultiselectorWgt =
282 new ModuleBase_WidgetMultiSelector(theParent, myWorkshop, myWidgetApi,myParentId);
283 myModelWidgets.append(aMultiselectorWgt);
284 return aMultiselectorWgt;
287 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString)
289 return QString::fromStdString(theStdString);