diff --git a/qt/scientific_interfaces/Direct/ALFView.cpp b/qt/scientific_interfaces/Direct/ALFView.cpp index b2bf9d70449fca45ce5b58f17b32dfdcad2d1ba0..d7206583a285552d81ad3ddf58c266cf0cf3c015 100644 --- a/qt/scientific_interfaces/Direct/ALFView.cpp +++ b/qt/scientific_interfaces/Direct/ALFView.cpp @@ -15,6 +15,8 @@ #include "ALFView.h" +#include <tuple> + namespace MantidQt { namespace CustomInterfaces { @@ -24,7 +26,8 @@ DECLARE_SUBWINDOW(ALFView) Mantid::Kernel::Logger g_log("ALFView"); ALFView::ALFView(QWidget *parent) - : UserSubWindow(parent), m_view(nullptr), m_presenter(nullptr) { + : UserSubWindow(parent), m_view(nullptr), m_presenter(nullptr), + m_extractSingleTubeObserver(nullptr), m_averageTubeObserver(nullptr) { m_model = new ALFView_model(); m_view = new ALFView_view(m_model->getInstrument(), this); m_presenter = new ALFView_presenter(m_view, m_model); @@ -32,7 +35,66 @@ ALFView::ALFView(QWidget *parent) void ALFView::initLayout() { this->setCentralWidget(m_view); - m_presenter->initLayout(); + m_extractSingleTubeObserver = new VoidObserver(); + m_averageTubeObserver = new VoidObserver(); + auto setUp = initInstrument(); + + m_presenter->initLayout(&setUp); } + +typedef std::pair<std::string, + std::vector<std::function<bool(std::map<std::string, bool>)>>> + instrumentSetUp; +typedef std::vector<std::tuple<std::string, Observer *>> + instrumentObserverOptions; + +/** +* This creates the custom instrument widget +* @return <instrumentSetUp, + instrumentObserverOptions> : a pair of the +*/ +std::pair<instrumentSetUp, instrumentObserverOptions> +ALFView::initInstrument() { + + instrumentSetUp setUpContextConditions; + + // set up the slots for the custom context menu + std::vector<std::tuple<std::string, Observer *>> customInstrumentOptions; + + // set up custom context menu conditions + std::function<bool(std::map<std::string, bool>)> extractConditionBinder = + std::bind(&ALFView_model::extractTubeConditon, m_model, + std::placeholders::_1); + std::function<bool(std::map<std::string, bool>)> averageTubeConditonBinder = + std::bind(&ALFView_model::averageTubeConditon, m_model, + std::placeholders::_1); + + std::vector < std::function<bool(std::map<std::string, bool>)>> binders = { + extractConditionBinder, averageTubeConditonBinder}; + + setUpContextConditions = + std::make_pair(m_model->dataFileName(),binders ); + + + // set up single tube extract + std::function<void()> extractSingleTubeBinder = + std::bind(&ALFView_model::extractSingleTube, m_model); // binder for slot + m_extractSingleTubeObserver->setSlot( + extractSingleTubeBinder); // add slot to observer + std::tuple<std::string, Observer *> tmp = std::make_tuple( + "singleTube", m_extractSingleTubeObserver); // store observer for later + customInstrumentOptions.push_back(tmp); + + // set up average tube + std::function<void()> averageTubeBinder = + std::bind(&ALFView_model::averageTube, m_model); + m_averageTubeObserver->setSlot(averageTubeBinder); + tmp = std::make_tuple("averageTube", m_averageTubeObserver); + customInstrumentOptions.push_back(tmp); + + return std::make_pair(setUpContextConditions, customInstrumentOptions); + + } + } // namespace CustomInterfaces } // namespace MantidQt diff --git a/qt/scientific_interfaces/Direct/ALFView.h b/qt/scientific_interfaces/Direct/ALFView.h index 9d5f7f6542d284a00ebcdb5d56233954869e8b12..599ce4d4ce9f47375992d8c2b05c0c02ced393ec 100644 --- a/qt/scientific_interfaces/Direct/ALFView.h +++ b/qt/scientific_interfaces/Direct/ALFView.h @@ -12,6 +12,7 @@ #include "ALFView_view.h" #include "DllConfig.h" #include "MantidQtWidgets/Common/UserSubWindow.h" +#include "MantidQtWidgets/Common/ObserverPattern.h" namespace MantidQt { namespace CustomInterfaces { @@ -25,6 +26,8 @@ public: ~ALFView() { delete m_presenter; delete m_model; + delete m_extractSingleTubeObserver; + delete m_averageTubeObserver; }; static std::string name() { return "ALF View"; } static QString categoryInfo() { return "Direct"; } @@ -33,9 +36,21 @@ protected: void initLayout() override; private: + +typedef std::pair < std::string, + std::vector<std::function<bool(std::map<std::string, bool>)>>> + instrumentSetUp; + typedef std::vector<std::tuple<std::string, Observer *>> + instrumentObserverOptions; + + std::pair<instrumentSetUp, instrumentObserverOptions> initInstrument(); + ALFView_view *m_view; ALFView_model *m_model; ALFView_presenter *m_presenter; + VoidObserver *m_extractSingleTubeObserver; + VoidObserver *m_averageTubeObserver; + }; } // namespace CustomInterfaces } // namespace MantidQt diff --git a/qt/scientific_interfaces/Direct/ALFView_model.cpp b/qt/scientific_interfaces/Direct/ALFView_model.cpp index 5217007a115488c6a2fa5198018593e3ad7e1be6..1b2e59fcfdf605d194e2fd598a56f901ca8f14fc 100644 --- a/qt/scientific_interfaces/Direct/ALFView_model.cpp +++ b/qt/scientific_interfaces/Direct/ALFView_model.cpp @@ -19,10 +19,6 @@ #include <utility> namespace { -const std::string TMPNAME = "ALF_tmp"; -const std::string INSTRUMENTNAME = "ALF"; -const std::string WSNAME = "ALFData"; -const int ERRORCODE = -999; const std::string EXTRACTEDWS = "extractedTubes_"; const std::string CURVES = "Curves"; } // namespace @@ -31,14 +27,13 @@ using namespace Mantid::API; namespace MantidQt { namespace CustomInterfaces { -void ALFView_model::loadEmptyInstrument() { - auto alg = - Mantid::API::AlgorithmManager::Instance().create("LoadEmptyInstrument"); - alg->initialize(); - alg->setProperty("OutputWorkspace", WSNAME); - alg->setProperty("InstrumentName", INSTRUMENTNAME); - alg->execute(); +ALFView_model::ALFView_model() + : m_numberOfTubesInAverage(0) { + setTmpName("ALF_tmp"); + setInstrumentName("ALF"); + setWSName("ALFData"); } + /* * Loads data for use in ALFView * Loads data, normalise to current and then converts to d spacing @@ -49,10 +44,10 @@ std::pair<int, std::string> ALFView_model::loadData(const std::string &name) { auto alg = AlgorithmManager::Instance().create("Load"); alg->initialize(); alg->setProperty("Filename", name); - alg->setProperty("OutputWorkspace", TMPNAME); // write to tmp ws + alg->setProperty("OutputWorkspace", getTmpName()); // write to tmp ws alg->execute(); auto ws = - AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(TMPNAME); + AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(getTmpName()); int runNumber = ws->getRunNumber(); std::string message = "success"; auto bools = isDataValid(); @@ -61,7 +56,7 @@ std::pair<int, std::string> ALFView_model::loadData(const std::string &name) { } else { // reset to the previous data - message = "Not the corrct instrument, expected " + INSTRUMENTNAME; + message = "Not the corrct instrument, expected " + getInstrumentName(); remove(); } if (bools["IsValidInstrument"] && !bools["IsItDSpace"]) { @@ -76,11 +71,11 @@ std::pair<int, std::string> ALFView_model::loadData(const std::string &name) { */ std::map<std::string, bool> ALFView_model::isDataValid() { auto ws = - AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(TMPNAME); + AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(getTmpName()); bool isItALF = false; bool isItDSpace = false; - if (ws->getInstrument()->getName() == INSTRUMENTNAME) { + if (ws->getInstrument()->getName() == getInstrumentName()) { isItALF = true; } auto axis = ws->getAxis(0); @@ -96,44 +91,21 @@ std::map<std::string, bool> ALFView_model::isDataValid() { * If already d-space does nothing. */ void ALFView_model::transformData() { + const std::string wsName = getWSName(); auto normAlg = AlgorithmManager::Instance().create("NormaliseByCurrent"); normAlg->initialize(); - normAlg->setProperty("InputWorkspace", WSNAME); - normAlg->setProperty("OutputWorkspace", WSNAME); + normAlg->setProperty("InputWorkspace", wsName); + normAlg->setProperty("OutputWorkspace", wsName); normAlg->execute(); auto dSpacingAlg = AlgorithmManager::Instance().create("ConvertUnits"); dSpacingAlg->initialize(); - dSpacingAlg->setProperty("InputWorkspace", WSNAME); + dSpacingAlg->setProperty("InputWorkspace", wsName); dSpacingAlg->setProperty("Target", "dSpacing"); - dSpacingAlg->setProperty("OutputWorkspace", WSNAME); + dSpacingAlg->setProperty("OutputWorkspace", wsName); dSpacingAlg->execute(); } -void ALFView_model::rename() { - AnalysisDataService::Instance().rename(TMPNAME, WSNAME); -} -void ALFView_model::remove() { - AnalysisDataService::Instance().remove(TMPNAME); -} - -std::string ALFView_model::dataFileName() { return WSNAME; } - -int ALFView_model::currentRun() { - try { - - auto ws = - AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(WSNAME); - return ws->getRunNumber(); - } catch (...) { - return ERRORCODE; - } -} - -bool ALFView_model::isErrorCode(const int run) { return (run == ERRORCODE); } - -std::string ALFView_model::getInstrument() { return INSTRUMENTNAME; } - void ALFView_model::storeSingleTube(const std::string &name) { auto alg = AlgorithmManager::Instance().create("ScaleX"); alg->initialize(); @@ -151,12 +123,13 @@ void ALFView_model::storeSingleTube(const std::string &name) { AnalysisDataService::Instance().remove(CURVES); } -void ALFView_model::averageTube(const int &oldTotalNumber, - const std::string &name) { +void ALFView_model::averageTube() { + const std::string name = getInstrumentName() + std::to_string(getCurrentRun()); + const int oldTotalNumber = m_numberOfTubesInAverage; // multiply up current average auto ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>( EXTRACTEDWS + name); - ws->mutableY(0) * double(oldTotalNumber); + ws*=double(oldTotalNumber); // get the data to add storeSingleTube(name); @@ -180,11 +153,45 @@ void ALFView_model::averageTube(const int &oldTotalNumber, name); ws->mutableY(0) /= (double(oldTotalNumber) + 1.0); AnalysisDataService::Instance().addOrReplace(EXTRACTEDWS + name, ws); + m_numberOfTubesInAverage++; } bool ALFView_model::hasTubeBeenExtracted(const std::string &name) { return AnalysisDataService::Instance().doesExist(EXTRACTEDWS + name); } +bool ALFView_model::extractTubeConditon( + std::map<std::string, bool> tabBools) { + try { + + bool ifCurve = (tabBools.find("plotStroed")->second || + tabBools.find("hasCurve")->second); + return (tabBools.find("isTube")->second && ifCurve); + } catch (...) { + return false; + } +} + +bool ALFView_model::averageTubeConditon( + std::map<std::string, bool> tabBools) { + try { + + bool ifCurve = (tabBools.find("plotStroed")->second || + tabBools.find("hasCurve")->second); + return (m_numberOfTubesInAverage > 0 && tabBools.find("isTube")->second && + ifCurve && + hasTubeBeenExtracted(getInstrumentName() + + std::to_string(getCurrentRun()))); + } catch (...) { + return false; + } +} +void ALFView_model::extractSingleTube() { + storeSingleTube(getInstrumentName() + std::to_string(getCurrentRun())); + m_numberOfTubesInAverage = 1; +} + + + } // namespace CustomInterfaces } // namespace MantidQt diff --git a/qt/scientific_interfaces/Direct/ALFView_model.h b/qt/scientific_interfaces/Direct/ALFView_model.h index 093b6b37036ad648a099996ba74bf2d607af32e4..7db072497dabc69a6a6e5917410d42dc6ec80843 100644 --- a/qt/scientific_interfaces/Direct/ALFView_model.h +++ b/qt/scientific_interfaces/Direct/ALFView_model.h @@ -6,27 +6,35 @@ // SPDX - License - Identifier: GPL - 3.0 + #ifndef MANTIDQT_CUSTOMINTERFACES_ALFVIEWMODEL_H_ #define MANTIDQT_CUSTOMINTERFACES_ALFVIEWMODEL_H_ + +#include "BaseInstrumentModel.h" + #include <map> #include <string> + namespace MantidQt { namespace CustomInterfaces { -class ALFView_model { +class ALFView_model : public BaseInstrumentModel { + + public: - void loadEmptyInstrument(); - std::pair<int, std::string> loadData(const std::string &name); + ALFView_model(); + ~ALFView_model(){}; + std::pair<int, std::string> loadData(const std::string &name) override; std::map<std::string, bool> isDataValid(); void transformData(); - void rename(); - void remove(); - std::string dataFileName(); - int currentRun(); - bool isErrorCode(const int run); - std::string getInstrument(); void storeSingleTube(const std::string &name); - void averageTube(const int &oldTotalNumber, const std::string &name); + void averageTube(); bool hasTubeBeenExtracted(const std::string &name); + bool extractTubeConditon(std::map<std::string, bool> tabBools); + bool averageTubeConditon(std::map<std::string, bool> tabBools); + void extractSingleTube(); + +private: + int m_numberOfTubesInAverage; + int m_currentRun; }; } // namespace CustomInterfaces diff --git a/qt/scientific_interfaces/Direct/ALFView_presenter.cpp b/qt/scientific_interfaces/Direct/ALFView_presenter.cpp index 265a48cdbbe544bf0055f9e2b8e6700e941b479c..a3573f96872cc3234f033541bd686983074f5f60 100644 --- a/qt/scientific_interfaces/Direct/ALFView_presenter.cpp +++ b/qt/scientific_interfaces/Direct/ALFView_presenter.cpp @@ -5,33 +5,32 @@ // & Institut Laue - Langevin // SPDX - License - Identifier: GPL - 3.0 + #include "ALFView_presenter.h" -#include "ALFView_model.h" #include "ALFView_view.h" #include "MantidAPI/FileFinder.h" #include <functional> +#include <tuple> namespace MantidQt { namespace CustomInterfaces { -ALFView_presenter::ALFView_presenter(ALFView_view *view, ALFView_model *model) +ALFView_presenter::ALFView_presenter(ALFView_view *view, + BaseInstrumentModel *model) : m_view(view), m_model(model), m_currentRun(0), m_currentFile(""), - m_loadRunObserver(nullptr), m_numberOfTubesInAverage(0), - m_extractSingleTubeObserver(nullptr), m_averageTubeObserver(nullptr) { + m_loadRunObserver(nullptr) { m_loadRunObserver = new VoidObserver(); - m_extractSingleTubeObserver = new VoidObserver(); - m_averageTubeObserver = new VoidObserver(); m_model->loadEmptyInstrument(); } -void ALFView_presenter::initLayout() { +void ALFView_presenter::initLayout( + std::pair<instrumentSetUp, instrumentObserverOptions> *setUp) { // connect to new run m_view->observeLoadRun(m_loadRunObserver); std::function<void()> loadBinder = std::bind(&ALFView_presenter::loadRunNumber, this); m_loadRunObserver->setSlot(loadBinder); - initInstrument(); + initInstrument(setUp); } void ALFView_presenter::loadAndAnalysis(const std::string &pathToRun) { @@ -47,8 +46,10 @@ void ALFView_presenter::loadAndAnalysis(const std::string &pathToRun) { } // make displayed run number be in sinc m_view->setRunQuietly(std::to_string(m_currentRun)); + m_model->setCurrentRun(m_currentRun); } catch (...) { m_view->setRunQuietly(std::to_string(m_currentRun)); + m_model->setCurrentRun(m_currentRun); } } @@ -60,68 +61,23 @@ void ALFView_presenter::loadRunNumber() { loadAndAnalysis(pathToRun); } +// All of the below are specific to ALF -void ALFView_presenter::initInstrument() { - // set up instrument - std::function<bool(std::map<std::string, bool>)> extractConditionBinder = - std::bind(&ALFView_presenter::extractTubeConditon, this, - std::placeholders::_1); - std::function<bool(std::map<std::string, bool>)> averageTubeConditonBinder = - std::bind(&ALFView_presenter::averageTubeConditon, this, - std::placeholders::_1); - m_view->setUpInstrument(m_model->dataFileName(), extractConditionBinder, - averageTubeConditonBinder); - - // set up single tube extract - m_view->observeExtractSingleTube(m_extractSingleTubeObserver); - std::function<void()> extractSingleTubeBinder = - std::bind(&ALFView_presenter::extractSingleTube, this); - m_extractSingleTubeObserver->setSlot(extractSingleTubeBinder); - - // set up average tube - m_view->observeAverageTube(m_averageTubeObserver); - std::function<void()> averageTubeBinder = - std::bind(&ALFView_presenter::averageTube, this); - m_averageTubeObserver->setSlot(averageTubeBinder); -} - -bool ALFView_presenter::extractTubeConditon( - std::map<std::string, bool> tabBools) { - try { - - bool ifCurve = (tabBools.find("plotStroed")->second || - tabBools.find("hasCurve")->second); - return (tabBools.find("isTube")->second && ifCurve); - } catch (...) { - return false; +void ALFView_presenter::initInstrument( + std::pair<instrumentSetUp, instrumentObserverOptions> *setUp) { + if (!setUp) { + return; } -} + // set up instrument + auto instrumentSetUp = setUp->first; -bool ALFView_presenter::averageTubeConditon( - std::map<std::string, bool> tabBools) { - try { + m_view->setUpInstrument(instrumentSetUp.first, instrumentSetUp.second); - bool ifCurve = (tabBools.find("plotStroed")->second || - tabBools.find("hasCurve")->second); - return (m_numberOfTubesInAverage > 0 && tabBools.find("isTube")->second && - ifCurve && - m_model->hasTubeBeenExtracted(m_model->getInstrument() + - std::to_string(m_currentRun))); - } catch (...) { - return false; + auto customContextMenu = setUp->second; + for (auto options : customContextMenu) { + m_view->addObserver(options); } } -void ALFView_presenter::extractSingleTube() { - m_model->storeSingleTube(m_model->getInstrument() + - std::to_string(m_currentRun)); - m_numberOfTubesInAverage = 1; -} - -void ALFView_presenter::averageTube() { - m_model->averageTube(m_numberOfTubesInAverage, - m_model->getInstrument() + std::to_string(m_currentRun)); - m_numberOfTubesInAverage++; -} } // namespace CustomInterfaces } // namespace MantidQt \ No newline at end of file diff --git a/qt/scientific_interfaces/Direct/ALFView_presenter.h b/qt/scientific_interfaces/Direct/ALFView_presenter.h index 7b1717225a85020bfca779c9abc5dd8a713adb9b..dcafe117dea524f99985df1ecf2008f4e18daf9b 100644 --- a/qt/scientific_interfaces/Direct/ALFView_presenter.h +++ b/qt/scientific_interfaces/Direct/ALFView_presenter.h @@ -7,7 +7,7 @@ #ifndef MANTIDQT_CUSTOMINTERFACES_ALFVIEWPRESENTER_H_ #define MANTIDQT_CUSTOMINTERFACES_ALFVIEWPRESENTER_H_ -#include "ALFView_model.h" +#include "BaseInstrumentModel.h" #include "ALFView_view.h" #include "DllConfig.h" #include "MantidQtWidgets/Common/ObserverPattern.h" @@ -22,28 +22,30 @@ class MANTIDQT_DIRECT_DLL ALFView_presenter : public QObject { Q_OBJECT public: - ALFView_presenter(ALFView_view *view, ALFView_model *model); + ALFView_presenter(ALFView_view *view, BaseInstrumentModel *model); ~ALFView_presenter() { delete m_loadRunObserver; }; - void initLayout(); + + typedef std::pair<std::string, + std::vector<std::function<bool(std::map<std::string, bool>)>>> + instrumentSetUp; + typedef std::vector<std::tuple<std::string, Observer *>> + instrumentObserverOptions; + + void initLayout(std::pair<instrumentSetUp, instrumentObserverOptions> *setUp = nullptr); private slots: void loadRunNumber(); private: void loadAndAnalysis(const std::string &run); - void initInstrument(); - bool extractTubeConditon(std::map<std::string, bool> tabBools); - bool averageTubeConditon(std::map<std::string, bool> tabBools); - void extractSingleTube(); - void averageTube(); + void initInstrument(std::pair<instrumentSetUp, instrumentObserverOptions> *setUp); + ALFView_view *m_view; - ALFView_model *m_model; + BaseInstrumentModel *m_model; int m_currentRun; std::string m_currentFile; VoidObserver *m_loadRunObserver; - int m_numberOfTubesInAverage; - VoidObserver *m_extractSingleTubeObserver; - VoidObserver *m_averageTubeObserver; + }; } // namespace CustomInterfaces } // namespace MantidQt diff --git a/qt/scientific_interfaces/Direct/ALFView_view.cpp b/qt/scientific_interfaces/Direct/ALFView_view.cpp index 2a4f21b8e0247a6f68d4c3c65c858cc343b0b42d..7df009ee3716f6d80b687c397b1edbb1ce74dc9d 100644 --- a/qt/scientific_interfaces/Direct/ALFView_view.cpp +++ b/qt/scientific_interfaces/Direct/ALFView_view.cpp @@ -19,15 +19,15 @@ ALFView_view::ALFView_view(const std::string &instrument, QWidget *parent) : QSplitter(Qt::Vertical, parent), m_loadRunObservable(nullptr), m_files(nullptr), m_instrument(QString::fromStdString(instrument)), m_extractSingleTubeObservable(nullptr), m_averageTubeObservable(nullptr), - m_instrumentWidget(nullptr), m_extractAction(nullptr), m_averageAction (nullptr){ + m_instrumentWidget(nullptr), m_extractAction(nullptr), + m_averageAction(nullptr) { auto loadWidget = generateLoadWidget(); this->addWidget(loadWidget); } void ALFView_view::setUpInstrument( std::string fileName, - std::function<bool(std::map<std::string, bool>)> &extractBinder, - std::function<bool(std::map<std::string, bool>)> &averageBinder) { + std::vector<std::function<bool(std::map<std::string, bool>)>> &binders) { m_extractSingleTubeObservable = new Observable(); m_averageTubeObservable = new Observable(); @@ -42,13 +42,13 @@ void ALFView_view::setUpInstrument( connect(m_extractAction, SIGNAL(triggered()), this, SLOT(extractSingleTube())), m_instrumentWidget->getPickTab()->addToContextMenu(m_extractAction, - extractBinder); + binders[0]); // set up add to average m_averageAction = new QAction("Add Tube To Average", this); connect(m_averageAction, SIGNAL(triggered()), this, SLOT(averageTube())), m_instrumentWidget->getPickTab()->addToContextMenu(m_averageAction, - averageBinder); + binders[1]); } QWidget *ALFView_view::generateLoadWidget() { @@ -113,5 +113,20 @@ void ALFView_view::averageTube() { m_instrumentWidget->getPickTab()->savePlotToWorkspace(); m_averageTubeObservable->notify(); } + +void ALFView_view::observeExtractSingleTube(Observer *listner) { + m_extractSingleTubeObservable->attach(listner); +} +void ALFView_view::observeAverageTube(Observer *listner) { + m_averageTubeObservable->attach(listner); +} + +void ALFView_view::addObserver(std::tuple<std::string, Observer *> &listener) { + if (std::get<0>(listener) == "singleTube") { + observeExtractSingleTube(std::get<1>(listener)); + } else if (std::get<0>(listener) == "averageTube") { + observeAverageTube(std::get<1>(listener)); + } +} } // namespace CustomInterfaces } // namespace MantidQt \ No newline at end of file diff --git a/qt/scientific_interfaces/Direct/ALFView_view.h b/qt/scientific_interfaces/Direct/ALFView_view.h index a0c4ee145de11ec8deb7a0482b171bf86736fad8..31012430959af07f329e3b9a7da36ec1d177865c 100644 --- a/qt/scientific_interfaces/Direct/ALFView_view.h +++ b/qt/scientific_interfaces/Direct/ALFView_view.h @@ -32,16 +32,15 @@ public: m_loadRunObservable->attach(listener); }; void warningBox(const std::string &message); - void observeExtractSingleTube(Observer *listner) { - m_extractSingleTubeObservable->attach(listner); - } - void observeAverageTube(Observer *listner) { - m_averageTubeObservable->attach(listner); - } + void observeExtractSingleTube(Observer *listner); + void observeAverageTube(Observer *listner); + void setUpInstrument( const std::string fileName, - std::function<bool(std::map<std::string, bool>)> &extractBinder, - std::function<bool(std::map<std::string, bool>)> &averageBinder); + std::vector<std::function<bool(std::map<std::string, bool>)>> &binders); + + void addObserver(std::tuple<std::string, Observer *> &listener); + public slots: void fileLoaded(); void extractSingleTube(); diff --git a/qt/scientific_interfaces/Direct/BaseInstrumentModel.cpp b/qt/scientific_interfaces/Direct/BaseInstrumentModel.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7689e41afe99670f458d95458360408c736f7279 --- /dev/null +++ b/qt/scientific_interfaces/Direct/BaseInstrumentModel.cpp @@ -0,0 +1,86 @@ +// Mantid Repository : https://github.com/mantidproject/mantid +// +// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI, +// NScD Oak Ridge National Laboratory, European Spallation Source +// & Institut Laue - Langevin +// SPDX - License - Identifier: GPL - 3.0 + + +#include "BaseInstrumentModel.h" +#include "MantidAPI/Algorithm.h" +#include "MantidAPI/AlgorithmManager.h" +#include "MantidAPI/AnalysisDataService.h" +#include "MantidAPI/MatrixWorkspace.h" + +#include "MantidKernel/Unit.h" + +#include <utility> + +namespace { +const int ERRORCODE = -999; +} + +using namespace Mantid::API; +namespace MantidQt { +namespace CustomInterfaces { + +BaseInstrumentModel::BaseInstrumentModel() + : m_currentRun(0), m_tmpName("tmp"), m_instrumentName("EMU"), + m_wsName("data") {} + +void BaseInstrumentModel::loadEmptyInstrument() { + auto alg = + Mantid::API::AlgorithmManager::Instance().create("LoadEmptyInstrument"); + alg->initialize(); + alg->setProperty("OutputWorkspace", m_wsName); + alg->setProperty("InstrumentName", m_instrumentName); + alg->execute(); +} +/* + * Loads data + * Loads data, normalise to current and then converts to d spacing + * @param name:: string name for data + * @return std::pair<int,std::string>:: the run number and status + */ +std::pair<int, std::string> +BaseInstrumentModel::loadData(const std::string &name) { + auto alg = AlgorithmManager::Instance().create("Load"); + alg->initialize(); + alg->setProperty("Filename", name); + alg->setProperty("OutputWorkspace", m_tmpName); // write to tmp ws + alg->execute(); + auto ws = + AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(m_tmpName); + int runNumber = ws->getRunNumber(); + std::string message = "success"; + rename(); + return std::make_pair(runNumber, message); +} + +void BaseInstrumentModel::rename() { + AnalysisDataService::Instance().rename(m_tmpName, m_wsName); +} +void BaseInstrumentModel::remove() { + AnalysisDataService::Instance().remove(m_tmpName); +} + +std::string BaseInstrumentModel::dataFileName() { return m_wsName; } + +int BaseInstrumentModel::currentRun() { + try { + + auto ws = + AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(m_wsName); + return ws->getRunNumber(); + } catch (...) { + return ERRORCODE; + } +} + +bool BaseInstrumentModel::isErrorCode(const int run) { + return (run == ERRORCODE); +} + +std::string BaseInstrumentModel::getInstrument() { return m_instrumentName; } + +} // namespace CustomInterfaces +} // namespace MantidQt diff --git a/qt/scientific_interfaces/Direct/BaseInstrumentModel.h b/qt/scientific_interfaces/Direct/BaseInstrumentModel.h new file mode 100644 index 0000000000000000000000000000000000000000..c7d0e7afea2e575f870af787c1178028d0fddbcc --- /dev/null +++ b/qt/scientific_interfaces/Direct/BaseInstrumentModel.h @@ -0,0 +1,50 @@ +// Mantid Repository : https://github.com/mantidproject/mantid +// +// Copyright © 2014 ISIS Rutherford Appleton Laboratory UKRI, +// NScD Oak Ridge National Laboratory, European Spallation Source +// & Institut Laue - Langevin +// SPDX - License - Identifier: GPL - 3.0 + +#ifndef MANTIDQT_CUSTOMINTERFACES_BASEINSTRUMENTMODEL_H_ +#define MANTIDQT_CUSTOMINTERFACES_BASEINSTRUMENTMODEL_H_ +#include <map> +#include <string> + +namespace MantidQt { +namespace CustomInterfaces { + +class BaseInstrumentModel { + + +public: + BaseInstrumentModel(); + ~BaseInstrumentModel(){}; + virtual void loadEmptyInstrument(); + virtual std::pair<int, std::string> loadData(const std::string &name); + void setCurrentRun(int &run) { m_currentRun = run; }; + int getCurrentRun() { return m_currentRun; }; + void rename(); + void remove(); + std::string dataFileName(); + int currentRun(); + bool isErrorCode(const int run); + std::string getInstrument(); + + void setTmpName(const std::string &name) { m_tmpName = name; }; + void setInstrumentName(const std::string &name) { m_instrumentName = name; }; + void setWSName(const std::string &name) { m_wsName = name; }; + const std::string getTmpName() { return m_tmpName; }; + const std::string getInstrumentName() { return m_instrumentName; }; + const std::string getWSName() { return m_wsName; }; + + +private: + int m_currentRun; + std::string m_tmpName; + std::string m_instrumentName; + std::string m_wsName; +}; + +} // namespace CustomInterfaces +} // namespace MantidQt + +#endif /* MANTIDQT_CUSTOMINTERFACES_BASEINSTRUMENTMODEL_H_ */ diff --git a/qt/scientific_interfaces/Direct/CMakeLists.txt b/qt/scientific_interfaces/Direct/CMakeLists.txt index c6c10352d0eae9cf60c52e2611420e3d2f1aa5ea..6ed3788c24ce4e4bf0b7ba779e937efc1a994507 100644 --- a/qt/scientific_interfaces/Direct/CMakeLists.txt +++ b/qt/scientific_interfaces/Direct/CMakeLists.txt @@ -2,7 +2,8 @@ set(SRC_FILES ALFView.cpp ALFView_view.cpp ALFView_presenter.cpp - ALFView_model.cpp) + ALFView_model.cpp + BaseInstrumentModel.cpp) set(MOC_FILES ALFView.h