Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qpainterstateguard.h
Go to the documentation of this file.
1// Copyright (C) 2024 Christian Ehrlicher <ch.ehrlicher@gmx.de>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QPAINTERSTATEGUARD_H
6#define QPAINTERSTATEGUARD_H
7
8#include <QtCore/qtclasshelpermacros.h>
9#include <QtGui/qpainter.h>
10
11QT_BEGIN_NAMESPACE
12
13class QPainterStateGuard
14{
15 Q_DISABLE_COPY(QPainterStateGuard)
16public:
17 enum class InitialState : quint8 {
18 Save,
19 NoSave,
20 };
21
22 QPainterStateGuard(QPainterStateGuard &&other) noexcept
23 : m_painter(std::exchange(other.m_painter, nullptr))
24 , m_level(std::exchange(other.m_level, 0))
25 {}
26 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QPainterStateGuard)
27 void swap(QPainterStateGuard &other) noexcept
28 {
29 qt_ptr_swap(m_painter, other.m_painter);
30 std::swap(m_level, other.m_level);
31 }
32
33 Q_NODISCARD_CTOR
34 explicit QPainterStateGuard(QPainter *painter, InitialState state = InitialState::Save)
35 : m_painter(painter)
36 {
37 verifyPainter();
38 if (state == InitialState::Save)
39 save();
40 }
41
42 ~QPainterStateGuard()
43 {
44 while (m_level > 0)
45 restore();
46 }
47
48 void save()
49 {
50 verifyPainter();
51 m_painter->save();
52 ++m_level;
53 }
54
55 void restore()
56 {
57 verifyPainter();
58 Q_ASSERT(m_level > 0);
59 --m_level;
60 m_painter->restore();
61 }
62
63private:
64 void verifyPainter()
65 {
66 Q_ASSERT(m_painter);
67 }
68
69 QPainter *m_painter;
70 int m_level = 0;
71};
72
74
75#endif // QPAINTERSTATEGUARD_H
Combined button and popup list for selecting options.