Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Inelastic QENS FitTab #37193

Merged
merged 16 commits into from Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions qt/python/mantidqt/mantidqt/_common.sip
Expand Up @@ -911,10 +911,10 @@ public:
void setNumberOfDatasets(int n);
void setCurrentDataset(int i) throw(std::exception);
int getCurrentDataset() const;
void setDatasets(const QStringList &datasetNames);
void addDatasets(const QStringList &names);
void setDatasets(const std::vector<std::string> &datasetNames);
void addDatasets(const std::vector<std::string> &names);
void removeDatasets(QList<int> indices);
QStringList getDatasetNames() const;
std::vector<std::string> getDatasetNames() const;
void setErrorsEnabled(bool enabled);
void hideGlobalCheckbox();
void showGlobalCheckbox();
Expand Down
48 changes: 30 additions & 18 deletions qt/scientific_interfaces/Inelastic/Common/InterfaceUtils.cpp
Expand Up @@ -5,6 +5,7 @@
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "Common/InterfaceUtils.h"
#include "Common/SettingsHelper.h"
#include "MantidKernel/Logger.h"
#include "MantidQtWidgets/Common/ParseKeyValueString.h"
#include <QDomDocument>
Expand Down Expand Up @@ -67,6 +68,11 @@ static std::string getInterfaceAttribute(QDomElement const &root, std::string co
return "";
}

/// The function to use to check whether input data should be restricted based on its name.
/// This is defined, rather than calling SettingsHelper::restrictInputDataByName() directly, to make it
/// possible to override it in tests in order to mock out the SettingsHelper.
std::function<bool()> restrictInputDataByName = SettingsHelper::restrictInputDataByName;

std::string getInterfaceProperty(std::string const &interfaceName, std::string const &propertyName,
std::string const &attribute) {
QFile file(":/interface-properties.xml");
Expand All @@ -87,52 +93,58 @@ QStringList getCalibrationExtensions(std::string const &interfaceName) {
return toQStringList(getInterfaceProperty(interfaceName, "EXTENSIONS", "calibration"), ",");
}

QStringList getSampleFBSuffixes(std::string const &interfaceName) {
return toQStringList(getInterfaceProperty(interfaceName, "FILE-SUFFIXES", "sample"), ",");
QStringList getFBSuffixes(std::string const &interfaceName, std::string const &fileType) {
if (!restrictInputDataByName()) {
return getExtensions(interfaceName);
}
return toQStringList(getInterfaceProperty(interfaceName, "FILE-SUFFIXES", fileType), ",");
}

QStringList getSampleWSSuffixes(std::string const &interfaceName) {
return toQStringList(getInterfaceProperty(interfaceName, "WORKSPACE-SUFFIXES", "sample"), ",");
QStringList getWSSuffixes(std::string const &interfaceName, std::string const &fileType) {
if (!restrictInputDataByName()) {
return QStringList{""};
}
return toQStringList(getInterfaceProperty(interfaceName, "WORKSPACE-SUFFIXES", fileType), ",");
}

QStringList getVanadiumFBSuffixes(std::string const &interfaceName) {
return toQStringList(getInterfaceProperty(interfaceName, "FILE-SUFFIXES", "vanadium"), ",");
}
QStringList getSampleFBSuffixes(std::string const &interfaceName) { return getFBSuffixes(interfaceName, "sample"); }

QStringList getVanadiumWSSuffixes(std::string const &interfaceName) {
return toQStringList(getInterfaceProperty(interfaceName, "WORKSPACE-SUFFIXES", "vanadium"), ",");
}
QStringList getSampleWSSuffixes(std::string const &interfaceName) { return getWSSuffixes(interfaceName, "sample"); }

QStringList getVanadiumFBSuffixes(std::string const &interfaceName) { return getFBSuffixes(interfaceName, "vanadium"); }

QStringList getVanadiumWSSuffixes(std::string const &interfaceName) { return getWSSuffixes(interfaceName, "vanadium"); }

QStringList getResolutionFBSuffixes(std::string const &interfaceName) {
return toQStringList(getInterfaceProperty(interfaceName, "FILE-SUFFIXES", "resolution"), ",");
return getFBSuffixes(interfaceName, "resolution");
}

QStringList getResolutionWSSuffixes(std::string const &interfaceName) {
return toQStringList(getInterfaceProperty(interfaceName, "WORKSPACE-SUFFIXES", "resolution"), ",");
return getWSSuffixes(interfaceName, "resolution");
}

QStringList getCalibrationFBSuffixes(std::string const &interfaceName) {
return toQStringList(getInterfaceProperty(interfaceName, "FILE-SUFFIXES", "calibration"), ",");
return getFBSuffixes(interfaceName, "calibration");
}

QStringList getCalibrationWSSuffixes(std::string const &interfaceName) {
return toQStringList(getInterfaceProperty(interfaceName, "WORKSPACE-SUFFIXES", "calibration"), ",");
return getWSSuffixes(interfaceName, "calibration");
}

QStringList getContainerFBSuffixes(std::string const &interfaceName) {
return toQStringList(getInterfaceProperty(interfaceName, "FILE-SUFFIXES", "container"), ",");
return getFBSuffixes(interfaceName, "container");
}

QStringList getContainerWSSuffixes(std::string const &interfaceName) {
return toQStringList(getInterfaceProperty(interfaceName, "WORKSPACE-SUFFIXES", "container"), ",");
return getWSSuffixes(interfaceName, "container");
}

QStringList getCorrectionsFBSuffixes(std::string const &interfaceName) {
return toQStringList(getInterfaceProperty(interfaceName, "FILE-SUFFIXES", "corrections"), ",");
return getFBSuffixes(interfaceName, "corrections");
}

QStringList getCorrectionsWSSuffixes(std::string const &interfaceName) {
return toQStringList(getInterfaceProperty(interfaceName, "WORKSPACE-SUFFIXES", "corrections"), ",");
return getWSSuffixes(interfaceName, "corrections");
}

QPair<double, double> convertTupleToQPair(std::tuple<double, double> const &doubleTuple) {
Expand Down
5 changes: 5 additions & 0 deletions qt/scientific_interfaces/Inelastic/Common/InterfaceUtils.h
Expand Up @@ -14,6 +14,11 @@ namespace MantidQt {
namespace CustomInterfaces {
namespace InterfaceUtils {

/// The function to use to check whether input data should be restricted based on its name.
/// This is defined, rather than calling SettingsHelper::restrictInputDataByName() directly, to make it
/// possible to override it in tests in order to mock out the SettingsHelper.
MANTIDQT_INELASTIC_DLL extern std::function<bool()> restrictInputDataByName;

MANTIDQT_INELASTIC_DLL std::string getInterfaceProperty(std::string const &interfaceName,
std::string const &propertyName, std::string const &attribute);

Expand Down
Expand Up @@ -5,7 +5,9 @@
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "ConvFitDataView.h"
#include "Common/InterfaceUtils.h"
#include "ConvFitAddWorkspaceDialog.h"
#include "FitDataPresenter.h"

#include <QComboBox>
#include <QHeaderView>
Expand Down Expand Up @@ -38,11 +40,12 @@ void ConvFitDataView::showAddWorkspaceDialog() {
connect(dialog, SIGNAL(addData(MantidWidgets::IAddWorkspaceDialog *)), this,
SLOT(notifyAddData(MantidWidgets::IAddWorkspaceDialog *)));

auto tabName = m_presenter->tabName();
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->setWSSuffices(m_wsSampleSuffixes);
dialog->setFBSuffices(m_fbSampleSuffixes);
dialog->setResolutionWSSuffices(m_wsResolutionSuffixes);
dialog->setResolutionFBSuffices(m_fbResolutionSuffixes);
dialog->setWSSuffices(InterfaceUtils::getSampleWSSuffixes(tabName));
dialog->setFBSuffices(InterfaceUtils::getSampleFBSuffixes(tabName));
dialog->setResolutionWSSuffices(InterfaceUtils::getResolutionWSSuffixes(tabName));
dialog->setResolutionFBSuffices(InterfaceUtils::getResolutionFBSuffixes(tabName));
dialog->updateSelectedSpectra();
dialog->show();
}
Expand Down
Expand Up @@ -22,11 +22,11 @@ Presenter for a table of convolution fitting data.
class MANTIDQT_INELASTIC_DLL ConvFitDataView : public FitDataView {
Q_OBJECT
public:
ConvFitDataView(QWidget *parent = nullptr);
ConvFitDataView(QWidget *parent);
void addTableEntry(size_t row, FitDataRow newRow) override;

protected:
ConvFitDataView(const QStringList &headers, QWidget *parent = nullptr);
ConvFitDataView(const QStringList &headers, QWidget *parent);

protected slots:
void showAddWorkspaceDialog() override;
Expand Down
Expand Up @@ -61,6 +61,11 @@ FunctionModelSpectra FitDataModel::getSpectra(WorkspaceID workspaceID) const {
return FunctionModelSpectra("");
}

FunctionModelDataset FitDataModel::getDataset(WorkspaceID workspaceID) const {
auto const name = getWorkspace(workspaceID)->getName();
return FunctionModelDataset(name, getSpectra(workspaceID));
}

std::string FitDataModel::createDisplayName(WorkspaceID workspaceID) const {
if (getNumberOfWorkspaces() > workspaceID)
return getFitDataName(getWorkspaceNames()[workspaceID.value], getSpectra(workspaceID));
Expand Down
2 changes: 2 additions & 0 deletions qt/scientific_interfaces/Inelastic/QENSFitting/FitDataModel.h
Expand Up @@ -11,6 +11,7 @@
#include "IFitDataModel.h"

#include "MantidAPI/MatrixWorkspace.h"
#include "MantidQtWidgets/Common/FunctionModelDataset.h"
#include "MantidQtWidgets/Common/FunctionModelSpectra.h"
#include "MantidQtWidgets/Common/IndexTypes.h"

Expand Down Expand Up @@ -43,6 +44,7 @@ class MANTIDQT_INELASTIC_DLL FitDataModel : public IFitDataModel {
void setSpectra(FunctionModelSpectra &&spectra, WorkspaceID workspaceID) override;
void setSpectra(const FunctionModelSpectra &spectra, WorkspaceID workspaceID) override;
FunctionModelSpectra getSpectra(WorkspaceID workspaceID) const override;
FunctionModelDataset getDataset(WorkspaceID workspaceID) const override;
size_t getSpectrum(FitDomainIndex index) const override;
size_t getNumberOfSpectra(WorkspaceID workspaceID) const override;

Expand Down
26 changes: 11 additions & 15 deletions qt/scientific_interfaces/Inelastic/QENSFitting/FitDataPresenter.cpp
Expand Up @@ -26,6 +26,8 @@ std::vector<FitData> *FitDataPresenter::getFittingData() { return m_model->getFi

IFitDataView const *FitDataPresenter::getView() const { return m_view; }

std::string FitDataPresenter::tabName() const { return m_tab->tabName(); }

bool FitDataPresenter::addWorkspaceFromDialog(MantidWidgets::IAddWorkspaceDialog const *dialog) {
if (const auto indirectDialog = dynamic_cast<MantidWidgets::AddWorkspaceDialog const *>(dialog)) {
addWorkspace(indirectDialog->workspaceName(), indirectDialog->workspaceIndices());
Expand All @@ -45,18 +47,6 @@ void FitDataPresenter::setResolution(const std::string &name) {
}
}

void FitDataPresenter::setSampleWSSuffices(const QStringList &suffixes) { m_view->setSampleWSSuffices(suffixes); }

void FitDataPresenter::setSampleFBSuffices(const QStringList &suffixes) { m_view->setSampleFBSuffices(suffixes); }

void FitDataPresenter::setResolutionWSSuffices(const QStringList &suffixes) {
m_view->setResolutionWSSuffices(suffixes);
}

void FitDataPresenter::setResolutionFBSuffices(const QStringList &suffixes) {
m_view->setResolutionFBSuffices(suffixes);
}

void FitDataPresenter::setStartX(double startX, WorkspaceID workspaceID) {
if (m_model->getNumberOfWorkspaces() > workspaceID) {
m_model->setStartX(startX, workspaceID);
Expand Down Expand Up @@ -85,7 +75,7 @@ std::vector<std::pair<std::string, size_t>> FitDataPresenter::getResolutionsForF
return m_model->getResolutionsForFit();
}

UserInputValidator &FitDataPresenter::validate(UserInputValidator &validator) { return m_view->validate(validator); }
void FitDataPresenter::validate(UserInputValidator &validator) { m_view->validate(validator); }

void FitDataPresenter::handleAddData(MantidWidgets::IAddWorkspaceDialog const *dialog) {
try {
Expand All @@ -110,8 +100,14 @@ WorkspaceID FitDataPresenter::getNumberOfWorkspaces() const { return m_model->ge

size_t FitDataPresenter::getNumberOfDomains() const { return m_model->getNumberOfDomains(); }

FunctionModelSpectra FitDataPresenter::getSpectra(WorkspaceID workspaceID) const {
return m_model->getSpectra(workspaceID);
QList<FunctionModelDataset> FitDataPresenter::getDatasets() const {
QList<FunctionModelDataset> datasets;
robertapplin marked this conversation as resolved.
Show resolved Hide resolved

for (auto i = 0u; i < m_model->getNumberOfWorkspaces().value; ++i) {
WorkspaceID workspaceID{i};
datasets.append(m_model->getDataset(workspaceID));
}
return datasets;
}

DataForParameterEstimationCollection
Expand Down
Expand Up @@ -24,6 +24,8 @@ class IFitTab;

class MANTIDQT_INELASTIC_DLL IFitDataPresenter {
public:
virtual std::string tabName() const = 0;

virtual void handleAddData(MantidWidgets::IAddWorkspaceDialog const *dialog) = 0;
virtual void handleRemoveClicked() = 0;
virtual void handleUnifyClicked() = 0;
Expand All @@ -38,10 +40,6 @@ class MANTIDQT_INELASTIC_DLL FitDataPresenter : public IFitDataPresenter, public
virtual bool addWorkspaceFromDialog(MantidWidgets::IAddWorkspaceDialog const *dialog);
void addWorkspace(const std::string &workspaceName, const FunctionModelSpectra &workspaceIndices);
void setResolution(const std::string &name);
void setSampleWSSuffices(const QStringList &suffices);
void setSampleFBSuffices(const QStringList &suffices);
void setResolutionWSSuffices(const QStringList &suffices);
void setResolutionFBSuffices(const QStringList &suffices);
void setStartX(double startX, WorkspaceID workspaceID);
void setStartX(double startX, WorkspaceID workspaceID, WorkspaceIndex spectrum);
void setEndX(double startX, WorkspaceID workspaceID);
Expand All @@ -52,11 +50,11 @@ class MANTIDQT_INELASTIC_DLL FitDataPresenter : public IFitDataPresenter, public
void updateTableFromModel();
WorkspaceID getNumberOfWorkspaces() const;
size_t getNumberOfDomains() const;
FunctionModelSpectra getSpectra(WorkspaceID workspaceID) const;
QList<FunctionModelDataset> getDatasets() const;
DataForParameterEstimationCollection getDataForParameterEstimation(const EstimationDataSelector &selector) const;
std::vector<double> getQValuesForData() const;
std::vector<std::string> createDisplayNames() const;
UserInputValidator &validate(UserInputValidator &validator);
void validate(UserInputValidator &validator);

virtual void addWorkspace(const std::string &workspaceName, const std::string &paramType, const int &spectrum_index) {
UNUSED_ARG(workspaceName);
Expand All @@ -77,6 +75,8 @@ class MANTIDQT_INELASTIC_DLL FitDataPresenter : public IFitDataPresenter, public

virtual void subscribeFitPropertyBrowser(IInelasticFitPropertyBrowser *browser) { UNUSED_ARG(browser); };

std::string tabName() const override;

void handleAddData(MantidWidgets::IAddWorkspaceDialog const *dialog) override;
void handleRemoveClicked() override;
void handleUnifyClicked() override;
Expand Down
16 changes: 4 additions & 12 deletions qt/scientific_interfaces/Inelastic/QENSFitting/FitDataView.cpp
Expand Up @@ -68,10 +68,9 @@ void FitDataView::setHorizontalHeaders(const QStringList &headers) {
m_uiForm->tbFitData->verticalHeader()->setVisible(false);
}

UserInputValidator &FitDataView::validate(UserInputValidator &validator) {
void FitDataView::validate(UserInputValidator &validator) {
if (m_uiForm->tbFitData->rowCount() == 0)
validator.addErrorMessage("No input data has been provided.");
return validator;
}

void FitDataView::displayWarning(const std::string &warning) {
Expand Down Expand Up @@ -131,22 +130,15 @@ QModelIndexList FitDataView::getSelectedIndexes() const {
return m_uiForm->tbFitData->selectionModel()->selectedIndexes();
}

void FitDataView::setSampleWSSuffices(const QStringList &suffixes) { m_wsSampleSuffixes = suffixes; }

void FitDataView::setSampleFBSuffices(const QStringList &suffixes) { m_fbSampleSuffixes = suffixes; }

void FitDataView::setResolutionWSSuffices(const QStringList &suffixes) { m_wsResolutionSuffixes = suffixes; }

void FitDataView::setResolutionFBSuffices(const QStringList &suffixes) { m_fbResolutionSuffixes = suffixes; }

void FitDataView::showAddWorkspaceDialog() {
auto dialog = new MantidWidgets::AddWorkspaceDialog(parentWidget());
connect(dialog, SIGNAL(addData(MantidWidgets::IAddWorkspaceDialog *)), this,
SLOT(notifyAddData(MantidWidgets::IAddWorkspaceDialog *)));

auto tabName = m_presenter->tabName();
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->setWSSuffices(m_wsSampleSuffixes);
dialog->setFBSuffices(m_fbSampleSuffixes);
dialog->setWSSuffices(InterfaceUtils::getSampleWSSuffixes(tabName));
dialog->setFBSuffices(InterfaceUtils::getSampleFBSuffixes(tabName));
dialog->updateSelectedSpectra();
dialog->show();
}
Expand Down
12 changes: 1 addition & 11 deletions qt/scientific_interfaces/Inelastic/QENSFitting/FitDataView.h
Expand Up @@ -36,7 +36,7 @@ class MANTIDQT_INELASTIC_DLL FitDataView : public QTabWidget, public IFitDataVie
QTableWidget *getDataTable() const override;
bool isTableEmpty() const override;

UserInputValidator &validate(UserInputValidator &validator) override;
void validate(UserInputValidator &validator) override;
virtual void addTableEntry(size_t row, FitDataRow newRow) override;
virtual void updateNumCellEntry(double numEntry, size_t row, size_t column) override;
int getColumnIndexFromName(std::string const &ColName) override;
Expand All @@ -45,11 +45,6 @@ class MANTIDQT_INELASTIC_DLL FitDataView : public QTabWidget, public IFitDataVie
QModelIndexList getSelectedIndexes() const override;
bool dataColumnContainsText(std::string const &columnText) const override;

void setSampleWSSuffices(const QStringList &suffices) override;
void setSampleFBSuffices(const QStringList &suffices) override;
void setResolutionWSSuffices(const QStringList &suffices) override;
void setResolutionFBSuffices(const QStringList &suffices) override;

void displayWarning(const std::string &warning) override;

protected slots:
Expand All @@ -61,11 +56,6 @@ protected slots:
std::unique_ptr<Ui::FitDataView> m_uiForm;
void setCell(std::unique_ptr<QTableWidgetItem> cell, size_t row, size_t column);

QStringList m_wsSampleSuffixes;
QStringList m_fbSampleSuffixes;
QStringList m_wsResolutionSuffixes;
QStringList m_fbResolutionSuffixes;

IFitDataPresenter *m_presenter;

protected slots:
Expand Down