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 | #include "qrasterwindow.h" |
5 | |
6 | #include <QtGui/private/qpaintdevicewindow_p.h> |
7 | |
8 | #include <QtGui/QBackingStore> |
9 | #include <QtGui/QPainter> |
10 | |
11 | QT_BEGIN_NAMESPACE |
12 | |
13 | /*! |
14 | \class QRasterWindow |
15 | \inmodule QtGui |
16 | \since 5.4 |
17 | \brief QRasterWindow is a convenience class for using QPainter on a QWindow. |
18 | |
19 | QRasterWindow is a QWindow with a raster-based, non-OpenGL surface. On top of |
20 | the functionality offered by QWindow, QRasterWindow adds a virtual |
21 | paintEvent() function and the possibility to open a QPainter on itself. The |
22 | underlying paint engine will be the raster one, meaning that all drawing will |
23 | happen on the CPU. For performing accelerated, OpenGL-based drawing, use |
24 | QOpenGLWindow instead. |
25 | |
26 | Internally the class is thin wrapper for QWindow and QBackingStore |
27 | and is very similar to the \l{Raster Window Example}{Raster Window |
28 | Example} that uses these classes directly. |
29 | |
30 | \sa QPaintDeviceWindow::paintEvent(), QPaintDeviceWindow::update() |
31 | */ |
32 | |
33 | class QRasterWindowPrivate : public QPaintDeviceWindowPrivate |
34 | { |
35 | Q_DECLARE_PUBLIC(QRasterWindow) |
36 | public: |
37 | void beginPaint(const QRegion ®ion) override |
38 | { |
39 | Q_Q(QRasterWindow); |
40 | const QSize size = q->size(); |
41 | if (backingstore->size() != size) { |
42 | backingstore->resize(size); |
43 | markWindowAsDirty(); |
44 | } |
45 | backingstore->beginPaint(region); |
46 | } |
47 | |
48 | void endPaint() override |
49 | { |
50 | backingstore->endPaint(); |
51 | } |
52 | |
53 | void flush(const QRegion ®ion) override |
54 | { |
55 | Q_Q(QRasterWindow); |
56 | backingstore->flush(region, window: q); |
57 | } |
58 | |
59 | QScopedPointer<QBackingStore> backingstore; |
60 | }; |
61 | |
62 | /*! |
63 | Constructs a new QRasterWindow with \a parent. |
64 | */ |
65 | QRasterWindow::QRasterWindow(QWindow *parent) |
66 | : QPaintDeviceWindow(*(new QRasterWindowPrivate), parent) |
67 | { |
68 | setSurfaceType(QSurface::RasterSurface); |
69 | d_func()->backingstore.reset(other: new QBackingStore(this)); |
70 | } |
71 | |
72 | QRasterWindow::~QRasterWindow() |
73 | { |
74 | Q_D(QRasterWindow); |
75 | // Delete backingstore while window is still alive, as it |
76 | // might need to reference the window in the process |
77 | d->backingstore.reset(other: nullptr); |
78 | } |
79 | |
80 | /*! |
81 | \internal |
82 | */ |
83 | int QRasterWindow::metric(PaintDeviceMetric metric) const |
84 | { |
85 | Q_D(const QRasterWindow); |
86 | |
87 | switch (metric) { |
88 | case PdmDepth: |
89 | return d->backingstore->paintDevice()->depth(); |
90 | default: |
91 | break; |
92 | } |
93 | return QPaintDeviceWindow::metric(metric); |
94 | } |
95 | |
96 | /*! |
97 | \internal |
98 | */ |
99 | QPaintDevice *QRasterWindow::redirected(QPoint *) const |
100 | { |
101 | Q_D(const QRasterWindow); |
102 | return d->backingstore->paintDevice(); |
103 | } |
104 | |
105 | QT_END_NAMESPACE |
106 | |
107 | #include "moc_qrasterwindow.cpp" |
108 |