Mantid
Loading...
Searching...
No Matches
TableWidgetValidators.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2024 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 +
8#include <QRegularExpressionValidator>
9#include <stdexcept>
10
11namespace {
12
13std::string OR(const std::string &lhs, const std::string &rhs) { return "(" + lhs + "|" + rhs + ")"; }
14std::string NATURAL_NUMBER_WITH_PRECISION(std::size_t digits) {
15 return OR("0", "[1-9][0-9]{," + std::to_string(digits - 1) + "}");
16}
17// non numeric characters
18const std::string EMPTY = "^$";
19const std::string SPACE = "(\\s)*";
20const std::string COMMA = SPACE + "," + SPACE;
21const std::string DASH = "\\-";
22
23// number and numeric sets
24const std::string NATURAL_NUMBER = "(0|[1-9][0-9]*)";
25const std::string REAL_NUMBER = "(-?" + NATURAL_NUMBER + "(\\.[0-9]*)?)";
26const std::string REAL_RANGE = "(" + REAL_NUMBER + COMMA + REAL_NUMBER + ")";
27const std::string NUMBER = NATURAL_NUMBER_WITH_PRECISION(4);
28const std::string NATURAL_RANGE = "(" + NUMBER + DASH + NUMBER + ")";
29const std::string NATURAL_OR_RANGE = OR(NATURAL_RANGE, NUMBER);
30
31// final lists
32const std::string MASK_LIST = "(" + REAL_RANGE + "(" + COMMA + REAL_RANGE + ")*" + ")|" + EMPTY;
33const std::string SPECTRA_LIST = "(" + NATURAL_OR_RANGE + "(" + COMMA + NATURAL_OR_RANGE + ")*)";
34} // namespace
35
36namespace MantidQt {
37namespace MantidWidgets {
38
39std::string getRegexValidatorString(const RegexValidatorStrings &validatorMask) {
40 switch (validatorMask) {
42 return SPECTRA_LIST;
44 return MASK_LIST;
45 default:
46 throw std::logic_error("Invalid or Missing Validator String");
47 }
48}
49
50QString makeQStringNumber(double value, int precision) { return QString::number(value, 'f', precision); }
51
52RegexInputDelegate::RegexInputDelegate(QWidget *parent, const std::string &validator)
53 : QStyledItemDelegate(parent), m_validator(QRegularExpression(QString::fromStdString(validator))) {}
54
55QWidget *RegexInputDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /*option*/,
56 const QModelIndex & /*index*/) const {
57 auto lineEdit = new QLineEdit(parent);
58 auto validator = new QRegularExpressionValidator(m_validator, parent);
59 lineEdit->setValidator(validator);
60 return lineEdit;
61}
62
63NumericInputDelegate::NumericInputDelegate(QWidget *parent, int precision)
64 : QStyledItemDelegate(parent), m_precision(precision) {}
65
66QWidget *NumericInputDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const {
67
68 auto lineEdit = new QLineEdit(parent);
69 auto validator = new QDoubleValidator(parent);
70
71 validator->setDecimals(m_precision);
72 validator->setNotation(QDoubleValidator::StandardNotation);
73 lineEdit->setValidator(validator);
74 return lineEdit;
75}
76
77void NumericInputDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
78 const auto value = index.model()->data(index, Qt::EditRole).toDouble();
79 static_cast<QLineEdit *>(editor)->setText(makeQStringNumber(value, m_precision));
80}
81
82} // namespace MantidWidgets
83} // namespace MantidQt
Kernel::IValidator_sptr m_validator
const std::vector< double > & rhs
double value
The value of the point.
Definition FitMW.cpp:51
std::map< DeltaEMode::Type, std::string > index
NumericInputDelegate(QWidget *parent=nullptr, int=DEFAULT_NUMERICAL_PRECISION)
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const override
void setEditorData(QWidget *editor, const QModelIndex &index) const override
RegexInputDelegate(QWidget *parent=nullptr, const std::string &validator=DEFAULT_REGEX)
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const override
EXPORT_OPT_MANTIDQT_COMMON QString makeQStringNumber(double value, int precision)
EXPORT_OPT_MANTIDQT_COMMON std::string getRegexValidatorString(const RegexValidatorStrings &validatorMask)
The AlgorithmProgressDialogPresenter keeps track of the running algorithms and displays a progress ba...
std::string to_string(const wide_integer< Bits, Signed > &n)