2 * ModuleBase_WidgetFactory.cpp
4 * Created on: Apr 3, 2014
8 #include <ModuleBase_WidgetFactory.h>
10 #include <ModuleBase_Operation.h>
11 #include <ModuleBase_OperationDescription.h>
12 //#include <ModuleBase_WidgetFeatureOrAttribute.h>
13 //#include <ModuleBase_WidgetFeature.h>
14 #include <ModuleBase_WidgetEditor.h>
15 #include <ModuleBase_WidgetSwitch.h>
16 #include <ModuleBase_WidgetShapeSelector.h>
17 #include <ModuleBase_WidgetDoubleValue.h>
18 #include <ModuleBase_WidgetBoolValue.h>
19 //#include <ModuleBase_WidgetPoint2dDistance.h>
20 #include <ModuleBase_WidgetFileSelector.h>
21 #include <ModuleBase_WidgetChoice.h>
22 #include <ModuleBase_IWorkshop.h>
23 #include <ModuleBase_IModule.h>
24 #include <ModuleBase_Tools.h>
25 #include <ModuleBase_WidgetLineEdit.h>
26 #include <ModuleBase_WidgetMultiSelector.h>
27 #include <ModuleBase_WidgetLabel.h>
29 #include <ModelAPI_Validator.h>
30 #include <ModelAPI_Session.h>
32 #include <Config_Keywords.h>
33 #include <Config_WidgetAPI.h>
36 #include <QHBoxLayout>
37 #include <QGridLayout>
39 #include <QMetaProperty>
52 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,
53 ModuleBase_IWorkshop* theWorkshop)
54 : myWorkshop(theWorkshop)
56 myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
59 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
64 void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
66 myParentId = myWidgetApi->widgetId();
67 if (!myWidgetApi->toChildWidget())
70 QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
71 do { //Iterate over each node
72 std::string aWdgType = myWidgetApi->widgetType();
73 //Create a widget (doublevalue, groupbox, toolbox, etc.
74 QWidget* aWidget = createWidgetByType(aWdgType, theParent);
76 if (!myWidgetApi->getBooleanAttribute(ATTRIBUTE_INTERNAL, false)) {
77 aWidgetLay->addWidget(aWidget);
79 aWidget->setVisible(false);
82 if (myWidgetApi->isContainerWidget()) {
83 //if current widget is groupbox (container) process it's children recursively
84 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
85 createWidget(aWidget);
86 ModuleBase_Tools::adjustMargins(aWidget);
87 QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
88 aGrBox->setTitle(aGroupName);
90 if (myWidgetApi->isPagedWidget()) {
91 //If current widget is toolbox or switch-casebox then fetch all
92 //it's pages recursively and setup into the widget.
93 myWidgetApi->toChildWidget();
95 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
96 QWidget* aPage = new QWidget(aWidget);
97 ModuleBase_Tools::adjustMargins(aPage);
99 if (aWdgType == WDG_SWITCH) {
100 ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
101 aSwitch->addPage(aPage, aPageName);
102 } else if (aWdgType == WDG_TOOLBOX) {
103 QToolBox* aToolbox = qobject_cast<QToolBox*>(aWidget);
104 aToolbox->addItem(aPage, aPageName);
106 } while (myWidgetApi->toNextWidget());
108 } while (myWidgetApi->toNextWidget());
109 theParent->setLayout(aWidgetLay);
113 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
115 ModuleBase_WidgetLabel* aWgt =
116 new ModuleBase_WidgetLabel(theParent, myWidgetApi, myParentId);
117 myModelWidgets.append(aWgt);
118 return aWgt->getControl();
122 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
125 QWidget* result = NULL;
126 if (theType == WDG_DOUBLEVALUE) {
127 result = doubleSpinBoxControl(theParent);
129 } else if (theType == WDG_INFO) {
130 result = labelControl(theParent);
132 } else if (theType == WDG_SHAPE_SELECTOR) {
133 result = shapeSelectorControl(theParent);
135 } else if (theType == WDG_BOOLVALUE) {
136 result = booleanControl(theParent);
138 //} else if (theType == WDG_FEATURE_SELECTOR) {
139 // result = featureSelectorControl(theParent);
141 //} else if (theType == WDG_FEATURE_OR_ATTRIBUTE_SELECTOR) {
142 // result = featureOrAttributeSelectorControl(theParent);
144 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
145 result = doubleValueEditor(theParent);
147 } else if (theType == WDG_FILE_SELECTOR) {
148 result = fileSelectorControl(theParent);
150 } else if (theType == WDG_CHOICE) {
151 result = choiceControl(theParent);
153 } else if (theType == WDG_STRINGVALUE) {
154 result = lineEditControl(theParent);
156 } else if (theType == WDG_MULTISELECTOR) {
157 result = multiSelectorControl(theParent);
159 } else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
160 result = createContainer(theType, theParent);
162 result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi,
163 myParentId, myModelWidgets);
165 if (!result) {qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type");}
171 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
173 QWidget* result = NULL;
174 if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
175 QGroupBox* aGroupBox = new QGroupBox(theParent);
176 aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
178 } else if (theType == WDG_TOOLBOX) {
179 result = new QToolBox(theParent);
180 } else if (theType == WDG_SWITCH) {
181 result = new ModuleBase_WidgetSwitch(theParent);
182 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
186 else {qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type";}
191 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
193 ModuleBase_WidgetDoubleValue* aDblWgt =
194 new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi, myParentId);
195 myModelWidgets.append(aDblWgt);
196 return aDblWgt->getControl();
199 QWidget* ModuleBase_WidgetFactory::doubleValueEditor(QWidget* theParent)
201 ModuleBase_WidgetEditor* aWidget =
202 new ModuleBase_WidgetEditor(theParent, myWidgetApi, myParentId);
203 myModelWidgets.append(aWidget);
204 return aWidget->getControl();
207 QWidget* ModuleBase_WidgetFactory::shapeSelectorControl(QWidget* theParent)
209 ModuleBase_WidgetShapeSelector* aSelector =
210 new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi, myParentId);
211 myModelWidgets.append(aSelector);
212 return aSelector->getControl();
215 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
217 ModuleBase_WidgetBoolValue* aBoolWgt =
218 new ModuleBase_WidgetBoolValue(theParent, myWidgetApi, myParentId);
219 myModelWidgets.append(aBoolWgt);
220 return aBoolWgt->getControl();
223 QWidget* ModuleBase_WidgetFactory::fileSelectorControl(QWidget* theParent)
225 ModuleBase_WidgetFileSelector* aFileSelectorWgt =
226 new ModuleBase_WidgetFileSelector(theParent, myWidgetApi, myParentId);
227 myModelWidgets.append(aFileSelectorWgt);
228 return aFileSelectorWgt->getControl();
231 QWidget* ModuleBase_WidgetFactory::choiceControl(QWidget* theParent)
233 ModuleBase_WidgetChoice* aChoiceWgt =
234 new ModuleBase_WidgetChoice(theParent, myWidgetApi,myParentId);
235 myModelWidgets.append(aChoiceWgt);
236 return aChoiceWgt->getControl();
239 QWidget* ModuleBase_WidgetFactory::lineEditControl(QWidget* theParent)
241 ModuleBase_WidgetLineEdit* aLineEditWgt =
242 new ModuleBase_WidgetLineEdit(theParent, myWidgetApi,myParentId);
243 myModelWidgets.append(aLineEditWgt);
244 return aLineEditWgt->getControl();
247 QWidget* ModuleBase_WidgetFactory::multiSelectorControl(QWidget* theParent)
249 ModuleBase_WidgetMultiSelector* aMultiselectorWgt =
250 new ModuleBase_WidgetMultiSelector(theParent, myWorkshop, myWidgetApi,myParentId);
251 myModelWidgets.append(aMultiselectorWgt);
252 return aMultiselectorWgt->getControl();
255 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
257 return QString::fromStdString(theStdString);