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>
31 #include <Config_Keywords.h>
32 #include <Config_WidgetAPI.h>
35 #include <QHBoxLayout>
36 #include <QGridLayout>
38 #include <QMetaProperty>
51 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,
52 ModuleBase_IWorkshop* theWorkshop)
53 : myWorkshop(theWorkshop)
55 myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
58 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
62 void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
64 myParentId = myWidgetApi->widgetId();
65 if (!myWidgetApi->toChildWidget())
68 QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
69 do { //Iterate over each node
70 std::string aWdgType = myWidgetApi->widgetType();
71 //Create a widget (doublevalue, groupbox, toolbox, etc.
72 QWidget* aWidget = createWidgetByType(aWdgType, theParent);
74 if (!myWidgetApi->getBooleanAttribute(FEATURE_INTERNAL, false)) {
75 aWidgetLay->addWidget(aWidget);
77 aWidget->setVisible(false);
80 if (myWidgetApi->isContainerWidget()) {
81 //if current widget is groupbox (container) process it's children recursively
82 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
83 createWidget(aWidget);
84 ModuleBase_Tools::adjustMargins(aWidget);
85 QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
86 aGrBox->setTitle(aGroupName);
88 if (myWidgetApi->isPagedWidget()) {
89 //If current widget is toolbox or switch-casebox then fetch all
90 //it's pages recursively and setup into the widget.
91 myWidgetApi->toChildWidget();
93 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
94 QWidget* aPage = new QWidget(aWidget);
95 ModuleBase_Tools::adjustMargins(aPage);
97 if (aWdgType == WDG_SWITCH) {
98 ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
99 aSwitch->addPage(aPage, aPageName);
100 } else if (aWdgType == WDG_TOOLBOX) {
101 QToolBox* aToolbox = qobject_cast<QToolBox*>(aWidget);
102 aToolbox->addItem(aPage, aPageName);
104 } while (myWidgetApi->toNextWidget());
106 } while (myWidgetApi->toNextWidget());
107 theParent->setLayout(aWidgetLay);
110 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
112 QWidget* result = new QWidget(theParent);
113 QVBoxLayout* aLabelLay = new QVBoxLayout(result);
114 QLabel* aLabel = new QLabel(result);
115 aLabel->setWordWrap(true);
116 aLabel->setText(qs(myWidgetApi->getProperty(INFO_WDG_TEXT)));
117 aLabel->setToolTip(qs(myWidgetApi->getProperty(INFO_WDG_TOOLTIP)));
118 aLabelLay->addWidget(aLabel);
119 aLabelLay->addStretch(1);
120 result->setLayout(aLabelLay);
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_POINT_SELECTOR) {
141 result = pointSelectorControl(theParent);
143 } else if (theType == WDG_FEATURE_SELECTOR) {
144 result = featureSelectorControl(theParent);
146 } else if (theType == WDG_FEATURE_OR_ATTRIBUTE_SELECTOR) {
147 result = featureOrAttributeSelectorControl(theParent);
149 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
150 result = doubleValueEditor(theParent);
152 } else if (theType == WDG_POINT2D_DISTANCE) {
153 result = point2dDistanceControl(theParent);
155 } else if (theType == WDG_FILE_SELECTOR) {
156 result = fileSelectorControl(theParent);
158 } else if (theType == WDG_CHOICE) {
159 result = choiceControl(theParent);
161 } else if (theType == WDG_STRINGVALUE) {
162 result = lineEditControl(theParent);
164 } else if (theType == WDG_MULTISELECTOR) {
165 result = multiSelectorControl(theParent);
167 } else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
168 result = createContainer(theType, theParent);
170 result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi,
173 if (!result) {qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type");}
177 // register that this attribute in feature is not obligatory for the feature execution
178 // so, it is not needed for the standard validation mechanism
180 myWidgetApi ? myWidgetApi->getBooleanAttribute(FEATURE_OBLIGATORY, true) : true;
182 ModelAPI_Session::get()->validators()->registerNotObligatory(
183 myParentId, myWidgetApi->widgetId());
190 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
192 QWidget* result = NULL;
193 if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
194 QGroupBox* aGroupBox = new QGroupBox(theParent);
195 aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
197 } else if (theType == WDG_TOOLBOX) {
198 result = new QToolBox(theParent);
199 } else if (theType == WDG_SWITCH) {
200 result = new ModuleBase_WidgetSwitch(theParent);
201 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
205 else {qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type";}
210 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
212 ModuleBase_WidgetDoubleValue* aDblWgt =
213 new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi, myParentId);
214 myModelWidgets.append(aDblWgt);
215 return aDblWgt->getControl();
218 QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
220 ModuleBase_WidgetPoint2D* aWidget =
221 new ModuleBase_WidgetPoint2D(theParent, myWidgetApi,myParentId);
222 myModelWidgets.append(aWidget);
223 return aWidget->getControl();
226 QWidget* ModuleBase_WidgetFactory::featureSelectorControl(QWidget* theParent)
228 ModuleBase_WidgetFeature* aWidget =
229 new ModuleBase_WidgetFeature(theParent, myWidgetApi,myParentId);
230 myModelWidgets.append(aWidget);
231 return aWidget->getControl();
234 QWidget* ModuleBase_WidgetFactory::featureOrAttributeSelectorControl(QWidget* theParent)
236 ModuleBase_WidgetFeatureOrAttribute* aWidget =
237 new ModuleBase_WidgetFeatureOrAttribute(theParent, myWidgetApi, myParentId);
238 myModelWidgets.append(aWidget);
239 return aWidget->getControl();
242 QWidget* ModuleBase_WidgetFactory::doubleValueEditor(QWidget* theParent)
244 ModuleBase_WidgetEditor* aWidget =
245 new ModuleBase_WidgetEditor(theParent, myWidgetApi, myParentId);
246 myModelWidgets.append(aWidget);
247 return aWidget->getControl();
250 QWidget* ModuleBase_WidgetFactory::shapeSelectorControl(QWidget* theParent)
252 ModuleBase_WidgetShapeSelector* aSelector =
253 new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi, myParentId);
254 myModelWidgets.append(aSelector);
255 return aSelector->getControl();
258 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
260 ModuleBase_WidgetBoolValue* aBoolWgt =
261 new ModuleBase_WidgetBoolValue(theParent, myWidgetApi, myParentId);
262 myModelWidgets.append(aBoolWgt);
263 return aBoolWgt->getControl();
266 QWidget* ModuleBase_WidgetFactory::point2dDistanceControl(QWidget* theParent)
268 ModuleBase_WidgetPoint2dDistance* aDistWgt =
269 new ModuleBase_WidgetPoint2dDistance(theParent, myWidgetApi, myParentId);
270 myModelWidgets.append(aDistWgt);
271 return aDistWgt->getControl();
274 QWidget* ModuleBase_WidgetFactory::fileSelectorControl(QWidget* theParent)
276 ModuleBase_WidgetFileSelector* aFileSelectorWgt =
277 new ModuleBase_WidgetFileSelector(theParent, myWidgetApi, myParentId);
278 myModelWidgets.append(aFileSelectorWgt);
279 return aFileSelectorWgt->getControl();
282 QWidget* ModuleBase_WidgetFactory::choiceControl(QWidget* theParent)
284 ModuleBase_WidgetChoice* aChoiceWgt =
285 new ModuleBase_WidgetChoice(theParent, myWidgetApi,myParentId);
286 myModelWidgets.append(aChoiceWgt);
287 return aChoiceWgt->getControl();
290 QWidget* ModuleBase_WidgetFactory::lineEditControl(QWidget* theParent)
292 ModuleBase_WidgetLineEdit* aLineEditWgt =
293 new ModuleBase_WidgetLineEdit(theParent, myWidgetApi,myParentId);
294 myModelWidgets.append(aLineEditWgt);
295 return aLineEditWgt->getControl();
298 QWidget* ModuleBase_WidgetFactory::multiSelectorControl(QWidget* theParent)
300 ModuleBase_WidgetMultiSelector* aMultiselectorWgt =
301 new ModuleBase_WidgetMultiSelector(theParent, myWidgetApi,myParentId);
302 myModelWidgets.append(aMultiselectorWgt);
303 return aMultiselectorWgt->getControl();
306 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
308 return QString::fromStdString(theStdString);