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