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_WidgetPoint2D.h>
13 #include <ModuleBase_WidgetFeatureOrAttribute.h>
14 #include <ModuleBase_WidgetFeature.h>
15 #include <ModuleBase_WidgetEditor.h>
16 #include <ModuleBase_WidgetSwitch.h>
17 #include <ModuleBase_WidgetShapeSelector.h>
18 #include <ModuleBase_WidgetDoubleValue.h>
19 #include <ModuleBase_WidgetBoolValue.h>
20 #include <ModuleBase_WidgetPoint2dDistance.h>
21 #include <ModuleBase_WidgetFileSelector.h>
22 #include <ModuleBase_WidgetChoice.h>
23 #include <ModuleBase_IWorkshop.h>
24 #include <ModuleBase_IModule.h>
25 #include <ModuleBase_Tools.h>
26 #include <ModuleBase_WidgetLineEdit.h>
27 #include <ModuleBase_WidgetMultiSelector.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);
112 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
114 QWidget* result = new QWidget(theParent);
115 QVBoxLayout* aLabelLay = new QVBoxLayout(result);
116 QLabel* aLabel = new QLabel(result);
117 aLabel->setWordWrap(true);
118 aLabel->setText(qs(myWidgetApi->getProperty(INFO_WDG_TEXT)));
119 aLabel->setToolTip(qs(myWidgetApi->getProperty(INFO_WDG_TOOLTIP)));
120 aLabelLay->addWidget(aLabel);
121 aLabelLay->addStretch(1);
122 result->setLayout(aLabelLay);
126 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
129 QWidget* result = NULL;
130 if (theType == WDG_DOUBLEVALUE) {
131 result = doubleSpinBoxControl(theParent);
133 } else if (theType == WDG_INFO) {
134 result = labelControl(theParent);
136 } else if (theType == WDG_SHAPE_SELECTOR) {
137 result = shapeSelectorControl(theParent);
139 } else if (theType == WDG_BOOLVALUE) {
140 result = booleanControl(theParent);
142 } else if (theType == WDG_POINT_SELECTOR) {
143 result = pointSelectorControl(theParent);
145 } else if (theType == WDG_FEATURE_SELECTOR) {
146 result = featureSelectorControl(theParent);
148 } else if (theType == WDG_FEATURE_OR_ATTRIBUTE_SELECTOR) {
149 result = featureOrAttributeSelectorControl(theParent);
151 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
152 result = doubleValueEditor(theParent);
154 } else if (theType == WDG_POINT2D_DISTANCE) {
155 result = point2dDistanceControl(theParent);
157 } else if (theType == WDG_FILE_SELECTOR) {
158 result = fileSelectorControl(theParent);
160 } else if (theType == WDG_CHOICE) {
161 result = choiceControl(theParent);
163 } else if (theType == WDG_STRINGVALUE) {
164 result = lineEditControl(theParent);
166 } else if (theType == WDG_MULTISELECTOR) {
167 result = multiSelectorControl(theParent);
169 } else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
170 result = createContainer(theType, theParent);
172 result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi,
175 if (!result) {qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type");}
181 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
183 QWidget* result = NULL;
184 if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
185 QGroupBox* aGroupBox = new QGroupBox(theParent);
186 aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
188 } else if (theType == WDG_TOOLBOX) {
189 result = new QToolBox(theParent);
190 } else if (theType == WDG_SWITCH) {
191 result = new ModuleBase_WidgetSwitch(theParent);
192 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
196 else {qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type";}
201 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
203 ModuleBase_WidgetDoubleValue* aDblWgt =
204 new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi, myParentId);
205 myModelWidgets.append(aDblWgt);
206 return aDblWgt->getControl();
209 QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
211 ModuleBase_WidgetPoint2D* aWidget =
212 new ModuleBase_WidgetPoint2D(theParent, myWidgetApi,myParentId);
213 myModelWidgets.append(aWidget);
214 return aWidget->getControl();
217 QWidget* ModuleBase_WidgetFactory::featureSelectorControl(QWidget* theParent)
219 ModuleBase_WidgetFeature* aWidget =
220 new ModuleBase_WidgetFeature(theParent, myWidgetApi,myParentId);
221 myModelWidgets.append(aWidget);
222 return aWidget->getControl();
225 QWidget* ModuleBase_WidgetFactory::featureOrAttributeSelectorControl(QWidget* theParent)
227 ModuleBase_WidgetFeatureOrAttribute* aWidget =
228 new ModuleBase_WidgetFeatureOrAttribute(theParent, myWidgetApi, myParentId);
229 myModelWidgets.append(aWidget);
230 return aWidget->getControl();
233 QWidget* ModuleBase_WidgetFactory::doubleValueEditor(QWidget* theParent)
235 ModuleBase_WidgetEditor* aWidget =
236 new ModuleBase_WidgetEditor(theParent, myWidgetApi, myParentId);
237 myModelWidgets.append(aWidget);
238 return aWidget->getControl();
241 QWidget* ModuleBase_WidgetFactory::shapeSelectorControl(QWidget* theParent)
243 ModuleBase_WidgetShapeSelector* aSelector =
244 new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi, myParentId);
245 myModelWidgets.append(aSelector);
246 return aSelector->getControl();
249 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
251 ModuleBase_WidgetBoolValue* aBoolWgt =
252 new ModuleBase_WidgetBoolValue(theParent, myWidgetApi, myParentId);
253 myModelWidgets.append(aBoolWgt);
254 return aBoolWgt->getControl();
257 QWidget* ModuleBase_WidgetFactory::point2dDistanceControl(QWidget* theParent)
259 ModuleBase_WidgetPoint2dDistance* aDistWgt =
260 new ModuleBase_WidgetPoint2dDistance(theParent, myWidgetApi, myParentId);
261 myModelWidgets.append(aDistWgt);
262 return aDistWgt->getControl();
265 QWidget* ModuleBase_WidgetFactory::fileSelectorControl(QWidget* theParent)
267 ModuleBase_WidgetFileSelector* aFileSelectorWgt =
268 new ModuleBase_WidgetFileSelector(theParent, myWidgetApi, myParentId);
269 myModelWidgets.append(aFileSelectorWgt);
270 return aFileSelectorWgt->getControl();
273 QWidget* ModuleBase_WidgetFactory::choiceControl(QWidget* theParent)
275 ModuleBase_WidgetChoice* aChoiceWgt =
276 new ModuleBase_WidgetChoice(theParent, myWidgetApi,myParentId);
277 myModelWidgets.append(aChoiceWgt);
278 return aChoiceWgt->getControl();
281 QWidget* ModuleBase_WidgetFactory::lineEditControl(QWidget* theParent)
283 ModuleBase_WidgetLineEdit* aLineEditWgt =
284 new ModuleBase_WidgetLineEdit(theParent, myWidgetApi,myParentId);
285 myModelWidgets.append(aLineEditWgt);
286 return aLineEditWgt->getControl();
289 QWidget* ModuleBase_WidgetFactory::multiSelectorControl(QWidget* theParent)
291 ModuleBase_WidgetMultiSelector* aMultiselectorWgt =
292 new ModuleBase_WidgetMultiSelector(theParent, myWorkshop, myWidgetApi,myParentId);
293 myModelWidgets.append(aMultiselectorWgt);
294 return aMultiselectorWgt->getControl();
297 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
299 return QString::fromStdString(theStdString);