Salome HOME
Feature selector widget for recover feature.
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetFactory.cpp
index eedb3a390759f687d552ed6e15fcbe8a7d0e23aa..c0045ec1da324153e01be47d221c1636d766195e 100644 (file)
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-
-/*
- * ModuleBase_WidgetFactory.cpp
- *
- *  Created on: Apr 3, 2014
- *      Author: sbh
- */
-
-#include <ModuleBase_WidgetFactory.h>
-
-#include <ModuleBase_Operation.h>
-#include <ModuleBase_OperationDescription.h>
-#include <ModuleBase_WidgetEditor.h>
-#include <ModuleBase_WidgetSwitch.h>
-#include <ModuleBase_WidgetShapeSelector.h>
-#include <ModuleBase_WidgetDoubleValue.h>
-#include <ModuleBase_WidgetBoolValue.h>
-#include <ModuleBase_WidgetFileSelector.h>
-#include <ModuleBase_WidgetChoice.h>
-#include <ModuleBase_IWorkshop.h>
-#include <ModuleBase_IModule.h>
-#include <ModuleBase_Tools.h>
-#include <ModuleBase_WidgetLineEdit.h>
-#include <ModuleBase_WidgetMultiSelector.h>
-#include <ModuleBase_WidgetLabel.h>
-#include <ModuleBase_WidgetToolbox.h>
-#include <ModuleBase_PageBase.h>
-#include <ModuleBase_PageGroupBox.h>
-#include <ModuleBase_PageWidget.h>
-#include <ModuleBase_WidgetExprEditor.h>
-
-#include <ModelAPI_Validator.h>
-#include <ModelAPI_Session.h>
-
-#include <Config_Keywords.h>
-#include <Config_WidgetAPI.h>
-
-#include <QWidget>
-#include <QHBoxLayout>
-#include <QGridLayout>
-#include <QSpinBox>
-#include <QMetaProperty>
-#include <QLabel>
-#include <QPixmap>
-#include <QGroupBox>
-#include <QToolBox>
-
-#ifdef _DEBUG
-#include <QDebug>
-#endif
-
-#include <cfloat>
-#include <climits>
-
-ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,
-                                                   ModuleBase_IWorkshop* theWorkshop)
-    : myWorkshop(theWorkshop)
-{
-  myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);
-}
-
-ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
-{
-  delete myWidgetApi;
-}
-
-void ModuleBase_WidgetFactory::createWidget(ModuleBase_PageBase* thePage)
-{
-  myParentId = myWidgetApi->widgetId();
-  if (!myWidgetApi->toChildWidget())
-    return;
-
-  do {  //Iterate over each node
-    std::string aWdgType = myWidgetApi->widgetType();
-    // Create PageGroup TODO: extract
-    if (myWidgetApi->isGroupBoxWidget()) {
-      //if current widget is groupbox (container) process it's children recursively
-      QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
-      ModuleBase_PageGroupBox* aPage = new ModuleBase_PageGroupBox(thePage->pageWidget());
-      aPage->setTitle(aGroupName);
-      createWidget(aPage);
-      thePage->addPageWidget(aPage);
-    } else {
-      // Create a ModelWidget
-      ModuleBase_ModelWidget* aWidget = createWidgetByType(aWdgType, thePage->pageWidget());
-      if (aWidget) {
-        if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {
-          thePage->addModelWidget(aWidget);
-        } else {
-          aWidget->setVisible(false);
-        }
-      }
-      // Create PagedContainer TODO: extract
-      if (myWidgetApi->isPagedWidget()) {
-        //If current widget is toolbox or switch-casebox then fetch all
-        //it's pages recursively and setup into the widget.
-        myWidgetApi->toChildWidget();
-        do {
-          QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
-          QString aCaseId = qs(myWidgetApi->getProperty(_ID));
-          ModuleBase_PageBase* aPage = new ModuleBase_PageWidget(aWidget);
-          createWidget(aPage);
-          if (aWdgType == WDG_SWITCH || aWdgType == WDG_TOOLBOX) {
-            ModuleBase_PagedContainer* aContainer = qobject_cast<ModuleBase_PagedContainer*>(aWidget);
-            aContainer->addPage(aPage, aPageName, aCaseId);
-          }
-        } while (myWidgetApi->toNextWidget());
-      }
-    }
-  } while (myWidgetApi->toNextWidget());
-
-  thePage->alignToTop();
-}
-
-ModuleBase_ModelWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
-                                                                     QWidget* theParent)
-{
-  ModuleBase_ModelWidget* result = NULL;
-
-  if (theType == WDG_INFO) {
-    result = new ModuleBase_WidgetLabel(theParent, myWidgetApi, myParentId);
-  } else if (theType == WDG_DOUBLEVALUE) {
-    result = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi, myParentId);
-  } else if (theType == WDG_SHAPE_SELECTOR) {
-    result = new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi, myParentId);
-  } else if (theType == WDG_BOOLVALUE) {
-    result = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi, myParentId);
-  } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
-    result = new ModuleBase_WidgetEditor(theParent, myWidgetApi, myParentId);
-  } else if (theType == WDG_FILE_SELECTOR) {
-    result = new ModuleBase_WidgetFileSelector(theParent, myWidgetApi, myParentId);
-  } else if (theType == WDG_CHOICE) {
-    result = new ModuleBase_WidgetChoice(theParent, myWidgetApi, myParentId);
-  } else if (theType == WDG_STRINGVALUE) {
-    result = new ModuleBase_WidgetLineEdit(theParent, myWidgetApi, myParentId);
-  } else if (theType == WDG_EXPR_EDITOR) {
-    result = new ModuleBase_WidgetExprEditor(theParent, myWidgetApi, myParentId);
-  } else if (theType == WDG_MULTISELECTOR) {
-    result = new ModuleBase_WidgetMultiSelector(theParent, myWorkshop, myWidgetApi, myParentId);
-  } else if (theType == WDG_TOOLBOX) {
-    result = new ModuleBase_WidgetToolbox(theParent, myWidgetApi, myParentId);
-  } else if (theType == WDG_SWITCH) {
-    result = new ModuleBase_WidgetSwitch(theParent, myWidgetApi, myParentId);
-  } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
-    // Do nothing for "box" and "case"
-    result = NULL;
-  } else {
-    result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi, myParentId);
-    #ifdef _DEBUG
-    if (!result) {
-      qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type");
-    }
-    #endif
-  }
-  if (result) {
-    myModelWidgets.append(result);
-  }
-  return result;
-}
-
-QString ModuleBase_WidgetFactory::qs(const std::string& theStdString)
-{
-  return QString::fromStdString(theStdString);
-}
-
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D\r
+\r
+/*\r
+ * ModuleBase_WidgetFactory.cpp\r
+ *\r
+ *  Created on: Apr 3, 2014\r
+ *      Author: sbh\r
+ */\r
+\r
+#include <ModuleBase_WidgetFactory.h>\r
+#include <ModuleBase_IconFactory.h>\r
+\r
+#include <ModuleBase_Operation.h>\r
+#include <ModuleBase_OperationDescription.h>\r
+#include <ModuleBase_WidgetEditor.h>\r
+#include <ModuleBase_WidgetSwitch.h>\r
+#include <ModuleBase_WidgetShapeSelector.h>\r
+#include <ModuleBase_WidgetFeatureSelector.h>\r
+#include <ModuleBase_WidgetDoubleValue.h>\r
+#include <ModuleBase_WidgetIntValue.h>\r
+#include <ModuleBase_WidgetBoolValue.h>\r
+#include <ModuleBase_WidgetFileSelector.h>\r
+#include <ModuleBase_WidgetChoice.h>\r
+#include <ModuleBase_IWorkshop.h>\r
+#include <ModuleBase_IModule.h>\r
+#include <ModuleBase_Tools.h>\r
+#include <ModuleBase_WidgetLineEdit.h>\r
+#include <ModuleBase_WidgetMultiSelector.h>\r
+#include <ModuleBase_WidgetConcealedObjects.h>\r
+#include <ModuleBase_WidgetLabel.h>\r
+#include <ModuleBase_WidgetToolbox.h>\r
+#include <ModuleBase_PageBase.h>\r
+#include <ModuleBase_PageGroupBox.h>\r
+#include <ModuleBase_WidgetOptionalBox.h>\r
+#include <ModuleBase_PageWidget.h>\r
+#include <ModuleBase_WidgetExprEditor.h>\r
+#include <ModuleBase_WidgetCreatorFactory.h>\r
+#include <ModuleBase_WidgetAction.h>\r
+\r
+#include <ModelAPI_Validator.h>\r
+#include <ModelAPI_Session.h>\r
+\r
+#include <Config_Keywords.h>\r
+#include <Config_WidgetAPI.h>\r
+\r
+#include <QWidget>\r
+#include <QHBoxLayout>\r
+#include <QGridLayout>\r
+#include <QSpinBox>\r
+#include <QMetaProperty>\r
+#include <QLabel>\r
+#include <QPixmap>\r
+#include <QGroupBox>\r
+#include <QToolBox>\r
+\r
+#ifdef _DEBUG\r
+#include <QDebug>\r
+#endif\r
+\r
+#include <cfloat>\r
+#include <climits>\r
+\r
+ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepresentation,\r
+                                                   ModuleBase_IWorkshop* theWorkshop)\r
+    : myWorkshop(theWorkshop)\r
+{\r
+  myWidgetApi = new Config_WidgetAPI(theXmlRepresentation);\r
+}\r
+\r
+ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()\r
+{\r
+  delete myWidgetApi;\r
+}\r
+\r
+void ModuleBase_WidgetFactory::createWidget(ModuleBase_PageBase* thePage, bool alignToTop)\r
+{\r
+  std::string aWType = myWidgetApi->widgetType();\r
+  if (aWType == NODE_FEATURE) {\r
+    // if XML definition of the feature contains the next key, the widgets should not be created,\r
+    // but a specific panel should be made. However, to provide persistent of the panel values,\r
+    // we need to get into the panel the feature of the operation. As a result this panel should\r
+    // be created after the feature creating(create operation). The method setPanel() of this\r
+    // class is used for this. Here, we just return to avoid the widgets creation.\r
+    std::string aPanelName = myWidgetApi->getProperty(PROPERTY_PANEL_ID);\r
+    if (!aPanelName.empty())\r
+      return;\r
+  }\r
+\r
+  if (!myWidgetApi->toChildWidget())\r
+    return;\r
+\r
+  do {  //Iterate over each node\r
+    std::string aWdgType = myWidgetApi->widgetType();\r
+    // Create PageGroup TODO: extract\r
+    if (myWidgetApi->isGroupBoxWidget() ||\r
+        ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {\r
+\r
+      //if current widget is groupbox (container) process it's children recursively\r
+      ModuleBase_PageBase* aPage = createPageByType(aWdgType, thePage->pageWidget());\r
+\r
+      createWidget(aPage);\r
+      thePage->addPageWidget(aPage);\r
+    } else {\r
+      // Create a ModelWidget\r
+      ModuleBase_ModelWidget* aWidget = createWidgetByType(aWdgType, thePage->pageWidget());\r
+      if (aWidget) {\r
+        if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {\r
+          thePage->addModelWidget(aWidget);\r
+        } else {\r
+          aWidget->setVisible(false);\r
+        }\r
+      }\r
+      // Create PagedContainer TODO: extract\r
+      if (myWidgetApi->isPagedWidget()) {\r
+        //If current widget is toolbox or switch-casebox then fetch all\r
+        //it's pages recursively and setup into the widget.\r
+        if (myWidgetApi->toChildWidget()) {\r
+          do {\r
+            QString aPageName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));\r
+            QString aCaseId = qs(myWidgetApi->getProperty(_ID));\r
+            ModuleBase_PageBase* aPage = new ModuleBase_PageWidget(aWidget);\r
+            createWidget(aPage);\r
+            if (aWdgType == WDG_SWITCH || aWdgType == WDG_TOOLBOX) {\r
+              ModuleBase_PagedContainer* aContainer = qobject_cast<ModuleBase_PagedContainer*>(aWidget);\r
+\r
+              QString anIconPath = qs( myWidgetApi->getProperty( CONTAINER_PAGE_ICON ) );\r
+              QPixmap anIcon = ModuleBase_IconFactory::loadPixmap( anIconPath );\r
+              aContainer->addPage( aPage, aPageName, aCaseId, anIcon );\r
+            }\r
+          } while (myWidgetApi->toNextWidget());\r
+        }\r
+      }\r
+    }\r
+  } while (myWidgetApi->toNextWidget());\r
+\r
+  if (alignToTop)\r
+    thePage->alignToTop();\r
+}\r
+\r
+void ModuleBase_WidgetFactory::createPanel(ModuleBase_PageBase* thePage,\r
+                                           const FeaturePtr& theFeature)\r
+{\r
+  std::string aPanelName = myWidgetApi->getProperty(PROPERTY_PANEL_ID);\r
+  if (!aPanelName.empty() && ModuleBase_WidgetCreatorFactory::get()->hasPanelWidget(aPanelName)) {\r
+    QWidget* aPanel = ModuleBase_WidgetCreatorFactory::get()->createPanelByType(aPanelName,\r
+                                                               thePage->pageWidget(), theFeature);\r
+    thePage->addWidget(aPanel);\r
+    thePage->alignToTop();\r
+  }\r
+}\r
+\r
+void ModuleBase_WidgetFactory::createWidget(ModuleBase_PageBase* thePage,\r
+                                            const std::string& theWidgetId)\r
+{\r
+  bool aFound = false;\r
+  moveToWidgetId(theWidgetId, aFound);\r
+  if (aFound) {\r
+    std::string aWdgType = myWidgetApi->widgetType();\r
+\r
+    // Create a ModelWidget\r
+    ModuleBase_ModelWidget* aWidget = createWidgetByType(aWdgType, thePage->pageWidget());\r
+    if (aWidget) {\r
+      if (!myWidgetApi->getBooleanAttribute(ATTR_INTERNAL, false)) {\r
+        thePage->addModelWidget(aWidget);\r
+      }\r
+      else {\r
+        aWidget->setVisible(false);\r
+      }\r
+    }\r
+  }\r
+  thePage->alignToTop();\r
+}\r
+\r
+void ModuleBase_WidgetFactory::getAttributeTitle(const std::string& theAttributeId,\r
+                                                 std::string& theTitle)\r
+{\r
+  bool aFound = false;\r
+  moveToWidgetId(theAttributeId, aFound);\r
+  if (aFound) {\r
+    theTitle = QString::fromStdString(myWidgetApi->widgetLabel()).toStdString().c_str();\r
+    if (theTitle.empty())\r
+      theTitle = QString::fromStdString(myWidgetApi->getProperty(CONTAINER_PAGE_NAME)).toStdString().c_str();\r
+  }\r
+}\r
+\r
+void ModuleBase_WidgetFactory::getGreedAttribute(std::string& theAttributeId)\r
+{\r
+  if (!theAttributeId.empty())\r
+    return;\r
+\r
+  if (!myWidgetApi->toChildWidget())\r
+    return;\r
+\r
+  do {  //Iterate over each node\r
+    std::string aWdgType = myWidgetApi->widgetType();\r
+    // Find title under PageGroup\r
+    if (myWidgetApi->isGroupBoxWidget() ||\r
+      ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {\r
+      getGreedAttribute(theAttributeId);\r
+    }\r
+    else {\r
+      // Find title here\r
+      std::string anAttributeId = myWidgetApi->widgetId();\r
+      if (myWidgetApi->getBooleanAttribute(ATTR_GREED, false))\r
+        theAttributeId = anAttributeId;\r
+      if (theAttributeId.empty() && myWidgetApi->isPagedWidget()) {\r
+        //If current widget is toolbox or switch-casebox then fetch all\r
+        //it's pages recursively and setup into the widget.\r
+        if (myWidgetApi->toChildWidget()) {\r
+          do {\r
+            getGreedAttribute(theAttributeId);\r
+          } while (theAttributeId.empty() && myWidgetApi->toNextWidget());\r
+        }\r
+      }\r
+    }\r
+  } while (theAttributeId.empty() && myWidgetApi->toNextWidget());\r
+}\r
+\r
+void ModuleBase_WidgetFactory::moveToWidgetId(const std::string& theWidgetId, bool& theFound)\r
+{\r
+  if (theFound)\r
+    return;\r
+\r
+  if (!myWidgetApi->toChildWidget())\r
+    return;\r
+\r
+  do {  //Iterate over each node\r
+    std::string aWdgType = myWidgetApi->widgetType();\r
+    // Find title under PageGroup\r
+    if (myWidgetApi->isGroupBoxWidget() ||\r
+      ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {\r
+      moveToWidgetId(theWidgetId, theFound);\r
+    }\r
+    else {\r
+      // Find title here\r
+      std::string anAttributeId = myWidgetApi->widgetId();\r
+      theFound = anAttributeId == theWidgetId;\r
+      if (!theFound && myWidgetApi->isPagedWidget()) {\r
+        //If current widget is toolbox or switch-casebox then fetch all\r
+        //it's pages recursively and setup into the widget.\r
+        if (myWidgetApi->toChildWidget()) {\r
+          do {\r
+            moveToWidgetId(theWidgetId, theFound);\r
+          } while (!theFound && myWidgetApi->toNextWidget());\r
+        }\r
+      }\r
+    }\r
+  } while (!theFound && myWidgetApi->toNextWidget());\r
+}\r
+\r
+ModuleBase_PageBase* ModuleBase_WidgetFactory::createPageByType(const std::string& theType,\r
+                                                                QWidget* theParent)\r
+{\r
+  ModuleBase_PageBase* aResult = NULL;\r
+\r
+  if (theType == WDG_GROUP) {\r
+    QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));\r
+    ModuleBase_PageGroupBox* aPage = new ModuleBase_PageGroupBox(theParent);\r
+    aPage->setTitle(aGroupName);\r
+    aResult = aPage;\r
+  }\r
+  else if (theType == WDG_OPTIONALBOX) {\r
+    ModuleBase_WidgetOptionalBox* aPage = new ModuleBase_WidgetOptionalBox(theParent,\r
+                                                                myWidgetApi);\r
+    aResult = aPage;\r
+  }\r
+  if (!aResult)\r
+    aResult = ModuleBase_WidgetCreatorFactory::get()->createPageByType(theType, theParent,\r
+                                                                       myWidgetApi);\r
+\r
+  ModuleBase_ModelWidget* aWidget = dynamic_cast<ModuleBase_ModelWidget*>(aResult);\r
+  if (aWidget)\r
+    myModelWidgets.append(aWidget);\r
+\r
+  return aResult;\r
+}\r
+\r
+ModuleBase_ModelWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,\r
+                                                                     QWidget* theParent)\r
+{\r
+  ModuleBase_ModelWidget* result = NULL;\r
+\r
+  if (theType == WDG_INFO) {\r
+    result = new ModuleBase_WidgetLabel(theParent, myWidgetApi);\r
+  } else if (theType == WDG_DOUBLEVALUE) {\r
+    result = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi);\r
+  } else if (theType == WDG_INTEGERVALUE) {\r
+    result = new ModuleBase_WidgetIntValue(theParent, myWidgetApi);\r
+  } else if (theType == WDG_SHAPE_SELECTOR) {\r
+    result = new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi);\r
+  } else if (theType == WDG_FEATURE_SELECTOR) {\r
+    result = new ModuleBase_WidgetFeatureSelector(theParent, myWorkshop, myWidgetApi);\r
+  } else if (theType == WDG_BOOLVALUE) {\r
+    result = new ModuleBase_WidgetBoolValue(theParent, myWidgetApi);\r
+  //} else if (theType == WDG_DOUBLEVALUE_EDITOR) {\r
+  //  result = new ModuleBase_WidgetEditor(theParent, myWidgetApi);\r
+  } else if (theType == WDG_FILE_SELECTOR) {\r
+    result = new ModuleBase_WidgetFileSelector(theParent, myWidgetApi);\r
+  } else if (theType == WDG_CHOICE) {\r
+    result = new ModuleBase_WidgetChoice(theParent, myWidgetApi);\r
+  } else if (theType == WDG_STRINGVALUE) {\r
+    std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );\r
+    result = new ModuleBase_WidgetLineEdit( theParent, myWidgetApi, aPlaceHolder );\r
+  } else if (theType == WDG_EXPR_EDITOR) {\r
+    std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );\r
+    result = new ModuleBase_WidgetExprEditor( theParent, myWidgetApi, aPlaceHolder );\r
+  } else if (theType == WDG_MULTISELECTOR) {\r
+    result = new ModuleBase_WidgetMultiSelector(theParent, myWorkshop, myWidgetApi);\r
+  } else if (theType == WDG_CONCEALED_OBJECTS_VIEW) {\r
+    result = new ModuleBase_WidgetConcealedObjects(theParent, myWidgetApi);\r
+  } else if (theType == WDG_TOOLBOX) {\r
+    result = new ModuleBase_WidgetToolbox(theParent, myWidgetApi);\r
+  } else if (theType == WDG_SWITCH) {\r
+    result = new ModuleBase_WidgetSwitch(theParent, myWidgetApi);\r
+  } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE ||\r
+             theType == NODE_VALIDATOR) {\r
+    // Do nothing for "box" and "case"\r
+    result = NULL;\r
+  } else if (theType == WDG_ACTION) {\r
+    result = new ModuleBase_WidgetAction(theParent, myWidgetApi);\r
+  } else {\r
+    result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi);\r
+    if (!result)\r
+      result = ModuleBase_WidgetCreatorFactory::get()->createWidgetByType(theType, theParent,\r
+                                                              myWidgetApi, myWorkshop);\r
+    #ifdef _DEBUG\r
+    if (!result) {\r
+      qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type %s", theType.c_str());\r
+    }\r
+    #endif\r
+  }\r
+  if (result)\r
+    myModelWidgets.append(result);\r
+  return result;\r
+}\r
+\r
+QString ModuleBase_WidgetFactory::qs(const std::string& theStdString)\r
+{\r
+  return QString::fromStdString(theStdString);\r
+}\r
+\r