Salome HOME
Copyright update 2020
[modules/shaper.git] / src / XGUI / XGUI_Displayer.h
index da11c4d8ef6f65ba1ca039fe7f9c208d41c92a43..80d1806576bc06975d86fd77618767c8ee72a9fc 100644 (file)
@@ -1,8 +1,21 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
-
-// File:        XGUI_Displayer.h
-// Created:     20 Apr 2014
-// Author:      Natalia ERMOLAEVA
+// Copyright (C) 2014-2020  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
+//
 
 #ifndef XGUI_Displayer_H
 #define XGUI_Displayer_H
 #include "XGUI.h"
 
 #include <GeomAPI_AISObject.h>
-#include <TopoDS_Shape.hxx>
-#include <AIS_InteractiveObject.hxx>
-#include <AIS_InteractiveContext.hxx>
-#include <NCollection_List.hxx>
+#include <GeomAPI_Pln.h>
 
 #include <ModelAPI_Result.h>
 
 #include <ModuleBase_Definitions.h>
-#include <ModuleBase_ViewerPrs.h>
-
-#include <GeomAPI_ICustomPrs.h>
 
+#include <AIS_InteractiveObject.hxx>
+#include <AIS_InteractiveContext.hxx>
+#include <NCollection_Map.hxx>
+#include <NCollection_DataMap.hxx>
 #include <SelectMgr_AndFilter.hxx>
+#include <TopoDS_Shape.hxx>
 
-#include <QString>
+#include <QColor>
 #include <QMap>
+#include <QObject>
+#include <QString>
 
+class ModuleBase_ViewerPrs;
 class ModelAPI_Feature;
+class XGUI_SelectionActivate;
 class XGUI_Workshop;
 
+#define OPTIMIZE_PRS
+
+#ifdef TINSPECTOR
+class VInspectorAPI_CallBack;
+#endif
+
+
+#ifdef OPTIMIZE_PRS
+class XGUI_TwoSidePresentationMap
+{
+public:
+  ~XGUI_TwoSidePresentationMap() { clear(); }
+
+  /// Add new values pair to the map
+  /// \param theObj an object
+  /// \param theAIS a corresponded presentation
+  bool add(const ObjectPtr& theObj, const AISObjectPtr& theAIS)
+  {
+    if (myResultToAISMap.contains(theObj))
+      return false;
+    Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
+    myResultToAISMap[theObj] = anAIS;
+    myAIStoResultMap[anAIS] = theObj;
+    return true;
+  }
+
+  /// Removes values by object
+  /// \param theObj an object
+  bool remove(const ObjectPtr& theObj)
+  {
+    if (!myResultToAISMap.contains(theObj))
+      return false;
+    Handle(AIS_InteractiveObject) aAIS = myResultToAISMap[theObj];
+    myResultToAISMap.remove(theObj);
+    myAIStoResultMap.remove(aAIS);
+    return true;
+  }
+
+  /// Removes values by presentation
+  /// \param theAIS a presentation
+  bool remove(const AISObjectPtr& theAIS)
+  {
+    Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
+    if (!myAIStoResultMap.contains(anAIS))
+      return false;
+    ObjectPtr aObj = myAIStoResultMap[anAIS];
+    myResultToAISMap.remove(aObj);
+    myAIStoResultMap.remove(anAIS);
+    return true;
+  }
+
+  /// Removes all values
+  void clear()
+  {
+    myResultToAISMap.clear();
+    myAIStoResultMap.clear();
+  }
+
+  /// Returns presentation by object
+  /// \param theObj an object
+  AISObjectPtr value(const ObjectPtr& theObj) const
+  {
+    if (myResultToAISMap.contains(theObj)) {
+      Handle(AIS_InteractiveObject) anAIS = myResultToAISMap[theObj];
+      AISObjectPtr anAISObj = AISObjectPtr(new GeomAPI_AISObject());
+      anAISObj->setImpl(new Handle(AIS_InteractiveObject)(anAIS));
+      return anAISObj;
+    }
+    return AISObjectPtr();
+  }
+
+  /// Returns object by presentation
+  /// \param theAIS a presentation
+  ObjectPtr value(const AISObjectPtr& theAIS) const
+  {
+    Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
+    if (myAIStoResultMap.contains(anAIS))
+      return myAIStoResultMap[anAIS];
+    return ObjectPtr();
+  }
+
+  /// Returns object by presentation
+  /// \param theAIS a presentation
+  ObjectPtr value(const Handle(AIS_InteractiveObject)& theAIS) const
+  {
+    if (myAIStoResultMap.contains(theAIS))
+      return myAIStoResultMap[theAIS];
+    return ObjectPtr();
+  }
+
+  /// Returns number of values
+  int size() const { return myResultToAISMap.size(); }
+
+  /// Returns list of objects
+  QObjectPtrList objects() const { return myResultToAISMap.keys(); }
+
+  /// returns list of presentations
+  QList<Handle(AIS_InteractiveObject)> presentations() const { return myAIStoResultMap.keys(); }
+
+  /// Returns true if the Map contains the object
+  /// \param theObj an object
+  bool contains(const ObjectPtr& theObj) const { return myResultToAISMap.contains(theObj); }
+
+  /// Returns true if the Map contains the presentation
+  /// \param theAIS a presentation
+  bool contains(const AISObjectPtr& theAIS) const
+  {
+    Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
+    return myAIStoResultMap.contains(anAIS);
+  }
+
+private:
+  QMap<ObjectPtr, Handle(AIS_InteractiveObject)> myResultToAISMap;
+  QMap<Handle(AIS_InteractiveObject), ObjectPtr> myAIStoResultMap;
+};
+#endif
+
+
 /**\class XGUI_Displayer
  * \ingroup GUI
  * \brief Displayer. Provides mechanizm of display/erase of objects in the viewer
  */
-class XGUI_EXPORT XGUI_Displayer
+class XGUI_EXPORT XGUI_Displayer : public QObject
 {
- public:
-   /// \enum DisplayMode display mode
-   enum DisplayMode { 
-     /// Mode is not defined
-     NoMode = -1, 
-     /// Wireframe display mode
-     Wireframe, 
-     /// Shading display mode
-     Shading      
-   };
+  Q_OBJECT
+public:
+  /// \enum DisplayMode display mode
+  enum DisplayMode {
+    NoMode = -1, ///< Mode is not defined
+    Wireframe, ///< Wireframe display mode
+    Shading ///< Shading display mode
+  };
 
   /// Constructor
   /// \param theWorkshop a workshop instance
@@ -60,43 +192,65 @@ class XGUI_EXPORT XGUI_Displayer
 
   /// Display the feature. Obtain the visualized object from the feature.
   /// \param theObject an object to display
-  /// \param isUpdateViewer the parameter whether the viewer should be update immediatelly
-  /// Returns true if the Feature succesfully displayed
-  void display(ObjectPtr theObject, bool isUpdateViewer = true);
-
-  /// Display the given AIS object. To hide this object use corresponde erase method
+  /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
+  /// \return true if the object visibility state is changed
+  bool display(ObjectPtr theObject, bool theUpdateViewer = true);
+
+  /// Display the given AIS object.
+  /// This object is not added to the displayer internal map of objects
+  /// So, it can not be obtained from displayer. This is just a wrap method of OCC display in
+  /// order to perform the display with correct flags.
   /// \param theAIS AIOS object to display
-  /// \param isUpdate the parameter whether the viewer should be update immediatelly
-  void displayAIS(AISObjectPtr theAIS, bool isUpdate = true);
+  /// \param toActivateInSelectionModes boolean value whether the presentation should be
+  /// activated in the current selection modes
+  /// \param theDisplayMode mode how the presentation should be displayed
+  /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
+  /// \return true if the object visibility state is changed
+  bool displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
+    const Standard_Integer theDisplayMode = 0, bool theUpdateViewer = true);
+
+  /// Redisplay the shape if it was displayed
+  /// \param theObject an object instance
+  /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
+  /// \return true if the object visibility state is changed
+  bool redisplay(ObjectPtr theObject, bool theUpdateViewer = true);
 
-  /**
-   * Add presentations which corresponds to the given features to current selection
-   * \param theFeatures a list of features to be selected
-   * \param isUpdateViewer the parameter whether the viewer should be update immediatelly
-   */
-  void setSelected(const QObjectPtrList& theFeatures, bool isUpdateViewer = true);
+  /// Sends and flushes a signal to redisplay all visualized objects.
+  void redisplayObjects();
 
+  /// Add presentations to current selection. It unhighlight and deselect the current selection.
+  /// The shape and result components are processed in the values. If the presentation shape is not
+  /// empty, select it, otherwise select the result.
+  /// \param theValues a list of presentation to be selected
+  /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
+  void setSelected(const  QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
+    bool theUpdateViewer = true);
 
   /// Unselect all objects
-  void clearSelected();
+  /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
+  /// \param theUpdateViewer the parameter to update viewer
+  void clearSelected(const bool theUpdateViewer = true);
 
   /// Erase the feature and a shape.
   /// \param theObject an object instance
-  /// \param isUpdateViewer the parameter whether the viewer should be update immediatelly
-  void erase(ObjectPtr theObject, const bool isUpdateViewer = true);
+  /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
+  /// \return true if the object visibility state is changed
+  bool erase(ObjectPtr theObject, const bool theUpdateViewer = true);
 
   /// Erase the given AIS object displayed by corresponded display method
   /// \param theAIS instance of AIS object
-  /// \param isUpdate the parameter whether the viewer should be update immediatelly
-  void eraseAIS(AISObjectPtr theAIS, const bool isUpdate = true);
+  /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
+  /// \return true if the object visibility state is changed
+  bool eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer = true);
 
   /// Erase all presentations
-  /// \param isUpdateViewer the parameter whether the viewer should be update immediatelly
-  void eraseAll(const bool isUpdateViewer = true);
+  /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
+  /// \return true if the object visibility state is changed
+  bool eraseAll(const bool theUpdateViewer = true);
 
-  /// Deactivates selection of sub-shapes
-  /// \param isUpdateViewer the parameter whether the viewer should be update immediatelly
-  void closeLocalContexts(const bool isUpdateViewer = true);
+  /// Remove default selection filters of the module from the current viewer
+  /// \param theAddFilterOnly if is not 'true' it will deactivate all fiters in viewer
+  void deactivateSelectionFilters(const bool theAddFilterOnly = true);
 
   /// \brief Add selection filter
   /// \param theFilter a filter instance
@@ -106,18 +260,29 @@ class XGUI_EXPORT XGUI_Displayer
   /// \param theFilter a filter instance
   void removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
 
+  /// Returns true if the selection filter is set to the viewer
+  /// \param theFilter a selection filter
+  virtual bool hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
+
   /// Remove all selection filters
   void removeFilters();
 
-  /**
-   * Sets a flag to the displayer whether the internal viewer can be updated by 
-   * the updateViewer method call. If it is not enabled, this method do nothing
-   * \param isEnabled a boolean value
-   */
+  /// Sets a flag to the displayer whether the internal viewer can be updated by
+  /// the updateViewer method call. If it is not enabled, this method do nothing.
+  /// This state maintain recurse, if the update is blocked twice or three times, the
+  /// viewer will not be updated until it is unblocked necessary times
+  /// (twice or three in the example).
+  /// \param isEnabled a boolean value
   bool enableUpdateViewer(const bool isEnabled);
 
+  /// Returns true if the viewer update is not blocked
+  bool isUpdateEnabled() const
+  {
+    return myViewerBlockedRecursiveCount == 0;
+  }
+
   /// Updates the viewer
-  void updateViewer();
+  void updateViewer() const;
 
   /// Searches the interactive object by feature
   /// \param theObject the object or presentable feature
@@ -134,37 +299,14 @@ class XGUI_EXPORT XGUI_Displayer
   /// \return corresponded object or NULL if it not found
   ObjectPtr getObject(const Handle(AIS_InteractiveObject)& theIO) const;
 
-  /// Deactivates the given object (not allow selection)
-  /// \param theObject object to deactivate
-  void deactivate(ObjectPtr theObject);
-
-  /// Activates the given object (it can be selected)
-  /// \param theObject object to activate
-  /// \param theModes - modes on which it has to be activated (can be empty)
-  //void activate(ObjectPtr theObject, const QIntList& theModes);
-
-  /// Returns the modes of activation
-  /// \param theObject the feature or NULL if it not visualized
-  /// \param theModes - modes on which it is activated (can be empty)
-  void getModesOfActivation(ObjectPtr theObject, QIntList& theModes);
-
-  /// Activates the given object with default modes
-  /// \param theObject object to activate
-  //void activate(ObjectPtr theObject);
-
-  /// Returns true if the given object can be selected
-  /// \param theObject object to check
-  bool isActive(ObjectPtr theObject) const;
-
-  /// Activates in local context displayed outside of the context.
-  /// \param theModes - modes on which it has to be activated (can be empty)
-  void activateObjects(const QIntList& theModes);
-
-  /// Activates in local context displayed outside of the context.
-  void deactivateObjects();
+  /// Deactivates the given objects (not allow selection)
+  /// \param theObjList - list of objects which has to be deactivated.
+  /// \param theUpdateViewer update viewer flag
+  void deactivateObjects(const QObjectPtrList& theObjList,
+    const bool theUpdateViewer = true);
 
   /// Sets display mode for the given object if this object is displayed
-  void setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate = true);
+  void setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer = true);
 
   /// Returns current display mode for the given object.
   /// If object is not dis played then returns NoMode.
@@ -179,12 +321,80 @@ class XGUI_EXPORT XGUI_Displayer
   int objectsCount() const { return myResult2AISObjectMap.size(); }
 
   /// Returns list of displayed objects
-  QObjectPtrList displayedObjects() const { return myResult2AISObjectMap.keys(); }
+  QObjectPtrList displayedObjects() const {
+#ifdef OPTIMIZE_PRS
+    return myResult2AISObjectMap.objects();
+#else
+    return myResult2AISObjectMap.keys();
+#endif
+  }
+
+  /// Returns list of displayed objects
+#ifdef OPTIMIZE_PRS
+  QList<Handle(AIS_InteractiveObject)> displayedPresentations() const
+  {
+    return myResult2AISObjectMap.presentations();
+  }
+#else
+  QList<AISObjectPtr> displayedPresentations() const
+  {
+    return myResult2AISObjectMap.values();
+  }
+#endif
 
   /// Returns true if the given object can be shown in shaded mode
   /// \param theObject object to check
   bool canBeShaded(ObjectPtr theObject) const;
 
+  /// Set color on presentation of an object if it is displayed
+  /// \param theObject an object
+  /// \param theColor a color which has to be set
+  /// \param theUpdateViewer update viewer flag
+  /// \return previously defined color on the object
+  QColor setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer = true);
+
+  /// Displays/erases thrihedron in current modes. It will be activated or deactivated
+  /// depending on the trihedron visible state and displayer active trihedron state
+  void displayTrihedron(bool theToDisplay) const;
+
+#ifdef TINSPECTOR
+  void setCallBack(const Handle(VInspectorAPI_CallBack)& theCallBack)
+    { myVCallBack = theCallBack; }
+  Handle(VInspectorAPI_CallBack) getCallBack() const { return myVCallBack; }
+#endif
+
+  /// Return true if the object is visible. If the object is feature, it returns true
+  /// if all results of the feature are shown
+  /// \param theDisplayer a displayer
+  /// \param theObject an object
+  /// \return a boolean value
+  static bool isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject);
+
+
+  /// Returns screen plane of active view
+  GeomPlanePtr getScreenPlane() const;
+
+  /// Returns scale of active view
+  double getViewScale() const;
+
+  /// Set color of selection
+  /// \param theColor R,G,B values of color
+  void setSelectionColor(const std::vector<int>& theColor);
+
+  /// Returns current selection color
+  std::vector<int> selectionColor() const;
+
+signals:
+  /// Signal on object display
+  /// \param theObject a data object
+  /// \param theAIS a presentation object
+  void objectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS);
+
+  /// Signal on before object erase
+  /// \param theObject a data object
+  /// \param theAIS a presentation object
+  void beforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS);
+
  protected:
   /// Returns currently installed AIS_InteractiveContext
   Handle(AIS_InteractiveContext) AISContext() const;
@@ -197,54 +407,71 @@ class XGUI_EXPORT XGUI_Displayer
   /// \param theObject an object instance
   /// \param theAIS AIS presentation
   /// \param isShading flag to show in shading mode
-  /// \param isUpdateViewer the parameter whether the viewer should be update immediatelly
-  /// \return true if the object is succesfully displayed
-  void display(ObjectPtr theObject, AISObjectPtr theAIS, bool isShading,
-               bool isUpdateViewer = true);
-
-  /** Redisplay the shape if it was displayed
-   * \param theObject an object instance
-   * \param isUpdateViewer the parameter whether the viewer should be update immediatelly
-   */
-  void redisplay(ObjectPtr theObject, bool isUpdateViewer = true);
-
-  /// Activates the interactive object in the local context.
-  /// \param theIO an interactive object
-  /// \param theModes - modes on which it has to be activated (can be empty)
-  void activate(const Handle(AIS_InteractiveObject)& theIO, const QIntList& theModes) const;
-
-  /// Opens local context. Does nothing if it is already opened.
-  void openLocalContext();
-
-  /** Update the object presentable properties such as color, lines width and other
-   * If the object is result with the color attribute value set, it is used,
-   * otherwise the customize is applyed to the object's feature if it is a custom prs
-   * \param theObject an object instance
-   * \return the true state if there is changes and the presentation is customized
-   */
+  /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
+  /// \return true if the object visibility state is changed
+  bool display(ObjectPtr theObject, AISObjectPtr theAIS, bool isShading,
+               bool theUpdateViewer = true);
+
+private:
+  /// Update the object presentable properties such as color, lines width and other
+  /// If the object is result with the color attribute value set, it is used,
+  /// otherwise the customize is applyed to the object's feature if it is a custom prs
+  /// \param theObject an object instance
+  /// \return the true state if there is changes and the presentation is customized
   bool customizeObject(ObjectPtr theObject);
 
- protected:
-   /// Reference to workshop
-  XGUI_Workshop* myWorkshop;
+  /// Append the objects in the internal map. Checks whether the map already contains the object
+  /// \param theObject an object to display
+  /// \param theAIS AIOS object to display
+  void appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS);
 
-  /// A container for selection filters
-  Handle(SelectMgr_AndFilter) myAndFilter;
+#ifdef _DEBUG
+  /// Returns an information about alredy displayed objects
+  /// \return a string representation
+  std::string getResult2AISObjectMapInfo() const;
+#endif
 
-  /// A default custom presentation, which is used if the displayed feature is not a custom presentation
-  GeomCustomPrsPtr myCustomPrs;
+  /// Returns container of visible presentations for the object. For a feature object,
+  /// the feature results are processed also. The presentations map is not cleared inside.
+  /// \param theObject a feature or result
+  /// \param thePresentations result map of presentations
+  void getPresentations(const ObjectPtr& theObject,
+                        NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations);
+
+  /// Sets the shapes selected in the context. It contains logic of the similar method
+  /// in OCCT but improved for performance. The modification is to iterates by a list
+  /// of owners in the context only once.
+  /// \param theContext a viewer context. It has opened local context
+  /// \param theShapesToBeSelected a map of shapes. Owner's shape is searched in the map and the
+  /// owner is selected if it is found there.
+  /// Only first owner is processed(according to OCCT logic)
+  static void AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
+    const NCollection_DataMap<TopoDS_Shape,
+      NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected);
+
+private:
+  XGUI_SelectionActivate* selectionActivate() const;
+
+private:
+  XGUI_Workshop* myWorkshop; ///< Reference to workshop
+#ifdef TINSPECTOR
+  Handle(VInspectorAPI_CallBack) myVCallBack;
+#endif
+  Handle(SelectMgr_AndFilter) myAndFilter; ///< A container for selection filters
 
   /// Definition of a type of map which defines correspondance between objects and presentations
+#ifdef OPTIMIZE_PRS
+  XGUI_TwoSidePresentationMap myResult2AISObjectMap; ///< A map of displayed objects
+#else
   typedef QMap<ObjectPtr, AISObjectPtr> ResultToAISMap;
+  ResultToAISMap myResult2AISObjectMap; ///< A map of displayed objects
+#endif
 
-  /// A map of displayed objects
-  ResultToAISMap myResult2AISObjectMap;
-
-  /// Selection modes installed for external objects in local context
-  QIntList myActiveSelectionModes;
+  /// Number of blocking of the viewer update. The viewer is updated only if it is zero
+  int myViewerBlockedRecursiveCount;
 
-  /// the enable update viewer flag
-  bool myEnableUpdateViewer;  
+  mutable void* myContextId;
+  mutable bool myNeedUpdate; ///< A flag that update was requested but not done
 };
 
 #endif