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 <stdexcept>
9
10namespace {
11
12std::string OR(const std::string &lhs, const std::string &rhs) { return "(" + lhs + "|" + rhs + ")"; }
13std::string NATURAL_NUMBER_WITH_PRECISION(std::size_t digits) {
14 return OR("0", "[1-9][0-9]{," + std::to_string(digits - 1) + "}");
15}
16// non numeric characters
17const std::string EMPTY = "^$";
18const std::string SPACE = "(\\s)*";
19const std::string COMMA = SPACE + "," + SPACE;
20const std::string DASH = "\\-";
21
22// number and numeric sets
23const std::string NATURAL_NUMBER = "(0|[1-9][0-9]*)";
24const std::string REAL_NUMBER = "(-?" + NATURAL_NUMBER + "(\\.[0-9]*)?)";
25const std::string REAL_RANGE = "(" + REAL_NUMBER + COMMA + REAL_NUMBER + ")";
26const std::string NUMBER = NATURAL_NUMBER_WITH_PRECISION(4);
27const std::string NATURAL_RANGE = "(" + NUMBER + DASH + NUMBER + ")";
28const std::string NATURAL_OR_RANGE = OR(NATURAL_RANGE, NUMBER);
29
30// final lists
31const std::string MASK_LIST = "(" + REAL_RANGE + "(" + COMMA + REAL_RANGE + ")*" + ")|" + EMPTY;
32const std::string SPECTRA_LIST = "(" + NATURAL_OR_RANGE + "(" + COMMA + NATURAL_OR_RANGE + ")*)";
33} // namespace
34
35namespace MantidQt {
36namespace MantidWidgets {
37
38std::string getRegexValidatorString(const RegexValidatorStrings &validatorMask) {
39 switch (validatorMask) {
41 return SPECTRA_LIST;
43 return MASK_LIST;
44 default:
45 throw std::logic_error("Invalid or Missing Validator String");
46 }
47}
48
49QString makeQStringNumber(double value, int precision) { return QString::number(value, 'f', precision); }
50
51RegexInputDelegate::RegexInputDelegate(QWidget *parent, const std::string &validator)
52 : QStyledItemDelegate(parent), m_validator(QRegExp(QString::fromStdString(validator))) {}
53
54QWidget *RegexInputDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /*option*/,
55 const QModelIndex & /*index*/) const {
56 auto lineEdit = new QLineEdit(parent);
57 auto validator = new QRegExpValidator(m_validator, parent);
58 lineEdit->setValidator(validator);
59 return lineEdit;
60}
61
62NumericInputDelegate::NumericInputDelegate(QWidget *parent, int precision)
63 : QStyledItemDelegate(parent), m_precision(precision) {}
64
65QWidget *NumericInputDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const {
66
67 auto lineEdit = new QLineEdit(parent);
68 auto validator = new QDoubleValidator(parent);
69
70 validator->setDecimals(m_precision);
71 validator->setNotation(QDoubleValidator::StandardNotation);
72 lineEdit->setValidator(validator);
73 return lineEdit;
74}
75
76void NumericInputDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
77 const auto value = index.model()->data(index, Qt::EditRole).toDouble();
78 static_cast<QLineEdit *>(editor)->setText(makeQStringNumber(value, m_precision));
79}
80
81} // namespace MantidWidgets
82} // 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)