2 * ModuleBase_WidgetFactory.cpp
4 * Created on: Apr 3, 2014
8 #include <ModuleBase_WidgetFactory.h>
10 #include <ModuleBase_Operation.h>
11 #include <ModuleBase_OperationDescription.h>
12 //#include <ModuleBase_WidgetFeatureOrAttribute.h>
13 //#include <ModuleBase_WidgetFeature.h>
14 #include <ModuleBase_WidgetEditor.h>
15 #include <ModuleBase_WidgetSwitch.h>
16 #include <ModuleBase_WidgetShapeSelector.h>
17 #include <ModuleBase_WidgetDoubleValue.h>
18 #include <ModuleBase_WidgetBoolValue.h>
19 //#include <ModuleBase_WidgetPoint2dDistance.h>
20 #include <ModuleBase_WidgetFileSelector.h>
21 #include <ModuleBase_WidgetChoice.h>
22 #include <ModuleBase_IWorkshop.h>
23 #include <ModuleBase_IModule.h>
24 #include <ModuleBase_Tools.h>
25 #include <ModuleBase_WidgetLineEdit.h>
26 #include <ModuleBase_WidgetMultiSelector.h>
28 #include <ModelAPI_Validator.h>
29 #include <ModelAPI_Session.h>
31 #include <Config_Keywords.h>
32 #include <Config_WidgetAPI.h>
35 #include <QHBoxLayout>
36 #include <QGridLayout>
38 #include <QMetaProperty>
51 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,
52 ModuleBase_IWorkshop* theWorkshop)
53 : myWorkshop(theWorkshop)
55 myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
58 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
63 void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
65 myParentId = myWidgetApi->widgetId();
66 if (!myWidgetApi->toChildWidget())
69 QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
70 do { //Iterate over each node
71 std::string aWdgType = myWidgetApi->widgetType();
72 //Create a widget (doublevalue, groupbox, toolbox, etc.
73 QWidget* aWidget = createWidgetByType(aWdgType, theParent);
75 if (!myWidgetApi->getBooleanAttribute(ATTRIBUTE_INTERNAL, false)) {
76 aWidgetLay->addWidget(aWidget);
78 aWidget->setVisible(false);
81 if (myWidgetApi->isContainerWidget()) {
82 //if current widget is groupbox (container) process it's children recursively
83 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
84 createWidget(aWidget);
85 ModuleBase_Tools::adjustMargins(aWidget);
86 QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
87 aGrBox->setTitle(aGroupName);
89 if (myWidgetApi->isPagedWidget()) {
90 //If current widget is toolbox or switch-casebox then fetch all
91 //it's pages recursively and setup into the widget.
92 myWidgetApi->toChildWidget();
94 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
95 QWidget* aPage = new QWidget(aWidget);
96 ModuleBase_Tools::adjustMargins(aPage);
98 if (aWdgType == WDG_SWITCH) {
99 ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
100 aSwitch->addPage(aPage, aPageName);
101 } else if (aWdgType == WDG_TOOLBOX) {
102 QToolBox* aToolbox = qobject_cast<QToolBox*>(aWidget);
103 aToolbox->addItem(aPage, aPageName);
105 } while (myWidgetApi->toNextWidget());
107 } while (myWidgetApi->toNextWidget());
108 theParent->setLayout(aWidgetLay);
111 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
113 QWidget* result = new QWidget(theParent);
114 QVBoxLayout* aLabelLay = new QVBoxLayout(result);
115 QLabel* aLabel = new QLabel(result);
116 aLabel->setWordWrap(true);
117 aLabel->setText(qs(myWidgetApi->getProperty(INFO_WDG_TEXT)));
118 aLabel->setToolTip(qs(myWidgetApi->getProperty(INFO_WDG_TOOLTIP)));
119 aLabelLay->addWidget(aLabel);
120 aLabelLay->addStretch(1);
121 result->setLayout(aLabelLay);
125 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
128 QWidget* result = NULL;
129 if (theType == WDG_DOUBLEVALUE) {
130 result = doubleSpinBoxControl(theParent);
132 } else if (theType == WDG_INFO) {
133 result = labelControl(theParent);
135 } else if (theType == WDG_SHAPE_SELECTOR) {
136 result = shapeSelectorControl(theParent);
138 } else if (theType == WDG_BOOLVALUE) {
139 result = booleanControl(theParent);
141 //} else if (theType == WDG_FEATURE_SELECTOR) {
142 // result = featureSelectorControl(theParent);
144 //} else if (theType == WDG_FEATURE_OR_ATTRIBUTE_SELECTOR) {
145 // result = featureOrAttributeSelectorControl(theParent);
147 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
148 result = doubleValueEditor(theParent);
150 } else if (theType == WDG_FILE_SELECTOR) {
151 result = fileSelectorControl(theParent);
153 } else if (theType == WDG_CHOICE) {
154 result = choiceControl(theParent);
156 } else if (theType == WDG_STRINGVALUE) {
157 result = lineEditControl(theParent);
159 } else if (theType == WDG_MULTISELECTOR) {
160 result = multiSelectorControl(theParent);
162 } else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
163 result = createContainer(theType, theParent);
165 result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi,
166 myParentId, myModelWidgets);
168 if (!result) {qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type");}
174 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
176 QWidget* result = NULL;
177 if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
178 QGroupBox* aGroupBox = new QGroupBox(theParent);
179 aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
181 } else if (theType == WDG_TOOLBOX) {
182 result = new QToolBox(theParent);
183 } else if (theType == WDG_SWITCH) {
184 result = new ModuleBase_WidgetSwitch(theParent);
185 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
189 else {qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type";}
194 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
196 ModuleBase_WidgetDoubleValue* aDblWgt =
197 new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi, myParentId);
198 myModelWidgets.append(aDblWgt);
199 return aDblWgt->getControl();
202 //QWidget* ModuleBase_WidgetFactory::featureSelectorControl(QWidget* theParent)
204 // ModuleBase_WidgetFeature* aWidget =
205 // new ModuleBase_WidgetFeature(theParent, myWidgetApi,myParentId);
206 // myModelWidgets.append(aWidget);
207 // return aWidget->getControl();
210 //QWidget* ModuleBase_WidgetFactory::featureOrAttributeSelectorControl(QWidget* theParent)
212 // ModuleBase_WidgetFeatureOrAttribute* aWidget =
213 // new ModuleBase_WidgetFeatureOrAttribute(theParent, myWidgetApi, myParentId);
214 // myModelWidgets.append(aWidget);
215 // return aWidget->getControl();
218 QWidget* ModuleBase_WidgetFactory::doubleValueEditor(QWidget* theParent)
220 ModuleBase_WidgetEditor* aWidget =
221 new ModuleBase_WidgetEditor(theParent, myWidgetApi, myParentId);
222 myModelWidgets.append(aWidget);
223 return aWidget->getControl();
226 QWidget* ModuleBase_WidgetFactory::shapeSelectorControl(QWidget* theParent)
228 ModuleBase_WidgetShapeSelector* aSelector =
229 new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi, myParentId);
230 myModelWidgets.append(aSelector);
231 return aSelector->getControl();
234 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
236 ModuleBase_WidgetBoolValue* aBoolWgt =
237 new ModuleBase_WidgetBoolValue(theParent, myWidgetApi, myParentId);
238 myModelWidgets.append(aBoolWgt);
239 return aBoolWgt->getControl();
242 QWidget* ModuleBase_WidgetFactory::fileSelectorControl(QWidget* theParent)
244 ModuleBase_WidgetFileSelector* aFileSelectorWgt =
245 new ModuleBase_WidgetFileSelector(theParent, myWidgetApi, myParentId);
246 myModelWidgets.append(aFileSelectorWgt);
247 return aFileSelectorWgt->getControl();
250 QWidget* ModuleBase_WidgetFactory::choiceControl(QWidget* theParent)
252 ModuleBase_WidgetChoice* aChoiceWgt =
253 new ModuleBase_WidgetChoice(theParent, myWidgetApi,myParentId);
254 myModelWidgets.append(aChoiceWgt);
255 return aChoiceWgt->getControl();
258 QWidget* ModuleBase_WidgetFactory::lineEditControl(QWidget* theParent)
260 ModuleBase_WidgetLineEdit* aLineEditWgt =
261 new ModuleBase_WidgetLineEdit(theParent, myWidgetApi,myParentId);
262 myModelWidgets.append(aLineEditWgt);
263 return aLineEditWgt->getControl();
266 QWidget* ModuleBase_WidgetFactory::multiSelectorControl(QWidget* theParent)
268 ModuleBase_WidgetMultiSelector* aMultiselectorWgt =
269 new ModuleBase_WidgetMultiSelector(theParent, myWorkshop, myWidgetApi,myParentId);
270 myModelWidgets.append(aMultiselectorWgt);
271 return aMultiselectorWgt->getControl();
274 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
276 return QString::fromStdString(theStdString);