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_WidgetPoint2D.h>
13 #include <ModuleBase_WidgetFeatureOrAttribute.h>
14 #include <ModuleBase_WidgetFeature.h>
15 #include <ModuleBase_WidgetEditor.h>
16 #include <ModuleBase_WidgetSwitch.h>
17 #include <ModuleBase_WidgetShapeSelector.h>
18 #include <ModuleBase_WidgetDoubleValue.h>
19 #include <ModuleBase_WidgetBoolValue.h>
20 #include <ModuleBase_WidgetPoint2dDistance.h>
21 #include <ModuleBase_WidgetFileSelector.h>
22 #include <ModuleBase_WidgetChoice.h>
23 #include <ModuleBase_IWorkshop.h>
24 #include <ModuleBase_IModule.h>
25 #include <ModuleBase_Tools.h>
26 #include <ModuleBase_WidgetLineEdit.h>
27 #include <ModuleBase_WidgetMultiSelector.h>
29 #include <ModelAPI_Validator.h>
30 #include <ModelAPI_Session.h>
32 #include <Config_Keywords.h>
33 #include <Config_WidgetAPI.h>
36 #include <QHBoxLayout>
37 #include <QGridLayout>
39 #include <QMetaProperty>
52 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,
53 ModuleBase_IWorkshop* theWorkshop)
54 : myWorkshop(theWorkshop)
56 myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
59 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
64 void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
66 myParentId = myWidgetApi->widgetId();
67 if (!myWidgetApi->toChildWidget())
70 QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
71 do { //Iterate over each node
72 std::string aWdgType = myWidgetApi->widgetType();
73 //Create a widget (doublevalue, groupbox, toolbox, etc.
74 QWidget* aWidget = createWidgetByType(aWdgType, theParent);
76 if (!myWidgetApi->getBooleanAttribute(ATTRIBUTE_INTERNAL, false)) {
77 aWidgetLay->addWidget(aWidget);
79 aWidget->setVisible(false);
82 if (myWidgetApi->isContainerWidget()) {
83 //if current widget is groupbox (container) process it's children recursively
84 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
85 createWidget(aWidget);
86 ModuleBase_Tools::adjustMargins(aWidget);
87 QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
88 aGrBox->setTitle(aGroupName);
90 if (myWidgetApi->isPagedWidget()) {
91 //If current widget is toolbox or switch-casebox then fetch all
92 //it's pages recursively and setup into the widget.
93 myWidgetApi->toChildWidget();
95 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
96 QWidget* aPage = new QWidget(aWidget);
97 ModuleBase_Tools::adjustMargins(aPage);
99 if (aWdgType == WDG_SWITCH) {
100 ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
101 aSwitch->addPage(aPage, aPageName);
102 } else if (aWdgType == WDG_TOOLBOX) {
103 QToolBox* aToolbox = qobject_cast<QToolBox*>(aWidget);
104 aToolbox->addItem(aPage, aPageName);
106 } while (myWidgetApi->toNextWidget());
108 } while (myWidgetApi->toNextWidget());
109 theParent->setLayout(aWidgetLay);
112 QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
114 QWidget* result = new QWidget(theParent);
115 QVBoxLayout* aLabelLay = new QVBoxLayout(result);
116 QLabel* aLabel = new QLabel(result);
117 aLabel->setWordWrap(true);
118 aLabel->setText(qs(myWidgetApi->getProperty(INFO_WDG_TEXT)));
119 aLabel->setToolTip(qs(myWidgetApi->getProperty(INFO_WDG_TOOLTIP)));
120 aLabelLay->addWidget(aLabel);
121 aLabelLay->addStretch(1);
122 result->setLayout(aLabelLay);
126 void ModuleBase_WidgetFactory::processAttributes()
128 // register that this attribute in feature is not obligatory for the feature execution
129 // so, it is not needed for the standard validation mechanism
130 bool isObligatory = true;
131 bool isConcealment = false;
133 isObligatory = myWidgetApi->getBooleanAttribute(ATTRIBUTE_OBLIGATORY, true);
134 isConcealment = myWidgetApi->getBooleanAttribute(ATTRIBUTE_CONCEALMENT, false);
136 boost::shared_ptr<ModelAPI_Session> aSession = ModelAPI_Session::get();
138 aSession->validators()->registerNotObligatory(myParentId, myWidgetApi->widgetId());
141 aSession->validators()->registerConcealment(myParentId, myWidgetApi->widgetId());
145 QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
148 QWidget* result = NULL;
149 if (theType == WDG_DOUBLEVALUE) {
150 result = doubleSpinBoxControl(theParent);
152 } else if (theType == WDG_INFO) {
153 result = labelControl(theParent);
155 } else if (theType == WDG_SHAPE_SELECTOR) {
156 result = shapeSelectorControl(theParent);
158 } else if (theType == WDG_BOOLVALUE) {
159 result = booleanControl(theParent);
161 } else if (theType == WDG_POINT_SELECTOR) {
162 result = pointSelectorControl(theParent);
164 } else if (theType == WDG_FEATURE_SELECTOR) {
165 result = featureSelectorControl(theParent);
167 } else if (theType == WDG_FEATURE_OR_ATTRIBUTE_SELECTOR) {
168 result = featureOrAttributeSelectorControl(theParent);
170 } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
171 result = doubleValueEditor(theParent);
173 } else if (theType == WDG_POINT2D_DISTANCE) {
174 result = point2dDistanceControl(theParent);
176 } else if (theType == WDG_FILE_SELECTOR) {
177 result = fileSelectorControl(theParent);
179 } else if (theType == WDG_CHOICE) {
180 result = choiceControl(theParent);
182 } else if (theType == WDG_STRINGVALUE) {
183 result = lineEditControl(theParent);
185 } else if (theType == WDG_MULTISELECTOR) {
186 result = multiSelectorControl(theParent);
188 } else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
189 result = createContainer(theType, theParent);
191 result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi,
194 if (!result) {qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type");}
204 QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
206 QWidget* result = NULL;
207 if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
208 QGroupBox* aGroupBox = new QGroupBox(theParent);
209 aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
211 } else if (theType == WDG_TOOLBOX) {
212 result = new QToolBox(theParent);
213 } else if (theType == WDG_SWITCH) {
214 result = new ModuleBase_WidgetSwitch(theParent);
215 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
219 else {qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type";}
224 QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
226 ModuleBase_WidgetDoubleValue* aDblWgt =
227 new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi, myParentId);
228 myModelWidgets.append(aDblWgt);
229 return aDblWgt->getControl();
232 QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
234 ModuleBase_WidgetPoint2D* aWidget =
235 new ModuleBase_WidgetPoint2D(theParent, myWidgetApi,myParentId);
236 myModelWidgets.append(aWidget);
237 return aWidget->getControl();
240 QWidget* ModuleBase_WidgetFactory::featureSelectorControl(QWidget* theParent)
242 ModuleBase_WidgetFeature* aWidget =
243 new ModuleBase_WidgetFeature(theParent, myWidgetApi,myParentId);
244 myModelWidgets.append(aWidget);
245 return aWidget->getControl();
248 QWidget* ModuleBase_WidgetFactory::featureOrAttributeSelectorControl(QWidget* theParent)
250 ModuleBase_WidgetFeatureOrAttribute* aWidget =
251 new ModuleBase_WidgetFeatureOrAttribute(theParent, myWidgetApi, myParentId);
252 myModelWidgets.append(aWidget);
253 return aWidget->getControl();
256 QWidget* ModuleBase_WidgetFactory::doubleValueEditor(QWidget* theParent)
258 ModuleBase_WidgetEditor* aWidget =
259 new ModuleBase_WidgetEditor(theParent, myWidgetApi, myParentId);
260 myModelWidgets.append(aWidget);
261 return aWidget->getControl();
264 QWidget* ModuleBase_WidgetFactory::shapeSelectorControl(QWidget* theParent)
266 ModuleBase_WidgetShapeSelector* aSelector =
267 new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi, myParentId);
268 myModelWidgets.append(aSelector);
269 return aSelector->getControl();
272 QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
274 ModuleBase_WidgetBoolValue* aBoolWgt =
275 new ModuleBase_WidgetBoolValue(theParent, myWidgetApi, myParentId);
276 myModelWidgets.append(aBoolWgt);
277 return aBoolWgt->getControl();
280 QWidget* ModuleBase_WidgetFactory::point2dDistanceControl(QWidget* theParent)
282 ModuleBase_WidgetPoint2dDistance* aDistWgt =
283 new ModuleBase_WidgetPoint2dDistance(theParent, myWidgetApi, myParentId);
284 myModelWidgets.append(aDistWgt);
285 return aDistWgt->getControl();
288 QWidget* ModuleBase_WidgetFactory::fileSelectorControl(QWidget* theParent)
290 ModuleBase_WidgetFileSelector* aFileSelectorWgt =
291 new ModuleBase_WidgetFileSelector(theParent, myWidgetApi, myParentId);
292 myModelWidgets.append(aFileSelectorWgt);
293 return aFileSelectorWgt->getControl();
296 QWidget* ModuleBase_WidgetFactory::choiceControl(QWidget* theParent)
298 ModuleBase_WidgetChoice* aChoiceWgt =
299 new ModuleBase_WidgetChoice(theParent, myWidgetApi,myParentId);
300 myModelWidgets.append(aChoiceWgt);
301 return aChoiceWgt->getControl();
304 QWidget* ModuleBase_WidgetFactory::lineEditControl(QWidget* theParent)
306 ModuleBase_WidgetLineEdit* aLineEditWgt =
307 new ModuleBase_WidgetLineEdit(theParent, myWidgetApi,myParentId);
308 myModelWidgets.append(aLineEditWgt);
309 return aLineEditWgt->getControl();
312 QWidget* ModuleBase_WidgetFactory::multiSelectorControl(QWidget* theParent)
314 ModuleBase_WidgetMultiSelector* aMultiselectorWgt =
315 new ModuleBase_WidgetMultiSelector(theParent, myWorkshop, myWidgetApi,myParentId);
316 myModelWidgets.append(aMultiselectorWgt);
317 return aMultiselectorWgt->getControl();
320 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
322 return QString::fromStdString(theStdString);