qwt_symbol.cpp 43.2 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 10
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#include "qwt_symbol.h"
Bryant's avatar
Bryant committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 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 262 263 264 265 266 267 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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 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 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 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 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
#include "qwt_painter.h"
#include "qwt_graphic.h"
#include <qapplication.h>
#include <qpainter.h>
#include <qpainterpath.h>
#include <qpixmap.h>
#include <qpaintengine.h>
#include <qmath.h>
#ifndef QWT_NO_SVG
#include <qsvgrenderer.h>
#endif

namespace QwtTriangle
{
    enum Type
    {
        Left,
        Right,
        Up,
        Down
    };
}

static QwtGraphic qwtPathGraphic( const QPainterPath &path, 
    const QPen &pen, const QBrush& brush )
{
    QwtGraphic graphic;
    graphic.setRenderHint( QwtGraphic::RenderPensUnscaled );

    QPainter painter( &graphic );
    painter.setPen( pen );
    painter.setBrush( brush );
    painter.drawPath( path );
    painter.end();

    return graphic;
}

static inline QRectF qwtScaledBoundingRect( 
    const QwtGraphic &graphic, const QSizeF size )
{
    QSizeF scaledSize = size;
    if ( scaledSize.isEmpty() )
        scaledSize = graphic.defaultSize();
        
    const QSizeF sz = graphic.controlPointRect().size();

    double sx = 1.0;
    if ( sz.width() > 0.0 )
        sx = scaledSize.width() / sz.width();
    
    double sy = 1.0;
    if ( sz.height() > 0.0 )
        sy = scaledSize.height() / sz.height();

    return graphic.scaledBoundingRect( sx, sy );
}

static inline void qwtDrawPixmapSymbols( QPainter *painter,
    const QPointF *points, int numPoints, const QwtSymbol &symbol )
{
    QSize size = symbol.size();
    if ( size.isEmpty() )
        size = symbol.pixmap().size();

    const QTransform transform = painter->transform();
    if ( transform.isScaling() )
    {
        const QRect r( 0, 0, size.width(), size.height() );
        size = transform.mapRect( r ).size();
    }

    QPixmap pm = symbol.pixmap();
    if ( pm.size() != size )
        pm = pm.scaled( size );
    
    QPointF pinPoint( 0.5 * size.width(), 0.5 * size.height() );
    if ( symbol.isPinPointEnabled() )
        pinPoint = symbol.pinPoint();

    painter->resetTransform();

    for ( int i = 0; i < numPoints; i++ )
    {
        const QPointF pos = transform.map( points[i] ) - pinPoint;

        QwtPainter::drawPixmap( painter, 
            QRect( pos.toPoint(), pm.size() ), pm );
    }
}

#ifndef QWT_NO_SVG

static inline void qwtDrawSvgSymbols( QPainter *painter, 
    const QPointF *points, int numPoints, 
    QSvgRenderer *renderer, const QwtSymbol &symbol )
{
    if ( renderer == NULL || !renderer->isValid() )
        return;

    const QRectF viewBox = renderer->viewBoxF();
    if ( viewBox.isEmpty() )
        return;

    QSizeF sz = symbol.size();
    if ( !sz.isValid() )
        sz = viewBox.size();

    const double sx = sz.width() / viewBox.width();
    const double sy = sz.height() / viewBox.height();

    QPointF pinPoint = viewBox.center();
    if ( symbol.isPinPointEnabled() )
        pinPoint = symbol.pinPoint();

    const double dx = sx * ( pinPoint.x() - viewBox.left() );
    const double dy = sy * ( pinPoint.y() - viewBox.top() );

    for ( int i = 0; i < numPoints; i++ )
    {
        const double x = points[i].x() - dx;
        const double y = points[i].y() - dy;

        renderer->render( painter, 
            QRectF( x, y, sz.width(), sz.height() ) );
    }
}

#endif

static inline void qwtDrawGraphicSymbols( QPainter *painter, 
    const QPointF *points, int numPoints, const QwtGraphic &graphic,
    const QwtSymbol &symbol )
{
    const QRectF pointRect = graphic.controlPointRect();
    if ( pointRect.isEmpty() )
        return;

    double sx = 1.0;
    double sy = 1.0;

    const QSize sz = symbol.size();
    if ( sz.isValid() )
    {
        sx = sz.width() / pointRect.width();
        sy = sz.height() / pointRect.height();
    }

    QPointF pinPoint = pointRect.center();
    if ( symbol.isPinPointEnabled() )
        pinPoint = symbol.pinPoint();

    const QTransform transform = painter->transform();

    for ( int i = 0; i < numPoints; i++ )
    {
        QTransform tr = transform;
        tr.translate( points[i].x(), points[i].y() );
        tr.scale( sx, sy );
        tr.translate( -pinPoint.x(), -pinPoint.y() );

        painter->setTransform( tr );

        graphic.render( painter );
    }

    painter->setTransform( transform );
}

static inline void qwtDrawEllipseSymbols( QPainter *painter,
    const QPointF *points, int numPoints, const QwtSymbol &symbol )
{
    painter->setBrush( symbol.brush() );
    painter->setPen( symbol.pen() );

    const QSize size = symbol.size();

    if ( QwtPainter::roundingAlignment( painter ) )
    {
        const int sw = size.width();
        const int sh = size.height();
        const int sw2 = size.width() / 2;
        const int sh2 = size.height() / 2;

        for ( int i = 0; i < numPoints; i++ )
        {
            const int x = qRound( points[i].x() );
            const int y = qRound( points[i].y() );

            const QRectF r( x - sw2, y - sh2, sw, sh );
            QwtPainter::drawEllipse( painter, r );
        }
    }
    else
    {
        const double sw = size.width();
        const double sh = size.height();
        const double sw2 = 0.5 * size.width();
        const double sh2 = 0.5 * size.height();

        for ( int i = 0; i < numPoints; i++ )
        {
            const double x = points[i].x();
            const double y = points[i].y();

            const QRectF r( x - sw2, y - sh2, sw, sh );
            QwtPainter::drawEllipse( painter, r );
        }
    }
}

static inline void qwtDrawRectSymbols( QPainter *painter,
    const QPointF *points, int numPoints, const QwtSymbol &symbol )
{
    const QSize size = symbol.size();

    QPen pen = symbol.pen();
    pen.setJoinStyle( Qt::MiterJoin );
    painter->setPen( pen );
    painter->setBrush( symbol.brush() );
    painter->setRenderHint( QPainter::Antialiasing, false );

    if ( QwtPainter::roundingAlignment( painter ) )
    {
        const int sw = size.width();
        const int sh = size.height();
        const int sw2 = size.width() / 2;
        const int sh2 = size.height() / 2;

        for ( int i = 0; i < numPoints; i++ )
        {
            const int x = qRound( points[i].x() );
            const int y = qRound( points[i].y() );

            const QRect r( x - sw2, y - sh2, sw, sh );
            QwtPainter::drawRect( painter, r );
        }
    }
    else
    {
        const double sw = size.width();
        const double sh = size.height();
        const double sw2 = 0.5 * size.width();
        const double sh2 = 0.5 * size.height();

        for ( int i = 0; i < numPoints; i++ )
        {
            const double x = points[i].x();
            const double y = points[i].y();

            const QRectF r( x - sw2, y - sh2, sw, sh );
            QwtPainter::drawRect( painter, r );
        }
    }
}

static inline void qwtDrawDiamondSymbols( QPainter *painter,
    const QPointF *points, int numPoints, const QwtSymbol &symbol )
{
    const QSize size = symbol.size();

    QPen pen = symbol.pen();
    pen.setJoinStyle( Qt::MiterJoin );
    painter->setPen( pen );
    painter->setBrush( symbol.brush() );

    if ( QwtPainter::roundingAlignment( painter ) )
    {
        for ( int i = 0; i < numPoints; i++ )
        {
            const int x = qRound( points[i].x() );
            const int y = qRound( points[i].y() );

            const int x1 = x - size.width() / 2;
            const int y1 = y - size.height() / 2;
            const int x2 = x1 + size.width();
            const int y2 = y1 + size.height();

            QPolygonF polygon;
            polygon += QPointF( x, y1 );
            polygon += QPointF( x1, y );
            polygon += QPointF( x, y2 );
            polygon += QPointF( x2, y );

            QwtPainter::drawPolygon( painter, polygon );
        }
    }
    else
    {
        for ( int i = 0; i < numPoints; i++ )
        {
            const QPointF &pos = points[i];

            const double x1 = pos.x() - 0.5 * size.width();
            const double y1 = pos.y() - 0.5 * size.height();
            const double x2 = x1 + size.width();
            const double y2 = y1 + size.height();

            QPolygonF polygon;
            polygon += QPointF( pos.x(), y1 );
            polygon += QPointF( x2, pos.y() );
            polygon += QPointF( pos.x(), y2 );
            polygon += QPointF( x1, pos.y() );

            QwtPainter::drawPolygon( painter, polygon );
        }
    }
}

static inline void qwtDrawTriangleSymbols(
    QPainter *painter, QwtTriangle::Type type,
    const QPointF *points, int numPoints,
    const QwtSymbol &symbol )
{
    const QSize size = symbol.size();

    QPen pen = symbol.pen();
    pen.setJoinStyle( Qt::MiterJoin );
    painter->setPen( pen );

    painter->setBrush( symbol.brush() );

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

    double sw2 = 0.5 * size.width();
    double sh2 = 0.5 * size.height();

    if ( doAlign )
    {
        sw2 = qFloor( sw2 );
        sh2 = qFloor( sh2 );
    }

    QPolygonF triangle( 3 );
    QPointF *trianglePoints = triangle.data();

    for ( int i = 0; i < numPoints; i++ )
    {
        const QPointF &pos = points[i];

        double x = pos.x();
        double y = pos.y();

        if ( doAlign )
        {
            x = qRound( x );
            y = qRound( y );
        }

        const double x1 = x - sw2;
        const double x2 = x1 + size.width();
        const double y1 = y - sh2;
        const double y2 = y1 + size.height();

        switch ( type )
        {
            case QwtTriangle::Left:
            {
                trianglePoints[0].rx() = x2;
                trianglePoints[0].ry() = y1;

                trianglePoints[1].rx() = x1;
                trianglePoints[1].ry() = y;

                trianglePoints[2].rx() = x2;
                trianglePoints[2].ry() = y2;

                break;
            }
            case QwtTriangle::Right:
            {
                trianglePoints[0].rx() = x1;
                trianglePoints[0].ry() = y1;

                trianglePoints[1].rx() = x2;
                trianglePoints[1].ry() = y;

                trianglePoints[2].rx() = x1;
                trianglePoints[2].ry() = y2;

                break;
            }
            case QwtTriangle::Up:
            {
                trianglePoints[0].rx() = x1;
                trianglePoints[0].ry() = y2;

                trianglePoints[1].rx() = x;
                trianglePoints[1].ry() = y1;

                trianglePoints[2].rx() = x2;
                trianglePoints[2].ry() = y2;

                break;
            }
            case QwtTriangle::Down:
            {
                trianglePoints[0].rx() = x1;
                trianglePoints[0].ry() = y1;

                trianglePoints[1].rx() = x;
                trianglePoints[1].ry() = y2;

                trianglePoints[2].rx() = x2;
                trianglePoints[2].ry() = y1;

                break;
            }
        }
        QwtPainter::drawPolygon( painter, triangle );
    }
}

static inline void qwtDrawLineSymbols(
    QPainter *painter, int orientations,
    const QPointF *points, int numPoints, const QwtSymbol &symbol )
{
    const QSize size = symbol.size();

    int off = 0;

    QPen pen = symbol.pen();
    if ( pen.width() > 1 )
    {
        pen.setCapStyle( Qt::FlatCap );
        off = 1;
    }

    painter->setPen( pen );
    painter->setRenderHint( QPainter::Antialiasing, false );

    if ( QwtPainter::roundingAlignment( painter ) )
    {
        const int sw = qFloor( size.width() );
        const int sh = qFloor( size.height() );
        const int sw2 = size.width() / 2;
        const int sh2 = size.height() / 2;

        for ( int i = 0; i < numPoints; i++ )
        {
            if ( orientations & Qt::Horizontal )
            {
                const int x = qRound( points[i].x() ) - sw2;
                const int y = qRound( points[i].y() );

                QwtPainter::drawLine( painter, x, y, x + sw + off, y );
            }
            if ( orientations & Qt::Vertical )
            {
                const int x = qRound( points[i].x() );
                const int y = qRound( points[i].y() ) - sh2;

                QwtPainter::drawLine( painter, x, y, x, y + sh + off );
            }
        }
    }
    else
    {
        const double sw = size.width();
        const double sh = size.height();
        const double sw2 = 0.5 * size.width();
        const double sh2 = 0.5 * size.height();

        for ( int i = 0; i < numPoints; i++ )
        {
            if ( orientations & Qt::Horizontal )
            {
                const double x = points[i].x() - sw2;
                const double y = points[i].y();

                QwtPainter::drawLine( painter, x, y, x + sw, y );
            }
            if ( orientations & Qt::Vertical )
            {
                const double y = points[i].y() - sh2;
                const double x = points[i].x();

                QwtPainter::drawLine( painter, x, y, x, y + sh );
            }
        }
    }
}

static inline void qwtDrawXCrossSymbols( QPainter *painter,
    const QPointF *points, int numPoints, const QwtSymbol &symbol )
{
    const QSize size = symbol.size();
    int off = 0;

    QPen pen = symbol.pen();
    if ( pen.width() > 1 )
    {
        pen.setCapStyle( Qt::FlatCap );
        off = 1;
    }
    painter->setPen( pen );


    if ( QwtPainter::roundingAlignment( painter ) )
    {
        const int sw = size.width();
        const int sh = size.height();
        const int sw2 = size.width() / 2;
        const int sh2 = size.height() / 2;

        for ( int i = 0; i < numPoints; i++ )
        {
            const QPointF &pos = points[i];

            const int x = qRound( pos.x() );
            const int y = qRound( pos.y() );

            const int x1 = x - sw2;
            const int x2 = x1 + sw + off;
            const int y1 = y - sh2;
            const int y2 = y1 + sh + off;

            QwtPainter::drawLine( painter, x1, y1, x2, y2 );
            QwtPainter::drawLine( painter, x2, y1, x1, y2 );
        }
    }
    else
    {
        const double sw = size.width();
        const double sh = size.height();
        const double sw2 = 0.5 * size.width();
        const double sh2 = 0.5 * size.height();

        for ( int i = 0; i < numPoints; i++ )
        {
            const QPointF &pos = points[i];

            const double x1 = pos.x() - sw2;
            const double x2 = x1 + sw;
            const double y1 = pos.y() - sh2;
            const double y2 = y1 + sh;

            QwtPainter::drawLine( painter, x1, y1, x2, y2 );
            QwtPainter::drawLine( painter, x1, y2, x2, y1 );
        }
    }
}

static inline void qwtDrawStar1Symbols( QPainter *painter,
    const QPointF *points, int numPoints, const QwtSymbol &symbol )
{
    const QSize size = symbol.size();
    painter->setPen( symbol.pen() );

    if ( QwtPainter::roundingAlignment( painter ) )
    {
        QRect r( 0, 0, size.width(), size.height() );

        for ( int i = 0; i < numPoints; i++ )
        {
            r.moveCenter( points[i].toPoint() );

            const double sqrt1_2 = 0.70710678118654752440; /* 1/sqrt(2) */

            const double d1 = r.width() / 2.0 * ( 1.0 - sqrt1_2 );

            QwtPainter::drawLine( painter,
                qRound( r.left() + d1 ), qRound( r.top() + d1 ),
                qRound( r.right() - d1 ), qRound( r.bottom() - d1 ) );
            QwtPainter::drawLine( painter,
                qRound( r.left() + d1 ), qRound( r.bottom() - d1 ),
                qRound( r .right() - d1), qRound( r.top() + d1 ) );

            const QPoint c = r.center();

            QwtPainter::drawLine( painter,
                c.x(), r.top(), c.x(), r.bottom() );
            QwtPainter::drawLine( painter,
                r.left(), c.y(), r.right(), c.y() );
        }
    }
    else
    {
        QRectF r( 0, 0, size.width(), size.height() );

        for ( int i = 0; i < numPoints; i++ )
        {
            r.moveCenter( points[i] );

            const double sqrt1_2 = 0.70710678118654752440; /* 1/sqrt(2) */

            const QPointF c = r.center();
            const double d1  = r.width() / 2.0 * ( 1.0 - sqrt1_2 );

            QwtPainter::drawLine( painter,
                r.left() + d1, r.top() + d1,
                r.right() - d1, r.bottom() - d1 );
            QwtPainter::drawLine( painter,
                r.left() + d1, r.bottom() - d1,
                r.right() - d1, r.top() + d1 );
            QwtPainter::drawLine( painter,
                c.x(), r.top(),
                c.x(), r.bottom() );
            QwtPainter::drawLine( painter,
                r.left(), c.y(),
                r.right(), c.y() );
        }
    }
}

static inline void qwtDrawStar2Symbols( QPainter *painter,
    const QPointF *points, int numPoints, const QwtSymbol &symbol )
{
    QPen pen = symbol.pen();
    if ( pen.width() > 1 )
        pen.setCapStyle( Qt::FlatCap );
    pen.setJoinStyle( Qt::MiterJoin );
    painter->setPen( pen );

    painter->setBrush( symbol.brush() );

    const double cos30 = 0.866025; // cos(30°)

    const double dy = 0.25 * symbol.size().height();
    const double dx = 0.5 * symbol.size().width() * cos30 / 3.0;

    QPolygonF star( 12 );
    QPointF *starPoints = star.data();

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

    for ( int i = 0; i < numPoints; i++ )
    {
        double x = points[i].x();
        double y = points[i].y();
        if ( doAlign )
        {
            x = qRound( x );
            y = qRound( y );
        }

        double x1 = x - 3 * dx;
        double y1 = y - 2 * dy;
        if ( doAlign )
        {
            x1 = qRound( x - 3 * dx );
            y1 = qRound( y - 2 * dy );
        }

        const double x2 = x1 + 1 * dx;
        const double x3 = x1 + 2 * dx;
        const double x4 = x1 + 3 * dx;
        const double x5 = x1 + 4 * dx;
        const double x6 = x1 + 5 * dx;
        const double x7 = x1 + 6 * dx;

        const double y2 = y1 + 1 * dy;
        const double y3 = y1 + 2 * dy;
        const double y4 = y1 + 3 * dy;
        const double y5 = y1 + 4 * dy;

        starPoints[0].rx() = x4;
        starPoints[0].ry() = y1;

        starPoints[1].rx() = x5;
        starPoints[1].ry() = y2;

        starPoints[2].rx() = x7;
        starPoints[2].ry() = y2;

        starPoints[3].rx() = x6;
        starPoints[3].ry() = y3;

        starPoints[4].rx() = x7;
        starPoints[4].ry() = y4;

        starPoints[5].rx() = x5;
        starPoints[5].ry() = y4;

        starPoints[6].rx() = x4;
        starPoints[6].ry() = y5;

        starPoints[7].rx() = x3;
        starPoints[7].ry() = y4;

        starPoints[8].rx() = x1;
        starPoints[8].ry() = y4;

        starPoints[9].rx() = x2;
        starPoints[9].ry() = y3;

        starPoints[10].rx() = x1;
        starPoints[10].ry() = y2;

        starPoints[11].rx() = x3;
        starPoints[11].ry() = y2;

        QwtPainter::drawPolygon( painter, star );
    }
}

static inline void qwtDrawHexagonSymbols( QPainter *painter,
    const QPointF *points, int numPoints, const QwtSymbol &symbol )
{
    painter->setBrush( symbol.brush() );
    painter->setPen( symbol.pen() );

    const double cos30 = 0.866025; // cos(30°)
    const double dx = 0.5 * ( symbol.size().width() - cos30 );

    const double dy = 0.25 * symbol.size().height();

    QPolygonF hexaPolygon( 6 );
    QPointF *hexaPoints = hexaPolygon.data();

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

    for ( int i = 0; i < numPoints; i++ )
    {
        double x = points[i].x();
        double y = points[i].y();
        if ( doAlign )
        {
            x = qRound( x );
            y = qRound( y );
        }

        double x1 = x - dx;
        double y1 = y - 2 * dy;
        if ( doAlign )
        {
            x1 = qCeil( x1 );
            y1 = qCeil( y1 );
        }

        const double x2 = x1 + 1 * dx;
        const double x3 = x1 + 2 * dx;

        const double y2 = y1 + 1 * dy;
        const double y3 = y1 + 3 * dy;
        const double y4 = y1 + 4 * dy;

        hexaPoints[0].rx() = x2;
        hexaPoints[0].ry() = y1;

        hexaPoints[1].rx() = x3;
        hexaPoints[1].ry() = y2;

        hexaPoints[2].rx() = x3;
        hexaPoints[2].ry() = y3;

        hexaPoints[3].rx() = x2;
        hexaPoints[3].ry() = y4;

        hexaPoints[4].rx() = x1;
        hexaPoints[4].ry() = y3;

        hexaPoints[5].rx() = x1;
        hexaPoints[5].ry() = y2;

        QwtPainter::drawPolygon( painter, hexaPolygon );
    }
}

class QwtSymbol::PrivateData
{
public:
    PrivateData( QwtSymbol::Style st, const QBrush &br,
            const QPen &pn, const QSize &sz ):
        style( st ),
        size( sz ),
        brush( br ),
        pen( pn ),
        isPinPointEnabled( false )
    {
        cache.policy = QwtSymbol::AutoCache;
#ifndef QWT_NO_SVG
        svg.renderer = NULL;
#endif
    }

    ~PrivateData()
    {
#ifndef QWT_NO_SVG
        delete svg.renderer;
#endif
    }

    Style style;
    QSize size;
    QBrush brush;
    QPen pen;

    bool isPinPointEnabled;
    QPointF pinPoint;

    struct Path
    {
        QPainterPath path;
        QwtGraphic graphic;

    } path;

    struct Pixmap
    {
        QPixmap pixmap;

    } pixmap;

    struct Graphic
    {
        QwtGraphic graphic;

    } graphic;

#ifndef QWT_NO_SVG
    struct SVG
    {
        QSvgRenderer *renderer;
    } svg;
#endif

    struct PaintCache
    {
        QwtSymbol::CachePolicy policy;
        QPixmap pixmap;

    } cache;
};
pixhawk's avatar
pixhawk committed
835 836 837

/*!
  Default Constructor
Bryant's avatar
Bryant committed
838
  \param style Symbol Style
pixhawk's avatar
pixhawk committed
839 840 841 842

  The symbol is constructed with gray interior,
  black outline with zero width, no size and style 'NoSymbol'.
*/
Bryant's avatar
Bryant committed
843
QwtSymbol::QwtSymbol( Style style )
pixhawk's avatar
pixhawk committed
844
{
Bryant's avatar
Bryant committed
845 846
    d_data = new PrivateData( style, QBrush( Qt::gray ),
        QPen( Qt::black, 0 ), QSize() );
pixhawk's avatar
pixhawk committed
847 848 849 850 851 852
}

/*!
  \brief Constructor
  \param style Symbol Style
  \param brush brush to fill the interior
853
  \param pen outline pen
pixhawk's avatar
pixhawk committed
854
  \param size size
Bryant's avatar
Bryant committed
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875

  \sa setStyle(), setBrush(), setPen(), setSize()
*/
QwtSymbol::QwtSymbol( QwtSymbol::Style style, const QBrush &brush,
    const QPen &pen, const QSize &size )
{
    d_data = new PrivateData( style, brush, pen, size );
}

/*!
  \brief Constructor

  The symbol gets initialized by a painter path. The style is
  set to QwtSymbol::Path, the size is set to empty ( the path
  is displayed unscaled ).

  \param path painter path
  \param brush brush to fill the interior
  \param pen outline pen

  \sa setPath(), setBrush(), setPen(), setSize()
pixhawk's avatar
pixhawk committed
876
*/
Bryant's avatar
Bryant committed
877 878 879

QwtSymbol::QwtSymbol( const QPainterPath &path, 
    const QBrush &brush, const QPen &pen )
pixhawk's avatar
pixhawk committed
880
{
Bryant's avatar
Bryant committed
881 882
    d_data = new PrivateData( QwtSymbol::Path, brush, pen, QSize() );
    setPath( path );
pixhawk's avatar
pixhawk committed
883 884 885 886 887
}

//! Destructor
QwtSymbol::~QwtSymbol()
{
Bryant's avatar
Bryant committed
888
    delete d_data;
pixhawk's avatar
pixhawk committed
889 890
}

Bryant's avatar
Bryant committed
891 892 893 894 895 896 897 898 899 900
/*!
  Change the cache policy

  The default policy is AutoCache

  \param policy Cache policy
  \sa CachePolicy, cachePolicy()
*/
void QwtSymbol::setCachePolicy(
    QwtSymbol::CachePolicy policy )
pixhawk's avatar
pixhawk committed
901
{
Bryant's avatar
Bryant committed
902 903 904 905 906 907
    if ( d_data->cache.policy != policy )
    {
        d_data->cache.policy = policy;
        invalidateCache();
    }
}
pixhawk's avatar
pixhawk committed
908

Bryant's avatar
Bryant committed
909 910 911 912 913 914 915
/*!
  \return Cache policy
  \sa CachePolicy, setCachePolicy()
*/
QwtSymbol::CachePolicy QwtSymbol::cachePolicy() const
{
    return d_data->cache.policy;
pixhawk's avatar
pixhawk committed
916 917
}

Bryant's avatar
Bryant committed
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
/*!
  \brief Set a painter path as symbol

  The symbol is represented by a painter path, where the 
  origin ( 0, 0 ) of the path coordinate system is mapped to
  the position of the symbol.

  When the symbol has valid size the painter path gets scaled
  to fit into the size. Otherwise the symbol size depends on
  the bounding rectangle of the path.

  The following code defines a symbol drawing an arrow:

  \verbatim
#include <qwt_symbol.h>

QwtSymbol *symbol = new QwtSymbol();

QPen pen( Qt::black, 2 );
pen.setJoinStyle( Qt::MiterJoin );

symbol->setPen( pen );
symbol->setBrush( Qt::red );

QPainterPath path;
path.moveTo( 0, 8 );
path.lineTo( 0, 5 );
path.lineTo( -3, 5 );
path.lineTo( 0, 0 );
path.lineTo( 3, 5 );
path.lineTo( 0, 5 );

QTransform transform;
transform.rotate( -30.0 );
path = transform.map( path );

symbol->setPath( path );
symbol->setPinPoint( QPointF( 0.0, 0.0 ) );

setSize( 10, 14 );
\endverbatim

  \param path Painter path

  \note The style is implicitely set to QwtSymbol::Path.
  \sa path(), setSize()
 */
void QwtSymbol::setPath( const QPainterPath &path )
{
    d_data->style = QwtSymbol::Path;
    d_data->path.path = path;
    d_data->path.graphic.reset();
}

/*!
   \return Painter path for displaying the symbol
   \sa setPath()
*/
const QPainterPath &QwtSymbol::path() const
{
    return d_data->path.path;
}

/*!
  Set a pixmap as symbol

  \param pixmap Pixmap

  \sa pixmap(), setGraphic()

  \note the style() is set to QwtSymbol::Pixmap
  \note brush() and pen() have no effect
 */
void QwtSymbol::setPixmap( const QPixmap &pixmap )
{
    d_data->style = QwtSymbol::Pixmap;
    d_data->pixmap.pixmap = pixmap;
}

/*!
  \return Assigned pixmap
  \sa setPixmap()
 */
const QPixmap &QwtSymbol::pixmap() const
{
    return d_data->pixmap.pixmap;
}

/*!
  Set a graphic as symbol

  \param graphic Graphic

  \sa graphic(), setPixmap()

  \note the style() is set to QwtSymbol::Graphic
  \note brush() and pen() have no effect
 */
void QwtSymbol::setGraphic( const QwtGraphic &graphic )
{
    d_data->style = QwtSymbol::Graphic;
    d_data->graphic.graphic = graphic;
}

/*!
  \return Assigned graphic
  \sa setGraphic()
 */
const QwtGraphic &QwtSymbol::graphic() const
{
    return d_data->graphic.graphic;
}

#ifndef QWT_NO_SVG

/*!
  Set a SVG icon as symbol

  \param svgDocument SVG icon

  \sa setGraphic(), setPixmap()

  \note the style() is set to QwtSymbol::SvgDocument
  \note brush() and pen() have no effect
 */
void QwtSymbol::setSvgDocument( const QByteArray &svgDocument )
{
    d_data->style = QwtSymbol::SvgDocument;
    if ( d_data->svg.renderer == NULL )
        d_data->svg.renderer = new QSvgRenderer();

    d_data->svg.renderer->load( svgDocument );
}

#endif

pixhawk's avatar
pixhawk committed
1054 1055 1056 1057 1058 1059
/*!
  \brief Specify the symbol's size

  If the 'h' parameter is left out or less than 0,
  and the 'w' parameter is greater than or equal to 0,
  the symbol size will be set to (w,w).
Bryant's avatar
Bryant committed
1060 1061 1062 1063 1064

  \param width Width
  \param height Height (defaults to -1)

  \sa size()
pixhawk's avatar
pixhawk committed
1065
*/
Bryant's avatar
Bryant committed
1066
void QwtSymbol::setSize( int width, int height )
pixhawk's avatar
pixhawk committed
1067
{
Bryant's avatar
Bryant committed
1068 1069 1070 1071
    if ( ( width >= 0 ) && ( height < 0 ) )
        height = width;

    setSize( QSize( width, height ) );
pixhawk's avatar
pixhawk committed
1072 1073
}

Bryant's avatar
Bryant committed
1074 1075 1076 1077 1078 1079 1080
/*!
   Set the symbol's size
   \param size Size

   \sa size()
*/
void QwtSymbol::setSize( const QSize &size )
pixhawk's avatar
pixhawk committed
1081
{
Bryant's avatar
Bryant committed
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
    if ( size.isValid() && size != d_data->size )
    {
        d_data->size = size;
        invalidateCache();
    }
}

/*!
   \return Size
   \sa setSize()
*/
const QSize& QwtSymbol::size() const
{
    return d_data->size;
pixhawk's avatar
pixhawk committed
1096 1097 1098 1099 1100 1101
}

/*!
  \brief Assign a brush

  The brush is used to draw the interior of the symbol.
Bryant's avatar
Bryant committed
1102 1103 1104
  \param brush Brush

  \sa brush()
pixhawk's avatar
pixhawk committed
1105
*/
Bryant's avatar
Bryant committed
1106
void QwtSymbol::setBrush( const QBrush &brush )
pixhawk's avatar
pixhawk committed
1107
{
Bryant's avatar
Bryant committed
1108 1109 1110 1111 1112 1113 1114 1115
    if ( brush != d_data->brush )
    {
        d_data->brush = brush;
        invalidateCache();

        if ( d_data->style == QwtSymbol::Path )
            d_data->path.graphic.reset();
    }
pixhawk's avatar
pixhawk committed
1116 1117 1118
}

/*!
Bryant's avatar
Bryant committed
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
  \return Brush
  \sa setBrush()
*/
const QBrush& QwtSymbol::brush() const
{
    return d_data->brush;
}

/*!
  Build and assign a 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 QwtSymbol::setPen( const QColor &color,
    qreal width, Qt::PenStyle style )
{
    setPen( QPen( color, width, style ) );
}

/*!
  Assign a pen
pixhawk's avatar
pixhawk committed
1148 1149 1150

  The pen is used to draw the symbol's outline.

Bryant's avatar
Bryant committed
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
  \param pen Pen
  \sa pen(), setBrush()
*/
void QwtSymbol::setPen( const QPen &pen )
{
    if ( pen != d_data->pen )
    {
        d_data->pen = pen;
        invalidateCache();

        if ( d_data->style == QwtSymbol::Path )
            d_data->path.graphic.reset();
    }
}

/*!
  \return Pen
  \sa setPen(), brush()
pixhawk's avatar
pixhawk committed
1169
*/
Bryant's avatar
Bryant committed
1170
const QPen& QwtSymbol::pen() const
pixhawk's avatar
pixhawk committed
1171
{
Bryant's avatar
Bryant committed
1172
    return d_data->pen;
pixhawk's avatar
pixhawk committed
1173 1174 1175
}

/*!
Bryant's avatar
Bryant committed
1176 1177 1178 1179 1180 1181 1182 1183
  \brief Set the color of the symbol

  Change the color of the brush for symbol types with a filled area.
  For all other symbol types the color will be assigned to the pen.

  \param color Color

  \sa setBrush(), setPen(), brush(), pen()
pixhawk's avatar
pixhawk committed
1184
*/
Bryant's avatar
Bryant committed
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
void QwtSymbol::setColor( const QColor &color )
{
    switch ( d_data->style )
    {
        case QwtSymbol::Ellipse:
        case QwtSymbol::Rect:
        case QwtSymbol::Diamond:
        case QwtSymbol::Triangle:
        case QwtSymbol::UTriangle:
        case QwtSymbol::DTriangle:
        case QwtSymbol::RTriangle:
        case QwtSymbol::LTriangle:
        case QwtSymbol::Star2:
        case QwtSymbol::Hexagon:
        {
            if ( d_data->brush.color() != color )
            {
                d_data->brush.setColor( color );
                invalidateCache();
            }
            break;
        }
        case QwtSymbol::Cross:
        case QwtSymbol::XCross:
        case QwtSymbol::HLine:
        case QwtSymbol::VLine:
        case QwtSymbol::Star1:
        {
            if ( d_data->pen.color() != color )
            {
                d_data->pen.setColor( color );
                invalidateCache();
            }
            break;
        }
        default:
        {
            if ( d_data->brush.color() != color ||
                d_data->pen.color() != color )
            {
                invalidateCache();
            }

            d_data->brush.setColor( color );
            d_data->pen.setColor( color );
        }
    }
}

/*!
  \brief Set and enable a pin point

  The position of a complex symbol is not always aligned to its center
  ( f.e an arrow, where the peak points to a position ). The pin point
  defines the position inside of a Pixmap, Graphic, SvgDocument 
  or PainterPath symbol where the represented point has to
  be aligned to.
  
  \param pos Position
  \param enable En/Disable the pin point alignment

  \sa pinPoint(), setPinPointEnabled()
 */
void QwtSymbol::setPinPoint( const QPointF &pos, bool enable )
{
    if ( d_data->pinPoint != pos )
    {
        d_data->pinPoint = pos;
        if ( d_data->isPinPointEnabled )
        {
            invalidateCache();
        }
    }

    setPinPointEnabled( enable );
}

/*!
  \return Pin point
  \sa setPinPoint(), setPinPointEnabled()
 */
QPointF QwtSymbol::pinPoint() const
pixhawk's avatar
pixhawk committed
1267
{
Bryant's avatar
Bryant committed
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
    return d_data->pinPoint;
}

/*!
  En/Disable the pin point alignment

  \param on Enabled, when on is true
  \sa setPinPoint(), isPinPointEnabled()
 */
void QwtSymbol::setPinPointEnabled( bool on )
{
    if ( d_data->isPinPointEnabled != on )
    {
        d_data->isPinPointEnabled = on;
        invalidateCache();
    }
pixhawk's avatar
pixhawk committed
1284 1285
}

Bryant's avatar
Bryant committed
1286 1287 1288 1289 1290 1291 1292 1293
/*!
  \return True, when the pin point translation is enabled
  \sa setPinPoint(), setPinPointEnabled()
 */
bool QwtSymbol::isPinPointEnabled() const
{
    return d_data->isPinPointEnabled;
}
pixhawk's avatar
pixhawk committed
1294 1295

/*!
Bryant's avatar
Bryant committed
1296
  Render an array of symbols
pixhawk's avatar
pixhawk committed
1297

Bryant's avatar
Bryant committed
1298 1299 1300
  Painting several symbols is more effective than drawing symbols
  one by one, as a couple of layout calculations and setting of pen/brush
  can be done once for the complete array.
pixhawk's avatar
pixhawk committed
1301 1302

  \param painter Painter
Bryant's avatar
Bryant committed
1303 1304
  \param points Array of points
  \param numPoints Number of points
pixhawk's avatar
pixhawk committed
1305
*/
Bryant's avatar
Bryant committed
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
void QwtSymbol::drawSymbols( QPainter *painter,
    const QPointF *points, int numPoints ) const
{
    if ( numPoints <= 0 )
        return;

    bool useCache = false;

    // Don't use the pixmap, when the paint device
    // could generate scalable vectors

    if ( QwtPainter::roundingAlignment( painter ) &&
        !painter->transform().isScaling() )
    {
        if ( d_data->cache.policy == QwtSymbol::Cache )
        {
            useCache = true;
        }
        else if ( d_data->cache.policy == QwtSymbol::AutoCache )
        {
            if ( painter->paintEngine()->type() == QPaintEngine::Raster )
            {
                useCache = true;
            }
            else
            {
                switch( d_data->style )
                {
                    case QwtSymbol::XCross:
                    case QwtSymbol::HLine:
                    case QwtSymbol::VLine:
                    case QwtSymbol::Cross:
                        break;

                    case QwtSymbol::Pixmap:
                    {
                        if ( !d_data->size.isEmpty() &&
                            d_data->size != d_data->pixmap.pixmap.size() ) 
                        {
                            useCache = true;
                        }
                        break;
                    }                       
                    default:
                        useCache = true;
                }
            }
        }
    }

    if ( useCache )
    {
        const QRect br = boundingRect();

        const QRect rect( 0, 0, br.width(), br.height() );
        
        if ( d_data->cache.pixmap.isNull() )
        {
            d_data->cache.pixmap = QwtPainter::backingStore( NULL, br.size() );
            d_data->cache.pixmap.fill( Qt::transparent );

            QPainter p( &d_data->cache.pixmap );
            p.setRenderHints( painter->renderHints() );
            p.translate( -br.topLeft() );

            const QPointF pos;
            renderSymbols( &p, &pos, 1 );
        }

        const int dx = br.left();
        const int dy = br.top();

        for ( int i = 0; i < numPoints; i++ )
        {
            const int left = qRound( points[i].x() ) + dx;
            const int top = qRound( points[i].y() ) + dy;

            painter->drawPixmap( left, top, d_data->cache.pixmap );
        }
    }
    else
    {
        painter->save();
        renderSymbols( painter, points, numPoints );
        painter->restore();
    }
}

/*!
  \brief Draw the symbol into a rectangle

  The symbol is painted centered and scaled into the target rectangle.
  It is always painted uncached and the pin point is ignored.

  This method is primarily intended for drawing a symbol to
  the legend.
pixhawk's avatar
pixhawk committed
1402 1403

  \param painter Painter
Bryant's avatar
Bryant committed
1404
  \param rect Target rectangle for the symbol 
pixhawk's avatar
pixhawk committed
1405
*/
Bryant's avatar
Bryant committed
1406
void QwtSymbol::drawSymbol( QPainter *painter, const QRectF &rect ) const
pixhawk's avatar
pixhawk committed
1407
{
Bryant's avatar
Bryant committed
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
    if ( d_data->style == QwtSymbol::NoSymbol )
        return;

    if ( d_data->style == QwtSymbol::Graphic )
    {
        d_data->graphic.graphic.render( 
            painter, rect, Qt::KeepAspectRatio );
    }
    else if ( d_data->style == QwtSymbol::Path )
    {
        if ( d_data->path.graphic.isNull() )
        {
            d_data->path.graphic = qwtPathGraphic( 
                d_data->path.path, d_data->pen, d_data->brush );
        }

        d_data->path.graphic.render( 
            painter, rect, Qt::KeepAspectRatio );
        return;
    }
    else if ( d_data->style == QwtSymbol::SvgDocument )
    {
#ifndef QWT_NO_SVG
        if ( d_data->svg.renderer )
        {
            QRectF scaledRect;

            QSizeF sz = d_data->svg.renderer->viewBoxF().size();
            if ( !sz.isEmpty() )
            {
                sz.scale( rect.size(), Qt::KeepAspectRatio );
                scaledRect.setSize( sz );
                scaledRect.moveCenter( rect.center() );
            }
            else
            {
                scaledRect = rect;
            }

            d_data->svg.renderer->render( 
                painter, scaledRect );
        }
#endif
    }
    else
    {
        const QRect br = boundingRect();

        // scale the symbol size to fit into rect.

        const double ratio = qMin( rect.width() / br.width(), 
            rect.height() / br.height() );

        painter->save();

        painter->translate( rect.center() );
        painter->scale( ratio, ratio );
pixhawk's avatar
pixhawk committed
1465

Bryant's avatar
Bryant committed
1466 1467
        const bool isPinPointEnabled = d_data->isPinPointEnabled;
        d_data->isPinPointEnabled = false;
1468

Bryant's avatar
Bryant committed
1469 1470 1471 1472 1473 1474 1475
        const QPointF pos;
        renderSymbols( painter, &pos, 1 );
    
        d_data->isPinPointEnabled = isPinPointEnabled;

        painter->restore();
    }
pixhawk's avatar
pixhawk committed
1476 1477 1478
}

/*!
Bryant's avatar
Bryant committed
1479
  Render the symbol to series of points
pixhawk's avatar
pixhawk committed
1480

Bryant's avatar
Bryant committed
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
  \param painter Qt painter
  \param points Positions of the symbols
  \param numPoints Number of points
 */
void QwtSymbol::renderSymbols( QPainter *painter,
    const QPointF *points, int numPoints ) const
{
    switch ( d_data->style )
    {
        case QwtSymbol::Ellipse:
        {
            qwtDrawEllipseSymbols( painter, points, numPoints, *this );
            break;
        }
        case QwtSymbol::Rect:
        {
            qwtDrawRectSymbols( painter, points, numPoints, *this );
            break;
        }
        case QwtSymbol::Diamond:
        {
            qwtDrawDiamondSymbols( painter, points, numPoints, *this );
            break;
        }
        case QwtSymbol::Cross:
        {
            qwtDrawLineSymbols( painter, Qt::Horizontal | Qt::Vertical,
                points, numPoints, *this );
            break;
        }
        case QwtSymbol::XCross:
        {
            qwtDrawXCrossSymbols( painter, points, numPoints, *this );
            break;
        }
        case QwtSymbol::Triangle:
        case QwtSymbol::UTriangle:
        {
            qwtDrawTriangleSymbols( painter, QwtTriangle::Up,
                points, numPoints, *this );
            break;
        }
        case QwtSymbol::DTriangle:
        {
            qwtDrawTriangleSymbols( painter, QwtTriangle::Down,
                points, numPoints, *this );
            break;
        }
        case QwtSymbol::RTriangle:
        {
            qwtDrawTriangleSymbols( painter, QwtTriangle::Right,
                points, numPoints, *this );
            break;
        }
        case QwtSymbol::LTriangle:
        {
            qwtDrawTriangleSymbols( painter, QwtTriangle::Left,
                points, numPoints, *this );
            break;
        }
        case QwtSymbol::HLine:
        {
            qwtDrawLineSymbols( painter, Qt::Horizontal,
                points, numPoints, *this );
            break;
        }
        case QwtSymbol::VLine:
        {
            qwtDrawLineSymbols( painter, Qt::Vertical,
                points, numPoints, *this );
            break;
        }
        case QwtSymbol::Star1:
        {
            qwtDrawStar1Symbols( painter, points, numPoints, *this );
            break;
        }
        case QwtSymbol::Star2:
        {
            qwtDrawStar2Symbols( painter, points, numPoints, *this );
            break;
        }
        case QwtSymbol::Hexagon:
        {
            qwtDrawHexagonSymbols( painter, points, numPoints, *this );
            break;
        }
        case QwtSymbol::Path:
        {
            if ( d_data->path.graphic.isNull() )
            {
                d_data->path.graphic = qwtPathGraphic( d_data->path.path, 
                    d_data->pen, d_data->brush );
            }
pixhawk's avatar
pixhawk committed
1575

Bryant's avatar
Bryant committed
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739
            qwtDrawGraphicSymbols( painter, points, numPoints, 
                d_data->path.graphic, *this );
            break;
        }
        case QwtSymbol::Pixmap:
        {
            qwtDrawPixmapSymbols( painter, points, numPoints, *this );
            break;
        }
        case QwtSymbol::Graphic:
        {
            qwtDrawGraphicSymbols( painter, points, numPoints, 
                d_data->graphic.graphic, *this );
            break;
        }
        case QwtSymbol::SvgDocument:
        {
#ifndef QWT_NO_SVG
            qwtDrawSvgSymbols( painter, points, numPoints, 
                d_data->svg.renderer, *this );
#endif
            break;
        }
        default:;
    }
}

/*!
  Calculate the bounding rectangle for a symbol
  at position (0,0).

  \return Bounding rectangle
 */
QRect QwtSymbol::boundingRect() const
{
    QRectF rect;

    switch ( d_data->style )
    {
        case QwtSymbol::Ellipse:
        case QwtSymbol::Rect:
        case QwtSymbol::Hexagon:
        {
            qreal pw = 0.0;
            if ( d_data->pen.style() != Qt::NoPen )
                pw = qMax( d_data->pen.widthF(), qreal( 1.0 ) );

            rect.setSize( d_data->size + QSizeF( pw, pw ) );
            rect.moveCenter( QPointF( 0.0, 0.0 ) );

            break;
        }
        case QwtSymbol::XCross:
        case QwtSymbol::Diamond:
        case QwtSymbol::Triangle:
        case QwtSymbol::UTriangle:
        case QwtSymbol::DTriangle:
        case QwtSymbol::RTriangle:
        case QwtSymbol::LTriangle:
        case QwtSymbol::Star1:
        case QwtSymbol::Star2:
        {
            qreal pw = 0.0;
            if ( d_data->pen.style() != Qt::NoPen )
                pw = qMax( d_data->pen.widthF(), qreal( 1.0 ) );

            rect.setSize( d_data->size + QSizeF( 2 * pw, 2 * pw ) );
            rect.moveCenter( QPointF( 0.0, 0.0 ) );
            break;
        }
        case QwtSymbol::Path:
        {
            if ( d_data->path.graphic.isNull() )
            {
                d_data->path.graphic = qwtPathGraphic(
                    d_data->path.path, d_data->pen, d_data->brush );
            }

            rect = qwtScaledBoundingRect( 
                d_data->path.graphic, d_data->size );

            break;
        }
        case QwtSymbol::Pixmap:
        {
            if ( d_data->size.isEmpty() )
                rect.setSize( d_data->pixmap.pixmap.size() );
            else
                rect.setSize( d_data->size );
            
            rect.moveCenter( QPointF( 0.0, 0.0 ) );

            // pinpoint ???
            break;
        }
        case QwtSymbol::Graphic:
        {
            rect = qwtScaledBoundingRect( 
                d_data->graphic.graphic, d_data->size );

            break;
        }
#ifndef QWT_NO_SVG
        case QwtSymbol::SvgDocument:
        {
            if ( d_data->svg.renderer )
                rect = d_data->svg.renderer->viewBoxF();

            if ( d_data->size.isValid() && !rect.isEmpty() )
            {
                QSizeF sz = rect.size();

                const double sx = d_data->size.width() / sz.width();
                const double sy = d_data->size.height() / sz.height();

                QTransform transform;
                transform.scale( sx, sy );

                rect = transform.mapRect( rect );
            }
            break;
        }
#endif
        default:
        {
            rect.setSize( d_data->size );
            rect.moveCenter( QPointF( 0.0, 0.0 ) );
        }
    }

    if ( d_data->style == QwtSymbol::Graphic || 
        d_data->style == QwtSymbol::SvgDocument || d_data->style == QwtSymbol::Path )
    {
        QPointF pinPoint( 0.0, 0.0 );
        if ( d_data->isPinPointEnabled )
            pinPoint = rect.center() - d_data->pinPoint;

        rect.moveCenter( pinPoint );
    }

    QRect r;
    r.setLeft( qFloor( rect.left() ) );
    r.setTop( qFloor( rect.top() ) );
    r.setRight( qCeil( rect.right() ) );
    r.setBottom( qCeil( rect.bottom() ) );

    if ( d_data->style != QwtSymbol::Pixmap )
        r.adjust( -1, -1, 1, 1 ); // for antialiasing

    return r;
}

/*!
  Invalidate the cached symbol pixmap

  The symbol invalidates its cache, whenever an attribute is changed
  that has an effect ob how to display a symbol. In case of derived
  classes with individual styles ( >= QwtSymbol::UserStyle ) it
  might be necessary to call invalidateCache() for attributes
  that are relevant for this style.

  \sa CachePolicy, setCachePolicy(), drawSymbols()
 */
void QwtSymbol::invalidateCache()
pixhawk's avatar
pixhawk committed
1740
{
Bryant's avatar
Bryant committed
1741 1742
    if ( !d_data->cache.pixmap.isNull() )
        d_data->cache.pixmap = QPixmap();
pixhawk's avatar
pixhawk committed
1743 1744
}

Bryant's avatar
Bryant committed
1745 1746 1747 1748 1749 1750 1751
/*!
  Specify the symbol style

  \param style Style
  \sa style()
*/
void QwtSymbol::setStyle( QwtSymbol::Style style )
pixhawk's avatar
pixhawk committed
1752
{
Bryant's avatar
Bryant committed
1753 1754 1755 1756 1757
    if ( d_data->style != style )
    {
        d_data->style = style;
        invalidateCache();
    }
pixhawk's avatar
pixhawk committed
1758 1759
}

Bryant's avatar
Bryant committed
1760 1761 1762 1763 1764
/*!
  \return Current symbol style
  \sa setStyle()
*/
QwtSymbol::Style QwtSymbol::style() const
pixhawk's avatar
pixhawk committed
1765
{
Bryant's avatar
Bryant committed
1766
    return d_data->style;
pixhawk's avatar
pixhawk committed
1767
}