]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetFactory.cpp
Salome HOME
Prepare extrusion feature
[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
16 #include <Config_Keywords.h>
17 #include <Config_WidgetAPI.h>
18
19 #include <QWidget>
20 #include <QHBoxLayout>
21 #include <QGridLayout>
22 #include <QSpinBox>
23 #include <QMetaProperty>
24 #include <QLabel>
25 #include <QPixmap>
26 #include <QGroupBox>
27 #include <QToolBox>
28 #include <QLineEdit>
29 #include <QToolButton>
30 #include <QCheckBox>
31
32 #ifdef _DEBUG
33 #include <QDebug>
34 #endif
35
36 #include <cfloat>
37 #include <climits>
38
39 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(ModuleBase_Operation* theOperation)
40  : myOperation(theOperation)
41 {
42   QString aXml = myOperation->getDescription()->xmlRepresentation();
43   myWidgetApi = new Config_WidgetAPI(aXml.toStdString());
44 }
45
46 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
47 {
48 }
49
50 void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
51 {
52   if (!myWidgetApi->toChildWidget())
53     return;
54
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);
61     if (aWidget) {
62       aWidgetLay->addWidget(aWidget);
63     }
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);
70     }
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();
75       do {
76         QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
77         QWidget* aPage = new QWidget(aWidget);
78         createWidget(aPage);
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);
85         }
86       } while(myWidgetApi->toNextWidget());
87     }
88   } while(myWidgetApi->toNextWidget());
89   theParent->setLayout(aWidgetLay);
90 }
91
92 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
93 {
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);
102   return result;
103 }
104
105 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType, QWidget* theParent)
106 {
107   QWidget* result = NULL;
108   if (theType == WDG_DOUBLEVALUE) {
109     result = doubleSpinBoxControl();
110
111   } else if (theType == WDG_INFO) {
112     result = labelControl(theParent);
113
114   } else if (theType == WDG_SELECTOR) {
115     result = selectorControl(theParent);
116
117   } else if (theType == WDG_BOOLVALUE) {
118     result = booleanControl(theParent);
119
120   } else if (theType == WDG_POINT_SELECTOR) {
121     result = pointSelectorControl(theParent);
122
123   } else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
124     result = createContainer(theType, theParent);
125   }
126 #ifdef _DEBUG
127   else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad widget type"; }
128 #endif
129   return result;
130 }
131
132 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
133 {
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);
138     result = aGroupBox;
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) {
144     result = NULL;
145   }
146 #ifdef _DEBUG
147   else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type"; }
148 #endif
149   return result;
150 }
151
152 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl()
153 {
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));
161
162   aControlLay->addWidget(aLabel);
163   QDoubleSpinBox* aBox = new QDoubleSpinBox(result);
164   QString anObjName = QString::fromStdString(myWidgetApi->widgetId());
165   aBox->setObjectName(anObjName);
166   bool isOk = false;
167   std::string aProp = myWidgetApi->getProperty(DOUBLE_WDG_MIN);
168   double aMinVal = qs(aProp).toDouble(&isOk);
169   if (isOk) {
170     aBox->setMinimum(aMinVal);
171   } else {
172     aBox->setMinimum(-DBL_MAX);
173   }
174   aProp = myWidgetApi->getProperty(DOUBLE_WDG_MAX);
175   double aMaxVal = qs(aProp).toDouble(&isOk);
176   if (isOk) {
177     aBox->setMaximum(aMaxVal);
178   } else {
179     aBox->setMaximum(DBL_MAX);
180   }
181   aProp = myWidgetApi->getProperty(DOUBLE_WDG_STEP);
182   double aStepVal = qs(aProp).toDouble(&isOk);
183   if (isOk) {
184     aBox->setSingleStep(aStepVal);
185   }
186   aProp = myWidgetApi->getProperty(DOUBLE_WDG_DFLT);
187   double aDefVal = qs(aProp).toDouble(&isOk);
188   if (isOk) {
189     aBox->setValue(aDefVal);
190   }
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);
197   return result;
198 }
199
200 QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
201 {
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();
208 }
209
210 bool ModuleBase_WidgetFactory::connectWidget(QObject* theWidget,  const QString& theType)
211 {
212   bool result = false;
213   if (theType == WDG_DOUBLEVALUE) {
214     result = QObject::connect(theWidget, SIGNAL(valueChanged(double)), 
215                               myOperation, SLOT(storeReal(double)));
216   }
217   if (theType == WDG_POINT_SELECTOR) {
218     result = QObject::connect(theWidget, SIGNAL(valuesChanged()),
219                               myOperation, SLOT(storeCustomValue()));
220   }
221   return result;
222 }
223
224 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
225 {
226   return QString::fromStdString(theStdString);
227 }
228
229
230 QWidget* ModuleBase_WidgetFactory::selectorControl(QWidget* theParent)
231 {
232   QWidget* aRes = new QWidget();
233   QHBoxLayout* aLayout = new QHBoxLayout(aRes);
234
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));
240
241   aLayout->addWidget(aLabel);
242
243   QLineEdit* aTextLine = new QLineEdit(aRes);
244   aTextLine->setReadOnly(true);
245
246   aLayout->addWidget(aTextLine);
247
248   QToolButton* aActivateBtn = new QToolButton(aRes);
249   aActivateBtn->setIcon(QIcon(":icons/hand_point.png"));
250   aActivateBtn->setCheckable(true);
251
252   aLayout->addWidget(aActivateBtn);
253
254   return aRes;
255 }
256
257
258 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
259 {
260   QString aText = qs(myWidgetApi->widgetLabel());
261   QString aToolTip = qs(myWidgetApi->widgetTooltip());
262   QString aDefault = qs(myWidgetApi->getProperty("default"));
263
264   QCheckBox* aRes = new QCheckBox(aText, theParent);
265   aRes->setToolTip(aToolTip);
266   aRes->setChecked(aDefault == "true");
267   return aRes;
268 }