Skip to content
Permalink
Browse files

Allow hiding clear button in QgsFilterLineEdit

(cherry-pick from d959384)
  • Loading branch information
nyalldawson committed Sep 8, 2016
1 parent 5ee1b16 commit ce7c5eb92c44e88b82f354075c67811b462d032c
@@ -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 );

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

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

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

@@ -25,6 +25,7 @@

QgsFilterLineEdit::QgsFilterLineEdit( QWidget* parent, const QString& nullValue )
: QLineEdit( parent )
, mClearButtonVisible( true )
, mClearMode( ClearToNull )
, mNullValue( nullValue )
, mFocusInEvent( false )
@@ -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 )
@@ -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 )
@@ -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:

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

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

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

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

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

private:

bool mClearButtonVisible;

ClearMode mClearMode;

QString mNullValue;
@@ -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 """

0 comments on commit ce7c5eb

Please sign in to comment.
You can’t perform that action at this time.