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_WidgetFeatureOrAttribute.h>
14 #include <ModuleBase_WidgetFeature.h>
15 #include <ModuleBase_WidgetEditor.h>
16 #include <ModuleBase_WidgetSwitch.h>
17 #include <ModuleBase_WidgetSelector.h>
18 #include <ModuleBase_WidgetDoubleValue.h>
19 #include <ModuleBase_WidgetBoolValue.h>
20 #include <ModuleBase_WidgetPoint2dDistance.h>
21 #include <ModuleBase_WidgetFileSelector.h>
22 #include <ModuleBase_IWorkshop.h>
23 #include <ModuleBase_IModule.h>
25 #include <Config_Keywords.h>
26 #include <Config_WidgetAPI.h>
29 #include <QHBoxLayout>
30 #include <QGridLayout>
32 #include <QMetaProperty>
45 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,
46 ModuleBase_IWorkshop* theWorkshop)
47 : myWorkshop(theWorkshop)
49 myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
52 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
56 void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
58 myParentId = myWidgetApi->widgetId();
59 if (!myWidgetApi->toChildWidget())
62 QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
63 aWidgetLay->setContentsMargins(2, 2, 2, 2);
64 do { //Iterate over each node
65 std::string aWdgType = myWidgetApi->widgetType();
66 //Create a widget (doublevalue, groupbox, toolbox, etc.
67 QWidget* aWidget = createWidgetByType(aWdgType, theParent);
69 if (!isInternalWidget(aWdgType)) {
70 aWidgetLay->addWidget(aWidget);
72 aWidget->setVisible(false);
75 if (myWidgetApi->isContainerWidget()) {
76 //if current widget is groupbox (container) process it's children recursively
77 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
78 createWidget(aWidget);
79 QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
80 aGrBox->setTitle(aGroupName);
82 if (myWidgetApi->isPagedWidget()) {
83 //If current widget is toolbox or switch-casebox then fetch all
84 //it's pages recursively and setup into the widget.
85 myWidgetApi->toChildWidget();
87 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
88 QWidget* aPage = new QWidget(aWidget);
90 if (aWdgType == WDG_SWITCH) {
91 ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
92 aSwitch->addPage(aPage, aPageName);
93 } else if (aWdgType == WDG_TOOLBOX) {
94 QToolBox* aToolbox = qobject_cast<QToolBox*>(aWidget);
95 aToolbox->addItem(aPage, aPageName);
97 } while (myWidgetApi->toNextWidget());
99 } while (myWidgetApi->toNextWidget());
100 theParent->setLayout(aWidgetLay);
103 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
105 QWidget* result = new QWidget(theParent);
106 QVBoxLayout* aLabelLay = new QVBoxLayout(result);
107 QLabel* aLabel = new QLabel(result);
108 aLabel->setWordWrap(true);
109 aLabel->setText(qs(myWidgetApi->getProperty(INFO_WDG_TEXT)));
110 aLabel->setToolTip(qs(myWidgetApi->getProperty(INFO_WDG_TOOLTIP)));
111 aLabelLay->addWidget(aLabel);
112 aLabelLay->addStretch(1);
113 result->setLayout(aLabelLay);
117 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
120 QWidget* result = NULL;
121 if (theType == WDG_DOUBLEVALUE) {
122 result = doubleSpinBoxControl(theParent);
124 } else if (theType == WDG_INFO) {
125 result = labelControl(theParent);
127 } else if (theType == WDG_SELECTOR) {
128 result = selectorControl(theParent);
130 } else if (theType == WDG_BOOLVALUE) {
131 result = booleanControl(theParent);
133 } else if (theType == WDG_POINT_SELECTOR) {
134 result = pointSelectorControl(theParent);
136 } else if (theType == WDG_FEATURE_SELECTOR) {
137 result = featureSelectorControl(theParent);
139 } else if (theType == WDG_FEATURE_OR_ATTRIBUTE_SELECTOR) {
140 result = featureOrAttributeSelectorControl(theParent);
142 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
143 result = doubleValueEditor(theParent);
145 } else if (theType == WDG_POINT2D_DISTANCE) {
146 result = point2dDistanceControl(theParent);
147 } else if (theType == WDG_FILE_SELECTOR) {
148 result = fileSelectorControl(theParent);
149 } else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
150 result = createContainer(theType, theParent);
152 result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi,
155 if (!result) {qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type");}
161 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
163 QWidget* result = NULL;
164 if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
165 QGroupBox* aGroupBox = new QGroupBox(theParent);
166 aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
168 } else if (theType == WDG_TOOLBOX) {
169 result = new QToolBox(theParent);
170 } else if (theType == WDG_SWITCH) {
171 result = new ModuleBase_WidgetSwitch(theParent);
172 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
176 else {qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type";}
181 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
183 ModuleBase_WidgetDoubleValue* aDblWgt = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi,
185 myModelWidgets.append(aDblWgt);
187 return aDblWgt->getControl();
190 QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
192 ModuleBase_WidgetPoint2D* aWidget = new ModuleBase_WidgetPoint2D(theParent, myWidgetApi,
194 myModelWidgets.append(aWidget);
195 return aWidget->getControl();
198 QWidget* ModuleBase_WidgetFactory::featureSelectorControl(QWidget* theParent)
200 ModuleBase_WidgetFeature* aWidget = new ModuleBase_WidgetFeature(theParent, myWidgetApi,
202 myModelWidgets.append(aWidget);
203 return aWidget->getControl();
206 QWidget* ModuleBase_WidgetFactory::featureOrAttributeSelectorControl(QWidget* theParent)
208 ModuleBase_WidgetFeatureOrAttribute* aWidget = new ModuleBase_WidgetFeatureOrAttribute(
209 theParent, myWidgetApi, myParentId);
210 myModelWidgets.append(aWidget);
211 return aWidget->getControl();
214 QWidget* ModuleBase_WidgetFactory::doubleValueEditor(QWidget* theParent)
216 ModuleBase_WidgetEditor* aWidget = new ModuleBase_WidgetEditor(theParent, myWidgetApi,
218 myModelWidgets.append(aWidget);
219 return aWidget->getControl();
222 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
224 return QString::fromStdString(theStdString);
227 bool ModuleBase_WidgetFactory::isInternalWidget(const std::string& theType)
229 std::string prop = myWidgetApi->getProperty(FEATURE_INTERNAL);
231 std::transform(prop.begin(), prop.end(), prop.begin(), ::tolower);
232 if (prop.empty() || prop == "false" || prop == "0") {
238 QWidget* ModuleBase_WidgetFactory::selectorControl(QWidget* theParent)
240 ModuleBase_WidgetSelector* aSelector = new ModuleBase_WidgetSelector(theParent, myWorkshop,
241 myWidgetApi, myParentId);
242 myModelWidgets.append(aSelector);
243 return aSelector->getControl();
246 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
248 ModuleBase_WidgetBoolValue* aBoolWgt = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi,
250 myModelWidgets.append(aBoolWgt);
252 return aBoolWgt->getControl();
255 QWidget* ModuleBase_WidgetFactory::point2dDistanceControl(QWidget* theParent)
257 ModuleBase_WidgetPoint2dDistance* aDistWgt = new ModuleBase_WidgetPoint2dDistance(theParent,
260 myModelWidgets.append(aDistWgt);
262 return aDistWgt->getControl();
265 QWidget* ModuleBase_WidgetFactory::fileSelectorControl(QWidget* theParent)
267 ModuleBase_WidgetFileSelector* aFileSelectorWgt =
268 new ModuleBase_WidgetFileSelector(theParent, myWidgetApi, myParentId);
269 myModelWidgets.append(aFileSelectorWgt);
271 return aFileSelectorWgt->getControl();