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 if (!myWidgetApi->toChildWidget())
60 QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
61 aWidgetLay->setContentsMargins(2, 2, 2, 2);
62 do { //Iterate over each node
63 std::string aWdgType = myWidgetApi->widgetType();
64 //Create a widget (doublevalue, groupbox, toolbox, etc.
65 QWidget* aWidget = createWidgetByType(aWdgType, theParent);
67 if (!isInternalWidget(aWdgType)) {
68 aWidgetLay->addWidget(aWidget);
71 aWidget->setVisible(false);
74 if (myWidgetApi->isContainerWidget()) {
75 //if current widget is groupbox (container) process it's children recursively
76 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
77 createWidget(aWidget);
78 QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
79 aGrBox->setTitle(aGroupName);
81 if (myWidgetApi->isPagedWidget()) {
82 //If current widget is toolbox or switch-casebox then fetch all
83 //it's pages recursively and setup into the widget.
84 myWidgetApi->toChildWidget();
86 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
87 QWidget* aPage = new QWidget(aWidget);
89 if (aWdgType == WDG_SWITCH) {
90 ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
91 aSwitch->addPage(aPage, aPageName);
92 } else if (aWdgType == WDG_TOOLBOX){
93 QToolBox* aToolbox = qobject_cast<QToolBox*>(aWidget);
94 aToolbox->addItem(aPage, aPageName);
96 } while(myWidgetApi->toNextWidget());
98 } while(myWidgetApi->toNextWidget());
99 theParent->setLayout(aWidgetLay);
102 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
104 QWidget* result = new QWidget(theParent);
105 QVBoxLayout* aLabelLay = new QVBoxLayout(result);
106 QLabel* aLabel = new QLabel(result);
107 aLabel->setWordWrap(true);
108 aLabel->setText(qs(myWidgetApi->getProperty(INFO_WDG_TEXT)));
109 aLabel->setToolTip(qs(myWidgetApi->getProperty(INFO_WDG_TOOLTIP)));
110 aLabelLay->addWidget(aLabel);
111 aLabelLay->addStretch(1);
112 result->setLayout(aLabelLay);
116 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType, QWidget* theParent)
118 QWidget* result = NULL;
119 if (theType == WDG_DOUBLEVALUE) {
120 result = doubleSpinBoxControl(theParent);
122 } else if (theType == WDG_INFO) {
123 result = labelControl(theParent);
125 } else if (theType == WDG_SELECTOR) {
126 result = selectorControl(theParent);
128 } else if (theType == WDG_BOOLVALUE) {
129 result = booleanControl(theParent);
131 } else if (theType == WDG_POINT_SELECTOR) {
132 result = pointSelectorControl(theParent);
134 } else if (theType == WDG_FEATURE_SELECTOR) {
135 result = featureSelectorControl(theParent);
137 } else if (theType == WDG_FEATURE_OR_ATTRIBUTE_SELECTOR) {
138 result = featureOrAttributeSelectorControl(theParent);
140 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
141 result = doubleValueEditor(theParent);
143 } else if (theType == WDG_POINT2D_DISTANCE) {
144 result = point2dDistanceControl(theParent);
147 else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
148 result = createContainer(theType, theParent);
150 result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi, myModelWidgets);
152 if (!result) { qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type"); }
158 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
160 QWidget* result = NULL;
161 if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
162 QGroupBox* aGroupBox = new QGroupBox(theParent);
163 aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
165 } else if (theType == WDG_TOOLBOX) {
166 result = new QToolBox(theParent);
167 } else if (theType == WDG_SWITCH) {
168 result = new ModuleBase_WidgetSwitch(theParent);
169 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
173 else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type"; }
178 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
180 ModuleBase_WidgetDoubleValue* aDblWgt = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi);
181 myModelWidgets.append(aDblWgt);
183 return aDblWgt->getControl();
186 QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
188 ModuleBase_WidgetPoint2D* aWidget = new ModuleBase_WidgetPoint2D(theParent, myWidgetApi);
189 myModelWidgets.append(aWidget);
190 return aWidget->getControl();
193 QWidget* ModuleBase_WidgetFactory::featureSelectorControl(QWidget* theParent)
195 ModuleBase_WidgetFeature* aWidget = new ModuleBase_WidgetFeature(theParent, myWidgetApi);
196 myModelWidgets.append(aWidget);
197 return aWidget->getControl();
200 QWidget* ModuleBase_WidgetFactory::featureOrAttributeSelectorControl(QWidget* theParent)
202 ModuleBase_WidgetFeatureOrAttribute* aWidget = new ModuleBase_WidgetFeatureOrAttribute(theParent,
204 myModelWidgets.append(aWidget);
205 return aWidget->getControl();
208 QWidget* ModuleBase_WidgetFactory::doubleValueEditor(QWidget* theParent)
210 ModuleBase_WidgetEditor* aWidget = new ModuleBase_WidgetEditor(theParent, myWidgetApi);
211 myModelWidgets.append(aWidget);
212 return aWidget->getControl();
215 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
217 return QString::fromStdString(theStdString);
220 bool ModuleBase_WidgetFactory::isInternalWidget(const std::string& theType)
222 std::string prop = myWidgetApi->getProperty(FEATURE_INTERNAL);
224 std::transform(prop.begin(), prop.end(), prop.begin(), ::tolower);
225 if(prop.empty() || prop == "false" || prop == "0") {
231 QWidget* ModuleBase_WidgetFactory::selectorControl(QWidget* theParent)
233 ModuleBase_WidgetSelector* aSelector = new ModuleBase_WidgetSelector(theParent, myWorkshop, myWidgetApi);
234 myModelWidgets.append(aSelector);
235 return aSelector->getControl();
239 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
241 ModuleBase_WidgetBoolValue* aBoolWgt = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi);
242 myModelWidgets.append(aBoolWgt);
244 return aBoolWgt->getControl();
248 QWidget* ModuleBase_WidgetFactory::point2dDistanceControl(QWidget* theParent)
250 ModuleBase_WidgetPoint2dDistance* aDistWgt = new ModuleBase_WidgetPoint2dDistance(theParent, myWidgetApi);
251 myModelWidgets.append(aDistWgt);
253 return aDistWgt->getControl();