1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096 |
- #include "qwt_plot_curve.h"
- #include "qwt_math.h"
- #include "qwt_clipper.h"
- #include "qwt_painter.h"
- #include "qwt_legend.h"
- #include "qwt_legend_item.h"
- #include "qwt_scale_map.h"
- #include "qwt_plot.h"
- #include "qwt_plot_canvas.h"
- #include "qwt_curve_fitter.h"
- #include "qwt_symbol.h"
- #include <qpainter.h>
- #include <qpixmap.h>
- #include <qalgorithms.h>
- #include <qmath.h>
- static int verifyRange( int size, int &i1, int &i2 )
- {
- if ( size < 1 )
- return 0;
- i1 = qwtLim( i1, 0, size - 1 );
- i2 = qwtLim( i2, 0, size - 1 );
- if ( i1 > i2 )
- qSwap( i1, i2 );
- return ( i2 - i1 + 1 );
- }
- class QwtPlotCurve::PrivateData
- {
- public:
- PrivateData():
- style( QwtPlotCurve::Lines ),
- baseline( 0.0 ),
- symbol( NULL ),
- attributes( 0 ),
- paintAttributes( QwtPlotCurve::ClipPolygons ),
- legendAttributes( 0 )
- {
- pen = QPen( Qt::black );
- curveFitter = new QwtSplineCurveFitter;
- }
- ~PrivateData()
- {
- delete symbol;
- delete curveFitter;
- }
- QwtPlotCurve::CurveStyle style;
- double baseline;
- const QwtSymbol *symbol;
- QwtCurveFitter *curveFitter;
- QPen pen;
- QBrush brush;
- int attributes;
- int paintAttributes;
- int legendAttributes;
- };
- QwtPlotCurve::QwtPlotCurve( const QwtText &title ):
- QwtPlotSeriesItem<QPointF>( title )
- {
- init();
- }
- QwtPlotCurve::QwtPlotCurve( const QString &title ):
- QwtPlotSeriesItem<QPointF>( QwtText( title ) )
- {
- init();
- }
- QwtPlotCurve::~QwtPlotCurve()
- {
- delete d_data;
- }
- void QwtPlotCurve::init()
- {
- setItemAttribute( QwtPlotItem::Legend );
- setItemAttribute( QwtPlotItem::AutoScale );
- d_data = new PrivateData;
- d_series = new QwtPointSeriesData();
- setZ( 20.0 );
- }
- int QwtPlotCurve::rtti() const
- {
- return QwtPlotItem::Rtti_PlotCurve;
- }
- void QwtPlotCurve::setPaintAttribute( PaintAttribute attribute, bool on )
- {
- if ( on )
- d_data->paintAttributes |= attribute;
- else
- d_data->paintAttributes &= ~attribute;
- }
- bool QwtPlotCurve::testPaintAttribute( PaintAttribute attribute ) const
- {
- return ( d_data->paintAttributes & attribute );
- }
- void QwtPlotCurve::setLegendAttribute( LegendAttribute attribute, bool on )
- {
- if ( on )
- d_data->legendAttributes |= attribute;
- else
- d_data->legendAttributes &= ~attribute;
- }
- bool QwtPlotCurve::testLegendAttribute( LegendAttribute attribute ) const
- {
- return ( d_data->legendAttributes & attribute );
- }
- void QwtPlotCurve::setStyle( CurveStyle style )
- {
- if ( style != d_data->style )
- {
- d_data->style = style;
- itemChanged();
- }
- }
- QwtPlotCurve::CurveStyle QwtPlotCurve::style() const
- {
- return d_data->style;
- }
- void QwtPlotCurve::setSymbol( const QwtSymbol *symbol )
- {
- if ( symbol != d_data->symbol )
- {
- delete d_data->symbol;
- d_data->symbol = symbol;
- itemChanged();
- }
- }
- const QwtSymbol *QwtPlotCurve::symbol() const
- {
- return d_data->symbol;
- }
- void QwtPlotCurve::setPen( const QPen &pen )
- {
- if ( pen != d_data->pen )
- {
- d_data->pen = pen;
- itemChanged();
- }
- }
- const QPen& QwtPlotCurve::pen() const
- {
- return d_data->pen;
- }
- void QwtPlotCurve::setBrush( const QBrush &brush )
- {
- if ( brush != d_data->brush )
- {
- d_data->brush = brush;
- itemChanged();
- }
- }
- const QBrush& QwtPlotCurve::brush() const
- {
- return d_data->brush;
- }
- void QwtPlotCurve::drawSeries( QPainter *painter,
- const QwtScaleMap &xMap, const QwtScaleMap &yMap,
- const QRectF &canvasRect, int from, int to ) const
- {
- if ( !painter || dataSize() <= 0 )
- return;
- if ( to < 0 )
- to = dataSize() - 1;
- if ( verifyRange( dataSize(), from, to ) > 0 )
- {
- painter->save();
- painter->setPen( d_data->pen );
-
- drawCurve( painter, d_data->style, xMap, yMap, canvasRect, from, to );
- painter->restore();
- if ( d_data->symbol &&
- ( d_data->symbol->style() != QwtSymbol::NoSymbol ) )
- {
- painter->save();
- drawSymbols( painter, *d_data->symbol,
- xMap, yMap, canvasRect, from, to );
- painter->restore();
- }
- }
- }
- void QwtPlotCurve::drawCurve( QPainter *painter, int style,
- const QwtScaleMap &xMap, const QwtScaleMap &yMap,
- const QRectF &canvasRect, int from, int to ) const
- {
- switch ( style )
- {
- case Lines:
- if ( testCurveAttribute( Fitted ) )
- {
-
-
- from = 0;
- to = dataSize() - 1;
- }
- drawLines( painter, xMap, yMap, canvasRect, from, to );
- break;
- case Sticks:
- drawSticks( painter, xMap, yMap, canvasRect, from, to );
- break;
- case Steps:
- drawSteps( painter, xMap, yMap, canvasRect, from, to );
- break;
- case Dots:
- drawDots( painter, xMap, yMap, canvasRect, from, to );
- break;
- case NoCurve:
- default:
- break;
- }
- }
- void QwtPlotCurve::drawLines( QPainter *painter,
- const QwtScaleMap &xMap, const QwtScaleMap &yMap,
- const QRectF &canvasRect, int from, int to ) const
- {
- int size = to - from + 1;
- if ( size <= 0 )
- return;
- const bool doAlign = QwtPainter::roundingAlignment( painter );
- QPolygonF polyline( size );
- QPointF *points = polyline.data();
- for ( int i = from; i <= to; i++ )
- {
- const QPointF sample = d_series->sample( i );
- double x = xMap.transform( sample.x() );
- double y = yMap.transform( sample.y() );
- if ( doAlign )
- {
- x = qRound( x );
- y = qRound( y );
- }
- points[i - from].rx() = x;
- points[i - from].ry() = y;
- }
- if ( ( d_data->attributes & Fitted ) && d_data->curveFitter )
- polyline = d_data->curveFitter->fitCurve( polyline );
- if ( d_data->paintAttributes & ClipPolygons )
- {
- qreal pw = qMax( qreal( 1.0 ), painter->pen().widthF());
- polyline = QwtClipper::clipPolygonF(
- canvasRect.adjusted(-pw, -pw, pw, pw), polyline );
- }
- QwtPainter::drawPolyline( painter, polyline );
- if ( d_data->brush.style() != Qt::NoBrush )
- fillCurve( painter, xMap, yMap, polyline );
- }
- void QwtPlotCurve::drawSticks( QPainter *painter,
- const QwtScaleMap &xMap, const QwtScaleMap &yMap,
- const QRectF &, int from, int to ) const
- {
- painter->save();
- painter->setRenderHint( QPainter::Antialiasing, false );
- const bool doAlign = QwtPainter::roundingAlignment( painter );
- double x0 = xMap.transform( d_data->baseline );
- double y0 = yMap.transform( d_data->baseline );
- if ( doAlign )
- {
- x0 = qRound( x0 );
- y0 = qRound( y0 );
- }
- const Qt::Orientation o = orientation();
- for ( int i = from; i <= to; i++ )
- {
- const QPointF sample = d_series->sample( i );
- double xi = xMap.transform( sample.x() );
- double yi = yMap.transform( sample.y() );
- if ( doAlign )
- {
- xi = qRound( xi );
- yi = qRound( yi );
- }
- if ( o == Qt::Horizontal )
- QwtPainter::drawLine( painter, x0, yi, xi, yi );
- else
- QwtPainter::drawLine( painter, xi, y0, xi, yi );
- }
- painter->restore();
- }
- void QwtPlotCurve::drawDots( QPainter *painter,
- const QwtScaleMap &xMap, const QwtScaleMap &yMap,
- const QRectF &canvasRect, int from, int to ) const
- {
- const bool doFill = d_data->brush.style() != Qt::NoBrush;
- const bool doAlign = QwtPainter::roundingAlignment( painter );
- QPolygonF polyline;
- if ( doFill )
- polyline.resize( to - from + 1 );
- QPointF *points = polyline.data();
- for ( int i = from; i <= to; i++ )
- {
- const QPointF sample = d_series->sample( i );
- double xi = xMap.transform( sample.x() );
- double yi = yMap.transform( sample.y() );
- if ( doAlign )
- {
- xi = qRound( xi );
- yi = qRound( yi );
- }
- QwtPainter::drawPoint( painter, QPointF( xi, yi ) );
- if ( doFill )
- {
- points[i - from].rx() = xi;
- points[i - from].ry() = yi;
- }
- }
- if ( doFill )
- {
- if ( d_data->paintAttributes & ClipPolygons )
- polyline = QwtClipper::clipPolygonF( canvasRect, polyline );
- fillCurve( painter, xMap, yMap, polyline );
- }
- }
- void QwtPlotCurve::drawSteps( QPainter *painter,
- const QwtScaleMap &xMap, const QwtScaleMap &yMap,
- const QRectF &canvasRect, int from, int to ) const
- {
- const bool doAlign = QwtPainter::roundingAlignment( painter );
- QPolygonF polygon( 2 * ( to - from ) + 1 );
- QPointF *points = polygon.data();
- bool inverted = orientation() == Qt::Vertical;
- if ( d_data->attributes & Inverted )
- inverted = !inverted;
- int i, ip;
- for ( i = from, ip = 0; i <= to; i++, ip += 2 )
- {
- const QPointF sample = d_series->sample( i );
- double xi = xMap.transform( sample.x() );
- double yi = yMap.transform( sample.y() );
- if ( doAlign )
- {
- xi = qRound( xi );
- yi = qRound( yi );
- }
- if ( ip > 0 )
- {
- const QPointF &p0 = points[ip - 2];
- QPointF &p = points[ip - 1];
- if ( inverted )
- {
- p.rx() = p0.x();
- p.ry() = yi;
- }
- else
- {
- p.rx() = xi;
- p.ry() = p0.y();
- }
- }
- points[ip].rx() = xi;
- points[ip].ry() = yi;
- }
- if ( d_data->paintAttributes & ClipPolygons )
- polygon = QwtClipper::clipPolygonF( canvasRect, polygon );
- QwtPainter::drawPolyline( painter, polygon );
- if ( d_data->brush.style() != Qt::NoBrush )
- fillCurve( painter, xMap, yMap, polygon );
- }
- void QwtPlotCurve::setCurveAttribute( CurveAttribute attribute, bool on )
- {
- if ( bool( d_data->attributes & attribute ) == on )
- return;
- if ( on )
- d_data->attributes |= attribute;
- else
- d_data->attributes &= ~attribute;
- itemChanged();
- }
- bool QwtPlotCurve::testCurveAttribute( CurveAttribute attribute ) const
- {
- return d_data->attributes & attribute;
- }
- void QwtPlotCurve::setCurveFitter( QwtCurveFitter *curveFitter )
- {
- delete d_data->curveFitter;
- d_data->curveFitter = curveFitter;
- itemChanged();
- }
- QwtCurveFitter *QwtPlotCurve::curveFitter() const
- {
- return d_data->curveFitter;
- }
- void QwtPlotCurve::fillCurve( QPainter *painter,
- const QwtScaleMap &xMap, const QwtScaleMap &yMap,
- QPolygonF &polygon ) const
- {
- if ( d_data->brush.style() == Qt::NoBrush )
- return;
- closePolyline( painter, xMap, yMap, polygon );
- if ( polygon.count() <= 2 )
- return;
- QBrush b = d_data->brush;
- if ( !b.color().isValid() )
- b.setColor( d_data->pen.color() );
- painter->save();
- painter->setPen( QPen( Qt::NoPen ) );
- painter->setBrush( b );
- QwtPainter::drawPolygon( painter, polygon );
- painter->restore();
- }
- void QwtPlotCurve::closePolyline( QPainter *painter,
- const QwtScaleMap &xMap, const QwtScaleMap &yMap,
- QPolygonF &polygon ) const
- {
- if ( polygon.size() < 2 )
- return;
- const bool doAlign = QwtPainter::roundingAlignment( painter );
- double baseline = d_data->baseline;
-
- if ( orientation() == Qt::Vertical )
- {
- if ( yMap.transformation()->type() == QwtScaleTransformation::Log10 )
- {
- if ( baseline < QwtScaleMap::LogMin )
- baseline = QwtScaleMap::LogMin;
- }
- double refY = yMap.transform( baseline );
- if ( doAlign )
- refY = qRound( refY );
- polygon += QPointF( polygon.last().x(), refY );
- polygon += QPointF( polygon.first().x(), refY );
- }
- else
- {
- if ( xMap.transformation()->type() == QwtScaleTransformation::Log10 )
- {
- if ( baseline < QwtScaleMap::LogMin )
- baseline = QwtScaleMap::LogMin;
- }
- double refX = xMap.transform( baseline );
- if ( doAlign )
- refX = qRound( refX );
- polygon += QPointF( refX, polygon.last().y() );
- polygon += QPointF( refX, polygon.first().y() );
- }
- }
- void QwtPlotCurve::drawSymbols( QPainter *painter, const QwtSymbol &symbol,
- const QwtScaleMap &xMap, const QwtScaleMap &yMap,
- const QRectF &canvasRect, int from, int to ) const
- {
- const bool doAlign = QwtPainter::roundingAlignment( painter );
- bool usePixmap = testPaintAttribute( CacheSymbols );
- if ( usePixmap && !doAlign )
- {
-
-
- usePixmap = false;
- }
- if ( usePixmap )
- {
- QPixmap pm( symbol.boundingSize() );
- pm.fill( Qt::transparent );
- const double pw2 = 0.5 * pm.width();
- const double ph2 = 0.5 * pm.height();
- QPainter p( &pm );
- p.setRenderHints( painter->renderHints() );
- symbol.drawSymbol( &p, QPointF( pw2, ph2 ) );
- p.end();
- for ( int i = from; i <= to; i++ )
- {
- const QPointF sample = d_series->sample( i );
- double xi = xMap.transform( sample.x() );
- double yi = yMap.transform( sample.y() );
- if ( doAlign )
- {
- xi = qRound( xi );
- yi = qRound( yi );
- }
- if ( canvasRect.contains( xi, yi ) )
- {
- const int left = qCeil( xi ) - pw2;
- const int top = qCeil( yi ) - ph2;
- painter->drawPixmap( left, top, pm );
- }
- }
- }
- else
- {
- const int chunkSize = 500;
- for ( int i = from; i <= to; i += chunkSize )
- {
- const int n = qMin( chunkSize, to - i + 1 );
- QPolygonF points;
- for ( int j = 0; j < n; j++ )
- {
- const QPointF sample = d_series->sample( i + j );
- const double xi = xMap.transform( sample.x() );
- const double yi = yMap.transform( sample.y() );
- if ( canvasRect.contains( xi, yi ) )
- points += QPointF( xi, yi );
- }
- if ( points.size() > 0 )
- symbol.drawSymbols( painter, points );
- }
- }
- }
- void QwtPlotCurve::setBaseline( double value )
- {
- if ( d_data->baseline != value )
- {
- d_data->baseline = value;
- itemChanged();
- }
- }
- double QwtPlotCurve::baseline() const
- {
- return d_data->baseline;
- }
- int QwtPlotCurve::closestPoint( const QPoint &pos, double *dist ) const
- {
- if ( plot() == NULL || dataSize() <= 0 )
- return -1;
- const QwtScaleMap xMap = plot()->canvasMap( xAxis() );
- const QwtScaleMap yMap = plot()->canvasMap( yAxis() );
- int index = -1;
- double dmin = 1.0e10;
- for ( uint i = 0; i < dataSize(); i++ )
- {
- const QPointF sample = d_series->sample( i );
- const double cx = xMap.transform( sample.x() ) - pos.x();
- const double cy = yMap.transform( sample.y() ) - pos.y();
- const double f = qwtSqr( cx ) + qwtSqr( cy );
- if ( f < dmin )
- {
- index = i;
- dmin = f;
- }
- }
- if ( dist )
- *dist = qSqrt( dmin );
- return index;
- }
- void QwtPlotCurve::updateLegend( QwtLegend *legend ) const
- {
- if ( legend && testItemAttribute( QwtPlotItem::Legend )
- && ( d_data->legendAttributes & QwtPlotCurve::LegendShowSymbol )
- && d_data->symbol
- && d_data->symbol->style() != QwtSymbol::NoSymbol )
- {
- QWidget *lgdItem = legend->find( this );
- if ( lgdItem == NULL )
- {
- lgdItem = legendItem();
- if ( lgdItem )
- legend->insert( this, lgdItem );
- }
- if ( lgdItem && lgdItem->inherits( "QwtLegendItem" ) )
- {
- QwtLegendItem *l = ( QwtLegendItem * )lgdItem;
- l->setIdentifierSize( d_data->symbol->boundingSize() );
- }
- }
- QwtPlotItem::updateLegend( legend );
- }
- void QwtPlotCurve::drawLegendIdentifier(
- QPainter *painter, const QRectF &rect ) const
- {
- if ( rect.isEmpty() )
- return;
- const int dim = qMin( rect.width(), rect.height() );
- QSize size( dim, dim );
- QRectF r( 0, 0, size.width(), size.height() );
- r.moveCenter( rect.center() );
- if ( d_data->legendAttributes == 0 )
- {
- QBrush brush = d_data->brush;
- if ( brush.style() == Qt::NoBrush )
- {
- if ( style() != QwtPlotCurve::NoCurve )
- brush = QBrush( pen().color() );
- else if ( d_data->symbol &&
- ( d_data->symbol->style() != QwtSymbol::NoSymbol ) )
- {
- brush = QBrush( d_data->symbol->pen().color() );
- }
- }
- if ( brush.style() != Qt::NoBrush )
- painter->fillRect( r, brush );
- }
- if ( d_data->legendAttributes & QwtPlotCurve::LegendShowBrush )
- {
- if ( d_data->brush.style() != Qt::NoBrush )
- painter->fillRect( r, d_data->brush );
- }
- if ( d_data->legendAttributes & QwtPlotCurve::LegendShowLine )
- {
- if ( pen() != Qt::NoPen )
- {
- painter->setPen( pen() );
- QwtPainter::drawLine( painter, rect.left(), rect.center().y(),
- rect.right() - 1.0, rect.center().y() );
- }
- }
- if ( d_data->legendAttributes & QwtPlotCurve::LegendShowSymbol )
- {
- if ( d_data->symbol &&
- ( d_data->symbol->style() != QwtSymbol::NoSymbol ) )
- {
- QSize symbolSize = d_data->symbol->boundingSize();
- symbolSize -= QSize( 2, 2 );
-
- double xRatio = 1.0;
- if ( rect.width() < symbolSize.width() )
- xRatio = rect.width() / symbolSize.width();
- double yRatio = 1.0;
- if ( rect.height() < symbolSize.height() )
- yRatio = rect.height() / symbolSize.height();
- const double ratio = qMin( xRatio, yRatio );
- painter->save();
- painter->scale( ratio, ratio );
- d_data->symbol->drawSymbol( painter, rect.center() / ratio );
- painter->restore();
- }
- }
- }
- void QwtPlotCurve::setSamples( const QVector<QPointF> &samples )
- {
- delete d_series;
- d_series = new QwtPointSeriesData( samples );
- itemChanged();
- }
- #ifndef QWT_NO_COMPAT
- void QwtPlotCurve::setRawSamples( const double *xData, const double *yData, int size )
- {
- delete d_series;
- d_series = new QwtCPointerData( xData, yData, size );
- itemChanged();
- }
- void QwtPlotCurve::setSamples( const double *xData, const double *yData, int size )
- {
- delete d_series;
- d_series = new QwtPointArrayData( xData, yData, size );
- itemChanged();
- }
- void QwtPlotCurve::setSamples( const QVector<double> &xData,
- const QVector<double> &yData )
- {
- delete d_series;
- d_series = new QwtPointArrayData( xData, yData );
- itemChanged();
- }
- #endif
|