Skip to content

Commit e9d6e38

Browse files
committedOct 24, 2017
Add a spinner icon to QgsFilterLineEdit
1 parent ceb31fa commit e9d6e38

File tree

3 files changed

+102
-2
lines changed

3 files changed

+102
-2
lines changed
 

‎python/gui/qgsfilterlineedit.sip

Lines changed: 22 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,11 @@ 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+
\copydoc showSpinner
210+
%End
211+
191212
protected:
192213
virtual void mousePressEvent( QMouseEvent *e );
193214

‎src/gui/qgsfilterlineedit.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717

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

2122
#include <QToolButton>
2223
#include <QStyle>
2324
#include <QFocusEvent>
2425
#include <QPainter>
26+
#include <QDebug>
2527

2628
QgsFilterLineEdit::QgsFilterLineEdit( QWidget *parent, const QString &nullValue )
2729
: QLineEdit( parent )
@@ -151,6 +153,13 @@ void QgsFilterLineEdit::paintEvent( QPaintEvent *e )
151153
QPainter p( this );
152154
p.drawPixmap( r.left(), r.top(), mSearchIconPixmap );
153155
}
156+
157+
if ( mShowSpinner )
158+
{
159+
QRect r = busySpinnerRect();
160+
QPainter p( this );
161+
p.drawPixmap( r.left(), r.top(), mBusySpinner->icon().pixmap( r.size() ) );
162+
}
154163
}
155164

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

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

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

‎src/gui/qgsfilterlineedit.h

Lines changed: 27 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,11 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
206223
*/
207224
void valueChanged( const QString &value );
208225

226+
/**
227+
* \copydoc showSpinner
228+
*/
229+
void showSpinnerChanged();
230+
209231
protected:
210232
void mousePressEvent( QMouseEvent *e ) override;
211233
void mouseMoveEvent( QMouseEvent *e ) override;
@@ -215,11 +237,13 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
215237

216238
private slots:
217239
void onTextChanged( const QString &text );
240+
void updateBusySpinner();
218241

219242
private:
220243

221244
bool mClearButtonVisible = true;
222245
bool mSearchIconVisible = false;
246+
bool mShowSpinner = false;
223247

224248
ClearMode mClearMode = ClearToNull;
225249

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

236260
QSize mSearchIconSize;
237261
QPixmap mSearchIconPixmap;
262+
QgsAnimatedIcon *mBusySpinner = nullptr;
238263

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

242267
QRect clearRect() const;
243268
QRect searchRect() const;
269+
QRect busySpinnerRect() const;
244270
};
245271

246272
/// @cond PRIVATE

0 commit comments

Comments
 (0)
Please sign in to comment.