diff --git a/src/gui/editorwidgets/qgsdoublespinbox.cpp b/src/gui/editorwidgets/qgsdoublespinbox.cpp index a8821d4887f..e7c875c0569 100644 --- a/src/gui/editorwidgets/qgsdoublespinbox.cpp +++ b/src/gui/editorwidgets/qgsdoublespinbox.cpp @@ -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 ) @@ -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 diff --git a/src/gui/editorwidgets/qgsspinbox.cpp b/src/gui/editorwidgets/qgsspinbox.cpp index fc3a532a60b..d2f22af4cd6 100644 --- a/src/gui/editorwidgets/qgsspinbox.cpp +++ b/src/gui/editorwidgets/qgsspinbox.cpp @@ -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 ) @@ -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 diff --git a/src/gui/qgsfilterlineedit.cpp b/src/gui/qgsfilterlineedit.cpp index a30e2ae95a2..113bc6c50ae 100644 --- a/src/gui/qgsfilterlineedit.cpp +++ b/src/gui/qgsfilterlineedit.cpp @@ -89,7 +89,6 @@ void QgsFilterLineEdit::focusInEvent( QFocusEvent *e ) QLineEdit::focusInEvent( e ); if ( e->reason() == Qt::MouseFocusReason && ( isNull() || mSelectOnFocus ) ) { - mFocusInEvent = true; mWaitingForMouseRelease = true; } } @@ -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 diff --git a/src/gui/qgsfilterlineedit.h b/src/gui/qgsfilterlineedit.h index 6cf22fcad3b..0d1cebf5333 100644 --- a/src/gui/qgsfilterlineedit.h +++ b/src/gui/qgsfilterlineedit.h @@ -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; @@ -325,6 +324,9 @@ class SIP_SKIP QgsSpinBoxLineEdit : public QgsFilterLineEdit setModified( true ); emit cleared(); } + + protected: + void focusInEvent( QFocusEvent *e ) override; }; /// @endcond diff --git a/tests/src/gui/testqgsrangewidgetwrapper.cpp b/tests/src/gui/testqgsrangewidgetwrapper.cpp index e38f65bef67..a8bcd3a5fc2 100644 --- a/tests/src/gui/testqgsrangewidgetwrapper.cpp +++ b/tests/src/gui/testqgsrangewidgetwrapper.cpp @@ -51,6 +51,7 @@ class TestQgsRangeWidgetWrapper : public QObject void test_setDoubleSmallerRange(); void test_setDoubleLimits(); void test_nulls(); + void test_focus(); private: std::unique_ptr widget0; // For field 0 @@ -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( widget1->createWidget( w ) ); + QVERIFY( editor1 ); + widget1->initWidget( editor1 ); + + widget2->setConfig( cfg ); + QgsDoubleSpinBox *editor2 = qobject_cast( 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"