Commit 0944f485 authored by Anthony Lim's avatar Anthony Lim
Browse files

refs #26544 generalised ALF view and added custom instrument view

parent b8541f1c
......@@ -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
......@@ -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
......
......@@ -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
......@@ -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
......
......@@ -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
......@@ -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
......
......@@ -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
......@@ -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_ave