qwt_graphic.cpp 24.8 KB
Newer Older
Bryant's avatar
Bryant committed
1 2 3 4 5 6 7 8 9 10 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 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 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
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#include "qwt_graphic.h"
#include "qwt_painter_command.h"
#include <qvector.h>
#include <qpainter.h>
#include <qpaintengine.h>
#include <qimage.h>
#include <qpixmap.h>
#include <qpainterpath.h>
#include <qmath.h>

static bool qwtHasScalablePen( const QPainter *painter )
{
    const QPen pen = painter->pen();

    bool scalablePen = false;

    if ( pen.style() != Qt::NoPen && pen.brush().style() != Qt::NoBrush )
    {
        scalablePen = !pen.isCosmetic();
        if ( !scalablePen && pen.widthF() == 0.0 )
        {
            const QPainter::RenderHints hints = painter->renderHints();
            if ( hints.testFlag( QPainter::NonCosmeticDefaultPen ) )
                scalablePen = true;
        }
    }

    return scalablePen;
}

static QRectF qwtStrokedPathRect( 
    const QPainter *painter, const QPainterPath &path )
{
    QPainterPathStroker stroker;
    stroker.setWidth( painter->pen().widthF() );
    stroker.setCapStyle( painter->pen().capStyle() );
    stroker.setJoinStyle( painter->pen().joinStyle() );
    stroker.setMiterLimit( painter->pen().miterLimit() );

    QRectF rect;
    if ( qwtHasScalablePen( painter ) )
    {
        QPainterPath stroke = stroker.createStroke(path);
        rect = painter->transform().map(stroke).boundingRect();
    }
    else
    {
        QPainterPath mappedPath = painter->transform().map(path);
        mappedPath = stroker.createStroke( mappedPath );

        rect = mappedPath.boundingRect();
    }

    return rect;
}

static inline void qwtExecCommand( 
    QPainter *painter, const QwtPainterCommand &cmd, 
    QwtGraphic::RenderHints renderHints,
    const QTransform &transform )
{
    switch( cmd.type() )
    {
        case QwtPainterCommand::Path:
        {
            bool doMap = false;

            if ( renderHints.testFlag( QwtGraphic::RenderPensUnscaled )
                && painter->transform().isScaling() )
            {
                bool isCosmetic = painter->pen().isCosmetic();
                if ( isCosmetic && painter->pen().widthF() == 0.0 )
                {
                    QPainter::RenderHints hints = painter->renderHints();
                    if ( hints.testFlag( QPainter::NonCosmeticDefaultPen ) )
                        isCosmetic = false;
                }

                doMap = !isCosmetic;
            }

            if ( doMap )
            {
                const QTransform transform = painter->transform();

                painter->resetTransform();
                painter->drawPath( transform.map( *cmd.path() ) );

                painter->setTransform( transform );
            }
            else
            {
                painter->drawPath( *cmd.path() );
            }
            break;
        }
        case QwtPainterCommand::Pixmap:
        {
            const QwtPainterCommand::PixmapData *data = cmd.pixmapData();
            painter->drawPixmap( data->rect, data->pixmap, data->subRect );
            break;
        }
        case QwtPainterCommand::Image:
        {
            const QwtPainterCommand::ImageData *data = cmd.imageData();
            painter->drawImage( data->rect, data->image, 
                data->subRect, data->flags );
            break;
        }
        case QwtPainterCommand::State:
        {
            const QwtPainterCommand::StateData *data = cmd.stateData();

            if ( data->flags & QPaintEngine::DirtyPen ) 
                painter->setPen( data->pen );

            if ( data->flags & QPaintEngine::DirtyBrush ) 
                painter->setBrush( data->brush );

            if ( data->flags & QPaintEngine::DirtyBrushOrigin ) 
                painter->setBrushOrigin( data->brushOrigin );

            if ( data->flags & QPaintEngine::DirtyFont ) 
                painter->setFont( data->font );

            if ( data->flags & QPaintEngine::DirtyBackground ) 
            {
                painter->setBackgroundMode( data->backgroundMode );
                painter->setBackground( data->backgroundBrush );
            }

            if ( data->flags & QPaintEngine::DirtyTransform ) 
            {
                painter->setTransform( data->transform * transform );
            }

            if ( data->flags & QPaintEngine::DirtyClipEnabled ) 
                painter->setClipping( data->isClipEnabled );

            if ( data->flags & QPaintEngine::DirtyClipRegion) 
            {
                painter->setClipRegion( data->clipRegion, 
                    data->clipOperation );
            }

            if ( data->flags & QPaintEngine::DirtyClipPath ) 
            {
                painter->setClipPath( data->clipPath, data->clipOperation );
            }

            if ( data->flags & QPaintEngine::DirtyHints) 
            {
                const QPainter::RenderHints hints = data->renderHints;

                painter->setRenderHint( QPainter::Antialiasing,
                    hints.testFlag( QPainter::Antialiasing ) );

                painter->setRenderHint( QPainter::TextAntialiasing,
                    hints.testFlag( QPainter::TextAntialiasing ) );

                painter->setRenderHint( QPainter::SmoothPixmapTransform,
                    hints.testFlag( QPainter::SmoothPixmapTransform ) );

                painter->setRenderHint( QPainter::HighQualityAntialiasing,
                    hints.testFlag( QPainter::HighQualityAntialiasing ) );

                painter->setRenderHint( QPainter::NonCosmeticDefaultPen,
                    hints.testFlag( QPainter::NonCosmeticDefaultPen ) );
            }

            if ( data->flags & QPaintEngine::DirtyCompositionMode) 
                painter->setCompositionMode( data->compositionMode );

            if ( data->flags & QPaintEngine::DirtyOpacity) 
                painter->setOpacity( data->opacity );

            break;
        }
        default:
            break;
    }

}

class QwtGraphic::PathInfo
{
public:
    PathInfo():
        d_scalablePen( false )
    {
        // QVector needs a default constructor
    }

    PathInfo( const QRectF &pointRect, 
            const QRectF &boundingRect, bool scalablePen ):
        d_pointRect( pointRect ),
        d_boundingRect( boundingRect ),
        d_scalablePen( scalablePen )
    {
    }

    inline QRectF scaledBoundingRect( double sx, double sy,
        bool scalePens ) const
    {
        if ( sx == 1.0 && sy == 1.0 )
            return d_boundingRect;

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

        QRectF rect;
        if ( scalePens && d_scalablePen )
        {
            rect = transform.mapRect( d_boundingRect );
        }
        else
        {
            rect = transform.mapRect( d_pointRect );

            const double l = qAbs( d_pointRect.left() - d_boundingRect.left() );
            const double r = qAbs( d_pointRect.right() - d_boundingRect.right() );
            const double t = qAbs( d_pointRect.top() - d_boundingRect.top() );
            const double b = qAbs( d_pointRect.bottom() - d_boundingRect.bottom() );

            rect.adjust( -l, -t, r, b );
        }

        return rect;
    }

    inline double scaleFactorX( const QRectF& pathRect, 
        const QRectF &targetRect, bool scalePens ) const
    {
        if ( pathRect.width() <= 0.0 )
            return 0.0;

        const QPointF p0 = d_pointRect.center();

        const double l = qAbs( pathRect.left() - p0.x() );
        const double r = qAbs( pathRect.right() - p0.x() );

        const double w = 2.0 * qMin( l, r ) 
            * targetRect.width() / pathRect.width();

        double sx;
        if ( scalePens && d_scalablePen )
        {
            sx = w / d_boundingRect.width();
        }
        else
        {
            const double pw = qMax( 
                qAbs( d_boundingRect.left() - d_pointRect.left() ),
                qAbs( d_boundingRect.right() - d_pointRect.right() ) );

            sx = ( w - 2 * pw ) / d_pointRect.width();
        }

        return sx;
    }

    inline double scaleFactorY( const QRectF& pathRect, 
        const QRectF &targetRect, bool scalePens ) const
    {
        if ( pathRect.height() <= 0.0 )
            return 0.0;

        const QPointF p0 = d_pointRect.center();

        const double t = qAbs( pathRect.top() - p0.y() );
        const double b = qAbs( pathRect.bottom() - p0.y() );

        const double h = 2.0 * qMin( t, b ) 
            * targetRect.height() / pathRect.height();

        double sy;
        if ( scalePens && d_scalablePen )
        {
            sy = h / d_boundingRect.height();
        }
        else
        {
            const double pw = 
                qMax( qAbs( d_boundingRect.top() - d_pointRect.top() ),
                qAbs( d_boundingRect.bottom() - d_pointRect.bottom() ) );

            sy = ( h - 2 * pw ) / d_pointRect.height();
        }

        return sy;
    }

private:
    QRectF d_pointRect;
    QRectF d_boundingRect;
    bool d_scalablePen;
};

class QwtGraphic::PrivateData
{
public:
    PrivateData():
        boundingRect( 0.0, 0.0, -1.0, -1.0 ),
        pointRect( 0.0, 0.0, -1.0, -1.0 )
    {
    }

    QSizeF defaultSize;
    QVector<QwtPainterCommand> commands;
    QVector<QwtGraphic::PathInfo> pathInfos;

    QRectF boundingRect;
    QRectF pointRect;

    QwtGraphic::RenderHints renderHints;
};

/*!
  \brief Constructor

  Initializes a null graphic
  \sa isNull()
 */
QwtGraphic::QwtGraphic():
    QwtNullPaintDevice()
{
    setMode( QwtNullPaintDevice::PathMode );
    d_data = new PrivateData;
}

/*!
  \brief Copy constructor

  \param other Source 
  \sa operator=()
 */
QwtGraphic::QwtGraphic( const QwtGraphic &other ):
    QwtNullPaintDevice()
{
    setMode( other.mode() );
    d_data = new PrivateData( *other.d_data );
}

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

/*!
  \brief Assignment operator

  \param other Source 
  \return A reference of this object
 */
QwtGraphic& QwtGraphic::operator=(const QwtGraphic &other)
{
    setMode( other.mode() );
    *d_data = *other.d_data;

    return *this;
}

/*!
  \brief Clear all stored commands 
  \sa isNull()
 */
void QwtGraphic::reset() 
{
    d_data->commands.clear();
    d_data->pathInfos.clear();

    d_data->boundingRect = QRectF( 0.0, 0.0, -1.0, -1.0 );
    d_data->pointRect = QRectF( 0.0, 0.0, -1.0, -1.0 );
    d_data->defaultSize = QSizeF();

}

/*!
  \return True, when no painter commands have been stored
  \sa isEmpty(), commands()
*/
bool QwtGraphic::isNull() const
{
    return d_data->commands.isEmpty();
}

/*!
  \return True, when the bounding rectangle is empty
  \sa boundingRect(), isNull()
*/
bool QwtGraphic::isEmpty() const
{
    return d_data->boundingRect.isEmpty();
}

/*!
  Toggle an render hint

  \param hint Render hint
  \param on true/false

  \sa testRenderHint(), RenderHint
*/
void QwtGraphic::setRenderHint( RenderHint hint, bool on )
{
    if ( on )
        d_data->renderHints |= hint;
    else
        d_data->renderHints &= ~hint;
}

/*!
  Test a render hint

  \param hint Render hint
  \return true/false
  \sa setRenderHint(), RenderHint
*/
bool QwtGraphic::testRenderHint( RenderHint hint ) const
{
    return d_data->renderHints.testFlag( hint );
}

/*!
  The bounding rectangle is the controlPointRect()
  extended by the areas needed for rendering the outlines
  with unscaled pens.

  \return Bounding rectangle of the graphic
  \sa controlPointRect(), scaledBoundingRect()
 */
QRectF QwtGraphic::boundingRect() const
{
    if ( d_data->boundingRect.width() < 0 )
        return QRectF();

    return d_data->boundingRect;
}

/*!
  The control point rectangle is the bounding rectangle 
  of all control points of the paths and the target
  rectangles of the images/pixmaps.

  \return Control point rectangle
  \sa boundingRect(), scaledBoundingRect()
 */
QRectF QwtGraphic::controlPointRect() const
{
    if ( d_data->pointRect.width() < 0 )
        return QRectF();

    return d_data->pointRect;
}

/*!
  \brief Calculate the target rectangle for scaling the graphic

  \param sx Horizontal scaling factor 
  \param sy Vertical scaling factor 

  \note In case of paths that are painted with a cosmetic pen 
        ( see QPen::isCosmetic() ) the target rectangle is different to
        multiplying the bounding rectangle.

  \return Scaled bounding rectangle
  \sa boundingRect(), controlPointRect()
 */
QRectF QwtGraphic::scaledBoundingRect( double sx, double sy ) const
{
    if ( sx == 1.0 && sy == 1.0 )
        return d_data->boundingRect;

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

    QRectF rect = transform.mapRect( d_data->pointRect );

    for ( int i = 0; i < d_data->pathInfos.size(); i++ )
    {
        rect |= d_data->pathInfos[i].scaledBoundingRect( sx, sy, 
            !d_data->renderHints.testFlag( RenderPensUnscaled ) );
    }

    return rect;
}

//! \return Ceiled defaultSize()
QSize QwtGraphic::sizeMetrics() const
{
    const QSizeF sz = defaultSize();
    return QSize( qCeil( sz.width() ), qCeil( sz.height() ) );
}

/*!
  \brief Set a default size

  The default size is used in all methods rendering the graphic,
  where no size is explicitly specified. Assigning an empty size
  means, that the default size will be calculated from the bounding 
  rectangle.

  The default setting is an empty size.
    
  \param size Default size

  \sa defaultSize(), boundingRect()
 */
void QwtGraphic::setDefaultSize( const QSizeF &size )
{
    const double w = qMax( qreal( 0.0 ), size.width() );
    const double h = qMax( qreal( 0.0 ), size.height() );

    d_data->defaultSize = QSizeF( w, h );
}

/*!
  \brief Default size

  When a non empty size has been assigned by setDefaultSize() this
  size will be returned. Otherwise the default size is the size
  of the bounding rectangle.

  The default size is used in all methods rendering the graphic,
  where no size is explicitly specified. 

  \return Default size
  \sa setDefaultSize(), boundingRect()
 */
QSizeF QwtGraphic::defaultSize() const
{
    if ( !d_data->defaultSize.isEmpty() )
        return d_data->defaultSize;

    return boundingRect().size();
}

/*!
  \brief Replay all recorded painter commands
  \param painter Qt painter
 */
void QwtGraphic::render( QPainter *painter ) const
{
    if ( isNull() )
        return;

    const int numCommands = d_data->commands.size();
    const QwtPainterCommand *commands = d_data->commands.constData();

    const QTransform transform = painter->transform();

    painter->save();

    for ( int i = 0; i < numCommands; i++ )
    {
        qwtExecCommand( painter, commands[i], 
            d_data->renderHints, transform );
    }

    painter->restore();
}

/*!
  \brief Replay all recorded painter commands

  The graphic is scaled to fit into the rectangle
  of the given size starting at ( 0, 0 ).

  \param painter Qt painter
  \param size Size for the scaled graphic
  \param aspectRatioMode Mode how to scale - See Qt::AspectRatioMode
 */
void QwtGraphic::render( QPainter *painter, const QSizeF &size, 
    Qt::AspectRatioMode aspectRatioMode ) const
{
    const QRectF r( 0.0, 0.0, size.width(), size.height() );
    render( painter, r, aspectRatioMode );
}

/*!
  \brief Replay all recorded painter commands

  The graphic is scaled to fit into the given rectangle

  \param painter Qt painter
  \param rect Rectangle for the scaled graphic
  \param aspectRatioMode Mode how to scale - See Qt::AspectRatioMode
 */
void QwtGraphic::render( QPainter *painter, const QRectF &rect, 
    Qt::AspectRatioMode aspectRatioMode ) const
{
    if ( isEmpty() || rect.isEmpty() )
        return;

    double sx = 1.0; 
    double sy = 1.0;

    if ( d_data->pointRect.width() > 0.0 )
        sx = rect.width() / d_data->pointRect.width();

    if ( d_data->pointRect.height() > 0.0 )
        sy = rect.height() / d_data->pointRect.height();

    const bool scalePens = 
        !d_data->renderHints.testFlag( RenderPensUnscaled );

    for ( int i = 0; i < d_data->pathInfos.size(); i++ )
    {
        const PathInfo info = d_data->pathInfos[i];

        const double ssx = info.scaleFactorX( 
            d_data->pointRect, rect, scalePens );

        if ( ssx > 0.0 )
            sx = qMin( sx, ssx );

        const double ssy = info.scaleFactorY( 
            d_data->pointRect, rect, scalePens );

        if ( ssy > 0.0 )
            sy = qMin( sy, ssy );
    }

    if ( aspectRatioMode == Qt::KeepAspectRatio )
    {
        const double s = qMin( sx, sy );
        sx = s;
        sy = s;
    }
    else if ( aspectRatioMode == Qt::KeepAspectRatioByExpanding )
    {
        const double s = qMax( sx, sy );
        sx = s;
        sy = s;
    }

    QTransform tr;
    tr.translate( rect.center().x() - 0.5 * sx * d_data->pointRect.width(),
        rect.center().y() - 0.5 * sy * d_data->pointRect.height() );
    tr.scale( sx, sy );
    tr.translate( -d_data->pointRect.x(), -d_data->pointRect.y() );

    const QTransform transform = painter->transform();

    painter->setTransform( tr, true );
    render( painter );

    painter->setTransform( transform );
}

/*!
  \brief Replay all recorded painter commands

  The graphic is scaled to the defaultSize() and aligned
  to a position.

  \param painter Qt painter
  \param pos Reference point, where to render
  \param alignment Flags how to align the target rectangle 
                   to pos.
 */
void QwtGraphic::render( QPainter *painter, 
    const QPointF &pos, Qt::Alignment alignment ) const
{
    QRectF r( pos, defaultSize() );

    if ( alignment & Qt::AlignLeft )
    {
        r.moveLeft( pos.x() );
    }
    else if ( alignment & Qt::AlignHCenter )
    {
        r.moveCenter( QPointF( pos.x(), r.center().y() ) );
    }
    else if ( alignment & Qt::AlignRight )
    {
        r.moveRight( pos.x() );
    }

    if ( alignment & Qt::AlignTop )
    {
        r.moveTop( pos.y() );
    }
    else if ( alignment & Qt::AlignVCenter )
    {
        r.moveCenter( QPointF( r.center().x(), pos.y() ) );
    }
    else if ( alignment & Qt::AlignBottom )
    {
        r.moveBottom( pos.y() );
    }

    render( painter, r );
}

/*!
  \brief Convert the graphic to a QPixmap
    
  All pixels of the pixmap get initialized by Qt::transparent
  before the graphic is scaled and rendered on it.
    
  The size of the pixmap is the default size ( ceiled to integers )
  of the graphic.

  \return The graphic as pixmap in default size
  \sa defaultSize(), toImage(), render()
 */ 
QPixmap QwtGraphic::toPixmap() const
{
    if ( isNull() )
        return QPixmap();

    const QSizeF sz = defaultSize();

    const int w = qCeil( sz.width() );
    const int h = qCeil( sz.height() );

    QPixmap pixmap( w, h );
    pixmap.fill( Qt::transparent );

    const QRectF r( 0.0, 0.0, sz.width(), sz.height() );

    QPainter painter( &pixmap );
    render( &painter, r, Qt::KeepAspectRatio );
    painter.end();

    return pixmap;
}

/*!
  \brief Convert the graphic to a QPixmap

  All pixels of the pixmap get initialized by Qt::transparent
  before the graphic is scaled and rendered on it.

  \param size Size of the image
  \param aspectRatioMode Aspect ratio how to scale the graphic

  \return The graphic as pixmap
  \sa toImage(), render()
 */
QPixmap QwtGraphic::toPixmap( const QSize &size,
    Qt::AspectRatioMode aspectRatioMode ) const
{
    QPixmap pixmap( size );
    pixmap.fill( Qt::transparent );

    const QRect r( 0, 0, size.width(), size.height() );

    QPainter painter( &pixmap );
    render( &painter, r, aspectRatioMode );
    painter.end();

    return pixmap;
}

/*!
  \brief Convert the graphic to a QImage

  All pixels of the image get initialized by 0 ( transparent )
  before the graphic is scaled and rendered on it.

  The format of the image is QImage::Format_ARGB32_Premultiplied.

  \param size Size of the image
  \param aspectRatioMode Aspect ratio how to scale the graphic

  \return The graphic as image
  \sa toPixmap(), render()
 */
QImage QwtGraphic::toImage( const QSize &size,
    Qt::AspectRatioMode aspectRatioMode  ) const
{
    QImage image( size, QImage::Format_ARGB32_Premultiplied );
    image.fill( 0 );

    const QRect r( 0, 0, size.width(), size.height() );

    QPainter painter( &image );
    render( &painter, r, aspectRatioMode );
    painter.end();

    return image;
}

/*!
  \brief Convert the graphic to a QImage
    
  All pixels of the image get initialized by 0 ( transparent )
  before the graphic is scaled and rendered on it.

  The format of the image is QImage::Format_ARGB32_Premultiplied.

  The size of the image is the default size ( ceiled to integers )
  of the graphic.
    
  \return The graphic as image in default size
  \sa defaultSize(), toPixmap(), render()
 */
QImage QwtGraphic::toImage() const
{
    if ( isNull() )
        return QImage();

    const QSizeF sz = defaultSize();

    const int w = qCeil( sz.width() );
    const int h = qCeil( sz.height() );

    QImage image( w, h, QImage::Format_ARGB32 );
    image.fill( 0 );

    const QRect r( 0, 0, sz.width(), sz.height() );

    QPainter painter( &image );
    render( &painter, r, Qt::KeepAspectRatio );
    painter.end();

    return image;
}

/*!
  Store a path command in the command list

  \param path Painter path
  \sa QPaintEngine::drawPath()
*/
void QwtGraphic::drawPath( const QPainterPath &path )
{
    const QPainter *painter = paintEngine()->painter();
    if ( painter == NULL )
        return;

    d_data->commands += QwtPainterCommand( path );

    if ( !path.isEmpty() )
    {
        const QPainterPath scaledPath = painter->transform().map( path );

        QRectF pointRect = scaledPath.boundingRect();
        QRectF boundingRect = pointRect;

        if ( painter->pen().style() != Qt::NoPen 
            && painter->pen().brush().style() != Qt::NoBrush )
        {
            boundingRect = qwtStrokedPathRect( painter, path );
        }

        updateControlPointRect( pointRect );
        updateBoundingRect( boundingRect );

        d_data->pathInfos += PathInfo( pointRect, 
            boundingRect, qwtHasScalablePen( painter ) );
    }
}

/*!
  \brief Store a pixmap command in the command list

  \param rect target rectangle
  \param pixmap Pixmap to be painted
  \param subRect Reactangle of the pixmap to be painted

  \sa QPaintEngine::drawPixmap()
*/
void QwtGraphic::drawPixmap( const QRectF &rect, 
    const QPixmap &pixmap, const QRectF &subRect )
{
    const QPainter *painter = paintEngine()->painter();
    if ( painter == NULL )
        return;

    d_data->commands += QwtPainterCommand( rect, pixmap, subRect );

    const QRectF r = painter->transform().mapRect( rect );
    updateControlPointRect( r );
    updateBoundingRect( r );
}

/*!
  \brief Store a image command in the command list

  \param rect traget rectangle
  \param image Image to be painted
  \param subRect Reactangle of the pixmap to be painted
  \param flags Image conversion flags

  \sa QPaintEngine::drawImage()
 */
void QwtGraphic::drawImage( const QRectF &rect, const QImage &image,
    const QRectF &subRect, Qt::ImageConversionFlags flags)
{
    const QPainter *painter = paintEngine()->painter();
    if ( painter == NULL )
        return;

    d_data->commands += QwtPainterCommand( rect, image, subRect, flags );

    const QRectF r = painter->transform().mapRect( rect );

    updateControlPointRect( r );
    updateBoundingRect( r );
}

/*!
  \brief Store a state command in the command list

  \param state State to be stored
  \sa QPaintEngine::updateState()
 */
void QwtGraphic::updateState( const QPaintEngineState &state)
{
    d_data->commands += QwtPainterCommand( state );
}

void QwtGraphic::updateBoundingRect( const QRectF &rect )
{
    QRectF br = rect;

    const QPainter *painter = paintEngine()->painter();
    if ( painter && painter->hasClipping() )
    {
        QRectF cr = painter->clipRegion().boundingRect();
        cr = painter->transform().mapRect( br );

        br &= cr;
    }

    if ( d_data->boundingRect.width() < 0 )
        d_data->boundingRect = br;
    else
        d_data->boundingRect |= br;
}

void QwtGraphic::updateControlPointRect( const QRectF &rect )
{
    if ( d_data->pointRect.width() < 0.0 )
        d_data->pointRect = rect;
    else
        d_data->pointRect |= rect;
}

/*!
  \return List of recorded paint commands
  \sa setCommands()
 */
const QVector< QwtPainterCommand > &QwtGraphic::commands() const
{
    return d_data->commands;
}

/*!
  \brief Append paint commands

  \param commands Paint commands
  \sa commands()
 */
void QwtGraphic::setCommands( QVector< QwtPainterCommand > &commands )
{
    reset();

    const int numCommands = commands.size();
    if ( numCommands <= 0 )
        return;

    // to calculate a proper bounding rectangle we don't simply copy 
    // the commands. 

    const QwtPainterCommand *cmds = commands.constData();

    QPainter painter( this );
    for ( int i = 0; i < numCommands; i++ )
        qwtExecCommand( &painter, cmds[i], RenderHints(), QTransform() );

    painter.end();
}