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