From 8350fb4a261dffaa08c8a32978dedd12b4a9f893 Mon Sep 17 00:00:00 2001 From: signedav Date: Thu, 8 Nov 2018 11:20:45 +0100 Subject: [PATCH 1/6] unused variable removed --- src/gui/qgsfilterlineedit.cpp | 1 - src/gui/qgsfilterlineedit.h | 1 - 2 files changed, 2 deletions(-) diff --git a/src/gui/qgsfilterlineedit.cpp b/src/gui/qgsfilterlineedit.cpp index a30e2ae95a2..a65b1d144c6 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; } } diff --git a/src/gui/qgsfilterlineedit.h b/src/gui/qgsfilterlineedit.h index 6cf22fcad3b..14a5a5efea8 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; From 6da99a1866cfbb9c16aab7d8b92260b135432e60 Mon Sep 17 00:00:00 2001 From: signedav Date: Thu, 8 Nov 2018 17:16:29 +0100 Subject: [PATCH 2/6] pass SpecialValueText as NullValue --- src/gui/editorwidgets/qgsdoublespinbox.cpp | 6 ++++++ src/gui/editorwidgets/qgsspinbox.cpp | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/src/gui/editorwidgets/qgsdoublespinbox.cpp b/src/gui/editorwidgets/qgsdoublespinbox.cpp index a8821d4887f..b493eacae95 100644 --- a/src/gui/editorwidgets/qgsdoublespinbox.cpp +++ b/src/gui/editorwidgets/qgsdoublespinbox.cpp @@ -155,9 +155,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..8f5b9348c74 100644 --- a/src/gui/editorwidgets/qgsspinbox.cpp +++ b/src/gui/editorwidgets/qgsspinbox.cpp @@ -152,9 +152,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 From a02fb082908a299bbe7dd472fcdc35343b8f176f Mon Sep 17 00:00:00 2001 From: signedav Date: Thu, 8 Nov 2018 17:37:34 +0100 Subject: [PATCH 3/6] Remove the null representator text on getting focus of `QgsSpinBoxLineEdit` This fixes #20153 --- src/gui/qgsfilterlineedit.cpp | 19 +++++++++++++++++++ src/gui/qgsfilterlineedit.h | 8 ++++++++ 2 files changed, 27 insertions(+) diff --git a/src/gui/qgsfilterlineedit.cpp b/src/gui/qgsfilterlineedit.cpp index a65b1d144c6..541731ed86c 100644 --- a/src/gui/qgsfilterlineedit.cpp +++ b/src/gui/qgsfilterlineedit.cpp @@ -212,3 +212,22 @@ bool QgsFilterLineEdit::event( QEvent *event ) return QLineEdit::event( event );; } + +void QgsSpinBoxLineEdit::focusInEvent( QFocusEvent *e ) +{ + QLineEdit::focusInEvent( e ); + if ( e->reason() == Qt::MouseFocusReason && ( isNull() ) ) + { + mWaitingForMouseRelease = true; + } +} + +void QgsSpinBoxLineEdit::mouseReleaseEvent( QMouseEvent *e ) +{ + QLineEdit::mouseReleaseEvent( e ); + if ( mWaitingForMouseRelease ) + { + mWaitingForMouseRelease = false; + clear(); + } +} diff --git a/src/gui/qgsfilterlineedit.h b/src/gui/qgsfilterlineedit.h index 14a5a5efea8..8a19c58eff6 100644 --- a/src/gui/qgsfilterlineedit.h +++ b/src/gui/qgsfilterlineedit.h @@ -324,6 +324,14 @@ class SIP_SKIP QgsSpinBoxLineEdit : public QgsFilterLineEdit setModified( true ); emit cleared(); } + + protected: + void focusInEvent( QFocusEvent *e ) override; + void mouseReleaseEvent( QMouseEvent *e ) override; + + private: + bool mWaitingForMouseRelease = false; + }; /// @endcond From f44af04b5f90acd8eac78904cbdb0fcb5f0c8252 Mon Sep 17 00:00:00 2001 From: signedav Date: Thu, 8 Nov 2018 17:51:41 +0100 Subject: [PATCH 4/6] clear value on clear in case the value is the null representator (null) --- src/gui/editorwidgets/qgsdoublespinbox.cpp | 2 ++ src/gui/editorwidgets/qgsspinbox.cpp | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/gui/editorwidgets/qgsdoublespinbox.cpp b/src/gui/editorwidgets/qgsdoublespinbox.cpp index b493eacae95..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 ) diff --git a/src/gui/editorwidgets/qgsspinbox.cpp b/src/gui/editorwidgets/qgsspinbox.cpp index 8f5b9348c74..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 ) From f96078d0812ea56b6c622a3a1f41c32a71c561df Mon Sep 17 00:00:00 2001 From: signedav Date: Mon, 12 Nov 2018 16:03:16 +0100 Subject: [PATCH 5/6] remove mouserelesae event because it's not needet. on focus it should clear. cond / endcond --- src/gui/qgsfilterlineedit.cpp | 14 +++----------- src/gui/qgsfilterlineedit.h | 5 ----- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/src/gui/qgsfilterlineedit.cpp b/src/gui/qgsfilterlineedit.cpp index 541731ed86c..113bc6c50ae 100644 --- a/src/gui/qgsfilterlineedit.cpp +++ b/src/gui/qgsfilterlineedit.cpp @@ -213,21 +213,13 @@ bool QgsFilterLineEdit::event( QEvent *event ) return QLineEdit::event( event );; } +/// @cond PRIVATE void QgsSpinBoxLineEdit::focusInEvent( QFocusEvent *e ) { QLineEdit::focusInEvent( e ); - if ( e->reason() == Qt::MouseFocusReason && ( isNull() ) ) + if ( isNull() ) { - mWaitingForMouseRelease = true; - } -} - -void QgsSpinBoxLineEdit::mouseReleaseEvent( QMouseEvent *e ) -{ - QLineEdit::mouseReleaseEvent( e ); - if ( mWaitingForMouseRelease ) - { - mWaitingForMouseRelease = false; clear(); } } +/// @endcond diff --git a/src/gui/qgsfilterlineedit.h b/src/gui/qgsfilterlineedit.h index 8a19c58eff6..0d1cebf5333 100644 --- a/src/gui/qgsfilterlineedit.h +++ b/src/gui/qgsfilterlineedit.h @@ -327,11 +327,6 @@ class SIP_SKIP QgsSpinBoxLineEdit : public QgsFilterLineEdit protected: void focusInEvent( QFocusEvent *e ) override; - void mouseReleaseEvent( QMouseEvent *e ) override; - - private: - bool mWaitingForMouseRelease = false; - }; /// @endcond From c8fc7a2419f6cdf1101b0e3adb30344d7625857d Mon Sep 17 00:00:00 2001 From: signedav Date: Wed, 14 Nov 2018 12:43:26 +0100 Subject: [PATCH 6/6] tests for focus on null field --- tests/src/gui/testqgsrangewidgetwrapper.cpp | 58 +++++++++++++++++++++ 1 file changed, 58 insertions(+) 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"