From d71453d84ad6821bfa6aa8f745de9dac2eb0b2ba Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Thu, 8 Sep 2016 08:11:32 +1000 Subject: [PATCH] Add ability to set default values for QgsFilterLineEdit and have clearing the widget reset to default rather than null --- python/gui/qgsfilterlineedit.sip | 36 ++++++++++++++++++ src/gui/qgsfilterlineedit.cpp | 27 +++++++++++++- src/gui/qgsfilterlineedit.h | 43 ++++++++++++++++++++++ tests/src/python/test_qgsfilterlineedit.py | 27 +++++++++++++- 4 files changed, 130 insertions(+), 3 deletions(-) diff --git a/python/gui/qgsfilterlineedit.sip b/python/gui/qgsfilterlineedit.sip index 870921c2aff..6e23df7cd39 100644 --- a/python/gui/qgsfilterlineedit.sip +++ b/python/gui/qgsfilterlineedit.sip @@ -14,12 +14,31 @@ class QgsFilterLineEdit : QLineEdit %End public: + //! Behaviour when clearing value of widget + enum ClearMode + { + ClearToNull, //!< Reset value to null + ClearToDefault, //!< Reset value to default value (see defaultValue() ) + }; + /** Constructor for QgsFilterLineEdit. * @param parent parent widget * @param nullValue string for representing null values */ QgsFilterLineEdit( QWidget* parent /TransferThis/ = 0, const QString& nullValue = QString::null ); + /** Returns the clear mode for the widget. The clear mode defines the behaviour of the + * widget when its value is cleared. This defaults to ClearToNull. + * @see setClearMode() + */ + ClearMode clearMode() const; + + /** Sets the clear mode for the widget. The clear mode defines the behaviour of the + * widget when its value is cleared. This defaults to ClearToNull. + * @see clearMode() + */ + void setClearMode( ClearMode mode ); + /** Sets the string representation for null values in the widget. This does not * affect the values returned for null values by value(), rather it only affects * the text that is shown to users when the widget's value is null. @@ -34,6 +53,23 @@ class QgsFilterLineEdit : QLineEdit */ QString nullValue() const; + /** Sets the default value for the widget. The default value is a value + * which the widget will be reset to if it is cleared and the clearMode() + * is equal to ClearToDefault. + * @param defaultValue default value + * @see defaultValue() + * @see clearMode() + */ + void setDefaultValue( const QString& defaultValue ); + + /** Returns the default value for the widget. The default value is a value + * which the widget will be reset to if it is cleared and the clearMode() + * is equal to ClearToDefault. + * @see setDefaultValue() + * @see clearMode() + */ + QString defaultValue() const; + /** * Sets the current text for the widget with support for handling null values. * diff --git a/src/gui/qgsfilterlineedit.cpp b/src/gui/qgsfilterlineedit.cpp index 5a6fc8306c6..4de612f7584 100644 --- a/src/gui/qgsfilterlineedit.cpp +++ b/src/gui/qgsfilterlineedit.cpp @@ -25,6 +25,7 @@ QgsFilterLineEdit::QgsFilterLineEdit( QWidget* parent, const QString& nullValue ) : QLineEdit( parent ) + , mClearMode( ClearToNull ) , mNullValue( nullValue ) , mFocusInEvent( false ) , mClearHover( false ) @@ -87,7 +88,18 @@ void QgsFilterLineEdit::focusInEvent( QFocusEvent* e ) void QgsFilterLineEdit::clearValue() { - setText( mNullValue ); + switch ( mClearMode ) + { + case ClearToNull: + setText( mNullValue ); + break; + + case ClearToDefault: + setText( mDefaultValue ); + break; + + } + setModified( true ); emit cleared(); } @@ -133,7 +145,18 @@ void QgsFilterLineEdit::onTextChanged( const QString &text ) bool QgsFilterLineEdit::shouldShowClear() const { - return isEnabled() && !isReadOnly() && !isNull(); + if ( !isEnabled() || isReadOnly() ) + return false; + + switch ( mClearMode ) + { + case ClearToNull: + return !isNull(); + + case ClearToDefault: + return value() != mDefaultValue; + } + return false; //avoid warnings } QRect QgsFilterLineEdit::clearRect() const diff --git a/src/gui/qgsfilterlineedit.h b/src/gui/qgsfilterlineedit.h index 6cbf19b09fa..466b7a2539a 100644 --- a/src/gui/qgsfilterlineedit.h +++ b/src/gui/qgsfilterlineedit.h @@ -34,17 +34,39 @@ class QToolButton; class GUI_EXPORT QgsFilterLineEdit : public QLineEdit { Q_OBJECT + Q_ENUMS( ClearMode ) + Q_PROPERTY( ClearMode clearMode READ clearMode WRITE setClearMode ) Q_PROPERTY( QString nullValue READ nullValue WRITE setNullValue ) + Q_PROPERTY( QString defaultValue READ defaultValue WRITE setDefaultValue ) Q_PROPERTY( QString value READ value WRITE setValue NOTIFY valueChanged ) public: + //! Behaviour when clearing value of widget + enum ClearMode + { + ClearToNull = 0, //!< Reset value to null + ClearToDefault, //!< Reset value to default value (see defaultValue() ) + }; + /** Constructor for QgsFilterLineEdit. * @param parent parent widget * @param nullValue string for representing null values */ QgsFilterLineEdit( QWidget* parent = nullptr, const QString& nullValue = QString::null ); + /** Returns the clear mode for the widget. The clear mode defines the behaviour of the + * widget when its value is cleared. This defaults to ClearToNull. + * @see setClearMode() + */ + ClearMode clearMode() const { return mClearMode; } + + /** Sets the clear mode for the widget. The clear mode defines the behaviour of the + * widget when its value is cleared. This defaults to ClearToNull. + * @see clearMode() + */ + void setClearMode( ClearMode mode ) { mClearMode = mode; } + /** Sets the string representation for null values in the widget. This does not * affect the values returned for null values by value(), rather it only affects * the text that is shown to users when the widget's value is null. @@ -59,6 +81,23 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit */ QString nullValue() const { return mNullValue; } + /** Sets the default value for the widget. The default value is a value + * which the widget will be reset to if it is cleared and the clearMode() + * is equal to ClearToDefault. + * @param defaultValue default value + * @see defaultValue() + * @see clearMode() + */ + void setDefaultValue( const QString& defaultValue ) { mDefaultValue = defaultValue; } + + /** Returns the default value for the widget. The default value is a value + * which the widget will be reset to if it is cleared and the clearMode() + * is equal to ClearToDefault. + * @see setDefaultValue() + * @see clearMode() + */ + QString defaultValue() const { return mDefaultValue; } + /** * Sets the current text for the widget with support for handling null values. * @@ -118,7 +157,11 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit void onTextChanged( const QString &text ); private: + + ClearMode mClearMode; + QString mNullValue; + QString mDefaultValue; QString mStyleSheet; bool mFocusInEvent; bool mClearHover; diff --git a/tests/src/python/test_qgsfilterlineedit.py b/tests/src/python/test_qgsfilterlineedit.py index 2046cfb510f..e32f7341281 100644 --- a/tests/src/python/test_qgsfilterlineedit.py +++ b/tests/src/python/test_qgsfilterlineedit.py @@ -38,6 +38,10 @@ class TestQgsFilterLineEdit(unittest.TestCase): w.setValue('value') self.assertEqual(w.value(), 'value') self.assertEqual(w.text(), 'value') + w.setDefaultValue('default') + self.assertEqual(w.defaultValue(), 'default') + w.setClearMode(QgsFilterLineEdit.ClearToDefault) + self.assertEqual(w.clearMode(), QgsFilterLineEdit.ClearToDefault) def testNullValueHandling(self): """ test widget handling of null values """ @@ -70,7 +74,7 @@ class TestQgsFilterLineEdit(unittest.TestCase): self.assertTrue(w.isNull()) self.assertFalse(w.value()) - def testClear(self): + def testClearToNull(self): """ test clearing widget """ w = qgis.gui.QgsFilterLineEdit() @@ -90,6 +94,27 @@ class TestQgsFilterLineEdit(unittest.TestCase): self.assertTrue(w.isNull()) self.assertFalse(w.value()) + def testClearToDefault(self): + # test clearing to default value + w = qgis.gui.QgsFilterLineEdit() + w.setClearMode(QgsFilterLineEdit.ClearToDefault) + + w.setValue('abc') + w.clearValue() + self.assertTrue(w.isNull()) + self.assertFalse(w.value()) + w.clearValue() + self.assertTrue(w.isNull()) + self.assertFalse(w.value()) + + w.setDefaultValue('def') + w.setValue('abc') + self.assertFalse(w.isNull()) + w.clearValue() + self.assertEqual(w.value(), 'def') + self.assertEqual(w.text(), 'def') + self.assertFalse(w.isNull()) + @unittest.skipIf(not use_signal_spy, "No QSignalSpy available") def test_ChangedSignals(self): """ test that signals are correctly emitted when clearing"""