2 * ModuleBase_WidgetFactory.cpp
4 * Created on: Apr 3, 2014
8 #include <ModuleBase_WidgetFactory.h>
10 #include <ModuleBase_MetaWidget.h>
11 #include <ModuleBase_Operation.h>
12 #include <ModuleBase_OperationDescription.h>
13 #include <ModuleBase_WidgetPoint2D.h>
14 #include <ModuleBase_WidgetSwitch.h>
16 #include <Config_Keywords.h>
17 #include <Config_WidgetAPI.h>
20 #include <QHBoxLayout>
21 #include <QGridLayout>
23 #include <QMetaProperty>
36 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(ModuleBase_Operation* theOperation)
37 : myOperation(theOperation)
39 QString aXml = myOperation->getDescription()->xmlRepresentation();
40 myWidgetApi = new Config_WidgetAPI(aXml.toStdString());
43 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
47 void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
49 if (!myWidgetApi->toChildWidget())
52 QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
53 aWidgetLay->setContentsMargins(2, 2, 2, 2);
54 do { //Iterate over each node
55 std::string aWdgType = myWidgetApi->widgetType();
56 //Create a widget (doublevalue, groupbox, toolbox, etc.
57 QWidget* aWidget = createWidgetByType(aWdgType, theParent);
59 aWidgetLay->addWidget(aWidget);
61 if (myWidgetApi->isContainerWidget()) {
62 //if current widget is groupbox (container) process it's children recursively
63 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
64 createWidget(aWidget);
65 QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
66 aGrBox->setTitle(aGroupName);
68 if (myWidgetApi->isPagedWidget()) {
69 //If current widget is toolbox or switch-casebox then fetch all
70 //it's pages recursively and setup into the widget.
71 myWidgetApi->toChildWidget();
73 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
74 QWidget* aPage = new QWidget(aWidget);
76 if (aWdgType == WDG_SWITCH) {
77 ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
78 aSwitch->addPage(aPage, aPageName);
79 } else if (aWdgType == WDG_TOOLBOX){
80 QToolBox* aToolbox = qobject_cast<QToolBox*>(aWidget);
81 aToolbox->addItem(aPage, aPageName);
83 } while(myWidgetApi->toNextWidget());
85 } while(myWidgetApi->toNextWidget());
86 theParent->setLayout(aWidgetLay);
89 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
91 QWidget* result = new QWidget(theParent);
92 QVBoxLayout* aLabelLay = new QVBoxLayout(result);
93 QLabel* aLabel = new QLabel(result);
94 aLabel->setText(qs(myWidgetApi->getProperty(INFO_WDG_TEXT)));
95 aLabel->setToolTip(qs(myWidgetApi->getProperty(INFO_WDG_TOOLTIP)));
96 aLabelLay->addWidget(aLabel);
97 aLabelLay->addStretch(1);
98 result->setLayout(aLabelLay);
102 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType, QWidget* theParent)
104 QWidget* result = NULL;
105 if (theType == WDG_DOUBLEVALUE) {
106 result = doubleSpinBoxControl();
107 } else if (theType == WDG_INFO) {
108 result = labelControl(theParent);
110 else if (theType == WDG_POINT_SELECTOR) {
111 result = pointSelectorControl(theParent);
113 else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
114 result = createContainer(theType, theParent);
117 else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad widget type"; }
122 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
124 QWidget* result = NULL;
125 if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
126 QGroupBox* aGroupBox = new QGroupBox(theParent);
127 aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
129 } else if (theType == WDG_TOOLBOX) {
130 result = new QToolBox(theParent);
131 } else if (theType == WDG_SWITCH) {
132 result = new ModuleBase_WidgetSwitch(theParent);
133 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
137 else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type"; }
142 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl()
144 QWidget* result = new QWidget();
145 QHBoxLayout* aControlLay = new QHBoxLayout(result);
146 aControlLay->setContentsMargins(0, 0, 0, 0);
147 QString aLabelText = qs(myWidgetApi->widgetLabel());
148 QString aLabelIcon = qs(myWidgetApi->widgetIcon());
149 QLabel* aLabel = new QLabel(aLabelText);
150 aLabel->setPixmap(QPixmap(aLabelIcon));
152 aControlLay->addWidget(aLabel);
153 QDoubleSpinBox* aBox = new QDoubleSpinBox(result);
154 QString anObjName = QString::fromStdString(myWidgetApi->widgetId());
155 aBox->setObjectName(anObjName);
157 std::string aProp = myWidgetApi->getProperty(DOUBLE_WDG_MIN);
158 double aMinVal = qs(aProp).toDouble(&isOk);
160 aBox->setMinimum(aMinVal);
162 aBox->setMinimum(-DBL_MAX);
164 aProp = myWidgetApi->getProperty(DOUBLE_WDG_MAX);
165 double aMaxVal = qs(aProp).toDouble(&isOk);
167 aBox->setMaximum(aMaxVal);
169 aBox->setMaximum(DBL_MAX);
171 aProp = myWidgetApi->getProperty(DOUBLE_WDG_STEP);
172 double aStepVal = qs(aProp).toDouble(&isOk);
174 aBox->setSingleStep(aStepVal);
176 aProp = myWidgetApi->getProperty(DOUBLE_WDG_DFLT);
177 double aDefVal = qs(aProp).toDouble(&isOk);
179 aBox->setValue(aDefVal);
181 QString aTTip = qs(myWidgetApi->widgetTooltip());
182 aBox->setToolTip(aTTip);
183 aControlLay->addWidget(aBox);
184 aControlLay->setStretch(1, 1);
185 result->setLayout(aControlLay);
186 connectWidget(aBox, WDG_DOUBLEVALUE);
190 QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
192 ModuleBase_WidgetPoint2D* aWidget = new ModuleBase_WidgetPoint2D(theParent,
193 qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME)),
194 myWidgetApi->widgetId());
195 connectWidget(aWidget, WDG_POINT_SELECTOR);
196 myModelWidgets.append(aWidget);
197 return aWidget->getControl();
200 bool ModuleBase_WidgetFactory::connectWidget(QObject* theWidget, const QString& theType)
203 if (theType == WDG_DOUBLEVALUE) {
204 result = QObject::connect(theWidget, SIGNAL(valueChanged(double)),
205 myOperation, SLOT(storeReal(double)));
207 if (theType == WDG_POINT_SELECTOR) {
208 result = QObject::connect(theWidget, SIGNAL(valuesChanged()),
209 myOperation, SLOT(storeCustomValue()));
214 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
216 return QString::fromStdString(theStdString);