Commit
… properties form QgsDoubleValidator modification after review update doc
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/gui/qgsdoublevalidator.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ | ||
|
||
|
||
|
||
|
||
class QgsDoubleValidator : QRegularExpressionValidator | ||
{ | ||
%Docstring | ||
|
||
QgsDoubleValidator is a QLineEdit Validator that combines QDoubleValidator | ||
and QRegularExpressionValidator to allow user to enter double with both | ||
local and C interpretation. | ||
|
||
.. versionadded:: 3.14 | ||
%End | ||
|
||
%TypeHeaderCode | ||
#include "qgsdoublevalidator.h" | ||
%End | ||
public: | ||
|
||
explicit QgsDoubleValidator( QObject *parent ); | ||
%Docstring | ||
Constructor for QgsDoubleValidator. | ||
|
||
.. versionadded:: 3.14 | ||
%End | ||
|
||
QgsDoubleValidator( QRegularExpression reg, double bottom, double top, QObject *parent ); | ||
%Docstring | ||
Constructor for QgsDoubleValidator. | ||
|
||
.. versionadded:: 3.14 | ||
%End | ||
|
||
QgsDoubleValidator( double bottom, double top, QObject *parent ); | ||
%Docstring | ||
Constructor for QgsDoubleValidator. | ||
|
||
.. versionadded:: 3.14 | ||
%End | ||
|
||
QgsDoubleValidator( double bottom, double top, int dec, QObject *parent ); | ||
%Docstring | ||
Constructor for QgsDoubleValidator. | ||
|
||
.. versionadded:: 3.14 | ||
%End | ||
|
||
virtual QValidator::State validate( QString &input, int & ) const; | ||
|
||
%Docstring | ||
Evaluates input QString validity according to QRegularExpression | ||
and ability to be converted in double value. | ||
|
||
.. versionadded:: 3.14 | ||
%End | ||
|
||
QValidator::State validate( QString input ) const; | ||
%Docstring | ||
Evaluates input QString validity according to QRegularExpression | ||
and ability to be converted in double value. | ||
|
||
.. versionadded:: 3.14 | ||
%End | ||
|
||
|
||
static double toDouble( QString input ); | ||
%Docstring | ||
Converts QString to double value. | ||
It used locale interpretation first | ||
and C locale interpretation as fallback | ||
|
||
.. versionadded:: 3.14 | ||
%End | ||
|
||
void setBottom( double bottom ); | ||
%Docstring | ||
Set top range limit | ||
|
||
.. seealso:: :py:func:`setTop` | ||
|
||
.. seealso:: :py:func:`setRange` | ||
|
||
.. versionadded:: 3.14 | ||
%End | ||
|
||
void setTop( double top ); | ||
%Docstring | ||
Set top range limit | ||
|
||
.. seealso:: :py:func:`setBottom` | ||
|
||
.. seealso:: :py:func:`setRange` | ||
|
||
.. versionadded:: 3.14 | ||
%End | ||
|
||
virtual void setRange( double bottom, double top ); | ||
%Docstring | ||
Set bottom and top range limits | ||
|
||
.. seealso:: :py:func:`setBottom` | ||
|
||
.. seealso:: :py:func:`setTop` | ||
|
||
.. versionadded:: 3.14 | ||
%End | ||
|
||
double bottom() const; | ||
%Docstring | ||
Returns top range limit | ||
|
||
.. seealso:: :py:func:`setBottom` | ||
|
||
.. versionadded:: 3.14 | ||
%End | ||
|
||
double top() const; | ||
%Docstring | ||
Returns top range limit | ||
|
||
.. seealso:: :py:func:`setTop` | ||
|
||
.. versionadded:: 3.14 | ||
%End | ||
|
||
}; | ||
|
||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/gui/qgsdoublevalidator.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/*************************************************************************** | ||
qgsdoublevalidator.cpp - description | ||
------------------- | ||
begin : June 2020 | ||
copyright : (C) 2020 by Sebastien Peillet | ||
email : sebastien.peillet@oslandia.com | ||
adapted version of Qgslonglongvalidator + QgsFieldValidator | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* 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. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include <limits> | ||
#include <QRegExpValidator> | ||
#include <QLocale> | ||
#include "qgis_gui.h" | ||
|
||
#include "qgsdoublevalidator.h" | ||
|
||
const QString PERMISSIVE_DOUBLE = R"(-?[\d]{0,1000}([\.%1][\d]{0,1000})?(e[+-]?[\d]{0,%2})?)"; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
SebastienPeillet
Contributor
|
||
|
||
QgsDoubleValidator::QgsDoubleValidator( QObject *parent ) | ||
: QRegularExpressionValidator( parent ) | ||
, b( std::numeric_limits<qreal>::min() ) | ||
, t( std::numeric_limits<qreal>::max() ) | ||
{ | ||
// The regular expression accept double with point as decimal point but also the locale decimal point | ||
QRegularExpression reg( PERMISSIVE_DOUBLE.arg( locale().decimalPoint() ).arg( 1000 ) ); | ||
this->setRegularExpression( reg ); | ||
} | ||
|
||
QgsDoubleValidator::QgsDoubleValidator( QRegularExpression reg, double bottom, double top, QObject *parent ) | ||
: QRegularExpressionValidator( parent ) | ||
, b( bottom ) | ||
, t( top ) | ||
{ | ||
this->setRegularExpression( reg ); | ||
} | ||
|
||
QgsDoubleValidator::QgsDoubleValidator( double bottom, double top, QObject *parent ) | ||
: QRegularExpressionValidator( parent ) | ||
, b( bottom ) | ||
, t( top ) | ||
{ | ||
// The regular expression accept double with point as decimal point but also the locale decimal point | ||
QRegularExpression reg( PERMISSIVE_DOUBLE.arg( locale().decimalPoint() ).arg( 1000 ) ); | ||
this->setRegularExpression( reg ); | ||
} | ||
|
||
QgsDoubleValidator::QgsDoubleValidator( double bottom, double top, int dec, QObject *parent ) | ||
: QRegularExpressionValidator( parent ) | ||
, b( bottom ) | ||
, t( top ) | ||
{ | ||
// The regular expression accept double with point as decimal point but also the locale decimal point | ||
QRegularExpression reg( PERMISSIVE_DOUBLE.arg( locale().decimalPoint() ).arg( QString::number( dec ) ) ); | ||
this->setRegularExpression( reg ); | ||
} | ||
|
||
QValidator::State QgsDoubleValidator::validate( QString &input, int & ) const | ||
{ | ||
if ( input.isEmpty() ) | ||
return Intermediate; | ||
|
||
|
||
bool ok = false; | ||
double entered = QgsDoubleValidator::toDouble( input, &ok ); | ||
if ( ! ok ) | ||
{ | ||
if ( regularExpression().match( input ).captured( 0 ) == input ) | ||
return Intermediate; | ||
else | ||
return Invalid; | ||
} | ||
|
||
if ( entered >= b && entered <= t && regularExpression().match( input ).captured( 0 ) == input ) | ||
return Acceptable; | ||
else | ||
return Intermediate; | ||
} | ||
|
||
QValidator::State QgsDoubleValidator::validate( QString input ) const | ||
{ | ||
if ( input.isEmpty() ) | ||
return Intermediate; | ||
|
||
|
||
bool ok = false; | ||
double entered = QgsDoubleValidator::toDouble( input, &ok ); | ||
if ( ! ok ) | ||
{ | ||
if ( regularExpression().match( input ).captured( 0 ) == input ) | ||
return Intermediate; | ||
else | ||
return Invalid; | ||
} | ||
|
||
if ( entered >= b && entered <= t && regularExpression().match( input ).captured( 0 ) == input ) | ||
return Acceptable; | ||
else | ||
return Intermediate; | ||
} | ||
|
||
double QgsDoubleValidator::toDouble( QString input ) | ||
{ | ||
bool ok = false; | ||
double value = QLocale().toDouble( input, &ok ); | ||
if ( ! ok ) | ||
{ | ||
value = QLocale( QLocale::C ).toDouble( input, &ok ); | ||
} | ||
return value; | ||
} | ||
|
||
double QgsDoubleValidator::toDouble( QString input, bool *ok ) | ||
{ | ||
double value = QLocale().toDouble( input, ok ); | ||
|
||
if ( ! *ok ) | ||
{ | ||
value = QLocale( QLocale::C ).toDouble( input, ok ); | ||
} | ||
return value ; | ||
} |
@SebastienPeillet
I don't understand how the validation is supposed to work with
decimals
,say I want to use the validator for integers, I would set
decimals
= 0 but the regexp checks for the exponent so this would be valid and the test fails (assumeen
locale):Can you please clarify?