QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgslonglongvalidator.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgslonglongvalidator.h - description
3  -------------------
4  begin : August 2010
5  copyright : (C) 2010 by Jürgen E. Fischer
6  email : [email protected]
7 
8  adapted version of QIntValidator for qint64
9  ***************************************************************************/
10 
11 /***************************************************************************
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * *
18  ***************************************************************************/
19 
20 #ifndef QGSLONGLONGVALIDATOR_H
21 #define QGSLONGLONGVALIDATOR_H
22 
23 #include <limits>
24 #include <QValidator>
25 #include <QLocale>
26 
30 class GUI_EXPORT QgsLongLongValidator : public QValidator
31 {
32  Q_OBJECT
33 
34  public:
35  explicit QgsLongLongValidator( QObject *parent )
36  : QValidator( parent )
37  , b( std::numeric_limits<qint64>::min() )
38  , t( std::numeric_limits<qint64>::max() )
39  {}
40 
41  QgsLongLongValidator( qint64 bottom, qint64 top, QObject *parent )
42  : QValidator( parent )
43  , b( bottom )
44  , t( top )
45  {}
46 
48  {}
49 
50  QValidator::State validate( QString &input, int& ) const override
51  {
52  if ( input.isEmpty() )
53  return Intermediate;
54 
55  if ( b >= 0 && input.startsWith( '-' ) )
56  return Invalid;
57 
58  if ( t < 0 && input.startsWith( '+' ) )
59  return Invalid;
60 
61  if ( input == "-" || input == "+" )
62  return Intermediate;
63 
64 
65  bool ok;
66  qlonglong entered = input.toLongLong( &ok );
67  if ( !ok )
68  return Invalid;
69 
70  if ( entered >= b && entered <= t )
71  return Acceptable;
72 
73  if ( entered >= 0 )
74  {
75  // the -entered < b condition is necessary to allow people to type
76  // the minus last (e.g. for right-to-left languages)
77  return ( entered > t && -entered < b ) ? Invalid : Intermediate;
78  }
79  else
80  {
81  return ( entered < b ) ? Invalid : Intermediate;
82  }
83  }
84 
85  void setBottom( qint64 bottom ) { b = bottom; }
86  void setTop( qint64 top ) { t = top; }
87 
88  virtual void setRange( qint64 bottom, qint64 top )
89  {
90  b = bottom;
91  t = top;
92  }
93 
94  qint64 bottom() const { return b; }
95  qint64 top() const { return t; }
96 
97  private:
98  Q_DISABLE_COPY( QgsLongLongValidator )
99 
100  qint64 b;
101  qint64 t;
102 };
103 
104 #endif // QGSLONGLONGVALIDATOR_H
virtual void setRange(qint64 bottom, qint64 top)
void setBottom(qint64 bottom)
double ANALYSIS_EXPORT max(double x, double y)
Returns the maximum of two doubles or the first argument if both are equal.
bool isEmpty() const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
QgsLongLongValidator(QObject *parent)
QValidator::State validate(QString &input, int &) const override
double ANALYSIS_EXPORT min(double x, double y)
Returns the minimum of two doubles or the first argument if both are equal.
QgsLongLongValidator(qint64 bottom, qint64 top, QObject *parent)
qlonglong toLongLong(bool *ok, int base) const