1
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
3
* Copyright (C) 1997 Josef Wilgen
4
* Copyright (C) 2002 Uwe Rathmann
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
*****************************************************************************/
13
#if QT_VERSION >= 0x040100
14
#include <qsvgrenderer.h>
19
#if QT_VERSION < 0x040000
20
#include <qpaintdevicemetrics.h>
22
#include <qwt_scale_map.h>
23
#include <qwt_legend.h>
24
#include <qwt_legend_item.h>
25
#include "qwt_plot_svgitem.h"
27
class QwtPlotSvgItem::PrivateData
34
QwtDoubleRect boundingRect;
35
#if QT_VERSION >= 0x040100
36
QSvgRenderer renderer;
45
Sets the following item attributes:
46
- QwtPlotItem::AutoScale: true
47
- QwtPlotItem::Legend: false
51
QwtPlotSvgItem::QwtPlotSvgItem(const QString& title):
52
QwtPlotItem(QwtText(title))
60
Sets the following item attributes:
61
- QwtPlotItem::AutoScale: true
62
- QwtPlotItem::Legend: false
66
QwtPlotSvgItem::QwtPlotSvgItem(const QwtText& title):
73
QwtPlotSvgItem::~QwtPlotSvgItem()
78
void QwtPlotSvgItem::init()
80
d_data = new PrivateData();
82
setItemAttribute(QwtPlotItem::AutoScale, true);
83
setItemAttribute(QwtPlotItem::Legend, false);
88
//! \return QwtPlotItem::Rtti_PlotSVG
89
int QwtPlotSvgItem::rtti() const
91
return QwtPlotItem::Rtti_PlotSVG;
97
\param rect Bounding rectangle
98
\param fileName SVG file name
100
\return true, if the SVG file could be loaded
102
bool QwtPlotSvgItem::loadFile(const QwtDoubleRect &rect,
103
const QString &fileName)
105
d_data->boundingRect = rect;
106
#if QT_VERSION >= 0x040100
107
const bool ok = d_data->renderer.load(fileName);
109
const bool ok = d_data->picture.load(fileName, "svg");
118
\param rect Bounding rectangle
119
\param data in SVG format
121
\return true, if the SVG data could be loaded
123
bool QwtPlotSvgItem::loadData(const QwtDoubleRect &rect,
124
const QByteArray &data)
126
d_data->boundingRect = rect;
127
#if QT_VERSION >= 0x040100
128
const bool ok = d_data->renderer.load(data);
130
#if QT_VERSION >= 0x040000
131
QBuffer buffer(&(QByteArray&)data);
133
QBuffer buffer(data);
135
const bool ok = d_data->picture.load(&buffer, "svg");
141
//! Bounding rect of the item
142
QwtDoubleRect QwtPlotSvgItem::boundingRect() const
144
return d_data->boundingRect;
147
#if QT_VERSION >= 0x040100
149
//! \return Renderer used to render the SVG data
150
const QSvgRenderer &QwtPlotSvgItem::renderer() const
152
return d_data->renderer;
155
//! \return Renderer used to render the SVG data
156
QSvgRenderer &QwtPlotSvgItem::renderer()
158
return d_data->renderer;
165
\param painter Painter
166
\param xMap X-Scale Map
167
\param yMap Y-Scale Map
168
\param canvasRect Contents rect of the plot canvas
170
void QwtPlotSvgItem::draw(QPainter *painter,
171
const QwtScaleMap &xMap, const QwtScaleMap &yMap,
172
const QRect &canvasRect) const
174
const QwtDoubleRect cRect = invTransform(xMap, yMap, canvasRect);
175
const QwtDoubleRect bRect = boundingRect();
176
if ( bRect.isValid() && cRect.isValid() )
178
QwtDoubleRect rect = bRect;
179
if ( bRect.contains(cRect) )
182
const QRect r = transform(xMap, yMap, rect);
183
render(painter, viewBox(rect), r);
190
\param painter Painter
191
\param viewBox View Box, see QSvgRenderer::viewBox
192
\param rect Traget rectangle on the paint device
194
void QwtPlotSvgItem::render(QPainter *painter,
195
const QwtDoubleRect &viewBox, const QRect &rect) const
197
if ( !viewBox.isValid() )
200
#if QT_VERSION >= 0x040200
201
d_data->renderer.setViewBox(viewBox);
202
d_data->renderer.render(painter, rect);
206
#if QT_VERSION >= 0x040100
207
const QSize paintSize(painter->window().width(),
208
painter->window().height());
209
if ( !paintSize.isValid() )
212
const double xRatio = paintSize.width() / viewBox.width();
213
const double yRatio = paintSize.height() / viewBox.height();
215
const double dx = rect.left() / xRatio + 1.0;
216
const double dy = rect.top() / yRatio + 1.0;
218
const double mx = double(rect.width()) / paintSize.width();
219
const double my = double(rect.height()) / paintSize.height();
223
painter->translate(dx, dy);
224
painter->scale(mx, my);
226
d_data->renderer.setViewBox(viewBox.toRect());
227
d_data->renderer.render(painter);
231
const double mx = rect.width() / viewBox.width();
232
const double my = rect.height() / viewBox.height();
233
const double dx = rect.x() - mx * viewBox.x();
234
const double dy = rect.y() - my * viewBox.y();
238
painter->translate(dx, dy);
239
painter->scale(mx, my);
241
d_data->picture.play(painter);
249
Calculate the viewBox from an rect and boundingRect().
251
\param rect Rectangle in scale coordinates
252
\return viewBox View Box, see QSvgRenderer::viewBox
254
QwtDoubleRect QwtPlotSvgItem::viewBox(const QwtDoubleRect &rect) const
256
#if QT_VERSION >= 0x040100
257
const QSize sz = d_data->renderer.defaultSize();
259
#if QT_VERSION > 0x040000
260
const QSize sz(d_data->picture.width(),
261
d_data->picture.height());
263
QPaintDeviceMetrics metrics(&d_data->picture);
264
const QSize sz(metrics.width(), metrics.height());
267
const QwtDoubleRect br = boundingRect();
269
if ( !rect.isValid() || !br.isValid() || sz.isNull() )
270
return QwtDoubleRect();
273
xMap.setScaleInterval(br.left(), br.right());
274
xMap.setPaintInterval(0, sz.width());
277
yMap.setScaleInterval(br.top(), br.bottom());
278
yMap.setPaintInterval(sz.height(), 0);
280
const double x1 = xMap.xTransform(rect.left());
281
const double x2 = xMap.xTransform(rect.right());
282
const double y1 = yMap.xTransform(rect.bottom());
283
const double y2 = yMap.xTransform(rect.top());
285
return QwtDoubleRect(x1, y1, x2 - x1, y2 - y1);