Salome HOME
It corrects the validator parameter from the entrity to viewerprs in order to validat...
[modules/shaper.git] / src / PartSet / PartSet_WidgetSketchLabel.cpp
index f3001c881b3325eeb4f240d2d84406c932a83ccd..42d4cd3edecc0d737ba7f675269b026f89cea5c1 100644 (file)
@@ -7,6 +7,8 @@
 #include "PartSet_WidgetSketchLabel.h"
 #include "PartSet_Tools.h"
 
+#include "SketchPlugin_SketchEntity.h"
+
 #include <XGUI_Workshop.h>
 #include <XGUI_Displayer.h>
 #include <XGUI_SelectionMgr.h>
 
 #include <ModuleBase_Operation.h>
 #include <ModuleBase_ViewerPrs.h>
+#include <ModuleBase_Tools.h>
+#include <ModuleBase_IModule.h>
+
+#include <ModelAPI_ResultBody.h>
+#include <ModelAPI_Tools.h>
 
 #include <GeomAlgoAPI_FaceBuilder.h>
 #include <GeomDataAPI_Point.h>
@@ -23,6 +30,7 @@
 #include <GeomAPI_XYZ.h>
 
 #include <SketchPlugin_Sketch.h>
+#include <SketcherPrs_Tools.h>
 
 #include <Precision.hxx>
 #include <gp_Pln.hxx>
 #include <Config_PropManager.h>
 
 #include <QLabel>
-#include <QTimer>
+//#include <QTimer>
 #include <QApplication>
+#include <QVBoxLayout>
+#include <QCheckBox>
 
 
 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
                                                      const Config_WidgetAPI* theData,
-                                                     const std::string& theParentId)
-    : ModuleBase_ModelWidget(theParent, theData, theParentId), myPreviewDisplayed(false)
+                                                     const std::string& theParentId,
+                                                     bool toShowConstraints)
+    : ModuleBase_WidgetValidated(theParent, theData, theParentId),
+      myPreviewDisplayed(false),
+      myWorkshop(NULL)
 {
   myText = QString::fromStdString(theData->getProperty("title"));
   myLabel = new QLabel("", theParent);
@@ -51,9 +64,20 @@ PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
   myLabel->setToolTip("");
   myLabel->setIndent(5);
 
-  mySelectionTimer = new QTimer(this);
-  connect(mySelectionTimer, SIGNAL(timeout()), SLOT(setSketchingMode()));
-  mySelectionTimer->setSingleShot(true);
+  //mySelectionTimer = new QTimer(this);
+  //connect(mySelectionTimer, SIGNAL(timeout()), SLOT(setSketchingMode()));
+  //mySelectionTimer->setSingleShot(true);
+
+  QVBoxLayout* aLayout = new QVBoxLayout(this);
+  ModuleBase_Tools::zeroMargins(aLayout);
+  aLayout->addWidget(myLabel);
+
+  myShowConstraints = new QCheckBox(tr("Show constraints"), this);
+  aLayout->addWidget(myShowConstraints);
+
+  setLayout(aLayout);
+  connect(myShowConstraints, SIGNAL(toggled(bool)), this, SIGNAL(showConstraintToggled(bool)));
+  myShowConstraints->setChecked(toShowConstraints);
 }
 
 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
@@ -63,61 +87,75 @@ PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
 
 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
 {
-  return QList<QWidget*>();
+  QList<QWidget*> aResult;
+  aResult << myLabel;
+  return aResult;
 }
 
-QWidget* PartSet_WidgetSketchLabel::getControl() const
-{
-  return myLabel;
-}
-
-void PartSet_WidgetSketchLabel::onPlaneSelected()
+void PartSet_WidgetSketchLabel::onSelectionChanged()
 {
+  ModuleBase_ViewerPrs aPrs;
+  // 1. find selected presentation either in the viewer or in OB
   XGUI_Selection* aSelection = myWorkshop->selector()->selection();
   QList<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
+  // the selection in OCC viewer - the selection of a face in the viewer
+  // it can be th main plane's face of a face on a visualized body
   if (!aSelected.empty()) {
-    ModuleBase_ViewerPrs aPrs = aSelected.first();
+    aPrs = aSelected.first();
+  }
+  else {
+    // the selection in Object Browser: the plane object can be used as sketch plane
+    QObjectPtrList anObjects = aSelection->selectedObjects();
+    if (!anObjects.empty()) {
+      aPrs.setObject(anObjects.first());
+    }
+  }
+  if (aPrs.isEmpty())
+    return;
+
+  if (isValidSelection(aPrs)) {
+    // 2. set the selection to sketch
+    setSelectionCustom(aPrs);
+    // 3. hide main planes if they have been displayed
+    erasePreviewPlanes();
+    // 4. if the planes were displayed, change the view projection
     TopoDS_Shape aShape = aPrs.shape();
     if (!aShape.IsNull()) {
-      std::shared_ptr<GeomAPI_Dir> aDir = setSketchPlane(aShape);
-      if (aDir) {
-        erasePreviewPlanes();
-
-        if (aPrs.object() && (feature() != aPrs.object())) {
-          DataPtr aData = feature()->data();
-          AttributeSelectionPtr aSelAttr = 
-            std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
-            (aData->attribute(SketchPlugin_Feature::EXTERNAL_ID()));
-          if (aSelAttr) {
-            ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
-            if (aRes) {
-              GeomShapePtr aShapePtr(new GeomAPI_Shape());
-              aShapePtr->setImpl(new TopoDS_Shape(aShape));
-              aSelAttr->setValue(aRes, aShapePtr);
-            }
-          }
-        } else
-          myWorkshop->viewer()->setViewProjection(aDir->x(), aDir->y(), aDir->z());
+      DataPtr aData = feature()->data();
+      AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
+                                (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
+      if (aSelAttr) {
+        GeomShapePtr aShapePtr = aSelAttr->value();
+        if (aShapePtr.get() == NULL || aShapePtr->isNull()) {
+          //TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
+          std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
+          aGShape->setImpl(new TopoDS_Shape(aShape));
+          // get plane parameters
+          std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
+          std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
 
-        // Clear text in the label
-        myLabel->setText("");
-        myLabel->setToolTip("");
-        disconnect(myWorkshop->selector(), SIGNAL(selectionChanged()), 
-                   this, SLOT(onPlaneSelected()));
-
-        // Clear selection mode and define sketching mode
-        XGUI_Displayer* aDisp = myWorkshop->displayer();
-        aDisp->removeSelectionFilter(myFaceFilter);
-        //aDisp->closeLocalContexts();
-        emit planeSelected(plane());
-        setSketchingMode();
-
-        // Update sketcher actions
-        XGUI_ActionsMgr* anActMgr = myWorkshop->actionsMgr();
-        anActMgr->update();
-        myWorkshop->viewer()->update();
+          myWorkshop->viewer()->setViewProjection(aDir->x(), aDir->y(), aDir->z());
+        }
       }
     }
+    // 5. Clear text in the label
+    myLabel->setText("");
+    myLabel->setToolTip("");
+    disconnect(myWorkshop->selector(), SIGNAL(selectionChanged()), 
+               this, SLOT(onSelectionChanged()));
+    // 6. deactivate face selection filter
+    activateFilters(myWorkshop->module()->workshop(), false);
+
+    // 7. Clear selection mode and define sketching mode
+    //XGUI_Displayer* aDisp = myWorkshop->displayer();
+    //aDisp->closeLocalContexts();
+    emit planeSelected(plane());
+    //setSketchingMode();
+
+    // 8. Update sketcher actions
+    XGUI_ActionsMgr* anActMgr = myWorkshop->actionsMgr();
+    anActMgr->update();
+    myWorkshop->viewer()->update();
   }
 }
 
@@ -128,44 +166,115 @@ std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
 
 }
 
+bool PartSet_WidgetSketchLabel::focusTo()
+{
+  myLabel->setFocus();
+  return true;
+}
+
+void PartSet_WidgetSketchLabel::enableFocusProcessing()
+{
+  myLabel->installEventFilter(this);
+}
+
+void PartSet_WidgetSketchLabel::storeAttributeValue()
+{
+}
+
+void PartSet_WidgetSketchLabel::restoreAttributeValue(const bool theValid)
+{
+  // it is not necessary to save the previous plane value because the plane is chosen once
+  DataPtr aData = feature()->data();
+  AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
+    (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
+  if (aSelAttr) {
+    ResultPtr anEmptyResult;
+    GeomShapePtr anEmptyShape;
+    aSelAttr->setValue(anEmptyResult, anEmptyShape);
+  }
+}
+
+bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
+{
+  bool isOwnerSet = false;
+
+  const TopoDS_Shape& aShape = thePrs.shape();
+  std::shared_ptr<GeomAPI_Dir> aDir;
+
+  if (thePrs.object() && (feature() != thePrs.object())) {
+    DataPtr aData = feature()->data();
+    AttributeSelectionPtr aSelAttr = 
+      std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
+      (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
+    if (aSelAttr) {
+      ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
+      if (aRes) {
+        GeomShapePtr aShapePtr(new GeomAPI_Shape());
+        if (aShape.IsNull()) {
+          aShapePtr = ModelAPI_Tools::shape(aRes);
+        }
+        else {
+          aShapePtr->setImpl(new TopoDS_Shape(aShape));
+        }
+        aSelAttr->setValue(aRes, aShapePtr);
+        isOwnerSet = true;
+      }
+    }
+  }
+  else if (!aShape.IsNull()) {
+    aDir = setSketchPlane(aShape);
+    isOwnerSet = aDir;
+  }
+  return isOwnerSet;
+}
+
 void PartSet_WidgetSketchLabel::activateCustom()
 {
   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
-  if (aPlane) {
+  bool aBodyIsVisualized = false;
+  XGUI_Displayer* aDisp = myWorkshop->displayer();
+  QObjectPtrList aDisplayed = aDisp->displayedObjects();
+  foreach (ObjectPtr anObj, aDisplayed) {
+    ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
+    if (aResult.get() != NULL) {
+      aBodyIsVisualized = aResult->groupName() == ModelAPI_ResultBody::group();
+      if (aBodyIsVisualized)
+        break;
+    }
+  }
+
+  if (aPlane || aBodyIsVisualized) {
     //setSketchingMode();
     // In order to avoid Opening/Closing of context too often
-    mySelectionTimer->start(20);
+    // it can be useful for a delay on the property panel filling
+    // it is possible that it is not necessary anymore, but it requires a check
+    //mySelectionTimer->start(20);
+    //setSketchingMode();
   } else {
     // We have to select a plane before any operation
     showPreviewPlanes();
-
-    XGUI_Displayer* aDisp = myWorkshop->displayer();
-    //aDisp->openLocalContext();
-    //aDisp->activateObjects(QIntList());
-    if (myFaceFilter.IsNull())
-      myFaceFilter = new StdSelect_FaceFilter(StdSelect_Plane);
-    aDisp->addSelectionFilter(myFaceFilter);
-    QIntList aModes;
-    aModes << TopAbs_FACE;
-    aDisp->activateObjects(aModes);
-
-    myLabel->setText(myText);
-    myLabel->setToolTip(myTooltip);
-
-    connect(myWorkshop->selector(), SIGNAL(selectionChanged()), this, SLOT(onPlaneSelected()));
-    aDisp->updateViewer();
   }
+  QIntList aModes;
+  aModes << TopAbs_FACE;
+  aDisp->activateObjects(aModes);
+
+  myLabel->setText(myText);
+  myLabel->setToolTip(myTooltip);
+
+  connect(myWorkshop->selector(), SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
+  activateFilters(myWorkshop->module()->workshop(), true);
+
+  aDisp->updateViewer();
 }
 
 void PartSet_WidgetSketchLabel::deactivate()
 {
   // Do not set selection mode if the widget was activated for a small moment 
-  mySelectionTimer->stop();
-  XGUI_Displayer* aDisp = myWorkshop->displayer();
-  aDisp->removeSelectionFilter(myFaceFilter);
-  //aDisp->removeSelectionFilter(mySketchFilter);
+  //mySelectionTimer->stop();
+  //XGUI_Displayer* aDisp = myWorkshop->displayer();
   //aDisp->closeLocalContexts();
   erasePreviewPlanes();
+  activateFilters(myWorkshop->module()->workshop(), false);
 }
 
 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
@@ -268,15 +377,12 @@ std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const Top
   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
   aDirX->setValue(aXDir);
-  std::shared_ptr<GeomDataAPI_Dir> aDirY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
-      aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
-  aDirY->setValue(aYDir);
   std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
   return aDir;
 }
 
 
-void PartSet_WidgetSketchLabel::setSketchingMode()
+/*void PartSet_WidgetSketchLabel::setSketchingMode()
 {
   XGUI_Displayer* aDisp = myWorkshop->displayer();
   // Clear standard selection modes if they are defined
@@ -284,11 +390,19 @@ void PartSet_WidgetSketchLabel::setSketchingMode()
   //aDisp->openLocalContext();
 
   // Get default selection modes
+  
   QIntList aModes;
-  aModes.append(AIS_DSM_Text);
-  aModes.append(AIS_DSM_Line);
+  aModes.append(SketcherPrs_Tools::Sel_Dimension_Text);
+  aModes.append(SketcherPrs_Tools::Sel_Dimension_Line);
+  aModes.append(SketcherPrs_Tools::Sel_Constraint);
   aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
   aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));
 
   aDisp->activateObjects(aModes);
+}*/
+
+void PartSet_WidgetSketchLabel::showConstraints(bool theOn)
+{
+  myShowConstraints->setChecked(theOn);
+  emit showConstraintToggled(theOn);
 }