Skip to content
Permalink
Browse files

Add a spinner icon to QgsFilterLineEdit

  • Loading branch information
m-kuhn committed Oct 24, 2017
1 parent ceb31fa commit e9d6e38796db210463d3d0bf638ebb15dc283312
Showing with 102 additions and 2 deletions.
  1. +22 −1 python/gui/qgsfilterlineedit.sip
  2. +53 −0 src/gui/qgsfilterlineedit.cpp
  3. +27 −1 src/gui/qgsfilterlineedit.h
@@ -10,7 +10,6 @@




class QgsFilterLineEdit : QLineEdit
{
%Docstring
@@ -164,6 +163,23 @@ class QgsFilterLineEdit : QLineEdit
:rtype: bool
%End

bool showSpinner() const;
%Docstring
Show a spinner icon. This can be used for search boxes to indicate that
something is going on in the background.

.. versionadded:: 3.0
:rtype: bool
%End

void setShowSpinner( bool showSpinner );
%Docstring
Show a spinner icon. This can be used for search boxes to indicate that
something is going on in the background.

.. versionadded:: 3.0
%End

public slots:

virtual void clearValue();
@@ -188,6 +204,11 @@ class QgsFilterLineEdit : QLineEdit
\param value The current text or null string if it matches the nullValue() property.
%End

void showSpinnerChanged();
%Docstring
\copydoc showSpinner
%End

protected:
virtual void mousePressEvent( QMouseEvent *e );

@@ -17,11 +17,13 @@

#include "qgsfilterlineedit.h"
#include "qgsapplication.h"
#include "qgsanimatedicon.h"

#include <QToolButton>
#include <QStyle>
#include <QFocusEvent>
#include <QPainter>
#include <QDebug>

QgsFilterLineEdit::QgsFilterLineEdit( QWidget *parent, const QString &nullValue )
: QLineEdit( parent )
@@ -151,6 +153,13 @@ void QgsFilterLineEdit::paintEvent( QPaintEvent *e )
QPainter p( this );
p.drawPixmap( r.left(), r.top(), mSearchIconPixmap );
}

if ( mShowSpinner )
{
QRect r = busySpinnerRect();
QPainter p( this );
p.drawPixmap( r.left(), r.top(), mBusySpinner->icon().pixmap( r.size() ) );
}
}

void QgsFilterLineEdit::leaveEvent( QEvent *e )
@@ -184,6 +193,38 @@ void QgsFilterLineEdit::onTextChanged( const QString &text )
}
}

void QgsFilterLineEdit::updateBusySpinner()
{
update();
}

bool QgsFilterLineEdit::showSpinner() const
{
return mShowSpinner;
}

void QgsFilterLineEdit::setShowSpinner( bool showSpinner )
{
if ( showSpinner == mShowSpinner )
return;

if ( showSpinner )
{
if ( !mBusySpinner )
mBusySpinner = new QgsAnimatedIcon( QgsApplication::iconPath( QStringLiteral( "/mIconLoading.gif" ) ), this );

mBusySpinner->connectFrameChanged( this, &QgsFilterLineEdit::updateBusySpinner );
}
else
{
mBusySpinner->disconnectFrameChanged( this, &QgsFilterLineEdit::updateBusySpinner );
update();
}

mShowSpinner = showSpinner;
emit showSpinnerChanged();
}

bool QgsFilterLineEdit::shouldShowClear() const
{
if ( !isEnabled() || isReadOnly() || !mClearButtonVisible )
@@ -209,6 +250,18 @@ QRect QgsFilterLineEdit::clearRect() const
mClearIconSize.height() );
}

QRect QgsFilterLineEdit::busySpinnerRect() const
{
int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );

int offset = shouldShowClear() ? mClearIconSize.width() + frameWidth * 2 : frameWidth;

return QRect( rect().right() - offset - mClearIconSize.width(),
( rect().bottom() + 1 - mClearIconSize.height() ) / 2,
mClearIconSize.width(),
mClearIconSize.height() );
}

QRect QgsFilterLineEdit::searchRect() const
{
int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
@@ -23,7 +23,7 @@
#include "qgis_gui.h"

class QToolButton;

class QgsAnimatedIcon;

/**
* \class QgsFilterLineEdit
@@ -54,6 +54,7 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
Q_PROPERTY( QString value READ value WRITE setValue NOTIFY valueChanged )
Q_PROPERTY( bool showClearButton READ showClearButton WRITE setShowClearButton )
Q_PROPERTY( bool showSearchIcon READ showSearchIcon WRITE setShowSearchIcon )
Q_PROPERTY( bool showSpinner READ showSpinner WRITE setShowSpinner NOTIFY showSpinnerChanged )

public:

@@ -182,6 +183,22 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
*/
inline bool isNull() const { return text() == mNullValue; }

/**
* Show a spinner icon. This can be used for search boxes to indicate that
* something is going on in the background.
*
* \since QGIS 3.0
*/
bool showSpinner() const;

/**
* Show a spinner icon. This can be used for search boxes to indicate that
* something is going on in the background.
*
* \since QGIS 3.0
*/
void setShowSpinner( bool showSpinner );

public slots:

/**
@@ -206,6 +223,11 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
*/
void valueChanged( const QString &value );

/**
* \copydoc showSpinner
*/
void showSpinnerChanged();

protected:
void mousePressEvent( QMouseEvent *e ) override;
void mouseMoveEvent( QMouseEvent *e ) override;
@@ -215,11 +237,13 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit

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

private:

bool mClearButtonVisible = true;
bool mSearchIconVisible = false;
bool mShowSpinner = false;

ClearMode mClearMode = ClearToNull;

@@ -235,12 +259,14 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit

QSize mSearchIconSize;
QPixmap mSearchIconPixmap;
QgsAnimatedIcon *mBusySpinner = nullptr;

//! Returns true if clear button should be shown
bool shouldShowClear() const;

QRect clearRect() const;
QRect searchRect() const;
QRect busySpinnerRect() const;
};

/// @cond PRIVATE

0 comments on commit e9d6e38

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