Skip to content

Commit

Permalink
Merge pull request #5441 from m-kuhn/spinner
Browse files Browse the repository at this point in the history
[feature] Spinner icon on QgsFilterLineEdit
  • Loading branch information
m-kuhn authored Oct 25, 2017
2 parents c728a35 + 435fab1 commit dbd2cc9
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 2 deletions.
26 changes: 25 additions & 1 deletion python/gui/qgsfilterlineedit.sip
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@




class QgsFilterLineEdit : QLineEdit
{
%Docstring
Expand Down Expand Up @@ -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();
Expand All @@ -188,6 +204,14 @@ class QgsFilterLineEdit : QLineEdit
\param value The current text or null string if it matches the nullValue() property.
%End

void showSpinnerChanged();
%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

protected:
virtual void mousePressEvent( QMouseEvent *e );

Expand Down
6 changes: 6 additions & 0 deletions src/gui/locator/qgslocatorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ void QgsLocatorWidget::searchFinished()
mHasQueuedRequest = false;
updateResults( nextSearch );
}
else
{
if ( !mLocator->isRunning() )
mLineEdit->setShowSpinner( false );
}
}

bool QgsLocatorWidget::eventFilter( QObject *obj, QEvent *event )
Expand Down Expand Up @@ -292,6 +297,7 @@ void QgsLocatorWidget::configMenuAboutToShow()

void QgsLocatorWidget::updateResults( const QString &text )
{
mLineEdit->setShowSpinner( true );
if ( mLocator->isRunning() )
{
// can't do anything while a query is running, and can't block
Expand Down
52 changes: 52 additions & 0 deletions src/gui/qgsfilterlineedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

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

#include <QToolButton>
#include <QStyle>
Expand Down Expand Up @@ -151,6 +152,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 )
Expand Down Expand Up @@ -184,6 +192,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 )
Expand All @@ -209,6 +249,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 );
Expand Down
31 changes: 30 additions & 1 deletion src/gui/qgsfilterlineedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "qgis_gui.h"

class QToolButton;

class QgsAnimatedIcon;

/**
* \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( 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:

Expand Down Expand Up @@ -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:

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

/**
* 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 showSpinnerChanged();

protected:
void mousePressEvent( QMouseEvent *e ) override;
void mouseMoveEvent( QMouseEvent *e ) override;
Expand All @@ -215,11 +240,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;

Expand All @@ -235,12 +262,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
Expand Down

0 comments on commit dbd2cc9

Please sign in to comment.