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_WidgetSwitch.h>
14 #include <ModuleBase_WidgetSelector.h>
15 #include <ModuleBase_WidgetDoubleValue.h>
16 #include <ModuleBase_WidgetBoolValue.h>
17 #include <ModuleBase_WidgetPoint2dDistance.h>
19 #include <Config_Keywords.h>
20 #include <Config_WidgetAPI.h>
23 #include <QHBoxLayout>
24 #include <QGridLayout>
26 #include <QMetaProperty>
39 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,
40 ModuleBase_IWorkshop* theWorkshop)
41 : myWorkshop(theWorkshop)
43 myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
46 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
50 void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
52 if (!myWidgetApi->toChildWidget())
55 QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
56 aWidgetLay->setContentsMargins(2, 2, 2, 2);
57 do { //Iterate over each node
58 std::string aWdgType = myWidgetApi->widgetType();
59 //Create a widget (doublevalue, groupbox, toolbox, etc.
60 QWidget* aWidget = createWidgetByType(aWdgType, theParent);
62 aWidgetLay->addWidget(aWidget);
64 if (myWidgetApi->isContainerWidget()) {
65 //if current widget is groupbox (container) process it's children recursively
66 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
67 createWidget(aWidget);
68 QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
69 aGrBox->setTitle(aGroupName);
71 if (myWidgetApi->isPagedWidget()) {
72 //If current widget is toolbox or switch-casebox then fetch all
73 //it's pages recursively and setup into the widget.
74 myWidgetApi->toChildWidget();
76 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
77 QWidget* aPage = new QWidget(aWidget);
79 if (aWdgType == WDG_SWITCH) {
80 ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
81 aSwitch->addPage(aPage, aPageName);
82 } else if (aWdgType == WDG_TOOLBOX){
83 QToolBox* aToolbox = qobject_cast<QToolBox*>(aWidget);
84 aToolbox->addItem(aPage, aPageName);
86 } while(myWidgetApi->toNextWidget());
88 } while(myWidgetApi->toNextWidget());
89 theParent->setLayout(aWidgetLay);
92 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
94 QWidget* result = new QWidget(theParent);
95 QVBoxLayout* aLabelLay = new QVBoxLayout(result);
96 QLabel* aLabel = new QLabel(result);
97 aLabel->setText(qs(myWidgetApi->getProperty(INFO_WDG_TEXT)));
98 aLabel->setToolTip(qs(myWidgetApi->getProperty(INFO_WDG_TOOLTIP)));
99 aLabelLay->addWidget(aLabel);
100 aLabelLay->addStretch(1);
101 result->setLayout(aLabelLay);
105 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType, QWidget* theParent)
107 QWidget* result = NULL;
108 if (theType == WDG_DOUBLEVALUE) {
109 result = doubleSpinBoxControl(theParent);
111 } else if (theType == WDG_INFO) {
112 result = labelControl(theParent);
114 } else if (theType == WDG_SELECTOR) {
115 result = selectorControl(theParent);
117 } else if (theType == WDG_BOOLVALUE) {
118 result = booleanControl(theParent);
120 } else if (theType == WDG_POINT_SELECTOR) {
121 result = pointSelectorControl(theParent);
123 } else if (theType == WDG_POINT2D_DISTANCE) {
124 result = point2dDistanceControl(theParent);
126 } else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
127 result = createContainer(theType, theParent);
130 else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad widget type"; }
135 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
137 QWidget* result = NULL;
138 if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
139 QGroupBox* aGroupBox = new QGroupBox(theParent);
140 aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
142 } else if (theType == WDG_TOOLBOX) {
143 result = new QToolBox(theParent);
144 } else if (theType == WDG_SWITCH) {
145 result = new ModuleBase_WidgetSwitch(theParent);
146 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
150 else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type"; }
155 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
157 ModuleBase_WidgetDoubleValue* aDblWgt = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi);
158 myModelWidgets.append(aDblWgt);
160 return aDblWgt->getControl();
163 QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
165 ModuleBase_WidgetPoint2D* aWidget = new ModuleBase_WidgetPoint2D(theParent, myWidgetApi);
166 myModelWidgets.append(aWidget);
167 return aWidget->getControl();
170 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
172 return QString::fromStdString(theStdString);
176 QWidget* ModuleBase_WidgetFactory::selectorControl(QWidget* theParent)
178 ModuleBase_WidgetSelector* aSelector = new ModuleBase_WidgetSelector(theParent, myWorkshop, myWidgetApi);
179 myModelWidgets.append(aSelector);
180 return aSelector->getControl();
184 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
186 ModuleBase_WidgetBoolValue* aBoolWgt = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi);
187 myModelWidgets.append(aBoolWgt);
189 return aBoolWgt->getControl();
193 QWidget* ModuleBase_WidgetFactory::point2dDistanceControl(QWidget* theParent)
195 ModuleBase_WidgetPoint2dDistance* aDistWgt = new ModuleBase_WidgetPoint2dDistance(theParent, myWidgetApi);
196 myModelWidgets.append(aDistWgt);
198 return aDistWgt->getControl();