Mantid
Loading...
Searching...
No Matches
WorkspaceObserver.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7//-----------------------------------
8// Includes
9//-----------------------------------
10#include <utility>
11
14
15namespace MantidQt::API {
16
17//---------------------------------------------------------------------------
18// Observer callback
19//---------------------------------------------------------------------------
22}
23
24void ObserverCallback::handlePostDelete(const std::string &name) { m_observer->postDeleteHandle(name); }
25
28}
29
32}
33
34void ObserverCallback::handleRename(const std::string &oldName, const std::string &newName) {
35 m_observer->renameHandle(oldName, newName);
36}
37
39
40//---------------------------------------------------------------------------
41// WorkspaceObserver
42//---------------------------------------------------------------------------
43
46 : m_preDeleteObserver(*this, &WorkspaceObserver::_preDeleteHandle),
47 m_postDeleteObserver(*this, &WorkspaceObserver::_postDeleteHandle),
48 m_addObserver(*this, &WorkspaceObserver::_addHandle),
49 m_afterReplaceObserver(*this, &WorkspaceObserver::_afterReplaceHandle),
50 m_renameObserver(*this, &WorkspaceObserver::_renameHandle),
51 m_clearADSObserver(*this, &WorkspaceObserver::_clearADSHandle), m_proxy(std::make_unique<ObserverCallback>(this)),
52 m_predel_observed(false), m_postdel_observed(false), m_add_observed(false), m_repl_observed(false),
53 m_rename_observed(false), m_clr_observed(false) {}
54
57 auto &notificationCenter = Mantid::API::AnalysisDataService::Instance().notificationCenter;
58 // Do the minimum cleanup of dangling observer refs, rather than disconnecting signals / slots
59 // through observeX, as we're about to destroy the proxy anyway
60 notificationCenter.removeObserver(m_preDeleteObserver);
61 notificationCenter.removeObserver(m_postDeleteObserver);
62 notificationCenter.removeObserver(m_addObserver);
63 notificationCenter.removeObserver(m_afterReplaceObserver);
64 notificationCenter.removeObserver(m_renameObserver);
65 notificationCenter.removeObserver(m_clearADSObserver);
66
67 m_proxy->disconnect();
68}
69
76 if (turnOn && !m_predel_observed) // Turning it on
77 {
79 m_proxy->connect(m_proxy.get(), SIGNAL(preDeleteRequested(const std::string &, Mantid::API::Workspace_sptr)),
80 SLOT(handlePreDelete(const std::string &, Mantid::API::Workspace_sptr)), Qt::QueuedConnection);
81 } else if (!turnOn && m_predel_observed) // Turning it off
82 {
83 Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_preDeleteObserver);
84 m_proxy->disconnect(m_proxy.get(), SIGNAL(preDeleteRequested(const std::string &, Mantid::API::Workspace_sptr)),
85 m_proxy.get(), SLOT(handlePreDelete(const std::string &, Mantid::API::Workspace_sptr)));
86 } else {
87 }
88 m_predel_observed = turnOn;
89}
90
97 if (turnOn && !m_postdel_observed) // Turning it on
98 {
100 m_proxy->connect(m_proxy.get(), SIGNAL(postDeleteRequested(const std::string &)),
101 SLOT(handlePostDelete(const std::string &)), Qt::QueuedConnection);
102 } else if (!turnOn && m_postdel_observed) // Turning it off
103 {
104 Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_postDeleteObserver);
105 m_proxy->disconnect(m_proxy.get(), SIGNAL(postDeleteRequested(const std::string &)), m_proxy.get(),
106 SLOT(handlePostDelete(const std::string &)));
107 } else {
108 }
109 m_postdel_observed = turnOn;
110}
111
118 if (turnOn && !m_repl_observed) {
120 m_proxy->connect(m_proxy.get(), SIGNAL(afterReplaced(const std::string &, Mantid::API::Workspace_sptr)),
121 SLOT(handleAfterReplace(const std::string &, Mantid::API::Workspace_sptr)), Qt::QueuedConnection);
122 } else if (!turnOn && m_repl_observed) {
123 Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_afterReplaceObserver);
124 m_proxy->disconnect(m_proxy.get(), SIGNAL(afterReplaced(const std::string &, Mantid::API::Workspace_sptr)),
125 m_proxy.get(), SLOT(handleAfterReplace(const std::string &, Mantid::API::Workspace_sptr)));
126 }
127 m_repl_observed = turnOn;
128}
129
136 if (turnOn && !m_rename_observed) {
137 Mantid::API::AnalysisDataService::Instance().notificationCenter.addObserver(m_renameObserver);
138 m_proxy->connect(m_proxy.get(), SIGNAL(renamed(const std::string &, const std::string &)),
139 SLOT(handleRename(const std::string &, const std::string &)), Qt::QueuedConnection);
140 } else if (!turnOn && m_rename_observed) {
141 Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_renameObserver);
142 m_proxy->disconnect(m_proxy.get(), SIGNAL(renamed(const std::string &, const std::string &)), m_proxy.get(),
143 SLOT(handleRename(const std::string &, const std::string &)));
144 }
145 m_rename_observed = turnOn;
146}
147
154 if (turnOn && !m_add_observed) {
155 Mantid::API::AnalysisDataService::Instance().notificationCenter.addObserver(m_addObserver);
156 m_proxy->connect(m_proxy.get(), SIGNAL(addRequested(const std::string &, Mantid::API::Workspace_sptr)),
157 SLOT(handleAdd(const std::string &, Mantid::API::Workspace_sptr)), Qt::QueuedConnection);
158 } else if (!turnOn && m_add_observed) {
159 Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_addObserver);
160 m_proxy->disconnect(m_proxy.get(), SIGNAL(addRequested(const std::string &, Mantid::API::Workspace_sptr)),
161 m_proxy.get(), SLOT(handleAdd(const std::string &, Mantid::API::Workspace_sptr)));
162 }
163 m_add_observed = turnOn;
164}
165
172 if (turnOn && !m_clr_observed) {
173 Mantid::API::AnalysisDataService::Instance().notificationCenter.addObserver(m_clearADSObserver);
174 m_proxy->connect(m_proxy.get(), SIGNAL(adsCleared()), SLOT(handleClearADS()), Qt::QueuedConnection);
175 } else if (!turnOn && m_clr_observed) {
176 Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_clearADSObserver);
177 m_proxy->disconnect(m_proxy.get(), SIGNAL(adsCleared()), m_proxy.get(), SLOT(handleClearADS()));
178 }
179 m_clr_observed = turnOn;
180}
181
182} // namespace MantidQt::API
IPeaksWorkspace_sptr workspace
Definition: IndexPeaks.cpp:114
A simple callback class so that we avoid multiple inheritance issues with QObject.
void handleAdd(const std::string &name, const Mantid::API::Workspace_sptr &workspace)
Add slot.
void handlePostDelete(const std::string &name)
Post Delete slot.
WorkspaceObserver * m_observer
Object to call back to.
void handlePreDelete(const std::string &name, const Mantid::API::Workspace_sptr &workspace)
Pre Delete slot.
void handleAfterReplace(const std::string &name, const Mantid::API::Workspace_sptr &workspace)
Replace slot.
void handleRename(const std::string &oldName, const std::string &newName)
Rename slot.
Observes AnalysisDataService notifications: add,replace,delete.
void observeADSClear(bool on=true)
Observe clearances.
virtual void afterReplaceHandle(const std::string &wsName, const Mantid::API::Workspace_sptr &ws)
Handler of the AfterReplace notifications.
Poco::NObserver< WorkspaceObserver, Mantid::API::WorkspaceAddNotification > m_addObserver
Poco::NObserver for DataServise::AddNotification.
void observeAdd(bool on=true)
Observe additions.
virtual void postDeleteHandle(const std::string &wsName)
Handler of the delete notifications.
Poco::NObserver< WorkspaceObserver, Mantid::API::ClearADSNotification > m_clearADSObserver
Clear notification observer.
WorkspaceObserver()
Default constructor.
virtual void addHandle(const std::string &wsName, const Mantid::API::Workspace_sptr &ws)
Handler of the add notifications.
virtual void preDeleteHandle(const std::string &wsName, const Mantid::API::Workspace_sptr &ws)
Handler of the delete notifications.
void observePostDelete(bool on=true)
Observe workspace deletes.
Poco::NObserver< WorkspaceObserver, Mantid::API::WorkspaceAfterReplaceNotification > m_afterReplaceObserver
Poco::NObserver for DataServise::AfterReplaceNotification.
Poco::NObserver< WorkspaceObserver, Mantid::API::WorkspaceRenameNotification > m_renameObserver
Poco::NObserver for DataServise::RenameNotification.
void observeAfterReplace(bool on=true)
Observe replacements.
virtual void renameHandle(const std::string &oldName, const std::string &newName)
Handler of the Rename notifications.
void observePreDelete(bool on=true)
Observe workspace deletes.
Poco::NObserver< WorkspaceObserver, Mantid::API::WorkspacePreDeleteNotification > m_preDeleteObserver
Poco::NObserver for DataServise::DeleteNotification.
virtual ~WorkspaceObserver()
Destructor.
virtual void clearADSHandle()
Handle an ADS clear notification.
void observeRename(bool on=true)
Observe renaming.
Poco::NObserver< WorkspaceObserver, Mantid::API::WorkspacePostDeleteNotification > m_postDeleteObserver
Poco::NObserver for DataServise::DeleteNotification.
std::unique_ptr< ObserverCallback > m_proxy
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Definition: Workspace_fwd.h:20
STL namespace.