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_IWorkshop.h>
22 #include <ModuleBase_IModule.h>
24 #include <Config_Keywords.h>
25 #include <Config_WidgetAPI.h>
28 #include <QHBoxLayout>
29 #include <QGridLayout>
31 #include <QMetaProperty>
44 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,
45 ModuleBase_IWorkshop* theWorkshop)
46 : myWorkshop(theWorkshop)
48 myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
51 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
55 void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
57 myParentId = myWidgetApi->widgetId();
58 if (!myWidgetApi->toChildWidget())
61 QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
62 aWidgetLay->setContentsMargins(2, 2, 2, 2);
63 do { //Iterate over each node
64 std::string aWdgType = myWidgetApi->widgetType();
65 //Create a widget (doublevalue, groupbox, toolbox, etc.
66 QWidget* aWidget = createWidgetByType(aWdgType, theParent);
68 if (!isInternalWidget(aWdgType)) {
69 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, QWidget* theParent)
119 QWidget* result = NULL;
120 if (theType == WDG_DOUBLEVALUE) {
121 result = doubleSpinBoxControl(theParent);
123 } else if (theType == WDG_INFO) {
124 result = labelControl(theParent);
126 } else if (theType == WDG_SELECTOR) {
127 result = selectorControl(theParent);
129 } else if (theType == WDG_BOOLVALUE) {
130 result = booleanControl(theParent);
132 } else if (theType == WDG_POINT_SELECTOR) {
133 result = pointSelectorControl(theParent);
135 } else if (theType == WDG_FEATURE_SELECTOR) {
136 result = featureSelectorControl(theParent);
138 } else if (theType == WDG_FEATURE_OR_ATTRIBUTE_SELECTOR) {
139 result = featureOrAttributeSelectorControl(theParent);
141 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
142 result = doubleValueEditor(theParent);
144 } else if (theType == WDG_POINT2D_DISTANCE) {
145 result = point2dDistanceControl(theParent);
148 else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
149 result = createContainer(theType, theParent);
151 result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi, myModelWidgets);
153 if (!result) { qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type"); }
159 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
161 QWidget* result = NULL;
162 if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
163 QGroupBox* aGroupBox = new QGroupBox(theParent);
164 aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
166 } else if (theType == WDG_TOOLBOX) {
167 result = new QToolBox(theParent);
168 } else if (theType == WDG_SWITCH) {
169 result = new ModuleBase_WidgetSwitch(theParent);
170 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
174 else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type"; }
179 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
181 ModuleBase_WidgetDoubleValue* aDblWgt =
182 new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi, myParentId);
183 myModelWidgets.append(aDblWgt);
185 return aDblWgt->getControl();
188 QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
190 ModuleBase_WidgetPoint2D* aWidget =
191 new ModuleBase_WidgetPoint2D(theParent, myWidgetApi, myParentId);
192 myModelWidgets.append(aWidget);
193 return aWidget->getControl();
196 QWidget* ModuleBase_WidgetFactory::featureSelectorControl(QWidget* theParent)
198 ModuleBase_WidgetFeature* aWidget =
199 new ModuleBase_WidgetFeature(theParent, myWidgetApi, myParentId);
200 myModelWidgets.append(aWidget);
201 return aWidget->getControl();
204 QWidget* ModuleBase_WidgetFactory::featureOrAttributeSelectorControl(QWidget* theParent)
206 ModuleBase_WidgetFeatureOrAttribute* aWidget =
207 new ModuleBase_WidgetFeatureOrAttribute(theParent, myWidgetApi, myParentId);
208 myModelWidgets.append(aWidget);
209 return aWidget->getControl();
212 QWidget* ModuleBase_WidgetFactory::doubleValueEditor(QWidget* theParent)
214 ModuleBase_WidgetEditor* aWidget =
215 new ModuleBase_WidgetEditor(theParent, myWidgetApi, myParentId);
216 myModelWidgets.append(aWidget);
217 return aWidget->getControl();
220 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
222 return QString::fromStdString(theStdString);
225 bool ModuleBase_WidgetFactory::isInternalWidget(const std::string& theType)
227 std::string prop = myWidgetApi->getProperty(FEATURE_INTERNAL);
229 std::transform(prop.begin(), prop.end(), prop.begin(), ::tolower);
230 if(prop.empty() || prop == "false" || prop == "0") {
236 QWidget* ModuleBase_WidgetFactory::selectorControl(QWidget* theParent)
238 ModuleBase_WidgetSelector* aSelector =
239 new ModuleBase_WidgetSelector(theParent, myWorkshop, myWidgetApi, myParentId);
240 myModelWidgets.append(aSelector);
241 return aSelector->getControl();
245 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
247 ModuleBase_WidgetBoolValue* aBoolWgt =
248 new ModuleBase_WidgetBoolValue(theParent, myWidgetApi, myParentId);
249 myModelWidgets.append(aBoolWgt);
251 return aBoolWgt->getControl();
255 QWidget* ModuleBase_WidgetFactory::point2dDistanceControl(QWidget* theParent)
257 ModuleBase_WidgetPoint2dDistance* aDistWgt =
258 new ModuleBase_WidgetPoint2dDistance(theParent, myWidgetApi, myParentId);
259 myModelWidgets.append(aDistWgt);
261 return aDistWgt->getControl();