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_WidgetSwitch.h>
14 #include <ModuleBase_SelectorWidget.h>
15 #include <ModuleBase_Widgets.h>
17 #include <Config_Keywords.h>
18 #include <Config_WidgetAPI.h>
21 #include <QHBoxLayout>
22 #include <QGridLayout>
24 #include <QMetaProperty>
37 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(ModuleBase_Operation* theOperation, ModuleBase_IWorkshop* theWorkshop)
38 : myOperation(theOperation), myWorkshop(theWorkshop)
40 QString aXml = myOperation->getDescription()->xmlRepresentation();
41 myWidgetApi = new Config_WidgetAPI(aXml.toStdString());
44 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
48 void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
50 if (!myWidgetApi->toChildWidget())
53 QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
54 aWidgetLay->setContentsMargins(2, 2, 2, 2);
55 do { //Iterate over each node
56 std::string aWdgType = myWidgetApi->widgetType();
57 //Create a widget (doublevalue, groupbox, toolbox, etc.
58 QWidget* aWidget = createWidgetByType(aWdgType, theParent);
60 aWidgetLay->addWidget(aWidget);
62 if (myWidgetApi->isContainerWidget()) {
63 //if current widget is groupbox (container) process it's children recursively
64 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
65 createWidget(aWidget);
66 QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
67 aGrBox->setTitle(aGroupName);
69 if (myWidgetApi->isPagedWidget()) {
70 //If current widget is toolbox or switch-casebox then fetch all
71 //it's pages recursively and setup into the widget.
72 myWidgetApi->toChildWidget();
74 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
75 QWidget* aPage = new QWidget(aWidget);
77 if (aWdgType == WDG_SWITCH) {
78 ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
79 aSwitch->addPage(aPage, aPageName);
80 } else if (aWdgType == WDG_TOOLBOX){
81 QToolBox* aToolbox = qobject_cast<QToolBox*>(aWidget);
82 aToolbox->addItem(aPage, aPageName);
84 } while(myWidgetApi->toNextWidget());
86 } while(myWidgetApi->toNextWidget());
87 theParent->setLayout(aWidgetLay);
90 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
92 QWidget* result = new QWidget(theParent);
93 QVBoxLayout* aLabelLay = new QVBoxLayout(result);
94 QLabel* aLabel = new QLabel(result);
95 aLabel->setText(qs(myWidgetApi->getProperty(INFO_WDG_TEXT)));
96 aLabel->setToolTip(qs(myWidgetApi->getProperty(INFO_WDG_TOOLTIP)));
97 aLabelLay->addWidget(aLabel);
98 aLabelLay->addStretch(1);
99 result->setLayout(aLabelLay);
103 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType, QWidget* theParent)
105 QWidget* result = NULL;
106 if (theType == WDG_DOUBLEVALUE) {
107 result = doubleSpinBoxControl(theParent);
109 } else if (theType == WDG_INFO) {
110 result = labelControl(theParent);
112 } else if (theType == WDG_SELECTOR) {
113 result = selectorControl(theParent);
115 } else if (theType == WDG_BOOLVALUE) {
116 result = booleanControl(theParent);
118 } else if (theType == WDG_POINT_SELECTOR) {
119 result = pointSelectorControl(theParent);
121 } else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
122 result = createContainer(theType, theParent);
125 else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad widget type"; }
130 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
132 QWidget* result = NULL;
133 if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
134 QGroupBox* aGroupBox = new QGroupBox(theParent);
135 aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
137 } else if (theType == WDG_TOOLBOX) {
138 result = new QToolBox(theParent);
139 } else if (theType == WDG_SWITCH) {
140 result = new ModuleBase_WidgetSwitch(theParent);
141 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
145 else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type"; }
150 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
152 ModuleBase_DoubleValueWidget* aDblWgt = new ModuleBase_DoubleValueWidget(theParent, myWidgetApi);
153 QObject::connect(aDblWgt, SIGNAL(valuesChanged()), myOperation, SLOT(storeCustomValue()));
155 myModelWidgets.append(aDblWgt);
157 // Init default values
158 if (!myOperation->isEditOperation())
159 aDblWgt->storeValue(myOperation->feature());
160 return aDblWgt->getControl();
163 QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
165 ModuleBase_WidgetPoint2D* aWidget = new ModuleBase_WidgetPoint2D(theParent,
166 qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME)),
167 myWidgetApi->widgetId());
168 connectWidget(aWidget, WDG_POINT_SELECTOR);
169 myModelWidgets.append(aWidget);
170 return aWidget->getControl();
173 bool ModuleBase_WidgetFactory::connectWidget(QObject* theWidget, const QString& theType)
176 if (theType == WDG_DOUBLEVALUE) {
177 result = QObject::connect(theWidget, SIGNAL(valueChanged(double)),
178 myOperation, SLOT(storeReal(double)));
180 if (theType == WDG_POINT_SELECTOR) {
181 result = QObject::connect(theWidget, SIGNAL(valuesChanged()),
182 myOperation, SLOT(storeCustomValue()));
187 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
189 return QString::fromStdString(theStdString);
193 QWidget* ModuleBase_WidgetFactory::selectorControl(QWidget* theParent)
195 ModuleBase_SelectorWidget* aSelector = new ModuleBase_SelectorWidget(theParent, myWorkshop, myWidgetApi);
197 QObject::connect(aSelector, SIGNAL(valuesChanged()), myOperation, SLOT(storeCustomValue()));
199 myModelWidgets.append(aSelector);
200 return aSelector->getControl();
204 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
206 ModuleBase_BoolValueWidget* aBoolWgt = new ModuleBase_BoolValueWidget(theParent, myWidgetApi);
207 QObject::connect(aBoolWgt, SIGNAL(valuesChanged()), myOperation, SLOT(storeCustomValue()));
209 myModelWidgets.append(aBoolWgt);
211 // Init default values
212 if (!myOperation->isEditOperation())
213 aBoolWgt->storeValue(myOperation->feature());
214 return aBoolWgt->getControl();