1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <ModuleBase_WidgetFactory.h>
22 #include <ModuleBase_IconFactory.h>
24 #include <ModuleBase_Operation.h>
25 #include <ModuleBase_OperationDescription.h>
26 #include <ModuleBase_WidgetEditor.h>
27 #include <ModuleBase_WidgetSwitch.h>
28 #include <ModuleBase_WidgetShapeSelector.h>
29 #include <ModuleBase_WidgetFeatureSelector.h>
30 #include <ModuleBase_WidgetDoubleValue.h>
31 #include <ModuleBase_WidgetLabelValue.h>
32 #include <ModuleBase_WidgetIntValue.h>
33 #include <ModuleBase_WidgetBoolValue.h>
34 #include <ModuleBase_WidgetFileSelector.h>
35 #include <ModuleBase_WidgetChoice.h>
36 #include <ModuleBase_IWorkshop.h>
37 #include <ModuleBase_IModule.h>
38 #include <ModuleBase_Tools.h>
39 #include <ModuleBase_WidgetLineEdit.h>
40 #include <ModuleBase_WidgetMultiSelector.h>
41 #include <ModuleBase_WidgetConcealedObjects.h>
42 #include <ModuleBase_WidgetLabel.h>
43 #include <ModuleBase_WidgetToolbox.h>
44 #include <ModuleBase_PageBase.h>
45 #include <ModuleBase_PageGroupBox.h>
46 #include <ModuleBase_WidgetOptionalBox.h>
47 #include <ModuleBase_PageWidget.h>
48 #include <ModuleBase_WidgetExprEditor.h>
49 #include <ModuleBase_WidgetCreatorFactory.h>
50 #include <ModuleBase_WidgetAction.h>
52 #include <ModelAPI_Validator.h>
53 #include <ModelAPI_Session.h>
55 #include <Config_Keywords.h>
56 #include <Config_WidgetAPI.h>
59 #include <QHBoxLayout>
60 #include <QGridLayout>
62 #include <QMetaProperty>
75 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,
76 ModuleBase_IWorkshop* theWorkshop)
77 : myWorkshop(theWorkshop)
79 myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
82 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
87 void ModuleBase_WidgetFactory::createWidget(ModuleBase_PageBase* thePage, bool alignToTop)
89 std::string aWType = myWidgetApi->widgetType();
90 if (aWType == NODE_FEATURE) {
91 // if XML definition of the feature contains the next key, the widgets should not be created,
92 // but a specific panel should be made. However, to provide persistent of the panel values,
93 // we need to get into the panel the feature of the operation. As a result this panel should
94 // be created after the feature creating(create operation). The method setPanel() of this
95 // class is used for this. Here, we just return to avoid the widgets creation.
96 std::string aPanelName = myWidgetApi->getProperty(PROPERTY_PANEL_ID);
97 if (!aPanelName.empty())
101 if (!myWidgetApi->toChildWidget())
104 do { //Iterate over each node
105 std::string aWdgType = myWidgetApi->widgetType();
106 // Create PageGroup TODO: extract
107 if (myWidgetApi->isGroupBoxWidget() ||
108 ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
110 //if current widget is groupbox (container) process it's children recursively
111 ModuleBase_PageBase* aPage = createPageByType(aWdgType, thePage->pageWidget());
114 thePage->addPageWidget(aPage);
116 // Create a ModelWidget
117 ModuleBase_ModelWidget* aWidget = createWidgetByType(aWdgType, thePage->pageWidget());
119 if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {
120 thePage->addModelWidget(aWidget);
122 aWidget->setVisible(false);
125 // Create PagedContainer TODO: extract
126 if (myWidgetApi->isPagedWidget()) {
127 //If current widget is toolbox or switch-casebox then fetch all
128 //it's pages recursively and setup into the widget.
129 if (myWidgetApi->toChildWidget()) {
131 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
132 QString aCaseId = qs(myWidgetApi->getProperty(_ID));
133 ModuleBase_PageBase* aPage = new ModuleBase_PageWidget(aWidget);
135 if (aWdgType == WDG_SWITCH || aWdgType == WDG_TOOLBOX) {
136 ModuleBase_PagedContainer* aContainer =
137 qobject_cast<ModuleBase_PagedContainer*>(aWidget);
139 QString anIconPath = qs( myWidgetApi->getProperty( CONTAINER_PAGE_ICON ) );
140 QPixmap anIcon = ModuleBase_IconFactory::loadPixmap( anIconPath );
141 aContainer->addPage( aPage, aPageName, aCaseId, anIcon );
143 } while (myWidgetApi->toNextWidget());
147 } while (myWidgetApi->toNextWidget());
150 thePage->alignToTop();
153 void ModuleBase_WidgetFactory::createPanel(ModuleBase_PageBase* thePage,
154 const FeaturePtr& theFeature)
156 std::string aPanelName = myWidgetApi->getProperty(PROPERTY_PANEL_ID);
157 if (!aPanelName.empty() && ModuleBase_WidgetCreatorFactory::get()->hasPanelWidget(aPanelName)) {
158 QWidget* aPanel = ModuleBase_WidgetCreatorFactory::get()->createPanelByType(aPanelName,
159 thePage->pageWidget(), theFeature);
160 thePage->addWidget(aPanel);
161 thePage->alignToTop();
165 void ModuleBase_WidgetFactory::createWidget(ModuleBase_PageBase* thePage,
166 const std::string& theWidgetId)
169 moveToWidgetId(theWidgetId, aFound);
171 std::string aWdgType = myWidgetApi->widgetType();
173 // Create a ModelWidget
174 ModuleBase_ModelWidget* aWidget = createWidgetByType(aWdgType, thePage->pageWidget());
176 if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {
177 thePage->addModelWidget(aWidget);
180 aWidget->setVisible(false);
184 thePage->alignToTop();
187 void ModuleBase_WidgetFactory::getAttributeTitle(const std::string& theAttributeId,
188 std::string& theTitle)
191 moveToWidgetId(theAttributeId, aFound);
193 theTitle = QString::fromStdString(myWidgetApi->widgetLabel()).toStdString().c_str();
194 if (theTitle.empty())
196 QString::fromStdString(myWidgetApi->getProperty(CONTAINER_PAGE_NAME)).toStdString().c_str();
200 void ModuleBase_WidgetFactory::getGreedAttribute(std::string& theAttributeId)
202 if (!theAttributeId.empty())
205 if (!myWidgetApi->toChildWidget())
208 do { //Iterate over each node
209 std::string aWdgType = myWidgetApi->widgetType();
210 // Find title under PageGroup
211 if (myWidgetApi->isGroupBoxWidget() ||
212 ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
213 getGreedAttribute(theAttributeId);
217 std::string anAttributeId = myWidgetApi->widgetId();
218 if (myWidgetApi->getBooleanAttribute(ATTR_GREED, false))
219 theAttributeId = anAttributeId;
220 if (theAttributeId.empty() && myWidgetApi->isPagedWidget()) {
221 //If current widget is toolbox or switch-casebox then fetch all
222 //it's pages recursively and setup into the widget.
223 if (myWidgetApi->toChildWidget()) {
225 getGreedAttribute(theAttributeId);
226 } while (theAttributeId.empty() && myWidgetApi->toNextWidget());
230 } while (theAttributeId.empty() && myWidgetApi->toNextWidget());
233 void ModuleBase_WidgetFactory::moveToWidgetId(const std::string& theWidgetId, bool& theFound)
238 if (!myWidgetApi->toChildWidget())
241 do { //Iterate over each node
242 std::string aWdgType = myWidgetApi->widgetType();
243 // Find title under PageGroup
244 if (myWidgetApi->isGroupBoxWidget() ||
245 ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
246 moveToWidgetId(theWidgetId, theFound);
250 std::string anAttributeId = myWidgetApi->widgetId();
251 theFound = anAttributeId == theWidgetId;
252 if (!theFound && myWidgetApi->isPagedWidget()) {
253 //If current widget is toolbox or switch-casebox then fetch all
254 //it's pages recursively and setup into the widget.
255 if (myWidgetApi->toChildWidget()) {
257 moveToWidgetId(theWidgetId, theFound);
258 } while (!theFound && myWidgetApi->toNextWidget());
262 } while (!theFound && myWidgetApi->toNextWidget());
265 ModuleBase_PageBase* ModuleBase_WidgetFactory::createPageByType(const std::string& theType,
268 ModuleBase_PageBase* aResult = NULL;
270 if (theType == WDG_GROUP) {
271 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
272 ModuleBase_PageGroupBox* aPage = new ModuleBase_PageGroupBox(theParent);
273 aPage->setTitle(aGroupName);
276 else if (theType == WDG_OPTIONALBOX) {
277 ModuleBase_WidgetOptionalBox* aPage = new ModuleBase_WidgetOptionalBox(theParent,
282 aResult = ModuleBase_WidgetCreatorFactory::get()->createPageByType(theType, theParent,
285 ModuleBase_ModelWidget* aWidget = dynamic_cast<ModuleBase_ModelWidget*>(aResult);
287 myModelWidgets.append(aWidget);
292 ModuleBase_ModelWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
295 ModuleBase_ModelWidget* result = NULL;
297 if (theType == WDG_INFO) {
298 result = new ModuleBase_WidgetLabel(theParent, myWidgetApi);
299 } else if (theType == WDG_DOUBLEVALUE) {
300 result = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi);
301 } else if (theType == WDG_DOUBLEVALUELABEL) {
302 result = new ModuleBase_WidgetLabelValue(theParent, myWidgetApi);
303 } else if (theType == WDG_INTEGERVALUE) {
304 result = new ModuleBase_WidgetIntValue(theParent, myWidgetApi);
305 } else if (theType == WDG_SHAPE_SELECTOR) {
306 result = new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi);
307 } else if (theType == WDG_FEATURE_SELECTOR) {
308 result = new ModuleBase_WidgetFeatureSelector(theParent, myWorkshop, myWidgetApi);
309 } else if (theType == WDG_BOOLVALUE) {
310 result = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi);
311 //} else if (theType == WDG_DOUBLEVALUE_EDITOR) {
312 // result = new ModuleBase_WidgetEditor(theParent, myWidgetApi);
313 } else if (theType == WDG_FILE_SELECTOR) {
314 result = new ModuleBase_WidgetFileSelector(theParent, myWidgetApi);
315 } else if (theType == WDG_CHOICE) {
316 result = new ModuleBase_WidgetChoice(theParent, myWidgetApi);
317 } else if (theType == WDG_STRINGVALUE) {
318 std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
319 result = new ModuleBase_WidgetLineEdit( theParent, myWidgetApi, aPlaceHolder );
320 } else if (theType == WDG_EXPR_EDITOR) {
321 std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
322 result = new ModuleBase_WidgetExprEditor( theParent, myWidgetApi, aPlaceHolder );
323 } else if (theType == WDG_MULTISELECTOR) {
324 result = new ModuleBase_WidgetMultiSelector(theParent, myWorkshop, myWidgetApi);
325 } else if (theType == WDG_CONCEALED_OBJECTS_VIEW) {
326 result = new ModuleBase_WidgetConcealedObjects(theParent, myWidgetApi);
327 } else if (theType == WDG_TOOLBOX) {
328 result = new ModuleBase_WidgetToolbox(theParent, myWidgetApi);
329 } else if (theType == WDG_SWITCH) {
330 result = new ModuleBase_WidgetSwitch(theParent, myWidgetApi);
331 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE ||
332 theType == NODE_VALIDATOR) {
333 // Do nothing for "box" and "case"
335 } else if (theType == WDG_ACTION) {
336 result = new ModuleBase_WidgetAction(theParent, myWidgetApi);
338 result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi);
340 result = ModuleBase_WidgetCreatorFactory::get()->createWidgetByType(theType, theParent,
341 myWidgetApi, myWorkshop);
344 qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type %s", theType.c_str());
349 myModelWidgets.append(result);
353 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString)
355 return QString::fromStdString(theStdString);