Skip to content
Permalink
Browse files

Merge pull request #5441 from m-kuhn/spinner

[feature] Spinner icon on QgsFilterLineEdit
  • Loading branch information
m-kuhn committed Oct 25, 2017
2 parents c728a35 + 435fab1 commit dbd2cc95f2ebaf2d12d7155130c890e3fc5c4675
@@ -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,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 );

@@ -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 )
@@ -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
@@ -17,6 +17,7 @@

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

#include <QToolButton>
#include <QStyle>
@@ -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 )
@@ -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 )
@@ -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 );
@@ -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,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;
@@ -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;

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

0 comments on commit dbd2cc9

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