Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow hiding clear button in QgsFilterLineEdit
  • Loading branch information
nyalldawson committed Sep 8, 2016
1 parent d71453d commit d959384
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
18 changes: 18 additions & 0 deletions python/gui/qgsfilterlineedit.sip
Expand Up @@ -27,15 +27,30 @@ class QgsFilterLineEdit : QLineEdit
*/
QgsFilterLineEdit( QWidget* parent /TransferThis/ = 0, const QString& nullValue = QString::null );

/** Returns true if the widget's clear button is visible.
* @see setShowClearButton()
* @note added in QGIS 3.0
*/
bool showClearButton() const;

/** Sets whether the widget's clear button is visible.
* @param visible set to false to hide the clear button
* @see showClearButton()
* @note added in QGIS 3.0
*/
void setShowClearButton( bool visible );

/** 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()
* @note added in QGIS 3.0
*/
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()
* @note added in QGIS 3.0
*/
void setClearMode( ClearMode mode );

Expand All @@ -59,6 +74,7 @@ class QgsFilterLineEdit : QLineEdit
* @param defaultValue default value
* @see defaultValue()
* @see clearMode()
* @note added in QGIS 3.0
*/
void setDefaultValue( const QString& defaultValue );

Expand All @@ -67,6 +83,7 @@ class QgsFilterLineEdit : QLineEdit
* is equal to ClearToDefault.
* @see setDefaultValue()
* @see clearMode()
* @note added in QGIS 3.0
*/
QString defaultValue() const;

Expand Down Expand Up @@ -101,6 +118,7 @@ class QgsFilterLineEdit : QLineEdit

/** Clears the widget and resets it to the null value.
* @see nullValue()
* @note added in QGIS 3.0
*/
virtual void clearValue();

Expand Down
12 changes: 11 additions & 1 deletion src/gui/qgsfilterlineedit.cpp
Expand Up @@ -25,6 +25,7 @@

QgsFilterLineEdit::QgsFilterLineEdit( QWidget* parent, const QString& nullValue )
: QLineEdit( parent )
, mClearButtonVisible( true )
, mClearMode( ClearToNull )
, mNullValue( nullValue )
, mFocusInEvent( false )
Expand All @@ -43,6 +44,15 @@ QgsFilterLineEdit::QgsFilterLineEdit( QWidget* parent, const QString& nullValue
SLOT( onTextChanged( const QString& ) ) );
}

void QgsFilterLineEdit::setShowClearButton( bool visible )
{
mClearButtonVisible = visible;
if ( !visible )
mClearHover = false;

update();
}

void QgsFilterLineEdit::mousePressEvent( QMouseEvent* e )
{
if ( !mFocusInEvent )
Expand Down Expand Up @@ -145,7 +155,7 @@ void QgsFilterLineEdit::onTextChanged( const QString &text )

bool QgsFilterLineEdit::shouldShowClear() const
{
if ( !isEnabled() || isReadOnly() )
if ( !isEnabled() || isReadOnly() || !mClearButtonVisible )
return false;

switch ( mClearMode )
Expand Down
21 changes: 21 additions & 0 deletions src/gui/qgsfilterlineedit.h
Expand Up @@ -39,6 +39,7 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
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 )
Q_PROPERTY( bool showClearButton READ showClearButton WRITE setShowClearButton )

public:

Expand All @@ -55,15 +56,30 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
*/
QgsFilterLineEdit( QWidget* parent = nullptr, const QString& nullValue = QString::null );

/** Returns true if the widget's clear button is visible.
* @see setShowClearButton()
* @note added in QGIS 3.0
*/
bool showClearButton() const { return mClearButtonVisible; }

/** Sets whether the widget's clear button is visible.
* @param visible set to false to hide the clear button
* @see showClearButton()
* @note added in QGIS 3.0
*/
void setShowClearButton( bool visible );

/** 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()
* @note added in QGIS 3.0
*/
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()
* @note added in QGIS 3.0
*/
void setClearMode( ClearMode mode ) { mClearMode = mode; }

Expand All @@ -87,6 +103,7 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
* @param defaultValue default value
* @see defaultValue()
* @see clearMode()
* @note added in QGIS 3.0
*/
void setDefaultValue( const QString& defaultValue ) { mDefaultValue = defaultValue; }

Expand All @@ -95,6 +112,7 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
* is equal to ClearToDefault.
* @see setDefaultValue()
* @see clearMode()
* @note added in QGIS 3.0
*/
QString defaultValue() const { return mDefaultValue; }

Expand Down Expand Up @@ -129,6 +147,7 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit

/** Clears the widget and resets it to the null value.
* @see nullValue()
* @note added in QGIS 3.0
*/
virtual void clearValue();

Expand Down Expand Up @@ -158,6 +177,8 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit

private:

bool mClearButtonVisible;

ClearMode mClearMode;

QString mNullValue;
Expand Down
4 changes: 4 additions & 0 deletions tests/src/python/test_qgsfilterlineedit.py
Expand Up @@ -42,6 +42,10 @@ def testGettersSetters(self):
self.assertEqual(w.defaultValue(), 'default')
w.setClearMode(QgsFilterLineEdit.ClearToDefault)
self.assertEqual(w.clearMode(), QgsFilterLineEdit.ClearToDefault)
w.setShowClearButton(False)
self.assertFalse(w.showClearButton())
w.setShowClearButton(True)
self.assertTrue(w.showClearButton())

def testNullValueHandling(self):
""" test widget handling of null values """
Expand Down

0 comments on commit d959384

Please sign in to comment.