2 * XGUI_WidgetFactory.cpp
4 * Created on: Apr 3, 2014
8 #include <XGUI_WidgetFactory.h>
10 #include <XGUI_SwitchWidget.h>
12 #include <ModuleBase_PropPanelOperation.h>
13 #include <Config_Keywords.h>
14 #include <Config_WidgetAPI.h>
17 #include <QHBoxLayout>
19 #include <QMetaProperty>
32 XGUI_WidgetFactory::XGUI_WidgetFactory(ModuleBase_PropPanelOperation* theOperation)
33 : myOperation(theOperation)
35 QString aXml = myOperation->xmlRepresentation();
36 myWidgetApi = new Config_WidgetAPI(aXml.toStdString());
39 XGUI_WidgetFactory::~XGUI_WidgetFactory()
43 void XGUI_WidgetFactory::createWidget(QWidget* theParent)
45 if (!myWidgetApi->toChildWidget())
48 QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
49 aWidgetLay->setContentsMargins(2, 2, 2, 2);
50 do { //Iterate over each node
51 std::string aWdgType = myWidgetApi->widgetType();
52 //Create a widget (doublevalue, groupbox, toolbox, etc.
53 QWidget* aWidget = createWidgetByType(aWdgType, theParent);
55 aWidgetLay->addWidget(aWidget);
57 if (myWidgetApi->isContainerWidget()) {
58 //if current widget is groupbox (container) process it's children recursively
59 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
60 createWidget(aWidget);
61 QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
62 aGrBox->setTitle(aGroupName);
64 if (myWidgetApi->isPagedWidget()) {
65 //If current widget is toolbox or switch-casebox then fetch all
66 //it's pages recursively and setup into the widget.
67 myWidgetApi->toChildWidget();
69 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
70 QWidget* aPage = new QWidget(aWidget);
72 if (aWdgType == WDG_SWITCH) {
73 XGUI_SwitchWidget* aSwitch = qobject_cast<XGUI_SwitchWidget*>(aWidget);
74 aSwitch->addPage(aPage, aPageName);
75 } else if (aWdgType == WDG_TOOLBOX){
76 QToolBox* aToolbox = qobject_cast<QToolBox*>(aWidget);
77 aToolbox->addItem(aPage, aPageName);
79 } while(myWidgetApi->toNextWidget());
81 } while(myWidgetApi->toNextWidget());
82 theParent->setLayout(aWidgetLay);
85 QWidget* XGUI_WidgetFactory::createWidgetByType(const std::string& theType, QWidget* theParent)
87 QWidget* result = NULL;
88 if (theType == WDG_DOUBLEVALUE) {
89 result = doubleSpinBoxControl();
90 } else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
91 result = createContainer(theType, theParent);
94 else { qDebug() << "XGUI_WidgetFactory::fillWidget: find bad widget type"; }
99 QWidget* XGUI_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
101 QWidget* result = NULL;
102 if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
103 QGroupBox* aGroupBox = new QGroupBox(theParent);
104 aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
106 } else if (theType == WDG_TOOLBOX) {
107 result = new QToolBox(theParent);
108 } else if (theType == WDG_SWITCH) {
109 result = new XGUI_SwitchWidget(theParent);
110 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
114 else { qDebug() << "XGUI_WidgetFactory::fillWidget: find bad container type"; }
119 QWidget* XGUI_WidgetFactory::doubleSpinBoxControl()
121 QWidget* result = new QWidget();
122 QHBoxLayout* aControlLay = new QHBoxLayout(result);
123 aControlLay->setContentsMargins(0, 0, 0, 0);
124 QString aLabelText = qs(myWidgetApi->widgetLabel());
125 QString aLabelIcon = qs(myWidgetApi->widgetIcon());
126 QLabel* aLabel = new QLabel(aLabelText);
127 aLabel->setPixmap(QPixmap(aLabelIcon));
129 aControlLay->addWidget(aLabel);
130 QDoubleSpinBox* aBox = new QDoubleSpinBox(result);
131 QString anObjName = QString::fromStdString(myWidgetApi->widgetId());
132 aBox->setObjectName(anObjName);
134 std::string aProp = myWidgetApi->getProperty(DOUBLE_WDG_MIN);
135 double aMinVal = qs(aProp).toDouble(&isOk);
137 aBox->setMinimum(aMinVal);
139 aBox->setMinimum(-DBL_MAX);
141 aProp = myWidgetApi->getProperty(DOUBLE_WDG_MAX);
142 double aMaxVal = qs(aProp).toDouble(&isOk);
144 aBox->setMaximum(aMaxVal);
146 aBox->setMaximum(DBL_MAX);
148 aProp = myWidgetApi->getProperty(DOUBLE_WDG_STEP);
149 double aStepVal = qs(aProp).toDouble(&isOk);
151 aBox->setSingleStep(aStepVal);
153 aProp = myWidgetApi->getProperty(DOUBLE_WDG_DFLT);
154 double aDefVal = qs(aProp).toDouble(&isOk);
156 aBox->setValue(aDefVal);
158 QString aTTip = qs(myWidgetApi->widgetTooltip());
159 aBox->setToolTip(aTTip);
160 aControlLay->addWidget(aBox);
161 aControlLay->setStretch(1, 1);
162 result->setLayout(aControlLay);
163 connectWidget(aBox, WDG_DOUBLEVALUE);
167 bool XGUI_WidgetFactory::connectWidget(QWidget* theWidget, const QString& theType)
170 if (theType == WDG_DOUBLEVALUE) {
171 result = QObject::connect(theWidget, SIGNAL(valueChanged(double)),
172 myOperation, SLOT(storeReal(double)));
177 QString XGUI_WidgetFactory::qs(const std::string& theStdString) const
179 return QString::fromStdString(theStdString);