Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #8442 from signedav/textfield_null
Range widget: Remove null representator during editing
  • Loading branch information
m-kuhn committed Nov 16, 2018
2 parents 30692fa + c8fc7a2 commit 438a1da
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/gui/editorwidgets/qgsdoublespinbox.cpp
Expand Up @@ -107,6 +107,8 @@ void QgsDoubleSpinBox::changed( double value )
void QgsDoubleSpinBox::clear()
{
setValue( clearValue() );
if ( mLineEdit->isNull() )
mLineEdit->clear();
}

void QgsDoubleSpinBox::setClearValue( double customValue, const QString &specialValueText )
Expand Down Expand Up @@ -155,9 +157,15 @@ void QgsDoubleSpinBox::setLineEditAlignment( Qt::Alignment alignment )
void QgsDoubleSpinBox::setSpecialValueText( const QString &txt )
{
if ( txt.isEmpty() )
{
QDoubleSpinBox::setSpecialValueText( SPECIAL_TEXT_WHEN_EMPTY );
mLineEdit->setNullValue( SPECIAL_TEXT_WHEN_EMPTY );
}
else
{
QDoubleSpinBox::setSpecialValueText( txt );
mLineEdit->setNullValue( SPECIAL_TEXT_WHEN_EMPTY );
}
}

QString QgsDoubleSpinBox::stripped( const QString &originalText ) const
Expand Down
8 changes: 8 additions & 0 deletions src/gui/editorwidgets/qgsspinbox.cpp
Expand Up @@ -104,6 +104,8 @@ void QgsSpinBox::changed( int value )
void QgsSpinBox::clear()
{
setValue( clearValue() );
if ( mLineEdit->isNull() )
mLineEdit->clear();
}

void QgsSpinBox::setClearValue( int customValue, const QString &specialValueText )
Expand Down Expand Up @@ -152,9 +154,15 @@ void QgsSpinBox::setLineEditAlignment( Qt::Alignment alignment )
void QgsSpinBox::setSpecialValueText( const QString &txt )
{
if ( txt.isEmpty() )
{
QSpinBox::setSpecialValueText( SPECIAL_TEXT_WHEN_EMPTY );
mLineEdit->setNullValue( SPECIAL_TEXT_WHEN_EMPTY );
}
else
{
QSpinBox::setSpecialValueText( txt );
mLineEdit->setNullValue( txt );
}
}

int QgsSpinBox::valueFromText( const QString &text ) const
Expand Down
12 changes: 11 additions & 1 deletion src/gui/qgsfilterlineedit.cpp
Expand Up @@ -89,7 +89,6 @@ void QgsFilterLineEdit::focusInEvent( QFocusEvent *e )
QLineEdit::focusInEvent( e );
if ( e->reason() == Qt::MouseFocusReason && ( isNull() || mSelectOnFocus ) )
{
mFocusInEvent = true;
mWaitingForMouseRelease = true;
}
}
Expand Down Expand Up @@ -213,3 +212,14 @@ bool QgsFilterLineEdit::event( QEvent *event )

return QLineEdit::event( event );;
}

/// @cond PRIVATE
void QgsSpinBoxLineEdit::focusInEvent( QFocusEvent *e )
{
QLineEdit::focusInEvent( e );
if ( isNull() )
{
clear();
}
}
/// @endcond
4 changes: 3 additions & 1 deletion src/gui/qgsfilterlineedit.h
Expand Up @@ -286,7 +286,6 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
QString mNullValue;
QString mDefaultValue;
QString mStyleSheet;
bool mFocusInEvent = false;
bool mWaitingForMouseRelease = false;
bool mSelectOnFocus = false;

Expand Down Expand Up @@ -325,6 +324,9 @@ class SIP_SKIP QgsSpinBoxLineEdit : public QgsFilterLineEdit
setModified( true );
emit cleared();
}

protected:
void focusInEvent( QFocusEvent *e ) override;
};
/// @endcond

Expand Down
58 changes: 58 additions & 0 deletions tests/src/gui/testqgsrangewidgetwrapper.cpp
Expand Up @@ -51,6 +51,7 @@ class TestQgsRangeWidgetWrapper : public QObject
void test_setDoubleSmallerRange();
void test_setDoubleLimits();
void test_nulls();
void test_focus();

private:
std::unique_ptr<QgsRangeWidgetWrapper> widget0; // For field 0
Expand Down Expand Up @@ -328,7 +329,64 @@ void TestQgsRangeWidgetWrapper::test_nulls()

}

void TestQgsRangeWidgetWrapper::test_focus()
{
QgsApplication::setNullRepresentation( QString( "nope" ) );

QWidget *w = new QWidget(); //required for focus events
QApplication::setActiveWindow( w );

QVariantMap cfg;
cfg.insert( QStringLiteral( "AllowNull" ), true );

widget1->setConfig( cfg );
QgsDoubleSpinBox *editor1 = qobject_cast<QgsDoubleSpinBox *>( widget1->createWidget( w ) );
QVERIFY( editor1 );
widget1->initWidget( editor1 );

widget2->setConfig( cfg );
QgsDoubleSpinBox *editor2 = qobject_cast<QgsDoubleSpinBox *>( widget2->createWidget( w ) );
QVERIFY( editor2 );
widget2->initWidget( editor2 );

editor1->mLineEdit->setNullValue( QgsApplication::nullRepresentation() );
editor2->mLineEdit->setNullValue( QgsApplication::nullRepresentation() );

QVERIFY( editor1->mLineEdit->isNull() );
QVERIFY( editor2->mLineEdit->isNull() );
QVERIFY( !editor1->mLineEdit->hasFocus() );
QVERIFY( !editor2->mLineEdit->hasFocus() );
QCOMPARE( editor1->mLineEdit->text(), QStringLiteral( "nope" ) );
QCOMPARE( editor2->mLineEdit->text(), QStringLiteral( "nope" ) );

editor1->mLineEdit->setFocus();
QVERIFY( editor1->mLineEdit->hasFocus() );
QVERIFY( !editor2->mLineEdit->hasFocus() );
QCOMPARE( editor1->mLineEdit->text(), QStringLiteral( "" ) );
QCOMPARE( editor2->mLineEdit->text(), QStringLiteral( "nope" ) );

editor2->mLineEdit->setFocus();
QVERIFY( !editor1->mLineEdit->hasFocus() );
QVERIFY( editor2->mLineEdit->hasFocus() );
QCOMPARE( editor1->mLineEdit->text(), QStringLiteral( "nope" ) );
QCOMPARE( editor2->mLineEdit->text(), QStringLiteral( "" ) );

editor1->mLineEdit->setFocus();
editor1->mLineEdit->setText( QString( "151.000000000" ) );
QVERIFY( !editor1->mLineEdit->isNull() );
QVERIFY( editor2->mLineEdit->isNull() );
QVERIFY( editor1->mLineEdit->hasFocus() );
QVERIFY( !editor2->mLineEdit->hasFocus() );
QCOMPARE( editor1->mLineEdit->text(), QStringLiteral( "151.000000000" ) );
QCOMPARE( editor2->mLineEdit->text(), QStringLiteral( "nope" ) );

editor2->mLineEdit->setFocus();
QVERIFY( !editor1->mLineEdit->hasFocus() );
QVERIFY( editor2->mLineEdit->hasFocus() );
QCOMPARE( editor1->mLineEdit->text(), QStringLiteral( "151.000000000" ) );
QCOMPARE( editor2->mLineEdit->text(), QStringLiteral( "" ) );

}

QGSTEST_MAIN( TestQgsRangeWidgetWrapper )
#include "testqgsrangewidgetwrapper.moc"

0 comments on commit 438a1da

Please sign in to comment.