1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
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 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | #include "svgview.h" |
51 | |
52 | #include <QSvgRenderer> |
53 | |
54 | #include <QWheelEvent> |
55 | #include <QMouseEvent> |
56 | #include <QGraphicsRectItem> |
57 | #include <QGraphicsSvgItem> |
58 | #include <QPaintEvent> |
59 | #include <qmath.h> |
60 | |
61 | #ifndef QT_NO_OPENGL |
62 | #include <QGLWidget> |
63 | #endif |
64 | |
65 | SvgView::SvgView(QWidget *parent) |
66 | : QGraphicsView(parent) |
67 | , m_renderer(Native) |
68 | , m_svgItem(nullptr) |
69 | , m_backgroundItem(nullptr) |
70 | , m_outlineItem(nullptr) |
71 | { |
72 | setScene(new QGraphicsScene(this)); |
73 | setTransformationAnchor(AnchorUnderMouse); |
74 | setDragMode(ScrollHandDrag); |
75 | setViewportUpdateMode(FullViewportUpdate); |
76 | |
77 | // Prepare background check-board pattern |
78 | QPixmap tilePixmap(64, 64); |
79 | tilePixmap.fill(fillColor: Qt::white); |
80 | QPainter tilePainter(&tilePixmap); |
81 | QColor color(220, 220, 220); |
82 | tilePainter.fillRect(x: 0, y: 0, w: 32, h: 32, b: color); |
83 | tilePainter.fillRect(x: 32, y: 32, w: 32, h: 32, b: color); |
84 | tilePainter.end(); |
85 | |
86 | setBackgroundBrush(tilePixmap); |
87 | } |
88 | |
89 | void SvgView::drawBackground(QPainter *p, const QRectF &) |
90 | { |
91 | p->save(); |
92 | p->resetTransform(); |
93 | p->drawTiledPixmap(rect: viewport()->rect(), pm: backgroundBrush().texture()); |
94 | p->restore(); |
95 | } |
96 | |
97 | QSize SvgView::svgSize() const |
98 | { |
99 | return m_svgItem ? m_svgItem->boundingRect().size().toSize() : QSize(); |
100 | } |
101 | |
102 | bool SvgView::openFile(const QString &fileName) |
103 | { |
104 | QGraphicsScene *s = scene(); |
105 | |
106 | const bool drawBackground = (m_backgroundItem ? m_backgroundItem->isVisible() : false); |
107 | const bool drawOutline = (m_outlineItem ? m_outlineItem->isVisible() : true); |
108 | |
109 | QScopedPointer<QGraphicsSvgItem> svgItem(new QGraphicsSvgItem(fileName)); |
110 | if (!svgItem->renderer()->isValid()) |
111 | return false; |
112 | |
113 | s->clear(); |
114 | resetTransform(); |
115 | |
116 | m_svgItem = svgItem.take(); |
117 | m_svgItem->setFlags(QGraphicsItem::ItemClipsToShape); |
118 | m_svgItem->setCacheMode(mode: QGraphicsItem::NoCache); |
119 | m_svgItem->setZValue(0); |
120 | |
121 | m_backgroundItem = new QGraphicsRectItem(m_svgItem->boundingRect()); |
122 | m_backgroundItem->setBrush(Qt::white); |
123 | m_backgroundItem->setPen(Qt::NoPen); |
124 | m_backgroundItem->setVisible(drawBackground); |
125 | m_backgroundItem->setZValue(-1); |
126 | |
127 | m_outlineItem = new QGraphicsRectItem(m_svgItem->boundingRect()); |
128 | QPen outline(Qt::black, 2, Qt::DashLine); |
129 | outline.setCosmetic(true); |
130 | m_outlineItem->setPen(outline); |
131 | m_outlineItem->setBrush(Qt::NoBrush); |
132 | m_outlineItem->setVisible(drawOutline); |
133 | m_outlineItem->setZValue(1); |
134 | |
135 | s->addItem(item: m_backgroundItem); |
136 | s->addItem(item: m_svgItem); |
137 | s->addItem(item: m_outlineItem); |
138 | |
139 | s->setSceneRect(m_outlineItem->boundingRect().adjusted(xp1: -10, yp1: -10, xp2: 10, yp2: 10)); |
140 | return true; |
141 | } |
142 | |
143 | void SvgView::setRenderer(RendererType type) |
144 | { |
145 | m_renderer = type; |
146 | |
147 | if (m_renderer == OpenGL) { |
148 | #ifndef QT_NO_OPENGL |
149 | setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers))); |
150 | #endif |
151 | } else { |
152 | setViewport(new QWidget); |
153 | } |
154 | } |
155 | |
156 | void SvgView::setAntialiasing(bool antialiasing) |
157 | { |
158 | setRenderHint(hint: QPainter::Antialiasing, enabled: antialiasing); |
159 | } |
160 | |
161 | void SvgView::setViewBackground(bool enable) |
162 | { |
163 | if (!m_backgroundItem) |
164 | return; |
165 | |
166 | m_backgroundItem->setVisible(enable); |
167 | } |
168 | |
169 | void SvgView::setViewOutline(bool enable) |
170 | { |
171 | if (!m_outlineItem) |
172 | return; |
173 | |
174 | m_outlineItem->setVisible(enable); |
175 | } |
176 | |
177 | qreal SvgView::zoomFactor() const |
178 | { |
179 | return transform().m11(); |
180 | } |
181 | |
182 | void SvgView::zoomIn() |
183 | { |
184 | zoomBy(factor: 2); |
185 | } |
186 | |
187 | void SvgView::zoomOut() |
188 | { |
189 | zoomBy(factor: 0.5); |
190 | } |
191 | |
192 | void SvgView::resetZoom() |
193 | { |
194 | if (!qFuzzyCompare(p1: zoomFactor(), p2: qreal(1))) { |
195 | resetTransform(); |
196 | emit zoomChanged(); |
197 | } |
198 | } |
199 | |
200 | void SvgView::paintEvent(QPaintEvent *event) |
201 | { |
202 | if (m_renderer == Image) { |
203 | if (m_image.size() != viewport()->size()) { |
204 | m_image = QImage(viewport()->size(), QImage::Format_ARGB32_Premultiplied); |
205 | } |
206 | |
207 | QPainter imagePainter(&m_image); |
208 | QGraphicsView::render(painter: &imagePainter); |
209 | imagePainter.end(); |
210 | |
211 | QPainter p(viewport()); |
212 | p.drawImage(x: 0, y: 0, image: m_image); |
213 | |
214 | } else { |
215 | QGraphicsView::paintEvent(event); |
216 | } |
217 | } |
218 | |
219 | void SvgView::wheelEvent(QWheelEvent *event) |
220 | { |
221 | zoomBy(factor: qPow(x: 1.2, y: event->angleDelta().y() / 240.0)); |
222 | } |
223 | |
224 | void SvgView::zoomBy(qreal factor) |
225 | { |
226 | const qreal currentZoom = zoomFactor(); |
227 | if ((factor < 1 && currentZoom < 0.1) || (factor > 1 && currentZoom > 10)) |
228 | return; |
229 | scale(sx: factor, sy: factor); |
230 | emit zoomChanged(); |
231 | } |
232 | |
233 | QSvgRenderer *SvgView::renderer() const |
234 | { |
235 | if (m_svgItem) |
236 | return m_svgItem->renderer(); |
237 | return nullptr; |
238 | } |
239 | |