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>
27 #include <ModelAPI_Validator.h>
29 #include <Config_Keywords.h>
30 #include <Config_WidgetAPI.h>
33 #include <QHBoxLayout>
34 #include <QGridLayout>
36 #include <QMetaProperty>
49 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,
50 ModuleBase_IWorkshop* theWorkshop)
51 : myWorkshop(theWorkshop)
53 myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
56 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
60 void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
62 myParentId = myWidgetApi->widgetId();
63 if (!myWidgetApi->toChildWidget())
66 QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
67 do { //Iterate over each node
68 std::string aWdgType = myWidgetApi->widgetType();
69 //Create a widget (doublevalue, groupbox, toolbox, etc.
70 QWidget* aWidget = createWidgetByType(aWdgType, theParent);
72 if (!myWidgetApi->getBooleanAttribute(FEATURE_INTERNAL, false)) {
73 aWidgetLay->addWidget(aWidget);
75 aWidget->setVisible(false);
78 if (myWidgetApi->isContainerWidget()) {
79 //if current widget is groupbox (container) process it's children recursively
80 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
81 createWidget(aWidget);
82 ModuleBase_Tools::adjustMargins(aWidget);
83 QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
84 aGrBox->setTitle(aGroupName);
86 if (myWidgetApi->isPagedWidget()) {
87 //If current widget is toolbox or switch-casebox then fetch all
88 //it's pages recursively and setup into the widget.
89 myWidgetApi->toChildWidget();
91 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
92 QWidget* aPage = new QWidget(aWidget);
93 ModuleBase_Tools::adjustMargins(aPage);
95 if (aWdgType == WDG_SWITCH) {
96 ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
97 aSwitch->addPage(aPage, aPageName);
98 } else if (aWdgType == WDG_TOOLBOX) {
99 QToolBox* aToolbox = qobject_cast<QToolBox*>(aWidget);
100 aToolbox->addItem(aPage, aPageName);
102 } while (myWidgetApi->toNextWidget());
104 } while (myWidgetApi->toNextWidget());
105 theParent->setLayout(aWidgetLay);
108 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
110 QWidget* result = new QWidget(theParent);
111 QVBoxLayout* aLabelLay = new QVBoxLayout(result);
112 QLabel* aLabel = new QLabel(result);
113 aLabel->setWordWrap(true);
114 aLabel->setText(qs(myWidgetApi->getProperty(INFO_WDG_TEXT)));
115 aLabel->setToolTip(qs(myWidgetApi->getProperty(INFO_WDG_TOOLTIP)));
116 aLabelLay->addWidget(aLabel);
117 aLabelLay->addStretch(1);
118 result->setLayout(aLabelLay);
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_POINT_SELECTOR) {
139 result = pointSelectorControl(theParent);
141 } else if (theType == WDG_FEATURE_SELECTOR) {
142 result = featureSelectorControl(theParent);
144 } else if (theType == WDG_FEATURE_OR_ATTRIBUTE_SELECTOR) {
145 result = featureOrAttributeSelectorControl(theParent);
147 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
148 result = doubleValueEditor(theParent);
150 } else if (theType == WDG_POINT2D_DISTANCE) {
151 result = point2dDistanceControl(theParent);
153 } else if (theType == WDG_FILE_SELECTOR) {
154 result = fileSelectorControl(theParent);
156 } else if (theType == WDG_CHOICE) {
157 result = choiceControl(theParent);
159 } else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
160 result = createContainer(theType, theParent);
162 result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi,
165 if (!result) {qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type");}
169 // register that this attribute in feature is not obligatory for the feature execution
170 // so, it is not needed for the standard validation mechanism
172 myWidgetApi ? myWidgetApi->getBooleanAttribute(FEATURE_OBLIGATORY, true) : true;
174 ModelAPI_Session::get()->validators()->registerNotObligatory(
175 myParentId, myWidgetApi->widgetId());
182 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
184 QWidget* result = NULL;
185 if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
186 QGroupBox* aGroupBox = new QGroupBox(theParent);
187 aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
189 } else if (theType == WDG_TOOLBOX) {
190 result = new QToolBox(theParent);
191 } else if (theType == WDG_SWITCH) {
192 result = new ModuleBase_WidgetSwitch(theParent);
193 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
197 else {qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type";}
202 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
204 ModuleBase_WidgetDoubleValue* aDblWgt =
205 new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi, myParentId);
206 myModelWidgets.append(aDblWgt);
207 return aDblWgt->getControl();
210 QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
212 ModuleBase_WidgetPoint2D* aWidget =
213 new ModuleBase_WidgetPoint2D(theParent, myWidgetApi,myParentId);
214 myModelWidgets.append(aWidget);
215 return aWidget->getControl();
218 QWidget* ModuleBase_WidgetFactory::featureSelectorControl(QWidget* theParent)
220 ModuleBase_WidgetFeature* aWidget =
221 new ModuleBase_WidgetFeature(theParent, myWidgetApi,myParentId);
222 myModelWidgets.append(aWidget);
223 return aWidget->getControl();
226 QWidget* ModuleBase_WidgetFactory::featureOrAttributeSelectorControl(QWidget* theParent)
228 ModuleBase_WidgetFeatureOrAttribute* aWidget =
229 new ModuleBase_WidgetFeatureOrAttribute(theParent, myWidgetApi, myParentId);
230 myModelWidgets.append(aWidget);
231 return aWidget->getControl();
234 QWidget* ModuleBase_WidgetFactory::doubleValueEditor(QWidget* theParent)
236 ModuleBase_WidgetEditor* aWidget =
237 new ModuleBase_WidgetEditor(theParent, myWidgetApi, myParentId);
238 myModelWidgets.append(aWidget);
239 return aWidget->getControl();
242 QWidget* ModuleBase_WidgetFactory::shapeSelectorControl(QWidget* theParent)
244 ModuleBase_WidgetShapeSelector* aSelector =
245 new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi, myParentId);
246 myModelWidgets.append(aSelector);
247 return aSelector->getControl();
250 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
252 ModuleBase_WidgetBoolValue* aBoolWgt =
253 new ModuleBase_WidgetBoolValue(theParent, myWidgetApi, myParentId);
254 myModelWidgets.append(aBoolWgt);
255 return aBoolWgt->getControl();
258 QWidget* ModuleBase_WidgetFactory::point2dDistanceControl(QWidget* theParent)
260 ModuleBase_WidgetPoint2dDistance* aDistWgt =
261 new ModuleBase_WidgetPoint2dDistance(theParent, myWidgetApi, myParentId);
262 myModelWidgets.append(aDistWgt);
263 return aDistWgt->getControl();
266 QWidget* ModuleBase_WidgetFactory::fileSelectorControl(QWidget* theParent)
268 ModuleBase_WidgetFileSelector* aFileSelectorWgt =
269 new ModuleBase_WidgetFileSelector(theParent, myWidgetApi, myParentId);
270 myModelWidgets.append(aFileSelectorWgt);
271 return aFileSelectorWgt->getControl();
274 QWidget* ModuleBase_WidgetFactory::choiceControl(QWidget* theParent)
276 ModuleBase_WidgetChoice* aChoiceWgt =
277 new ModuleBase_WidgetChoice(theParent, myWidgetApi,myParentId);
278 myModelWidgets.append(aChoiceWgt);
279 return aChoiceWgt->getControl();
282 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
284 return QString::fromStdString(theStdString);