qwt_plot_textlabel.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_textlabel.h"
00011 #include "qwt_painter.h"
00012 #include "qwt_scale_map.h"
00013 #include <qpainter.h>
00014 #include <qpixmap.h>
00015 #include <qmath.h>
00016 
00017 static QRect qwtItemRect( int renderFlags,
00018     const QRectF &rect, const QSizeF &itemSize ) 
00019 {
00020     int x;
00021     if ( renderFlags & Qt::AlignLeft )
00022     {
00023         x = rect.left();
00024     }
00025     else if ( renderFlags & Qt::AlignRight )
00026     {
00027         x = rect.right() - itemSize.width();
00028     }
00029     else
00030     {
00031         x = rect.center().x() - 0.5 * itemSize.width();
00032     }
00033 
00034     int y;
00035     if ( renderFlags & Qt::AlignTop ) 
00036     {
00037         y = rect.top();
00038     }
00039     else if ( renderFlags & Qt::AlignBottom )
00040     {
00041         y = rect.bottom() - itemSize.height();
00042     }
00043     else
00044     {
00045         y = rect.center().y() - 0.5 * itemSize.height();
00046     }
00047 
00048     return QRect( x, y, itemSize.width(), itemSize.height() );
00049 }
00050 
00051 class QwtPlotTextLabel::PrivateData
00052 {   
00053 public:
00054     PrivateData():
00055         margin( 5 )
00056     {
00057     }
00058 
00059     QwtText text;
00060     int margin;
00061 
00062     QPixmap pixmap;
00063 };  
00064 
00080 QwtPlotTextLabel::QwtPlotTextLabel():
00081     QwtPlotItem( QwtText( "Label" ) )
00082 {
00083     d_data = new PrivateData;
00084 
00085     setItemAttribute( QwtPlotItem::AutoScale, false );
00086     setItemAttribute( QwtPlotItem::Legend, false );
00087 
00088     setZ( 150 );
00089 }
00090 
00092 QwtPlotTextLabel::~QwtPlotTextLabel()
00093 {
00094     delete d_data;
00095 }
00096 
00098 int QwtPlotTextLabel::rtti() const
00099 {
00100     return QwtPlotItem::Rtti_PlotTextLabel;
00101 }
00102 
00113 void QwtPlotTextLabel::setText( const QwtText &text )
00114 {
00115     if ( d_data->text != text )
00116     {
00117         d_data->text = text;
00118 
00119         invalidateCache();
00120         itemChanged();
00121     }
00122 }
00123 
00128 QwtText QwtPlotTextLabel::text() const
00129 {
00130     return d_data->text;
00131 }
00132 
00144 void QwtPlotTextLabel::setMargin( int margin )
00145 {
00146     margin = qMax( margin, 0 );
00147     if ( d_data->margin != margin )
00148     {
00149         d_data->margin = margin;
00150         itemChanged();
00151     }
00152 }
00153 
00158 int QwtPlotTextLabel::margin() const
00159 {
00160     return d_data->margin;
00161 }
00162 
00174 void QwtPlotTextLabel::draw( QPainter *painter,
00175     const QwtScaleMap &xMap, const QwtScaleMap &yMap,
00176     const QRectF &canvasRect ) const
00177 {
00178     Q_UNUSED( xMap );
00179     Q_UNUSED( yMap );
00180 
00181     const int m = d_data->margin;
00182 
00183     const QRectF rect = textRect( canvasRect.adjusted( m, m, -m, -m ),
00184         d_data->text.textSize( painter->font() ) );
00185 
00186     bool doCache = QwtPainter::roundingAlignment( painter );
00187     if ( doCache )
00188     {
00189         switch( painter->paintEngine()->type() )
00190         {
00191             case QPaintEngine::Picture:
00192             case QPaintEngine::User: // usually QwtGraphic
00193             {
00194                 // don't use a cache for record/replay devices
00195                 doCache = false;
00196                 break;
00197             }
00198             default:;
00199         }
00200     }
00201 
00202     if ( doCache )
00203     {
00204         // when the paint device is aligning it is not one
00205         // where scalability matters ( PDF, SVG ).
00206         // As rendering a text label is an expensive operation
00207         // we use a cache.
00208 
00209         int pw = 0;
00210         if ( d_data->text.borderPen().style() != Qt::NoPen )
00211             pw = qMax( d_data->text.borderPen().width(), 1 );
00212 
00213         QRect pixmapRect; 
00214         pixmapRect.setLeft( qFloor( rect.left() ) - pw );
00215         pixmapRect.setTop( qFloor( rect.top() ) - pw );
00216         pixmapRect.setRight( qCeil( rect.right() ) + pw );
00217         pixmapRect.setBottom( qCeil( rect.bottom() ) + pw );
00218         
00219 #if QT_VERSION >= 0x050000
00220         const qreal pixelRatio = QwtPainter::devicePixelRatio( painter->device() );
00221         const QSize scaledSize = pixmapRect.size() * pixelRatio;
00222 #else
00223         const QSize scaledSize = pixmapRect.size();
00224 #endif
00225 
00226         if ( d_data->pixmap.isNull() || 
00227             ( scaledSize != d_data->pixmap.size() )  )
00228         {
00229             d_data->pixmap = QPixmap( scaledSize );
00230 #if QT_VERSION >= 0x050000 
00231             d_data->pixmap.setDevicePixelRatio( pixelRatio );
00232 #endif
00233             d_data->pixmap.fill( Qt::transparent );
00234 
00235             const QRect r( pw, pw, 
00236                 pixmapRect.width() - 2 * pw, pixmapRect.height() - 2 * pw );
00237 
00238             QPainter pmPainter( &d_data->pixmap );
00239             d_data->text.draw( &pmPainter, r );
00240         }
00241 
00242         painter->drawPixmap( pixmapRect, d_data->pixmap );
00243     }
00244     else
00245     {
00246         d_data->text.draw( painter, rect );
00247     }
00248 }
00249 
00261 QRectF QwtPlotTextLabel::textRect( 
00262     const QRectF &rect, const QSizeF &textSize ) const
00263 {
00264     return qwtItemRect( d_data->text.renderFlags(), rect, textSize );
00265 }
00266 
00268 void QwtPlotTextLabel::invalidateCache()
00269 {
00270     d_data->pixmap = QPixmap();
00271 }


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