Salome HOME
Issue #2204 Display a sketch plane - highlight only source plane, do not create wrapp...
[modules/shaper.git] / src / PartSet / PartSet_WidgetFeaturePointSelector.cpp
index ed14ea8d6896a72b53a02f881e6c7b635386445b..2ee93fbb4ffbde724f2a9ff624178ccf2b22f62f 100644 (file)
@@ -1,8 +1,22 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-
-// File:        PartSet_WidgetFeaturePointSelector.cpp
-// Created:     28 Feb 2017
-// Author:      Natalia ERMOLAEVA
+// Copyright (C) 2014-2017  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
+// 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<mailto:webmaster.salome@opencascade.com>
+//
 
 #include <Config_WidgetAPI.h>
 
@@ -17,6 +31,7 @@
 #include <ModuleBase_ISelection.h>
 #include <ModuleBase_ViewerPrs.h>
 
+#include <ModelAPI_AttributeReference.h>
 #include <ModelAPI_Events.h>
 #include <ModelAPI_Feature.h>
 #include <ModelAPI_Tools.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,6 +64,13 @@ 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);
+
+  mySelectedObjectAttribute = anAttributesList[0].toStdString();
+  mySelectedPointAttribute = anAttributesList[1].toStdString();
+  myPreviewObjectAttribute = anAttributesList[2].toStdString();
+  myPreviewPointAttribute = anAttributesList[3].toStdString();
 }
 
 PartSet_WidgetFeaturePointSelector::~PartSet_WidgetFeaturePointSelector()
@@ -62,55 +81,54 @@ PartSet_WidgetFeaturePointSelector::~PartSet_WidgetFeaturePointSelector()
 bool PartSet_WidgetFeaturePointSelector::isValidSelection(
                                         const std::shared_ptr<ModuleBase_ViewerPrs>& theValue)
 {
-  return true;
+  return ModuleBase_WidgetShapeSelector::isValidSelection(theValue);
+  //return true;
 }
 
 //********************************************************************
-void PartSet_WidgetFeaturePointSelector::activateCustom()
+bool PartSet_WidgetFeaturePointSelector::activateSelectionAndFilters(bool toActivate)
 {
-  ModuleBase_WidgetShapeSelector::activateCustom();
-
-  myWorkshop->module()->activateCustomPrs(myFeature,
-                            ModuleBase_IModule::CustomizeHighlightedObjects, true);
-
-  Handle(AIS_InteractiveContext) aContext =
-                          XGUI_Tools::workshop(myWorkshop)->viewer()->AISContext();
-
-  std::vector<int> aColors;
-  aColors = Config_PropManager::color("Visualization", "sketch_entity_color",
-                                     SKETCH_ENTITY_COLOR);
-  Quantity_Color aColor(aColors[0] / 255., aColors[1] / 255., aColors[2] / 255., Quantity_TOC_RGB);
-
 #ifdef HIGHLIGHT_STAYS_PROBLEM
+  Handle(AIS_InteractiveContext) aContext =
+                            XGUI_Tools::workshop(myWorkshop)->viewer()->AISContext();
+  Quantity_Color aColor;
   Handle(Graphic3d_HighlightStyle) aHStyle = aContext->HighlightStyle();
-  myHighlightColor = aHStyle->Color();
+  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);
 
-  Handle(Graphic3d_HighlightStyle) aSStyle = aContext->SelectionStyle();
-  mySelectionColor = aSStyle->Color();
   aSStyle->SetColor(aColor);
   aContext->SetSelectionStyle(aSStyle);
+
 #endif
+
+  return ModuleBase_WidgetShapeSelector::activateSelectionAndFilters(toActivate);
 }
 
 //********************************************************************
-void PartSet_WidgetFeaturePointSelector::deactivate()
+void PartSet_WidgetFeaturePointSelector::activateCustom()
 {
-  ModuleBase_WidgetShapeSelector::deactivate();
-
-  Handle(AIS_InteractiveContext) aContext =
-                          XGUI_Tools::workshop(myWorkshop)->viewer()->AISContext();
+  ModuleBase_WidgetShapeSelector::activateCustom();
 
-#ifdef HIGHLIGHT_STAYS_PROBLEM
-  Handle(Graphic3d_HighlightStyle) aHStyle = aContext->HighlightStyle();
-  aHStyle->SetColor(myHighlightColor);
-  aContext->SetHighlightStyle(aHStyle);
+  myWorkshop->module()->activateCustomPrs(myFeature,
+                            ModuleBase_IModule::CustomizeHighlightedObjects, true);
+}
 
-  Handle(Graphic3d_HighlightStyle) aSStyle = aContext->SelectionStyle();
-  aSStyle->SetColor(mySelectionColor);
-  aContext->SetSelectionStyle(aSStyle);
-#endif
+//********************************************************************
+void PartSet_WidgetFeaturePointSelector::deactivate()
+{
+  ModuleBase_WidgetShapeSelector::deactivate();
 }
 
 //********************************************************************
@@ -120,10 +138,9 @@ void PartSet_WidgetFeaturePointSelector::mouseMoved(ModuleBase_IViewWindow* theW
   ModuleBase_ISelection* aSelect = myWorkshop->selection();
   QList<ModuleBase_ViewerPrsPtr> aHighlighted = aSelect->getHighlighted();
 
-  if (!aHighlighted.empty()) {
-    ModuleBase_ViewerPrsPtr aPrs = aHighlighted.first();
-    fillFeature(aPrs, theWindow, theEvent);
-  }
+  ModuleBase_ViewerPrsPtr aPrs = !aHighlighted.empty() ? aHighlighted.first()
+                                                       : ModuleBase_ViewerPrsPtr();
+  fillFeature(aPrs, theWindow, theEvent);
 }
 
 //********************************************************************
@@ -134,6 +151,30 @@ 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(myPreviewObjectAttribute));
+  ObjectPtr aPreviewObject = aRefPreviewAttr->value();
+  // 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(mySelectedObjectAttribute));
+  aRefSelectedAttr->setValue(aRefPreviewAttr->value());
+
+  std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
+                          std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+                          feature()->data()->attribute(mySelectedPointAttribute));
+  std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
+                          std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+                          feature()->data()->attribute(myPreviewPointAttribute));
+  aPointSelectedAttr->setValue(aPointPreviewAttr->x(), aPointPreviewAttr->y());
+
+  updateObject(feature());
+
   emit focusOutWidget(this);
   // we need to deselect base feature for better visibility of selected feature
   XGUI_Tools::workshop(myWorkshop)->displayer()->clearSelected(false);
@@ -146,30 +187,30 @@ bool PartSet_WidgetFeaturePointSelector::fillFeature(
                             QMouseEvent* theEvent)
 {
   bool aFilled = false;
-  if (theSelectedPrs.get() && theSelectedPrs->object().get()) {
-    ObjectPtr anObject = theSelectedPrs->object();
-    gp_Pnt aPnt = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWindow->v3dView());
-    double aX, anY;
-    Handle(V3d_View) aView = theWindow->v3dView();
-    PartSet_Tools::convertTo2D(aPnt, mySketch, aView, aX, anY);
-
-    std::shared_ptr<ModelAPI_AttributeReference> aRef =
-                            std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
-                            feature()->data()->attribute(SketchPlugin_Trim::BASE_OBJECT()));
-    aRef->setValue(anObject);
-
-    std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
-                    std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-                    feature()->data()->attribute(SketchPlugin_Trim::ENTITY_POINT()));
-    anAttributePoint->setValue(aX, anY);
-    // redisplay AIS presentation in viewer
+  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(myPreviewObjectAttribute));
+  aRef->setValue(anObject);
+
+  std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
+                  std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+                  feature()->data()->attribute(myPreviewPointAttribute));
+  std::shared_ptr<GeomAPI_Pnt2d> aPoint = PartSet_Tools::getPnt2d(theEvent, theWindow, mySketch);
+  anAttributePoint->setValue(aPoint);
+  // 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);
+  // an attempt to clear highlighted item in the viewer: but of OCCT
+  XGUI_Tools::workshop(myWorkshop)->displayer()->clearSelected(true);
 #endif
-    updateObject(feature());
-    aFilled = true;
-  }
+  updateObject(feature());
+  Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
+
+  aFilled = true;
+
   return aFilled;
 }
 
@@ -179,7 +220,6 @@ QList<ModuleBase_ViewerPrsPtr> PartSet_WidgetFeaturePointSelector::getAttributeS
   return QList<ModuleBase_ViewerPrsPtr>();
 }
 
-
 //********************************************************************
 bool PartSet_WidgetFeaturePointSelector::setSelection(
                                           QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
@@ -194,6 +234,7 @@ void PartSet_WidgetFeaturePointSelector::setPreSelection(
                                   ModuleBase_IViewWindow* theWnd,
                                   QMouseEvent* theEvent)
 {
-  if (fillFeature(thePreSelected, theWnd, theEvent))
-    mouseReleased(theWnd, theEvent);
+  // the method is empty because firstly by starging of the feature there is no selection of
+  // sub-segments in the viewer, secondly preselection of restart operation is processed by
+  // special reentrant message sent by the feature
 }