QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
49  QValidator::State validate( QString &input, int & ) const override
50  {
51  if ( input.isEmpty() )
52  return Intermediate;
53 
54  if ( b >= 0 && input.startsWith( '-' ) )
55  return Invalid;
56 
57  if ( t < 0 && input.startsWith( '+' ) )
58  return Invalid;
59 
60  if ( input == QLatin1String( "-" ) || input == QLatin1String( "+" ) )
61  return Intermediate;
62 
63 
64  bool ok;
65  qlonglong entered = input.toLongLong( &ok );
66  if ( !ok )
67  return Invalid;
68 
69  if ( entered >= b && entered <= t )
70  return Acceptable;
71 
72  if ( entered >= 0 )
73  {
74  // the -entered < b condition is necessary to allow people to type
75  // the minus last (e.g. for right-to-left languages)
76  return ( entered > t && -entered < b ) ? Invalid : Intermediate;
77  }
78  else
79  {
80  return ( entered < b ) ? Invalid : Intermediate;
81  }
82  }
83 
84  void setBottom( qint64 bottom ) { b = bottom; }
85  void setTop( qint64 top ) { t = top; }
86 
87  virtual void setRange( qint64 bottom, qint64 top )
88  {
89  b = bottom;
90  t = top;
91  }
92 
93  qint64 bottom() const { return b; }
94  qint64 top() const { return t; }
95 
96  private:
97  Q_DISABLE_COPY( QgsLongLongValidator )
98 
99  qint64 b;
100  qint64 t;
101 };
102 
103 #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)