Salome HOME
63646b9565474d81917db69ca089bd0cf30155bb
[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_Operation.h>
11 #include <ModuleBase_OperationDescription.h>
12 #include <ModuleBase_WidgetPoint2D.h>
13 #include <ModuleBase_WidgetSwitch.h>
14 #include <ModuleBase_SelectorWidget.h>
15 #include <ModuleBase_Widgets.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
30 #ifdef _DEBUG
31 #include <QDebug>
32 #endif
33
34 #include <cfloat>
35 #include <climits>
36
37 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(ModuleBase_Operation* theOperation, ModuleBase_IWorkshop* theWorkshop)
38  : myOperation(theOperation), myWorkshop(theWorkshop)
39 {
40   QString aXml = myOperation->getDescription()->xmlRepresentation();
41   myWidgetApi = new Config_WidgetAPI(aXml.toStdString());
42 }
43
44 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
45 {
46 }
47
48 void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
49 {
50   if (!myWidgetApi->toChildWidget())
51     return;
52
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);
59     if (aWidget) {
60       aWidgetLay->addWidget(aWidget);
61     }
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);
68     }
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();
73       do {
74         QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
75         QWidget* aPage = new QWidget(aWidget);
76         createWidget(aPage);
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);
83         }
84       } while(myWidgetApi->toNextWidget());
85     }
86   } while(myWidgetApi->toNextWidget());
87   theParent->setLayout(aWidgetLay);
88 }
89
90 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
91 {
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);
100   return result;
101 }
102
103 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType, QWidget* theParent)
104 {
105   QWidget* result = NULL;
106   if (theType == WDG_DOUBLEVALUE) {
107     result = doubleSpinBoxControl(theParent);
108
109   } else if (theType == WDG_INFO) {
110     result = labelControl(theParent);
111
112   } else if (theType == WDG_SELECTOR) {
113     result = selectorControl(theParent);
114
115   } else if (theType == WDG_BOOLVALUE) {
116     result = booleanControl(theParent);
117
118   } else if (theType == WDG_POINT_SELECTOR) {
119     result = pointSelectorControl(theParent);
120
121   } else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
122     result = createContainer(theType, theParent);
123   }
124 #ifdef _DEBUG
125   else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad widget type"; }
126 #endif
127   return result;
128 }
129
130 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
131 {
132   QWidget* result = NULL;
133   if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
134     QGroupBox* aGroupBox = new QGroupBox(theParent);
135     aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
136     result = aGroupBox;
137   } else if (theType == WDG_TOOLBOX) {
138     result = new QToolBox(theParent);
139   } else if (theType == WDG_SWITCH) {
140     result = new ModuleBase_WidgetSwitch(theParent);
141   } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
142     result = NULL;
143   }
144 #ifdef _DEBUG
145   else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type"; }
146 #endif
147   return result;
148 }
149
150 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
151 {
152   ModuleBase_DoubleValueWidget* aDblWgt = new ModuleBase_DoubleValueWidget(theParent, myWidgetApi);
153   QObject::connect(aDblWgt, SIGNAL(valuesChanged()),  myOperation, SLOT(storeCustomValue()));
154
155   myModelWidgets.append(aDblWgt);
156
157   // Init default values
158   if (!myOperation->isEditOperation())
159     aDblWgt->storeValue(myOperation->feature());
160   return aDblWgt->getControl();
161 }
162
163 QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
164 {
165   ModuleBase_WidgetPoint2D* aWidget = new ModuleBase_WidgetPoint2D(theParent,
166                        qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME)),
167                        myWidgetApi->widgetId());
168   connectWidget(aWidget, WDG_POINT_SELECTOR);
169   myModelWidgets.append(aWidget);
170   return aWidget->getControl();
171 }
172
173 bool ModuleBase_WidgetFactory::connectWidget(QObject* theWidget,  const QString& theType)
174 {
175   bool result = false;
176   if (theType == WDG_DOUBLEVALUE) {
177     result = QObject::connect(theWidget, SIGNAL(valueChanged(double)), 
178                               myOperation, SLOT(storeReal(double)));
179   }
180   if (theType == WDG_POINT_SELECTOR) {
181     result = QObject::connect(theWidget, SIGNAL(valuesChanged()),
182                               myOperation, SLOT(storeCustomValue()));
183   }
184   return result;
185 }
186
187 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
188 {
189   return QString::fromStdString(theStdString);
190 }
191
192
193 QWidget* ModuleBase_WidgetFactory::selectorControl(QWidget* theParent)
194 {
195   ModuleBase_SelectorWidget* aSelector = new ModuleBase_SelectorWidget(theParent, myWorkshop, myWidgetApi);
196   
197   QObject::connect(aSelector, SIGNAL(valuesChanged()),  myOperation, SLOT(storeCustomValue()));
198
199   myModelWidgets.append(aSelector);
200   return aSelector->getControl();
201 }
202
203
204 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
205 {
206   ModuleBase_BoolValueWidget* aBoolWgt = new ModuleBase_BoolValueWidget(theParent, myWidgetApi);
207   QObject::connect(aBoolWgt, SIGNAL(valuesChanged()),  myOperation, SLOT(storeCustomValue()));
208
209   myModelWidgets.append(aBoolWgt);
210
211   // Init default values
212   if (!myOperation->isEditOperation())
213     aBoolWgt->storeValue(myOperation->feature());
214   return aBoolWgt->getControl();
215 }