Skip to content
Snippets Groups Projects
qwt_plot_curve.cpp 29.9 KiB
Newer Older
  • Learn to ignore specific revisions
  • pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        if ( plot() == NULL || numSamples <= 0 )
    
    pixhawk's avatar
    pixhawk committed
            return -1;
    
    
    Bryant's avatar
    Bryant committed
        const QwtSeriesData<QPointF> *series = data();
    
        const QwtScaleMap xMap = plot()->canvasMap( xAxis() );
        const QwtScaleMap yMap = plot()->canvasMap( yAxis() );
    
    pixhawk's avatar
    pixhawk committed
    
        int index = -1;
        double dmin = 1.0e10;
    
    
    Bryant's avatar
    Bryant committed
        for ( uint i = 0; i < numSamples; i++ )
        {
            const QPointF sample = series->sample( i );
    
            const double cx = xMap.transform( sample.x() ) - pos.x();
            const double cy = yMap.transform( sample.y() ) - pos.y();
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
            const double f = qwtSqr( cx ) + qwtSqr( cy );
            if ( f < dmin )
            {
    
    pixhawk's avatar
    pixhawk committed
                index = i;
                dmin = f;
            }
        }
        if ( dist )
    
    Bryant's avatar
    Bryant committed
            *dist = qSqrt( dmin );
    
    pixhawk's avatar
    pixhawk committed
    
        return index;
    }
    
    
    Bryant's avatar
    Bryant committed
    /*!
       \return Icon representing the curve on the legend
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
       \param index Index of the legend entry 
                    ( ignored as there is only one )
       \param size Icon size
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
       \sa QwtPlotItem::setLegendIconSize(), QwtPlotItem::legendData()
     */
    QwtGraphic QwtPlotCurve::legendIcon( int index, 
        const QSizeF &size ) const
    {
        Q_UNUSED( index );
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        if ( size.isEmpty() )
            return QwtGraphic();
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        QwtGraphic graphic;
        graphic.setDefaultSize( size );
        graphic.setRenderHint( QwtGraphic::RenderPensUnscaled, true );
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        QPainter painter( &graphic );
        painter.setRenderHint( QPainter::Antialiasing,
            testRenderHint( QwtPlotItem::RenderAntialiased ) );
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        if ( d_data->legendAttributes == 0 ||
            d_data->legendAttributes & QwtPlotCurve::LegendShowBrush )
        {
            QBrush brush = d_data->brush;
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
            if ( brush.style() == Qt::NoBrush &&
                d_data->legendAttributes == 0 )
            {
                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() );
                }
            }
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
            if ( brush.style() != Qt::NoBrush )
            {
                QRectF r( 0, 0, size.width(), size.height() );
                painter.fillRect( r, brush );
            }
        }
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        if ( d_data->legendAttributes & QwtPlotCurve::LegendShowLine )
        {
            if ( pen() != Qt::NoPen )
            {
                QPen pn = pen();
                pn.setCapStyle( Qt::FlatCap );
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
                painter.setPen( pn );
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
                const double y = 0.5 * size.height();
                QwtPainter::drawLine( &painter, 0.0, y, size.width(), y );
    
    pixhawk's avatar
    pixhawk committed
            }
    
    Bryant's avatar
    Bryant committed
        }
    
        if ( d_data->legendAttributes & QwtPlotCurve::LegendShowSymbol )
        {
            if ( d_data->symbol )
            {
                QRectF r( 0, 0, size.width(), size.height() );
                d_data->symbol->drawSymbol( &painter, r );
    
    pixhawk's avatar
    pixhawk committed
            }
        }
    
    
    Bryant's avatar
    Bryant committed
        return graphic;
    }
    
    /*!
      Initialize data with an array of points.
    
      \param samples Vector of points
      \note QVector is implicitly shared
      \note QPolygonF is derived from QVector<QPointF>
    */
    void QwtPlotCurve::setSamples( const QVector<QPointF> &samples )
    {
        setData( new QwtPointSeriesData( samples ) );
    }
    
    /*!
      Assign a series of points
    
      setSamples() is just a wrapper for setData() without any additional
      value - beside that it is easier to find for the developer.
    
      \param data Data
      \warning The item takes ownership of the data object, deleting
               it when its not used anymore.
    */
    void QwtPlotCurve::setSamples( QwtSeriesData<QPointF> *data )
    {
        setData( data );
    }
    
    #ifndef QWT_NO_COMPAT
    
    /*!
      \brief Initialize the data by pointing to memory blocks which 
             are not managed by QwtPlotCurve.
    
      setRawSamples is provided for efficiency. 
      It is important to keep the pointers
      during the lifetime of the underlying QwtCPointerData class.
    
      \param xData pointer to x data
      \param yData pointer to y data
      \param size size of x and y
    
      \sa QwtCPointerData
    */
    void QwtPlotCurve::setRawSamples( 
        const double *xData, const double *yData, int size )
    {
        setData( new QwtCPointerData( xData, yData, size ) );
    
    pixhawk's avatar
    pixhawk committed
    }
    
    Bryant's avatar
    Bryant committed
    
    /*!
      Set data by copying x- and y-values from specified memory blocks.
      Contrary to setRawSamples(), this function makes a 'deep copy' of
      the data.
    
      \param xData pointer to x values
      \param yData pointer to y values
      \param size size of xData and yData
    
      \sa QwtPointArrayData
    */
    void QwtPlotCurve::setSamples( 
        const double *xData, const double *yData, int size )
    {
        setData( new QwtPointArrayData( xData, yData, size ) );
    }
    
    /*!
      \brief Initialize data with x- and y-arrays (explicitly shared)
    
      \param xData x data
      \param yData y data
    
      \sa QwtPointArrayData
    */
    void QwtPlotCurve::setSamples( const QVector<double> &xData,
        const QVector<double> &yData )
    {
        setData( new QwtPointArrayData( xData, yData ) );
    }
    
    #endif // !QWT_NO_COMPAT