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_WidgetSelector.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>
26 #include <Config_Keywords.h>
27 #include <Config_WidgetAPI.h>
30 #include <QHBoxLayout>
31 #include <QGridLayout>
33 #include <QMetaProperty>
46 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,
47 ModuleBase_IWorkshop* theWorkshop)
48 : myWorkshop(theWorkshop)
50 myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
53 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
57 void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
59 myParentId = myWidgetApi->widgetId();
60 if (!myWidgetApi->toChildWidget())
63 QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
64 aWidgetLay->setContentsMargins(2, 2, 2, 2);
65 do { //Iterate over each node
66 std::string aWdgType = myWidgetApi->widgetType();
67 //Create a widget (doublevalue, groupbox, toolbox, etc.
68 QWidget* aWidget = createWidgetByType(aWdgType, theParent);
70 if (!isInternalWidget(aWdgType)) {
71 aWidgetLay->addWidget(aWidget);
73 aWidget->setVisible(false);
76 if (myWidgetApi->isContainerWidget()) {
77 //if current widget is groupbox (container) process it's children recursively
78 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
79 createWidget(aWidget);
80 QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
81 aGrBox->setTitle(aGroupName);
83 if (myWidgetApi->isPagedWidget()) {
84 //If current widget is toolbox or switch-casebox then fetch all
85 //it's pages recursively and setup into the widget.
86 myWidgetApi->toChildWidget();
88 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
89 QWidget* aPage = new QWidget(aWidget);
91 if (aWdgType == WDG_SWITCH) {
92 ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
93 aSwitch->addPage(aPage, aPageName);
94 } else if (aWdgType == WDG_TOOLBOX) {
95 QToolBox* aToolbox = qobject_cast<QToolBox*>(aWidget);
96 aToolbox->addItem(aPage, aPageName);
98 } while (myWidgetApi->toNextWidget());
100 } while (myWidgetApi->toNextWidget());
101 theParent->setLayout(aWidgetLay);
104 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
106 QWidget* result = new QWidget(theParent);
107 QVBoxLayout* aLabelLay = new QVBoxLayout(result);
108 QLabel* aLabel = new QLabel(result);
109 aLabel->setWordWrap(true);
110 aLabel->setText(qs(myWidgetApi->getProperty(INFO_WDG_TEXT)));
111 aLabel->setToolTip(qs(myWidgetApi->getProperty(INFO_WDG_TOOLTIP)));
112 aLabelLay->addWidget(aLabel);
113 aLabelLay->addStretch(1);
114 result->setLayout(aLabelLay);
118 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
121 QWidget* result = NULL;
122 if (theType == WDG_DOUBLEVALUE) {
123 result = doubleSpinBoxControl(theParent);
125 } else if (theType == WDG_INFO) {
126 result = labelControl(theParent);
128 } else if (theType == WDG_SELECTOR) {
129 result = selectorControl(theParent);
131 } else if (theType == WDG_BOOLVALUE) {
132 result = booleanControl(theParent);
134 } else if (theType == WDG_POINT_SELECTOR) {
135 result = pointSelectorControl(theParent);
137 } else if (theType == WDG_FEATURE_SELECTOR) {
138 result = featureSelectorControl(theParent);
140 } else if (theType == WDG_FEATURE_OR_ATTRIBUTE_SELECTOR) {
141 result = featureOrAttributeSelectorControl(theParent);
143 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
144 result = doubleValueEditor(theParent);
146 } else if (theType == WDG_POINT2D_DISTANCE) {
147 result = point2dDistanceControl(theParent);
149 } else if (theType == WDG_FILE_SELECTOR) {
150 result = fileSelectorControl(theParent);
152 } else if (theType == WDG_CHOICE) {
153 result = choiceControl(theParent);
155 } else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
156 result = createContainer(theType, theParent);
158 result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi,
161 if (!result) {qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type");}
167 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
169 QWidget* result = NULL;
170 if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
171 QGroupBox* aGroupBox = new QGroupBox(theParent);
172 aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
174 } else if (theType == WDG_TOOLBOX) {
175 result = new QToolBox(theParent);
176 } else if (theType == WDG_SWITCH) {
177 result = new ModuleBase_WidgetSwitch(theParent);
178 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
182 else {qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type";}
187 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
189 ModuleBase_WidgetDoubleValue* aDblWgt = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi,
191 myModelWidgets.append(aDblWgt);
193 return aDblWgt->getControl();
196 QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
198 ModuleBase_WidgetPoint2D* aWidget = new ModuleBase_WidgetPoint2D(theParent, myWidgetApi,
200 myModelWidgets.append(aWidget);
201 return aWidget->getControl();
204 QWidget* ModuleBase_WidgetFactory::featureSelectorControl(QWidget* theParent)
206 ModuleBase_WidgetFeature* aWidget = new ModuleBase_WidgetFeature(theParent, myWidgetApi,
208 myModelWidgets.append(aWidget);
209 return aWidget->getControl();
212 QWidget* ModuleBase_WidgetFactory::featureOrAttributeSelectorControl(QWidget* theParent)
214 ModuleBase_WidgetFeatureOrAttribute* aWidget = new ModuleBase_WidgetFeatureOrAttribute(
215 theParent, myWidgetApi, myParentId);
216 myModelWidgets.append(aWidget);
217 return aWidget->getControl();
220 QWidget* ModuleBase_WidgetFactory::doubleValueEditor(QWidget* theParent)
222 ModuleBase_WidgetEditor* aWidget = new ModuleBase_WidgetEditor(theParent, myWidgetApi,
224 myModelWidgets.append(aWidget);
225 return aWidget->getControl();
228 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
230 return QString::fromStdString(theStdString);
233 bool ModuleBase_WidgetFactory::isInternalWidget(const std::string& theType)
235 std::string prop = myWidgetApi->getProperty(FEATURE_INTERNAL);
237 std::transform(prop.begin(), prop.end(), prop.begin(), ::tolower);
238 if (prop.empty() || prop == "false" || prop == "0") {
244 QWidget* ModuleBase_WidgetFactory::selectorControl(QWidget* theParent)
246 ModuleBase_WidgetSelector* aSelector = new ModuleBase_WidgetSelector(theParent, myWorkshop,
247 myWidgetApi, myParentId);
248 myModelWidgets.append(aSelector);
249 return aSelector->getControl();
252 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
254 ModuleBase_WidgetBoolValue* aBoolWgt = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi,
256 myModelWidgets.append(aBoolWgt);
258 return aBoolWgt->getControl();
261 QWidget* ModuleBase_WidgetFactory::point2dDistanceControl(QWidget* theParent)
263 ModuleBase_WidgetPoint2dDistance* aDistWgt = new ModuleBase_WidgetPoint2dDistance(theParent,
266 myModelWidgets.append(aDistWgt);
268 return aDistWgt->getControl();
271 QWidget* ModuleBase_WidgetFactory::fileSelectorControl(QWidget* theParent)
273 ModuleBase_WidgetFileSelector* aFileSelectorWgt =
274 new ModuleBase_WidgetFileSelector(theParent, myWidgetApi, myParentId);
275 myModelWidgets.append(aFileSelectorWgt);
277 return aFileSelectorWgt->getControl();
280 QWidget* ModuleBase_WidgetFactory::choiceControl(QWidget* theParent)
282 ModuleBase_WidgetChoice* aChoiceWgt = new ModuleBase_WidgetChoice(theParent, myWidgetApi,
284 myModelWidgets.append(aChoiceWgt);
286 return aChoiceWgt->getControl();