qwt_plot_svgitem.cpp
Go to the documentation of this file.
00001 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
00002  * Qwt Widget Library
00003  * Copyright (C) 1997   Josef Wilgen
00004  * Copyright (C) 2002   Uwe Rathmann
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the Qwt License, Version 1.0
00008  *****************************************************************************/
00009 
00010 #include "qwt_plot_svgitem.h"
00011 #include "qwt_scale_map.h"
00012 #include "qwt_painter.h"
00013 #include <qpainter.h>
00014 #include <qsvgrenderer.h>
00015 
00016 class QwtPlotSvgItem::PrivateData
00017 {
00018 public:
00019     PrivateData()
00020     {
00021     }
00022 
00023     QRectF boundingRect;
00024     QSvgRenderer renderer;
00025 };
00026 
00036 QwtPlotSvgItem::QwtPlotSvgItem( const QString& title ):
00037     QwtPlotItem( QwtText( title ) )
00038 {
00039     init();
00040 }
00041 
00051 QwtPlotSvgItem::QwtPlotSvgItem( const QwtText& title ):
00052     QwtPlotItem( title )
00053 {
00054     init();
00055 }
00056 
00058 QwtPlotSvgItem::~QwtPlotSvgItem()
00059 {
00060     delete d_data;
00061 }
00062 
00063 void QwtPlotSvgItem::init()
00064 {
00065     d_data = new PrivateData();
00066     d_data->boundingRect = QwtPlotItem::boundingRect();
00067 
00068     setItemAttribute( QwtPlotItem::AutoScale, true );
00069     setItemAttribute( QwtPlotItem::Legend, false );
00070 
00071     setZ( 8.0 );
00072 }
00073 
00075 int QwtPlotSvgItem::rtti() const
00076 {
00077     return QwtPlotItem::Rtti_PlotSVG;
00078 }
00079 
00088 bool QwtPlotSvgItem::loadFile( const QRectF &rect,
00089     const QString &fileName )
00090 {
00091     d_data->boundingRect = rect;
00092     const bool ok = d_data->renderer.load( fileName );
00093 
00094     legendChanged();
00095     itemChanged();
00096 
00097     return ok;
00098 }
00099 
00108 bool QwtPlotSvgItem::loadData( const QRectF &rect,
00109     const QByteArray &data )
00110 {
00111     d_data->boundingRect = rect;
00112     const bool ok = d_data->renderer.load( data );
00113 
00114     legendChanged();
00115     itemChanged();
00116 
00117     return ok;
00118 }
00119 
00121 QRectF QwtPlotSvgItem::boundingRect() const
00122 {
00123     return d_data->boundingRect;
00124 }
00125 
00127 const QSvgRenderer &QwtPlotSvgItem::renderer() const
00128 {
00129     return d_data->renderer;
00130 }
00131 
00133 QSvgRenderer &QwtPlotSvgItem::renderer()
00134 {
00135     return d_data->renderer;
00136 }
00137 
00146 void QwtPlotSvgItem::draw( QPainter *painter,
00147     const QwtScaleMap &xMap, const QwtScaleMap &yMap,
00148     const QRectF &canvasRect ) const
00149 {
00150     const QRectF cRect = QwtScaleMap::invTransform(
00151         xMap, yMap, canvasRect.toRect() );
00152     const QRectF bRect = boundingRect();
00153     if ( bRect.isValid() && cRect.isValid() )
00154     {
00155         QRectF rect = bRect;
00156         if ( bRect.contains( cRect ) )
00157             rect = cRect;
00158 
00159         const QRectF r = QwtScaleMap::transform( xMap, yMap, rect );
00160         render( painter, viewBox( rect ), r );
00161     }
00162 }
00163 
00171 void QwtPlotSvgItem::render( QPainter *painter,
00172     const QRectF &viewBox, const QRectF &rect ) const
00173 {
00174     if ( !viewBox.isValid() )
00175         return;
00176 
00177     QRectF r = rect;
00178 
00179     if ( QwtPainter::roundingAlignment( painter ) )
00180     {
00181         r.setLeft ( qRound( r.left() ) );
00182         r.setRight ( qRound( r.right() ) );
00183         r.setTop ( qRound( r.top() ) );
00184         r.setBottom ( qRound( r.bottom() ) );
00185     }
00186 
00187     d_data->renderer.setViewBox( viewBox );
00188     d_data->renderer.render( painter, r );
00189 }
00190 
00197 QRectF QwtPlotSvgItem::viewBox( const QRectF &rect ) const
00198 {
00199     const QSize sz = d_data->renderer.defaultSize();
00200     const QRectF br = boundingRect();
00201 
00202     if ( !rect.isValid() || !br.isValid() || sz.isNull() )
00203         return QRectF();
00204 
00205     QwtScaleMap xMap;
00206     xMap.setScaleInterval( br.left(), br.right() );
00207     xMap.setPaintInterval( 0, sz.width() );
00208 
00209     QwtScaleMap yMap;
00210     yMap.setScaleInterval( br.top(), br.bottom() );
00211     yMap.setPaintInterval( sz.height(), 0 );
00212 
00213     const double x1 = xMap.transform( rect.left() );
00214     const double x2 = xMap.transform( rect.right() );
00215     const double y1 = yMap.transform( rect.bottom() );
00216     const double y2 = yMap.transform( rect.top() );
00217 
00218     return QRectF( x1, y1, x2 - x1, y2 - y1 );
00219 }


plotjuggler
Author(s): Davide Faconti
autogenerated on Fri Sep 1 2017 02:41:56