|
| 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