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.
This commit is contained in:
Nyall Dawson 2018-03-12 15:31:46 +10:00
parent f05e9c3cac
commit f694f75f5d
3 changed files with 41 additions and 18 deletions

View File

@ -44,21 +44,7 @@
#include "qgslogger.h"
#include "qgsproperty.h"
#include "qgslayertree.h"
struct CursorOverride
{
CursorOverride()
{
QApplication::setOverrideCursor( Qt::BusyCursor );
}
~CursorOverride()
{
QApplication::restoreOverrideCursor();
}
};
#include "qgsguiutils.h"
QgsDwgImportDialog::QgsDwgImportDialog( QWidget *parent, Qt::WindowFlags f )
: QDialog( parent, f )
@ -163,7 +149,7 @@ void QgsDwgImportDialog::pbLoadDatabase_clicked()
if ( !QFileInfo::exists( leDatabase->text() ) )
return;
CursorOverride waitCursor;
QgsTemporaryCursorOverride waitCursor( Qt::BusyCursor );
bool lblVisible = false;
@ -261,7 +247,7 @@ void QgsDwgImportDialog::pbBrowseDrawing_clicked()
void QgsDwgImportDialog::pbImportDrawing_clicked()
{
CursorOverride waitCursor;
QgsTemporaryCursorOverride waitCursor( Qt::BusyCursor );
QgsDwgImporter importer( leDatabase->text(), mCrsSelector->crs() );
@ -457,7 +443,7 @@ void QgsDwgImportDialog::pbDeselectAll_clicked()
void QgsDwgImportDialog::buttonBox_accepted()
{
CursorOverride waitCursor;
QgsTemporaryCursorOverride waitCursor( Qt::BusyCursor );
QMap<QString, bool> layers;
bool allLayers = true;

View File

@ -21,6 +21,7 @@
#include <QImageWriter>
#include <QFontDialog>
#include <QApplication>
namespace QgsGuiUtils
@ -235,3 +236,17 @@ namespace QgsGuiUtils
return key;
}
}
//
// QgsTemporaryCursorOverride
//
QgsTemporaryCursorOverride::QgsTemporaryCursorOverride( const QCursor &cursor )
{
QApplication::setOverrideCursor( cursor );
}
QgsTemporaryCursorOverride::~QgsTemporaryCursorOverride()
{
QApplication::restoreOverrideCursor();
}

View File

@ -164,4 +164,26 @@ namespace QgsGuiUtils
QString createWidgetKey( QWidget *widget, const QString &keyName = QString() );
}
/**
* Temporarily sets a cursor override for the QApplication for the lifetime of the object.
*
* When the object is deleted, the cursor override is removed.
*
* \ingroup gui
* \since QGIS 3.2
*/
class GUI_EXPORT QgsTemporaryCursorOverride
{
public:
/**
* Constructor for QgsTemporaryCursorOverride. Sets the application override
* cursor to \a cursor.
*/
QgsTemporaryCursorOverride( const QCursor &cursor );
~QgsTemporaryCursorOverride();
};
#endif // QGSGUIUTILS_H