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