qwt_plot_svgitem.cpp 6.64 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#include <qglobal.h>

#include <qpainter.h>
#if QT_VERSION >= 0x040100
#include <qsvgrenderer.h>
#else
#include <qbuffer.h>
#include <qpicture.h>
#endif
#if QT_VERSION < 0x040000
#include <qpaintdevicemetrics.h>
#endif
#include "qwt_scale_map.h"
#include "qwt_legend.h"
#include "qwt_legend_item.h"
#include "qwt_plot_svgitem.h"

class QwtPlotSvgItem::PrivateData
{
public:
30
    PrivateData() {
pixhawk's avatar
pixhawk committed
31 32 33 34 35 36 37 38 39 40 41 42
    }

    QwtDoubleRect boundingRect;
#if QT_VERSION >= 0x040100
    QSvgRenderer renderer;
#else
    QPicture picture;
#endif
};

/*!
   \brief Constructor
43

pixhawk's avatar
pixhawk committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57
   Sets the following item attributes:
   - QwtPlotItem::AutoScale: true
   - QwtPlotItem::Legend:    false

   \param title Title
*/
QwtPlotSvgItem::QwtPlotSvgItem(const QString& title):
    QwtPlotItem(QwtText(title))
{
    init();
}

/*!
   \brief Constructor
58

pixhawk's avatar
pixhawk committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
   Sets the following item attributes:
   - QwtPlotItem::AutoScale: true
   - QwtPlotItem::Legend:    false

   \param title Title
*/
QwtPlotSvgItem::QwtPlotSvgItem(const QwtText& title):
    QwtPlotItem(title)
{
    init();
}

//! Destructor
QwtPlotSvgItem::~QwtPlotSvgItem()
{
    delete d_data;
}

void QwtPlotSvgItem::init()
{
    d_data = new PrivateData();

    setItemAttribute(QwtPlotItem::AutoScale, true);
    setItemAttribute(QwtPlotItem::Legend, false);

    setZ(8.0);
}

//! \return QwtPlotItem::Rtti_PlotSVG
int QwtPlotSvgItem::rtti() const
{
    return QwtPlotItem::Rtti_PlotSVG;
}

/*!
   Load a SVG file

   \param rect Bounding rectangle
   \param fileName SVG file name

   \return true, if the SVG file could be loaded
*/
101 102
bool QwtPlotSvgItem::loadFile(const QwtDoubleRect &rect,
                              const QString &fileName)
pixhawk's avatar
pixhawk committed
103 104 105 106 107 108 109 110 111 112 113 114
{
    d_data->boundingRect = rect;
#if QT_VERSION >= 0x040100
    const bool ok = d_data->renderer.load(fileName);
#else
    const bool ok = d_data->picture.load(fileName, "svg");
#endif
    itemChanged();
    return ok;
}

/*!
115
   Load SVG data
pixhawk's avatar
pixhawk committed
116 117 118 119 120 121

   \param rect Bounding rectangle
   \param data in SVG format

   \return true, if the SVG data could be loaded
*/
122 123
bool QwtPlotSvgItem::loadData(const QwtDoubleRect &rect,
                              const QByteArray &data)
pixhawk's avatar
pixhawk committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
{
    d_data->boundingRect = rect;
#if QT_VERSION >= 0x040100
    const bool ok = d_data->renderer.load(data);
#else
#if QT_VERSION >= 0x040000
    QBuffer buffer(&(QByteArray&)data);
#else
    QBuffer buffer(data);
#endif
    const bool ok = d_data->picture.load(&buffer, "svg");
#endif
    itemChanged();
    return ok;
}

//! Bounding rect of the item
QwtDoubleRect QwtPlotSvgItem::boundingRect() const
{
    return d_data->boundingRect;
}

#if QT_VERSION >= 0x040100

//! \return Renderer used to render the SVG data
const QSvgRenderer &QwtPlotSvgItem::renderer() const
{
    return d_data->renderer;
}

//! \return Renderer used to render the SVG data
QSvgRenderer &QwtPlotSvgItem::renderer()
{
    return d_data->renderer;
}
#endif

/*!
  Draw the SVG item

  \param painter Painter
  \param xMap X-Scale Map
  \param yMap Y-Scale Map
  \param canvasRect Contents rect of the plot canvas
*/
void QwtPlotSvgItem::draw(QPainter *painter,
170 171
                          const QwtScaleMap &xMap, const QwtScaleMap &yMap,
                          const QRect &canvasRect) const
pixhawk's avatar
pixhawk committed
172 173 174
{
    const QwtDoubleRect cRect = invTransform(xMap, yMap, canvasRect);
    const QwtDoubleRect bRect = boundingRect();
175
    if ( bRect.isValid() && cRect.isValid() ) {
pixhawk's avatar
pixhawk committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
        QwtDoubleRect rect = bRect;
        if ( bRect.contains(cRect) )
            rect = cRect;

        const QRect r = transform(xMap, yMap, rect);
        render(painter, viewBox(rect), r);
    }
}

/*!
  Render the SVG data

  \param painter Painter
  \param viewBox View Box, see QSvgRenderer::viewBox
  \param rect Traget rectangle on the paint device
*/
void QwtPlotSvgItem::render(QPainter *painter,
193
                            const QwtDoubleRect &viewBox, const QRect &rect) const
pixhawk's avatar
pixhawk committed
194 195 196 197 198 199 200 201 202 203 204 205
{
    if ( !viewBox.isValid() )
        return;

#if QT_VERSION >= 0x040200
    d_data->renderer.setViewBox(viewBox);
    d_data->renderer.render(painter, rect);
    return;
#else

#if QT_VERSION >= 0x040100
    const QSize paintSize(painter->window().width(),
206
                          painter->window().height());
pixhawk's avatar
pixhawk committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    if ( !paintSize.isValid() )
        return;

    const double xRatio = paintSize.width() / viewBox.width();
    const double yRatio = paintSize.height() / viewBox.height();

    const double dx = rect.left() / xRatio + 1.0;
    const double dy = rect.top() / yRatio + 1.0;

    const double mx = double(rect.width()) / paintSize.width();
    const double my = double(rect.height()) / paintSize.height();

    painter->save();

    painter->translate(dx, dy);
    painter->scale(mx, my);

    d_data->renderer.setViewBox(viewBox.toRect());
    d_data->renderer.render(painter);

    painter->restore();
#else
    const double mx = rect.width() / viewBox.width();
    const double my = rect.height() / viewBox.height();
    const double dx = rect.x() - mx * viewBox.x();
    const double dy = rect.y() - my * viewBox.y();

    painter->save();

    painter->translate(dx, dy);
    painter->scale(mx, my);
238

pixhawk's avatar
pixhawk committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    d_data->picture.play(painter);

    painter->restore();
#endif // < 0x040100
#endif // < 0x040200
}

/*!
  Calculate the viewBox from an rect and boundingRect().

  \param rect Rectangle in scale coordinates
  \return viewBox View Box, see QSvgRenderer::viewBox
*/
QwtDoubleRect QwtPlotSvgItem::viewBox(const QwtDoubleRect &rect) const
{
#if QT_VERSION >= 0x040100
    const QSize sz = d_data->renderer.defaultSize();
#else
#if QT_VERSION > 0x040000
258 259
    const QSize sz(d_data->picture.width(),
                   d_data->picture.height());
pixhawk's avatar
pixhawk committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
#else
    QPaintDeviceMetrics metrics(&d_data->picture);
    const QSize sz(metrics.width(), metrics.height());
#endif
#endif
    const QwtDoubleRect br = boundingRect();

    if ( !rect.isValid() || !br.isValid() || sz.isNull() )
        return QwtDoubleRect();

    QwtScaleMap xMap;
    xMap.setScaleInterval(br.left(), br.right());
    xMap.setPaintInterval(0, sz.width());

    QwtScaleMap yMap;
    yMap.setScaleInterval(br.top(), br.bottom());
    yMap.setPaintInterval(sz.height(), 0);

    const double x1 = xMap.xTransform(rect.left());
    const double x2 = xMap.xTransform(rect.right());
    const double y1 = yMap.xTransform(rect.bottom());
    const double y2 = yMap.xTransform(rect.top());

    return QwtDoubleRect(x1, y1, x2 - x1, y2 - y1);
}