Mantid
|
A class to try and get rid of some of the boiler-plate code surrounding input validation, and hopefully as a result make it more readable. More...
#include <UserInputValidator.h>
Public Member Functions | |
void | addErrorMessage (const QString &message, bool silent=false) |
Add a custom error message to the list. More... | |
bool | checkBins (double lower, double binWidth, double upper, double tolerance=0.00000001) |
Check that the given range can be split evenly into bins of the given width. More... | |
bool | checkDataSelectorIsValid (const QString &name, DataSelector *widget, bool silent=false) |
Check that the given DataSelector widget has valid input. More... | |
bool | checkFieldIsNotEmpty (const QString &name, QLineEdit *field, QLabel *errorLabel=nullptr) |
Check that the given QLineEdit field is not empty. More... | |
bool | checkFieldIsValid (const QString &errorMessage, QLineEdit *field, QLabel *errorLabel=nullptr) |
Check that the given QLineEdit field is valid as per any validators it might have. More... | |
bool | checkFileFinderWidgetIsValid (const QString &name, FileFinderWidget *widget) |
Check that the given FileFinderWidget widget has valid files. More... | |
bool | checkNotEqual (const QString &name, double x, double y=0.0, double tolerance=0.00000001) |
Checks two values are not equal. More... | |
bool | checkRangeIsEnclosed (const QString &outerName, std::pair< double, double > outer, const QString &innerName, std::pair< double, double > inner) |
Check that the given "outer" range completely encloses the given "inner" range. More... | |
bool | checkRangesDontOverlap (std::pair< double, double > rangeA, std::pair< double, double > rangeB) |
Check that the given ranges dont overlap. More... | |
bool | checkValidRange (const QString &name, std::pair< double, double > range) |
Check that the given start and end range is valid. More... | |
bool | checkWorkspaceExists (QString const &workspaceName, bool silent=false) |
Checks that a workspace exists in the ADS. More... | |
bool | checkWorkspaceGroupIsValid (QString const &groupName, QString const &inputType, bool silent=false) |
Checks that a workspace group contains valid matrix workspace's. More... | |
bool | checkWorkspaceNumberOfBins (const Mantid::API::MatrixWorkspace_sptr &, std::size_t const &validSize) |
Checks that a workspaces has a valid number of bins. More... | |
bool | checkWorkspaceNumberOfBins (QString const &workspaceName, std::size_t const &validSize) |
Checks the number of bins in a workspace. More... | |
bool | checkWorkspaceNumberOfHistograms (const Mantid::API::MatrixWorkspace_sptr &, std::size_t const &validSize) |
Checks that a workspaces has a valid number of histograms. More... | |
bool | checkWorkspaceNumberOfHistograms (QString const &workspaceName, std::size_t const &validSize) |
Checks the number of histograms in a workspace. More... | |
bool | checkWorkspaceSelectorIsNotEmpty (const QString &name, WorkspaceSelector *workspaceSelector) |
Check that the given WorkspaceSelector is not empty. More... | |
template<typename T = Mantid::API::MatrixWorkspace> | |
bool | checkWorkspaceType (QString const &workspaceName, QString const &inputType, QString const &validType, bool silent=false) |
Checks that a workspace has the correct workspace type. More... | |
QString | generateErrorMessage () |
Returns an error message which contains all the error messages raised by the check functions. More... | |
bool | isAllInputValid () |
Checks to see if all input is valid. More... | |
void | setErrorLabel (QLabel *errorLabel, bool valid) |
Sets a validation label. More... | |
UserInputValidator () | |
Default Constructor. More... | |
Private Member Functions | |
template<typename T = Mantid::API::MatrixWorkspace> | |
std::shared_ptr< T > | getADSWorkspace (std::string const &workspaceName) |
Gets a workspace from the ADS. More... | |
Private Attributes | |
bool | m_error |
True if there has been an error. More... | |
QStringList | m_errorMessages |
Any raised error messages. More... | |
A class to try and get rid of some of the boiler-plate code surrounding input validation, and hopefully as a result make it more readable.
It has as its state a QStringList, which are the accumulated error messages after multiple calls to its "check[...]" member-functions.
Definition at line 34 of file UserInputValidator.h.
MantidQt::CustomInterfaces::UserInputValidator::UserInputValidator | ( | ) |
Default Constructor.
Definition at line 51 of file UserInputValidator.cpp.
void MantidQt::CustomInterfaces::UserInputValidator::addErrorMessage | ( | const QString & | message, |
bool | silent = false |
||
) |
Add a custom error message to the list.
message | :: the message to add to the list |
silent | True if an error should not be added to the validator. |
Definition at line 390 of file UserInputValidator.cpp.
References m_error, and m_errorMessages.
Referenced by checkBins(), checkDataSelectorIsValid(), checkFieldIsNotEmpty(), checkFieldIsValid(), checkFileFinderWidgetIsValid(), checkNotEqual(), checkRangeIsEnclosed(), checkRangesDontOverlap(), checkValidRange(), checkWorkspaceExists(), checkWorkspaceGroupIsValid(), checkWorkspaceNumberOfBins(), checkWorkspaceNumberOfHistograms(), checkWorkspaceSelectorIsNotEmpty(), and checkWorkspaceType().
bool MantidQt::CustomInterfaces::UserInputValidator::checkBins | ( | double | lower, |
double | binWidth, | ||
double | upper, | ||
double | tolerance = 0.00000001 |
||
) |
Check that the given range can be split evenly into bins of the given width.
Given a range defined by lower and upper bounds, checks to see if it can be divided evenly into bins of a given width.
Due to the nature of doubles, we use a tolerance value.
lower | :: the lower bound of the range |
binWidth | :: the wdith of the bin |
upper | :: the upper bound of the range |
tolerance | :: the tolerance with which to judge range / bin suitablility |
Definition at line 230 of file UserInputValidator.cpp.
References addErrorMessage(), lower, tolerance, and upper.
bool MantidQt::CustomInterfaces::UserInputValidator::checkDataSelectorIsValid | ( | const QString & | name, |
DataSelector * | widget, | ||
bool | silent = false |
||
) |
Check that the given DataSelector widget has valid input.
Check that the given DataSelector widget has valid files.
name | :: the "name" of the widget so as to be recognised by the user. |
widget | :: the widget to check |
silent | True if an error should not be added to the validator. |
Definition at line 141 of file UserInputValidator.cpp.
References addErrorMessage(), MantidQt::MantidWidgets::DataSelector::getProblem(), and MantidQt::MantidWidgets::DataSelector::isValid().
bool MantidQt::CustomInterfaces::UserInputValidator::checkFieldIsNotEmpty | ( | const QString & | name, |
QLineEdit * | field, | ||
QLabel * | errorLabel = nullptr |
||
) |
Check that the given QLineEdit field is not empty.
Check that a given QLineEdit field (with given name) is not empty.
If it is empty then the given QLabel will be set to "*" and an error will be added to m_errorMessages. Else set the label to "".
name | :: name of the field, so as to be recognised by the user |
field | :: the field object to be checked |
errorLabel | :: the "*" or "" label. |
Definition at line 64 of file UserInputValidator.cpp.
References addErrorMessage(), and setErrorLabel().
bool MantidQt::CustomInterfaces::UserInputValidator::checkFieldIsValid | ( | const QString & | errorMessage, |
QLineEdit * | field, | ||
QLabel * | errorLabel = nullptr |
||
) |
Check that the given QLineEdit field is valid as per any validators it might have.
field | :: the field to be checked |
errorLabel | :: the "" or "*" label |
errorMessage | :: the message to log if invalid |
Definition at line 84 of file UserInputValidator.cpp.
References addErrorMessage(), and setErrorLabel().
bool MantidQt::CustomInterfaces::UserInputValidator::checkFileFinderWidgetIsValid | ( | const QString & | name, |
FileFinderWidget * | widget | ||
) |
Check that the given FileFinderWidget widget has valid files.
name | :: the "name" of the widget so as to be recognised by the user. |
widget | :: the widget to check |
Definition at line 124 of file UserInputValidator.cpp.
References addErrorMessage(), MantidQt::API::FileFinderWidget::getFileProblem(), and MantidQt::API::FileFinderWidget::isValid().
bool MantidQt::CustomInterfaces::UserInputValidator::checkNotEqual | ( | const QString & | name, |
double | x, | ||
double | y = 0.0 , |
||
double | tolerance = 0.00000001 |
||
) |
Checks two values are not equal.
name | Name of value |
x | First value |
y | Second value (defaults to zero) |
tolerance | Tolerance to which to compare |
Definition at line 269 of file UserInputValidator.cpp.
References addErrorMessage(), Mantid::Kernel::delta, tolerance, Mantid::Geometry::x, and Mantid::Geometry::y.
bool MantidQt::CustomInterfaces::UserInputValidator::checkRangeIsEnclosed | ( | const QString & | outerName, |
std::pair< double, double > | outer, | ||
const QString & | innerName, | ||
std::pair< double, double > | inner | ||
) |
Check that the given "outer" range completely encloses the given "inner" range.
outerName | :: the end of the range |
outer | :: pair of range bounds |
innerName | :: the start of the range |
inner | :: pair of range bounds |
Definition at line 205 of file UserInputValidator.cpp.
References addErrorMessage().
bool MantidQt::CustomInterfaces::UserInputValidator::checkRangesDontOverlap | ( | std::pair< double, double > | rangeA, |
std::pair< double, double > | rangeB | ||
) |
Check that the given ranges dont overlap.
rangeA | :: the start of the range |
rangeB | :: the end of the range |
Definition at line 178 of file UserInputValidator.cpp.
References addErrorMessage().
bool MantidQt::CustomInterfaces::UserInputValidator::checkValidRange | ( | const QString & | name, |
std::pair< double, double > | range | ||
) |
Check that the given start and end range is valid.
name | :: the name of the range |
range | :: the range |
Definition at line 157 of file UserInputValidator.cpp.
References addErrorMessage().
bool MantidQt::CustomInterfaces::UserInputValidator::checkWorkspaceExists | ( | QString const & | workspaceName, |
bool | silent = false |
||
) |
Checks that a workspace exists in the ADS.
Checks that a workspace exists within the ADS.
workspaceName | Name of the workspace |
silent | True if an error should not be added to the validator. |
Definition at line 291 of file UserInputValidator.cpp.
References addErrorMessage().
Referenced by checkWorkspaceNumberOfBins(), checkWorkspaceNumberOfHistograms(), and checkWorkspaceType().
bool MantidQt::CustomInterfaces::UserInputValidator::checkWorkspaceGroupIsValid | ( | QString const & | groupName, |
QString const & | inputType, | ||
bool | silent = false |
||
) |
Checks that a workspace group contains valid matrix workspace's.
Checks that a workspaces group is not empty and doesn't contain null workspaces.
groupName | The name of the workspace group |
inputType | The name to put in the error message (e.g. "Sample") |
silent | True if an error should not be added to the validator. |
Definition at line 371 of file UserInputValidator.cpp.
References addErrorMessage(), and error.
bool MantidQt::CustomInterfaces::UserInputValidator::checkWorkspaceNumberOfBins | ( | const Mantid::API::MatrixWorkspace_sptr & | workspace, |
std::size_t const & | validSize | ||
) |
Checks that a workspaces has a valid number of bins.
workspace | The workspace |
validSize | The valid number of bins |
Definition at line 352 of file UserInputValidator.cpp.
References addErrorMessage(), std::to_string(), and workspace.
bool MantidQt::CustomInterfaces::UserInputValidator::checkWorkspaceNumberOfBins | ( | QString const & | workspaceName, |
std::size_t const & | validSize | ||
) |
Checks the number of bins in a workspace.
Checks that a workspaces has a valid number of bins.
workspaceName | Name of the workspace |
validSize | The valid number of bins |
Definition at line 339 of file UserInputValidator.cpp.
References checkWorkspaceExists(), checkWorkspaceNumberOfBins(), and getADSWorkspace().
Referenced by checkWorkspaceNumberOfBins().
bool MantidQt::CustomInterfaces::UserInputValidator::checkWorkspaceNumberOfHistograms | ( | const Mantid::API::MatrixWorkspace_sptr & | workspace, |
std::size_t const & | validSize | ||
) |
Checks that a workspaces has a valid number of histograms.
workspace | The workspace |
validSize | The valid number of histograms |
Definition at line 322 of file UserInputValidator.cpp.
References addErrorMessage(), std::to_string(), and workspace.
bool MantidQt::CustomInterfaces::UserInputValidator::checkWorkspaceNumberOfHistograms | ( | QString const & | workspaceName, |
std::size_t const & | validSize | ||
) |
Checks the number of histograms in a workspace.
Checks that a workspaces has a valid number of histograms.
workspaceName | Name of the workspace |
validSize | The valid number of histograms |
Definition at line 309 of file UserInputValidator.cpp.
References checkWorkspaceExists(), checkWorkspaceNumberOfHistograms(), and getADSWorkspace().
Referenced by checkWorkspaceNumberOfHistograms().
bool MantidQt::CustomInterfaces::UserInputValidator::checkWorkspaceSelectorIsNotEmpty | ( | const QString & | name, |
WorkspaceSelector * | workspaceSelector | ||
) |
Check that the given WorkspaceSelector is not empty.
Appends a message to m_errorMessages if so.
name | :: the "name" of the workspace selector, so as to be recognised by the user |
workspaceSelector | :: the workspace selector to check |
Definition at line 108 of file UserInputValidator.cpp.
References addErrorMessage().
bool MantidQt::CustomInterfaces::UserInputValidator::checkWorkspaceType | ( | QString const & | workspaceName, |
QString const & | inputType, | ||
QString const & | validType, | ||
bool | silent = false |
||
) |
Checks that a workspace has the correct workspace type.
Checks if the workspace has the correct type.
workspaceName | The name of the workspace |
inputType | What the workspace is used for (e.g. Sample) |
validType | The type which is valid |
silent | True if an error should not be added to the validator. |
Definition at line 112 of file UserInputValidator.h.
References addErrorMessage(), and checkWorkspaceExists().
QString MantidQt::CustomInterfaces::UserInputValidator::generateErrorMessage | ( | ) |
Returns an error message which contains all the error messages raised by the check functions.
Generates and returns an error message which contains all the error messages raised by the check functions.
Definition at line 401 of file UserInputValidator.cpp.
References m_errorMessages.
|
private |
Gets a workspace from the ADS.
workspaceName | The name of the workspace |
Definition at line 130 of file UserInputValidator.h.
References Mantid::Kernel::SingletonHolder< T >::Instance().
Referenced by checkWorkspaceNumberOfBins(), and checkWorkspaceNumberOfHistograms().
bool MantidQt::CustomInterfaces::UserInputValidator::isAllInputValid | ( | ) |
Checks to see if all input is valid.
Checks if the user input checked is valid.
Definition at line 413 of file UserInputValidator.cpp.
References m_error.
void MantidQt::CustomInterfaces::UserInputValidator::setErrorLabel | ( | QLabel * | errorLabel, |
bool | valid | ||
) |
Sets a validation label.
Sets a validation label that is displyed next to the widget on the UI.
errorLabel | Label to change |
valid | If the input was valid |
Definition at line 421 of file UserInputValidator.cpp.
Referenced by checkFieldIsNotEmpty(), and checkFieldIsValid().
|
private |
True if there has been an error.
Definition at line 99 of file UserInputValidator.h.
Referenced by addErrorMessage(), and isAllInputValid().
|
private |
Any raised error messages.
Definition at line 97 of file UserInputValidator.h.
Referenced by addErrorMessage(), and generateErrorMessage().