File indexing completed on 2025-09-16 09:05:14
0001
0002
0003
0004 #ifndef QPAINTERSTATEGUARD_H
0005 #define QPAINTERSTATEGUARD_H
0006
0007 #include <QtCore/qtclasshelpermacros.h>
0008 #include <QtGui/qpainter.h>
0009
0010 QT_BEGIN_NAMESPACE
0011
0012 class QPainterStateGuard
0013 {
0014 Q_DISABLE_COPY(QPainterStateGuard)
0015 public:
0016 enum class InitialState : quint8 {
0017 Save,
0018 NoSave,
0019 };
0020
0021 QPainterStateGuard(QPainterStateGuard &&other) noexcept
0022 : m_painter(std::exchange(other.m_painter, nullptr))
0023 , m_level(std::exchange(other.m_level, 0))
0024 {}
0025 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QPainterStateGuard)
0026 void swap(QPainterStateGuard &other) noexcept
0027 {
0028 qt_ptr_swap(m_painter, other.m_painter);
0029 std::swap(m_level, other.m_level);
0030 }
0031
0032 Q_NODISCARD_CTOR
0033 explicit QPainterStateGuard(QPainter *painter, InitialState state = InitialState::Save)
0034 : m_painter(painter)
0035 {
0036 verifyPainter();
0037 if (state == InitialState::Save)
0038 save();
0039 }
0040
0041 ~QPainterStateGuard()
0042 {
0043 while (m_level > 0)
0044 restore();
0045 }
0046
0047 void save()
0048 {
0049 verifyPainter();
0050 m_painter->save();
0051 ++m_level;
0052 }
0053
0054 void restore()
0055 {
0056 verifyPainter();
0057 Q_ASSERT(m_level > 0);
0058 --m_level;
0059 m_painter->restore();
0060 }
0061
0062 private:
0063 void verifyPainter()
0064 {
0065 Q_ASSERT(m_painter);
0066 }
0067
0068 QPainter *m_painter;
0069 int m_level = 0;
0070 };
0071
0072 QT_END_NAMESPACE
0073
0074 #endif