]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetFactory.cpp
Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetFactory.cpp
1 /*
2  * ModuleBase_WidgetFactory.cpp
3  *
4  *  Created on: Apr 3, 2014
5  *      Author: sbh
6  */
7
8 #include <ModuleBase_WidgetFactory.h>
9
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>
16
17 #include <Config_Keywords.h>
18 #include <Config_WidgetAPI.h>
19
20 #include <QWidget>
21 #include <QHBoxLayout>
22 #include <QGridLayout>
23 #include <QSpinBox>
24 #include <QMetaProperty>
25 #include <QLabel>
26 #include <QPixmap>
27 #include <QGroupBox>
28 #include <QToolBox>
29 #include <QCheckBox>
30
31 #ifdef _DEBUG
32 #include <QDebug>
33 #endif
34
35 #include <cfloat>
36 #include <climits>
37
38 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(ModuleBase_Operation* theOperation, ModuleBase_IWorkshop* theWorkshop)
39  : myOperation(theOperation), myWorkshop(theWorkshop)
40 {
41   QString aXml = myOperation->getDescription()->xmlRepresentation();
42   myWidgetApi = new Config_WidgetAPI(aXml.toStdString());
43 }
44
45 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
46 {
47 }
48
49 void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
50 {
51   if (!myWidgetApi->toChildWidget())
52     return;
53
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);
60     if (aWidget) {
61       aWidgetLay->addWidget(aWidget);
62     }
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);
69     }
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();
74       do {
75         QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
76         QWidget* aPage = new QWidget(aWidget);
77         createWidget(aPage);
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);
84         }
85       } while(myWidgetApi->toNextWidget());
86     }
87   } while(myWidgetApi->toNextWidget());
88   theParent->setLayout(aWidgetLay);
89 }
90
91 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
92 {
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);
101   return result;
102 }
103
104 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType, QWidget* theParent)
105 {
106   QWidget* result = NULL;
107   if (theType == WDG_DOUBLEVALUE) {
108     result = doubleSpinBoxControl();
109
110   } else if (theType == WDG_INFO) {
111     result = labelControl(theParent);
112
113   } else if (theType == WDG_SELECTOR) {
114     result = selectorControl(theParent);
115
116   } else if (theType == WDG_BOOLVALUE) {
117     result = booleanControl(theParent);
118
119   } else if (theType == WDG_POINT_SELECTOR) {
120     result = pointSelectorControl(theParent);
121
122   } else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
123     result = createContainer(theType, theParent);
124   }
125 #ifdef _DEBUG
126   else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad widget type"; }
127 #endif
128   return result;
129 }
130
131 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
132 {
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);
137     result = aGroupBox;
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) {
143     result = NULL;
144   }
145 #ifdef _DEBUG
146   else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type"; }
147 #endif
148   return result;
149 }
150
151 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl()
152 {
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));
160
161   aControlLay->addWidget(aLabel);
162   QDoubleSpinBox* aBox = new QDoubleSpinBox(result);
163   QString anObjName = QString::fromStdString(myWidgetApi->widgetId());
164   aBox->setObjectName(anObjName);
165   bool isOk = false;
166   std::string aProp = myWidgetApi->getProperty(DOUBLE_WDG_MIN);
167   double aMinVal = qs(aProp).toDouble(&isOk);
168   if (isOk) {
169     aBox->setMinimum(aMinVal);
170   } else {
171     aBox->setMinimum(-DBL_MAX);
172   }
173   aProp = myWidgetApi->getProperty(DOUBLE_WDG_MAX);
174   double aMaxVal = qs(aProp).toDouble(&isOk);
175   if (isOk) {
176     aBox->setMaximum(aMaxVal);
177   } else {
178     aBox->setMaximum(DBL_MAX);
179   }
180   aProp = myWidgetApi->getProperty(DOUBLE_WDG_STEP);
181   double aStepVal = qs(aProp).toDouble(&isOk);
182   if (isOk) {
183     aBox->setSingleStep(aStepVal);
184   }
185   aProp = myWidgetApi->getProperty(DOUBLE_WDG_DFLT);
186   double aDefVal = qs(aProp).toDouble(&isOk);
187   if (isOk) {
188     aBox->setValue(aDefVal);
189   }
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);
196   return result;
197 }
198
199 QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
200 {
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();
207 }
208
209 bool ModuleBase_WidgetFactory::connectWidget(QObject* theWidget,  const QString& theType)
210 {
211   bool result = false;
212   if (theType == WDG_DOUBLEVALUE) {
213     result = QObject::connect(theWidget, SIGNAL(valueChanged(double)), 
214                               myOperation, SLOT(storeReal(double)));
215   }
216   if (theType == WDG_POINT_SELECTOR) {
217     result = QObject::connect(theWidget, SIGNAL(valuesChanged()),
218                               myOperation, SLOT(storeCustomValue()));
219   }
220   return result;
221 }
222
223 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
224 {
225   return QString::fromStdString(theStdString);
226 }
227
228
229 QWidget* ModuleBase_WidgetFactory::selectorControl(QWidget* theParent)
230 {
231   ModuleBase_SelectorWidget* aSelector = new ModuleBase_SelectorWidget(theParent, myWorkshop, myWidgetApi);
232   
233   QObject::connect(aSelector, SIGNAL(valuesChanged()),  myOperation, SLOT(storeCustomValue()));
234
235   myModelWidgets.append(aSelector);
236   return aSelector->getControl();
237 }
238
239
240 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
241 {
242   QString aText = qs(myWidgetApi->widgetLabel());
243   QString aToolTip = qs(myWidgetApi->widgetTooltip());
244   QString aDefault = qs(myWidgetApi->getProperty("default"));
245
246   QCheckBox* aRes = new QCheckBox(aText, theParent);
247   aRes->setToolTip(aToolTip);
248   aRes->setChecked(aDefault == "true");
249   return aRes;
250 }