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>
15 #include <ModuleBase_SelectorWidget.h>
17 #include <Config_Keywords.h>
18 #include <Config_WidgetAPI.h>
21 #include <QHBoxLayout>
22 #include <QGridLayout>
24 #include <QMetaProperty>
38 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(ModuleBase_Operation* theOperation, ModuleBase_IWorkshop* theWorkshop)
39 : myOperation(theOperation), myWorkshop(theWorkshop)
41 QString aXml = myOperation->getDescription()->xmlRepresentation();
42 myWidgetApi = new Config_WidgetAPI(aXml.toStdString());
45 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
49 void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
51 if (!myWidgetApi->toChildWidget())
54 QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
55 aWidgetLay->setContentsMargins(2, 2, 2, 2);
56 do { //Iterate over each node
57 std::string aWdgType = myWidgetApi->widgetType();
58 //Create a widget (doublevalue, groupbox, toolbox, etc.
59 QWidget* aWidget = createWidgetByType(aWdgType, theParent);
61 aWidgetLay->addWidget(aWidget);
63 if (myWidgetApi->isContainerWidget()) {
64 //if current widget is groupbox (container) process it's children recursively
65 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
66 createWidget(aWidget);
67 QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
68 aGrBox->setTitle(aGroupName);
70 if (myWidgetApi->isPagedWidget()) {
71 //If current widget is toolbox or switch-casebox then fetch all
72 //it's pages recursively and setup into the widget.
73 myWidgetApi->toChildWidget();
75 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
76 QWidget* aPage = new QWidget(aWidget);
78 if (aWdgType == WDG_SWITCH) {
79 ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
80 aSwitch->addPage(aPage, aPageName);
81 } else if (aWdgType == WDG_TOOLBOX){
82 QToolBox* aToolbox = qobject_cast<QToolBox*>(aWidget);
83 aToolbox->addItem(aPage, aPageName);
85 } while(myWidgetApi->toNextWidget());
87 } while(myWidgetApi->toNextWidget());
88 theParent->setLayout(aWidgetLay);
91 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
93 QWidget* result = new QWidget(theParent);
94 QVBoxLayout* aLabelLay = new QVBoxLayout(result);
95 QLabel* aLabel = new QLabel(result);
96 aLabel->setText(qs(myWidgetApi->getProperty(INFO_WDG_TEXT)));
97 aLabel->setToolTip(qs(myWidgetApi->getProperty(INFO_WDG_TOOLTIP)));
98 aLabelLay->addWidget(aLabel);
99 aLabelLay->addStretch(1);
100 result->setLayout(aLabelLay);
104 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType, QWidget* theParent)
106 QWidget* result = NULL;
107 if (theType == WDG_DOUBLEVALUE) {
108 result = doubleSpinBoxControl();
110 } else if (theType == WDG_INFO) {
111 result = labelControl(theParent);
113 } else if (theType == WDG_SELECTOR) {
114 result = selectorControl(theParent);
116 } else if (theType == WDG_BOOLVALUE) {
117 result = booleanControl(theParent);
119 } else if (theType == WDG_POINT_SELECTOR) {
120 result = pointSelectorControl(theParent);
122 } else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
123 result = createContainer(theType, theParent);
126 else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad widget type"; }
131 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
133 QWidget* result = NULL;
134 if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
135 QGroupBox* aGroupBox = new QGroupBox(theParent);
136 aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
138 } else if (theType == WDG_TOOLBOX) {
139 result = new QToolBox(theParent);
140 } else if (theType == WDG_SWITCH) {
141 result = new ModuleBase_WidgetSwitch(theParent);
142 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
146 else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type"; }
151 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl()
153 QWidget* result = new QWidget();
154 QHBoxLayout* aControlLay = new QHBoxLayout(result);
155 aControlLay->setContentsMargins(0, 0, 0, 0);
156 QString aLabelText = qs(myWidgetApi->widgetLabel());
157 QString aLabelIcon = qs(myWidgetApi->widgetIcon());
158 QLabel* aLabel = new QLabel(aLabelText);
159 aLabel->setPixmap(QPixmap(aLabelIcon));
161 aControlLay->addWidget(aLabel);
162 QDoubleSpinBox* aBox = new QDoubleSpinBox(result);
163 QString anObjName = QString::fromStdString(myWidgetApi->widgetId());
164 aBox->setObjectName(anObjName);
166 std::string aProp = myWidgetApi->getProperty(DOUBLE_WDG_MIN);
167 double aMinVal = qs(aProp).toDouble(&isOk);
169 aBox->setMinimum(aMinVal);
171 aBox->setMinimum(-DBL_MAX);
173 aProp = myWidgetApi->getProperty(DOUBLE_WDG_MAX);
174 double aMaxVal = qs(aProp).toDouble(&isOk);
176 aBox->setMaximum(aMaxVal);
178 aBox->setMaximum(DBL_MAX);
180 aProp = myWidgetApi->getProperty(DOUBLE_WDG_STEP);
181 double aStepVal = qs(aProp).toDouble(&isOk);
183 aBox->setSingleStep(aStepVal);
185 aProp = myWidgetApi->getProperty(DOUBLE_WDG_DFLT);
186 double aDefVal = qs(aProp).toDouble(&isOk);
188 aBox->setValue(aDefVal);
190 QString aTTip = qs(myWidgetApi->widgetTooltip());
191 aBox->setToolTip(aTTip);
192 aControlLay->addWidget(aBox);
193 aControlLay->setStretch(1, 1);
194 result->setLayout(aControlLay);
195 connectWidget(aBox, WDG_DOUBLEVALUE);
199 QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
201 ModuleBase_WidgetPoint2D* aWidget = new ModuleBase_WidgetPoint2D(theParent,
202 qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME)),
203 myWidgetApi->widgetId());
204 connectWidget(aWidget, WDG_POINT_SELECTOR);
205 myModelWidgets.append(aWidget);
206 return aWidget->getControl();
209 bool ModuleBase_WidgetFactory::connectWidget(QObject* theWidget, const QString& theType)
212 if (theType == WDG_DOUBLEVALUE) {
213 result = QObject::connect(theWidget, SIGNAL(valueChanged(double)),
214 myOperation, SLOT(storeReal(double)));
216 if (theType == WDG_POINT_SELECTOR) {
217 result = QObject::connect(theWidget, SIGNAL(valuesChanged()),
218 myOperation, SLOT(storeCustomValue()));
223 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
225 return QString::fromStdString(theStdString);
229 QWidget* ModuleBase_WidgetFactory::selectorControl(QWidget* theParent)
231 ModuleBase_SelectorWidget* aSelector = new ModuleBase_SelectorWidget(theParent, myWorkshop, myWidgetApi);
233 QObject::connect(aSelector, SIGNAL(valuesChanged()), myOperation, SLOT(storeCustomValue()));
235 myModelWidgets.append(aSelector);
236 return aSelector->getControl();
240 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
242 QString aText = qs(myWidgetApi->widgetLabel());
243 QString aToolTip = qs(myWidgetApi->widgetTooltip());
244 QString aDefault = qs(myWidgetApi->getProperty("default"));
246 QCheckBox* aRes = new QCheckBox(aText, theParent);
247 aRes->setToolTip(aToolTip);
248 aRes->setChecked(aDefault == "true");