Mantid
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
MantidQt::CustomInterfaces::UserInputValidator Class Reference

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...
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ UserInputValidator()

MantidQt::CustomInterfaces::UserInputValidator::UserInputValidator ( )

Default Constructor.

Definition at line 51 of file UserInputValidator.cpp.

Member Function Documentation

◆ addErrorMessage()

void MantidQt::CustomInterfaces::UserInputValidator::addErrorMessage ( const QString &  message,
bool  silent = false 
)

◆ checkBins()

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.

Parameters
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
Returns
True if the input was valid

Definition at line 230 of file UserInputValidator.cpp.

References addErrorMessage(), lower, tolerance, and upper.

◆ checkDataSelectorIsValid()

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.

Parameters
name:: the "name" of the widget so as to be recognised by the user.
widget:: the widget to check
silentTrue if an error should not be added to the validator.
Returns
True if the input was valid

Definition at line 141 of file UserInputValidator.cpp.

References addErrorMessage(), MantidQt::MantidWidgets::DataSelector::getProblem(), and MantidQt::MantidWidgets::DataSelector::isValid().

◆ checkFieldIsNotEmpty()

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 "".

Parameters
name:: name of the field, so as to be recognised by the user
field:: the field object to be checked
errorLabel:: the "*" or "" label.
Returns
True if the input was valid

Definition at line 64 of file UserInputValidator.cpp.

References addErrorMessage(), and setErrorLabel().

◆ checkFieldIsValid()

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.

Parameters
field:: the field to be checked
errorLabel:: the "" or "*" label
errorMessage:: the message to log if invalid
Returns
True if the input was valid

Definition at line 84 of file UserInputValidator.cpp.

References addErrorMessage(), and setErrorLabel().

◆ checkFileFinderWidgetIsValid()

bool MantidQt::CustomInterfaces::UserInputValidator::checkFileFinderWidgetIsValid ( const QString &  name,
FileFinderWidget widget 
)

Check that the given FileFinderWidget widget has valid files.

Parameters
name:: the "name" of the widget so as to be recognised by the user.
widget:: the widget to check
Returns
True if the input was valid

Definition at line 124 of file UserInputValidator.cpp.

References addErrorMessage(), MantidQt::API::FileFinderWidget::getFileProblem(), and MantidQt::API::FileFinderWidget::isValid().

◆ checkNotEqual()

bool MantidQt::CustomInterfaces::UserInputValidator::checkNotEqual ( const QString &  name,
double  x,
double  y = 0.0,
double  tolerance = 0.00000001 
)

Checks two values are not equal.

Parameters
nameName of value
xFirst value
ySecond value (defaults to zero)
toleranceTolerance to which to compare
Returns
True if input was valid

Definition at line 269 of file UserInputValidator.cpp.

References addErrorMessage(), Mantid::Kernel::delta, tolerance, Mantid::Geometry::x, and Mantid::Geometry::y.

◆ checkRangeIsEnclosed()

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.

Parameters
outerName:: the end of the range
outer:: pair of range bounds
innerName:: the start of the range
inner:: pair of range bounds
Returns
True if the input was valid

Definition at line 205 of file UserInputValidator.cpp.

References addErrorMessage().

◆ checkRangesDontOverlap()

bool MantidQt::CustomInterfaces::UserInputValidator::checkRangesDontOverlap ( std::pair< double, double >  rangeA,
std::pair< double, double >  rangeB 
)

Check that the given ranges dont overlap.

Parameters
rangeA:: the start of the range
rangeB:: the end of the range
Returns
True if the input was valid

Definition at line 178 of file UserInputValidator.cpp.

References addErrorMessage().

◆ checkValidRange()

bool MantidQt::CustomInterfaces::UserInputValidator::checkValidRange ( const QString &  name,
std::pair< double, double >  range 
)

Check that the given start and end range is valid.

Parameters
name:: the name of the range
range:: the range
Returns
True if the input was valid

Definition at line 157 of file UserInputValidator.cpp.

References addErrorMessage().

◆ checkWorkspaceExists()

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.

Parameters
workspaceNameName of the workspace
silentTrue if an error should not be added to the validator.
Returns
True if the workspace is in the ADS

Definition at line 291 of file UserInputValidator.cpp.

References addErrorMessage().

Referenced by checkWorkspaceNumberOfBins(), checkWorkspaceNumberOfHistograms(), and checkWorkspaceType().

◆ checkWorkspaceGroupIsValid()

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.

Parameters
groupNameThe name of the workspace group
inputTypeThe name to put in the error message (e.g. "Sample")
silentTrue if an error should not be added to the validator.
Returns
True if the workspace group is valid

Definition at line 371 of file UserInputValidator.cpp.

References addErrorMessage(), and error.

◆ checkWorkspaceNumberOfBins() [1/2]

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.

Parameters
workspaceThe workspace
validSizeThe valid number of bins
Returns
True if the workspace has the correct size

Definition at line 352 of file UserInputValidator.cpp.

References addErrorMessage(), std::to_string(), and workspace.

◆ checkWorkspaceNumberOfBins() [2/2]

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.

Parameters
workspaceNameName of the workspace
validSizeThe valid number of bins
Returns
True if the workspace has the correct size

Definition at line 339 of file UserInputValidator.cpp.

References checkWorkspaceExists(), checkWorkspaceNumberOfBins(), and getADSWorkspace().

Referenced by checkWorkspaceNumberOfBins().

◆ checkWorkspaceNumberOfHistograms() [1/2]

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.

Parameters
workspaceThe workspace
validSizeThe valid number of histograms
Returns
True if the workspace has the correct size

Definition at line 322 of file UserInputValidator.cpp.

References addErrorMessage(), std::to_string(), and workspace.

◆ checkWorkspaceNumberOfHistograms() [2/2]

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.

Parameters
workspaceNameName of the workspace
validSizeThe valid number of histograms
Returns
True if the workspace has the correct size

Definition at line 309 of file UserInputValidator.cpp.

References checkWorkspaceExists(), checkWorkspaceNumberOfHistograms(), and getADSWorkspace().

Referenced by checkWorkspaceNumberOfHistograms().

◆ checkWorkspaceSelectorIsNotEmpty()

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.

Parameters
name:: the "name" of the workspace selector, so as to be recognised by the user
workspaceSelector:: the workspace selector to check
Returns
True if the input was valid

Definition at line 108 of file UserInputValidator.cpp.

References addErrorMessage().

◆ checkWorkspaceType()

template<typename T >
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.

Parameters
workspaceNameThe name of the workspace
inputTypeWhat the workspace is used for (e.g. Sample)
validTypeThe type which is valid
silentTrue if an error should not be added to the validator.
Returns
True if the workspace has the correct type

Definition at line 112 of file UserInputValidator.h.

References addErrorMessage(), and checkWorkspaceExists().

◆ generateErrorMessage()

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.

◆ getADSWorkspace()

template<typename T >
std::shared_ptr< T > MantidQt::CustomInterfaces::UserInputValidator::getADSWorkspace ( std::string const &  workspaceName)
private

Gets a workspace from the ADS.

Parameters
workspaceNameThe name of the workspace
Returns
The workspace

Definition at line 130 of file UserInputValidator.h.

References Mantid::Kernel::SingletonHolder< T >::Instance().

Referenced by checkWorkspaceNumberOfBins(), and checkWorkspaceNumberOfHistograms().

◆ isAllInputValid()

bool MantidQt::CustomInterfaces::UserInputValidator::isAllInputValid ( )

Checks to see if all input is valid.

Checks if the user input checked is valid.

Returns
True if all input is valid, false otherwise

Definition at line 413 of file UserInputValidator.cpp.

References m_error.

◆ setErrorLabel()

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.

Parameters
errorLabelLabel to change
validIf the input was valid

Definition at line 421 of file UserInputValidator.cpp.

Referenced by checkFieldIsNotEmpty(), and checkFieldIsValid().

Member Data Documentation

◆ m_error

bool MantidQt::CustomInterfaces::UserInputValidator::m_error
private

True if there has been an error.

Definition at line 99 of file UserInputValidator.h.

Referenced by addErrorMessage(), and isAllInputValid().

◆ m_errorMessages

QStringList MantidQt::CustomInterfaces::UserInputValidator::m_errorMessages
private

Any raised error messages.

Definition at line 97 of file UserInputValidator.h.

Referenced by addErrorMessage(), and generateErrorMessage().


The documentation for this class was generated from the following files: