1 | /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
|
---|
2 | * Qwt Widget Library
|
---|
3 | * Copyright (C) 1997 Josef Wilgen
|
---|
4 | * Copyright (C) 2002 Uwe Rathmann
|
---|
5 | *
|
---|
6 | * This library is free software; you can redistribute it and/or
|
---|
7 | * modify it under the terms of the Qwt License, Version 1.0
|
---|
8 | *****************************************************************************/
|
---|
9 |
|
---|
10 | #include "qwt_plot_svgitem.h"
|
---|
11 | #include "qwt_scale_map.h"
|
---|
12 | #include "qwt_painter.h"
|
---|
13 | #include <qpainter.h>
|
---|
14 | #include <qsvgrenderer.h>
|
---|
15 |
|
---|
16 | class QwtPlotSvgItem::PrivateData
|
---|
17 | {
|
---|
18 | public:
|
---|
19 | PrivateData()
|
---|
20 | {
|
---|
21 | }
|
---|
22 |
|
---|
23 | QRectF boundingRect;
|
---|
24 | QSvgRenderer renderer;
|
---|
25 | };
|
---|
26 |
|
---|
27 | /*!
|
---|
28 | \brief Constructor
|
---|
29 |
|
---|
30 | Sets the following item attributes:
|
---|
31 | - QwtPlotItem::AutoScale: true
|
---|
32 | - QwtPlotItem::Legend: false
|
---|
33 |
|
---|
34 | \param title Title
|
---|
35 | */
|
---|
36 | QwtPlotSvgItem::QwtPlotSvgItem( const QString& title ):
|
---|
37 | QwtPlotItem( QwtText( title ) )
|
---|
38 | {
|
---|
39 | init();
|
---|
40 | }
|
---|
41 |
|
---|
42 | /*!
|
---|
43 | \brief Constructor
|
---|
44 |
|
---|
45 | Sets the following item attributes:
|
---|
46 | - QwtPlotItem::AutoScale: true
|
---|
47 | - QwtPlotItem::Legend: false
|
---|
48 |
|
---|
49 | \param title Title
|
---|
50 | */
|
---|
51 | QwtPlotSvgItem::QwtPlotSvgItem( const QwtText& title ):
|
---|
52 | QwtPlotItem( title )
|
---|
53 | {
|
---|
54 | init();
|
---|
55 | }
|
---|
56 |
|
---|
57 | //! Destructor
|
---|
58 | QwtPlotSvgItem::~QwtPlotSvgItem()
|
---|
59 | {
|
---|
60 | delete d_data;
|
---|
61 | }
|
---|
62 |
|
---|
63 | void QwtPlotSvgItem::init()
|
---|
64 | {
|
---|
65 | d_data = new PrivateData();
|
---|
66 | d_data->boundingRect = QwtPlotItem::boundingRect();
|
---|
67 |
|
---|
68 | setItemAttribute( QwtPlotItem::AutoScale, true );
|
---|
69 | setItemAttribute( QwtPlotItem::Legend, false );
|
---|
70 |
|
---|
71 | setZ( 8.0 );
|
---|
72 | }
|
---|
73 |
|
---|
74 | //! \return QwtPlotItem::Rtti_PlotSVG
|
---|
75 | int QwtPlotSvgItem::rtti() const
|
---|
76 | {
|
---|
77 | return QwtPlotItem::Rtti_PlotSVG;
|
---|
78 | }
|
---|
79 |
|
---|
80 | /*!
|
---|
81 | Load a SVG file
|
---|
82 |
|
---|
83 | \param rect Bounding rectangle
|
---|
84 | \param fileName SVG file name
|
---|
85 |
|
---|
86 | \return true, if the SVG file could be loaded
|
---|
87 | */
|
---|
88 | bool QwtPlotSvgItem::loadFile( const QRectF &rect,
|
---|
89 | const QString &fileName )
|
---|
90 | {
|
---|
91 | d_data->boundingRect = rect;
|
---|
92 | const bool ok = d_data->renderer.load( fileName );
|
---|
93 |
|
---|
94 | legendChanged();
|
---|
95 | itemChanged();
|
---|
96 |
|
---|
97 | return ok;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /*!
|
---|
101 | Load SVG data
|
---|
102 |
|
---|
103 | \param rect Bounding rectangle
|
---|
104 | \param data in SVG format
|
---|
105 |
|
---|
106 | \return true, if the SVG data could be loaded
|
---|
107 | */
|
---|
108 | bool QwtPlotSvgItem::loadData( const QRectF &rect,
|
---|
109 | const QByteArray &data )
|
---|
110 | {
|
---|
111 | d_data->boundingRect = rect;
|
---|
112 | const bool ok = d_data->renderer.load( data );
|
---|
113 |
|
---|
114 | legendChanged();
|
---|
115 | itemChanged();
|
---|
116 |
|
---|
117 | return ok;
|
---|
118 | }
|
---|
119 |
|
---|
120 | //! Bounding rectangle of the item
|
---|
121 | QRectF QwtPlotSvgItem::boundingRect() const
|
---|
122 | {
|
---|
123 | return d_data->boundingRect;
|
---|
124 | }
|
---|
125 |
|
---|
126 | //! \return Renderer used to render the SVG data
|
---|
127 | const QSvgRenderer &QwtPlotSvgItem::renderer() const
|
---|
128 | {
|
---|
129 | return d_data->renderer;
|
---|
130 | }
|
---|
131 |
|
---|
132 | //! \return Renderer used to render the SVG data
|
---|
133 | QSvgRenderer &QwtPlotSvgItem::renderer()
|
---|
134 | {
|
---|
135 | return d_data->renderer;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /*!
|
---|
139 | Draw the SVG item
|
---|
140 |
|
---|
141 | \param painter Painter
|
---|
142 | \param xMap X-Scale Map
|
---|
143 | \param yMap Y-Scale Map
|
---|
144 | \param canvasRect Contents rect of the plot canvas
|
---|
145 | */
|
---|
146 | void QwtPlotSvgItem::draw( QPainter *painter,
|
---|
147 | const QwtScaleMap &xMap, const QwtScaleMap &yMap,
|
---|
148 | const QRectF &canvasRect ) const
|
---|
149 | {
|
---|
150 | const QRectF cRect = QwtScaleMap::invTransform(
|
---|
151 | xMap, yMap, canvasRect.toRect() );
|
---|
152 | const QRectF bRect = boundingRect();
|
---|
153 | if ( bRect.isValid() && cRect.isValid() )
|
---|
154 | {
|
---|
155 | QRectF rect = bRect;
|
---|
156 | if ( bRect.contains( cRect ) )
|
---|
157 | rect = cRect;
|
---|
158 |
|
---|
159 | const QRectF r = QwtScaleMap::transform( xMap, yMap, rect );
|
---|
160 | render( painter, viewBox( rect ), r );
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | /*!
|
---|
165 | Render the SVG data
|
---|
166 |
|
---|
167 | \param painter Painter
|
---|
168 | \param viewBox View Box, see QSvgRenderer::viewBox()
|
---|
169 | \param rect Target rectangle on the paint device
|
---|
170 | */
|
---|
171 | void QwtPlotSvgItem::render( QPainter *painter,
|
---|
172 | const QRectF &viewBox, const QRectF &rect ) const
|
---|
173 | {
|
---|
174 | if ( !viewBox.isValid() )
|
---|
175 | return;
|
---|
176 |
|
---|
177 | QRectF r = rect;
|
---|
178 |
|
---|
179 | if ( QwtPainter::roundingAlignment( painter ) )
|
---|
180 | {
|
---|
181 | r.setLeft ( qRound( r.left() ) );
|
---|
182 | r.setRight ( qRound( r.right() ) );
|
---|
183 | r.setTop ( qRound( r.top() ) );
|
---|
184 | r.setBottom ( qRound( r.bottom() ) );
|
---|
185 | }
|
---|
186 |
|
---|
187 | d_data->renderer.setViewBox( viewBox );
|
---|
188 | d_data->renderer.render( painter, r );
|
---|
189 | }
|
---|
190 |
|
---|
191 | /*!
|
---|
192 | Calculate the view box from rect and boundingRect().
|
---|
193 |
|
---|
194 | \param rect Rectangle in scale coordinates
|
---|
195 | \return View box, see QSvgRenderer::viewBox()
|
---|
196 | */
|
---|
197 | QRectF QwtPlotSvgItem::viewBox( const QRectF &rect ) const
|
---|
198 | {
|
---|
199 | const QSize sz = d_data->renderer.defaultSize();
|
---|
200 | const QRectF br = boundingRect();
|
---|
201 |
|
---|
202 | if ( !rect.isValid() || !br.isValid() || sz.isNull() )
|
---|
203 | return QRectF();
|
---|
204 |
|
---|
205 | QwtScaleMap xMap;
|
---|
206 | xMap.setScaleInterval( br.left(), br.right() );
|
---|
207 | xMap.setPaintInterval( 0, sz.width() );
|
---|
208 |
|
---|
209 | QwtScaleMap yMap;
|
---|
210 | yMap.setScaleInterval( br.top(), br.bottom() );
|
---|
211 | yMap.setPaintInterval( sz.height(), 0 );
|
---|
212 |
|
---|
213 | const double x1 = xMap.transform( rect.left() );
|
---|
214 | const double x2 = xMap.transform( rect.right() );
|
---|
215 | const double y1 = yMap.transform( rect.bottom() );
|
---|
216 | const double y2 = yMap.transform( rect.top() );
|
---|
217 |
|
---|
218 | return QRectF( x1, y1, x2 - x1, y2 - y1 );
|
---|
219 | }
|
---|