From 8e01c54d70d92807aab9dda05e6695c07cd4162e Mon Sep 17 00:00:00 2001
From: Matthew Andrew <matthew.andrew@tessella.com>
Date: Mon, 9 Mar 2020 12:26:09 +0000
Subject: [PATCH] Updated previewplot to only replot if it contains workspace
 Re #

Previously on workbench the PreviewPlot mpl implementation was re-drawing every axes when any workspace changed. This was leading to the time taken to respond to ADS changed signals being high.
---
 .../inc/MantidQtWidgets/MplCpp/MantidAxes.h    |  4 ++--
 qt/widgets/mplcpp/src/MantidAxes.cpp           | 10 ++++++----
 qt/widgets/plotting/src/Mpl/PreviewPlot.cpp    | 18 ++++++++++++++----
 3 files changed, 22 insertions(+), 10 deletions(-)

diff --git a/qt/widgets/mplcpp/inc/MantidQtWidgets/MplCpp/MantidAxes.h b/qt/widgets/mplcpp/inc/MantidQtWidgets/MplCpp/MantidAxes.h
index b8fe6481941..5876a9af511 100644
--- a/qt/widgets/mplcpp/inc/MantidQtWidgets/MplCpp/MantidAxes.h
+++ b/qt/widgets/mplcpp/inc/MantidQtWidgets/MplCpp/MantidAxes.h
@@ -47,8 +47,8 @@ public:
 
   /// @name Artist removal/replacement
   ///@{
-  void removeWorkspaceArtists(const Mantid::API::MatrixWorkspace_sptr &ws);
-  void replaceWorkspaceArtists(const Mantid::API::MatrixWorkspace_sptr &newWS);
+  bool removeWorkspaceArtists(const Mantid::API::MatrixWorkspace_sptr &ws);
+  bool replaceWorkspaceArtists(const Mantid::API::MatrixWorkspace_sptr &newWS);
   ///@}
 };
 
diff --git a/qt/widgets/mplcpp/src/MantidAxes.cpp b/qt/widgets/mplcpp/src/MantidAxes.cpp
index c5df093cb1a..dc3b4edfa56 100644
--- a/qt/widgets/mplcpp/src/MantidAxes.cpp
+++ b/qt/widgets/mplcpp/src/MantidAxes.cpp
@@ -96,25 +96,27 @@ void MantidAxes::pcolormesh(
  * @param ws A reference to a workspace whose name is used to
  * lookup any artists for removal
  */
-void MantidAxes::removeWorkspaceArtists(
+bool MantidAxes::removeWorkspaceArtists(
     const Mantid::API::MatrixWorkspace_sptr &ws) {
   GlobalInterpreterLock lock;
+  bool removed = false;
   try {
-    pyobj().attr("remove_workspace_artists")(
+    removed = pyobj().attr("remove_workspace_artists")(
         Python::NewRef(MatrixWorkpaceToPython()(ws)));
   } catch (Python::ErrorAlreadySet &) {
     throw Mantid::PythonInterface::PythonException();
   }
+  return removed;
 }
 
 /**
  * Replace the artists on this axes instance that are based off this workspace
  * @param newWS A reference to the new workspace containing the data
  */
-void MantidAxes::replaceWorkspaceArtists(
+bool MantidAxes::replaceWorkspaceArtists(
     const Mantid::API::MatrixWorkspace_sptr &newWS) {
   GlobalInterpreterLock lock;
-  pyobj().attr("replace_workspace_artists")(
+  return pyobj().attr("replace_workspace_artists")(
       Python::NewRef(MatrixWorkpaceToPython()(newWS)));
 }
 
diff --git a/qt/widgets/plotting/src/Mpl/PreviewPlot.cpp b/qt/widgets/plotting/src/Mpl/PreviewPlot.cpp
index f6581efa370..4554c8aca96 100644
--- a/qt/widgets/plotting/src/Mpl/PreviewPlot.cpp
+++ b/qt/widgets/plotting/src/Mpl/PreviewPlot.cpp
@@ -588,15 +588,21 @@ QStringList PreviewPlot::linesWithErrors() const {
  */
 void PreviewPlot::onWorkspaceRemoved(
     Mantid::API::WorkspacePreDeleteNotification_ptr nf) {
+  if (m_lines.isEmpty()) {
+    return;
+  }
   // Ignore non matrix workspaces
   if (auto ws = boost::dynamic_pointer_cast<MatrixWorkspace>(nf->object())) {
     // the artist may have already been removed. ignore the event is that is the
     // case
+    bool removed = false;
     try {
-      m_canvas->gca<MantidAxes>().removeWorkspaceArtists(ws);
+      removed = m_canvas->gca<MantidAxes>().removeWorkspaceArtists(ws);
     } catch (Mantid::PythonInterface::PythonException &) {
     }
-    this->replot();
+    if (removed) {
+      this->replot();
+    }
   }
 }
 
@@ -606,13 +612,17 @@ void PreviewPlot::onWorkspaceRemoved(
  */
 void PreviewPlot::onWorkspaceReplaced(
     Mantid::API::WorkspaceBeforeReplaceNotification_ptr nf) {
+  if (m_lines.isEmpty()) {
+    return;
+  }
   // Ignore non matrix workspaces
   if (auto oldWS =
           boost::dynamic_pointer_cast<MatrixWorkspace>(nf->oldObject())) {
     if (auto newWS =
             boost::dynamic_pointer_cast<MatrixWorkspace>(nf->newObject())) {
-      m_canvas->gca<MantidAxes>().replaceWorkspaceArtists(newWS);
-      this->replot();
+      if (m_canvas->gca<MantidAxes>().replaceWorkspaceArtists(newWS)) {
+        this->replot();
+      }
     }
   }
 }
-- 
GitLab