Skip to content
qwt_plot_curve.cpp 29.9 KiB
Newer Older
pixhawk's avatar
pixhawk committed
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

Bryant's avatar
Bryant committed
#include "qwt_plot_curve.h"
#include "qwt_point_data.h"
pixhawk's avatar
pixhawk committed
#include "qwt_math.h"
#include "qwt_clipper.h"
#include "qwt_painter.h"
Bryant's avatar
Bryant committed
#include "qwt_scale_map.h"
pixhawk's avatar
pixhawk committed
#include "qwt_plot.h"
#include "qwt_curve_fitter.h"
#include "qwt_symbol.h"
Bryant's avatar
Bryant committed
#include "qwt_point_mapper.h"
#include <qpainter.h>
#include <qpixmap.h>
#include <qalgorithms.h>
#include <qmath.h>
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
static void qwtUpdateLegendIconSize( QwtPlotCurve *curve )
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    if ( curve->symbol() && 
        curve->testLegendAttribute( QwtPlotCurve::LegendShowSymbol ) )
    {
        QSize sz = curve->symbol()->boundingRect().size();
        sz += QSize( 2, 2 ); // margin
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
        if ( curve->testLegendAttribute( QwtPlotCurve::LegendShowLine ) )
        {
            // Avoid, that the line is completely covered by the symbol
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
            int w = qCeil( 1.5 * sz.width() );
            if ( w % 2 )
                w++;
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
            sz.setWidth( qMax( 8, w ) );
pixhawk's avatar
pixhawk committed
        }

Bryant's avatar
Bryant committed
        curve->setLegendIconSize( sz );
pixhawk's avatar
pixhawk committed
    }
}

Bryant's avatar
Bryant committed
static int qwtVerifyRange( int size, int &i1, int &i2 )
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    if ( size < 1 )
pixhawk's avatar
pixhawk committed
        return 0;

Bryant's avatar
Bryant committed
    i1 = qBound( 0, i1, size - 1 );
    i2 = qBound( 0, i2, size - 1 );
pixhawk's avatar
pixhawk committed

    if ( i1 > i2 )
Bryant's avatar
Bryant committed
        qSwap( i1, i2 );
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    return ( i2 - i1 + 1 );
pixhawk's avatar
pixhawk committed
}

class QwtPlotCurve::PrivateData
{
public:
    PrivateData():
Bryant's avatar
Bryant committed
        style( QwtPlotCurve::Lines ),
        baseline( 0.0 ),
        symbol( NULL ),
        attributes( 0 ),
        paintAttributes( 
            QwtPlotCurve::ClipPolygons | QwtPlotCurve::FilterPoints ),
        legendAttributes( 0 )
    {
        pen = QPen( Qt::black );
pixhawk's avatar
pixhawk committed
        curveFitter = new QwtSplineCurveFitter;
    }

Bryant's avatar
Bryant committed
    ~PrivateData()
    {
pixhawk's avatar
pixhawk committed
        delete symbol;
        delete curveFitter;
    }

    QwtPlotCurve::CurveStyle style;
Bryant's avatar
Bryant committed
    double baseline;
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    const QwtSymbol *symbol;
pixhawk's avatar
pixhawk committed
    QwtCurveFitter *curveFitter;

    QPen pen;
    QBrush brush;

Bryant's avatar
Bryant committed
    QwtPlotCurve::CurveAttributes attributes;
    QwtPlotCurve::PaintAttributes paintAttributes;
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    QwtPlotCurve::LegendAttributes legendAttributes;
pixhawk's avatar
pixhawk committed
};

/*!
  Constructor
Bryant's avatar
Bryant committed
  \param title Title of the curve
pixhawk's avatar
pixhawk committed
*/
Bryant's avatar
Bryant committed
QwtPlotCurve::QwtPlotCurve( const QwtText &title ):
    QwtPlotSeriesItem( title )
pixhawk's avatar
pixhawk committed
{
    init();
}

/*!
  Constructor
Bryant's avatar
Bryant committed
  \param title Title of the curve
pixhawk's avatar
pixhawk committed
*/
Bryant's avatar
Bryant committed
QwtPlotCurve::QwtPlotCurve( const QString &title ):
    QwtPlotSeriesItem( QwtText( title ) )
pixhawk's avatar
pixhawk committed
{
    init();
}

//! Destructor
QwtPlotCurve::~QwtPlotCurve()
{
    delete d_data;
}

Bryant's avatar
Bryant committed
//! Initialize internal members
pixhawk's avatar
pixhawk committed
void QwtPlotCurve::init()
{
Bryant's avatar
Bryant committed
    setItemAttribute( QwtPlotItem::Legend );
    setItemAttribute( QwtPlotItem::AutoScale );
pixhawk's avatar
pixhawk committed

    d_data = new PrivateData;
Bryant's avatar
Bryant committed
    setData( new QwtPointSeriesData() );
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    setZ( 20.0 );
pixhawk's avatar
pixhawk committed
}

//! \return QwtPlotItem::Rtti_PlotCurve
int QwtPlotCurve::rtti() const
{
    return QwtPlotItem::Rtti_PlotCurve;
}

/*!
Bryant's avatar
Bryant committed
  Specify an attribute how to draw the curve
pixhawk's avatar
pixhawk committed

  \param attribute Paint attribute
  \param on On/Off
Bryant's avatar
Bryant committed
  \sa testPaintAttribute()
pixhawk's avatar
pixhawk committed
*/
Bryant's avatar
Bryant committed
void QwtPlotCurve::setPaintAttribute( PaintAttribute attribute, bool on )
pixhawk's avatar
pixhawk committed
{
    if ( on )
        d_data->paintAttributes |= attribute;
    else
        d_data->paintAttributes &= ~attribute;
}

/*!
Bryant's avatar
Bryant committed
    \return True, when attribute is enabled
    \sa setPaintAttribute()
pixhawk's avatar
pixhawk committed
*/
Bryant's avatar
Bryant committed
bool QwtPlotCurve::testPaintAttribute( PaintAttribute attribute ) const
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    return ( d_data->paintAttributes & attribute );
pixhawk's avatar
pixhawk committed
}

/*!
Bryant's avatar
Bryant committed
  Specify an attribute how to draw the legend icon

  \param attribute Attribute
  \param on On/Off
  /sa testLegendAttribute(). legendIcon()
pixhawk's avatar
pixhawk committed
*/
Bryant's avatar
Bryant committed
void QwtPlotCurve::setLegendAttribute( LegendAttribute attribute, bool on )
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    if ( on != testLegendAttribute( attribute ) )
    {
        if ( on )
            d_data->legendAttributes |= attribute;
        else
            d_data->legendAttributes &= ~attribute;

        qwtUpdateLegendIconSize( this );
        legendChanged();
pixhawk's avatar
pixhawk committed
    }
}

/*!
Bryant's avatar
Bryant committed
  \return True, when attribute is enabled
  \sa setLegendAttribute()
pixhawk's avatar
pixhawk committed
*/
Bryant's avatar
Bryant committed
bool QwtPlotCurve::testLegendAttribute( LegendAttribute attribute ) const
Bryant's avatar
Bryant committed
    return ( d_data->legendAttributes & attribute );
pixhawk's avatar
pixhawk committed
}

/*!
Bryant's avatar
Bryant committed
  Set the curve's drawing style
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
  \param style Curve style
Loading
Loading full blame...