]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetFactory.cpp
Salome HOME
e122c52504b6633feb7e573e8ed2d885b91899ed
[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_WidgetFeature.h>
14 #include <ModuleBase_WidgetEditor.h>
15 #include <ModuleBase_WidgetSwitch.h>
16 #include <ModuleBase_WidgetSelector.h>
17 #include <ModuleBase_WidgetDoubleValue.h>
18 #include <ModuleBase_WidgetBoolValue.h>
19 #include <ModuleBase_WidgetPoint2dDistance.h>
20
21 #include <Config_Keywords.h>
22 #include <Config_WidgetAPI.h>
23
24 #include <QWidget>
25 #include <QHBoxLayout>
26 #include <QGridLayout>
27 #include <QSpinBox>
28 #include <QMetaProperty>
29 #include <QLabel>
30 #include <QPixmap>
31 #include <QGroupBox>
32 #include <QToolBox>
33
34 #ifdef _DEBUG
35 #include <QDebug>
36 #endif
37
38 #include <cfloat>
39 #include <climits>
40
41 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,
42                                                    ModuleBase_IWorkshop* theWorkshop)
43  : myWorkshop(theWorkshop)
44 {
45   myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
46 }
47
48 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
49 {
50 }
51
52 void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
53 {
54   if (!myWidgetApi->toChildWidget())
55     return;
56
57   QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
58   aWidgetLay->setContentsMargins(2, 2, 2, 2);
59   do { //Iterate over each node
60     std::string aWdgType = myWidgetApi->widgetType();
61     //Create a widget (doublevalue, groupbox, toolbox, etc.
62     QWidget* aWidget = createWidgetByType(aWdgType, theParent);
63     if (aWidget) {
64       if (!isInternalWidget(aWdgType)) {
65         aWidgetLay->addWidget(aWidget);
66       }
67       else
68         aWidget->setVisible(false);
69     }
70     if (myWidgetApi->isContainerWidget()) {
71       //if current widget is groupbox (container) process it's children recursively
72       QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
73       createWidget(aWidget);
74       QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
75       aGrBox->setTitle(aGroupName);
76     }
77     if (myWidgetApi->isPagedWidget()) {
78       //If current widget is toolbox or switch-casebox then fetch all
79       //it's pages recursively and setup into the widget.
80       myWidgetApi->toChildWidget();
81       do {
82         QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
83         QWidget* aPage = new QWidget(aWidget);
84         createWidget(aPage);
85         if (aWdgType == WDG_SWITCH) {
86           ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
87           aSwitch->addPage(aPage, aPageName);
88         } else if (aWdgType == WDG_TOOLBOX){
89           QToolBox* aToolbox = qobject_cast<QToolBox*>(aWidget);
90           aToolbox->addItem(aPage, aPageName);
91         }
92       } while(myWidgetApi->toNextWidget());
93     }
94   } while(myWidgetApi->toNextWidget());
95   theParent->setLayout(aWidgetLay);
96 }
97
98 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
99 {
100   QWidget* result = new QWidget(theParent);
101   QVBoxLayout* aLabelLay = new QVBoxLayout(result);
102   QLabel* aLabel = new QLabel(result);
103   aLabel->setText(qs(myWidgetApi->getProperty(INFO_WDG_TEXT)));
104   aLabel->setToolTip(qs(myWidgetApi->getProperty(INFO_WDG_TOOLTIP)));
105   aLabelLay->addWidget(aLabel);
106   aLabelLay->addStretch(1);
107   result->setLayout(aLabelLay);
108   return result;
109 }
110
111 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType, QWidget* theParent)
112 {
113   QWidget* result = NULL;
114   if (theType == WDG_DOUBLEVALUE) {
115     result = doubleSpinBoxControl(theParent);
116
117   } else if (theType == WDG_INFO) {
118     result = labelControl(theParent);
119
120   } else if (theType == WDG_SELECTOR) {
121     result = selectorControl(theParent);
122
123   } else if (theType == WDG_BOOLVALUE) {
124     result = booleanControl(theParent);
125
126   } else if (theType == WDG_POINT_SELECTOR) {
127     result = pointSelectorControl(theParent);
128
129   } else if (theType == WDG_FEATURE_SELECTOR) {
130     result = featureSelectorControl(theParent);
131
132   } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
133     result = doubleValueEditor(theParent);
134   
135   } else if (theType == WDG_POINT2D_DISTANCE) {
136     result = point2dDistanceControl(theParent);
137
138   }
139   else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
140     result = createContainer(theType, theParent);
141   }
142 #ifdef _DEBUG
143   else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad widget type"; }
144 #endif
145   return result;
146 }
147
148 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
149 {
150   QWidget* result = NULL;
151   if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
152     QGroupBox* aGroupBox = new QGroupBox(theParent);
153     aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
154     result = aGroupBox;
155   } else if (theType == WDG_TOOLBOX) {
156     result = new QToolBox(theParent);
157   } else if (theType == WDG_SWITCH) {
158     result = new ModuleBase_WidgetSwitch(theParent);
159   } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
160     result = NULL;
161   }
162 #ifdef _DEBUG
163   else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type"; }
164 #endif
165   return result;
166 }
167
168 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
169 {
170   ModuleBase_WidgetDoubleValue* aDblWgt = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi);
171   myModelWidgets.append(aDblWgt);
172
173   return aDblWgt->getControl();
174 }
175
176 QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
177 {
178   ModuleBase_WidgetPoint2D* aWidget = new ModuleBase_WidgetPoint2D(theParent, myWidgetApi);
179   myModelWidgets.append(aWidget);
180   return aWidget->getControl();
181 }
182
183 QWidget* ModuleBase_WidgetFactory::featureSelectorControl(QWidget* theParent)
184 {
185   ModuleBase_WidgetFeature* aWidget = new ModuleBase_WidgetFeature(theParent, myWidgetApi);
186   myModelWidgets.append(aWidget);
187   return aWidget->getControl();
188 }
189
190 QWidget* ModuleBase_WidgetFactory::doubleValueEditor(QWidget* theParent)
191 {
192   ModuleBase_WidgetEditor* aWidget = new ModuleBase_WidgetEditor(theParent, myWidgetApi);
193   myModelWidgets.append(aWidget);
194   return 0;
195 }
196
197 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
198 {
199   return QString::fromStdString(theStdString);
200 }
201
202 bool ModuleBase_WidgetFactory::isInternalWidget(const std::string& theType)
203 {
204   std::string prop = myWidgetApi->getProperty(FEATURE_INTERNAL);
205
206   std::transform(prop.begin(), prop.end(), prop.begin(), ::tolower);
207   if(prop.empty() || prop == "false" || prop == "0") {
208     return false;
209   }
210   return true; 
211 }
212
213 QWidget* ModuleBase_WidgetFactory::selectorControl(QWidget* theParent)
214 {
215   ModuleBase_WidgetSelector* aSelector = new ModuleBase_WidgetSelector(theParent, myWorkshop, myWidgetApi);
216   myModelWidgets.append(aSelector);
217   return aSelector->getControl();
218 }
219
220
221 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
222 {
223   ModuleBase_WidgetBoolValue* aBoolWgt = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi);
224   myModelWidgets.append(aBoolWgt);
225
226   return aBoolWgt->getControl();
227 }
228
229
230 QWidget* ModuleBase_WidgetFactory::point2dDistanceControl(QWidget* theParent)
231 {
232   ModuleBase_WidgetPoint2dDistance* aDistWgt = new ModuleBase_WidgetPoint2dDistance(theParent, myWidgetApi);
233   myModelWidgets.append(aDistWgt);
234
235   return aDistWgt->getControl();
236 }