Quantum GIS API Documentation
1.7.4
|
00001 /*************************************************************************** 00002 qgslonglongvalidator.h - description 00003 ------------------- 00004 begin : August 2010 00005 copyright : (C) 2010 by Jürgen E. Fischer 00006 email : jef@norbit.de 00007 00008 adapted version of QIntValidator for qint64 00009 ***************************************************************************/ 00010 00011 /*************************************************************************** 00012 * * 00013 * This program is free software; you can redistribute it and/or modify * 00014 * it under the terms of the GNU General Public License as published by * 00015 * the Free Software Foundation; either version 2 of the License, or * 00016 * (at your option) any later version. * 00017 * * 00018 ***************************************************************************/ 00019 /* $Id$ */ 00020 00021 #ifndef QGSLONGLONGVALIDATOR_H 00022 #define QGSLONGLONGVALIDATOR_H 00023 00024 #include <limits> 00025 #include <QValidator> 00026 #include <QLocale> 00027 00028 class GUI_EXPORT QgsLongLongValidator : public QValidator 00029 { 00030 Q_OBJECT 00031 00032 public: 00033 explicit QgsLongLongValidator( QObject *parent ) 00034 : QValidator( parent ) 00035 , b( std::numeric_limits<qint64>::min() ) 00036 , t( std::numeric_limits<qint64>::max() ) 00037 {} 00038 00039 QgsLongLongValidator( qint64 bottom, qint64 top, QObject *parent ) 00040 : QValidator( parent ) 00041 , b( bottom ) 00042 , t( top ) 00043 {} 00044 00045 ~QgsLongLongValidator() 00046 {} 00047 00048 QValidator::State validate( QString &input, int& ) const 00049 { 00050 if ( input.isEmpty() ) 00051 return Intermediate; 00052 00053 if ( b >= 0 && input.startsWith( '-' ) ) 00054 return Invalid; 00055 00056 if ( t < 0 && input.startsWith( '+' ) ) 00057 return Invalid; 00058 00059 if ( input == "-" || input == "+" ) 00060 return Intermediate; 00061 00062 00063 bool ok; 00064 qlonglong entered = input.toLongLong( &ok ); 00065 if ( !ok ) 00066 return Invalid; 00067 00068 if ( entered >= b && entered <= t ) 00069 return Acceptable; 00070 00071 if ( entered >= 0 ) 00072 { 00073 // the -entered < b condition is necessary to allow people to type 00074 // the minus last (e.g. for right-to-left languages) 00075 return ( entered > t && -entered < b ) ? Invalid : Intermediate; 00076 } 00077 else 00078 { 00079 return ( entered < b ) ? Invalid : Intermediate; 00080 } 00081 } 00082 00083 void setBottom( qint64 bottom ) { b = bottom; } 00084 void setTop( qint64 top ) { t = top; } 00085 00086 virtual void setRange( qint64 bottom, qint64 top ) 00087 { 00088 b = bottom; 00089 t = top; 00090 } 00091 00092 qint64 bottom() const { return b; } 00093 qint64 top() const { return t; } 00094 00095 private: 00096 Q_DISABLE_COPY( QgsLongLongValidator ) 00097 00098 qint64 b; 00099 qint64 t; 00100 }; 00101 00102 #endif // QGSLONGLONGVALIDATOR_H