OGS
StrictDoubleValidator.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#pragma once
5
6#include <QDoubleValidator>
7
12class StrictDoubleValidator : public QDoubleValidator
13{
14public:
15 StrictDoubleValidator ( double min, double max, std::size_t decimals, QObject* parent = nullptr) :
16 QDoubleValidator( min, max, decimals, parent)
17 {}
18
19 explicit StrictDoubleValidator(QObject* parent = nullptr)
20 : QDoubleValidator(parent)
21 {}
22
23 QValidator::State validate(QString& input, int& pos) const override
24 {
25 Q_UNUSED(pos);
26 if (input.isEmpty() || input == "." || input == "-") return Intermediate;
27
28 QChar const decimalPoint('.');
29 if (input.indexOf(decimalPoint) != -1)
30 {
31 int const charsAfterPoint = input.length() - input.indexOf(decimalPoint) - 1;
32 if (charsAfterPoint > decimals())
33 return QValidator::Invalid;
34 }
35
36 bool ok;
37 double const d = input.toDouble(&ok);
38
39 if (ok && d >= bottom() && d <= top())
40 return QValidator::Acceptable;
41 else
42 return QValidator::Invalid;
43 }
44};
StrictDoubleValidator(QObject *parent=nullptr)
QValidator::State validate(QString &input, int &pos) const override
StrictDoubleValidator(double min, double max, std::size_t decimals, QObject *parent=nullptr)