1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QSTYLEPAINTER_H
5#define QSTYLEPAINTER_H
6
7#include <QtWidgets/qtwidgetsglobal.h>
8#include <QtGui/qpainter.h>
9#include <QtWidgets/qstyle.h>
10#include <QtWidgets/qwidget.h>
11
12QT_BEGIN_NAMESPACE
13
14
15class QStylePainter : public QPainter
16{
17public:
18 inline QStylePainter() : QPainter(), widget(nullptr), wstyle(nullptr) {}
19 inline explicit QStylePainter(QWidget *w) { begin(pd: w, w); }
20 inline QStylePainter(QPaintDevice *pd, QWidget *w) { begin(pd, w); }
21 inline bool begin(QWidget *w) { return begin(pd: w, w); }
22 inline bool begin(QPaintDevice *pd, QWidget *w) {
23 Q_ASSERT_X(w, "QStylePainter::QStylePainter", "Widget must be non-zero");
24 widget = w;
25 wstyle = w->style();
26 const bool res = QPainter::begin(pd);
27 setRenderHint(hint: QPainter::SmoothPixmapTransform);
28 return res;
29 }
30 inline void drawPrimitive(QStyle::PrimitiveElement pe, const QStyleOption &opt);
31 inline void drawControl(QStyle::ControlElement ce, const QStyleOption &opt);
32 inline void drawComplexControl(QStyle::ComplexControl cc, const QStyleOptionComplex &opt);
33 inline void drawItemText(const QRect &r, int flags, const QPalette &pal, bool enabled,
34 const QString &text, QPalette::ColorRole textRole = QPalette::NoRole);
35 inline void drawItemPixmap(const QRect &r, int flags, const QPixmap &pixmap);
36 inline QStyle *style() const { return wstyle; }
37
38private:
39 QWidget *widget;
40 QStyle *wstyle;
41 Q_DISABLE_COPY(QStylePainter)
42};
43
44void QStylePainter::drawPrimitive(QStyle::PrimitiveElement pe, const QStyleOption &opt)
45{
46 wstyle->drawPrimitive(pe, opt: &opt, p: this, w: widget);
47}
48
49void QStylePainter::drawControl(QStyle::ControlElement ce, const QStyleOption &opt)
50{
51 wstyle->drawControl(element: ce, opt: &opt, p: this, w: widget);
52}
53
54void QStylePainter::drawComplexControl(QStyle::ComplexControl cc, const QStyleOptionComplex &opt)
55{
56 wstyle->drawComplexControl(cc, opt: &opt, p: this, widget);
57}
58
59void QStylePainter::drawItemText(const QRect &r, int flags, const QPalette &pal, bool enabled,
60 const QString &text, QPalette::ColorRole textRole)
61{
62 wstyle->drawItemText(painter: this, rect: r, flags, pal, enabled, text, textRole);
63}
64
65void QStylePainter::drawItemPixmap(const QRect &r, int flags, const QPixmap &pixmap)
66{
67 wstyle->drawItemPixmap(painter: this, rect: r, alignment: flags, pixmap);
68}
69
70QT_END_NAMESPACE
71
72#endif // QSTYLEPAINTER_H
73

source code of qtbase/src/widgets/styles/qstylepainter.h