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