Quantum GIS API Documentation
1.8
|
00001 /*************************************************************************** 00002 qgslonglongvalidator.h - description 00003 ------------------- 00004 begin : August 2010 00005 copyright : (C) 2010 by Jürgen E. Fischer 00006 email : [email protected] 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 00020 #ifndef QGSLONGLONGVALIDATOR_H 00021 #define QGSLONGLONGVALIDATOR_H 00022 00023 #include <limits> 00024 #include <QValidator> 00025 #include <QLocale> 00026 00027 class GUI_EXPORT QgsLongLongValidator : public QValidator 00028 { 00029 Q_OBJECT 00030 00031 public: 00032 explicit QgsLongLongValidator( QObject *parent ) 00033 : QValidator( parent ) 00034 , b( std::numeric_limits<qint64>::min() ) 00035 , t( std::numeric_limits<qint64>::max() ) 00036 {} 00037 00038 QgsLongLongValidator( qint64 bottom, qint64 top, QObject *parent ) 00039 : QValidator( parent ) 00040 , b( bottom ) 00041 , t( top ) 00042 {} 00043 00044 ~QgsLongLongValidator() 00045 {} 00046 00047 QValidator::State validate( QString &input, int& ) const 00048 { 00049 if ( input.isEmpty() ) 00050 return Intermediate; 00051 00052 if ( b >= 0 && input.startsWith( '-' ) ) 00053 return Invalid; 00054 00055 if ( t < 0 && input.startsWith( '+' ) ) 00056 return Invalid; 00057 00058 if ( input == "-" || input == "+" ) 00059 return Intermediate; 00060 00061 00062 bool ok; 00063 qlonglong entered = input.toLongLong( &ok ); 00064 if ( !ok ) 00065 return Invalid; 00066 00067 if ( entered >= b && entered <= t ) 00068 return Acceptable; 00069 00070 if ( entered >= 0 ) 00071 { 00072 // the -entered < b condition is necessary to allow people to type 00073 // the minus last (e.g. for right-to-left languages) 00074 return ( entered > t && -entered < b ) ? Invalid : Intermediate; 00075 } 00076 else 00077 { 00078 return ( entered < b ) ? Invalid : Intermediate; 00079 } 00080 } 00081 00082 void setBottom( qint64 bottom ) { b = bottom; } 00083 void setTop( qint64 top ) { t = top; } 00084 00085 virtual void setRange( qint64 bottom, qint64 top ) 00086 { 00087 b = bottom; 00088 t = top; 00089 } 00090 00091 qint64 bottom() const { return b; } 00092 qint64 top() const { return t; } 00093 00094 private: 00095 Q_DISABLE_COPY( QgsLongLongValidator ) 00096 00097 qint64 b; 00098 qint64 t; 00099 }; 00100 00101 #endif // QGSLONGLONGVALIDATOR_H