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

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