Skip to content

Commit 99752a8

Browse files
author
jef
committed
followup r14006: add QgsLongLongValidator
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@14009 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent f49931b commit 99752a8

File tree

3 files changed

+109
-1
lines changed

3 files changed

+109
-1
lines changed

src/gui/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ qgsprojectionselector.h
9595
qgsquickprint.h
9696
qgsludialog.h
9797
qgsprojectbadlayerguihandler.h
98+
qgslonglongvalidator.h
9899
)
99100

100101
QT4_WRAP_CPP(QGIS_GUI_MOC_SRCS ${QGIS_GUI_MOC_HDRS})

src/gui/qgsattributeeditor.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <qgsuniquevaluerenderer.h>
2323
#include <qgscategorizedsymbolrendererv2.h>
2424
#include <qgssymbol.h>
25+
#include <qgslonglongvalidator.h>
2526

2627
#include <QPushButton>
2728
#include <QLineEdit>
@@ -354,10 +355,14 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed
354355
le->setCompleter( c );
355356
}
356357

357-
if ( myFieldType == QVariant::Int || myFieldType == QVariant::LongLong )
358+
if ( myFieldType == QVariant::Int )
358359
{
359360
le->setValidator( new QIntValidator( le ) );
360361
}
362+
else if ( myFieldType == QVariant::LongLong )
363+
{
364+
le->setValidator( new QgsLongLongValidator( le ) );
365+
}
361366
else if ( myFieldType == QVariant::Double )
362367
{
363368
le->setValidator( new QDoubleValidator( le ) );

src/gui/qgslonglongvalidator.h

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
/* $Id$ */
20+
21+
#ifndef QGSLONGLONGVALIDATOR_H
22+
#define QGSLONGLONGVALIDATOR_H
23+
24+
#include <limits>
25+
#include <QValidator>
26+
#include <QLocale>
27+
28+
class GUI_EXPORT QgsLongLongValidator : public QValidator
29+
{
30+
Q_OBJECT
31+
32+
public:
33+
explicit QgsLongLongValidator( QObject *parent )
34+
: QValidator( parent )
35+
, b( std::numeric_limits<qint64>::min() )
36+
, t( std::numeric_limits<qint64>::max() )
37+
{}
38+
39+
QgsLongLongValidator( qint64 bottom, qint64 top, QObject *parent )
40+
: QValidator( parent )
41+
, b( bottom )
42+
, t( top )
43+
{}
44+
45+
~QgsLongLongValidator()
46+
{}
47+
48+
QValidator::State validate( QString &input, int& ) const
49+
{
50+
if ( input.isEmpty() )
51+
return Intermediate;
52+
53+
if ( b >= 0 && input.startsWith( '-' ) )
54+
return Invalid;
55+
56+
if ( t < 0 && input.startsWith( '+' ) )
57+
return Invalid;
58+
59+
if ( input == "-" || input == "+" )
60+
return Intermediate;
61+
62+
63+
bool ok;
64+
qlonglong entered = input.toLongLong( &ok );
65+
if ( !ok )
66+
return Invalid;
67+
68+
if ( entered >= b && entered <= t )
69+
return Acceptable;
70+
71+
if ( entered >= 0 )
72+
{
73+
// the -entered < b condition is necessary to allow people to type
74+
// the minus last (e.g. for right-to-left languages)
75+
return ( entered > t && -entered < b ) ? Invalid : Intermediate;
76+
}
77+
else
78+
{
79+
return ( entered < b ) ? Invalid : Intermediate;
80+
}
81+
}
82+
83+
void setBottom( qint64 bottom ) { b = bottom; }
84+
void setTop( qint64 top ) { t = top; }
85+
86+
virtual void setRange( qint64 bottom, qint64 top )
87+
{
88+
b = bottom;
89+
t = top;
90+
}
91+
92+
qint64 bottom() const { return b; }
93+
qint64 top() const { return t; }
94+
95+
private:
96+
Q_DISABLE_COPY( QgsLongLongValidator )
97+
98+
qint64 b;
99+
qint64 t;
100+
};
101+
102+
#endif // QGSLONGLONGVALIDATOR_H

0 commit comments

Comments
 (0)