QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
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
25#include "qgis_gui.h"
26
27#include <QLocale>
28#include <QValidator>
29
35class GUI_EXPORT QgsLongLongValidator : public QValidator
36{
37 Q_OBJECT
38
39 public:
40 explicit QgsLongLongValidator( QObject *parent )
41 : QValidator( parent )
42 , b( std::numeric_limits<qint64>::min() )
43 , t( std::numeric_limits<qint64>::max() )
44 {}
45
46 QgsLongLongValidator( qint64 bottom, qint64 top, QObject *parent )
47 : QValidator( parent )
48 , b( bottom )
49 , t( top )
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 const 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
A QValidator which allows validation of long long values.
virtual void setRange(qint64 bottom, qint64 top)
QgsLongLongValidator(QObject *parent)
QgsLongLongValidator(qint64 bottom, qint64 top, QObject *parent)
void setBottom(qint64 bottom)
QValidator::State validate(QString &input, int &) const override