Mantid
Loading...
Searching...
No Matches
QtAlgorithmRunner.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 +
8
9#include <Poco/ActiveResult.h>
10
11using namespace Mantid::Kernel;
12using namespace Mantid::API;
13
14namespace MantidQt::API {
15
17 : QObject(parent), m_finishedObserver(*this, &QtAlgorithmRunner::handleAlgorithmFinishedNotification),
18 m_progressObserver(*this, &QtAlgorithmRunner::handleAlgorithmProgressNotification),
19 m_errorObserver(*this, &QtAlgorithmRunner::handleAlgorithmErrorNotification), m_asyncResult(nullptr) {}
20
22
23//--------------------------------------------------------------------------------------
29 // Cancel any currently running algorithms
30 if (m_asyncAlg) {
31 if (m_asyncAlg->isRunning()) {
32 m_asyncAlg->cancel();
33 }
34 if (m_asyncResult) {
35 m_asyncResult->tryWait(1000);
36 delete m_asyncResult;
37 m_asyncResult = nullptr;
38 }
39 m_asyncAlg->removeObserver(m_finishedObserver);
40 m_asyncAlg->removeObserver(m_errorObserver);
41 m_asyncAlg->removeObserver(m_progressObserver);
42 m_asyncAlg.reset();
43 }
44}
45
46//--------------------------------------------------------------------------------------
53 if (!alg)
54 throw std::invalid_argument("QtAlgorithmRunner::startAlgorithm() given a NULL Algorithm");
55 if (!alg->isInitialized())
56 throw std::invalid_argument("QtAlgorithmRunner::startAlgorithm() given an uninitialized Algorithm");
57
59
60 // Observe the algorithm
61 alg->addObserver(m_finishedObserver);
62 alg->addObserver(m_errorObserver);
63 alg->addObserver(m_progressObserver);
64 // Start asynchronous execution
65 m_asyncAlg = std::move(alg);
67}
68
71
72//--------------------------------------------------------------------------------------
82void QtAlgorithmRunner::handleAlgorithmFinishedNotification(const Poco::AutoPtr<Algorithm::FinishedNotification> &pNf) {
83 UNUSED_ARG(pNf);
84 emit algorithmComplete(false);
85}
86
87//--------------------------------------------------------------------------------------
92void QtAlgorithmRunner::handleAlgorithmProgressNotification(const Poco::AutoPtr<Algorithm::ProgressNotification> &pNf) {
93 emit algorithmProgress(pNf->progress, pNf->message);
94}
95
96//--------------------------------------------------------------------------------------
102void QtAlgorithmRunner::handleAlgorithmErrorNotification(const Poco::AutoPtr<Algorithm::ErrorNotification> &pNf) {
103 UNUSED_ARG(pNf);
104 emit algorithmComplete(true);
105}
106
107} // namespace MantidQt::API
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition System.h:48
The QtAlgorithmRunner is a QObject that encapsulates methods for running an algorithm asynchronously ...
virtual Mantid::API::IAlgorithm_sptr getAlgorithm() const
Get back a pointer to the running algorithm.
void algorithmProgress(double p, const std::string &msg)
Signal emitted when the algorithm reports progress.
void handleAlgorithmFinishedNotification(const Poco::AutoPtr< Mantid::API::Algorithm::FinishedNotification > &pNf)
Algorithm notification handlers.
Poco::NObserver< QtAlgorithmRunner, Mantid::API::Algorithm::FinishedNotification > m_finishedObserver
QtAlgorithmRunner(QObject *parent=nullptr)
Mantid::API::IAlgorithm_sptr m_asyncAlg
Reference to the algorithm executing asynchronously.
void cancelRunningAlgorithm()
If an algorithm is already running, cancel it.
Poco::ActiveResult< bool > * m_asyncResult
For the asynchronous call in dynamic rebinning.
void handleAlgorithmErrorNotification(const Poco::AutoPtr< Mantid::API::Algorithm::ErrorNotification > &pNf)
Observer called when the async algorithm has encountered an error.
Poco::NObserver< QtAlgorithmRunner, Mantid::API::Algorithm::ErrorNotification > m_errorObserver
Poco::NObserver< QtAlgorithmRunner, Mantid::API::Algorithm::ProgressNotification > m_progressObserver
void handleAlgorithmProgressNotification(const Poco::AutoPtr< Mantid::API::Algorithm::ProgressNotification > &pNf)
Observer called when the async algorithm has progress to report.
void algorithmComplete(bool error)
Signal emitted when the algorithm has completed execution/encountered an error.
virtual void startAlgorithm(Mantid::API::IAlgorithm_sptr alg)
Begin asynchronous execution of an algorithm and observe its execution.
std::shared_ptr< IAlgorithm > IAlgorithm_sptr
shared pointer to Mantid::API::IAlgorithm