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_WidgetEditor.h>
15 #include <ModuleBase_WidgetSwitch.h>
16 #include <ModuleBase_WidgetSelector.h>
17 #include <ModuleBase_WidgetDoubleValue.h>
18 #include <ModuleBase_WidgetBoolValue.h>
19 #include <ModuleBase_WidgetPoint2dDistance.h>
21 #include <Config_Keywords.h>
22 #include <Config_WidgetAPI.h>
25 #include <QHBoxLayout>
26 #include <QGridLayout>
28 #include <QMetaProperty>
41 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,
42 ModuleBase_IWorkshop* theWorkshop)
43 : myWorkshop(theWorkshop)
45 myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
48 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
52 void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
54 if (!myWidgetApi->toChildWidget())
57 QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
58 aWidgetLay->setContentsMargins(2, 2, 2, 2);
59 do { //Iterate over each node
60 std::string aWdgType = myWidgetApi->widgetType();
61 //Create a widget (doublevalue, groupbox, toolbox, etc.
62 QWidget* aWidget = createWidgetByType(aWdgType, theParent);
64 if (!isInternalWidget(aWdgType)) {
65 aWidgetLay->addWidget(aWidget);
68 aWidget->setVisible(false);
70 if (myWidgetApi->isContainerWidget()) {
71 //if current widget is groupbox (container) process it's children recursively
72 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
73 createWidget(aWidget);
74 QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
75 aGrBox->setTitle(aGroupName);
77 if (myWidgetApi->isPagedWidget()) {
78 //If current widget is toolbox or switch-casebox then fetch all
79 //it's pages recursively and setup into the widget.
80 myWidgetApi->toChildWidget();
82 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
83 QWidget* aPage = new QWidget(aWidget);
85 if (aWdgType == WDG_SWITCH) {
86 ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
87 aSwitch->addPage(aPage, aPageName);
88 } else if (aWdgType == WDG_TOOLBOX){
89 QToolBox* aToolbox = qobject_cast<QToolBox*>(aWidget);
90 aToolbox->addItem(aPage, aPageName);
92 } while(myWidgetApi->toNextWidget());
94 } while(myWidgetApi->toNextWidget());
95 theParent->setLayout(aWidgetLay);
98 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
100 QWidget* result = new QWidget(theParent);
101 QVBoxLayout* aLabelLay = new QVBoxLayout(result);
102 QLabel* aLabel = new QLabel(result);
103 aLabel->setWordWrap(true);
104 aLabel->setText(qs(myWidgetApi->getProperty(INFO_WDG_TEXT)));
105 aLabel->setToolTip(qs(myWidgetApi->getProperty(INFO_WDG_TOOLTIP)));
106 aLabelLay->addWidget(aLabel);
107 aLabelLay->addStretch(1);
108 result->setLayout(aLabelLay);
112 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType, QWidget* theParent)
114 QWidget* result = NULL;
115 if (theType == WDG_DOUBLEVALUE) {
116 result = doubleSpinBoxControl(theParent);
118 } else if (theType == WDG_INFO) {
119 result = labelControl(theParent);
121 } else if (theType == WDG_SELECTOR) {
122 result = selectorControl(theParent);
124 } else if (theType == WDG_BOOLVALUE) {
125 result = booleanControl(theParent);
127 } else if (theType == WDG_POINT_SELECTOR) {
128 result = pointSelectorControl(theParent);
130 } else if (theType == WDG_FEATURE_SELECTOR) {
131 result = featureSelectorControl(theParent);
133 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
134 result = doubleValueEditor(theParent);
136 } else if (theType == WDG_POINT2D_DISTANCE) {
137 result = point2dDistanceControl(theParent);
140 else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
141 result = createContainer(theType, theParent);
144 else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad widget type"; }
149 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
151 QWidget* result = NULL;
152 if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
153 QGroupBox* aGroupBox = new QGroupBox(theParent);
154 aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
156 } else if (theType == WDG_TOOLBOX) {
157 result = new QToolBox(theParent);
158 } else if (theType == WDG_SWITCH) {
159 result = new ModuleBase_WidgetSwitch(theParent);
160 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
164 else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type"; }
169 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
171 ModuleBase_WidgetDoubleValue* aDblWgt = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi);
172 myModelWidgets.append(aDblWgt);
174 return aDblWgt->getControl();
177 QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
179 ModuleBase_WidgetPoint2D* aWidget = new ModuleBase_WidgetPoint2D(theParent, myWidgetApi);
180 myModelWidgets.append(aWidget);
181 return aWidget->getControl();
184 QWidget* ModuleBase_WidgetFactory::featureSelectorControl(QWidget* theParent)
186 ModuleBase_WidgetFeature* aWidget = new ModuleBase_WidgetFeature(theParent, myWidgetApi);
187 myModelWidgets.append(aWidget);
188 return aWidget->getControl();
191 QWidget* ModuleBase_WidgetFactory::doubleValueEditor(QWidget* theParent)
193 ModuleBase_WidgetEditor* aWidget = new ModuleBase_WidgetEditor(theParent, myWidgetApi);
194 myModelWidgets.append(aWidget);
198 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
200 return QString::fromStdString(theStdString);
203 bool ModuleBase_WidgetFactory::isInternalWidget(const std::string& theType)
205 std::string prop = myWidgetApi->getProperty(FEATURE_INTERNAL);
207 std::transform(prop.begin(), prop.end(), prop.begin(), ::tolower);
208 if(prop.empty() || prop == "false" || prop == "0") {
214 QWidget* ModuleBase_WidgetFactory::selectorControl(QWidget* theParent)
216 ModuleBase_WidgetSelector* aSelector = new ModuleBase_WidgetSelector(theParent, myWorkshop, myWidgetApi);
217 myModelWidgets.append(aSelector);
218 return aSelector->getControl();
222 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
224 ModuleBase_WidgetBoolValue* aBoolWgt = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi);
225 myModelWidgets.append(aBoolWgt);
227 return aBoolWgt->getControl();
231 QWidget* ModuleBase_WidgetFactory::point2dDistanceControl(QWidget* theParent)
233 ModuleBase_WidgetPoint2dDistance* aDistWgt = new ModuleBase_WidgetPoint2dDistance(theParent, myWidgetApi);
234 myModelWidgets.append(aDistWgt);
236 return aDistWgt->getControl();