Skip to content

Commit dbd2cc9

Browse files
authoredOct 25, 2017
Merge pull request #5441 from m-kuhn/spinner
[feature] Spinner icon on QgsFilterLineEdit
2 parents c728a35 + 435fab1 commit dbd2cc9

File tree

4 files changed

+113
-2
lines changed

4 files changed

+113
-2
lines changed
 

‎python/gui/qgsfilterlineedit.sip

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111

1212

13-
1413
class QgsFilterLineEdit : QLineEdit
1514
{
1615
%Docstring
@@ -164,6 +163,23 @@ class QgsFilterLineEdit : QLineEdit
164163
:rtype: bool
165164
%End
166165

166+
bool showSpinner() const;
167+
%Docstring
168+
Show a spinner icon. This can be used for search boxes to indicate that
169+
something is going on in the background.
170+
171+
.. versionadded:: 3.0
172+
:rtype: bool
173+
%End
174+
175+
void setShowSpinner( bool showSpinner );
176+
%Docstring
177+
Show a spinner icon. This can be used for search boxes to indicate that
178+
something is going on in the background.
179+
180+
.. versionadded:: 3.0
181+
%End
182+
167183
public slots:
168184

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

207+
void showSpinnerChanged();
208+
%Docstring
209+
Show a spinner icon. This can be used for search boxes to indicate that
210+
something is going on in the background.
211+
212+
.. versionadded:: 3.0
213+
%End
214+
191215
protected:
192216
virtual void mousePressEvent( QMouseEvent *e );
193217

‎src/gui/locator/qgslocatorwidget.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ void QgsLocatorWidget::searchFinished()
172172
mHasQueuedRequest = false;
173173
updateResults( nextSearch );
174174
}
175+
else
176+
{
177+
if ( !mLocator->isRunning() )
178+
mLineEdit->setShowSpinner( false );
179+
}
175180
}
176181

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

293298
void QgsLocatorWidget::updateResults( const QString &text )
294299
{
300+
mLineEdit->setShowSpinner( true );
295301
if ( mLocator->isRunning() )
296302
{
297303
// can't do anything while a query is running, and can't block

‎src/gui/qgsfilterlineedit.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "qgsfilterlineedit.h"
1919
#include "qgsapplication.h"
20+
#include "qgsanimatedicon.h"
2021

2122
#include <QToolButton>
2223
#include <QStyle>
@@ -151,6 +152,13 @@ void QgsFilterLineEdit::paintEvent( QPaintEvent *e )
151152
QPainter p( this );
152153
p.drawPixmap( r.left(), r.top(), mSearchIconPixmap );
153154
}
155+
156+
if ( mShowSpinner )
157+
{
158+
QRect r = busySpinnerRect();
159+
QPainter p( this );
160+
p.drawPixmap( r.left(), r.top(), mBusySpinner->icon().pixmap( r.size() ) );
161+
}
154162
}
155163

156164
void QgsFilterLineEdit::leaveEvent( QEvent *e )
@@ -184,6 +192,38 @@ void QgsFilterLineEdit::onTextChanged( const QString &text )
184192
}
185193
}
186194

195+
void QgsFilterLineEdit::updateBusySpinner()
196+
{
197+
update();
198+
}
199+
200+
bool QgsFilterLineEdit::showSpinner() const
201+
{
202+
return mShowSpinner;
203+
}
204+
205+
void QgsFilterLineEdit::setShowSpinner( bool showSpinner )
206+
{
207+
if ( showSpinner == mShowSpinner )
208+
return;
209+
210+
if ( showSpinner )
211+
{
212+
if ( !mBusySpinner )
213+
mBusySpinner = new QgsAnimatedIcon( QgsApplication::iconPath( QStringLiteral( "/mIconLoading.gif" ) ), this );
214+
215+
mBusySpinner->connectFrameChanged( this, &QgsFilterLineEdit::updateBusySpinner );
216+
}
217+
else
218+
{
219+
mBusySpinner->disconnectFrameChanged( this, &QgsFilterLineEdit::updateBusySpinner );
220+
update();
221+
}
222+
223+
mShowSpinner = showSpinner;
224+
emit showSpinnerChanged();
225+
}
226+
187227
bool QgsFilterLineEdit::shouldShowClear() const
188228
{
189229
if ( !isEnabled() || isReadOnly() || !mClearButtonVisible )
@@ -209,6 +249,18 @@ QRect QgsFilterLineEdit::clearRect() const
209249
mClearIconSize.height() );
210250
}
211251

252+
QRect QgsFilterLineEdit::busySpinnerRect() const
253+
{
254+
int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
255+
256+
int offset = shouldShowClear() ? mClearIconSize.width() + frameWidth * 2 : frameWidth;
257+
258+
return QRect( rect().right() - offset - mClearIconSize.width(),
259+
( rect().bottom() + 1 - mClearIconSize.height() ) / 2,
260+
mClearIconSize.width(),
261+
mClearIconSize.height() );
262+
}
263+
212264
QRect QgsFilterLineEdit::searchRect() const
213265
{
214266
int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );

‎src/gui/qgsfilterlineedit.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "qgis_gui.h"
2424

2525
class QToolButton;
26-
26+
class QgsAnimatedIcon;
2727

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

5859
public:
5960

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

186+
/**
187+
* Show a spinner icon. This can be used for search boxes to indicate that
188+
* something is going on in the background.
189+
*
190+
* \since QGIS 3.0
191+
*/
192+
bool showSpinner() const;
193+
194+
/**
195+
* Show a spinner icon. This can be used for search boxes to indicate that
196+
* something is going on in the background.
197+
*
198+
* \since QGIS 3.0
199+
*/
200+
void setShowSpinner( bool showSpinner );
201+
185202
public slots:
186203

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

226+
/**
227+
* Show a spinner icon. This can be used for search boxes to indicate that
228+
* something is going on in the background.
229+
*
230+
* \since QGIS 3.0
231+
*/
232+
void showSpinnerChanged();
233+
209234
protected:
210235
void mousePressEvent( QMouseEvent *e ) override;
211236
void mouseMoveEvent( QMouseEvent *e ) override;
@@ -215,11 +240,13 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
215240

216241
private slots:
217242
void onTextChanged( const QString &text );
243+
void updateBusySpinner();
218244

219245
private:
220246

221247
bool mClearButtonVisible = true;
222248
bool mSearchIconVisible = false;
249+
bool mShowSpinner = false;
223250

224251
ClearMode mClearMode = ClearToNull;
225252

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

236263
QSize mSearchIconSize;
237264
QPixmap mSearchIconPixmap;
265+
QgsAnimatedIcon *mBusySpinner = nullptr;
238266

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

242270
QRect clearRect() const;
243271
QRect searchRect() const;
272+
QRect busySpinnerRect() const;
244273
};
245274

246275
/// @cond PRIVATE

0 commit comments

Comments
 (0)
Please sign in to comment.