Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
New utility class QgsTemporaryCursorRestoreOverride
This class temporarily removes ALL cursor overrides for the
QApplication for the lifetime of the object, and restores them
(in the same order) upon object destruction.
  • Loading branch information
nyalldawson committed Mar 19, 2019
1 parent 6bb0723 commit 5fb9802
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/gui/qgsguiutils.cpp
Expand Up @@ -272,3 +272,31 @@ void QgsTemporaryCursorOverride::release()
mHasOverride = false;
QApplication::restoreOverrideCursor();
}


//
// QgsTemporaryCursorRestoreOverride
//

QgsTemporaryCursorRestoreOverride::QgsTemporaryCursorRestoreOverride()
{
while ( QApplication::overrideCursor() )
{
mCursors.emplace_back( QCursor( *QApplication::overrideCursor() ) );
QApplication::restoreOverrideCursor();
}
}

QgsTemporaryCursorRestoreOverride::~QgsTemporaryCursorRestoreOverride()
{
restore();
}

void QgsTemporaryCursorRestoreOverride::restore()
{
for ( auto it = mCursors.rbegin(); it != mCursors.rend(); ++it )
{
QApplication::setOverrideCursor( *it );
}
mCursors.clear();
}
31 changes: 31 additions & 0 deletions src/gui/qgsguiutils.h
Expand Up @@ -206,4 +206,35 @@ class GUI_EXPORT QgsTemporaryCursorOverride

};

/**
* Temporarily removes all cursor overrides for the QApplication for the lifetime of the object.
*
* When the object is deleted, all stacked cursor overrides are restored.
*
* \ingroup gui
* \since QGIS 3.8
*/
class GUI_EXPORT QgsTemporaryCursorRestoreOverride
{
public:

/**
* Constructor for QgsTemporaryCursorRestoreOverride. Removes all application override
* cursors.
*/
QgsTemporaryCursorRestoreOverride();

~QgsTemporaryCursorRestoreOverride();

/**
* Restores the cursor override early (i.e. before this object is destroyed).
*/
void restore();

private:

std::vector< QCursor > mCursors;

};

#endif // QGSGUIUTILS_H

0 comments on commit 5fb9802

Please sign in to comment.