Salome HOME
Issue #1302 Restricting preselection to the first argument only: mirror feature has...
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetFactory.cpp
index cbe7503a8e7a43833324fc3c275308d389ebf3db..267ab17b5468e28aa64ed4b1142689fdebce4421 100644 (file)
@@ -1,3 +1,5 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
 /*
  * ModuleBase_WidgetFactory.cpp
  *
 
 #include <ModuleBase_Operation.h>
 #include <ModuleBase_OperationDescription.h>
-#include <ModuleBase_WidgetPoint2D.h>
-#include <ModuleBase_WidgetFeatureOrAttribute.h>
-#include <ModuleBase_WidgetFeature.h>
 #include <ModuleBase_WidgetEditor.h>
 #include <ModuleBase_WidgetSwitch.h>
 #include <ModuleBase_WidgetShapeSelector.h>
 #include <ModuleBase_WidgetDoubleValue.h>
+#include <ModuleBase_WidgetIntValue.h>
 #include <ModuleBase_WidgetBoolValue.h>
-#include <ModuleBase_WidgetPoint2dDistance.h>
 #include <ModuleBase_WidgetFileSelector.h>
 #include <ModuleBase_WidgetChoice.h>
 #include <ModuleBase_IWorkshop.h>
 #include <ModuleBase_Tools.h>
 #include <ModuleBase_WidgetLineEdit.h>
 #include <ModuleBase_WidgetMultiSelector.h>
+#include <ModuleBase_WidgetLabel.h>
+#include <ModuleBase_WidgetErrorLabel.h>
+#include <ModuleBase_WidgetToolbox.h>
+#include <ModuleBase_PageBase.h>
+#include <ModuleBase_PageGroupBox.h>
+#include <ModuleBase_WidgetCheckGroupBox.h>
+#include <ModuleBase_PageWidget.h>
+#include <ModuleBase_WidgetExprEditor.h>
+#include <ModuleBase_WidgetCreatorFactory.h>
 
 #include <ModelAPI_Validator.h>
+#include <ModelAPI_Session.h>
 
 #include <Config_Keywords.h>
 #include <Config_WidgetAPI.h>
@@ -57,265 +66,246 @@ ModuleBase_WidgetFactory::ModuleBase_WidgetFactory(const std::string& theXmlRepr
 
 ModuleBase_WidgetFactory::~ModuleBase_WidgetFactory()
 {
+  delete myWidgetApi;
 }
 
-void ModuleBase_WidgetFactory::createWidget(QWidget* theParent)
+void ModuleBase_WidgetFactory::createWidget(ModuleBase_PageBase* thePage)
 {
   myParentId = myWidgetApi->widgetId();
   if (!myWidgetApi->toChildWidget())
     return;
 
-  QVBoxLayout* aWidgetLay = new QVBoxLayout(theParent);
   do {  //Iterate over each node
     std::string aWdgType = myWidgetApi->widgetType();
-    //Create a widget (doublevalue, groupbox, toolbox, etc.
-    QWidget* aWidget = createWidgetByType(aWdgType, theParent);
-    if (aWidget) {
-      if (!myWidgetApi->getBooleanAttribute(ATTRIBUTE_INTERNAL, false)) {
-        aWidgetLay->addWidget(aWidget);
-      } else {
-        aWidget->setVisible(false);
-      }
-    }
-    if (myWidgetApi->isContainerWidget()) {
+    // Create PageGroup TODO: extract
+    if (myWidgetApi->isGroupBoxWidget() ||
+        ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
+
       //if current widget is groupbox (container) process it's children recursively
-      QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
-      createWidget(aWidget);
-      ModuleBase_Tools::adjustMargins(aWidget);
-      QGroupBox* aGrBox = qobject_cast<QGroupBox*>(aWidget);
-      aGrBox->setTitle(aGroupName);
-    }
-    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));
-        QWidget* aPage = new QWidget(aWidget);
-        ModuleBase_Tools::adjustMargins(aPage);
-        createWidget(aPage);
-        if (aWdgType == WDG_SWITCH) {
-          ModuleBase_WidgetSwitch* aSwitch = qobject_cast<ModuleBase_WidgetSwitch*>(aWidget);
-          aSwitch->addPage(aPage, aPageName);
-        } else if (aWdgType == WDG_TOOLBOX) {
-          QToolBox* aToolbox = qobject_cast<QToolBox*>(aWidget);
-          aToolbox->addItem(aPage, aPageName);
+      ModuleBase_PageBase* aPage = createPageByType(aWdgType, thePage->pageWidget());
+
+      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);
         }
-      } while (myWidgetApi->toNextWidget());
+      }
+      // 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);
+
+            QString anIconPath = qs( myWidgetApi->getProperty( CONTAINER_PAGE_ICON ) );
+            QPixmap anIcon( anIconPath );
+            aContainer->addPage( aPage, aPageName, aCaseId, anIcon );
+          }
+        } while (myWidgetApi->toNextWidget());
+      }
     }
   } while (myWidgetApi->toNextWidget());
-  theParent->setLayout(aWidgetLay);
-}
 
-QWidget* ModuleBase_WidgetFactory::labelControl(QWidget* theParent)
-{
-  QWidget* result = new QWidget(theParent);
-  QVBoxLayout* aLabelLay = new QVBoxLayout(result);
-  QLabel* aLabel = new QLabel(result);
-  aLabel->setWordWrap(true);
-  aLabel->setText(qs(myWidgetApi->getProperty(INFO_WDG_TEXT)));
-  aLabel->setToolTip(qs(myWidgetApi->getProperty(INFO_WDG_TOOLTIP)));
-  aLabelLay->addWidget(aLabel);
-  aLabelLay->addStretch(1);
-  result->setLayout(aLabelLay);
-  return result;
+  thePage->alignToTop();
 }
 
-void ModuleBase_WidgetFactory::processAttributes()
+void ModuleBase_WidgetFactory::createWidget(ModuleBase_PageBase* thePage,
+                                            const std::string& theWidgetId)
 {
-  // register that this attribute in feature is not obligatory for the feature execution
-  // so, it is not needed for the standard validation mechanism
-  bool isObligatory = true;
-  bool isConcealment = false;
-  if( myWidgetApi ){
-    isObligatory = myWidgetApi->getBooleanAttribute(ATTRIBUTE_OBLIGATORY, true);
-    isConcealment = myWidgetApi->getBooleanAttribute(ATTRIBUTE_CONCEALMENT, false);
-  }
-  boost::shared_ptr<ModelAPI_Session> aSession = ModelAPI_Session::get();
-  if (!isObligatory) {
-    aSession->validators()->registerNotObligatory(myParentId, myWidgetApi->widgetId());
-  }
-  if(isConcealment) {
-    aSession->validators()->registerConcealment(myParentId, myWidgetApi->widgetId());
-  }
-}
-
-QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
-                                                      QWidget* theParent)
-{
-  QWidget* result = NULL;
-  if (theType == WDG_DOUBLEVALUE) {
-    result = doubleSpinBoxControl(theParent);
-
-  } else if (theType == WDG_INFO) {
-    result = labelControl(theParent);
-
-  } else if (theType == WDG_SHAPE_SELECTOR) {
-    result = shapeSelectorControl(theParent);
-
-  } else if (theType == WDG_BOOLVALUE) {
-    result = booleanControl(theParent);
-
-  } else if (theType == WDG_POINT_SELECTOR) {
-    result = pointSelectorControl(theParent);
-
-  } else if (theType == WDG_FEATURE_SELECTOR) {
-    result = featureSelectorControl(theParent);
-
-  } else if (theType == WDG_FEATURE_OR_ATTRIBUTE_SELECTOR) {
-    result = featureOrAttributeSelectorControl(theParent);
-
-  } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
-    result = doubleValueEditor(theParent);
-
-  } else if (theType == WDG_POINT2D_DISTANCE) {
-    result = point2dDistanceControl(theParent);
-
-  } else if (theType == WDG_FILE_SELECTOR) {
-    result = fileSelectorControl(theParent);
-
-  } else if (theType == WDG_CHOICE) {
-    result = choiceControl(theParent);
-
-  } else if (theType == WDG_STRINGVALUE) {
-    result = lineEditControl(theParent);
-
-  } else if (theType == WDG_MULTISELECTOR) {
-    result = multiSelectorControl(theParent);
+  bool aFound = false;
+  moveToWidgetId(theWidgetId, aFound);
+  if (aFound) {
+    std::string aWdgType = myWidgetApi->widgetType();
 
-  } else if (myWidgetApi->isContainerWidget() || myWidgetApi->isPagedWidget()) {
-    result = createContainer(theType, theParent);
-  } else {
-    result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi,
-                                                      myModelWidgets);
-#ifdef _DEBUG
-    if (!result) {qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type");}
-#endif
-  }
-  if (result) {
-    processAttributes();
+    // 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);
+      }
+    }
   }
-
-  return result;
+  thePage->alignToTop();
 }
 
-QWidget* ModuleBase_WidgetFactory::createContainer(const std::string& theType, QWidget* theParent)
+void ModuleBase_WidgetFactory::getAttributeTitle(const std::string& theAttributeId,
+                                                 std::string& theTitle)
 {
-  QWidget* result = NULL;
-  if (theType == WDG_GROUP || theType == WDG_CHECK_GROUP) {
-    QGroupBox* aGroupBox = new QGroupBox(theParent);
-    aGroupBox->setCheckable(theType == WDG_CHECK_GROUP);
-    result = aGroupBox;
-  } else if (theType == WDG_TOOLBOX) {
-    result = new QToolBox(theParent);
-  } else if (theType == WDG_SWITCH) {
-    result = new ModuleBase_WidgetSwitch(theParent);
-  } else if (theType == WDG_TOOLBOX_BOX || theType == WDG_SWITCH_CASE) {
-    result = NULL;
+  bool aFound = false;
+  moveToWidgetId(theAttributeId, aFound);
+  if (aFound) {
+    theTitle = QString::fromStdString(myWidgetApi->widgetLabel()).toStdString().c_str();
+    if (theTitle.empty())
+      theTitle = QString::fromStdString(myWidgetApi->getProperty(CONTAINER_PAGE_NAME)).toStdString().c_str();
   }
-#ifdef _DEBUG
-  else {qDebug() << "ModuleBase_WidgetFactory::fillWidget: find bad container type";}
-#endif
-  return result;
-}
-
-QWidget* ModuleBase_WidgetFactory::doubleSpinBoxControl(QWidget* theParent)
-{
-  ModuleBase_WidgetDoubleValue* aDblWgt =
-      new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi, myParentId);
-  myModelWidgets.append(aDblWgt);
-  return aDblWgt->getControl();
 }
 
-QWidget* ModuleBase_WidgetFactory::pointSelectorControl(QWidget* theParent)
+void ModuleBase_WidgetFactory::getGreedAttribute(std::string& theAttributeId)
 {
-  ModuleBase_WidgetPoint2D* aWidget =
-      new ModuleBase_WidgetPoint2D(theParent, myWidgetApi,myParentId);
-  myModelWidgets.append(aWidget);
-  return aWidget->getControl();
-}
+  if (!theAttributeId.empty())
+    return;
 
-QWidget* ModuleBase_WidgetFactory::featureSelectorControl(QWidget* theParent)
-{
-  ModuleBase_WidgetFeature* aWidget =
-      new ModuleBase_WidgetFeature(theParent, myWidgetApi,myParentId);
-  myModelWidgets.append(aWidget);
-  return aWidget->getControl();
-}
+  myParentId = myWidgetApi->widgetId();
+  if (!myWidgetApi->toChildWidget())
+    return;
 
-QWidget* ModuleBase_WidgetFactory::featureOrAttributeSelectorControl(QWidget* theParent)
-{
-  ModuleBase_WidgetFeatureOrAttribute* aWidget =
-      new ModuleBase_WidgetFeatureOrAttribute(theParent, myWidgetApi, myParentId);
-  myModelWidgets.append(aWidget);
-  return aWidget->getControl();
+  do {  //Iterate over each node
+    std::string aWdgType = myWidgetApi->widgetType();
+    // Find title under PageGroup
+    if (myWidgetApi->isGroupBoxWidget() ||
+      ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
+      getGreedAttribute(theAttributeId);
+    }
+    else {
+      // Find title here
+      std::string anAttributeId = myWidgetApi->widgetId();
+      if (myWidgetApi->getBooleanAttribute(ATTR_GREED, false))
+        theAttributeId = anAttributeId;
+      if (theAttributeId.empty() && 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 {
+          getGreedAttribute(theAttributeId);
+        } while (theAttributeId.empty() && myWidgetApi->toNextWidget());
+      }
+    }
+  } while (theAttributeId.empty() && myWidgetApi->toNextWidget());
 }
 
-QWidget* ModuleBase_WidgetFactory::doubleValueEditor(QWidget* theParent)
+void ModuleBase_WidgetFactory::moveToWidgetId(const std::string& theWidgetId, bool& theFound)
 {
-  ModuleBase_WidgetEditor* aWidget =
-      new ModuleBase_WidgetEditor(theParent, myWidgetApi, myParentId);
-  myModelWidgets.append(aWidget);
-  return aWidget->getControl();
-}
+  if (theFound)
+    return;
 
-QWidget* ModuleBase_WidgetFactory::shapeSelectorControl(QWidget* theParent)
-{
-  ModuleBase_WidgetShapeSelector* aSelector =
-      new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi, myParentId);
-  myModelWidgets.append(aSelector);
-  return aSelector->getControl();
-}
+  myParentId = myWidgetApi->widgetId();
+  if (!myWidgetApi->toChildWidget())
+    return;
 
-QWidget* ModuleBase_WidgetFactory::booleanControl(QWidget* theParent)
-{
-  ModuleBase_WidgetBoolValue* aBoolWgt =
-      new ModuleBase_WidgetBoolValue(theParent, myWidgetApi, myParentId);
-  myModelWidgets.append(aBoolWgt);
-  return aBoolWgt->getControl();
+  do {  //Iterate over each node
+    std::string aWdgType = myWidgetApi->widgetType();
+    // Find title under PageGroup
+    if (myWidgetApi->isGroupBoxWidget() ||
+      ModuleBase_WidgetCreatorFactory::get()->hasPageWidget(aWdgType)) {
+      moveToWidgetId(theWidgetId, theFound);
+    }
+    else {
+      // Find title here
+      std::string anAttributeId = myWidgetApi->widgetId();
+      theFound = anAttributeId == theWidgetId;
+      if (!theFound && 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 {
+          moveToWidgetId(theWidgetId, theFound);
+        } while (!theFound && myWidgetApi->toNextWidget());
+      }
+    }
+  } while (!theFound && myWidgetApi->toNextWidget());
 }
 
-QWidget* ModuleBase_WidgetFactory::point2dDistanceControl(QWidget* theParent)
+ModuleBase_PageBase* ModuleBase_WidgetFactory::createPageByType(const std::string& theType,
+                                                                QWidget* theParent)
 {
-  ModuleBase_WidgetPoint2dDistance* aDistWgt =
-      new ModuleBase_WidgetPoint2dDistance(theParent, myWidgetApi, myParentId);
-  myModelWidgets.append(aDistWgt);
-  return aDistWgt->getControl();
-}
+  ModuleBase_PageBase* aResult = NULL;
 
-QWidget* ModuleBase_WidgetFactory::fileSelectorControl(QWidget* theParent)
-{
-  ModuleBase_WidgetFileSelector* aFileSelectorWgt =
-      new ModuleBase_WidgetFileSelector(theParent, myWidgetApi, myParentId);
-  myModelWidgets.append(aFileSelectorWgt);
-  return aFileSelectorWgt->getControl();
-}
+  if (theType == WDG_GROUP) {
+    QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
+    ModuleBase_PageGroupBox* aPage = new ModuleBase_PageGroupBox(theParent);
+    aPage->setTitle(aGroupName);
+    aResult = aPage;
+  }
+  else if (theType == WDG_CHECK_GROUP) {
+    QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME));
+    ModuleBase_WidgetCheckGroupBox* aPage = new ModuleBase_WidgetCheckGroupBox(theParent,
+                                                                myWidgetApi, myParentId);
+    aPage->setTitle(aGroupName);
+    aResult = aPage;
+  }
+  if (!aResult)
+    aResult = ModuleBase_WidgetCreatorFactory::get()->createPageByType(theType, theParent,
+                                                                       myWidgetApi, myParentId);
 
-QWidget* ModuleBase_WidgetFactory::choiceControl(QWidget* theParent)
-{
-  ModuleBase_WidgetChoice* aChoiceWgt =
-      new ModuleBase_WidgetChoice(theParent, myWidgetApi,myParentId);
-  myModelWidgets.append(aChoiceWgt);
-  return aChoiceWgt->getControl();
-}
+  ModuleBase_ModelWidget* aWidget = dynamic_cast<ModuleBase_ModelWidget*>(aResult);
+  if (aWidget)
+    myModelWidgets.append(aWidget);
 
-QWidget* ModuleBase_WidgetFactory::lineEditControl(QWidget* theParent)
-{
-  ModuleBase_WidgetLineEdit* aLineEditWgt =
-      new ModuleBase_WidgetLineEdit(theParent, myWidgetApi,myParentId);
-  myModelWidgets.append(aLineEditWgt);
-  return aLineEditWgt->getControl();
+  return aResult;
 }
 
-QWidget* ModuleBase_WidgetFactory::multiSelectorControl(QWidget* theParent)
+ModuleBase_ModelWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType,
+                                                                     QWidget* theParent)
 {
-  ModuleBase_WidgetMultiSelector* aMultiselectorWgt =
-      new ModuleBase_WidgetMultiSelector(theParent, myWorkshop, myWidgetApi,myParentId);
-  myModelWidgets.append(aMultiselectorWgt);
-  return aMultiselectorWgt->getControl();
+  ModuleBase_ModelWidget* result = NULL;
+
+  if (theType == WDG_INFO) {
+    result = new ModuleBase_WidgetLabel(theParent, myWidgetApi, myParentId);
+  } else if (theType == WDG_ERRORINFO) {
+    result = new ModuleBase_WidgetErrorLabel(theParent, myWidgetApi, myParentId);
+  } else if (theType == WDG_DOUBLEVALUE) {
+    result = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi, myParentId);
+  } else if (theType == WDG_INTEGERVALUE) {
+    result = new ModuleBase_WidgetIntValue(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) {
+    std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
+    result = new ModuleBase_WidgetLineEdit( theParent, myWidgetApi, myParentId, aPlaceHolder );
+  } else if (theType == WDG_EXPR_EDITOR) {
+    std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
+    result = new ModuleBase_WidgetExprEditor( theParent, myWidgetApi, myParentId, aPlaceHolder );
+  } 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 ||
+             theType == NODE_VALIDATOR) {
+    // Do nothing for "box" and "case"
+    result = NULL;
+  } else {
+    result = myWorkshop->module()->createWidgetByType(theType, theParent, myWidgetApi, myParentId);
+    if (!result)
+      result = ModuleBase_WidgetCreatorFactory::get()->createWidgetByType(theType, theParent,
+                                                              myWidgetApi, myParentId, myWorkshop);
+    #ifdef _DEBUG
+    if (!result) {
+      qDebug("ModuleBase_WidgetFactory::fillWidget: find bad widget type %s", theType.c_str());
+    }
+    #endif
+  }
+  if (result)
+    myModelWidgets.append(result);
+  return result;
 }
 
-QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const
+QString ModuleBase_WidgetFactory::qs(const std::string& theStdString)
 {
   return QString::fromStdString(theStdString);
 }
+