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