QGIS API Documentation  2.99.0-Master (ae4d26a)
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 #include "qgis_gui.h"
27 
32 class GUI_EXPORT QgsLongLongValidator : public QValidator
33 {
34  Q_OBJECT
35 
36  public:
37  explicit QgsLongLongValidator( QObject *parent )
38  : QValidator( parent )
39  , b( std::numeric_limits<qint64>::min() )
40  , t( std::numeric_limits<qint64>::max() )
41  {}
42 
43  QgsLongLongValidator( qint64 bottom, qint64 top, QObject *parent )
44  : QValidator( parent )
45  , b( bottom )
46  , t( top )
47  {}
48 
50  {}
51 
52  QValidator::State validate( QString &input, int & ) const override
53  {
54  if ( input.isEmpty() )
55  return Intermediate;
56 
57  if ( b >= 0 && input.startsWith( '-' ) )
58  return Invalid;
59 
60  if ( t < 0 && input.startsWith( '+' ) )
61  return Invalid;
62 
63  if ( input == QLatin1String( "-" ) || input == QLatin1String( "+" ) )
64  return Intermediate;
65 
66 
67  bool ok;
68  qlonglong entered = input.toLongLong( &ok );
69  if ( !ok )
70  return Invalid;
71 
72  if ( entered >= b && entered <= t )
73  return Acceptable;
74 
75  if ( entered >= 0 )
76  {
77  // the -entered < b condition is necessary to allow people to type
78  // the minus last (e.g. for right-to-left languages)
79  return ( entered > t && -entered < b ) ? Invalid : Intermediate;
80  }
81  else
82  {
83  return ( entered < b ) ? Invalid : Intermediate;
84  }
85  }
86 
87  void setBottom( qint64 bottom ) { b = bottom; }
88  void setTop( qint64 top ) { t = top; }
89 
90  virtual void setRange( qint64 bottom, qint64 top )
91  {
92  b = bottom;
93  t = top;
94  }
95 
96  qint64 bottom() const { return b; }
97  qint64 top() const { return t; }
98 
99  private:
100  Q_DISABLE_COPY( QgsLongLongValidator )
101 
102  qint64 b;
103  qint64 t;
104 };
105 
106 #endif // QGSLONGLONGVALIDATOR_H
virtual void setRange(qint64 bottom, qint64 top)
void setBottom(qint64 bottom)
QgsLongLongValidator(QObject *parent)
QValidator::State validate(QString &input, int &) const override
QgsLongLongValidator(qint64 bottom, qint64 top, QObject *parent)