Add ability to set default values for QgsFilterLineEdit

and have clearing the widget reset to default rather than
null
This commit is contained in:
Nyall Dawson 2016-09-08 08:11:32 +10:00
parent edcc247c3e
commit d71453d84a
4 changed files with 130 additions and 3 deletions

View File

@ -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.
*

View File

@ -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

View File

@ -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;

View File

@ -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"""