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_PropPanelOperation.h>
14 #include <ModuleBase_WidgetPoint2D.h>
15 #include <ModuleBase_WidgetSwitch.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)
38 : myOperation(theOperation)
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();
108 } else if (theType == WDG_INFO) {
109 result = labelControl(theParent);
111 else if (theType == WDG_POINT_SELECTOR) {
112 result = pointSelectorControl(theParent);
114 else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
115 result = createContainer(theType, theParent);
118 else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad widget type"; }
123 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
125 QWidget* result = NULL;
126 if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
127 QGroupBox* aGroupBox = new QGroupBox(theParent);
128 aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
130 } else if (theType == WDG_TOOLBOX) {
131 result = new QToolBox(theParent);
132 } else if (theType == WDG_SWITCH) {
133 result = new ModuleBase_WidgetSwitch(theParent);
134 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
138 else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type"; }
143 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl()
145 QWidget* result = new QWidget();
146 QHBoxLayout* aControlLay = new QHBoxLayout(result);
147 aControlLay->setContentsMargins(0, 0, 0, 0);
148 QString aLabelText = qs(myWidgetApi->widgetLabel());
149 QString aLabelIcon = qs(myWidgetApi->widgetIcon());
150 QLabel* aLabel = new QLabel(aLabelText);
151 aLabel->setPixmap(QPixmap(aLabelIcon));
153 aControlLay->addWidget(aLabel);
154 QDoubleSpinBox* aBox = new QDoubleSpinBox(result);
155 QString anObjName = QString::fromStdString(myWidgetApi->widgetId());
156 aBox->setObjectName(anObjName);
158 std::string aProp = myWidgetApi->getProperty(DOUBLE_WDG_MIN);
159 double aMinVal = qs(aProp).toDouble(&isOk);
161 aBox->setMinimum(aMinVal);
163 aBox->setMinimum(-DBL_MAX);
165 aProp = myWidgetApi->getProperty(DOUBLE_WDG_MAX);
166 double aMaxVal = qs(aProp).toDouble(&isOk);
168 aBox->setMaximum(aMaxVal);
170 aBox->setMaximum(DBL_MAX);
172 aProp = myWidgetApi->getProperty(DOUBLE_WDG_STEP);
173 double aStepVal = qs(aProp).toDouble(&isOk);
175 aBox->setSingleStep(aStepVal);
177 aProp = myWidgetApi->getProperty(DOUBLE_WDG_DFLT);
178 double aDefVal = qs(aProp).toDouble(&isOk);
180 aBox->setValue(aDefVal);
182 QString aTTip = qs(myWidgetApi->widgetTooltip());
183 aBox->setToolTip(aTTip);
184 aControlLay->addWidget(aBox);
185 aControlLay->setStretch(1, 1);
186 result->setLayout(aControlLay);
187 connectWidget(aBox, WDG_DOUBLEVALUE);
191 QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
193 ModuleBase_WidgetPoint2D* aWidget = new ModuleBase_WidgetPoint2D(theParent,
194 qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME)),
195 myWidgetApi->widgetId());
196 connectWidget(aWidget, WDG_POINT_SELECTOR);
197 myModelWidgets.append(aWidget);
198 return aWidget->getControl();
201 bool ModuleBase_WidgetFactory::connectWidget(QObject* theWidget, const QString& theType)
204 if (theType == WDG_DOUBLEVALUE) {
205 result = QObject::connect(theWidget, SIGNAL(valueChanged(double)),
206 myOperation, SLOT(storeReal(double)));
208 if (theType == WDG_POINT_SELECTOR) {
209 result = QObject::connect(theWidget, SIGNAL(valuesChanged()),
210 myOperation, SLOT(storeCustomValue()));
215 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
217 return QString::fromStdString(theStdString);