swift
uppercasevalidator.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2014 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QString>
7 #include <QtGlobal>
8 
9 #include "misc/stringutils.h"
10 
11 using namespace swift::misc;
12 
13 namespace swift::gui
14 {
15  CUpperCaseValidator::CUpperCaseValidator(QObject *parent) : QValidator(parent) {}
16 
17  CUpperCaseValidator::CUpperCaseValidator(int minLength, int maxLength, QObject *parent)
18  : QValidator(parent), m_minLength(minLength), m_maxLength(maxLength)
19  {
20  if (minLength < 1) { m_optionalValue = true; };
21  }
22 
23  CUpperCaseValidator::CUpperCaseValidator(bool optionalValue, int minLength, int maxLength, QObject *parent)
24  : QValidator(parent), m_optionalValue(optionalValue), m_minLength(minLength), m_maxLength(maxLength)
25  {
26  if (minLength < 1) { m_optionalValue = true; };
27  }
28 
30  {
31  static const QString chars("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
32  this->setAllowedCharacters(chars);
33  }
34 
35  QValidator::State CUpperCaseValidator::validate(QString &input, int &pos) const
36  {
37  Q_UNUSED(input);
38  Q_UNUSED(pos);
39  this->fixup(input);
40 
41  if (m_optionalValue && input.isEmpty()) { return Acceptable; }
42  if (input.length() > m_maxLength) { return Invalid; }
43  if (input.length() < m_minLength) { return Intermediate; }
44  if (!m_restrictions.isEmpty())
45  {
46  bool valid = false;
47  for (const QString &r : m_restrictions)
48  {
49  if (r.startsWith(input))
50  {
51  valid = true;
52  break;
53  }
54  }
55  if (!valid) { return Invalid; }
56  }
57  return Acceptable;
58  }
59 
60  void CUpperCaseValidator::fixup(QString &input) const
61  {
62  if (input.isEmpty()) { return; }
63  input = input.toUpper();
64  if (!m_allowedCharacters.isEmpty()) { input = removeIfNotInString(input, m_allowedCharacters); }
65  }
66 } // namespace swift::gui
CUpperCaseValidator(QObject *parent=nullptr)
Constructor.
void setAllowedCharacters09AZ()
Set the allowed characters as 0-9 and A-Z.
virtual void fixup(QString &input) const
virtual State validate(QString &input, int &pos) const
void setAllowedCharacters(const QString &chars)
Allowed characters.
GUI related classes.
Free functions in swift::misc.
QString removeIfNotInString(const QString &string, const QString &inString)
Remove if NOT in string.
Definition: stringutils.h:49