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>
18 #include <Config_Keywords.h>
19 #include <Config_WidgetAPI.h>
22 #include <QHBoxLayout>
23 #include <QGridLayout>
25 #include <QMetaProperty>
38 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(ModuleBase_Operation* theOperation, ModuleBase_IWorkshop* theWorkshop)
39 : myOperation(theOperation), myWorkshop(theWorkshop)
41 QString aXml = myOperation->getDescription()->xmlRepresentation();
42 myWidgetApi = new Config_WidgetAPI(aXml.toStdString());
45 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
49 void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
51 if (!myWidgetApi->toChildWidget())
54 QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
55 aWidgetLay->setContentsMargins(2, 2, 2, 2);
56 do { //Iterate over each node
57 std::string aWdgType = myWidgetApi->widgetType();
58 //Create a widget (doublevalue, groupbox, toolbox, etc.
59 QWidget* aWidget = createWidgetByType(aWdgType, theParent);
61 aWidgetLay->addWidget(aWidget);
63 if (myWidgetApi->isContainerWidget()) {
64 //if current widget is groupbox (container) process it's children recursively
65 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
66 createWidget(aWidget);
67 QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
68 aGrBox->setTitle(aGroupName);
70 if (myWidgetApi->isPagedWidget()) {
71 //If current widget is toolbox or switch-casebox then fetch all
72 //it's pages recursively and setup into the widget.
73 myWidgetApi->toChildWidget();
75 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
76 QWidget* aPage = new QWidget(aWidget);
78 if (aWdgType == WDG_SWITCH) {
79 ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
80 aSwitch->addPage(aPage, aPageName);
81 } else if (aWdgType == WDG_TOOLBOX){
82 QToolBox* aToolbox = qobject_cast<QToolBox*>(aWidget);
83 aToolbox->addItem(aPage, aPageName);
85 } while(myWidgetApi->toNextWidget());
87 } while(myWidgetApi->toNextWidget());
88 theParent->setLayout(aWidgetLay);
91 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
93 QWidget* result = new QWidget(theParent);
94 QVBoxLayout* aLabelLay = new QVBoxLayout(result);
95 QLabel* aLabel = new QLabel(result);
96 aLabel->setText(qs(myWidgetApi->getProperty(INFO_WDG_TEXT)));
97 aLabel->setToolTip(qs(myWidgetApi->getProperty(INFO_WDG_TOOLTIP)));
98 aLabelLay->addWidget(aLabel);
99 aLabelLay->addStretch(1);
100 result->setLayout(aLabelLay);
104 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType, QWidget* theParent)
106 QWidget* result = NULL;
107 if (theType == WDG_DOUBLEVALUE) {
108 result = doubleSpinBoxControl(theParent);
110 } else if (theType == WDG_INFO) {
111 result = labelControl(theParent);
113 } else if (theType == WDG_SELECTOR) {
114 result = selectorControl(theParent);
116 } else if (theType == WDG_BOOLVALUE) {
117 result = booleanControl(theParent);
119 } else if (theType == WDG_POINT_SELECTOR) {
120 result = pointSelectorControl(theParent);
122 } else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
123 result = createContainer(theType, theParent);
126 else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad widget type"; }
131 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
133 QWidget* result = NULL;
134 if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
135 QGroupBox* aGroupBox = new QGroupBox(theParent);
136 aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
138 } else if (theType == WDG_TOOLBOX) {
139 result = new QToolBox(theParent);
140 } else if (theType == WDG_SWITCH) {
141 result = new ModuleBase_WidgetSwitch(theParent);
142 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
146 else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type"; }
151 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
153 ModuleBase_WidgetDoubleValue* aDblWgt = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi);
154 QObject::connect(aDblWgt, SIGNAL(valuesChanged()), myOperation, SLOT(storeCustomValue()));
156 myModelWidgets.append(aDblWgt);
158 return aDblWgt->getControl();
161 QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
163 ModuleBase_WidgetPoint2D* aWidget = new ModuleBase_WidgetPoint2D(theParent, myWidgetApi);
164 QObject::connect(aWidget, SIGNAL(valuesChanged()), myOperation, SLOT(storeCustomValue()));
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);
180 QObject::connect(aSelector, SIGNAL(valuesChanged()), myOperation, SLOT(storeCustomValue()));
182 myModelWidgets.append(aSelector);
183 return aSelector->getControl();
187 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
189 ModuleBase_WidgetBoolValue* aBoolWgt = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi);
190 QObject::connect(aBoolWgt, SIGNAL(valuesChanged()), myOperation, SLOT(storeCustomValue()));
192 myModelWidgets.append(aBoolWgt);
194 return aBoolWgt->getControl();