qwt_plot_canvas.cpp 28 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9
/* -*- 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
 *****************************************************************************/

Bryant's avatar
Bryant committed
10 11 12 13 14
#include "qwt_plot_canvas.h"
#include "qwt_painter.h"
#include "qwt_null_paintdevice.h"
#include "qwt_math.h"
#include "qwt_plot.h"
pixhawk's avatar
pixhawk committed
15 16 17 18
#include <qpainter.h>
#include <qstyle.h>
#include <qstyleoption.h>
#include <qpaintengine.h>
Bryant's avatar
Bryant committed
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
#include <qevent.h>

class QwtStyleSheetRecorder: public QwtNullPaintDevice
{
public:
    QwtStyleSheetRecorder( const QSize &size ):
        d_size( size )
    {
    }

    virtual void updateState( const QPaintEngineState &state )
    {
        if ( state.state() & QPaintEngine::DirtyPen )
        {
            d_pen = state.pen();
        }
        if ( state.state() & QPaintEngine::DirtyBrush )
        {
            d_brush = state.brush();
        }
        if ( state.state() & QPaintEngine::DirtyBrushOrigin )
        {
            d_origin = state.brushOrigin();
        }
    }

    virtual void drawRects(const QRectF *rects, int count )
    {
        for ( int i = 0; i < count; i++ )
            border.rectList += rects[i];
    }

    virtual void drawPath( const QPainterPath &path )
    {
        const QRectF rect( QPointF( 0.0, 0.0 ), d_size );
        if ( path.controlPointRect().contains( rect.center() ) )
        {
            setCornerRects( path );
            alignCornerRects( rect );

            background.path = path;
            background.brush = d_brush;
            background.origin = d_origin;
        }
        else
        {
            border.pathList += path;
        }
    }

    void setCornerRects( const QPainterPath &path )
    {
        QPointF pos( 0.0, 0.0 );

        for ( int i = 0; i < path.elementCount(); i++ )
        {
            QPainterPath::Element el = path.elementAt(i); 
            switch( el.type )
            {
                case QPainterPath::MoveToElement:
                case QPainterPath::LineToElement:
                {
                    pos.setX( el.x );
                    pos.setY( el.y );
                    break;
                }
                case QPainterPath::CurveToElement:
                {
                    QRectF r( pos, QPointF( el.x, el.y ) );
                    clipRects += r.normalized();

                    pos.setX( el.x );
                    pos.setY( el.y );

                    break;
                }
                case QPainterPath::CurveToDataElement:
                {
                    if ( clipRects.size() > 0 )
                    {
                        QRectF r = clipRects.last();
                        r.setCoords( 
                            qMin( r.left(), el.x ),
                            qMin( r.top(), el.y ),
                            qMax( r.right(), el.x ),
                            qMax( r.bottom(), el.y )
                        );
                        clipRects.last() = r.normalized();
                    }
                    break;
                }
            }
        }
    }

protected:
    virtual QSize sizeMetrics() const
    {
        return d_size;
    }

private:
    void alignCornerRects( const QRectF &rect )
    {
        for ( int i = 0; i < clipRects.size(); i++ )
        {
            QRectF &r = clipRects[i];
            if ( r.center().x() < rect.center().x() )
                r.setLeft( rect.left() );
            else
                r.setRight( rect.right() );

            if ( r.center().y() < rect.center().y() )
                r.setTop( rect.top() );
            else
                r.setBottom( rect.bottom() );
        }
    }


public:
    QVector<QRectF> clipRects;

    struct Border
    {
        QList<QPainterPath> pathList;
        QList<QRectF> rectList;
        QRegion clipRegion;
    } border;

    struct Background
    {
        QPainterPath path;
        QBrush brush;
        QPointF origin;
    } background;

private:
    const QSize d_size;

    QPen d_pen;
    QBrush d_brush;
    QPointF d_origin;
};

static void qwtDrawBackground( QPainter *painter, QwtPlotCanvas *canvas )
{
    painter->save();

    const QPainterPath borderClip = canvas->borderPath( canvas->rect() );
    if ( !borderClip.isEmpty() )
        painter->setClipPath( borderClip, Qt::IntersectClip );

    const QBrush &brush = 
        canvas->palette().brush( canvas->backgroundRole() );

    if ( brush.style() == Qt::TexturePattern )
    {
        QPixmap pm( canvas->size() );
        QwtPainter::fillPixmap( canvas, pm );
        painter->drawPixmap( 0, 0, pm );
    }
    else if ( brush.gradient() )
    {
        QVector<QRect> rects;

        if ( brush.gradient()->coordinateMode() == QGradient::ObjectBoundingMode )
        {
            rects += canvas->rect();
        } 
        else 
        {
            rects = painter->clipRegion().rects();
        }

#if 1
        bool useRaster = false;

        if ( painter->paintEngine()->type() == QPaintEngine::X11 )
        {
            // Qt 4.7.1: gradients on X11 are broken ( subrects + 
            // QGradient::StretchToDeviceMode ) and horrible slow.
            // As workaround we have to use the raster paintengine.
            // Even if the QImage -> QPixmap translation is slow
            // it is three times faster, than using X11 directly

            useRaster = true;
        }
pixhawk's avatar
pixhawk committed
207
#endif
Bryant's avatar
Bryant committed
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
        if ( useRaster )
        {
            QImage::Format format = QImage::Format_RGB32;

            const QGradientStops stops = brush.gradient()->stops();
            for ( int i = 0; i < stops.size(); i++ )
            {
                if ( stops[i].second.alpha() != 255 )
                {
                    // don't use Format_ARGB32_Premultiplied. It's
                    // recommended by the Qt docs, but QPainter::drawImage()
                    // is horrible slow on X11.

                    format = QImage::Format_ARGB32;
                    break;
                }
            }
            
            QImage image( canvas->size(), format );

            QPainter p( &image );
            p.setPen( Qt::NoPen );
            p.setBrush( brush );

            p.drawRects( rects );

            p.end();

            painter->drawImage( 0, 0, image );
        }
        else
        {
            painter->setPen( Qt::NoPen );
            painter->setBrush( brush );

            painter->drawRects( rects );
        }
    }
    else
    {
        painter->setPen( Qt::NoPen );
        painter->setBrush( brush );

        painter->drawRects( painter->clipRegion().rects() );

    }

    painter->restore();
}

static inline void qwtRevertPath( QPainterPath &path )
{
    if ( path.elementCount() == 4 )
    {
        QPainterPath::Element el0 = path.elementAt(0);
        QPainterPath::Element el3 = path.elementAt(3);

        path.setElementPositionAt( 0, el3.x, el3.y );
        path.setElementPositionAt( 3, el0.x, el0.y );
    }
}

static QPainterPath qwtCombinePathList( const QRectF &rect, 
    const QList<QPainterPath> &pathList )
{
    if ( pathList.isEmpty() )
        return QPainterPath();

    QPainterPath ordered[8]; // starting top left

    for ( int i = 0; i < pathList.size(); i++ )
    {
        int index = -1;
        QPainterPath subPath = pathList[i];

        const QRectF br = pathList[i].controlPointRect();
        if ( br.center().x() < rect.center().x() )
        {
            if ( br.center().y() < rect.center().y() )
            {
                if ( qAbs( br.top() - rect.top() ) < 
                    qAbs( br.left() - rect.left() ) )
                {
                    index = 1;
                }
                else
                {
                    index = 0;
                }
            }
            else
            {
                if ( qAbs( br.bottom() - rect.bottom() ) < 
                    qAbs( br.left() - rect.left() ) )
                {
                    index = 6;
                }
                else
                {
                    index = 7;
                }
            }

            if ( subPath.currentPosition().y() > br.center().y() )
                qwtRevertPath( subPath );
        }
        else
        {
            if ( br.center().y() < rect.center().y() )
            {
                if ( qAbs( br.top() - rect.top() ) < 
                    qAbs( br.right() - rect.right() ) )
                {
                    index = 2;
                }
                else
                {
                    index = 3;
                }
            }
            else
            {
                if ( qAbs( br.bottom() - rect.bottom() ) < 
                    qAbs( br.right() - rect.right() ) )
                {
                    index = 5;
                }
                else
                {
                    index = 4;
                }
            }
            if ( subPath.currentPosition().y() < br.center().y() )
                qwtRevertPath( subPath );
        }   
        ordered[index] = subPath;
    }

    for ( int i = 0; i < 4; i++ )
    {
        if ( ordered[ 2 * i].isEmpty() != ordered[2 * i + 1].isEmpty() )
        {
            // we don't accept incomplete rounded borders
            return QPainterPath();
        }
    }


    const QPolygonF corners( rect );

    QPainterPath path;
    //path.moveTo( rect.topLeft() );

    for ( int i = 0; i < 4; i++ )
    {
        if ( ordered[2 * i].isEmpty() )
        {
            path.lineTo( corners[i] );
        }
        else
        {
            path.connectPath( ordered[2 * i] );
            path.connectPath( ordered[2 * i + 1] );
        }
    }

    path.closeSubpath();

#if 0
    return path.simplified();
#else
    return path;
pixhawk's avatar
pixhawk committed
380
#endif
Bryant's avatar
Bryant committed
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
}

static inline void qwtDrawStyledBackground( 
    QWidget *w, QPainter *painter )
{
    QStyleOption opt;
    opt.initFrom(w);
    w->style()->drawPrimitive( QStyle::PE_Widget, &opt, painter, w);
}

static QWidget *qwtBackgroundWidget( QWidget *w )
{
    if ( w->parentWidget() == NULL )
        return w;

    if ( w->autoFillBackground() )
    {
        const QBrush brush = w->palette().brush( w->backgroundRole() );
        if ( brush.color().alpha() > 0 )
            return w;
    }

    if ( w->testAttribute( Qt::WA_StyledBackground ) )
    {
        QImage image( 1, 1, QImage::Format_ARGB32 );
        image.fill( Qt::transparent );

        QPainter painter( &image );
        painter.translate( -w->rect().center() );
        qwtDrawStyledBackground( w, &painter );
        painter.end();

        if ( qAlpha( image.pixel( 0, 0 ) ) != 0 )
            return w;
    }

    return qwtBackgroundWidget( w->parentWidget() );
}

static void qwtFillBackground( QPainter *painter, 
    QWidget *widget, const QVector<QRectF> &fillRects )
{
    if ( fillRects.isEmpty() )
        return;

    QRegion clipRegion;
    if ( painter->hasClipping() )
        clipRegion = painter->transform().map( painter->clipRegion() );
    else
        clipRegion = widget->contentsRect();

    // Try to find out which widget fills
    // the unfilled areas of the styled background

    QWidget *bgWidget = qwtBackgroundWidget( widget->parentWidget() );

    for ( int i = 0; i < fillRects.size(); i++ )
    {
        const QRect rect = fillRects[i].toAlignedRect();
        if ( clipRegion.intersects( rect ) )
        {
            QPixmap pm( rect.size() );
            QwtPainter::fillPixmap( bgWidget, pm, widget->mapTo( bgWidget, rect.topLeft() ) );
            painter->drawPixmap( rect, pm );
        }
    }
}

static void qwtFillBackground( QPainter *painter, QwtPlotCanvas *canvas )
{
    QVector<QRectF> rects;

    if ( canvas->testAttribute( Qt::WA_StyledBackground ) )
    {
        QwtStyleSheetRecorder recorder( canvas->size() );

        QPainter p( &recorder );
        qwtDrawStyledBackground( canvas, &p );
        p.end();

        if ( recorder.background.brush.isOpaque() )
            rects = recorder.clipRects;
        else
            rects += canvas->rect();
    }
    else
    {
        const QRectF r = canvas->rect();
        const double radius = canvas->borderRadius();
        if ( radius > 0.0 )
        {
            QSizeF sz( radius, radius );

            rects += QRectF( r.topLeft(), sz );
            rects += QRectF( r.topRight() - QPointF( radius, 0 ), sz );
            rects += QRectF( r.bottomRight() - QPointF( radius, radius ), sz );
            rects += QRectF( r.bottomLeft() - QPointF( 0, radius ), sz );
        }
    }

    qwtFillBackground( painter, canvas, rects);
}

pixhawk's avatar
pixhawk committed
484 485 486 487 488

class QwtPlotCanvas::PrivateData
{
public:
    PrivateData():
Bryant's avatar
Bryant committed
489 490 491 492 493 494
        focusIndicator( NoFocusIndicator ),
        borderRadius( 0 ),
        paintAttributes( 0 ),
        backingStore( NULL )
    {
        styleSheet.hasBorder = false;
pixhawk's avatar
pixhawk committed
495 496
    }

Bryant's avatar
Bryant committed
497 498 499
    ~PrivateData()
    {
        delete backingStore;
pixhawk's avatar
pixhawk committed
500 501 502
    }

    FocusIndicator focusIndicator;
Bryant's avatar
Bryant committed
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
    double borderRadius;
    QwtPlotCanvas::PaintAttributes paintAttributes;
    QPixmap *backingStore;

    struct StyleSheet
    {
        bool hasBorder;
        QPainterPath borderPath;
        QVector<QRectF> cornerRects;

        struct StyleSheetBackground
        {
            QBrush brush;
            QPointF origin;
        } background;

    } styleSheet;

pixhawk's avatar
pixhawk committed
521 522
};

Bryant's avatar
Bryant committed
523 524
/*! 
  \brief Constructor
pixhawk's avatar
pixhawk committed
525

Bryant's avatar
Bryant committed
526 527 528 529 530
  \param plot Parent plot widget
  \sa QwtPlot::setCanvas()
*/
QwtPlotCanvas::QwtPlotCanvas( QwtPlot *plot ):
    QFrame( plot )
pixhawk's avatar
pixhawk committed
531
{
Bryant's avatar
Bryant committed
532 533
    setFrameStyle( QFrame::Panel | QFrame::Sunken );
    setLineWidth( 2 );
pixhawk's avatar
pixhawk committed
534

Bryant's avatar
Bryant committed
535
    d_data = new PrivateData;
pixhawk's avatar
pixhawk committed
536 537

#ifndef QT_NO_CURSOR
Bryant's avatar
Bryant committed
538
    setCursor( Qt::CrossCursor );
pixhawk's avatar
pixhawk committed
539 540
#endif

Bryant's avatar
Bryant committed
541 542 543 544
    setAutoFillBackground( true );
    setPaintAttribute( QwtPlotCanvas::BackingStore, true );
    setPaintAttribute( QwtPlotCanvas::Opaque, true );
    setPaintAttribute( QwtPlotCanvas::HackStyledBackground, true );
pixhawk's avatar
pixhawk committed
545 546 547 548 549 550 551 552 553 554 555
}

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

//! Return parent plot widget
QwtPlot *QwtPlotCanvas::plot()
{
Bryant's avatar
Bryant committed
556
    return qobject_cast<QwtPlot *>( parent() );
pixhawk's avatar
pixhawk committed
557 558 559 560 561
}

//! Return parent plot widget
const QwtPlot *QwtPlotCanvas::plot() const
{
Bryant's avatar
Bryant committed
562
    return qobject_cast<const QwtPlot *>( parent() );
pixhawk's avatar
pixhawk committed
563 564 565 566 567 568 569 570
}

/*!
  \brief Changing the paint attributes

  \param attribute Paint attribute
  \param on On/Off

Bryant's avatar
Bryant committed
571
  \sa testPaintAttribute(), backingStore()
pixhawk's avatar
pixhawk committed
572
*/
Bryant's avatar
Bryant committed
573
void QwtPlotCanvas::setPaintAttribute( PaintAttribute attribute, bool on )
pixhawk's avatar
pixhawk committed
574
{
Bryant's avatar
Bryant committed
575
    if ( bool( d_data->paintAttributes & attribute ) == on )
pixhawk's avatar
pixhawk committed
576 577 578 579 580 581 582
        return;

    if ( on )
        d_data->paintAttributes |= attribute;
    else
        d_data->paintAttributes &= ~attribute;

Bryant's avatar
Bryant committed
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
    switch ( attribute )
    {
        case BackingStore:
        {
            if ( on )
            {
                if ( d_data->backingStore == NULL )
                    d_data->backingStore = new QPixmap();

                if ( isVisible() )
                {
#if QT_VERSION >= 0x050000
                    *d_data->backingStore = grab( rect() );
#else
                    *d_data->backingStore = 
                        QPixmap::grabWidget( this, rect() );
#endif
                }
601
            }
Bryant's avatar
Bryant committed
602 603 604 605 606 607
            else
            {
                delete d_data->backingStore;
                d_data->backingStore = NULL;
            }
            break;
pixhawk's avatar
pixhawk committed
608
        }
Bryant's avatar
Bryant committed
609 610 611 612
        case Opaque:
        {
            if ( on )
                setAttribute( Qt::WA_OpaquePaintEvent, true );
613

Bryant's avatar
Bryant committed
614 615 616 617 618 619 620
            break;
        }
        case HackStyledBackground:
        case ImmediatePaint:
        {
            break;
        }
pixhawk's avatar
pixhawk committed
621 622 623 624
    }
}

/*!
Bryant's avatar
Bryant committed
625
  Test whether a paint attribute is enabled
pixhawk's avatar
pixhawk committed
626 627

  \param attribute Paint attribute
Bryant's avatar
Bryant committed
628
  \return true, when attribute is enabled
pixhawk's avatar
pixhawk committed
629 630
  \sa setPaintAttribute()
*/
Bryant's avatar
Bryant committed
631
bool QwtPlotCanvas::testPaintAttribute( PaintAttribute attribute ) const
pixhawk's avatar
pixhawk committed
632
{
Bryant's avatar
Bryant committed
633
    return d_data->paintAttributes & attribute;
pixhawk's avatar
pixhawk committed
634 635
}

Bryant's avatar
Bryant committed
636 637
//! \return Backing store, might be null
const QPixmap *QwtPlotCanvas::backingStore() const
pixhawk's avatar
pixhawk committed
638
{
Bryant's avatar
Bryant committed
639
    return d_data->backingStore;
pixhawk's avatar
pixhawk committed
640 641
}

Bryant's avatar
Bryant committed
642 643
//! Invalidate the internal backing store
void QwtPlotCanvas::invalidateBackingStore()
pixhawk's avatar
pixhawk committed
644
{
Bryant's avatar
Bryant committed
645 646
    if ( d_data->backingStore )
        *d_data->backingStore = QPixmap();
pixhawk's avatar
pixhawk committed
647 648 649 650 651
}

/*!
  Set the focus indicator

Bryant's avatar
Bryant committed
652
  \sa FocusIndicator, focusIndicator()
pixhawk's avatar
pixhawk committed
653
*/
Bryant's avatar
Bryant committed
654
void QwtPlotCanvas::setFocusIndicator( FocusIndicator focusIndicator )
pixhawk's avatar
pixhawk committed
655 656 657 658 659 660
{
    d_data->focusIndicator = focusIndicator;
}

/*!
  \return Focus indicator
661

Bryant's avatar
Bryant committed
662
  \sa FocusIndicator, setFocusIndicator()
pixhawk's avatar
pixhawk committed
663 664 665 666 667 668
*/
QwtPlotCanvas::FocusIndicator QwtPlotCanvas::focusIndicator() const
{
    return d_data->focusIndicator;
}

Bryant's avatar
Bryant committed
669 670 671 672 673 674 675
/*!
  Set the radius for the corners of the border frame

  \param radius Radius of a rounded corner
  \sa borderRadius()
*/
void QwtPlotCanvas::setBorderRadius( double radius )
pixhawk's avatar
pixhawk committed
676
{
Bryant's avatar
Bryant committed
677 678 679 680 681 682 683 684 685 686 687
    d_data->borderRadius = qMax( 0.0, radius );
}

/*!
  \return Radius for the corners of the border frame
  \sa setBorderRadius()
*/
double QwtPlotCanvas::borderRadius() const
{
    return d_data->borderRadius;
}
pixhawk's avatar
pixhawk committed
688

Bryant's avatar
Bryant committed
689 690
/*!
  Qt event handler for QEvent::PolishRequest and QEvent::StyleChange
pixhawk's avatar
pixhawk committed
691

Bryant's avatar
Bryant committed
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
  \param event Qt Event
  \return See QFrame::event()
*/
bool QwtPlotCanvas::event( QEvent *event )
{
    if ( event->type() == QEvent::PolishRequest ) 
    {
        if ( testPaintAttribute( QwtPlotCanvas::Opaque ) )
        {
            // Setting a style sheet changes the 
            // Qt::WA_OpaquePaintEvent attribute, but we insist
            // on painting the background.
            
            setAttribute( Qt::WA_OpaquePaintEvent, true );
        }
pixhawk's avatar
pixhawk committed
707
    }
Bryant's avatar
Bryant committed
708 709 710 711 712 713 714 715

    if ( event->type() == QEvent::PolishRequest || 
        event->type() == QEvent::StyleChange )
    {
        updateStyleSheetInfo();
    }

    return QFrame::event( event );
pixhawk's avatar
pixhawk committed
716 717
}

Bryant's avatar
Bryant committed
718 719 720 721 722
/*!
  Paint event
  \param event Paint event
*/
void QwtPlotCanvas::paintEvent( QPaintEvent *event )
pixhawk's avatar
pixhawk committed
723
{
Bryant's avatar
Bryant committed
724 725 726 727 728 729 730 731 732 733
    QPainter painter( this );
    painter.setClipRegion( event->region() );

    if ( testPaintAttribute( QwtPlotCanvas::BackingStore ) &&
        d_data->backingStore != NULL )
    {
        QPixmap &bs = *d_data->backingStore;
        if ( bs.size() != size() )
        {
            bs = QwtPainter::backingStore( this, size() );
734

Bryant's avatar
Bryant committed
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
            if ( testAttribute(Qt::WA_StyledBackground) )
            {
                QPainter p( &bs );
                qwtFillBackground( &p, this );
                drawCanvas( &p, true );
            }
            else
            {
                QPainter p;
                if ( d_data->borderRadius <= 0.0 )
                {
                    QwtPainter::fillPixmap( this, bs );
                    p.begin( &bs );
                    drawCanvas( &p, false );
                }
                else
                {
                    p.begin( &bs );
                    qwtFillBackground( &p, this );
                    drawCanvas( &p, true );
                }

                if ( frameWidth() > 0 )
                    drawBorder( &p );
            }
        }

        painter.drawPixmap( 0, 0, *d_data->backingStore );
pixhawk's avatar
pixhawk committed
763
    }
Bryant's avatar
Bryant committed
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
    else
    {
        if ( testAttribute(Qt::WA_StyledBackground ) )
        {
            if ( testAttribute( Qt::WA_OpaquePaintEvent ) )
            {
                qwtFillBackground( &painter, this );
                drawCanvas( &painter, true );
            }
            else
            {
                drawCanvas( &painter, false );
            }
        }
        else
        {
            if ( testAttribute( Qt::WA_OpaquePaintEvent ) )
            {
                if ( autoFillBackground() )
                {
                    qwtFillBackground( &painter, this );
                    qwtDrawBackground( &painter, this );
                }
            }
            else
            {
                if ( borderRadius() > 0.0 )
                {
                    QPainterPath clipPath;
                    clipPath.addRect( rect() );
                    clipPath = clipPath.subtracted( borderPath( rect() ) );

                    painter.save();

                    painter.setClipPath( clipPath, Qt::IntersectClip );
                    qwtFillBackground( &painter, this );
                    qwtDrawBackground( &painter, this );

                    painter.restore();
                }
            }
pixhawk's avatar
pixhawk committed
805

Bryant's avatar
Bryant committed
806
            drawCanvas( &painter, false );
pixhawk's avatar
pixhawk committed
807

Bryant's avatar
Bryant committed
808 809 810 811
            if ( frameWidth() > 0 ) 
                drawBorder( &painter );
        }
    }
pixhawk's avatar
pixhawk committed
812

Bryant's avatar
Bryant committed
813 814
    if ( hasFocus() && focusIndicator() == CanvasFocusIndicator )
        drawFocusIndicator( &painter );
pixhawk's avatar
pixhawk committed
815 816
}

Bryant's avatar
Bryant committed
817
void QwtPlotCanvas::drawCanvas( QPainter *painter, bool withBackground ) 
pixhawk's avatar
pixhawk committed
818
{
Bryant's avatar
Bryant committed
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
    bool hackStyledBackground = false;

    if ( withBackground && testAttribute( Qt::WA_StyledBackground ) 
        && testPaintAttribute( HackStyledBackground ) )
    {
        // Antialiasing rounded borders is done by
        // inserting pixels with colors between the 
        // border color and the color on the canvas,
        // When the border is painted before the plot items
        // these colors are interpolated for the canvas
        // and the plot items need to be clipped excluding
        // the anialiased pixels. In situations, where
        // the plot items fill the area at the rounded
        // borders this is noticeable.
        // The only way to avoid these annoying "artefacts"
        // is to paint the border on top of the plot items.

        if ( d_data->styleSheet.hasBorder &&
            !d_data->styleSheet.borderPath.isEmpty() )
        {
            // We have a border with at least one rounded corner
            hackStyledBackground = true;
        }
    }
pixhawk's avatar
pixhawk committed
843

Bryant's avatar
Bryant committed
844 845 846
    if ( withBackground )
    {
        painter->save();
pixhawk's avatar
pixhawk committed
847

Bryant's avatar
Bryant committed
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
        if ( testAttribute( Qt::WA_StyledBackground ) )
        {
            if ( hackStyledBackground )
            {
                // paint background without border

                painter->setPen( Qt::NoPen );
                painter->setBrush( d_data->styleSheet.background.brush ); 
                painter->setBrushOrigin( d_data->styleSheet.background.origin );
                painter->setClipPath( d_data->styleSheet.borderPath );
                painter->drawRect( contentsRect() );
            }
            else
            {
                qwtDrawStyledBackground( this, painter );
            }
        }
        else if ( autoFillBackground() )
        {
            painter->setPen( Qt::NoPen );
            painter->setBrush( palette().brush( backgroundRole() ) );

            if ( d_data->borderRadius > 0.0 && ( rect() == frameRect() ) )
            {
                if ( frameWidth() > 0 )
                {
                    painter->setClipPath( borderPath( rect() ) );
                    painter->drawRect( rect() );
                }
                else
                {
                    painter->setRenderHint( QPainter::Antialiasing, true );
                    painter->drawPath( borderPath( rect() ) );
                }
            }
            else
            {
                painter->drawRect( rect() );
            }
        }

        painter->restore();
pixhawk's avatar
pixhawk committed
890 891
    }

Bryant's avatar
Bryant committed
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
    painter->save();

    if ( !d_data->styleSheet.borderPath.isEmpty() )
    {
        painter->setClipPath( 
            d_data->styleSheet.borderPath, Qt::IntersectClip );
    }
    else
    {
        if ( d_data->borderRadius > 0.0 )
            painter->setClipPath( borderPath( frameRect() ), Qt::IntersectClip );
        else
            painter->setClipRect( contentsRect(), Qt::IntersectClip );
    }

    plot()->drawCanvas( painter );

    painter->restore();

    if ( withBackground && hackStyledBackground )
    {
        // Now paint the border on top
        QStyleOptionFrame opt;
        opt.initFrom(this);
        style()->drawPrimitive( QStyle::PE_Frame, &opt, painter, this);
    }
pixhawk's avatar
pixhawk committed
918 919 920
}

/*!
Bryant's avatar
Bryant committed
921
  Draw the border of the plot canvas
pixhawk's avatar
pixhawk committed
922

Bryant's avatar
Bryant committed
923 924
  \param painter Painter
  \sa setBorderRadius()
pixhawk's avatar
pixhawk committed
925
*/
Bryant's avatar
Bryant committed
926
void QwtPlotCanvas::drawBorder( QPainter *painter )
pixhawk's avatar
pixhawk committed
927
{
Bryant's avatar
Bryant committed
928 929 930 931 932 933 934 935 936 937 938 939
    if ( d_data->borderRadius > 0 )
    {
        if ( frameWidth() > 0 )
        {
            QwtPainter::drawRoundedFrame( painter, QRectF( frameRect() ), 
                d_data->borderRadius, d_data->borderRadius,
                palette(), frameWidth(), frameStyle() );
        }
    }
    else
    {
#if QT_VERSION >= 0x040500
Don Gagne's avatar
Don Gagne committed
940 941 942 943
#if !defined(_MSC_VER)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
Bryant's avatar
Bryant committed
944
        QStyleOptionFrameV3 opt;
Don Gagne's avatar
Don Gagne committed
945 946 947
#if !defined(_MSC_VER)
#pragma GCC diagnostic pop
#endif
Bryant's avatar
Bryant committed
948
        opt.init(this);
pixhawk's avatar
pixhawk committed
949

Bryant's avatar
Bryant committed
950 951 952 953 954 955
        int frameShape  = frameStyle() & QFrame::Shape_Mask;
        int frameShadow = frameStyle() & QFrame::Shadow_Mask;

        opt.frameShape = QFrame::Shape( int( opt.frameShape ) | frameShape );
#if 0
        opt.rect = frameRect();
pixhawk's avatar
pixhawk committed
956 957
#endif

Bryant's avatar
Bryant committed
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
        switch (frameShape) 
        {
            case QFrame::Box:
            case QFrame::HLine:
            case QFrame::VLine:
            case QFrame::StyledPanel:
            case QFrame::Panel:
            {
                opt.lineWidth = lineWidth();
                opt.midLineWidth = midLineWidth();
                break; 
            }
            default: 
            {
                opt.lineWidth = frameWidth();
                break;
            }
        }
    
        if ( frameShadow == Sunken )
            opt.state |= QStyle::State_Sunken;
        else if ( frameShadow == Raised )
            opt.state |= QStyle::State_Raised;
pixhawk's avatar
pixhawk committed
981

Bryant's avatar
Bryant committed
982
        style()->drawControl(QStyle::CE_ShapedFrame, &opt, painter, this);
pixhawk's avatar
pixhawk committed
983
#else
Bryant's avatar
Bryant committed
984
        drawFrame( painter );
pixhawk's avatar
pixhawk committed
985
#endif
Bryant's avatar
Bryant committed
986 987
    }
}
pixhawk's avatar
pixhawk committed
988

Bryant's avatar
Bryant committed
989 990 991 992 993 994 995 996 997
/*!
  Resize event
  \param event Resize event
*/
void QwtPlotCanvas::resizeEvent( QResizeEvent *event )
{
    QFrame::resizeEvent( event );
    updateStyleSheetInfo();
}
pixhawk's avatar
pixhawk committed
998

Bryant's avatar
Bryant committed
999 1000 1001 1002 1003 1004 1005
/*!
  Draw the focus indication
  \param painter Painter
*/
void QwtPlotCanvas::drawFocusIndicator( QPainter *painter )
{
    const int margin = 1;
pixhawk's avatar
pixhawk committed
1006

Bryant's avatar
Bryant committed
1007 1008 1009
    QRect focusRect = contentsRect();
    focusRect.setRect( focusRect.x() + margin, focusRect.y() + margin,
        focusRect.width() - 2 * margin, focusRect.height() - 2 * margin );
pixhawk's avatar
pixhawk committed
1010

Bryant's avatar
Bryant committed
1011 1012
    QwtPainter::drawFocusRect( painter, this, focusRect );
}
pixhawk's avatar
pixhawk committed
1013

Bryant's avatar
Bryant committed
1014 1015 1016 1017 1018 1019 1020
/*!
   Invalidate the paint cache and repaint the canvas
   \sa invalidatePaintCache()
*/
void QwtPlotCanvas::replot()
{
    invalidateBackingStore();
pixhawk's avatar
pixhawk committed
1021

Bryant's avatar
Bryant committed
1022 1023 1024 1025 1026
    if ( testPaintAttribute( QwtPlotCanvas::ImmediatePaint ) )
        repaint( contentsRect() );
    else
        update( contentsRect() );
}
pixhawk's avatar
pixhawk committed
1027

Bryant's avatar
Bryant committed
1028 1029 1030 1031 1032
//! Update the cached information about the current style sheet
void QwtPlotCanvas::updateStyleSheetInfo()
{
    if ( !testAttribute(Qt::WA_StyledBackground ) )
        return;
pixhawk's avatar
pixhawk committed
1033

Bryant's avatar
Bryant committed
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
    QwtStyleSheetRecorder recorder( size() );
    
    QPainter painter( &recorder );
    
    QStyleOption opt;
    opt.initFrom(this);
    style()->drawPrimitive( QStyle::PE_Widget, &opt, &painter, this);
    
    painter.end();

    d_data->styleSheet.hasBorder = !recorder.border.rectList.isEmpty();
    d_data->styleSheet.cornerRects = recorder.clipRects;

    if ( recorder.background.path.isEmpty() )
    {
        if ( !recorder.border.rectList.isEmpty() )
        {
            d_data->styleSheet.borderPath = 
                qwtCombinePathList( rect(), recorder.border.pathList );
pixhawk's avatar
pixhawk committed
1053
        }
Bryant's avatar
Bryant committed
1054 1055 1056 1057 1058 1059
    }
    else
    {
        d_data->styleSheet.borderPath = recorder.background.path;
        d_data->styleSheet.background.brush = recorder.background.brush;
        d_data->styleSheet.background.origin = recorder.background.origin;
pixhawk's avatar
pixhawk committed
1060 1061 1062
    }
}

Bryant's avatar
Bryant committed
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
/*!
   Calculate the painter path for a styled or rounded border

   When the canvas has no styled background or rounded borders
   the painter path is empty.

   \param rect Bounding rectangle of the canvas
   \return Painter path, that can be used for clipping
*/
QPainterPath QwtPlotCanvas::borderPath( const QRect &rect ) const
pixhawk's avatar
pixhawk committed
1073
{
Bryant's avatar
Bryant committed
1074 1075 1076
    if ( testAttribute(Qt::WA_StyledBackground ) )
    {
        QwtStyleSheetRecorder recorder( rect.size() );
pixhawk's avatar
pixhawk committed
1077

Bryant's avatar
Bryant committed
1078
        QPainter painter( &recorder );
pixhawk's avatar
pixhawk committed
1079

Bryant's avatar
Bryant committed
1080 1081 1082 1083
        QStyleOption opt;
        opt.initFrom(this);
        opt.rect = rect;
        style()->drawPrimitive( QStyle::PE_Widget, &opt, &painter, this);
pixhawk's avatar
pixhawk committed
1084

Bryant's avatar
Bryant committed
1085 1086 1087 1088 1089 1090 1091
        painter.end();

        if ( !recorder.background.path.isEmpty() )
            return recorder.background.path;

        if ( !recorder.border.rectList.isEmpty() )
            return qwtCombinePathList( rect, recorder.border.pathList );
pixhawk's avatar
pixhawk committed
1092
    }
Bryant's avatar
Bryant committed
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
    else if ( d_data->borderRadius > 0.0 )
    {
        double fw2 = frameWidth() * 0.5;
        QRectF r = QRectF(rect).adjusted( fw2, fw2, -fw2, -fw2 );

        QPainterPath path;
        path.addRoundedRect( r, d_data->borderRadius, d_data->borderRadius );
        return path;
    }
    
    return QPainterPath();
pixhawk's avatar
pixhawk committed
1104
}