qwt_plot_zoneitem.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_zoneitem.h"
00011 #include "qwt_painter.h"
00012 #include "qwt_scale_map.h"
00013 #include <qpainter.h>
00014 
00015 class QwtPlotZoneItem::PrivateData
00016 {   
00017 public:
00018     PrivateData():
00019         orientation( Qt::Vertical ),
00020         pen( Qt::NoPen )
00021     {
00022         QColor c( Qt::darkGray );
00023         c.setAlpha( 100 );
00024         brush = QBrush( c );
00025     }
00026         
00027     Qt::Orientation orientation;
00028     QPen pen;
00029     QBrush brush;
00030     QwtInterval interval;
00031 };  
00032 
00047 QwtPlotZoneItem::QwtPlotZoneItem():
00048     QwtPlotItem( QwtText( "Zone" ) )
00049 {
00050     d_data = new PrivateData;
00051 
00052     setItemAttribute( QwtPlotItem::AutoScale, false );
00053     setItemAttribute( QwtPlotItem::Legend, false );
00054 
00055     setZ( 5 );
00056 }
00057 
00059 QwtPlotZoneItem::~QwtPlotZoneItem()
00060 {
00061     delete d_data;
00062 }
00063 
00065 int QwtPlotZoneItem::rtti() const
00066 {
00067     return QwtPlotItem::Rtti_PlotZone;
00068 }
00069 
00083 void QwtPlotZoneItem::setPen( const QColor &color, qreal width, Qt::PenStyle style )
00084 {   
00085     setPen( QPen( color, width, style ) );
00086 }
00087 
00096 void QwtPlotZoneItem::setPen( const QPen &pen )
00097 {
00098     if ( d_data->pen != pen )
00099     {
00100         d_data->pen = pen;
00101         itemChanged();
00102     }
00103 }
00104 
00109 const QPen &QwtPlotZoneItem::pen() const
00110 {
00111     return d_data->pen;
00112 }
00113 
00122 void QwtPlotZoneItem::setBrush( const QBrush &brush )
00123 {
00124     if ( d_data->brush != brush )
00125     {
00126         d_data->brush = brush;
00127         itemChanged();
00128     }
00129 }
00130 
00135 const QBrush &QwtPlotZoneItem::brush() const
00136 {
00137     return d_data->brush;
00138 }
00139 
00149 void QwtPlotZoneItem::setOrientation( Qt::Orientation orientation )
00150 {
00151     if ( d_data->orientation != orientation )
00152     {
00153         d_data->orientation = orientation;
00154         itemChanged();
00155     }
00156 }
00157 
00162 Qt::Orientation QwtPlotZoneItem::orientation()
00163 {
00164     return d_data->orientation;
00165 }
00166 
00178 void QwtPlotZoneItem::setInterval( double min, double max )
00179 {
00180     setInterval( QwtInterval( min, max ) );
00181 }
00182 
00193 void QwtPlotZoneItem::setInterval( const QwtInterval &interval )
00194 {
00195     if ( d_data->interval != interval )
00196     {
00197         d_data->interval = interval;
00198         itemChanged(); 
00199     }   
00200 }   
00201 
00206 QwtInterval QwtPlotZoneItem::interval() const
00207 {
00208     return d_data->interval;
00209 }   
00210 
00220 void QwtPlotZoneItem::draw( QPainter *painter,
00221     const QwtScaleMap &xMap, const QwtScaleMap &yMap,
00222     const QRectF &canvasRect ) const
00223 {
00224     if ( !d_data->interval.isValid() )
00225         return;
00226 
00227     QPen pen = d_data->pen;
00228     pen.setCapStyle( Qt::FlatCap );
00229 
00230     const bool doAlign = QwtPainter::roundingAlignment( painter );
00231 
00232     if ( d_data->orientation == Qt::Horizontal )
00233     {
00234         double y1 = yMap.transform( d_data->interval.minValue() );
00235         double y2 = yMap.transform( d_data->interval.maxValue() );
00236 
00237         if ( doAlign )
00238         {
00239             y1 = qRound( y1 );
00240             y2 = qRound( y2 );
00241         }
00242 
00243         QRectF r( canvasRect.left(), y1, canvasRect.width(), y2 - y1 );
00244         r = r.normalized();
00245 
00246         if ( ( d_data->brush.style() != Qt::NoBrush ) && ( y1 != y2 ) )
00247         {
00248             QwtPainter::fillRect( painter, r, d_data->brush );
00249         }
00250 
00251         if ( d_data->pen.style() != Qt::NoPen )
00252         {
00253             painter->setPen( d_data->pen );
00254 
00255             QwtPainter::drawLine( painter, r.left(), r.top(), r.right(), r.top() );
00256             QwtPainter::drawLine( painter, r.left(), r.bottom(), r.right(), r.bottom() );
00257         }
00258     }
00259     else
00260     {
00261         double x1 = xMap.transform( d_data->interval.minValue() );
00262         double x2 = xMap.transform( d_data->interval.maxValue() );
00263 
00264         if ( doAlign )
00265         {
00266             x1 = qRound( x1 );
00267             x2 = qRound( x2 );
00268         }
00269 
00270         QRectF r( x1, canvasRect.top(), x2 - x1, canvasRect.height() );
00271         r = r.normalized();
00272 
00273         if ( ( d_data->brush.style() != Qt::NoBrush ) && ( x1 != x2 ) )
00274         {
00275             QwtPainter::fillRect( painter, r, d_data->brush );
00276         }
00277 
00278         if ( d_data->pen.style() != Qt::NoPen )
00279         {
00280             painter->setPen( d_data->pen );
00281 
00282             QwtPainter::drawLine( painter, r.left(), r.top(), r.left(), r.bottom() );
00283             QwtPainter::drawLine( painter, r.right(), r.top(), r.right(), r.bottom() );
00284         }
00285     }
00286 }
00287 
00294 QRectF QwtPlotZoneItem::boundingRect() const
00295 {
00296     QRectF br = QwtPlotItem::boundingRect();
00297 
00298     const QwtInterval &intv = d_data->interval;
00299 
00300     if ( intv.isValid() )
00301     {
00302         if ( d_data->orientation == Qt::Horizontal )
00303         {
00304             br.setTop( intv.minValue() );
00305             br.setBottom( intv.maxValue() );
00306         }
00307         else
00308         {
00309             br.setLeft( intv.minValue() );
00310             br.setRight( intv.maxValue() );
00311         }
00312     }
00313 
00314     return br;
00315 }


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