Salome HOME
Fix for selection of groups features
[modules/shaper.git] / src / XGUI / XGUI_InspectionPanel.cpp
index 16ef5c1649512a63c08beee39240038da89ed397..95ae953a1bbe81cdc0dee04e8904c31ae4a4382d 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2019  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 //
 // You should have received a copy of the GNU Lesser General Public
 // License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-// See http://www.salome-platform.org/ or
-// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
 #include "XGUI_InspectionPanel.h"
+#include "XGUI_Workshop.h"
 #include "XGUI_SelectionMgr.h"
 #include "XGUI_Selection.h"
 #include "XGUI_Tools.h"
+#include "XGUI_ModuleConnector.h"
 
 #include <ModuleBase_ViewerPrs.h>
 #include <ModuleBase_Tools.h>
+#include <ModuleBase_OperationDescription.h>
+#include <ModuleBase_WidgetFactory.h>
+#include <ModuleBase_IModule.h>
+#include <ModuleBase_PageWidget.h>
+
+#include <ModelAPI_ResultField.h>
 
 #include <ModelAPI_Result.h>
 
@@ -52,6 +59,9 @@
 #include <QTableWidget>
 #include <QHeaderView>
 #include <QTextBrowser>
+#include <QResizeEvent>
+#include <QSplitter>
+#include <QStackedWidget>
 
 #include <BRepBndLib.hxx>
 #include <TopoDS_Iterator.hxx>
@@ -59,6 +69,7 @@
 #include <TopTools_ListOfShape.hxx>
 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic
 
+
 // ================     Auxiliary functions     ================
 #define TITLE(val) ("<b>" + (val) + "</b>")
 
@@ -106,35 +117,32 @@ static void appendNamedValueToParameters(const QString& theName,
 
 // ================     XGUI_InspectionPanel    ================
 
-XGUI_InspectionPanel::XGUI_InspectionPanel(QWidget* theParent, XGUI_SelectionMgr* theMgr)
+XGUI_InspectionPanel::XGUI_InspectionPanel(QWidget* theParent, XGUI_Workshop* theWorkshop)
   : QDockWidget(theParent),
-  mySelectionMgr(theMgr)
+  myWorkshop(theWorkshop)
 {
   setWindowTitle(tr("Inspection Panel"));
   setObjectName(INSPECTION_PANEL);
   setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
 
-  QScrollArea* aScrollArea = new QScrollArea(this);
-  setWidget(aScrollArea);
-
-  // Create an internal widget
-  QWidget* aMainWidget = new QWidget(aScrollArea);
+  myStackWgt = new QStackedWidget(this);
 
-  QVBoxLayout* aMainLayout = new QVBoxLayout(aMainWidget);
-  aMainLayout->setContentsMargins(5, 5, 5, 5);
+  // Create shape selection page
+  QSplitter* aSplitter = new QSplitter(Qt::Vertical, myStackWgt);
 
-  QWidget* aNameWgt = new QWidget(aMainWidget);
+  // Create an internal widget
+  QWidget* aNameWgt = new QWidget(aSplitter);
   QHBoxLayout* aNameLayout = new QHBoxLayout(aNameWgt);
-  aNameLayout->setContentsMargins(0, 0, 0, 0);
+  aNameLayout->setContentsMargins(3, 0, 3, 0);
   aNameLayout->addWidget(new QLabel(tr("Object"), aNameWgt));
   myNameEdt = new QLineEdit(aNameWgt);
   myNameEdt->setReadOnly(true);
   aNameLayout->addWidget(myNameEdt);
 
-  aMainLayout->addWidget(aNameWgt);
+  aSplitter->addWidget(aNameWgt);
 
   // Table with sub-shapes
-  mySubShapesTab = new QTableWidget(9, 2, aMainWidget);
+  mySubShapesTab = new QTableWidget(9, 2, aSplitter);
   mySubShapesTab->setFocusPolicy(Qt::NoFocus);
   mySubShapesTab->verticalHeader()->hide();
   QStringList aTitles;
@@ -142,8 +150,8 @@ XGUI_InspectionPanel::XGUI_InspectionPanel(QWidget* theParent, XGUI_SelectionMgr
   mySubShapesTab->setHorizontalHeaderLabels(aTitles);
 
   QStringList aSubShapes;
-  aSubShapes << "SHAPE" << "COMPOUND" << "COMPSOLID" <<
-    "SOLID" << "SHELL" << "FACE" << "WIRE" << "EDGE" << "VERTEX";
+  aSubShapes << tr("SHAPE") << tr("COMPOUND") << tr("COMPSOLID") <<
+    tr("SOLID") << tr("SHELL") << tr("FACE") << tr("WIRE") << tr("EDGE") << tr("VERTEX");
   int i = 0;
   foreach(QString aType, aSubShapes) {
     QTableWidgetItem* aItem = new QTableWidgetItem(aType);
@@ -159,35 +167,52 @@ XGUI_InspectionPanel::XGUI_InspectionPanel(QWidget* theParent, XGUI_SelectionMgr
   mySubShapesTab->setColumnWidth(0, 90);
   mySubShapesTab->setColumnWidth(1, 70);
 
-  mySubShapesTab->setMaximumWidth(170);
-  mySubShapesTab->setMinimumHeight(300);
-
-  aMainLayout->addWidget(mySubShapesTab);
+  aSplitter->addWidget(mySubShapesTab);
 
   // Type of object
-  QWidget* aTypeWgt = new QWidget(aMainWidget);
+  QWidget* aTypeWgt = new QWidget(aSplitter);
   QHBoxLayout* aTypeLayout = new QHBoxLayout(aTypeWgt);
-  aTypeLayout->setContentsMargins(0, 0, 0, 0);
+  aTypeLayout->setContentsMargins(3, 0, 3, 0);
 
   aTypeLayout->addWidget(new QLabel(tr("Type:"), aTypeWgt));
   myTypeLbl = new QLabel("", aTypeWgt);
   aTypeLayout->addWidget(myTypeLbl);
 
-  aMainLayout->addWidget(aTypeWgt);
+  aSplitter->addWidget(aTypeWgt);
 
-  myTypeParams = new QTextBrowser(aMainWidget);
-  myTypeParams->setMaximumWidth(170);
-  myTypeParams->setMaximumHeight(160);
+  myTypeParams = new QTextBrowser(aSplitter);
   myTypeParams->setReadOnly(true);
   myTypeParams->setFocusPolicy(Qt::NoFocus);
-  myTypeParams->setFrameStyle(QFrame::NoFrame);
   myTypeParams->viewport()->setBackgroundRole(QPalette::Window);
 
-  aMainLayout->addWidget(myTypeParams);
+  aSplitter->addWidget(myTypeParams);
 
-  aScrollArea->setWidget(aMainWidget);
+  aSplitter->setCollapsible(0, false);
+  aSplitter->setCollapsible(1, false);
+  aSplitter->setCollapsible(2, false);
+  aSplitter->setCollapsible(3, false);
 
-  connect(mySelectionMgr, SIGNAL(selectionChanged()), SLOT(onSelectionChanged()));
+  QList<int> aSizes;
+  aSizes << 10 << 140 << 10;
+  aSplitter->setSizes(aSizes);
+
+  myShapePanelId = myStackWgt->addWidget(aSplitter);
+
+  // Create feature selection page
+  QScrollArea* aScroll = new QScrollArea(myStackWgt);
+  aScroll->setWidgetResizable(true);
+  aScroll->setFrameStyle(QFrame::NoFrame);
+
+  myFeaturePane = new ModuleBase_PageWidget(aScroll);
+  myFeatureLayout = new QGridLayout(myFeaturePane);
+  myFeatureLayout->setContentsMargins(3, 3, 3, 3);
+  aScroll->setWidget(myFeaturePane);
+  //myFeaturePane->setEnabled(false);
+
+  myFeaturePanelId = myStackWgt->addWidget(aScroll);
+
+  setWidget(myStackWgt);
+  connect(myWorkshop->selector(), SIGNAL(selectionChanged()), SLOT(onSelectionChanged()));
 }
 
 //********************************************************************
@@ -209,24 +234,56 @@ void XGUI_InspectionPanel::clearContent()
     mySubShapesTab->item((SudShape)i, 1)->setText("");
   }
   myTypeLbl->setText("");
-  myTypeParams->setText("");
+  setParamsText("");
+
+  myFeaturePane->clearPage();
 }
 
 //********************************************************************
 void XGUI_InspectionPanel::onSelectionChanged()
 {
+  if (!isVisible())
+    return;
+
   clearContent();
-  XGUI_Selection* aSelection = mySelectionMgr->selection();
+
+  XGUI_Selection* aSelection = myWorkshop->selector()->selection();
   QList<ModuleBase_ViewerPrsPtr> aSelectedList =
-    aSelection->getSelected(ModuleBase_ISelection::Viewer);
+    aSelection->getSelected(myWorkshop->selector()->placeOfSelection());
+
   if (aSelectedList.count() > 0) {
     ModuleBase_ViewerPrsPtr aPrs = aSelectedList.first();
-    TopoDS_Shape aShape = ModuleBase_Tools::getSelectedShape(aPrs);
-    if (aShape.IsNull())
+    FieldStepPtr aStep =
+      std::dynamic_pointer_cast<ModelAPI_ResultField::ModelAPI_FieldStep>(aPrs->object());
+    if (aStep)
       return;
-    setName(XGUI_Tools::generateName(aPrs));
-    setShapeContent(aShape);
-    setShapeParams(aShape);
+    FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aPrs->object());
+    if (aFeature.get()) {
+      myStackWgt->setCurrentIndex(myFeaturePanelId);
+      buildFeaturePane(aFeature);
+    }
+    else {
+      myStackWgt->setCurrentIndex(myShapePanelId);
+      TopoDS_Shape aShape = ModuleBase_Tools::getSelectedShape(aPrs);
+      if (aShape.IsNull()) {
+        ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
+        if (aRes.get()) {
+          GeomShapePtr aShpPtr = aRes->shape();
+          if (aShpPtr.get()) {
+            aShape = aShpPtr->impl<TopoDS_Shape>();
+          }
+        }
+      }
+      if (aShape.IsNull())
+        return;
+      GeomShapePtr aShapePtr(new GeomAPI_Shape());
+      aShapePtr->setImpl(new TopoDS_Shape(aShape));
+
+      ModuleBase_ViewerPrsPtr aPrsCopy(new ModuleBase_ViewerPrs(aPrs->object(), aShapePtr));
+      setName(XGUI_Tools::generateName(aPrsCopy));
+      setShapeContent(aShape);
+      setShapeParams(aShape);
+    }
   }
 }
 
@@ -275,8 +332,6 @@ void XGUI_InspectionPanel::setShapeContent(const TopoDS_Shape& theShape)
     setSubShapeValue(ShapeId, aMapOfShape.Extent());
   }
   catch (Standard_Failure) {
-    Handle(Standard_Failure) aFail = Standard_Failure::Caught();
-    //SetErrorCode(aFail->GetMessageString());
   }
 }
 
@@ -321,7 +376,7 @@ void XGUI_InspectionPanel::fillVertex(const GeomVertexPtr& theVertex)
 
   QString aParams;
   appendPointToParameters(tr("Coordinates"), aPoint, aParams);
-  myTypeParams->setText(aParams);
+  setParamsText(aParams);
 }
 
 //********************************************************************
@@ -378,7 +433,7 @@ void XGUI_InspectionPanel::fillEdge(const GeomEdgePtr& theEdge)
     appendPointToParameters(tr("Start point"), aStartPnt, aParams);
     appendPointToParameters(tr("End point"), aEndPnt, aParams);
   }
-  myTypeParams->setText(aParams);
+  setParamsText(aParams);
 }
 
 //********************************************************************
@@ -400,7 +455,7 @@ void XGUI_InspectionPanel::fillWire(const GeomWirePtr& theWire)
   else
     myTypeLbl->setText(tr("Wire"));
 
-  myTypeParams->setText(aParams);
+  setParamsText(aParams);
 }
 
 //********************************************************************
@@ -439,7 +494,7 @@ void XGUI_InspectionPanel::fillFace(const GeomFacePtr& theFace)
           appendGroupNameToParameters(tr("Dimensions"), aParams);
           appendNamedValueToParameters(tr("Width"), aWidth, aParams);
           appendNamedValueToParameters(tr("Height"), aHeight, aParams);
-          myTypeParams->setText(aParams);
+          setParamsText(aParams);
 
           isCommonCase = false;
         }
@@ -567,8 +622,12 @@ void XGUI_InspectionPanel::fillContainer(const GeomShapePtr& theShape)
     myTypeLbl->setText("Compound");
 
   // fill bounding box
+  TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
   Bnd_Box aBB;
-  BRepBndLib::Add(theShape->impl<TopoDS_Shape>(), aBB);
+  BRepBndLib::Add(aShape, aBB);
+
+  if (aBB.IsVoid())
+    return;
 
   gp_Pnt aMinPnt = aBB.CornerMin();
   GeomPointPtr aMinPoint(new GeomAPI_Pnt(aMinPnt.X(), aMinPnt.Y(), aMinPnt.Z()));
@@ -582,6 +641,7 @@ void XGUI_InspectionPanel::fillContainer(const GeomShapePtr& theShape)
   appendPointToParameters(tr("Maximal corner"), aMaxPoint, aParams);
 }
 
+//********************************************************************
 void XGUI_InspectionPanel::setPlaneType(const QString& theTitle,
                                         const std::shared_ptr<GeomAPI_Pln>& thePlane)
 {
@@ -589,9 +649,10 @@ void XGUI_InspectionPanel::setPlaneType(const QString& theTitle,
   QString aParams;
   appendPointToParameters(tr("Origin"), thePlane->location(), aParams);
   appendDirToParameters(tr("Normal"), thePlane->direction(), aParams);
-  myTypeParams->setText(aParams);
+  setParamsText(aParams);
 }
 
+//********************************************************************
 void XGUI_InspectionPanel::setSphereType(const QString& theTitle,
                                          const std::shared_ptr<GeomAPI_Sphere>& theSphere)
 {
@@ -600,9 +661,10 @@ void XGUI_InspectionPanel::setSphereType(const QString& theTitle,
   appendPointToParameters(tr("Center"), theSphere->center(), aParams);
   appendGroupNameToParameters(tr("Dimensions"), aParams);
   appendNamedValueToParameters(tr("Radius"), theSphere->radius(), aParams);
-  myTypeParams->setText(aParams);
+  setParamsText(aParams);
 }
 
+//********************************************************************
 void XGUI_InspectionPanel::setCylinderType(const QString& theTitle,
                                            const std::shared_ptr<GeomAPI_Cylinder>& theCyl)
 {
@@ -613,9 +675,10 @@ void XGUI_InspectionPanel::setCylinderType(const QString& theTitle,
   appendGroupNameToParameters(tr("Dimensions"), aParams);
   appendNamedValueToParameters(tr("Radius"), theCyl->radius(), aParams);
   appendNamedValueToParameters(tr("Height"), theCyl->height(), aParams);
-  myTypeParams->setText(aParams);
+  setParamsText(aParams);
 }
 
+//********************************************************************
 void XGUI_InspectionPanel::setConeType(const QString& theTitle,
                                        const std::shared_ptr<GeomAPI_Cone>& theCone)
 {
@@ -627,9 +690,10 @@ void XGUI_InspectionPanel::setConeType(const QString& theTitle,
   appendNamedValueToParameters(tr("Radius 1"), theCone->radius1(), aParams);
   appendNamedValueToParameters(tr("Radius 2"), theCone->radius2(), aParams);
   appendNamedValueToParameters(tr("Height"), theCone->height(), aParams);
-  myTypeParams->setText(aParams);
+  setParamsText(aParams);
 }
 
+//********************************************************************
 void XGUI_InspectionPanel::setTorusType(const QString& theTitle,
                                         const std::shared_ptr<GeomAPI_Torus>& theTorus)
 {
@@ -640,9 +704,10 @@ void XGUI_InspectionPanel::setTorusType(const QString& theTitle,
   appendGroupNameToParameters(tr("Dimensions"), aParams);
   appendNamedValueToParameters(tr("Major radius"), theTorus->majorRadius(), aParams);
   appendNamedValueToParameters(tr("Minor radius"), theTorus->minorRadius(), aParams);
-  myTypeParams->setText(aParams);
+  setParamsText(aParams);
 }
 
+//********************************************************************
 void XGUI_InspectionPanel::setBoxType(const QString& theTitle,
                                       const std::shared_ptr<GeomAPI_Box>& theBox)
 {
@@ -653,9 +718,10 @@ void XGUI_InspectionPanel::setBoxType(const QString& theTitle,
   appendNamedValueToParameters(tr("Width"), theBox->width(), aParams);
   appendNamedValueToParameters(tr("Depth"), theBox->depth(), aParams);
   appendNamedValueToParameters(tr("Height"), theBox->height(), aParams);
-  myTypeParams->setText(aParams);
+  setParamsText(aParams);
 }
 
+//********************************************************************
 void XGUI_InspectionPanel::setRotatedBoxType(const QString& theTitle,
                                              const std::shared_ptr<GeomAPI_Box>& theBox)
 {
@@ -669,5 +735,47 @@ void XGUI_InspectionPanel::setRotatedBoxType(const QString& theTitle,
   appendNamedValueToParameters(tr("Width"), theBox->width(), aParams);
   appendNamedValueToParameters(tr("Depth"), theBox->depth(), aParams);
   appendNamedValueToParameters(tr("Height"), theBox->height(), aParams);
-  myTypeParams->setText(aParams);
+  setParamsText(aParams);
+}
+
+
+//********************************************************************
+void XGUI_InspectionPanel::setParamsText(const QString& theText)
+{
+  myTypeParams->setText(theText);
+}
+
+//********************************************************************
+void XGUI_InspectionPanel::buildFeaturePane(const FeaturePtr& theFeature)
+{
+  std::string aXmlCfg, aDescription;
+  myWorkshop->module()->getXMLRepresentation(theFeature->getKind(), aXmlCfg, aDescription);
+  if (!aXmlCfg.empty()) {
+    QList<ModuleBase_ModelWidget*> aWidgets;
+    if (!myWorkshop->module()->createWidgets(theFeature, aXmlCfg.c_str(), aWidgets)) {
+      ModuleBase_WidgetFactory aFactory(aXmlCfg, myWorkshop->moduleConnector());
+      aFactory.createWidget(myFeaturePane);
+      aWidgets = aFactory.getModelWidgets();
+    }
+    foreach(ModuleBase_ModelWidget* aWgt, aWidgets) {
+      if (aWgt->isInformative()) {
+        aWgt->setFeature(theFeature, false, false);
+        aWgt->setReadOnly(true);
+        aWgt->setEditingMode(true);
+        aWgt->restoreValue();
+        aWgt->showInformativePage();
+      }
+      else {
+        aWgt->setFeature(theFeature, false, false);
+        aWgt->setEditingMode(true);
+        aWgt->hide();
+      }
+    }
+  }
+}
+
+void XGUI_InspectionPanel::showEvent(QShowEvent* theEvent)
+{
+  QDockWidget::showEvent(theEvent);
+  onSelectionChanged();
 }