SVG Viewer Example▲
Sélectionnez
/**
**************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
***************************************************************************
*/
#include
"svgview.h"
#include <QSvgRenderer>
#include <QWheelEvent>
#include <QMouseEvent>
#include <QGraphicsRectItem>
#include <QGraphicsSvgItem>
#include <QPaintEvent>
#include <qmath.h>
#ifndef QT_NO_OPENGL
#include <QGLWidget>
#endif
SvgView::
SvgView(QWidget *
parent)
:
QGraphicsView(parent)
, m_renderer(Native)
, m_svgItem(nullptr
)
, m_backgroundItem(nullptr
)
, m_outlineItem(nullptr
)
{
setScene(new
QGraphicsScene(this
));
setTransformationAnchor(AnchorUnderMouse);
setDragMode(ScrollHandDrag);
setViewportUpdateMode(FullViewportUpdate);
// Prepare background check-board pattern
QPixmap tilePixmap(64
, 64
);
tilePixmap.fill(Qt::
white);
QPainter tilePainter(&
amp;tilePixmap);
QColor color(220
, 220
, 220
);
tilePainter.fillRect(0
, 0
, 32
, 32
, color);
tilePainter.fillRect(32
, 32
, 32
, 32
, color);
tilePainter.end();
setBackgroundBrush(tilePixmap);
}
void
SvgView::
drawBackground(QPainter *
p, const
QRectF &
amp;)
{
p-&
gt;save();
p-&
gt;resetTransform();
p-&
gt;drawTiledPixmap(viewport()-&
gt;rect(), backgroundBrush().texture());
p-&
gt;restore();
}
QSize SvgView::
svgSize() const
{
return
m_svgItem ? m_svgItem-&
gt;boundingRect().size().toSize() : QSize();
}
bool
SvgView::
openFile(const
QString &
amp;fileName)
{
QGraphicsScene *
s =
scene();
const
bool
drawBackground =
(m_backgroundItem ? m_backgroundItem-&
gt;isVisible() : false
);
const
bool
drawOutline =
(m_outlineItem ? m_outlineItem-&
gt;isVisible() : true
);
QScopedPointer&
lt;QGraphicsSvgItem&
gt; svgItem(new
QGraphicsSvgItem(fileName));
if
(!
svgItem-&
gt;renderer()-&
gt;isValid())
return
false
;
s-&
gt;clear();
resetTransform();
m_svgItem =
svgItem.take();
m_svgItem-&
gt;setFlags(QGraphicsItem::
ItemClipsToShape);
m_svgItem-&
gt;setCacheMode(QGraphicsItem::
NoCache);
m_svgItem-&
gt;setZValue(0
);
m_backgroundItem =
new
QGraphicsRectItem(m_svgItem-&
gt;boundingRect());
m_backgroundItem-&
gt;setBrush(Qt::
white);
m_backgroundItem-&
gt;setPen(Qt::
NoPen);
m_backgroundItem-&
gt;setVisible(drawBackground);
m_backgroundItem-&
gt;setZValue(-
1
);
m_outlineItem =
new
QGraphicsRectItem(m_svgItem-&
gt;boundingRect());
QPen outline(Qt::
black, 2
, Qt::
DashLine);
outline.setCosmetic(true
);
m_outlineItem-&
gt;setPen(outline);
m_outlineItem-&
gt;setBrush(Qt::
NoBrush);
m_outlineItem-&
gt;setVisible(drawOutline);
m_outlineItem-&
gt;setZValue(1
);
s-&
gt;addItem(m_backgroundItem);
s-&
gt;addItem(m_svgItem);
s-&
gt;addItem(m_outlineItem);
s-&
gt;setSceneRect(m_outlineItem-&
gt;boundingRect().adjusted(-
10
, -
10
, 10
, 10
));
return
true
;
}
void
SvgView::
setRenderer(RendererType type)
{
m_renderer =
type;
if
(m_renderer ==
OpenGL) {
#ifndef QT_NO_OPENGL
setViewport(new
QGLWidget(QGLFormat(QGL::
SampleBuffers)));
#endif
}
else
{
setViewport(new
QWidget);
}
}
void
SvgView::
setHighQualityAntialiasing(bool
highQualityAntialiasing)
{
#ifndef QT_NO_OPENGL
setRenderHint(QPainter::
HighQualityAntialiasing, highQualityAntialiasing);
#else
Q_UNUSED(highQualityAntialiasing);
#endif
}
void
SvgView::
setViewBackground(bool
enable)
{
if
(!
m_backgroundItem)
return
;
m_backgroundItem-&
gt;setVisible(enable);
}
void
SvgView::
setViewOutline(bool
enable)
{
if
(!
m_outlineItem)
return
;
m_outlineItem-&
gt;setVisible(enable);
}
qreal SvgView::
zoomFactor() const
{
return
transform().m11();
}
void
SvgView::
zoomIn()
{
zoomBy(2
);
}
void
SvgView::
zoomOut()
{
zoomBy(0.5
);
}
void
SvgView::
resetZoom()
{
if
(!
qFuzzyCompare(zoomFactor(), qreal(1
))) {
resetTransform();
emit zoomChanged();
}
}
void
SvgView::
paintEvent(QPaintEvent *
event)
{
if
(m_renderer ==
Image) {
if
(m_image.size() !=
viewport()-&
gt;size()) {
m_image =
QImage(viewport()-&
gt;size(), QImage::
Format_ARGB32_Premultiplied);
}
QPainter imagePainter(&
amp;m_image);
QGraphicsView::
render(&
amp;imagePainter);
imagePainter.end();
QPainter p(viewport());
p.drawImage(0
, 0
, m_image);
}
else
{
QGraphicsView::
paintEvent(event);
}
}
void
SvgView::
wheelEvent(QWheelEvent *
event)
{
zoomBy(qPow(1.2
, event-&
gt;delta() /
240.0
));
}
void
SvgView::
zoomBy(qreal factor)
{
const
qreal currentZoom =
zoomFactor();
if
((factor &
lt; 1
&
amp;&
amp; currentZoom &
lt; 0.1
) ||
(factor &
gt; 1
&
amp;&
amp; currentZoom &
gt; 10
))
return
;
scale(factor, factor);
emit zoomChanged();
}
QSvgRenderer *
SvgView::
renderer() const
{
if
(m_svgItem)
return
m_svgItem-&
gt;renderer();
return
nullptr
;
}