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>
29 #include <QToolButton>
39 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(ModuleBase_Operation* theOperation)
40 : myOperation(theOperation)
42 QString aXml = myOperation->getDescription()->xmlRepresentation();
43 myWidgetApi = new Config_WidgetAPI(aXml.toStdString());
46 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
50 void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
52 if (!myWidgetApi->toChildWidget())
55 QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
56 aWidgetLay->setContentsMargins(2, 2, 2, 2);
57 do { //Iterate over each node
58 std::string aWdgType = myWidgetApi->widgetType();
59 //Create a widget (doublevalue, groupbox, toolbox, etc.
60 QWidget* aWidget = createWidgetByType(aWdgType, theParent);
62 aWidgetLay->addWidget(aWidget);
64 if (myWidgetApi->isContainerWidget()) {
65 //if current widget is groupbox (container) process it's children recursively
66 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
67 createWidget(aWidget);
68 QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
69 aGrBox->setTitle(aGroupName);
71 if (myWidgetApi->isPagedWidget()) {
72 //If current widget is toolbox or switch-casebox then fetch all
73 //it's pages recursively and setup into the widget.
74 myWidgetApi->toChildWidget();
76 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
77 QWidget* aPage = new QWidget(aWidget);
79 if (aWdgType == WDG_SWITCH) {
80 ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
81 aSwitch->addPage(aPage, aPageName);
82 } else if (aWdgType == WDG_TOOLBOX){
83 QToolBox* aToolbox = qobject_cast<QToolBox*>(aWidget);
84 aToolbox->addItem(aPage, aPageName);
86 } while(myWidgetApi->toNextWidget());
88 } while(myWidgetApi->toNextWidget());
89 theParent->setLayout(aWidgetLay);
92 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
94 QWidget* result = new QWidget(theParent);
95 QVBoxLayout* aLabelLay = new QVBoxLayout(result);
96 QLabel* aLabel = new QLabel(result);
97 aLabel->setText(qs(myWidgetApi->getProperty(INFO_WDG_TEXT)));
98 aLabel->setToolTip(qs(myWidgetApi->getProperty(INFO_WDG_TOOLTIP)));
99 aLabelLay->addWidget(aLabel);
100 aLabelLay->addStretch(1);
101 result->setLayout(aLabelLay);
105 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType, QWidget* theParent)
107 QWidget* result = NULL;
108 if (theType == WDG_DOUBLEVALUE) {
109 result = doubleSpinBoxControl();
111 } else if (theType == WDG_INFO) {
112 result = labelControl(theParent);
114 } else if (theType == WDG_SELECTOR) {
115 result = selectorControl(theParent);
117 } else if (theType == WDG_BOOLVALUE) {
118 result = booleanControl(theParent);
120 } else if (theType == WDG_POINT_SELECTOR) {
121 result = pointSelectorControl(theParent);
123 } else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
124 result = createContainer(theType, theParent);
127 else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad widget type"; }
132 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
134 QWidget* result = NULL;
135 if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
136 QGroupBox* aGroupBox = new QGroupBox(theParent);
137 aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
139 } else if (theType == WDG_TOOLBOX) {
140 result = new QToolBox(theParent);
141 } else if (theType == WDG_SWITCH) {
142 result = new ModuleBase_WidgetSwitch(theParent);
143 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
147 else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type"; }
152 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl()
154 QWidget* result = new QWidget();
155 QHBoxLayout* aControlLay = new QHBoxLayout(result);
156 aControlLay->setContentsMargins(0, 0, 0, 0);
157 QString aLabelText = qs(myWidgetApi->widgetLabel());
158 QString aLabelIcon = qs(myWidgetApi->widgetIcon());
159 QLabel* aLabel = new QLabel(aLabelText);
160 aLabel->setPixmap(QPixmap(aLabelIcon));
162 aControlLay->addWidget(aLabel);
163 QDoubleSpinBox* aBox = new QDoubleSpinBox(result);
164 QString anObjName = QString::fromStdString(myWidgetApi->widgetId());
165 aBox->setObjectName(anObjName);
167 std::string aProp = myWidgetApi->getProperty(DOUBLE_WDG_MIN);
168 double aMinVal = qs(aProp).toDouble(&isOk);
170 aBox->setMinimum(aMinVal);
172 aBox->setMinimum(-DBL_MAX);
174 aProp = myWidgetApi->getProperty(DOUBLE_WDG_MAX);
175 double aMaxVal = qs(aProp).toDouble(&isOk);
177 aBox->setMaximum(aMaxVal);
179 aBox->setMaximum(DBL_MAX);
181 aProp = myWidgetApi->getProperty(DOUBLE_WDG_STEP);
182 double aStepVal = qs(aProp).toDouble(&isOk);
184 aBox->setSingleStep(aStepVal);
186 aProp = myWidgetApi->getProperty(DOUBLE_WDG_DFLT);
187 double aDefVal = qs(aProp).toDouble(&isOk);
189 aBox->setValue(aDefVal);
191 QString aTTip = qs(myWidgetApi->widgetTooltip());
192 aBox->setToolTip(aTTip);
193 aControlLay->addWidget(aBox);
194 aControlLay->setStretch(1, 1);
195 result->setLayout(aControlLay);
196 connectWidget(aBox, WDG_DOUBLEVALUE);
200 QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
202 ModuleBase_WidgetPoint2D* aWidget = new ModuleBase_WidgetPoint2D(theParent,
203 qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME)),
204 myWidgetApi->widgetId());
205 connectWidget(aWidget, WDG_POINT_SELECTOR);
206 myModelWidgets.append(aWidget);
207 return aWidget->getControl();
210 bool ModuleBase_WidgetFactory::connectWidget(QObject* theWidget, const QString& theType)
213 if (theType == WDG_DOUBLEVALUE) {
214 result = QObject::connect(theWidget, SIGNAL(valueChanged(double)),
215 myOperation, SLOT(storeReal(double)));
217 if (theType == WDG_POINT_SELECTOR) {
218 result = QObject::connect(theWidget, SIGNAL(valuesChanged()),
219 myOperation, SLOT(storeCustomValue()));
224 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
226 return QString::fromStdString(theStdString);
230 QWidget* ModuleBase_WidgetFactory::selectorControl(QWidget* theParent)
232 QWidget* aRes = new QWidget();
233 QHBoxLayout* aLayout = new QHBoxLayout(aRes);
235 aLayout->setContentsMargins(0, 0, 0, 0);
236 QString aLabelText = qs(myWidgetApi->widgetLabel());
237 QString aLabelIcon = qs(myWidgetApi->widgetIcon());
238 QLabel* aLabel = new QLabel(aLabelText, aRes);
239 aLabel->setPixmap(QPixmap(aLabelIcon));
241 aLayout->addWidget(aLabel);
243 QLineEdit* aTextLine = new QLineEdit(aRes);
244 aTextLine->setReadOnly(true);
246 aLayout->addWidget(aTextLine);
248 QToolButton* aActivateBtn = new QToolButton(aRes);
249 aActivateBtn->setIcon(QIcon(":icons/hand_point.png"));
250 aActivateBtn->setCheckable(true);
252 aLayout->addWidget(aActivateBtn);
258 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
260 QString aText = qs(myWidgetApi->widgetLabel());
261 QString aToolTip = qs(myWidgetApi->widgetTooltip());
262 QString aDefault = qs(myWidgetApi->getProperty("default"));
264 QCheckBox* aRes = new QCheckBox(aText, theParent);
265 aRes->setToolTip(aToolTip);
266 aRes->setChecked(aDefault == "true");