|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the examples of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 #include "svgview.h" |
|
42 |
|
43 #include <QFile> |
|
44 #include <QWheelEvent> |
|
45 #include <QMouseEvent> |
|
46 #include <QGraphicsRectItem> |
|
47 #include <QGraphicsSvgItem> |
|
48 #include <QPaintEvent> |
|
49 #include <qmath.h> |
|
50 |
|
51 #ifndef QT_NO_OPENGL |
|
52 #include <QGLWidget> |
|
53 #endif |
|
54 |
|
55 SvgView::SvgView(QWidget *parent) |
|
56 : QGraphicsView(parent) |
|
57 , m_renderer(Native) |
|
58 , m_svgItem(0) |
|
59 , m_backgroundItem(0) |
|
60 , m_outlineItem(0) |
|
61 { |
|
62 setScene(new QGraphicsScene(this)); |
|
63 setTransformationAnchor(AnchorUnderMouse); |
|
64 setDragMode(ScrollHandDrag); |
|
65 setViewportUpdateMode(FullViewportUpdate); |
|
66 |
|
67 // Prepare background check-board pattern |
|
68 QPixmap tilePixmap(64, 64); |
|
69 tilePixmap.fill(Qt::white); |
|
70 QPainter tilePainter(&tilePixmap); |
|
71 QColor color(220, 220, 220); |
|
72 tilePainter.fillRect(0, 0, 32, 32, color); |
|
73 tilePainter.fillRect(32, 32, 32, 32, color); |
|
74 tilePainter.end(); |
|
75 |
|
76 setBackgroundBrush(tilePixmap); |
|
77 } |
|
78 |
|
79 void SvgView::drawBackground(QPainter *p, const QRectF &) |
|
80 { |
|
81 p->save(); |
|
82 p->resetTransform(); |
|
83 p->drawTiledPixmap(viewport()->rect(), backgroundBrush().texture()); |
|
84 p->restore(); |
|
85 } |
|
86 |
|
87 void SvgView::openFile(const QFile &file) |
|
88 { |
|
89 if (!file.exists()) |
|
90 return; |
|
91 |
|
92 QGraphicsScene *s = scene(); |
|
93 |
|
94 bool drawBackground = (m_backgroundItem ? m_backgroundItem->isVisible() : false); |
|
95 bool drawOutline = (m_outlineItem ? m_outlineItem->isVisible() : true); |
|
96 |
|
97 s->clear(); |
|
98 resetTransform(); |
|
99 |
|
100 m_svgItem = new QGraphicsSvgItem(file.fileName()); |
|
101 m_svgItem->setFlags(QGraphicsItem::ItemClipsToShape); |
|
102 m_svgItem->setCacheMode(QGraphicsItem::NoCache); |
|
103 m_svgItem->setZValue(0); |
|
104 |
|
105 m_backgroundItem = new QGraphicsRectItem(m_svgItem->boundingRect()); |
|
106 m_backgroundItem->setBrush(Qt::white); |
|
107 m_backgroundItem->setPen(Qt::NoPen); |
|
108 m_backgroundItem->setVisible(drawBackground); |
|
109 m_backgroundItem->setZValue(-1); |
|
110 |
|
111 m_outlineItem = new QGraphicsRectItem(m_svgItem->boundingRect()); |
|
112 QPen outline(Qt::black, 2, Qt::DashLine); |
|
113 outline.setCosmetic(true); |
|
114 m_outlineItem->setPen(outline); |
|
115 m_outlineItem->setBrush(Qt::NoBrush); |
|
116 m_outlineItem->setVisible(drawOutline); |
|
117 m_outlineItem->setZValue(1); |
|
118 |
|
119 s->addItem(m_backgroundItem); |
|
120 s->addItem(m_svgItem); |
|
121 s->addItem(m_outlineItem); |
|
122 |
|
123 s->setSceneRect(m_outlineItem->boundingRect().adjusted(-10, -10, 10, 10)); |
|
124 } |
|
125 |
|
126 void SvgView::setRenderer(RendererType type) |
|
127 { |
|
128 m_renderer = type; |
|
129 |
|
130 if (m_renderer == OpenGL) { |
|
131 #ifndef QT_NO_OPENGL |
|
132 setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers))); |
|
133 #endif |
|
134 } else { |
|
135 setViewport(new QWidget); |
|
136 } |
|
137 } |
|
138 |
|
139 void SvgView::setHighQualityAntialiasing(bool highQualityAntialiasing) |
|
140 { |
|
141 #ifndef QT_NO_OPENGL |
|
142 setRenderHint(QPainter::HighQualityAntialiasing, highQualityAntialiasing); |
|
143 #else |
|
144 Q_UNUSED(highQualityAntialiasing); |
|
145 #endif |
|
146 } |
|
147 |
|
148 void SvgView::setViewBackground(bool enable) |
|
149 { |
|
150 if (!m_backgroundItem) |
|
151 return; |
|
152 |
|
153 m_backgroundItem->setVisible(enable); |
|
154 } |
|
155 |
|
156 void SvgView::setViewOutline(bool enable) |
|
157 { |
|
158 if (!m_outlineItem) |
|
159 return; |
|
160 |
|
161 m_outlineItem->setVisible(enable); |
|
162 } |
|
163 |
|
164 void SvgView::paintEvent(QPaintEvent *event) |
|
165 { |
|
166 if (m_renderer == Image) { |
|
167 if (m_image.size() != viewport()->size()) { |
|
168 m_image = QImage(viewport()->size(), QImage::Format_ARGB32_Premultiplied); |
|
169 } |
|
170 |
|
171 QPainter imagePainter(&m_image); |
|
172 QGraphicsView::render(&imagePainter); |
|
173 imagePainter.end(); |
|
174 |
|
175 QPainter p(viewport()); |
|
176 p.drawImage(0, 0, m_image); |
|
177 |
|
178 } else { |
|
179 QGraphicsView::paintEvent(event); |
|
180 } |
|
181 } |
|
182 |
|
183 void SvgView::wheelEvent(QWheelEvent *event) |
|
184 { |
|
185 qreal factor = qPow(1.2, event->delta() / 240.0); |
|
186 scale(factor, factor); |
|
187 event->accept(); |
|
188 } |
|
189 |