1 // Copyright (C) 2014-2019 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 email : webmaster.salome@opencascade.com
20 #include <ModuleBase_WidgetFactory.h>
21 #include <ModuleBase_IconFactory.h>
23 #include <ModuleBase_Operation.h>
24 #include <ModuleBase_OperationDescription.h>
25 #include <ModuleBase_WidgetEditor.h>
26 #include <ModuleBase_WidgetSwitch.h>
27 #include <ModuleBase_WidgetShapeSelector.h>
28 #include <ModuleBase_WidgetFeatureSelector.h>
29 #include <ModuleBase_WidgetDoubleValue.h>
30 #include <ModuleBase_WidgetLabelValue.h>
31 #include <ModuleBase_WidgetIntValue.h>
32 #include <ModuleBase_WidgetBoolValue.h>
33 #include <ModuleBase_WidgetFileSelector.h>
34 #include <ModuleBase_WidgetChoice.h>
35 #include <ModuleBase_IWorkshop.h>
36 #include <ModuleBase_IModule.h>
37 #include <ModuleBase_Tools.h>
38 #include <ModuleBase_WidgetLineEdit.h>
39 #include <ModuleBase_WidgetNameEdit.h>
40 #include <ModuleBase_WidgetMultiSelector.h>
41 #include <ModuleBase_WidgetConcealedObjects.h>
42 #include <ModuleBase_WidgetLabel.h>
43 #include <ModuleBase_WidgetToolbox.h>
44 #include <ModuleBase_WidgetRadiobox.h>
45 #include <ModuleBase_PageBase.h>
46 #include <ModuleBase_PageGroupBox.h>
47 #include <ModuleBase_WidgetOptionalBox.h>
48 #include <ModuleBase_PageWidget.h>
49 #include <ModuleBase_WidgetExprEditor.h>
50 #include <ModuleBase_WidgetCreatorFactory.h>
51 #include <ModuleBase_WidgetAction.h>
52 #include <ModuleBase_WidgetPointInput.h>
54 #include <ModelAPI_Validator.h>
55 #include <ModelAPI_Session.h>
57 #include <Config_Keywords.h>
58 #include <Config_WidgetAPI.h>
61 #include <QHBoxLayout>
62 #include <QGridLayout>
64 #include <QMetaProperty>
77 ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,
78 ModuleBase_IWorkshop* theWorkshop)
79 : myWorkshop(theWorkshop)
81 myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
84 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
89 void ModuleBase_WidgetFactory::createWidget(ModuleBase_PageBase* thePage, bool alignToTop)
91 std::string aWType = myWidgetApi->widgetType();
92 if (aWType == NODE_FEATURE) {
93 // if XML definition of the feature contains the next key, the widgets should not be created,
94 // but a specific panel should be made. However, to provide persistent of the panel values,
95 // we need to get into the panel the feature of the operation. As a result this panel should
96 // be created after the feature creating(create operation). The method setPanel() of this
97 // class is used for this. Here, we just return to avoid the widgets creation.
98 std::string aPanelName = myWidgetApi->getProperty(PROPERTY_PANEL_ID);
99 if (!aPanelName.empty())
103 if (!myWidgetApi->toChildWidget())
106 do { //Iterate over each node
107 std::string aWdgType = myWidgetApi->widgetType();
108 // Create PageGroup TODO: extract
109 if (myWidgetApi->isGroupBoxWidget() ||
110 ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
112 //if current widget is groupbox (container) process it's children recursively
113 ModuleBase_PageBase* aPage = createPageByType(aWdgType, thePage->pageWidget());
116 thePage->addPageWidget(aPage);
118 // Create a ModelWidget
119 ModuleBase_ModelWidget* aWidget = createWidgetByType(aWdgType, thePage->pageWidget());
121 if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {
122 thePage->addModelWidget(aWidget);
124 aWidget->setVisible(false);
127 // Create PagedContainer TODO: extract
128 if (myWidgetApi->isPagedWidget()) {
129 //If current widget is toolbox or switch-casebox then fetch all
130 //it's pages recursively and setup into the widget.
131 if (myWidgetApi->toChildWidget()) {
133 QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
134 QString aTooltip = qs(myWidgetApi->getProperty(FEATURE_TOOLTIP));
135 QString aCaseId = qs(myWidgetApi->getProperty(_ID));
136 ModuleBase_PageBase* aPage = new ModuleBase_PageWidget(aWidget);
138 if (aWdgType == WDG_SWITCH || aWdgType == WDG_TOOLBOX || aWdgType == WDG_RADIOBOX) {
139 ModuleBase_PagedContainer* aContainer =
140 qobject_cast<ModuleBase_PagedContainer*>(aWidget);
142 QString anIconPath = qs( myWidgetApi->getProperty( CONTAINER_PAGE_ICON ) );
143 QPixmap anIcon = ModuleBase_IconFactory::loadPixmap( anIconPath );
144 aContainer->addPage( aPage, aPageName, aCaseId, anIcon, aTooltip);
146 } while (myWidgetApi->toNextWidget());
150 } while (myWidgetApi->toNextWidget());
153 thePage->alignToTop();
156 void ModuleBase_WidgetFactory::createPanel(ModuleBase_PageBase* thePage,
157 const FeaturePtr& theFeature)
159 std::string aPanelName = myWidgetApi->getProperty(PROPERTY_PANEL_ID);
160 if (!aPanelName.empty() && ModuleBase_WidgetCreatorFactory::get()->hasPanelWidget(aPanelName)) {
161 QWidget* aPanel = ModuleBase_WidgetCreatorFactory::get()->createPanelByType(aPanelName,
162 thePage->pageWidget(), theFeature);
163 thePage->addWidget(aPanel);
164 thePage->alignToTop();
168 void ModuleBase_WidgetFactory::createWidget(ModuleBase_PageBase* thePage,
169 const std::string& theWidgetId)
172 moveToWidgetId(theWidgetId, aFound);
174 std::string aWdgType = myWidgetApi->widgetType();
176 // Create a ModelWidget
177 ModuleBase_ModelWidget* aWidget = createWidgetByType(aWdgType, thePage->pageWidget());
179 if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {
180 thePage->addModelWidget(aWidget);
183 aWidget->setVisible(false);
187 thePage->alignToTop();
190 void ModuleBase_WidgetFactory::getAttributeTitle(const std::string& theAttributeId,
191 std::string& theTitle)
194 moveToWidgetId(theAttributeId, aFound);
196 theTitle = QString::fromStdString(myWidgetApi->widgetLabel()).toStdString().c_str();
197 if (theTitle.empty())
199 QString::fromStdString(myWidgetApi->getProperty(CONTAINER_PAGE_NAME)).toStdString().c_str();
203 void ModuleBase_WidgetFactory::getGreedAttribute(std::string& theAttributeId)
205 if (!theAttributeId.empty())
208 if (!myWidgetApi->toChildWidget())
211 do { //Iterate over each node
212 std::string aWdgType = myWidgetApi->widgetType();
213 // Find title under PageGroup
214 if (myWidgetApi->isGroupBoxWidget() ||
215 ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
216 getGreedAttribute(theAttributeId);
220 std::string anAttributeId = myWidgetApi->widgetId();
221 if (myWidgetApi->getBooleanAttribute(ATTR_GREED, false))
222 theAttributeId = anAttributeId;
223 if (theAttributeId.empty() && myWidgetApi->isPagedWidget()) {
224 //If current widget is toolbox or switch-casebox then fetch all
225 //it's pages recursively and setup into the widget.
226 if (myWidgetApi->toChildWidget()) {
228 getGreedAttribute(theAttributeId);
229 } while (theAttributeId.empty() && myWidgetApi->toNextWidget());
233 } while (theAttributeId.empty() && myWidgetApi->toNextWidget());
236 void ModuleBase_WidgetFactory::moveToWidgetId(const std::string& theWidgetId, bool& theFound)
241 if (!myWidgetApi->toChildWidget())
244 do { //Iterate over each node
245 std::string aWdgType = myWidgetApi->widgetType();
246 // Find title under PageGroup
247 if (myWidgetApi->isGroupBoxWidget() ||
248 ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
249 moveToWidgetId(theWidgetId, theFound);
253 std::string anAttributeId = myWidgetApi->widgetId();
254 theFound = anAttributeId == theWidgetId;
255 if (!theFound && myWidgetApi->isPagedWidget()) {
256 //If current widget is toolbox or switch-casebox then fetch all
257 //it's pages recursively and setup into the widget.
258 if (myWidgetApi->toChildWidget()) {
260 moveToWidgetId(theWidgetId, theFound);
261 } while (!theFound && myWidgetApi->toNextWidget());
265 } while (!theFound && myWidgetApi->toNextWidget());
268 ModuleBase_PageBase* ModuleBase_WidgetFactory::createPageByType(const std::string& theType,
271 ModuleBase_PageBase* aResult = NULL;
273 if (theType == WDG_GROUP) {
274 QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
275 ModuleBase_PageGroupBox* aPage = new ModuleBase_PageGroupBox(theParent);
276 aPage->setTitle(aGroupName);
279 else if (theType == WDG_OPTIONALBOX) {
280 ModuleBase_WidgetOptionalBox* aPage = new ModuleBase_WidgetOptionalBox(theParent,
285 aResult = ModuleBase_WidgetCreatorFactory::get()->createPageByType(theType, theParent,
288 ModuleBase_ModelWidget* aWidget = dynamic_cast<ModuleBase_ModelWidget*>(aResult);
290 myModelWidgets.append(aWidget);
295 ModuleBase_ModelWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
298 ModuleBase_ModelWidget* result = NULL;
300 if (theType == WDG_INFO) {
301 result = new ModuleBase_WidgetLabel(theParent, myWidgetApi);
302 } else if (theType == WDG_DOUBLEVALUE) {
303 result = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi);
304 } else if (theType == WDG_DOUBLEVALUELABEL) {
305 result = new ModuleBase_WidgetLabelValue(theParent, myWidgetApi);
306 } else if (theType == WDG_INTEGERVALUE) {
307 result = new ModuleBase_WidgetIntValue(theParent, myWidgetApi);
308 } else if (theType == WDG_SHAPE_SELECTOR) {
309 result = new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi);
310 } else if (theType == WDG_FEATURE_SELECTOR) {
311 result = new ModuleBase_WidgetFeatureSelector(theParent, myWorkshop, myWidgetApi);
312 } else if (theType == WDG_BOOLVALUE) {
313 result = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi);
314 //} else if (theType == WDG_DOUBLEVALUE_EDITOR) {
315 // result = new ModuleBase_WidgetEditor(theParent, myWidgetApi);
316 } else if (theType == WDG_FILE_SELECTOR) {
317 result = new ModuleBase_WidgetFileSelector(theParent, myWidgetApi);
318 } else if (theType == WDG_CHOICE) {
319 result = new ModuleBase_WidgetChoice(theParent, myWidgetApi);
320 } else if (theType == WDG_STRINGVALUE) {
321 std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
322 result = new ModuleBase_WidgetLineEdit( theParent, myWidgetApi, aPlaceHolder );
323 } else if (theType == WDG_NAMEVALUE) {
324 std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
325 result = new ModuleBase_WidgetNameEdit( theParent, myWidgetApi, aPlaceHolder );
326 } else if (theType == WDG_EXPR_EDITOR) {
327 std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
328 result = new ModuleBase_WidgetExprEditor( theParent, myWidgetApi, aPlaceHolder );
329 } else if (theType == WDG_MULTISELECTOR) {
330 result = new ModuleBase_WidgetMultiSelector(theParent, myWorkshop, myWidgetApi);
331 } else if (theType == WDG_CONCEALED_OBJECTS_VIEW) {
332 result = new ModuleBase_WidgetConcealedObjects(theParent, myWidgetApi);
333 } else if (theType == WDG_TOOLBOX) {
334 result = new ModuleBase_WidgetToolbox(theParent, myWidgetApi);
335 } else if (theType == WDG_RADIOBOX) {
336 result = new ModuleBase_WidgetRadiobox(theParent, myWidgetApi);
337 } else if (theType == WDG_SWITCH) {
338 result = new ModuleBase_WidgetSwitch(theParent, myWidgetApi);
339 } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE ||
340 theType == NODE_VALIDATOR) {
341 // Do nothing for "box" and "case"
343 } else if (theType == WDG_ACTION) {
344 result = new ModuleBase_WidgetAction(theParent, myWidgetApi);
345 } else if (theType == WDG_POINT_INPUT) {
346 result = new ModuleBase_WidgetPointInput(theParent, myWorkshop, myWidgetApi);
348 result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi);
350 result = ModuleBase_WidgetCreatorFactory::get()->createWidgetByType(theType, theParent,
351 myWidgetApi, myWorkshop);
354 qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type %s", theType.c_str());
359 myModelWidgets.append(result);
363 QString ModuleBase_WidgetFactory::qs(const std::string& theStdString)
365 return QString::fromStdString(theStdString);