Skip to content

Commit

Permalink
followup r14006: add QgsLongLongValidator
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@14009 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Aug 4, 2010
1 parent f49931b commit 99752a8
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -95,6 +95,7 @@ qgsprojectionselector.h
qgsquickprint.h
qgsludialog.h
qgsprojectbadlayerguihandler.h
qgslonglongvalidator.h
)

QT4_WRAP_CPP(QGIS_GUI_MOC_SRCS ${QGIS_GUI_MOC_HDRS})
Expand Down
7 changes: 6 additions & 1 deletion src/gui/qgsattributeeditor.cpp
Expand Up @@ -22,6 +22,7 @@
#include <qgsuniquevaluerenderer.h>
#include <qgscategorizedsymbolrendererv2.h>
#include <qgssymbol.h>
#include <qgslonglongvalidator.h>

#include <QPushButton>
#include <QLineEdit>
Expand Down Expand Up @@ -354,10 +355,14 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed
le->setCompleter( c );
}

if ( myFieldType == QVariant::Int || myFieldType == QVariant::LongLong )
if ( myFieldType == QVariant::Int )
{
le->setValidator( new QIntValidator( le ) );
}
else if ( myFieldType == QVariant::LongLong )
{
le->setValidator( new QgsLongLongValidator( le ) );
}
else if ( myFieldType == QVariant::Double )
{
le->setValidator( new QDoubleValidator( le ) );
Expand Down
102 changes: 102 additions & 0 deletions src/gui/qgslonglongvalidator.h
@@ -0,0 +1,102 @@
/***************************************************************************
qgslonglongvalidator.h - description
-------------------
begin : August 2010
copyright : (C) 2010 by Jürgen E. Fischer
email : jef@norbit.de
adapted version of QIntValidator for qint64
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/* $Id$ */

#ifndef QGSLONGLONGVALIDATOR_H
#define QGSLONGLONGVALIDATOR_H

#include <limits>
#include <QValidator>
#include <QLocale>

class GUI_EXPORT QgsLongLongValidator : public QValidator
{
Q_OBJECT

public:
explicit QgsLongLongValidator( QObject *parent )
: QValidator( parent )
, b( std::numeric_limits<qint64>::min() )
, t( std::numeric_limits<qint64>::max() )
{}

QgsLongLongValidator( qint64 bottom, qint64 top, QObject *parent )
: QValidator( parent )
, b( bottom )
, t( top )
{}

~QgsLongLongValidator()
{}

QValidator::State validate( QString &input, int& ) const
{
if ( input.isEmpty() )
return Intermediate;

if ( b >= 0 && input.startsWith( '-' ) )
return Invalid;

if ( t < 0 && input.startsWith( '+' ) )
return Invalid;

if ( input == "-" || input == "+" )
return Intermediate;


bool ok;
qlonglong entered = input.toLongLong( &ok );
if ( !ok )
return Invalid;

if ( entered >= b && entered <= t )
return Acceptable;

if ( entered >= 0 )
{
// the -entered < b condition is necessary to allow people to type
// the minus last (e.g. for right-to-left languages)
return ( entered > t && -entered < b ) ? Invalid : Intermediate;
}
else
{
return ( entered < b ) ? Invalid : Intermediate;
}
}

void setBottom( qint64 bottom ) { b = bottom; }
void setTop( qint64 top ) { t = top; }

virtual void setRange( qint64 bottom, qint64 top )
{
b = bottom;
t = top;
}

qint64 bottom() const { return b; }
qint64 top() const { return t; }

private:
Q_DISABLE_COPY( QgsLongLongValidator )

qint64 b;
qint64 t;
};

#endif // QGSLONGLONGVALIDATOR_H

0 comments on commit 99752a8

Please sign in to comment.