<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>svgview.cpp Example File | Qt SVG 5.11</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td ><a href="../qtdoc/index.html">Qt 5.11</a></td><td ><a href="qtsvg-index.html">Qt SVG</a></td><td ><a href="qtsvg-svgviewer-example.html">SVG Viewer Example</a></td><td >svgview.cpp Example File</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right">Qt 5.11.2 Reference Documentation</td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">svgview.cpp Example File</h1> <span class="subtitle">svgviewer/svgview.cpp</span> <!-- $$$svgviewer/svgview.cpp-description --> <div class="descr"> <a name="details"></a> <pre class="cpp"> <span class="comment">/**************************************************************************** ** ** 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$ ** ****************************************************************************/</span> <span class="preprocessor">#include "svgview.h"</span> <span class="preprocessor">#include <QSvgRenderer></span> <span class="preprocessor">#include <QWheelEvent></span> <span class="preprocessor">#include <QMouseEvent></span> <span class="preprocessor">#include <QGraphicsRectItem></span> <span class="preprocessor">#include <QGraphicsSvgItem></span> <span class="preprocessor">#include <QPaintEvent></span> <span class="preprocessor">#include <qmath.h></span> <span class="preprocessor">#ifndef QT_NO_OPENGL</span> <span class="preprocessor">#include <QGLWidget></span> <span class="preprocessor">#endif</span> SvgView<span class="operator">::</span>SvgView(<span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span> <span class="operator">*</span>parent) : <span class="type"><a href="../qtwidgets/qgraphicsview.html">QGraphicsView</a></span>(parent) <span class="operator">,</span> m_renderer(Native) <span class="operator">,</span> m_svgItem(nullptr) <span class="operator">,</span> m_backgroundItem(nullptr) <span class="operator">,</span> m_outlineItem(nullptr) { setScene(<span class="keyword">new</span> <span class="type"><a href="../qtwidgets/qgraphicsscene.html">QGraphicsScene</a></span>(<span class="keyword">this</span>)); setTransformationAnchor(AnchorUnderMouse); setDragMode(ScrollHandDrag); setViewportUpdateMode(FullViewportUpdate); <span class="comment">// Prepare background check-board pattern</span> <span class="type"><a href="../qtgui/qpixmap.html">QPixmap</a></span> tilePixmap(<span class="number">64</span><span class="operator">,</span> <span class="number">64</span>); tilePixmap<span class="operator">.</span>fill(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>white); <span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> tilePainter(<span class="operator">&</span>tilePixmap); <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> color(<span class="number">220</span><span class="operator">,</span> <span class="number">220</span><span class="operator">,</span> <span class="number">220</span>); tilePainter<span class="operator">.</span>fillRect(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">32</span><span class="operator">,</span> <span class="number">32</span><span class="operator">,</span> color); tilePainter<span class="operator">.</span>fillRect(<span class="number">32</span><span class="operator">,</span> <span class="number">32</span><span class="operator">,</span> <span class="number">32</span><span class="operator">,</span> <span class="number">32</span><span class="operator">,</span> color); tilePainter<span class="operator">.</span>end(); setBackgroundBrush(tilePixmap); } <span class="type">void</span> SvgView<span class="operator">::</span>drawBackground(<span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> <span class="operator">*</span>p<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qrectf.html">QRectF</a></span> <span class="operator">&</span>) { p<span class="operator">-</span><span class="operator">></span>save(); p<span class="operator">-</span><span class="operator">></span>resetTransform(); p<span class="operator">-</span><span class="operator">></span>drawTiledPixmap(viewport()<span class="operator">-</span><span class="operator">></span>rect()<span class="operator">,</span> backgroundBrush()<span class="operator">.</span>texture()); p<span class="operator">-</span><span class="operator">></span>restore(); } <span class="type"><a href="../qtcore/qsize.html">QSize</a></span> SvgView<span class="operator">::</span>svgSize() <span class="keyword">const</span> { <span class="keyword">return</span> m_svgItem <span class="operator">?</span> m_svgItem<span class="operator">-</span><span class="operator">></span>boundingRect()<span class="operator">.</span>size()<span class="operator">.</span>toSize() : <span class="type"><a href="../qtcore/qsize.html">QSize</a></span>(); } bool SvgView<span class="operator">::</span>openFile(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&</span>fileName) { <span class="type"><a href="../qtwidgets/qgraphicsscene.html">QGraphicsScene</a></span> <span class="operator">*</span>s <span class="operator">=</span> scene(); <span class="keyword">const</span> bool drawBackground <span class="operator">=</span> (m_backgroundItem <span class="operator">?</span> m_backgroundItem<span class="operator">-</span><span class="operator">></span>isVisible() : <span class="keyword">false</span>); <span class="keyword">const</span> bool drawOutline <span class="operator">=</span> (m_outlineItem <span class="operator">?</span> m_outlineItem<span class="operator">-</span><span class="operator">></span>isVisible() : <span class="keyword">true</span>); <span class="type"><a href="../qtcore/qscopedpointer.html">QScopedPointer</a></span><span class="operator"><</span><span class="type"><a href="qgraphicssvgitem.html">QGraphicsSvgItem</a></span><span class="operator">></span> svgItem(<span class="keyword">new</span> <span class="type"><a href="qgraphicssvgitem.html">QGraphicsSvgItem</a></span>(fileName)); <span class="keyword">if</span> (<span class="operator">!</span>svgItem<span class="operator">-</span><span class="operator">></span>renderer()<span class="operator">-</span><span class="operator">></span>isValid()) <span class="keyword">return</span> <span class="keyword">false</span>; s<span class="operator">-</span><span class="operator">></span>clear(); resetTransform(); m_svgItem <span class="operator">=</span> svgItem<span class="operator">.</span>take(); m_svgItem<span class="operator">-</span><span class="operator">></span>setFlags(<span class="type"><a href="../qtwidgets/qgraphicsitem.html">QGraphicsItem</a></span><span class="operator">::</span>ItemClipsToShape); m_svgItem<span class="operator">-</span><span class="operator">></span>setCacheMode(<span class="type"><a href="../qtwidgets/qgraphicsitem.html">QGraphicsItem</a></span><span class="operator">::</span>NoCache); m_svgItem<span class="operator">-</span><span class="operator">></span>setZValue(<span class="number">0</span>); m_backgroundItem <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtwidgets/qgraphicsrectitem.html">QGraphicsRectItem</a></span>(m_svgItem<span class="operator">-</span><span class="operator">></span>boundingRect()); m_backgroundItem<span class="operator">-</span><span class="operator">></span>setBrush(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>white); m_backgroundItem<span class="operator">-</span><span class="operator">></span>setPen(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>NoPen); m_backgroundItem<span class="operator">-</span><span class="operator">></span>setVisible(drawBackground); m_backgroundItem<span class="operator">-</span><span class="operator">></span>setZValue(<span class="operator">-</span><span class="number">1</span>); m_outlineItem <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtwidgets/qgraphicsrectitem.html">QGraphicsRectItem</a></span>(m_svgItem<span class="operator">-</span><span class="operator">></span>boundingRect()); <span class="type"><a href="../qtgui/qpen.html">QPen</a></span> outline(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>black<span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>DashLine); outline<span class="operator">.</span>setCosmetic(<span class="keyword">true</span>); m_outlineItem<span class="operator">-</span><span class="operator">></span>setPen(outline); m_outlineItem<span class="operator">-</span><span class="operator">></span>setBrush(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>NoBrush); m_outlineItem<span class="operator">-</span><span class="operator">></span>setVisible(drawOutline); m_outlineItem<span class="operator">-</span><span class="operator">></span>setZValue(<span class="number">1</span>); s<span class="operator">-</span><span class="operator">></span>addItem(m_backgroundItem); s<span class="operator">-</span><span class="operator">></span>addItem(m_svgItem); s<span class="operator">-</span><span class="operator">></span>addItem(m_outlineItem); s<span class="operator">-</span><span class="operator">></span>setSceneRect(m_outlineItem<span class="operator">-</span><span class="operator">></span>boundingRect()<span class="operator">.</span>adjusted(<span class="operator">-</span><span class="number">10</span><span class="operator">,</span> <span class="operator">-</span><span class="number">10</span><span class="operator">,</span> <span class="number">10</span><span class="operator">,</span> <span class="number">10</span>)); <span class="keyword">return</span> <span class="keyword">true</span>; } <span class="type">void</span> SvgView<span class="operator">::</span>setRenderer(RendererType type) { m_renderer <span class="operator">=</span> type; <span class="keyword">if</span> (m_renderer <span class="operator">=</span><span class="operator">=</span> OpenGL) { <span class="preprocessor">#ifndef QT_NO_OPENGL</span> setViewport(<span class="keyword">new</span> <span class="type">QGLWidget</span>(<span class="type">QGLFormat</span>(QGL<span class="operator">::</span>SampleBuffers))); <span class="preprocessor">#endif</span> } <span class="keyword">else</span> { setViewport(<span class="keyword">new</span> <span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span>); } } <span class="type">void</span> SvgView<span class="operator">::</span>setHighQualityAntialiasing(bool highQualityAntialiasing) { <span class="preprocessor">#ifndef QT_NO_OPENGL</span> setRenderHint(<span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span><span class="operator">::</span>HighQualityAntialiasing<span class="operator">,</span> highQualityAntialiasing); <span class="preprocessor">#else</span> Q_UNUSED(highQualityAntialiasing); <span class="preprocessor">#endif</span> } <span class="type">void</span> SvgView<span class="operator">::</span>setViewBackground(bool enable) { <span class="keyword">if</span> (<span class="operator">!</span>m_backgroundItem) <span class="keyword">return</span>; m_backgroundItem<span class="operator">-</span><span class="operator">></span>setVisible(enable); } <span class="type">void</span> SvgView<span class="operator">::</span>setViewOutline(bool enable) { <span class="keyword">if</span> (<span class="operator">!</span>m_outlineItem) <span class="keyword">return</span>; m_outlineItem<span class="operator">-</span><span class="operator">></span>setVisible(enable); } <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> SvgView<span class="operator">::</span>zoomFactor() <span class="keyword">const</span> { <span class="keyword">return</span> transform()<span class="operator">.</span>m11(); } <span class="type">void</span> SvgView<span class="operator">::</span>zoomIn() { zoomBy(<span class="number">2</span>); } <span class="type">void</span> SvgView<span class="operator">::</span>zoomOut() { zoomBy(<span class="number">0.5</span>); } <span class="type">void</span> SvgView<span class="operator">::</span>resetZoom() { <span class="keyword">if</span> (<span class="operator">!</span><a href="../qtcore/qfloat16.html#qFuzzyCompare">qFuzzyCompare</a>(zoomFactor()<span class="operator">,</span> <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span>(<span class="number">1</span>))) { resetTransform(); <span class="keyword">emit</span> zoomChanged(); } } <span class="type">void</span> SvgView<span class="operator">::</span>paintEvent(<span class="type"><a href="../qtgui/qpaintevent.html">QPaintEvent</a></span> <span class="operator">*</span>event) { <span class="keyword">if</span> (m_renderer <span class="operator">=</span><span class="operator">=</span> Image) { <span class="keyword">if</span> (m_image<span class="operator">.</span>size() <span class="operator">!</span><span class="operator">=</span> viewport()<span class="operator">-</span><span class="operator">></span>size()) { m_image <span class="operator">=</span> <span class="type"><a href="../qtgui/qimage.html">QImage</a></span>(viewport()<span class="operator">-</span><span class="operator">></span>size()<span class="operator">,</span> <span class="type"><a href="../qtgui/qimage.html">QImage</a></span><span class="operator">::</span>Format_ARGB32_Premultiplied); } <span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> imagePainter(<span class="operator">&</span>m_image); <span class="type"><a href="../qtwidgets/qgraphicsview.html">QGraphicsView</a></span><span class="operator">::</span>render(<span class="operator">&</span>imagePainter); imagePainter<span class="operator">.</span>end(); <span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> p(viewport()); p<span class="operator">.</span>drawImage(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> m_image); } <span class="keyword">else</span> { <span class="type"><a href="../qtwidgets/qgraphicsview.html">QGraphicsView</a></span><span class="operator">::</span>paintEvent(event); } } <span class="type">void</span> SvgView<span class="operator">::</span>wheelEvent(<span class="type"><a href="../qtgui/qwheelevent.html">QWheelEvent</a></span> <span class="operator">*</span>event) { zoomBy(<a href="../qtcore/qtmath.html#qPow">qPow</a>(<span class="number">1.2</span><span class="operator">,</span> event<span class="operator">-</span><span class="operator">></span>delta() <span class="operator">/</span> <span class="number">240.0</span>)); } <span class="type">void</span> SvgView<span class="operator">::</span>zoomBy(<span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> factor) { <span class="keyword">const</span> <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> currentZoom <span class="operator">=</span> zoomFactor(); <span class="keyword">if</span> ((factor <span class="operator"><</span> <span class="number">1</span> <span class="operator">&</span><span class="operator">&</span> currentZoom <span class="operator"><</span> <span class="number">0.1</span>) <span class="operator">|</span><span class="operator">|</span> (factor <span class="operator">></span> <span class="number">1</span> <span class="operator">&</span><span class="operator">&</span> currentZoom <span class="operator">></span> <span class="number">10</span>)) <span class="keyword">return</span>; scale(factor<span class="operator">,</span> factor); <span class="keyword">emit</span> zoomChanged(); } <span class="type"><a href="qsvgrenderer.html">QSvgRenderer</a></span> <span class="operator">*</span>SvgView<span class="operator">::</span>renderer() <span class="keyword">const</span> { <span class="keyword">if</span> (m_svgItem) <span class="keyword">return</span> m_svgItem<span class="operator">-</span><span class="operator">></span>renderer(); <span class="keyword">return</span> nullptr; } </pre> </div> <!-- @@@svgviewer/svgview.cpp --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2018 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br/> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br/> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. </p> </div> </body> </html>