Skip to content
qwt_plot_marker.cpp 14 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
pixhawk's avatar
pixhawk committed
 * 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_marker.h"
pixhawk's avatar
pixhawk committed
#include "qwt_painter.h"
#include "qwt_scale_map.h"
#include "qwt_symbol.h"
#include "qwt_text.h"
#include "qwt_math.h"
Bryant's avatar
Bryant committed
#include <qpainter.h>
pixhawk's avatar
pixhawk committed

class QwtPlotMarker::PrivateData
{
public:
    PrivateData():
Bryant's avatar
Bryant committed
        labelAlignment( Qt::AlignCenter ),
        labelOrientation( Qt::Horizontal ),
        spacing( 2 ),
        symbol( NULL ),
        style( QwtPlotMarker::NoLine ),
        xValue( 0.0 ),
        yValue( 0.0 )
    {
pixhawk's avatar
pixhawk committed
    }

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

    QwtText label;
Bryant's avatar
Bryant committed
    Qt::Alignment labelAlignment;
    Qt::Orientation labelOrientation;
    int spacing;

pixhawk's avatar
pixhawk committed
    QPen pen;
Bryant's avatar
Bryant committed
    const QwtSymbol *symbol;
pixhawk's avatar
pixhawk committed
    LineStyle style;

    double xValue;
    double yValue;
};

Bryant's avatar
Bryant committed
//! Sets alignment to Qt::AlignCenter, and style to QwtPlotMarker::NoLine
QwtPlotMarker::QwtPlotMarker( const QString &title ):
    QwtPlotItem( QwtText( title ) )
pixhawk's avatar
pixhawk committed
{
    d_data = new PrivateData;
Bryant's avatar
Bryant committed
    setZ( 30.0 );
}

//! Sets alignment to Qt::AlignCenter, and style to QwtPlotMarker::NoLine
QwtPlotMarker::QwtPlotMarker( const QwtText &title ):
    QwtPlotItem( title )
{
    d_data = new PrivateData;
    setZ( 30.0 );
pixhawk's avatar
pixhawk committed
}

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

//! \return QwtPlotItem::Rtti_PlotMarker
int QwtPlotMarker::rtti() const
{
    return QwtPlotItem::Rtti_PlotMarker;
}

//! Return Value
Bryant's avatar
Bryant committed
QPointF QwtPlotMarker::value() const
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    return QPointF( d_data->xValue, d_data->yValue );
pixhawk's avatar
pixhawk committed
}

//! Return x Value
double QwtPlotMarker::xValue() const
{
    return d_data->xValue;
pixhawk's avatar
pixhawk committed
}

//! Return y Value
double QwtPlotMarker::yValue() const
{
    return d_data->yValue;
pixhawk's avatar
pixhawk committed
}

//! Set Value
Bryant's avatar
Bryant committed
void QwtPlotMarker::setValue( const QPointF& pos )
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    setValue( pos.x(), pos.y() );
pixhawk's avatar
pixhawk committed
}

//! Set Value
Bryant's avatar
Bryant committed
void QwtPlotMarker::setValue( double x, double y )
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    if ( x != d_data->xValue || y != d_data->yValue )
    {
        d_data->xValue = x;
        d_data->yValue = y;
        itemChanged();
pixhawk's avatar
pixhawk committed
    }
}

//! Set X Value
Bryant's avatar
Bryant committed
void QwtPlotMarker::setXValue( double x )
Bryant's avatar
Bryant committed
    setValue( x, d_data->yValue );
pixhawk's avatar
pixhawk committed
}

//! Set Y Value
Bryant's avatar
Bryant committed
void QwtPlotMarker::setYValue( double y )
Bryant's avatar
Bryant committed
    setValue( d_data->xValue, y );
pixhawk's avatar
pixhawk committed
}

/*!
Bryant's avatar
Bryant committed
  Draw the marker

  \param painter Painter
pixhawk's avatar
pixhawk committed
  \param xMap x Scale Map
  \param yMap y Scale Map
Bryant's avatar
Bryant committed
  \param canvasRect Contents rectangle of the canvas in painter coordinates
pixhawk's avatar
pixhawk committed
*/
Bryant's avatar
Bryant committed
void QwtPlotMarker::draw( QPainter *painter,
    const QwtScaleMap &xMap, const QwtScaleMap &yMap,
    const QRectF &canvasRect ) const
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    const QPointF pos( xMap.transform( d_data->xValue ), 
        yMap.transform( d_data->yValue ) );
pixhawk's avatar
pixhawk committed

    // draw lines
Bryant's avatar
Bryant committed

    drawLines( painter, canvasRect, pos );
pixhawk's avatar
pixhawk committed

    // draw symbol
Bryant's avatar
Bryant committed
    if ( d_data->symbol &&
        ( d_data->symbol->style() != QwtSymbol::NoSymbol ) )
    {
        const QSizeF sz = d_data->symbol->size();

        const QRectF clipRect = canvasRect.adjusted( 
            -sz.width(), -sz.height(), sz.width(), sz.height() );

        if ( clipRect.contains( pos ) )
            d_data->symbol->drawSymbol( painter, pos );
pixhawk's avatar
pixhawk committed
    }

Bryant's avatar
Bryant committed
    drawLabel( painter, canvasRect, pos );
}
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
/*!
  Draw the lines marker
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
  \param painter Painter
  \param canvasRect Contents rectangle of the canvas in painter coordinates
  \param pos Position of the marker, translated into widget coordinates
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
  \sa drawLabel(), QwtSymbol::drawSymbol()
*/
void QwtPlotMarker::drawLines( QPainter *painter,
    const QRectF &canvasRect, const QPointF &pos ) const
{
    if ( d_data->style == NoLine )
        return;

    const bool doAlign = QwtPainter::roundingAlignment( painter );

    painter->setPen( d_data->pen );
    if ( d_data->style == QwtPlotMarker::HLine ||
        d_data->style == QwtPlotMarker::Cross )
    {
        double y = pos.y();
        if ( doAlign )
            y = qRound( y );

        QwtPainter::drawLine( painter, canvasRect.left(),
            y, canvasRect.right() - 1.0, y );
    }
    if ( d_data->style == QwtPlotMarker::VLine ||
        d_data->style == QwtPlotMarker::Cross )
    {
        double x = pos.x();
        if ( doAlign )
            x = qRound( x );

        QwtPainter::drawLine( painter, x,
            canvasRect.top(), x, canvasRect.bottom() - 1.0 );
    }
}
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
/*!
  Align and draw the text label of the marker

  \param painter Painter
  \param canvasRect Contents rectangle of the canvas in painter coordinates
  \param pos Position of the marker, translated into widget coordinates

  \sa drawLabel(), QwtSymbol::drawSymbol()
*/
void QwtPlotMarker::drawLabel( QPainter *painter,
    const QRectF &canvasRect, const QPointF &pos ) const
{
    if ( d_data->label.isEmpty() )
        return;
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    Qt::Alignment align = d_data->labelAlignment;
    QPointF alignPos = pos;

    QSizeF symbolOff( 0, 0 );

    switch ( d_data->style )
    {
        case QwtPlotMarker::VLine:
        {
            // In VLine-style the y-position is pointless and
            // the alignment flags are relative to the canvas

            if ( d_data->labelAlignment & Qt::AlignTop )
            {
                alignPos.setY( canvasRect.top() );
                align &= ~Qt::AlignTop;
                align |= Qt::AlignBottom;
            }
            else if ( d_data->labelAlignment & Qt::AlignBottom )
            {
                // In HLine-style the x-position is pointless and
                // the alignment flags are relative to the canvas

                alignPos.setY( canvasRect.bottom() - 1 );
                align &= ~Qt::AlignBottom;
                align |= Qt::AlignTop;
            }
            else
            {
                alignPos.setY( canvasRect.center().y() );
            }
            break;
        }
        case QwtPlotMarker::HLine:
        {
            if ( d_data->labelAlignment & Qt::AlignLeft )
            {
                alignPos.setX( canvasRect.left() );
                align &= ~Qt::AlignLeft;
                align |= Qt::AlignRight;
            }
            else if ( d_data->labelAlignment & Qt::AlignRight )
            {
                alignPos.setX( canvasRect.right() - 1 );
                align &= ~Qt::AlignRight;
                align |= Qt::AlignLeft;
            }
pixhawk's avatar
pixhawk committed
            else
Bryant's avatar
Bryant committed
            {
                alignPos.setX( canvasRect.center().x() );
            }
            break;
pixhawk's avatar
pixhawk committed
        }
Bryant's avatar
Bryant committed
        default:
        {
            if ( d_data->symbol &&
                ( d_data->symbol->style() != QwtSymbol::NoSymbol ) )
            {
                symbolOff = d_data->symbol->size() + QSizeF( 1, 1 );
                symbolOff /= 2;
            }
        }
    }

    qreal pw2 = d_data->pen.widthF() / 2.0;
    if ( pw2 == 0.0 )
        pw2 = 0.5;

    const int spacing = d_data->spacing;

    const qreal xOff = qMax( pw2, symbolOff.width() );
    const qreal yOff = qMax( pw2, symbolOff.height() );

    const QSizeF textSize = d_data->label.textSize( painter->font() );

    if ( align & Qt::AlignLeft )
    {
        alignPos.rx() -= xOff + spacing;
        if ( d_data->labelOrientation == Qt::Vertical )
            alignPos.rx() -= textSize.height();
        else
            alignPos.rx() -= textSize.width();
    }
    else if ( align & Qt::AlignRight )
    {
        alignPos.rx() += xOff + spacing;
    }
    else
    {
        if ( d_data->labelOrientation == Qt::Vertical )
            alignPos.rx() -= textSize.height() / 2;
        else
            alignPos.rx() -= textSize.width() / 2;
    }
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    if ( align & Qt::AlignTop )
    {
        alignPos.ry() -= yOff + spacing;
        if ( d_data->labelOrientation != Qt::Vertical )
            alignPos.ry() -= textSize.height();
    }
    else if ( align & Qt::AlignBottom )
    {
        alignPos.ry() += yOff + spacing;
        if ( d_data->labelOrientation == Qt::Vertical )
            alignPos.ry() += textSize.width();
pixhawk's avatar
pixhawk committed
    }
Bryant's avatar
Bryant committed
    else
    {
        if ( d_data->labelOrientation == Qt::Vertical )
            alignPos.ry() += textSize.width() / 2;
        else
            alignPos.ry() -= textSize.height() / 2;
    }

    painter->translate( alignPos.x(), alignPos.y() );
    if ( d_data->labelOrientation == Qt::Vertical )
        painter->rotate( -90.0 );

    const QRectF textRect( 0, 0, textSize.width(), textSize.height() );
    d_data->label.draw( painter, textRect );
pixhawk's avatar
pixhawk committed
}

/*!
  \brief Set the line style
Bryant's avatar
Bryant committed
  \param style Line style. 
pixhawk's avatar
pixhawk committed
  \sa lineStyle()
*/
Bryant's avatar
Bryant committed
void QwtPlotMarker::setLineStyle( LineStyle style )
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    if ( style != d_data->style )
    {
        d_data->style = style;

        legendChanged();
pixhawk's avatar
pixhawk committed
        itemChanged();
    }
}

/*!
  \return the line style
Bryant's avatar
Bryant committed
  \sa setLineStyle()
pixhawk's avatar
pixhawk committed
*/
QwtPlotMarker::LineStyle QwtPlotMarker::lineStyle() const
{
    return d_data->style;
pixhawk's avatar
pixhawk committed
}

/*!
  \brief Assign a symbol
Bryant's avatar
Bryant committed
  \param symbol New symbol
pixhawk's avatar
pixhawk committed
  \sa symbol()
*/
Bryant's avatar
Bryant committed
void QwtPlotMarker::setSymbol( const QwtSymbol *symbol )
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    if ( symbol != d_data->symbol )
    {
        delete d_data->symbol;
        d_data->symbol = symbol;

        if ( symbol )
            setLegendIconSize( symbol->boundingRect().size() );

        legendChanged();
        itemChanged();
    }
pixhawk's avatar
pixhawk committed
}

/*!
  \return the symbol
  \sa setSymbol(), QwtSymbol
*/
Bryant's avatar
Bryant committed
const QwtSymbol *QwtPlotMarker::symbol() const
Bryant's avatar
Bryant committed
    return d_data->symbol;
pixhawk's avatar
pixhawk committed
}

/*!
  \brief Set the label
Bryant's avatar
Bryant committed
  \param label Label text
pixhawk's avatar
pixhawk committed
  \sa label()
*/
Bryant's avatar
Bryant committed
void QwtPlotMarker::setLabel( const QwtText& label )
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    if ( label != d_data->label )
    {
pixhawk's avatar
pixhawk committed
        d_data->label = label;
        itemChanged();
    }
}

/*!
  \return the label
  \sa setLabel()
*/
QwtText QwtPlotMarker::label() const
{
    return d_data->label;
pixhawk's avatar
pixhawk committed
}

/*!
  \brief Set the alignment of the label

Bryant's avatar
Bryant committed
  In case of QwtPlotMarker::HLine the alignment is relative to the
  y position of the marker, but the horizontal flags correspond to the
  canvas rectangle. In case of QwtPlotMarker::VLine the alignment is
  relative to the x position of the marker, but the vertical flags
  correspond to the canvas rectangle.

  In all other styles the alignment is relative to the marker's position.
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
  \param align Alignment. 
  \sa labelAlignment(), labelOrientation()
pixhawk's avatar
pixhawk committed
*/
Bryant's avatar
Bryant committed
void QwtPlotMarker::setLabelAlignment( Qt::Alignment align )
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    if ( align != d_data->labelAlignment )
    {
        d_data->labelAlignment = align;
        itemChanged();
    }
}

/*!
  \return the label alignment
  \sa setLabelAlignment(), setLabelOrientation()
*/
Qt::Alignment QwtPlotMarker::labelAlignment() const
{
    return d_data->labelAlignment;
}

/*!
  \brief Set the orientation of the label

  When orientation is Qt::Vertical the label is rotated by 90.0 degrees
  ( from bottom to top ).

  \param orientation Orientation of the label

  \sa labelOrientation(), setLabelAlignment()
*/
void QwtPlotMarker::setLabelOrientation( Qt::Orientation orientation )
{
    if ( orientation != d_data->labelOrientation )
    {
        d_data->labelOrientation = orientation;
        itemChanged();
    }
}

/*!
  \return the label orientation
  \sa setLabelOrientation(), labelAlignment()
*/
Qt::Orientation QwtPlotMarker::labelOrientation() const
{
    return d_data->labelOrientation;
}

/*!
  \brief Set the spacing

  When the label is not centered on the marker position, the spacing
  is the distance between the position and the label.

  \param spacing Spacing
  \sa spacing(), setLabelAlignment()
*/
void QwtPlotMarker::setSpacing( int spacing )
{
    if ( spacing < 0 )
        spacing = 0;

    if ( spacing == d_data->spacing )
pixhawk's avatar
pixhawk committed
        return;
Bryant's avatar
Bryant committed
    d_data->spacing = spacing;
pixhawk's avatar
pixhawk committed
    itemChanged();
}

/*!
Bryant's avatar
Bryant committed
  \return the spacing
  \sa setSpacing()
pixhawk's avatar
pixhawk committed
*/
Bryant's avatar
Bryant committed
int QwtPlotMarker::spacing() const
Bryant's avatar
Bryant committed
    return d_data->spacing;
}

/*! 
  Build and assign a line pen
    
  In Qt5 the default pen width is 1.0 ( 0.0 in Qt4 ) what makes it
  non cosmetic ( see QPen::isCosmetic() ). This method has been introduced
  to hide this incompatibility.
    
  \param color Pen color
  \param width Pen width
  \param style Pen style
    
  \sa pen(), brush()
 */ 
void QwtPlotMarker::setLinePen( const QColor &color, qreal width, Qt::PenStyle style )
{   
    setLinePen( QPen( color, width, style ) );
pixhawk's avatar
pixhawk committed
}

/*!
Bryant's avatar
Bryant committed
  Specify a pen for the line.

  \param pen New pen
pixhawk's avatar
pixhawk committed
  \sa linePen()
*/
Bryant's avatar
Bryant committed
void QwtPlotMarker::setLinePen( const QPen &pen )
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    if ( pen != d_data->pen )
    {
        d_data->pen = pen;

        legendChanged();
pixhawk's avatar
pixhawk committed
        itemChanged();
    }
}

/*!
  \return the line pen
  \sa setLinePen()
*/
const QPen &QwtPlotMarker::linePen() const
{
    return d_data->pen;
pixhawk's avatar
pixhawk committed
}

Bryant's avatar
Bryant committed
QRectF QwtPlotMarker::boundingRect() const
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    return QRectF( d_data->xValue, d_data->yValue, 0.0, 0.0 );
pixhawk's avatar
pixhawk committed
}
Bryant's avatar
Bryant committed

/*!
   \return Icon representing the marker on the legend

   \param index Index of the legend entry 
                ( usually there is only one )
   \param size Icon size

   \sa setLegendIconSize(), legendData()
*/
QwtGraphic QwtPlotMarker::legendIcon( int index,
    const QSizeF &size ) const
{
    Q_UNUSED( index );

    if ( size.isEmpty() )
        return QwtGraphic();

    QwtGraphic icon;
    icon.setDefaultSize( size );
    icon.setRenderHint( QwtGraphic::RenderPensUnscaled, true );

    QPainter painter( &icon );
    painter.setRenderHint( QPainter::Antialiasing,
        testRenderHint( QwtPlotItem::RenderAntialiased ) );

    if ( d_data->style != QwtPlotMarker::NoLine )
    {
        painter.setPen( d_data->pen );

        if ( d_data->style == QwtPlotMarker::HLine ||
            d_data->style == QwtPlotMarker::Cross )
        {
            const double y = 0.5 * size.height();

            QwtPainter::drawLine( &painter, 
                0.0, y, size.width(), y );
        }

        if ( d_data->style == QwtPlotMarker::VLine ||
            d_data->style == QwtPlotMarker::Cross )
        {
            const double x = 0.5 * size.width();

            QwtPainter::drawLine( &painter, 
                x, 0.0, x, size.height() );
        }
    }

    if ( d_data->symbol )
    {
        const QRect r( 0.0, 0.0, size.width(), size.height() );
        d_data->symbol->drawSymbol( &painter, r );
    }

    return icon;
}