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 <private/qpaintengine_preview_p.h>
41#include <private/qpainter_p.h>
42#include <private/qpaintengine_p.h>
43#include <private/qpicture_p.h>
44
45#include <QtPrintSupport/qprintengine.h>
46#include <QtGui/qpainter.h>
47#include <QtGui/qpicture.h>
48
49QT_BEGIN_NAMESPACE
50
51class QPreviewPaintEnginePrivate : public QPaintEnginePrivate
52{
53 Q_DECLARE_PUBLIC(QPreviewPaintEngine)
54public:
55 QPreviewPaintEnginePrivate() : state(QPrinter::Idle) {}
56 ~QPreviewPaintEnginePrivate() {}
57
58 QList<const QPicture *> pages;
59 QPaintEngine *engine;
60 QPainter *painter;
61 QPrinter::PrinterState state;
62
63 QPaintEngine *proxy_paint_engine;
64 QPrintEngine *proxy_print_engine;
65};
66
67
68QPreviewPaintEngine::QPreviewPaintEngine()
69 : QPaintEngine(*(new QPreviewPaintEnginePrivate), PaintEngineFeatures(AllFeatures & ~ObjectBoundingModeGradients))
70{
71 Q_D(QPreviewPaintEngine);
72 d->proxy_print_engine = nullptr;
73 d->proxy_paint_engine = nullptr;
74}
75
76QPreviewPaintEngine::~QPreviewPaintEngine()
77{
78 Q_D(QPreviewPaintEngine);
79
80 qDeleteAll(c: d->pages);
81}
82
83bool QPreviewPaintEngine::begin(QPaintDevice *)
84{
85 Q_D(QPreviewPaintEngine);
86
87 qDeleteAll(c: d->pages);
88 d->pages.clear();
89
90 QPicture *page = new QPicture;
91 page->d_func()->in_memory_only = true;
92 d->painter = new QPainter(page);
93 d->engine = d->painter->paintEngine();
94 *d->painter->d_func()->state = *painter()->d_func()->state;
95 d->pages.append(t: page);
96 d->state = QPrinter::Active;
97 return true;
98}
99
100bool QPreviewPaintEngine::end()
101{
102 Q_D(QPreviewPaintEngine);
103
104 delete d->painter;
105 d->painter = nullptr;
106 d->engine = nullptr;
107 d->state = QPrinter::Idle;
108 return true;
109}
110
111void QPreviewPaintEngine::updateState(const QPaintEngineState &state)
112{
113 Q_D(QPreviewPaintEngine);
114 d->engine->updateState(state);
115}
116
117void QPreviewPaintEngine::drawPath(const QPainterPath &path)
118{
119 Q_D(QPreviewPaintEngine);
120 d->engine->drawPath(path);
121}
122
123void QPreviewPaintEngine::drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode)
124{
125 Q_D(QPreviewPaintEngine);
126 d->engine->drawPolygon(points, pointCount, mode);
127}
128
129void QPreviewPaintEngine::drawTextItem(const QPointF &p, const QTextItem &textItem)
130{
131 Q_D(QPreviewPaintEngine);
132 d->engine->drawTextItem(p, textItem);
133}
134
135void QPreviewPaintEngine::drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr)
136{
137 Q_D(QPreviewPaintEngine);
138 d->engine->drawPixmap(r, pm, sr);
139}
140
141void QPreviewPaintEngine::drawTiledPixmap(const QRectF &r, const QPixmap &pm, const QPointF &p)
142{
143 Q_D(QPreviewPaintEngine);
144 d->engine->drawTiledPixmap(r, pixmap: pm, s: p);
145}
146
147bool QPreviewPaintEngine::newPage()
148{
149 Q_D(QPreviewPaintEngine);
150
151 QPicture *page = new QPicture;
152 page->d_func()->in_memory_only = true;
153 QPainter *tmp_painter = new QPainter(page);
154 QPaintEngine *tmp_engine = tmp_painter->paintEngine();
155
156 // copy the painter state from the original painter
157 Q_ASSERT(painter()->d_func()->state && tmp_painter->d_func()->state);
158 *tmp_painter->d_func()->state = *painter()->d_func()->state;
159
160 // composition modes aren't supported on a QPrinter and yields a
161 // warning, so ignore it for now
162 tmp_engine->setDirty(DirtyFlags(AllDirty & ~DirtyCompositionMode));
163 tmp_engine->syncState();
164
165 delete d->painter;
166 d->painter = tmp_painter;
167 d->pages.append(t: page);
168 d->engine = tmp_engine;
169 return true;
170}
171
172bool QPreviewPaintEngine::abort()
173{
174 Q_D(QPreviewPaintEngine);
175 end();
176 qDeleteAll(c: d->pages);
177 d->state = QPrinter::Aborted;
178
179 return true;
180}
181
182QList<const QPicture *> QPreviewPaintEngine::pages()
183{
184 Q_D(QPreviewPaintEngine);
185 return d->pages;
186}
187
188void QPreviewPaintEngine::setProxyEngines(QPrintEngine *printEngine, QPaintEngine *paintEngine)
189{
190 Q_D(QPreviewPaintEngine);
191 d->proxy_print_engine = printEngine;
192 d->proxy_paint_engine = paintEngine;
193}
194
195void QPreviewPaintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &value)
196{
197 Q_D(QPreviewPaintEngine);
198 d->proxy_print_engine->setProperty(key, value);
199}
200
201QVariant QPreviewPaintEngine::property(PrintEnginePropertyKey key) const
202{
203 Q_D(const QPreviewPaintEngine);
204 return d->proxy_print_engine->property(key);
205}
206
207int QPreviewPaintEngine::metric(QPaintDevice::PaintDeviceMetric id) const
208{
209 Q_D(const QPreviewPaintEngine);
210 return d->proxy_print_engine->metric(id);
211}
212
213QPrinter::PrinterState QPreviewPaintEngine::printerState() const
214{
215 Q_D(const QPreviewPaintEngine);
216 return d->state;
217}
218
219QT_END_NAMESPACE
220

source code of qtbase/src/printsupport/kernel/qpaintengine_preview.cpp