1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 | #include "svgview.h"
|
---|
41 |
|
---|
42 | #include <QFile>
|
---|
43 | #include <QWheelEvent>
|
---|
44 | #include <QMouseEvent>
|
---|
45 | #include <QGraphicsRectItem>
|
---|
46 | #include <QGraphicsSvgItem>
|
---|
47 | #include <QPaintEvent>
|
---|
48 | #include <qmath.h>
|
---|
49 |
|
---|
50 | #ifndef QT_NO_OPENGL
|
---|
51 | #include <QGLWidget>
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | SvgView::SvgView(QWidget *parent)
|
---|
55 | : QGraphicsView(parent)
|
---|
56 | , m_renderer(Native)
|
---|
57 | , m_svgItem(0)
|
---|
58 | , m_backgroundItem(0)
|
---|
59 | , m_outlineItem(0)
|
---|
60 | {
|
---|
61 | setScene(new QGraphicsScene(this));
|
---|
62 | setTransformationAnchor(AnchorUnderMouse);
|
---|
63 | setDragMode(ScrollHandDrag);
|
---|
64 | setViewportUpdateMode(FullViewportUpdate);
|
---|
65 |
|
---|
66 | // Prepare background check-board pattern
|
---|
67 | QPixmap tilePixmap(64, 64);
|
---|
68 | tilePixmap.fill(Qt::white);
|
---|
69 | QPainter tilePainter(&tilePixmap);
|
---|
70 | QColor color(220, 220, 220);
|
---|
71 | tilePainter.fillRect(0, 0, 32, 32, color);
|
---|
72 | tilePainter.fillRect(32, 32, 32, 32, color);
|
---|
73 | tilePainter.end();
|
---|
74 |
|
---|
75 | setBackgroundBrush(tilePixmap);
|
---|
76 | }
|
---|
77 |
|
---|
78 | void SvgView::drawBackground(QPainter *p, const QRectF &)
|
---|
79 | {
|
---|
80 | p->save();
|
---|
81 | p->resetTransform();
|
---|
82 | p->drawTiledPixmap(viewport()->rect(), backgroundBrush().texture());
|
---|
83 | p->restore();
|
---|
84 | }
|
---|
85 |
|
---|
86 | void SvgView::openFile(const QFile &file)
|
---|
87 | {
|
---|
88 | if (!file.exists())
|
---|
89 | return;
|
---|
90 |
|
---|
91 | QGraphicsScene *s = scene();
|
---|
92 |
|
---|
93 | bool drawBackground = (m_backgroundItem ? m_backgroundItem->isVisible() : false);
|
---|
94 | bool drawOutline = (m_outlineItem ? m_outlineItem->isVisible() : true);
|
---|
95 |
|
---|
96 | s->clear();
|
---|
97 | resetTransform();
|
---|
98 |
|
---|
99 | m_svgItem = new QGraphicsSvgItem(file.fileName());
|
---|
100 | m_svgItem->setFlags(QGraphicsItem::ItemClipsToShape);
|
---|
101 | m_svgItem->setCacheMode(QGraphicsItem::NoCache);
|
---|
102 | m_svgItem->setZValue(0);
|
---|
103 |
|
---|
104 | m_backgroundItem = new QGraphicsRectItem(m_svgItem->boundingRect());
|
---|
105 | m_backgroundItem->setBrush(Qt::white);
|
---|
106 | m_backgroundItem->setPen(Qt::NoPen);
|
---|
107 | m_backgroundItem->setVisible(drawBackground);
|
---|
108 | m_backgroundItem->setZValue(-1);
|
---|
109 |
|
---|
110 | m_outlineItem = new QGraphicsRectItem(m_svgItem->boundingRect());
|
---|
111 | QPen outline(Qt::black, 2, Qt::DashLine);
|
---|
112 | outline.setCosmetic(true);
|
---|
113 | m_outlineItem->setPen(outline);
|
---|
114 | m_outlineItem->setBrush(Qt::NoBrush);
|
---|
115 | m_outlineItem->setVisible(drawOutline);
|
---|
116 | m_outlineItem->setZValue(1);
|
---|
117 |
|
---|
118 | s->addItem(m_backgroundItem);
|
---|
119 | s->addItem(m_svgItem);
|
---|
120 | s->addItem(m_outlineItem);
|
---|
121 |
|
---|
122 | s->setSceneRect(m_outlineItem->boundingRect().adjusted(-10, -10, 10, 10));
|
---|
123 | }
|
---|
124 |
|
---|
125 | void SvgView::setRenderer(RendererType type)
|
---|
126 | {
|
---|
127 | m_renderer = type;
|
---|
128 |
|
---|
129 | if (m_renderer == OpenGL) {
|
---|
130 | #ifndef QT_NO_OPENGL
|
---|
131 | setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
|
---|
132 | #endif
|
---|
133 | } else {
|
---|
134 | setViewport(new QWidget);
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | void SvgView::setHighQualityAntialiasing(bool highQualityAntialiasing)
|
---|
139 | {
|
---|
140 | #ifndef QT_NO_OPENGL
|
---|
141 | setRenderHint(QPainter::HighQualityAntialiasing, highQualityAntialiasing);
|
---|
142 | #else
|
---|
143 | Q_UNUSED(highQualityAntialiasing);
|
---|
144 | #endif
|
---|
145 | }
|
---|
146 |
|
---|
147 | void SvgView::setViewBackground(bool enable)
|
---|
148 | {
|
---|
149 | if (!m_backgroundItem)
|
---|
150 | return;
|
---|
151 |
|
---|
152 | m_backgroundItem->setVisible(enable);
|
---|
153 | }
|
---|
154 |
|
---|
155 | void SvgView::setViewOutline(bool enable)
|
---|
156 | {
|
---|
157 | if (!m_outlineItem)
|
---|
158 | return;
|
---|
159 |
|
---|
160 | m_outlineItem->setVisible(enable);
|
---|
161 | }
|
---|
162 |
|
---|
163 | void SvgView::paintEvent(QPaintEvent *event)
|
---|
164 | {
|
---|
165 | if (m_renderer == Image) {
|
---|
166 | if (m_image.size() != viewport()->size()) {
|
---|
167 | m_image = QImage(viewport()->size(), QImage::Format_ARGB32_Premultiplied);
|
---|
168 | }
|
---|
169 |
|
---|
170 | QPainter imagePainter(&m_image);
|
---|
171 | QGraphicsView::render(&imagePainter);
|
---|
172 | imagePainter.end();
|
---|
173 |
|
---|
174 | QPainter p(viewport());
|
---|
175 | p.drawImage(0, 0, m_image);
|
---|
176 |
|
---|
177 | } else {
|
---|
178 | QGraphicsView::paintEvent(event);
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | void SvgView::wheelEvent(QWheelEvent *event)
|
---|
183 | {
|
---|
184 | qreal factor = qPow(1.2, event->delta() / 240.0);
|
---|
185 | scale(factor, factor);
|
---|
186 | event->accept();
|
---|
187 | }
|
---|
188 |
|
---|