Salome HOME
updated copyright message
[modules/shaper.git] / src / PartSet / PartSet_WidgetFeaturePointSelector.cpp
index f68e580d9713841684332f00b948fbbfe3b4c901..d635af3409021832ef7fc418cf4348c5d8a2b7f3 100644 (file)
@@ -1,8 +1,25 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+// Copyright (C) 2014-2023  CEA, EDF
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// 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
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
-// File:        PartSet_WidgetFeaturePointSelector.cpp
-// Created:     28 Feb 2017
-// Author:      Natalia ERMOLAEVA
+#include "PartSet_WidgetFeaturePointSelector.h"
+#include "PartSet_Tools.h"
+#include "PartSet_ExternalObjectsMgr.h"
 
 #include <Config_WidgetAPI.h>
 
@@ -17,6 +34,8 @@
 #include <ModuleBase_ISelection.h>
 #include <ModuleBase_ViewerPrs.h>
 
+#include <ModelAPI_AttributeRefAttr.h>
+#include <ModelAPI_AttributeReference.h>
 #include <ModelAPI_Events.h>
 #include <ModelAPI_Feature.h>
 #include <ModelAPI_Tools.h>
 #include <ModuleBase_IWorkshop.h>
 #include <ModuleBase_IModule.h>
 
-#include "PartSet_WidgetFeaturePointSelector.h"
-#include "PartSet_Tools.h"
-
-//#include <SketchPlugin_ConstraintCoincidence.h>
-//#include <SketchPlugin_Constraint.h>
 #include <SketchPlugin_Point.h>
-#include <SketchPlugin_Trim.h>
 
 #include <XGUI_Tools.h>
 #include <XGUI_Workshop.h>
@@ -52,10 +65,25 @@ PartSet_WidgetFeaturePointSelector::PartSet_WidgetFeaturePointSelector(QWidget*
                                                          const Config_WidgetAPI* theData)
 : ModuleBase_WidgetShapeSelector(theParent, theWorkshop, theData)
 {
+  std::string anAttributes = theData->getProperty("selection_attributes");
+  QStringList anAttributesList =
+    QString(anAttributes.c_str()).split(' ', QString::SkipEmptyParts);
+
+  myHasPreview = anAttributesList.size() >= 4;
+
+  mySelectedObjectAttribute = anAttributesList[0].toStdString();
+  mySelectedPointAttribute = anAttributesList[1].toStdString();
+  if (myHasPreview) {
+    myPreviewObjectAttribute = anAttributesList[2].toStdString();
+    myPreviewPointAttribute = anAttributesList[3].toStdString();
+  }
+  myExternalObjectMgr = new PartSet_ExternalObjectsMgr(theData->getProperty("use_external"),
+    theData->getProperty("can_create_external"), true);
 }
 
 PartSet_WidgetFeaturePointSelector::~PartSet_WidgetFeaturePointSelector()
 {
+  delete myExternalObjectMgr;
 }
 
 //********************************************************************
@@ -66,37 +94,6 @@ bool PartSet_WidgetFeaturePointSelector::isValidSelection(
   //return true;
 }
 
-//********************************************************************
-bool PartSet_WidgetFeaturePointSelector::activateSelectionAndFilters(bool toActivate)
-{
-#ifdef HIGHLIGHT_STAYS_PROBLEM
-  Handle(AIS_InteractiveContext) aContext =
-                            XGUI_Tools::workshop(myWorkshop)->viewer()->AISContext();
-  Quantity_Color aColor;
-  Handle(Graphic3d_HighlightStyle) aHStyle = aContext->HighlightStyle();
-  Handle(Graphic3d_HighlightStyle) aSStyle = aContext->SelectionStyle();
-  if (toActivate) {
-    std::vector<int> aColors;
-    aColors = Config_PropManager::color("Visualization", "sketch_entity_color");
-    aColor = Quantity_Color(aColors[0] / 255., aColors[1] / 255., aColors[2] / 255.,
-                            Quantity_TOC_RGB);
-    myHighlightColor = aHStyle->Color();
-    mySelectionColor = aSStyle->Color();
-  }
-  else {
-    aColor = myHighlightColor;
-  }
-  aHStyle->SetColor(aColor);
-  aContext->SetHighlightStyle(aHStyle);
-
-  aSStyle->SetColor(aColor);
-  aContext->SetSelectionStyle(aSStyle);
-
-#endif
-
-  return ModuleBase_WidgetShapeSelector::activateSelectionAndFilters(toActivate);
-}
-
 //********************************************************************
 void PartSet_WidgetFeaturePointSelector::activateCustom()
 {
@@ -121,7 +118,16 @@ void PartSet_WidgetFeaturePointSelector::mouseMoved(ModuleBase_IViewWindow* theW
 
   ModuleBase_ViewerPrsPtr aPrs = !aHighlighted.empty() ? aHighlighted.first()
                                                        : ModuleBase_ViewerPrsPtr();
-  fillFeature(aPrs, theWindow, theEvent);
+  myPreviewPoint = PartSet_Tools::getPnt2d(theEvent, theWindow, mySketch);
+  if (myHasPreview) {
+    if (aPrs.get() && aPrs->object().get())
+      myPreviewObject = aPrs->object();
+    else
+      myPreviewObject = ObjectPtr();
+    fillFeature();
+    updateObject(feature());
+    Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
+  }
 }
 
 //********************************************************************
@@ -132,67 +138,113 @@ void PartSet_WidgetFeaturePointSelector::mouseReleased(ModuleBase_IViewWindow* t
   if (theEvent->button() != Qt::LeftButton)
     return;
 
-  std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
-                          std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
-                          feature()->data()->attribute(SketchPlugin_Trim::PREVIEW_OBJECT()));
-  ObjectPtr aPreviewObject = aRefPreviewAttr->value();
+  ModuleBase_ISelection* aSelection = myWorkshop->selection();
+  QList<ModuleBase_ViewerPrsPtr> aSelected =
+    aSelection->getSelected(ModuleBase_ISelection::Viewer);
+
+  ModuleBase_ViewerPrsPtr aPrs =
+    !aSelected.empty() ? aSelected.first() : ModuleBase_ViewerPrsPtr();
+  if (aPrs.get() && aPrs->object().get()) {
+    myPreviewObject = aSelection->getResult(aPrs);
+    GeomShapePtr aShape = aSelection->getShape(aPrs);
+    myExternalObjectMgr->getGeomSelection(aPrs, myPreviewObject, aShape,
+      myWorkshop, sketch(), true);
+  }
+  myPreviewPoint = PartSet_Tools::getPnt2d(theEvent, theWindow, mySketch);
+
+  ObjectPtr aPreviewObject;
+  GeomPnt2dPtr aPreviewPoint;
+  if (myHasPreview) {
+    std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
+                            std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
+                            feature()->data()->attribute(myPreviewObjectAttribute));
+    aPreviewObject = aRefPreviewAttr->value();
+
+    std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
+                            std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+                            feature()->data()->attribute(myPreviewPointAttribute));
+    aPreviewPoint = aPointPreviewAttr->pnt();
+  }
+  else {
+    aPreviewObject = myPreviewObject;
+    aPreviewPoint = myPreviewPoint;
+  }
+
   // do not move focus from the current widget if the object is not highlighted/selected
   if (!aPreviewObject.get())
     return;
 
-  // set parameters of preview into parameters of selection in the feature
-  std::shared_ptr<ModelAPI_AttributeReference> aRefSelectedAttr =
-                          std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
-                          feature()->data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
-  aRefSelectedAttr->setValue(aRefPreviewAttr->value());
+  // Do not use non-sketcher objects
+  if (!sketch()->isSub(aPreviewObject))
+    return;
 
+  // set parameters of preview into parameters of selection in the feature
   std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
                           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-                          feature()->data()->attribute(SketchPlugin_Trim::SELECTED_POINT()));
-  std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
-                          std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-                          feature()->data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
-  aPointSelectedAttr->setValue(aPointPreviewAttr->x(), aPointPreviewAttr->y());
+                          feature()->data()->attribute(mySelectedPointAttribute));
+  aPointSelectedAttr->setValue(aPreviewPoint);
+
+  AttributeReferencePtr aRefSelectedAttr = feature()->reference(mySelectedObjectAttribute);
+  if (aRefSelectedAttr)
+    aRefSelectedAttr->setValue(aPreviewObject);
+  else {
+    AttributeRefAttrPtr aRefAttrSelectedAttr = feature()->refattr(mySelectedObjectAttribute);
+    if (aRefAttrSelectedAttr)
+      aRefAttrSelectedAttr->setObject(aPreviewObject);
+  }
 
   updateObject(feature());
 
-  emit focusOutWidget(this);
   // we need to deselect base feature for better visibility of selected feature
   XGUI_Tools::workshop(myWorkshop)->displayer()->clearSelected(false);
+
+  // focusOutWidget should be the last functionality in the method because after this emit,
+  // the widget may be deleted and members of this class are deleted (e.g. myWorkshop)
+  emit focusOutWidget(this);
 }
 
 //********************************************************************
-bool PartSet_WidgetFeaturePointSelector::fillFeature(
-                            const std::shared_ptr<ModuleBase_ViewerPrs>& theSelectedPrs,
-                            ModuleBase_IViewWindow* theWindow,
-                            QMouseEvent* theEvent)
+bool PartSet_WidgetFeaturePointSelector::fillFeature()
 {
-  bool aFilled = false;
-  ObjectPtr anObject;
-  if (theSelectedPrs.get() && theSelectedPrs->object().get())
-    anObject = theSelectedPrs->object();
-
-  std::shared_ptr<ModelAPI_AttributeReference> aRef =
-                          std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
-                          feature()->data()->attribute(SketchPlugin_Trim::PREVIEW_OBJECT()));
-  aRef->setValue(anObject);
-
-  std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
-                  std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-                  feature()->data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
-  std::shared_ptr<GeomAPI_Pnt2d> aPoint = PartSet_Tools::getPnt2d(theEvent, theWindow, mySketch);
-  anAttributePoint->setValue(aPoint);
+  if (myHasPreview) {
+    std::shared_ptr<ModelAPI_AttributeReference> aRef =
+                            std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
+                            feature()->data()->attribute(myPreviewObjectAttribute));
+    aRef->setValue(myPreviewObject);
+    if (myPreviewPoint.get()) {
+      std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
+        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+          feature()->data()->attribute(myPreviewPointAttribute));
+      anAttributePoint->setValue(myPreviewPoint);
+    }
+  }
+  else {
+    // Do not use non-sketcher objects
+    if (!sketch()->isSub(myPreviewObject))
+      return false;
+
+    // set parameters of preview into parameters of selection in the feature
+    if (myPreviewPoint.get()) {
+      std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
+        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+          feature()->data()->attribute(mySelectedPointAttribute));
+      aPointSelectedAttr->setValue(myPreviewPoint);
+    }
+    AttributeReferencePtr aRefSelectedAttr = feature()->reference(mySelectedObjectAttribute);
+    if (aRefSelectedAttr)
+      aRefSelectedAttr->setValue(myPreviewObject);
+    else {
+      AttributeRefAttrPtr aRefAttrSelectedAttr = feature()->refattr(mySelectedObjectAttribute);
+      if (aRefAttrSelectedAttr)
+        aRefAttrSelectedAttr->setObject(myPreviewObject);
+    }
+  }
   // redisplay AIS presentation in viewer
 #ifndef HIGHLIGHT_STAYS_PROBLEM
   // an attempt to clear highlighted item in the viewer: but of OCCT
   XGUI_Tools::workshop(myWorkshop)->displayer()->clearSelected(true);
 #endif
-  updateObject(feature());
-  Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
-
-  aFilled = true;
-
-  return aFilled;
+  return true;
 }
 
 //********************************************************************
@@ -202,16 +254,22 @@ QList<ModuleBase_ViewerPrsPtr> PartSet_WidgetFeaturePointSelector::getAttributeS
 }
 
 //********************************************************************
-bool PartSet_WidgetFeaturePointSelector::setSelection(
-                                          QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
-                                          const bool theToValidate)
+bool PartSet_WidgetFeaturePointSelector::setSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
 {
-  // false is returned to do not emit focus out widget by selected sub-shape
-  return false;
+  if (!thePrs.get() || !thePrs->object().get())
+    return false;
+
+  ModuleBase_ISelection* aSelection = myWorkshop->selection();
+  myPreviewObject = aSelection->getResult(thePrs);
+  GeomShapePtr aShape = aSelection->getShape(thePrs);
+  myExternalObjectMgr->getGeomSelection(thePrs, myPreviewObject, aShape, myWorkshop,
+    sketch(), true);
+  return fillFeature();
 }
 
+//********************************************************************
 void PartSet_WidgetFeaturePointSelector::setPreSelection(
-                                  const std::shared_ptr<ModuleBase_ViewerPrs>& thePreSelected,
+                                  const ModuleBase_ViewerPrsPtr& thePreSelected,
                                   ModuleBase_IViewWindow* theWnd,
                                   QMouseEvent* theEvent)
 {
@@ -219,3 +277,23 @@ void PartSet_WidgetFeaturePointSelector::setPreSelection(
   // sub-segments in the viewer, secondly preselection of restart operation is processed by
   // special reentrant message sent by the feature
 }
+
+//********************************************************************
+void PartSet_WidgetFeaturePointSelector::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
+  ObjectPtr& theObject, GeomShapePtr& theShape)
+{
+  ModuleBase_WidgetShapeSelector::getGeomSelection(thePrs, theObject, theShape);
+
+  myExternalObjectMgr->getGeomSelection(thePrs, theObject, theShape,
+    myWorkshop, sketch(), myIsInValidate);
+  myPreviewObject = theObject;
+}
+
+//********************************************************************
+void PartSet_WidgetFeaturePointSelector::restoreAttributeValue(const AttributePtr& theAttribute,
+  const bool theValid)
+{
+  ModuleBase_WidgetShapeSelector::restoreAttributeValue(theAttribute, theValid);
+  myExternalObjectMgr->removeExternal(sketch(), myFeature, myWorkshop, true);
+  myPreviewObject = ObjectPtr();
+}