Skip to content

Commit

Permalink
Add a spinner icon to QgsFilterLineEdit
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Oct 24, 2017
1 parent ceb31fa commit e9d6e38
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
23 changes: 22 additions & 1 deletion python/gui/qgsfilterlineedit.sip
Expand Up @@ -10,7 +10,6 @@







class QgsFilterLineEdit : QLineEdit class QgsFilterLineEdit : QLineEdit
{ {
%Docstring %Docstring
Expand Down Expand Up @@ -164,6 +163,23 @@ class QgsFilterLineEdit : QLineEdit
:rtype: bool :rtype: bool
%End %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: public slots:


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


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

protected: protected:
virtual void mousePressEvent( QMouseEvent *e ); virtual void mousePressEvent( QMouseEvent *e );


Expand Down
53 changes: 53 additions & 0 deletions src/gui/qgsfilterlineedit.cpp
Expand Up @@ -17,11 +17,13 @@


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


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


QgsFilterLineEdit::QgsFilterLineEdit( QWidget *parent, const QString &nullValue ) QgsFilterLineEdit::QgsFilterLineEdit( QWidget *parent, const QString &nullValue )
: QLineEdit( parent ) : QLineEdit( parent )
Expand Down Expand Up @@ -151,6 +153,13 @@ void QgsFilterLineEdit::paintEvent( QPaintEvent *e )
QPainter p( this ); QPainter p( this );
p.drawPixmap( r.left(), r.top(), mSearchIconPixmap ); 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 ) void QgsFilterLineEdit::leaveEvent( QEvent *e )
Expand Down Expand Up @@ -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 bool QgsFilterLineEdit::shouldShowClear() const
{ {
if ( !isEnabled() || isReadOnly() || !mClearButtonVisible ) if ( !isEnabled() || isReadOnly() || !mClearButtonVisible )
Expand All @@ -209,6 +250,18 @@ QRect QgsFilterLineEdit::clearRect() const
mClearIconSize.height() ); 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 QRect QgsFilterLineEdit::searchRect() const
{ {
int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth ); int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
Expand Down
28 changes: 27 additions & 1 deletion src/gui/qgsfilterlineedit.h
Expand Up @@ -23,7 +23,7 @@
#include "qgis_gui.h" #include "qgis_gui.h"


class QToolButton; class QToolButton;

class QgsAnimatedIcon;


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


public: public:


Expand Down Expand Up @@ -182,6 +183,22 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
*/ */
inline bool isNull() const { return text() == mNullValue; } 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: public slots:


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


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

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


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


private: private:


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


ClearMode mClearMode = ClearToNull; ClearMode mClearMode = ClearToNull;


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


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


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


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


/// @cond PRIVATE /// @cond PRIVATE
Expand Down

0 comments on commit e9d6e38

Please sign in to comment.