diff --git a/qt/widgets/plotting/inc/MantidQtWidgets/Plotting/ContourPreviewPlot.h b/qt/widgets/plotting/inc/MantidQtWidgets/Plotting/ContourPreviewPlot.h index 1897e6a31be9..fa817582cc0f 100644 --- a/qt/widgets/plotting/inc/MantidQtWidgets/Plotting/ContourPreviewPlot.h +++ b/qt/widgets/plotting/inc/MantidQtWidgets/Plotting/ContourPreviewPlot.h @@ -7,12 +7,11 @@ #pragma once #include "MantidAPI/AnalysisDataService.h" +#include "MantidAPI/AnalysisDataServiceObserver.h" #include "MantidAPI/MatrixWorkspace.h" #include "MantidQtWidgets/Plotting/AxisID.h" #include "MantidQtWidgets/Plotting/DllOption.h" -#include - #include #include @@ -28,12 +27,11 @@ class FigureCanvasQt; namespace MantidWidgets { -class EXPORT_OPT_MANTIDQT_PLOTTING ContourPreviewPlot : public QWidget { +class EXPORT_OPT_MANTIDQT_PLOTTING ContourPreviewPlot : public QWidget, public AnalysisDataServiceObserver { Q_OBJECT public: ContourPreviewPlot(QWidget *parent = nullptr, bool observeADS = true); - ~ContourPreviewPlot() override; void watchADS(bool on); @@ -47,15 +45,11 @@ class EXPORT_OPT_MANTIDQT_PLOTTING ContourPreviewPlot : public QWidget { private: void createLayout(); - void onWorkspaceRemoved(Mantid::API::WorkspacePreDeleteNotification_ptr nf); - void onWorkspaceReplaced(Mantid::API::WorkspaceBeforeReplaceNotification_ptr nf); + void replaceHandle(const std::string &wsName, const Workspace_sptr &workspace) override; + void deleteHandle(const std::string &wsName, const Workspace_sptr &workspace) override; /// Canvas objects Widgets::MplCpp::FigureCanvasQt *m_canvas; - - /// Observers for ADS Notifications - Poco::NObserver m_wsRemovedObserver; - Poco::NObserver m_wsReplacedObserver; }; } // namespace MantidWidgets diff --git a/qt/widgets/plotting/src/ContourPreviewPlot.cpp b/qt/widgets/plotting/src/ContourPreviewPlot.cpp index ab5cd1b6820d..5a35c9d2b694 100644 --- a/qt/widgets/plotting/src/ContourPreviewPlot.cpp +++ b/qt/widgets/plotting/src/ContourPreviewPlot.cpp @@ -22,15 +22,11 @@ constexpr auto MANTID_PROJECTION = "mantid"; namespace MantidQt::MantidWidgets { ContourPreviewPlot::ContourPreviewPlot(QWidget *parent, bool observeADS) - : QWidget(parent), m_canvas(new FigureCanvasQt(111, MANTID_PROJECTION, parent)), - m_wsRemovedObserver(*this, &ContourPreviewPlot::onWorkspaceRemoved), - m_wsReplacedObserver(*this, &ContourPreviewPlot::onWorkspaceReplaced) { + : QWidget(parent), m_canvas(new FigureCanvasQt(111, MANTID_PROJECTION, parent)) { createLayout(); watchADS(observeADS); } -ContourPreviewPlot::~ContourPreviewPlot() { watchADS(false); } - /** * Initialize the layout for the widget */ @@ -47,26 +43,21 @@ void ContourPreviewPlot::createLayout() { * @param on If true ADS observers are enabled else they are disabled */ void ContourPreviewPlot::watchADS(bool on) { - auto ¬ificationCenter = AnalysisDataService::Instance().notificationCenter; - if (on) { - notificationCenter.addObserver(m_wsRemovedObserver); - notificationCenter.addObserver(m_wsReplacedObserver); - } else { - notificationCenter.removeObserver(m_wsReplacedObserver); - notificationCenter.removeObserver(m_wsRemovedObserver); - } + this->observeReplace(on); + this->observeDelete(on); } /** * Observer method called when a workspace is removed from the ADS * @param nf A pointer to the notification object */ -void ContourPreviewPlot::onWorkspaceRemoved(Mantid::API::WorkspacePreDeleteNotification_ptr nf) { - if (auto workspace = std::dynamic_pointer_cast(nf->object())) { +void ContourPreviewPlot::deleteHandle(const std::string &wsName, const Workspace_sptr &workspace) { + (void)wsName; + if (auto matrixWorkspace = std::dynamic_pointer_cast(workspace)) { // If the artist has already been removed, ignore. bool workspaceRemoved = false; try { - workspaceRemoved = m_canvas->gca().removeWorkspaceArtists(workspace); + workspaceRemoved = m_canvas->gca().removeWorkspaceArtists(matrixWorkspace); } catch (Mantid::PythonInterface::PythonException &) { } if (workspaceRemoved) { @@ -79,12 +70,11 @@ void ContourPreviewPlot::onWorkspaceRemoved(Mantid::API::WorkspacePreDeleteNotif * Observer method called when a workspace is replaced in the ADS * @param nf A pointer to the notification object */ -void ContourPreviewPlot::onWorkspaceReplaced(Mantid::API::WorkspaceBeforeReplaceNotification_ptr nf) { - if (std::dynamic_pointer_cast(nf->oldObject())) { - if (auto newWorkspace = std::dynamic_pointer_cast(nf->newObject())) { - if (m_canvas->gca().replaceWorkspaceArtists(newWorkspace)) { - m_canvas->draw(); - } +void ContourPreviewPlot::replaceHandle(const std::string &wsName, const Workspace_sptr &workspace) { + (void)wsName; + if (auto newWorkspace = std::dynamic_pointer_cast(workspace)) { + if (m_canvas->gca().replaceWorkspaceArtists(newWorkspace)) { + m_canvas->draw(); } } }