From dae22f5a3f0cb9706d862a5752b250f6312b1b6f Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Sat, 15 Nov 2014 01:32:01 +0100 Subject: [PATCH] refs #288, validator for upper case inputs such as callsign, ICAO data --- src/blackgui/uppercasevalidator.cpp | 43 +++++++++++++++++++++++++++ src/blackgui/uppercasevalidator.h | 46 +++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 src/blackgui/uppercasevalidator.cpp create mode 100644 src/blackgui/uppercasevalidator.h diff --git a/src/blackgui/uppercasevalidator.cpp b/src/blackgui/uppercasevalidator.cpp new file mode 100644 index 000000000..cbeaded06 --- /dev/null +++ b/src/blackgui/uppercasevalidator.cpp @@ -0,0 +1,43 @@ +/* Copyright (C) 2014 + * swift project Community / Contributors + * + * This file is part of swift Project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "uppercasevalidator.h" + +namespace BlackGui +{ + CUpperCaseValidator::CUpperCaseValidator(QObject *parent) : QValidator(parent) + { } + + CUpperCaseValidator::CUpperCaseValidator(int minLength, int maxLength, QObject *parent) : QValidator(parent), + m_minLength(minLength), m_maxLength(maxLength) + { } + + CUpperCaseValidator::CUpperCaseValidator(bool optionalValue, int minLength, int maxLength, QObject *parent) : QValidator(parent), + m_minLength(minLength), m_maxLength(maxLength), m_optionalValue(optionalValue) + { } + + QValidator::State CUpperCaseValidator::validate(QString &input, int &pos) const + { + Q_UNUSED(input); + Q_UNUSED(pos); + fixup(input); + + if (m_optionalValue && input.isEmpty()) { return Acceptable; } + if (input.length() > m_maxLength) { return Invalid; } + if (input.length() < m_minLength) { return Intermediate; } + return Acceptable; + } + + void CUpperCaseValidator::fixup(QString &input) const + { + if (input.isEmpty()) { return; } + input = input.toUpper(); + } + +} // namespace diff --git a/src/blackgui/uppercasevalidator.h b/src/blackgui/uppercasevalidator.h new file mode 100644 index 000000000..3dff68ea7 --- /dev/null +++ b/src/blackgui/uppercasevalidator.h @@ -0,0 +1,46 @@ +/* Copyright (C) 2014 + * swift project Community / Contributors + * + * This file is part of swift Project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKGUI_UPPERCASEVALIDATOR_H +#define BLACKGUI_UPPERCASEVALIDATOR_H + +#include + +namespace BlackGui +{ + //! Forces uppercase + class CUpperCaseValidator : public QValidator + { + + public: + //! Constructor + explicit CUpperCaseValidator(QObject *parent = nullptr); + + //! Constructor + CUpperCaseValidator(int minLength, int maxLength, QObject *parent = nullptr); + + //! Constructor + CUpperCaseValidator(bool optionalValue, int minLength, int maxLength, QObject *parent = nullptr); + + //! \copydoc QValidator::validate + virtual State validate(QString &input, int &pos) const override; + + //! \copydoc QValidator::fixup + virtual void fixup(QString &input) const override; + + private: + bool m_optionalValue = false; + int m_minLength = 0; + int m_maxLength = 32678; // standard length + + }; +} +#endif // guard