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>
18 #include <ModuleBase_WidgetPoint2dDistance.h>
20 #include <Config_Keywords.h>
21 #include <Config_WidgetAPI.h>
24 #include <QHBoxLayout>
25 #include <QGridLayout>
27 #include <QMetaProperty>
40 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,
41 ModuleBase_IWorkshop* theWorkshop)
42 : myWorkshop(theWorkshop)
44 myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
47 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
51 void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
53 if (!myWidgetApi->toChildWidget())
56 QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
57 aWidgetLay->setContentsMargins(2, 2, 2, 2);
58 do { //Iterate over each node
59 std::string aWdgType = myWidgetApi->widgetType();
60 //Create a widget (doublevalue, groupbox, toolbox, etc.
61 QWidget* aWidget = createWidgetByType(aWdgType, theParent);
63 aWidgetLay->addWidget(aWidget);
65 if (myWidgetApi->isContainerWidget()) {
66 //if current widget is groupbox (container) process it's children recursively
67 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
68 createWidget(aWidget);
69 QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
70 aGrBox->setTitle(aGroupName);
72 if (myWidgetApi->isPagedWidget()) {
73 //If current widget is toolbox or switch-casebox then fetch all
74 //it's pages recursively and setup into the widget.
75 myWidgetApi->toChildWidget();
77 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
78 QWidget* aPage = new QWidget(aWidget);
80 if (aWdgType == WDG_SWITCH) {
81 ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
82 aSwitch->addPage(aPage, aPageName);
83 } else if (aWdgType == WDG_TOOLBOX){
84 QToolBox* aToolbox = qobject_cast<QToolBox*>(aWidget);
85 aToolbox->addItem(aPage, aPageName);
87 } while(myWidgetApi->toNextWidget());
89 } while(myWidgetApi->toNextWidget());
90 theParent->setLayout(aWidgetLay);
93 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
95 QWidget* result = new QWidget(theParent);
96 QVBoxLayout* aLabelLay = new QVBoxLayout(result);
97 QLabel* aLabel = new QLabel(result);
98 aLabel->setText(qs(myWidgetApi->getProperty(INFO_WDG_TEXT)));
99 aLabel->setToolTip(qs(myWidgetApi->getProperty(INFO_WDG_TOOLTIP)));
100 aLabelLay->addWidget(aLabel);
101 aLabelLay->addStretch(1);
102 result->setLayout(aLabelLay);
106 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType, QWidget* theParent)
108 QWidget* result = NULL;
109 if (theType == WDG_DOUBLEVALUE) {
110 result = doubleSpinBoxControl(theParent);
112 } else if (theType == WDG_INFO) {
113 result = labelControl(theParent);
115 } else if (theType == WDG_SELECTOR) {
116 result = selectorControl(theParent);
118 } else if (theType == WDG_BOOLVALUE) {
119 result = booleanControl(theParent);
121 } else if (theType == WDG_POINT_SELECTOR) {
122 result = pointSelectorControl(theParent);
124 } else if (theType == WDG_FEATURE_SELECTOR) {
125 result = featureSelectorControl(theParent);
127 } else if (theType == WDG_POINT2D_DISTANCE) {
128 result = point2dDistanceControl(theParent);
131 else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
132 result = createContainer(theType, theParent);
135 else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad widget type"; }
140 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
142 QWidget* result = NULL;
143 if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
144 QGroupBox* aGroupBox = new QGroupBox(theParent);
145 aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
147 } else if (theType == WDG_TOOLBOX) {
148 result = new QToolBox(theParent);
149 } else if (theType == WDG_SWITCH) {
150 result = new ModuleBase_WidgetSwitch(theParent);
151 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
155 else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type"; }
160 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
162 ModuleBase_WidgetDoubleValue* aDblWgt = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi);
163 myModelWidgets.append(aDblWgt);
165 return aDblWgt->getControl();
168 QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
170 ModuleBase_WidgetPoint2D* aWidget = new ModuleBase_WidgetPoint2D(theParent, myWidgetApi);
171 myModelWidgets.append(aWidget);
172 return aWidget->getControl();
175 QWidget* ModuleBase_WidgetFactory::featureSelectorControl(QWidget* theParent)
177 ModuleBase_WidgetFeature* aWidget = new ModuleBase_WidgetFeature(theParent, myWidgetApi);
178 myModelWidgets.append(aWidget);
179 return aWidget->getControl();
182 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
184 return QString::fromStdString(theStdString);
188 QWidget* ModuleBase_WidgetFactory::selectorControl(QWidget* theParent)
190 ModuleBase_WidgetSelector* aSelector = new ModuleBase_WidgetSelector(theParent, myWorkshop, myWidgetApi);
191 myModelWidgets.append(aSelector);
192 return aSelector->getControl();
196 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
198 ModuleBase_WidgetBoolValue* aBoolWgt = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi);
199 myModelWidgets.append(aBoolWgt);
201 return aBoolWgt->getControl();
205 QWidget* ModuleBase_WidgetFactory::point2dDistanceControl(QWidget* theParent)
207 ModuleBase_WidgetPoint2dDistance* aDistWgt = new ModuleBase_WidgetPoint2dDistance(theParent, myWidgetApi);
208 myModelWidgets.append(aDistWgt);
210 return aDistWgt->getControl();