From 7af90907435d2e2db544661f07c4fc9f93fd1c07 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Tue, 30 Jun 2020 16:47:16 +1000 Subject: [PATCH] Add QgsScopedQPainterState class for scoped save/restore calls to a QPainter object --- src/core/qgsrendercontext.h | 41 +++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/core/qgsrendercontext.h b/src/core/qgsrendercontext.h index 4e2afecc5ff..30e38559282 100644 --- a/src/core/qgsrendercontext.h +++ b/src/core/qgsrendercontext.h @@ -1040,6 +1040,47 @@ class QgsScopedRenderContextScaleToPixels QgsRenderContext &mContext; }; + + +/** + * \ingroup core + * + * Scoped object for saving and restoring a QPainter object's state. + * + * Temporarily saves the QPainter state for the lifetime of the object, before restoring it + * on destruction. + * + * \note Not available in Python bindings + * \since QGIS 3.16 + */ +class QgsScopedQPainterState +{ + public: + + /** + * Constructor for QgsScopedQPainterState. + * + * Saves the specified \a painter state. + */ + QgsScopedQPainterState( QPainter *painter ) + : mPainter( painter ) + { + mPainter->save(); + } + + /** + * Restores the painter back to its original state. + */ + ~QgsScopedQPainterState() + { + mPainter->restore(); + } + + private: + + QPainter *mPainter = nullptr; +}; + #endif #endif