Mantid
Loading...
Searching...
No Matches
UserInputValidator.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2007 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 +
7#pragma once
8
11#include "MantidQtWidgets/Common/DllOption.h"
15#include <QStringList>
16
20
21class QLineEdit;
22class QLabel;
23class QString;
24
25namespace MantidQt {
26namespace CustomInterfaces {
27
28class EXPORT_OPT_MANTIDQT_COMMON IUserInputValidator {
29public:
30 virtual ~IUserInputValidator() = default;
31
32 virtual bool checkFieldIsNotEmpty(const QString &name, QLineEdit *field, QLabel *errorLabel = nullptr) = 0;
33 virtual bool checkFieldIsValid(const QString &errorMessage, QLineEdit *field, QLabel *errorLabel = nullptr) = 0;
34 virtual bool checkWorkspaceSelectorIsNotEmpty(const QString &name, WorkspaceSelector *workspaceSelector) = 0;
35 virtual bool checkFileFinderWidgetIsValid(const QString &name, const FileFinderWidget *widget) = 0;
36 virtual bool checkDataSelectorIsValid(const QString &name, DataSelector *widget, bool const silent = false,
37 bool const autoLoad = true) = 0;
38 virtual bool checkWorkspaceGroupIsValid(QString const &groupName, QString const &inputType, bool silent = false) = 0;
39 virtual bool checkWorkspaceExists(QString const &workspaceName, bool silent = false) = 0;
40 virtual bool checkValidRange(const QString &name, std::pair<double, double> range) = 0;
41 virtual bool checkRangesDontOverlap(std::pair<double, double> rangeA, std::pair<double, double> rangeB) = 0;
42 virtual bool checkRangeIsEnclosed(const QString &outerName, std::pair<double, double> outer, const QString &innerName,
43 std::pair<double, double> inner) = 0;
44 virtual bool checkBins(double lower, double binWidth, double upper, double tolerance = 0.00000001) = 0;
45 template <typename T = Mantid::API::MatrixWorkspace>
46 bool checkWorkspaceType(QString const &workspaceName, QString const &inputType, QString const &validType,
47 bool silent = false);
48
49 virtual void setErrorLabel(QLabel *errorLabel, bool valid) = 0;
50
51 virtual void addErrorMessage(const std::string &message, bool const silent = false) = 0;
52
53 virtual std::string generateErrorMessage() const = 0;
54 virtual bool isAllInputValid() const = 0;
55};
56
66class EXPORT_OPT_MANTIDQT_COMMON UserInputValidator final : public IUserInputValidator {
67public:
71
73 bool checkFieldIsNotEmpty(const QString &name, QLineEdit *field, QLabel *errorLabel = nullptr) override;
75 bool checkFieldIsValid(const QString &errorMessage, QLineEdit *field, QLabel *errorLabel = nullptr) override;
77 bool checkWorkspaceSelectorIsNotEmpty(const QString &name, WorkspaceSelector *workspaceSelector) override;
79 bool checkFileFinderWidgetIsValid(const QString &name, const FileFinderWidget *widget) override;
81 bool checkDataSelectorIsValid(const QString &name, DataSelector *widget, bool const silent = false,
82 bool const autoLoad = true) override;
84 bool checkValidRange(const QString &name, std::pair<double, double> range) override;
86 bool checkRangesDontOverlap(std::pair<double, double> rangeA, std::pair<double, double> rangeB) override;
88 bool checkRangeIsEnclosed(const QString &outerName, std::pair<double, double> outer, const QString &innerName,
89 std::pair<double, double> inner) override;
91 bool checkBins(double lower, double binWidth, double upper, double tolerance = 0.00000001) override;
93 bool checkNotEqual(const QString &name, double x, double y = 0.0, double tolerance = 0.00000001);
94
96 bool checkWorkspaceExists(QString const &workspaceName, bool silent = false) override;
98 bool checkWorkspaceNumberOfHistograms(QString const &workspaceName, std::size_t const &validSize);
99 bool checkWorkspaceNumberOfHistograms(const Mantid::API::MatrixWorkspace_sptr &, std::size_t const &validSize);
101 bool checkWorkspaceNumberOfBins(QString const &workspaceName, std::size_t const &validSize);
102 bool checkWorkspaceNumberOfBins(const Mantid::API::MatrixWorkspace_sptr &, std::size_t const &validSize);
104 bool checkWorkspaceGroupIsValid(QString const &groupName, QString const &inputType, bool silent = false) override;
105
107 void addErrorMessage(const std::string &message, bool const silent = false) override;
108
110 void setErrorLabel(QLabel *errorLabel, bool valid) override;
111
114 std::string generateErrorMessage() const override;
116 bool isAllInputValid() const override;
117
118private:
120 QStringList m_errorMessages;
123};
124
134template <typename T>
135bool IUserInputValidator::checkWorkspaceType(QString const &workspaceName, QString const &inputType,
136 QString const &validType, bool silent) {
137 if (!checkWorkspaceExists(workspaceName, silent)) {
138 return false;
139 }
140
141 if (!MantidWidgets::WorkspaceUtils::getADSWorkspace<T>(workspaceName.toStdString())) {
142 addErrorMessage("The " + inputType.toStdString() + " workspace is not a " + validType.toStdString() + ".", silent);
143 return false;
144 }
145 return true;
146}
147
148} // namespace CustomInterfaces
149} // namespace MantidQt
std::string name
Definition Run.cpp:60
double tolerance
double lower
lower and upper bounds on the multiplier, if known
double upper
This class defines a widget for file searching.
virtual bool checkWorkspaceExists(QString const &workspaceName, bool silent=false)=0
virtual bool checkWorkspaceSelectorIsNotEmpty(const QString &name, WorkspaceSelector *workspaceSelector)=0
virtual bool checkFieldIsNotEmpty(const QString &name, QLineEdit *field, QLabel *errorLabel=nullptr)=0
virtual void addErrorMessage(const std::string &message, bool const silent=false)=0
virtual bool checkRangeIsEnclosed(const QString &outerName, std::pair< double, double > outer, const QString &innerName, std::pair< double, double > inner)=0
virtual bool checkDataSelectorIsValid(const QString &name, DataSelector *widget, bool const silent=false, bool const autoLoad=true)=0
virtual bool checkFieldIsValid(const QString &errorMessage, QLineEdit *field, QLabel *errorLabel=nullptr)=0
bool checkWorkspaceType(QString const &workspaceName, QString const &inputType, QString const &validType, bool silent=false)
Checks if the workspace has the correct type.
virtual bool checkFileFinderWidgetIsValid(const QString &name, const FileFinderWidget *widget)=0
virtual bool checkWorkspaceGroupIsValid(QString const &groupName, QString const &inputType, bool silent=false)=0
virtual bool checkValidRange(const QString &name, std::pair< double, double > range)=0
virtual bool checkRangesDontOverlap(std::pair< double, double > rangeA, std::pair< double, double > rangeB)=0
virtual bool checkBins(double lower, double binWidth, double upper, double tolerance=0.00000001)=0
virtual std::string generateErrorMessage() const =0
virtual void setErrorLabel(QLabel *errorLabel, bool valid)=0
A class to try and get rid of some of the boiler-plate code surrounding input validation,...
bool m_error
True if there has been an error.
QStringList m_errorMessages
Any raised error messages.
This class defines a widget for selecting a workspace of file path by using a combination of two chil...
This class defines a widget for selecting a workspace present in the AnalysisDataService.
The AlgorithmProgressDialogPresenter keeps track of the running algorithms and displays a progress ba...
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class