qwt_plot_marker.cpp 14 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
5
 *
pixhawk's avatar
pixhawk committed
6 7 8 9
 * 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
10
#include "qwt_plot_marker.h"
pixhawk's avatar
pixhawk committed
11 12 13 14 15
#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
16
#include <qpainter.h>
pixhawk's avatar
pixhawk committed
17 18 19 20 21

class QwtPlotMarker::PrivateData
{
public:
    PrivateData():
Bryant's avatar
Bryant committed
22 23 24 25 26 27 28 29
        labelAlignment( Qt::AlignCenter ),
        labelOrientation( Qt::Horizontal ),
        spacing( 2 ),
        symbol( NULL ),
        style( QwtPlotMarker::NoLine ),
        xValue( 0.0 ),
        yValue( 0.0 )
    {
pixhawk's avatar
pixhawk committed
30 31
    }

Bryant's avatar
Bryant committed
32 33
    ~PrivateData()
    {
pixhawk's avatar
pixhawk committed
34 35 36 37
        delete symbol;
    }

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

pixhawk's avatar
pixhawk committed
42
    QPen pen;
Bryant's avatar
Bryant committed
43
    const QwtSymbol *symbol;
pixhawk's avatar
pixhawk committed
44 45 46 47 48 49
    LineStyle style;

    double xValue;
    double yValue;
};

Bryant's avatar
Bryant committed
50 51 52
//! Sets alignment to Qt::AlignCenter, and style to QwtPlotMarker::NoLine
QwtPlotMarker::QwtPlotMarker( const QString &title ):
    QwtPlotItem( QwtText( title ) )
pixhawk's avatar
pixhawk committed
53 54
{
    d_data = new PrivateData;
Bryant's avatar
Bryant committed
55 56 57 58 59 60 61 62 63
    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
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
}

//! 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
79
QPointF QwtPlotMarker::value() const
pixhawk's avatar
pixhawk committed
80
{
Bryant's avatar
Bryant committed
81
    return QPointF( d_data->xValue, d_data->yValue );
pixhawk's avatar
pixhawk committed
82 83 84
}

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

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

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

//! Set Value
Bryant's avatar
Bryant committed
103
void QwtPlotMarker::setValue( double x, double y )
pixhawk's avatar
pixhawk committed
104
{
Bryant's avatar
Bryant committed
105 106
    if ( x != d_data->xValue || y != d_data->yValue )
    {
107 108 109
        d_data->xValue = x;
        d_data->yValue = y;
        itemChanged();
pixhawk's avatar
pixhawk committed
110 111 112 113
    }
}

//! Set X Value
Bryant's avatar
Bryant committed
114
void QwtPlotMarker::setXValue( double x )
115
{
Bryant's avatar
Bryant committed
116
    setValue( x, d_data->yValue );
pixhawk's avatar
pixhawk committed
117 118 119
}

//! Set Y Value
Bryant's avatar
Bryant committed
120
void QwtPlotMarker::setYValue( double y )
121
{
Bryant's avatar
Bryant committed
122
    setValue( d_data->xValue, y );
pixhawk's avatar
pixhawk committed
123 124 125
}

/*!
Bryant's avatar
Bryant committed
126 127 128
  Draw the marker

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

    // draw lines
Bryant's avatar
Bryant committed
141 142

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

    // draw symbol
Bryant's avatar
Bryant committed
145 146 147 148 149 150 151 152 153 154
    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
155 156
    }

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

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

Bryant's avatar
Bryant committed
163 164 165
  \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
166

Bryant's avatar
Bryant committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
  \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
199

Bryant's avatar
Bryant committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213
/*!
  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
214

Bryant's avatar
Bryant committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
    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
262
            else
Bryant's avatar
Bryant committed
263 264 265 266
            {
                alignPos.setX( canvasRect.center().x() );
            }
            break;
pixhawk's avatar
pixhawk committed
267
        }
Bryant's avatar
Bryant committed
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
        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
309

Bryant's avatar
Bryant committed
310 311 312 313 314 315 316 317 318 319 320
    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
321
    }
Bryant's avatar
Bryant committed
322 323 324 325 326 327 328 329 330 331 332 333 334 335
    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
336 337 338 339
}

/*!
  \brief Set the line style
Bryant's avatar
Bryant committed
340
  \param style Line style. 
pixhawk's avatar
pixhawk committed
341 342
  \sa lineStyle()
*/
Bryant's avatar
Bryant committed
343
void QwtPlotMarker::setLineStyle( LineStyle style )
pixhawk's avatar
pixhawk committed
344
{
Bryant's avatar
Bryant committed
345 346 347 348 349
    if ( style != d_data->style )
    {
        d_data->style = style;

        legendChanged();
pixhawk's avatar
pixhawk committed
350 351 352 353 354 355
        itemChanged();
    }
}

/*!
  \return the line style
Bryant's avatar
Bryant committed
356
  \sa setLineStyle()
pixhawk's avatar
pixhawk committed
357
*/
358 359 360
QwtPlotMarker::LineStyle QwtPlotMarker::lineStyle() const
{
    return d_data->style;
pixhawk's avatar
pixhawk committed
361 362 363 364
}

/*!
  \brief Assign a symbol
Bryant's avatar
Bryant committed
365
  \param symbol New symbol
pixhawk's avatar
pixhawk committed
366 367
  \sa symbol()
*/
Bryant's avatar
Bryant committed
368
void QwtPlotMarker::setSymbol( const QwtSymbol *symbol )
pixhawk's avatar
pixhawk committed
369
{
Bryant's avatar
Bryant committed
370 371 372 373 374 375 376 377 378 379 380
    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
381 382 383 384 385 386
}

/*!
  \return the symbol
  \sa setSymbol(), QwtSymbol
*/
Bryant's avatar
Bryant committed
387
const QwtSymbol *QwtPlotMarker::symbol() const
388
{
Bryant's avatar
Bryant committed
389
    return d_data->symbol;
pixhawk's avatar
pixhawk committed
390 391 392 393
}

/*!
  \brief Set the label
Bryant's avatar
Bryant committed
394
  \param label Label text
pixhawk's avatar
pixhawk committed
395 396
  \sa label()
*/
Bryant's avatar
Bryant committed
397
void QwtPlotMarker::setLabel( const QwtText& label )
pixhawk's avatar
pixhawk committed
398
{
Bryant's avatar
Bryant committed
399 400
    if ( label != d_data->label )
    {
pixhawk's avatar
pixhawk committed
401 402 403 404 405 406 407 408 409
        d_data->label = label;
        itemChanged();
    }
}

/*!
  \return the label
  \sa setLabel()
*/
410 411 412
QwtText QwtPlotMarker::label() const
{
    return d_data->label;
pixhawk's avatar
pixhawk committed
413 414 415 416 417
}

/*!
  \brief Set the alignment of the label

Bryant's avatar
Bryant committed
418 419 420 421 422 423 424
  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
425

Bryant's avatar
Bryant committed
426 427
  \param align Alignment. 
  \sa labelAlignment(), labelOrientation()
pixhawk's avatar
pixhawk committed
428
*/
Bryant's avatar
Bryant committed
429
void QwtPlotMarker::setLabelAlignment( Qt::Alignment align )
pixhawk's avatar
pixhawk committed
430
{
Bryant's avatar
Bryant committed
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
    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
490
        return;
491

Bryant's avatar
Bryant committed
492
    d_data->spacing = spacing;
pixhawk's avatar
pixhawk committed
493 494 495 496
    itemChanged();
}

/*!
Bryant's avatar
Bryant committed
497 498
  \return the spacing
  \sa setSpacing()
pixhawk's avatar
pixhawk committed
499
*/
Bryant's avatar
Bryant committed
500
int QwtPlotMarker::spacing() const
501
{
Bryant's avatar
Bryant committed
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
    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
521 522 523
}

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

  \param pen New pen
pixhawk's avatar
pixhawk committed
527 528
  \sa linePen()
*/
Bryant's avatar
Bryant committed
529
void QwtPlotMarker::setLinePen( const QPen &pen )
pixhawk's avatar
pixhawk committed
530
{
Bryant's avatar
Bryant committed
531 532 533 534 535
    if ( pen != d_data->pen )
    {
        d_data->pen = pen;

        legendChanged();
pixhawk's avatar
pixhawk committed
536 537 538 539 540 541 542 543
        itemChanged();
    }
}

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

Bryant's avatar
Bryant committed
549
QRectF QwtPlotMarker::boundingRect() const
pixhawk's avatar
pixhawk committed
550
{
Bryant's avatar
Bryant committed
551
    return QRectF( d_data->xValue, d_data->yValue, 0.0, 0.0 );
pixhawk's avatar
pixhawk committed
552
}
Bryant's avatar
Bryant committed
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610

/*!
   \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;
}