Skip to content

Commit

Permalink
Add stretch presenter
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedAlmaki committed Nov 12, 2024
1 parent b6ce734 commit 4def5d8
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 0 deletions.
165 changes: 165 additions & 0 deletions qt/scientific_interfaces/Inelastic/BayesFitting/StretchPresenter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
// 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 & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "StretchPresenter.h"
#include "MantidQtWidgets/Common/UserInputValidator.h"
#include "MantidQtWidgets/Common/WorkspaceUtils.h"
#include "MantidQtWidgets/Spectroscopy/InterfaceUtils.h"
#include "MantidQtWidgets/Spectroscopy/SettingsWidget/SettingsHelper.h"
#include <MantidQtWidgets/Common/ConfiguredAlgorithm.h>

using namespace Mantid::API;
using namespace MantidQt::MantidWidgets::WorkspaceUtils;

namespace {
Mantid::Kernel::Logger g_log("Stretch");
struct PlotType {
inline static const std::string ALL = "All";
inline static const std::string SIGMA = "Sigma";
inline static const std::string BETA = "Beta";
inline static const std::string FWHM = "FWHM";
};
} // namespace

namespace MantidQt::CustomInterfaces {
StretchPresenter::StretchPresenter(QWidget *parent, StretchView *view, std::unique_ptr<StretchModel> model,
std::unique_ptr<API::IAlgorithmRunner> algorithmRunner)
: BayesFittingTab(parent, std::move(algorithmRunner)), m_previewSpec(0), m_save(false), m_view(view),
m_model(std::move(model)) {
m_view->subscribePresenter(this);

setRunWidgetPresenter(std::make_unique<RunPresenter>(this, m_view->getRunWidget()));
}

void StretchPresenter::handleValidation(IUserInputValidator *validator) const { m_view->validateUserInput(validator); }

void StretchPresenter::handleRun() {
auto const saveDirectory = Mantid::Kernel::ConfigService::Instance().getString("defaultsave.directory");
if (saveDirectory.empty()) {
auto const result = m_view->displaySaveDirectoryMessage();
if (result) {
m_runPresenter->setRunEnabled(true);
return;
}
}

m_view->setPlotADSEnabled(false);

StretchRunData algParams = m_view->getRunData();

auto const cutIndex = algParams.sampleName.find_last_of("_");
auto const baseName = algParams.sampleName.substr(0, cutIndex);
m_fitWorkspaceName = baseName + "_Stretch_Fit";
m_contourWorkspaceName = baseName + "_Stretch_Contour";

auto stretch = m_model->stretchAlgorithm(algParams, m_fitWorkspaceName, m_contourWorkspaceName);
m_algorithmRunner->execute(stretch);
}

void StretchPresenter::runComplete(IAlgorithm_sptr const &algorithm, bool const error) {
(void)algorithm;

m_view->setPlotResultEnabled(!error);
m_view->setPlotContourEnabled(!error);
m_view->setSaveResultEnabled(!error);
if (!error) {
if (doesExistInADS(m_contourWorkspaceName))
resetPlotContourOptions();
else
m_view->setPlotContourEnabled(false);

m_view->setPlotADSEnabled(false);
}
}

void StretchPresenter::setButtonsEnabled(bool enabled) {
m_runPresenter->setRunEnabled(enabled);
m_view->setButtonsEnabled(enabled);
}

void StretchPresenter::setPlotResultIsPlotting(bool plotting) {
m_view->setPlotResultIsPlotting(plotting);
setButtonsEnabled(!plotting);
}

void StretchPresenter::setPlotContourIsPlotting(bool plotting) {
m_view->setPlotContourIsPlotting(plotting);
setButtonsEnabled(!plotting);
}

void StretchPresenter::resetPlotContourOptions() {
auto const contourGroup = getADSWorkspace<WorkspaceGroup>(m_contourWorkspaceName);
auto const contourNames = contourGroup->getNames();
m_view->resetPlotContourOptions(contourNames);
}

void StretchPresenter::setFileExtensionsByName(bool filter) { m_view->setFileExtensionsByName(filter); }

void StretchPresenter::setLoadHistory(bool doLoadHistory) { m_view->setLoadHistory(doLoadHistory); }

void StretchPresenter::notifySaveClicked() {
auto fitWorkspace = QString::fromStdString(m_fitWorkspaceName);
auto contourWorkspace = QString::fromStdString(m_contourWorkspaceName);

InterfaceUtils::checkADSForPlotSaveWorkspace(m_fitWorkspaceName, false);
InterfaceUtils::checkADSForPlotSaveWorkspace(m_contourWorkspaceName, false);

std::deque<MantidQt::API::IConfiguredAlgorithm_sptr> algorithmQueue;

algorithmQueue.push_back(m_model->setupSaveAlgorithm(fitWorkspace.toStdString()));
algorithmQueue.push_back(m_model->setupSaveAlgorithm(contourWorkspace.toStdString()));

m_algorithmRunner->execute(algorithmQueue);
}

void StretchPresenter::notifyPlotClicked() {
setPlotResultIsPlotting(true);

std::string const plotType = m_view->getPlotType();
auto const plotErrors = SettingsHelper::externalPlotErrorBars();
auto const plotSigma = (plotType == "All" || plotType == "Sigma");
auto const plotBeta = (plotType == "All" || plotType == "Beta");

auto const fitWorkspace = getADSWorkspace<WorkspaceGroup>(m_fitWorkspaceName);
for (auto it = fitWorkspace->begin(); it < fitWorkspace->end(); ++it) {
auto const name = (*it)->getName();
if (plotSigma && name.substr(name.length() - 5) == "Sigma") {
m_plotter->plotSpectra(name, "0", plotErrors);
} else if (plotBeta && name.substr(name.length() - 4) == "Beta") {
m_plotter->plotSpectra(name, "0", plotErrors);
}
}

setPlotResultIsPlotting(false);
}

void StretchPresenter::notifyPlotContourClicked() {
setPlotContourIsPlotting(true);

auto const workspaceName = m_view->getPlotContour();
if (checkADSForPlotSaveWorkspace(workspaceName, true))
m_plotter->plotContour(workspaceName);

setPlotContourIsPlotting(false);
}

void StretchPresenter::notifyPreviewSpecChanged(int specNum) { m_previewSpec = specNum; }

void StretchPresenter::notifyPlotCurrentPreviewClicked() {
auto previewData = m_view->getCurrentPreviewData();
if (previewData.hasSample) {
m_plotter->plotSpectra(previewData.sampleName, std::to_string(m_previewSpec),
SettingsHelper::externalPlotErrorBars());
}
}

void StretchPresenter::loadSettings(const QSettings &settings) { m_view->loadSettings(settings); }

void StretchPresenter::applySettings(std::map<std::string, QVariant> const &settings) {
m_view->applySettings(settings);
}

} // namespace MantidQt::CustomInterfaces
71 changes: 71 additions & 0 deletions qt/scientific_interfaces/Inelastic/BayesFitting/StretchPresenter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright &copy; 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once

#include "BayesFittingTab.h"
#include "DllConfig.h"
#include "MantidQtWidgets/Common/IAlgorithmRunnerSubscriber.h"
#include "MantidQtWidgets/Spectroscopy/RunWidget/IRunSubscriber.h"
#include "StretchData.h"
#include "StretchModel.h"
#include "StretchView.h"
#include "ui_Stretch.h"

namespace MantidQt {
namespace CustomInterfaces {

class MANTIDQT_INELASTIC_DLL IStretchPresenter : public IRunSubscriber,
public IStretchViewSubscriber,
public API::IAlgorithmRunnerSubscriber {
public:
virtual ~IStretchPresenter() = default;
};

class MANTIDQT_INELASTIC_DLL StretchPresenter : public BayesFittingTab, public IStretchPresenter {

public:
StretchPresenter(QWidget *parent, StretchView *view, std::unique_ptr<StretchModel> model,
std::unique_ptr<API::IAlgorithmRunner> algorithmRunner);
~StretchPresenter() override = default;

void handleValidation(IUserInputValidator *validator) const override;
void handleRun() override;
const std::string getSubscriberName() const override { return "Stretch"; }

void loadSettings(const QSettings &settings) override;
void applySettings(std::map<std::string, QVariant> const &settings) override;

void notifySaveClicked() override;
void notifyPlotClicked() override;
void notifyPlotContourClicked() override;
void notifyPlotCurrentPreviewClicked() override;
void notifyPreviewSpecChanged(int specNum) override;

protected:
void runComplete(IAlgorithm_sptr const &algorithm, bool const error) override;

private:
void setFileExtensionsByName(bool filter) override;
void setLoadHistory(bool doLoadHistory) override;

void setButtonsEnabled(bool enabled);
void setPlotResultIsPlotting(bool plotting);
void setPlotContourIsPlotting(bool plotting);

void resetPlotContourOptions();

int m_previewSpec;

std::string m_fitWorkspaceName;
std::string m_contourWorkspaceName;

bool m_save;
StretchView *m_view;
std::unique_ptr<StretchModel> m_model;
};
} // namespace CustomInterfaces
} // namespace MantidQt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ MANTID_SPECTROSCOPY_DLL void setRangeSelectorMax(QtDoublePropertyManager *dblPro
QtProperty *maxProperty, MantidWidgets::RangeSelector *rangeSelector,
double newValue);

MANTID_SPECTROSCOPY_DLL bool checkADSForPlotSaveWorkspace(const std::string &workspaceName, const bool plotting,
const bool warn = true);

} // namespace InterfaceUtils
} // namespace CustomInterfaces
} // namespace MantidQt
13 changes: 13 additions & 0 deletions qt/widgets/spectroscopy/src/InterfaceUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidQtWidgets/Spectroscopy/InterfaceUtils.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidKernel/Logger.h"
#include "MantidQtWidgets/Common/ParseKeyValueString.h"
#include "MantidQtWidgets/Spectroscopy/SettingsWidget/SettingsHelper.h"
Expand Down Expand Up @@ -233,6 +234,18 @@ void setRangeSelectorMax(QtDoublePropertyManager *dblPropertyManager, QtProperty
dblPropertyManager->setValue(maxProperty, rangeSelector->getMaximum());
}

bool checkADSForPlotSaveWorkspace(const std::string &workspaceName, const bool plotting, const bool warn) {
const auto workspaceExists = Mantid::API::AnalysisDataService::Instance().doesExist(workspaceName);
if (warn && !workspaceExists) {
const std::string plotSave = plotting ? "plotting" : "saving";
const auto errorMessage =
"Error while " + plotSave + ":\nThe workspace \"" + workspaceName + "\" could not be found.";
const char *textMessage = errorMessage.c_str();
QMessageBox::warning(nullptr, QObject::tr("Indirect "), QObject::tr(textMessage));
}
return workspaceExists;
}

} // namespace InterfaceUtils
} // namespace CustomInterfaces
} // namespace MantidQt

0 comments on commit 4def5d8

Please sign in to comment.