Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ADSObserver in ContourPreviewPlot widget #37411

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Poco/NObserver.h>

#include <QColor>
#include <QWidget>

Expand All @@ -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);

Expand All @@ -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<ContourPreviewPlot, Mantid::API::WorkspacePreDeleteNotification> m_wsRemovedObserver;
Poco::NObserver<ContourPreviewPlot, Mantid::API::WorkspaceBeforeReplaceNotification> m_wsReplacedObserver;
};

} // namespace MantidWidgets
Expand Down
34 changes: 12 additions & 22 deletions qt/widgets/plotting/src/ContourPreviewPlot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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 &notificationCenter = 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<MatrixWorkspace>(nf->object())) {
void ContourPreviewPlot::deleteHandle(const std::string &wsName, const Workspace_sptr &workspace) {
(void)wsName;
if (auto matrixWorkspace = std::dynamic_pointer_cast<MatrixWorkspace>(workspace)) {
// If the artist has already been removed, ignore.
bool workspaceRemoved = false;
try {
workspaceRemoved = m_canvas->gca<MantidAxes>().removeWorkspaceArtists(workspace);
workspaceRemoved = m_canvas->gca<MantidAxes>().removeWorkspaceArtists(matrixWorkspace);
} catch (Mantid::PythonInterface::PythonException &) {
}
if (workspaceRemoved) {
Expand All @@ -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<MatrixWorkspace>(nf->oldObject())) {
if (auto newWorkspace = std::dynamic_pointer_cast<MatrixWorkspace>(nf->newObject())) {
if (m_canvas->gca<MantidAxes>().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<MatrixWorkspace>(workspace)) {
if (m_canvas->gca<MantidAxes>().replaceWorkspaceArtists(newWorkspace)) {
m_canvas->draw();
}
}
}
Expand Down
Loading