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 <ModelAPI_Validator.h>
27 #include <Config_Keywords.h>
28 #include <Config_WidgetAPI.h>
31 #include <QHBoxLayout>
32 #include <QGridLayout>
34 #include <QMetaProperty>
47 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,
48 ModuleBase_IWorkshop* theWorkshop)
49 : myWorkshop(theWorkshop)
51 myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
54 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
58 void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
60 myParentId = myWidgetApi->widgetId();
61 if (!myWidgetApi->toChildWidget())
64 QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
65 aWidgetLay->setContentsMargins(2, 2, 2, 2);
66 do { //Iterate over each node
67 std::string aWdgType = myWidgetApi->widgetType();
68 //Create a widget (doublevalue, groupbox, toolbox, etc.
69 QWidget* aWidget = createWidgetByType(aWdgType, theParent);
71 if (!myWidgetApi->getBooleanAttribute(FEATURE_INTERNAL, false)) {
72 aWidgetLay->addWidget(aWidget);
74 aWidget->setVisible(false);
77 if (myWidgetApi->isContainerWidget()) {
78 //if current widget is groupbox (container) process it's children recursively
79 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
80 createWidget(aWidget);
81 QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
82 aGrBox->setTitle(aGroupName);
84 if (myWidgetApi->isPagedWidget()) {
85 //If current widget is toolbox or switch-casebox then fetch all
86 //it's pages recursively and setup into the widget.
87 myWidgetApi->toChildWidget();
89 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
90 QWidget* aPage = new QWidget(aWidget);
92 if (aWdgType == WDG_SWITCH) {
93 ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
94 aSwitch->addPage(aPage, aPageName);
95 } else if (aWdgType == WDG_TOOLBOX) {
96 QToolBox* aToolbox = qobject_cast<QToolBox*>(aWidget);
97 aToolbox->addItem(aPage, aPageName);
99 } while (myWidgetApi->toNextWidget());
101 } while (myWidgetApi->toNextWidget());
102 theParent->setLayout(aWidgetLay);
105 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
107 QWidget* result = new QWidget(theParent);
108 QVBoxLayout* aLabelLay = new QVBoxLayout(result);
109 QLabel* aLabel = new QLabel(result);
110 aLabel->setWordWrap(true);
111 aLabel->setText(qs(myWidgetApi->getProperty(INFO_WDG_TEXT)));
112 aLabel->setToolTip(qs(myWidgetApi->getProperty(INFO_WDG_TOOLTIP)));
113 aLabelLay->addWidget(aLabel);
114 aLabelLay->addStretch(1);
115 result->setLayout(aLabelLay);
119 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
122 QWidget* result = NULL;
123 if (theType == WDG_DOUBLEVALUE) {
124 result = doubleSpinBoxControl(theParent);
126 } else if (theType == WDG_INFO) {
127 result = labelControl(theParent);
129 } else if (theType == WDG_SHAPE_SELECTOR) {
130 result = shapeSelectorControl(theParent);
132 } else if (theType == WDG_BOOLVALUE) {
133 result = booleanControl(theParent);
135 } else if (theType == WDG_POINT_SELECTOR) {
136 result = pointSelectorControl(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_POINT2D_DISTANCE) {
148 result = point2dDistanceControl(theParent);
150 } else if (theType == WDG_FILE_SELECTOR) {
151 result = fileSelectorControl(theParent);
153 } else if (theType == WDG_CHOICE) {
154 result = choiceControl(theParent);
156 } else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
157 result = createContainer(theType, theParent);
159 result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi,
162 if (!result) {qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type");}
166 // register that this attribute in feature is not obligatory for the feature execution
167 // so, it is not needed for the standard validation mechanism
169 myWidgetApi ? myWidgetApi->getBooleanAttribute(FEATURE_OBLIGATORY, true) : true;
171 ModelAPI_Session::get()->validators()->registerNotObligatory(
172 myParentId, myWidgetApi->widgetId());
179 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
181 QWidget* result = NULL;
182 if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
183 QGroupBox* aGroupBox = new QGroupBox(theParent);
184 aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
186 } else if (theType == WDG_TOOLBOX) {
187 result = new QToolBox(theParent);
188 } else if (theType == WDG_SWITCH) {
189 result = new ModuleBase_WidgetSwitch(theParent);
190 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
194 else {qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type";}
199 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
201 ModuleBase_WidgetDoubleValue* aDblWgt =
202 new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi, myParentId);
203 myModelWidgets.append(aDblWgt);
204 return aDblWgt->getControl();
207 QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
209 ModuleBase_WidgetPoint2D* aWidget =
210 new ModuleBase_WidgetPoint2D(theParent, myWidgetApi,myParentId);
211 myModelWidgets.append(aWidget);
212 return aWidget->getControl();
215 QWidget* ModuleBase_WidgetFactory::featureSelectorControl(QWidget* theParent)
217 ModuleBase_WidgetFeature* aWidget =
218 new ModuleBase_WidgetFeature(theParent, myWidgetApi,myParentId);
219 myModelWidgets.append(aWidget);
220 return aWidget->getControl();
223 QWidget* ModuleBase_WidgetFactory::featureOrAttributeSelectorControl(QWidget* theParent)
225 ModuleBase_WidgetFeatureOrAttribute* aWidget =
226 new ModuleBase_WidgetFeatureOrAttribute(theParent, myWidgetApi, myParentId);
227 myModelWidgets.append(aWidget);
228 return aWidget->getControl();
231 QWidget* ModuleBase_WidgetFactory::doubleValueEditor(QWidget* theParent)
233 ModuleBase_WidgetEditor* aWidget =
234 new ModuleBase_WidgetEditor(theParent, myWidgetApi, myParentId);
235 myModelWidgets.append(aWidget);
236 return aWidget->getControl();
239 QWidget* ModuleBase_WidgetFactory::shapeSelectorControl(QWidget* theParent)
241 ModuleBase_WidgetShapeSelector* aSelector =
242 new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi, myParentId);
243 myModelWidgets.append(aSelector);
244 return aSelector->getControl();
247 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
249 ModuleBase_WidgetBoolValue* aBoolWgt =
250 new ModuleBase_WidgetBoolValue(theParent, myWidgetApi, myParentId);
251 myModelWidgets.append(aBoolWgt);
252 return aBoolWgt->getControl();
255 QWidget* ModuleBase_WidgetFactory::point2dDistanceControl(QWidget* theParent)
257 ModuleBase_WidgetPoint2dDistance* aDistWgt =
258 new ModuleBase_WidgetPoint2dDistance(theParent, myWidgetApi, myParentId);
259 myModelWidgets.append(aDistWgt);
260 return aDistWgt->getControl();
263 QWidget* ModuleBase_WidgetFactory::fileSelectorControl(QWidget* theParent)
265 ModuleBase_WidgetFileSelector* aFileSelectorWgt =
266 new ModuleBase_WidgetFileSelector(theParent, myWidgetApi, myParentId);
267 myModelWidgets.append(aFileSelectorWgt);
268 return aFileSelectorWgt->getControl();
271 QWidget* ModuleBase_WidgetFactory::choiceControl(QWidget* theParent)
273 ModuleBase_WidgetChoice* aChoiceWgt =
274 new ModuleBase_WidgetChoice(theParent, myWidgetApi,myParentId);
275 myModelWidgets.append(aChoiceWgt);
276 return aChoiceWgt->getControl();
279 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
281 return QString::fromStdString(theStdString);