Skip to content

Commit 5ee1b16

Browse files
committedSep 8, 2016
Add ability to set default values for QgsFilterLineEdit
and have clearing the widget reset to default rather than null (cherry-picked from d71453d)
1 parent 109c003 commit 5ee1b16

File tree

4 files changed

+130
-3
lines changed

4 files changed

+130
-3
lines changed
 

‎python/gui/qgsfilterlineedit.sip

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,31 @@ class QgsFilterLineEdit : QLineEdit
1414
%End
1515
public:
1616

17+
//! Behaviour when clearing value of widget
18+
enum ClearMode
19+
{
20+
ClearToNull, //!< Reset value to null
21+
ClearToDefault, //!< Reset value to default value (see defaultValue() )
22+
};
23+
1724
/** Constructor for QgsFilterLineEdit.
1825
* @param parent parent widget
1926
* @param nullValue string for representing null values
2027
*/
2128
QgsFilterLineEdit( QWidget* parent /TransferThis/ = 0, const QString& nullValue = QString::null );
2229

30+
/** Returns the clear mode for the widget. The clear mode defines the behaviour of the
31+
* widget when its value is cleared. This defaults to ClearToNull.
32+
* @see setClearMode()
33+
*/
34+
ClearMode clearMode() const;
35+
36+
/** Sets the clear mode for the widget. The clear mode defines the behaviour of the
37+
* widget when its value is cleared. This defaults to ClearToNull.
38+
* @see clearMode()
39+
*/
40+
void setClearMode( ClearMode mode );
41+
2342
/** Sets the string representation for null values in the widget. This does not
2443
* affect the values returned for null values by value(), rather it only affects
2544
* the text that is shown to users when the widget's value is null.
@@ -34,6 +53,23 @@ class QgsFilterLineEdit : QLineEdit
3453
*/
3554
QString nullValue() const;
3655

56+
/** Sets the default value for the widget. The default value is a value
57+
* which the widget will be reset to if it is cleared and the clearMode()
58+
* is equal to ClearToDefault.
59+
* @param defaultValue default value
60+
* @see defaultValue()
61+
* @see clearMode()
62+
*/
63+
void setDefaultValue( const QString& defaultValue );
64+
65+
/** Returns the default value for the widget. The default value is a value
66+
* which the widget will be reset to if it is cleared and the clearMode()
67+
* is equal to ClearToDefault.
68+
* @see setDefaultValue()
69+
* @see clearMode()
70+
*/
71+
QString defaultValue() const;
72+
3773
/**
3874
* Sets the current text for the widget with support for handling null values.
3975
*

‎src/gui/qgsfilterlineedit.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
QgsFilterLineEdit::QgsFilterLineEdit( QWidget* parent, const QString& nullValue )
2727
: QLineEdit( parent )
28+
, mClearMode( ClearToNull )
2829
, mNullValue( nullValue )
2930
, mFocusInEvent( false )
3031
, mClearHover( false )
@@ -87,7 +88,18 @@ void QgsFilterLineEdit::focusInEvent( QFocusEvent* e )
8788

8889
void QgsFilterLineEdit::clearValue()
8990
{
90-
setText( mNullValue );
91+
switch ( mClearMode )
92+
{
93+
case ClearToNull:
94+
setText( mNullValue );
95+
break;
96+
97+
case ClearToDefault:
98+
setText( mDefaultValue );
99+
break;
100+
101+
}
102+
91103
setModified( true );
92104
emit cleared();
93105
}
@@ -133,7 +145,18 @@ void QgsFilterLineEdit::onTextChanged( const QString &text )
133145

134146
bool QgsFilterLineEdit::shouldShowClear() const
135147
{
136-
return isEnabled() && !isReadOnly() && !isNull();
148+
if ( !isEnabled() || isReadOnly() )
149+
return false;
150+
151+
switch ( mClearMode )
152+
{
153+
case ClearToNull:
154+
return !isNull();
155+
156+
case ClearToDefault:
157+
return value() != mDefaultValue;
158+
}
159+
return false; //avoid warnings
137160
}
138161

139162
QRect QgsFilterLineEdit::clearRect() const

‎src/gui/qgsfilterlineedit.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,39 @@ class QToolButton;
3434
class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
3535
{
3636
Q_OBJECT
37+
Q_ENUMS( ClearMode )
38+
Q_PROPERTY( ClearMode clearMode READ clearMode WRITE setClearMode )
3739
Q_PROPERTY( QString nullValue READ nullValue WRITE setNullValue )
40+
Q_PROPERTY( QString defaultValue READ defaultValue WRITE setDefaultValue )
3841
Q_PROPERTY( QString value READ value WRITE setValue NOTIFY valueChanged )
3942

4043
public:
4144

45+
//! Behaviour when clearing value of widget
46+
enum ClearMode
47+
{
48+
ClearToNull = 0, //!< Reset value to null
49+
ClearToDefault, //!< Reset value to default value (see defaultValue() )
50+
};
51+
4252
/** Constructor for QgsFilterLineEdit.
4353
* @param parent parent widget
4454
* @param nullValue string for representing null values
4555
*/
4656
QgsFilterLineEdit( QWidget* parent = nullptr, const QString& nullValue = QString::null );
4757

58+
/** Returns the clear mode for the widget. The clear mode defines the behaviour of the
59+
* widget when its value is cleared. This defaults to ClearToNull.
60+
* @see setClearMode()
61+
*/
62+
ClearMode clearMode() const { return mClearMode; }
63+
64+
/** Sets the clear mode for the widget. The clear mode defines the behaviour of the
65+
* widget when its value is cleared. This defaults to ClearToNull.
66+
* @see clearMode()
67+
*/
68+
void setClearMode( ClearMode mode ) { mClearMode = mode; }
69+
4870
/** Sets the string representation for null values in the widget. This does not
4971
* affect the values returned for null values by value(), rather it only affects
5072
* the text that is shown to users when the widget's value is null.
@@ -59,6 +81,23 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
5981
*/
6082
QString nullValue() const { return mNullValue; }
6183

84+
/** Sets the default value for the widget. The default value is a value
85+
* which the widget will be reset to if it is cleared and the clearMode()
86+
* is equal to ClearToDefault.
87+
* @param defaultValue default value
88+
* @see defaultValue()
89+
* @see clearMode()
90+
*/
91+
void setDefaultValue( const QString& defaultValue ) { mDefaultValue = defaultValue; }
92+
93+
/** Returns the default value for the widget. The default value is a value
94+
* which the widget will be reset to if it is cleared and the clearMode()
95+
* is equal to ClearToDefault.
96+
* @see setDefaultValue()
97+
* @see clearMode()
98+
*/
99+
QString defaultValue() const { return mDefaultValue; }
100+
62101
/**
63102
* Sets the current text for the widget with support for handling null values.
64103
*
@@ -118,7 +157,11 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
118157
void onTextChanged( const QString &text );
119158

120159
private:
160+
161+
ClearMode mClearMode;
162+
121163
QString mNullValue;
164+
QString mDefaultValue;
122165
QString mStyleSheet;
123166
bool mFocusInEvent;
124167
bool mClearHover;

‎tests/src/python/test_qgsfilterlineedit.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ def testGettersSetters(self):
3838
w.setValue('value')
3939
self.assertEqual(w.value(), 'value')
4040
self.assertEqual(w.text(), 'value')
41+
w.setDefaultValue('default')
42+
self.assertEqual(w.defaultValue(), 'default')
43+
w.setClearMode(QgsFilterLineEdit.ClearToDefault)
44+
self.assertEqual(w.clearMode(), QgsFilterLineEdit.ClearToDefault)
4145

4246
def testNullValueHandling(self):
4347
""" test widget handling of null values """
@@ -70,7 +74,7 @@ def testNullValueHandling(self):
7074
self.assertTrue(w.isNull())
7175
self.assertFalse(w.value())
7276

73-
def testClear(self):
77+
def testClearToNull(self):
7478
""" test clearing widget """
7579
w = qgis.gui.QgsFilterLineEdit()
7680

@@ -90,6 +94,27 @@ def testClear(self):
9094
self.assertTrue(w.isNull())
9195
self.assertFalse(w.value())
9296

97+
def testClearToDefault(self):
98+
# test clearing to default value
99+
w = qgis.gui.QgsFilterLineEdit()
100+
w.setClearMode(QgsFilterLineEdit.ClearToDefault)
101+
102+
w.setValue('abc')
103+
w.clearValue()
104+
self.assertTrue(w.isNull())
105+
self.assertFalse(w.value())
106+
w.clearValue()
107+
self.assertTrue(w.isNull())
108+
self.assertFalse(w.value())
109+
110+
w.setDefaultValue('def')
111+
w.setValue('abc')
112+
self.assertFalse(w.isNull())
113+
w.clearValue()
114+
self.assertEqual(w.value(), 'def')
115+
self.assertEqual(w.text(), 'def')
116+
self.assertFalse(w.isNull())
117+
93118
@unittest.skipIf(not use_signal_spy, "No QSignalSpy available")
94119
def test_ChangedSignals(self):
95120
""" test that signals are correctly emitted when clearing"""

0 commit comments

Comments
 (0)
Please sign in to comment.