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_WidgetFeature.h>
14 #include <ModuleBase_WidgetSwitch.h>
15 #include <ModuleBase_WidgetSelector.h>
16 #include <ModuleBase_WidgetDoubleValue.h>
17 #include <ModuleBase_WidgetBoolValue.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_FEATURE_SELECTOR) {
124 result = featureSelectorControl(theParent);
127 else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
128 result = createContainer(theType, theParent);
131 else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad widget type"; }
136 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
138 QWidget* result = NULL;
139 if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
140 QGroupBox* aGroupBox = new QGroupBox(theParent);
141 aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
143 } else if (theType == WDG_TOOLBOX) {
144 result = new QToolBox(theParent);
145 } else if (theType == WDG_SWITCH) {
146 result = new ModuleBase_WidgetSwitch(theParent);
147 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
151 else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type"; }
156 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
158 ModuleBase_WidgetDoubleValue* aDblWgt = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi);
159 myModelWidgets.append(aDblWgt);
161 return aDblWgt->getControl();
164 QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
166 ModuleBase_WidgetPoint2D* aWidget = new ModuleBase_WidgetPoint2D(theParent, myWidgetApi);
167 myModelWidgets.append(aWidget);
168 return aWidget->getControl();
171 QWidget* ModuleBase_WidgetFactory::featureSelectorControl(QWidget* theParent)
173 ModuleBase_WidgetFeature* aWidget = new ModuleBase_WidgetFeature(theParent, myWidgetApi);
174 myModelWidgets.append(aWidget);
175 return aWidget->getControl();
178 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
180 return QString::fromStdString(theStdString);
184 QWidget* ModuleBase_WidgetFactory::selectorControl(QWidget* theParent)
186 ModuleBase_WidgetSelector* aSelector = new ModuleBase_WidgetSelector(theParent, myWorkshop, myWidgetApi);
187 myModelWidgets.append(aSelector);
188 return aSelector->getControl();
192 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
194 ModuleBase_WidgetBoolValue* aBoolWgt = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi);
195 myModelWidgets.append(aBoolWgt);
197 return aBoolWgt->getControl();