source: trunk/examples/painting/svgviewer/svgview.cpp@ 117

Last change on this file since 117 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 5.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
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
55SvgView::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
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
78void 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
86void 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
125void 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
138void 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
147void SvgView::setViewBackground(bool enable)
148{
149 if (!m_backgroundItem)
150 return;
151
152 m_backgroundItem->setVisible(enable);
153}
154
155void SvgView::setViewOutline(bool enable)
156{
157 if (!m_outlineItem)
158 return;
159
160 m_outlineItem->setVisible(enable);
161}
162
163void 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
182void SvgView::wheelEvent(QWheelEvent *event)
183{
184 qreal factor = qPow(1.2, event->delta() / 240.0);
185 scale(factor, factor);
186 event->accept();
187}
188
Note: See TracBrowser for help on using the repository browser.