1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qrasterwindow.h"
41
42 #include <QtGui/private/qpaintdevicewindow_p.h>
43
44 #include <QtGui/QBackingStore>
45 #include <QtGui/QPainter>
46
47 QT_BEGIN_NAMESPACE
48
49 /*!
50 \class QRasterWindow
51 \inmodule QtGui
52 \since 5.4
53 \brief QRasterWindow is a convenience class for using QPainter on a QWindow.
54
55 QRasterWindow is a QWindow with a raster-based, non-OpenGL surface. On top of
56 the functionality offered by QWindow, QRasterWindow adds a virtual
57 paintEvent() function and the possibility to open a QPainter on itself. The
58 underlying paint engine will be the raster one, meaning that all drawing will
59 happen on the CPU. For performing accelerated, OpenGL-based drawing, use
60 QOpenGLWindow instead.
61
62 Internally the class is thin wrapper for QWindow and QBackingStore
63 and is very similar to the \l{Raster Window Example}{Raster Window
64 Example} that uses these classes directly.
65
66 \sa QPaintDeviceWindow::paintEvent(), QPaintDeviceWindow::update()
67 */
68
69 class QRasterWindowPrivate : public QPaintDeviceWindowPrivate
70 {
71 Q_DECLARE_PUBLIC(QRasterWindow)
72 public:
beginPaint(const QRegion & region)73 void beginPaint(const QRegion ®ion) override
74 {
75 Q_Q(QRasterWindow);
76 const QSize size = q->size();
77 if (backingstore->size() != size) {
78 backingstore->resize(size);
79 markWindowAsDirty();
80 }
81 backingstore->beginPaint(region);
82 }
83
endPaint()84 void endPaint() override
85 {
86 backingstore->endPaint();
87 }
88
flush(const QRegion & region)89 void flush(const QRegion ®ion) override
90 {
91 Q_Q(QRasterWindow);
92 backingstore->flush(region, q);
93 }
94
95 QScopedPointer<QBackingStore> backingstore;
96 };
97
98 /*!
99 Constructs a new QRasterWindow with \a parent.
100 */
QRasterWindow(QWindow * parent)101 QRasterWindow::QRasterWindow(QWindow *parent)
102 : QPaintDeviceWindow(*(new QRasterWindowPrivate), parent)
103 {
104 setSurfaceType(QSurface::RasterSurface);
105 d_func()->backingstore.reset(new QBackingStore(this));
106 }
107
~QRasterWindow()108 QRasterWindow::~QRasterWindow()
109 {
110 Q_D(QRasterWindow);
111 // Delete backingstore while window is still alive, as it
112 // might need to reference the window in the process
113 d->backingstore.reset(nullptr);
114 }
115
116 /*!
117 \internal
118 */
metric(PaintDeviceMetric metric) const119 int QRasterWindow::metric(PaintDeviceMetric metric) const
120 {
121 Q_D(const QRasterWindow);
122
123 switch (metric) {
124 case PdmDepth:
125 return d->backingstore->paintDevice()->depth();
126 default:
127 break;
128 }
129 return QPaintDeviceWindow::metric(metric);
130 }
131
132 /*!
133 \internal
134 */
redirected(QPoint *) const135 QPaintDevice *QRasterWindow::redirected(QPoint *) const
136 {
137 Q_D(const QRasterWindow);
138 return d->backingstore->paintDevice();
139 }
140
141 QT_END_NAMESPACE
142