Skip to content
Permalink
Browse files

Add better Null support to QgsFilterLineEdit

  • Loading branch information
m-kuhn committed Sep 22, 2014
1 parent b90dba4 commit b1474c828c43afcd734859012294c241774b6758
Showing with 69 additions and 8 deletions.
  1. +33 −4 src/gui/qgsfilterlineedit.cpp
  2. +36 −4 src/gui/qgsfilterlineedit.h
@@ -20,10 +20,12 @@

#include <QToolButton>
#include <QStyle>
#include <QFocusEvent>

QgsFilterLineEdit::QgsFilterLineEdit( QWidget* parent, QString nullValue )
: QLineEdit( parent )
, mNullValue( nullValue )
, mFocusInEvent( false )
{
btnClear = new QToolButton( this );
btnClear->setIcon( QgsApplication::getThemeIcon( "/mIconClear.svg" ) );
@@ -35,17 +37,33 @@ QgsFilterLineEdit::QgsFilterLineEdit( QWidget* parent, QString nullValue )
connect( btnClear, SIGNAL( clicked() ), this, SLOT( clear() ) );
connect( btnClear, SIGNAL( clicked() ), this, SIGNAL( cleared() ) );
connect( this, SIGNAL( textChanged( const QString& ) ), this,
SLOT( toggleClearButton( const QString& ) ) );
SLOT( onTextChanged( const QString& ) ) );

int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
setStyleSheet( QString( "QLineEdit { padding-right: %1px; } " )
.arg( btnClear->sizeHint().width() + frameWidth + 1 ) );
mStyleSheet = QString( "QLineEdit { padding-right: %1px; } " )
.arg( btnClear->sizeHint().width() + frameWidth + 1 );

QSize msz = minimumSizeHint();
setMinimumSize( qMax( msz.width(), btnClear->sizeHint().height() + frameWidth * 2 + 2 ),
qMax( msz.height(), btnClear->sizeHint().height() + frameWidth * 2 + 2 ) );
}

void QgsFilterLineEdit::mousePressEvent( QMouseEvent* e )
{
if ( !mFocusInEvent )
QLineEdit::mousePressEvent( e );
}

void QgsFilterLineEdit::focusInEvent( QFocusEvent* e )
{
QLineEdit::focusInEvent( e );
if ( e->reason() == Qt::MouseFocusReason && isNull() )
{
mFocusInEvent = true;
selectAll();
}
}

void QgsFilterLineEdit::resizeEvent( QResizeEvent * )
{
QSize sz = btnClear->sizeHint();
@@ -69,7 +87,18 @@ void QgsFilterLineEdit::changeEvent( QEvent *e )
btnClear->setVisible( text() != mNullValue );
}

void QgsFilterLineEdit::toggleClearButton( const QString &text )
void QgsFilterLineEdit::onTextChanged( const QString &text )
{
btnClear->setVisible( !isReadOnly() && text != mNullValue );

if ( isNull() )
{
setStyleSheet( QString( "QLineEdit { font: italic; color: gray; } %1" ).arg( mStyleSheet ) );
emit valueChanged( QString::null );
}
else
{
setStyleSheet( mStyleSheet );
emit valueChanged( text );
}
}
@@ -35,22 +35,54 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit

void setNullValue( QString nullValue ) { mNullValue = nullValue; }

QString nullValue() const {return mNullValue;}
QString nullValue() const { return mNullValue; }

/**
* Sets the current text with NULL support
*
* @param value The text to set. If a Null string is provided, the text will match the nullValue.
*/
void setValue( QString value ) { setText( value.isNull() ? mNullValue : value ); }

/**
* Returns the text of this edit with NULL support
*
* @return Current text (Null string if it matches the nullValue property )
*/
QString value() { return isNull() ? QString::null : text(); }

/**
* Determine if the current text represents Null.
*
* @return True if the value is Null.
*/
inline bool isNull() { return text() == mNullValue; }

signals:
void cleared();

/**
* Same as textChanged(const QString& ) but with support for Null values.
*
* @param value The current text or Null string if it matches the nullValue property.
*/
void valueChanged( const QString& value );

protected:
void resizeEvent( QResizeEvent * );
void changeEvent( QEvent * );
void mousePressEvent( QMouseEvent* e );
void focusInEvent( QFocusEvent* e );
void resizeEvent( QResizeEvent* e );
void changeEvent( QEvent* e );

private slots:
void clear();
void toggleClearButton( const QString &text );
void onTextChanged( const QString &text );

private:
QString mNullValue;
QToolButton *btnClear;
QString mStyleSheet;
bool mFocusInEvent;
};

#endif // QGSFILTERLINEEDIT_H

0 comments on commit b1474c8

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