]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetFactory.cpp
Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[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_WidgetSwitch.h>
15 #include <ModuleBase_WidgetSelector.h>
16 #include <ModuleBase_WidgetDoubleValue.h>
17 #include <ModuleBase_WidgetBoolValue.h>
18
19 #include <Config_Keywords.h>
20 #include <Config_WidgetAPI.h>
21
22 #include <QWidget>
23 #include <QHBoxLayout>
24 #include <QGridLayout>
25 #include <QSpinBox>
26 #include <QMetaProperty>
27 #include <QLabel>
28 #include <QPixmap>
29 #include <QGroupBox>
30 #include <QToolBox>
31
32 #ifdef _DEBUG
33 #include <QDebug>
34 #endif
35
36 #include <cfloat>
37 #include <climits>
38
39 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,
40                                                    ModuleBase_IWorkshop* theWorkshop)
41  : myWorkshop(theWorkshop)
42 {
43   myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
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(theParent);
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 (theType == WDG_FEATURE_SELECTOR) {
124     result = featureSelectorControl(theParent);
125
126   }
127   else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
128     result = createContainer(theType, theParent);
129   }
130 #ifdef _DEBUG
131   else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad widget type"; }
132 #endif
133   return result;
134 }
135
136 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
137 {
138   QWidget* result = NULL;
139   if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
140     QGroupBox* aGroupBox = new QGroupBox(theParent);
141     aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
142     result = aGroupBox;
143   } else if (theType == WDG_TOOLBOX) {
144     result = new QToolBox(theParent);
145   } else if (theType == WDG_SWITCH) {
146     result = new ModuleBase_WidgetSwitch(theParent);
147   } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
148     result = NULL;
149   }
150 #ifdef _DEBUG
151   else { qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type"; }
152 #endif
153   return result;
154 }
155
156 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
157 {
158   ModuleBase_WidgetDoubleValue* aDblWgt = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi);
159   myModelWidgets.append(aDblWgt);
160
161   return aDblWgt->getControl();
162 }
163
164 QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
165 {
166   ModuleBase_WidgetPoint2D* aWidget = new ModuleBase_WidgetPoint2D(theParent, myWidgetApi);
167   myModelWidgets.append(aWidget);
168   return aWidget->getControl();
169 }
170
171 QWidget* ModuleBase_WidgetFactory::featureSelectorControl(QWidget* theParent)
172 {
173   ModuleBase_WidgetFeature* aWidget = new ModuleBase_WidgetFeature(theParent, myWidgetApi);
174   myModelWidgets.append(aWidget);
175   return aWidget->getControl();
176 }
177
178 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
179 {
180   return QString::fromStdString(theStdString);
181 }
182
183
184 QWidget* ModuleBase_WidgetFactory::selectorControl(QWidget* theParent)
185 {
186   ModuleBase_WidgetSelector* aSelector = new ModuleBase_WidgetSelector(theParent, myWorkshop, myWidgetApi);
187   myModelWidgets.append(aSelector);
188   return aSelector->getControl();
189 }
190
191
192 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
193 {
194   ModuleBase_WidgetBoolValue* aBoolWgt = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi);
195   myModelWidgets.append(aBoolWgt);
196
197   return aBoolWgt->getControl();
198 }