Skip to content

Commit f694f75

Browse files
committed
Add QgsTemporaryCursorOverride class
Temporarily sets a QApplication override cursor for the lifetime of the object, then removes it Makes it easier to ensure that the override is always removed regardless of the exit path taken.
1 parent f05e9c3 commit f694f75

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

src/app/dwg/qgsdwgimportdialog.cpp

+4-18
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,7 @@
4444
#include "qgslogger.h"
4545
#include "qgsproperty.h"
4646
#include "qgslayertree.h"
47-
48-
49-
struct CursorOverride
50-
{
51-
CursorOverride()
52-
{
53-
QApplication::setOverrideCursor( Qt::BusyCursor );
54-
}
55-
56-
~CursorOverride()
57-
{
58-
QApplication::restoreOverrideCursor();
59-
}
60-
};
61-
47+
#include "qgsguiutils.h"
6248

6349
QgsDwgImportDialog::QgsDwgImportDialog( QWidget *parent, Qt::WindowFlags f )
6450
: QDialog( parent, f )
@@ -163,7 +149,7 @@ void QgsDwgImportDialog::pbLoadDatabase_clicked()
163149
if ( !QFileInfo::exists( leDatabase->text() ) )
164150
return;
165151

166-
CursorOverride waitCursor;
152+
QgsTemporaryCursorOverride waitCursor( Qt::BusyCursor );
167153

168154
bool lblVisible = false;
169155

@@ -261,7 +247,7 @@ void QgsDwgImportDialog::pbBrowseDrawing_clicked()
261247

262248
void QgsDwgImportDialog::pbImportDrawing_clicked()
263249
{
264-
CursorOverride waitCursor;
250+
QgsTemporaryCursorOverride waitCursor( Qt::BusyCursor );
265251

266252
QgsDwgImporter importer( leDatabase->text(), mCrsSelector->crs() );
267253

@@ -457,7 +443,7 @@ void QgsDwgImportDialog::pbDeselectAll_clicked()
457443

458444
void QgsDwgImportDialog::buttonBox_accepted()
459445
{
460-
CursorOverride waitCursor;
446+
QgsTemporaryCursorOverride waitCursor( Qt::BusyCursor );
461447

462448
QMap<QString, bool> layers;
463449
bool allLayers = true;

src/gui/qgsguiutils.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <QImageWriter>
2323
#include <QFontDialog>
24+
#include <QApplication>
2425

2526

2627
namespace QgsGuiUtils
@@ -235,3 +236,17 @@ namespace QgsGuiUtils
235236
return key;
236237
}
237238
}
239+
240+
//
241+
// QgsTemporaryCursorOverride
242+
//
243+
244+
QgsTemporaryCursorOverride::QgsTemporaryCursorOverride( const QCursor &cursor )
245+
{
246+
QApplication::setOverrideCursor( cursor );
247+
}
248+
249+
QgsTemporaryCursorOverride::~QgsTemporaryCursorOverride()
250+
{
251+
QApplication::restoreOverrideCursor();
252+
}

src/gui/qgsguiutils.h

+22
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,26 @@ namespace QgsGuiUtils
164164
QString createWidgetKey( QWidget *widget, const QString &keyName = QString() );
165165
}
166166

167+
/**
168+
* Temporarily sets a cursor override for the QApplication for the lifetime of the object.
169+
*
170+
* When the object is deleted, the cursor override is removed.
171+
*
172+
* \ingroup gui
173+
* \since QGIS 3.2
174+
*/
175+
class GUI_EXPORT QgsTemporaryCursorOverride
176+
{
177+
public:
178+
179+
/**
180+
* Constructor for QgsTemporaryCursorOverride. Sets the application override
181+
* cursor to \a cursor.
182+
*/
183+
QgsTemporaryCursorOverride( const QCursor &cursor );
184+
185+
~QgsTemporaryCursorOverride();
186+
187+
};
188+
167189
#endif // QGSGUIUTILS_H

0 commit comments

Comments
 (0)