Skip to content
Permalink
Browse files

[bugfix] Allow empty null representation in spinboxes

Fixes #18583 - Widget "Range": Representation for NULL value is NOT always displayed correctly
  • Loading branch information
elpaso committed Sep 18, 2018
1 parent 5c15a65 commit d179635b921f09b22d5456605894e79fee80d6e6
@@ -124,6 +124,13 @@ Returns the value used when clear() is called.
Set alignment in the embedded line edit widget

:param alignment:
%End

void setSpecialValueText( const QString &txt );
%Docstring
Set the special-value text to be ``txt``
If set, the spin box will display this text instead of a numeric value whenever the current value
is equal to minimum(). Typical use is to indicate that this choice has a special (default) meaning.
%End

virtual double valueFromText( const QString &text ) const;
@@ -124,6 +124,13 @@ Returns the value used when clear() is called.
Set alignment in the embedded line edit widget

:param alignment:
%End

void setSpecialValueText( const QString &txt );
%Docstring
Set the special-value text to be ``txt``
If set, the spin box will display this text instead of a numeric value whenever the current value
is equal to minimum(). Typical use is to indicate that this choice has a special (default) meaning.
%End

virtual int valueFromText( const QString &text ) const;
@@ -26,6 +26,12 @@

#define CLEAR_ICON_SIZE 16

// This is required because private implementation of
// QAbstractSpinBoxPrivate checks for specialText emptiness
// and skips specialText handling if it's empty
QString QgsDoubleSpinBox::SPECIAL_TEXT_WHEN_EMPTY = QChar( 0x2063 );


QgsDoubleSpinBox::QgsDoubleSpinBox( QWidget *parent )
: QDoubleSpinBox( parent )
{
@@ -140,13 +146,24 @@ void QgsDoubleSpinBox::setLineEditAlignment( Qt::Alignment alignment )
mLineEdit->setAlignment( alignment );
}

void QgsDoubleSpinBox::setSpecialValueText( const QString &txt )
{
if ( txt.isEmpty() )
QDoubleSpinBox::setSpecialValueText( SPECIAL_TEXT_WHEN_EMPTY );
else
QDoubleSpinBox::setSpecialValueText( txt );
}

QString QgsDoubleSpinBox::stripped( const QString &originalText ) const
{
//adapted from QAbstractSpinBoxPrivate::stripped
//trims whitespace, prefix and suffix from spin box text
QString text = originalText;
if ( specialValueText().isEmpty() || text != specialValueText() )
{
// Strip SPECIAL_TEXT_WHEN_EMPTY
if ( text.contains( SPECIAL_TEXT_WHEN_EMPTY ) )
text = text.replace( SPECIAL_TEXT_WHEN_EMPTY, QString() );
int from = 0;
int size = text.size();
bool changed = false;
@@ -132,6 +132,13 @@ class GUI_EXPORT QgsDoubleSpinBox : public QDoubleSpinBox
*/
void setLineEditAlignment( Qt::Alignment alignment );

/**
* Set the special-value text to be \a txt
* If set, the spin box will display this text instead of a numeric value whenever the current value
* is equal to minimum(). Typical use is to indicate that this choice has a special (default) meaning.
*/
void setSpecialValueText( const QString &txt );

double valueFromText( const QString &text ) const override;
QValidator::State validate( QString &input, int &pos ) const override;
void paintEvent( QPaintEvent *e ) override;
@@ -156,6 +163,13 @@ class GUI_EXPORT QgsDoubleSpinBox : public QDoubleSpinBox
bool mExpressionsEnabled = true;

QString stripped( const QString &originalText ) const;

// This is required because private implementation of
// QAbstractSpinBoxPrivate checks for specialText emptiness
// and skips specialText handling if it's empty
static QString SPECIAL_TEXT_WHEN_EMPTY;

friend class TestQgsRangeWidgetWrapper;
};

#endif // QGSDOUBLESPINBOX_H
@@ -22,6 +22,8 @@
#include "qgsdial.h"
#include "qgsslider.h"



QgsRangeWidgetWrapper::QgsRangeWidgetWrapper( QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent )
: QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )

@@ -119,7 +121,11 @@ void QgsRangeWidgetWrapper::initWidget( QWidget *editor )
// Note: call setMinimum here or setValue won't work
mDoubleSpinBox->setMinimum( minval );
mDoubleSpinBox->setValue( minval );
mDoubleSpinBox->setSpecialValueText( QgsApplication::nullRepresentation() );
QgsDoubleSpinBox *doubleSpinBox( qobject_cast<QgsDoubleSpinBox *>( mDoubleSpinBox ) );
if ( doubleSpinBox )
doubleSpinBox->setSpecialValueText( QgsApplication::nullRepresentation() );
else
mDoubleSpinBox->setSpecialValueText( QgsApplication::nullRepresentation() );
}
mDoubleSpinBox->setMinimum( minval );
mDoubleSpinBox->setMaximum( maxval );
@@ -141,7 +147,11 @@ void QgsRangeWidgetWrapper::initWidget( QWidget *editor )
int stepval = step.isValid() ? step.toInt() : 1;
minval -= stepval;
mIntSpinBox->setValue( minval );
mIntSpinBox->setSpecialValueText( QgsApplication::nullRepresentation() );
QgsSpinBox *intSpinBox( qobject_cast<QgsSpinBox *>( mIntSpinBox ) );
if ( intSpinBox )
intSpinBox->setSpecialValueText( QgsApplication::nullRepresentation() );
else
mIntSpinBox->setSpecialValueText( QgsApplication::nullRepresentation() );
}
setupIntEditor( minval, max, step, mIntSpinBox, this );
if ( config( QStringLiteral( "Suffix" ) ).isValid() )
@@ -26,6 +26,12 @@

#define CLEAR_ICON_SIZE 16

// This is required because private implementation of
// QAbstractSpinBoxPrivate checks for specialText emptiness
// and skips specialText handling if it's empty
QString QgsSpinBox::SPECIAL_TEXT_WHEN_EMPTY = QChar( 0x2063 );


QgsSpinBox::QgsSpinBox( QWidget *parent )
: QSpinBox( parent )
{
@@ -137,6 +143,14 @@ void QgsSpinBox::setLineEditAlignment( Qt::Alignment alignment )
mLineEdit->setAlignment( alignment );
}

void QgsSpinBox::setSpecialValueText( const QString &txt )
{
if ( txt.isEmpty() )
QSpinBox::setSpecialValueText( SPECIAL_TEXT_WHEN_EMPTY );
else
QSpinBox::setSpecialValueText( txt );
}

int QgsSpinBox::valueFromText( const QString &text ) const
{
if ( !mExpressionsEnabled )
@@ -185,6 +199,9 @@ QString QgsSpinBox::stripped( const QString &originalText ) const
QString text = originalText;
if ( specialValueText().isEmpty() || text != specialValueText() )
{
// Strip SPECIAL_TEXT_WHEN_EMPTY
if ( text.contains( SPECIAL_TEXT_WHEN_EMPTY ) )
text = text.replace( SPECIAL_TEXT_WHEN_EMPTY, QString() );
int from = 0;
int size = text.size();
bool changed = false;
@@ -132,6 +132,13 @@ class GUI_EXPORT QgsSpinBox : public QSpinBox
*/
void setLineEditAlignment( Qt::Alignment alignment );

/**
* Set the special-value text to be \a txt
* If set, the spin box will display this text instead of a numeric value whenever the current value
* is equal to minimum(). Typical use is to indicate that this choice has a special (default) meaning.
*/
void setSpecialValueText( const QString &txt );

int valueFromText( const QString &text ) const override;
QValidator::State validate( QString &input, int &pos ) const override;

@@ -156,7 +163,15 @@ class GUI_EXPORT QgsSpinBox : public QSpinBox

bool mExpressionsEnabled = true;

// This is required because private implementation of
// QAbstractSpinBoxPrivate checks for specialText emptiness
// and skips specialText handling if it's empty
static QString SPECIAL_TEXT_WHEN_EMPTY;

QString stripped( const QString &originalText ) const;

friend class TestQgsRangeWidgetWrapper;

};

#endif // QGSSPINBOX_H

0 comments on commit d179635

Please sign in to comment.
You can’t perform that action at this time.