Changeset 11158 in ntrip for trunk/BNC/qwt/qwt_plot_canvas.cpp
- Timestamp:
- Nov 3, 2026, 10:53:35 AM (2 weeks ago)
- File:
-
- 1 edited
-
trunk/BNC/qwt/qwt_plot_canvas.cpp (modified) (20 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/BNC/qwt/qwt_plot_canvas.cpp
r9383 r11158 1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*-*****************************1 /****************************************************************************** 2 2 * Qwt Widget Library 3 3 * Copyright (C) 1997 Josef Wilgen … … 10 10 #include "qwt_plot_canvas.h" 11 11 #include "qwt_painter.h" 12 #include "qwt_null_paintdevice.h"13 #include "qwt_math.h"14 12 #include "qwt_plot.h" 13 15 14 #include <qpainter.h> 16 #include <qstyle.h> 17 #include <qstyleoption.h> 18 #include <qpaintengine.h> 15 #include <qpainterpath.h> 19 16 #include <qevent.h> 20 17 21 class QwtStyleSheetRecorder: public QwtNullPaintDevice 22 { 23 public: 24 QwtStyleSheetRecorder( const QSize &size ): 25 d_size( size ) 26 { 27 } 28 29 virtual void updateState( const QPaintEngineState &state ) 30 { 31 if ( state.state() & QPaintEngine::DirtyPen ) 32 { 33 d_pen = state.pen(); 34 } 35 if ( state.state() & QPaintEngine::DirtyBrush ) 36 { 37 d_brush = state.brush(); 38 } 39 if ( state.state() & QPaintEngine::DirtyBrushOrigin ) 40 { 41 d_origin = state.brushOrigin(); 42 } 43 } 44 45 virtual void drawRects(const QRectF *rects, int count ) 46 { 47 for ( int i = 0; i < count; i++ ) 48 border.rectList += rects[i]; 49 } 50 51 virtual void drawRects(const QRect *rects, int count ) 52 { 53 // to silence -Woverloaded-virtual 54 QwtNullPaintDevice::drawRects( rects, count ); 55 } 56 57 virtual void drawPath( const QPainterPath &path ) 58 { 59 const QRectF rect( QPointF( 0.0, 0.0 ), d_size ); 60 if ( path.controlPointRect().contains( rect.center() ) ) 61 { 62 setCornerRects( path ); 63 alignCornerRects( rect ); 64 65 background.path = path; 66 background.brush = d_brush; 67 background.origin = d_origin; 68 } 69 else 70 { 71 border.pathList += path; 72 } 73 } 74 75 void setCornerRects( const QPainterPath &path ) 76 { 77 QPointF pos( 0.0, 0.0 ); 78 79 for ( int i = 0; i < path.elementCount(); i++ ) 80 { 81 QPainterPath::Element el = path.elementAt(i); 82 switch( el.type ) 83 { 84 case QPainterPath::MoveToElement: 85 case QPainterPath::LineToElement: 86 { 87 pos.setX( el.x ); 88 pos.setY( el.y ); 89 break; 90 } 91 case QPainterPath::CurveToElement: 92 { 93 QRectF r( pos, QPointF( el.x, el.y ) ); 94 clipRects += r.normalized(); 95 96 pos.setX( el.x ); 97 pos.setY( el.y ); 98 99 break; 100 } 101 case QPainterPath::CurveToDataElement: 102 { 103 if ( clipRects.size() > 0 ) 104 { 105 QRectF r = clipRects.last(); 106 r.setCoords( 107 qMin( r.left(), el.x ), 108 qMin( r.top(), el.y ), 109 qMax( r.right(), el.x ), 110 qMax( r.bottom(), el.y ) 111 ); 112 clipRects.last() = r.normalized(); 113 } 114 break; 115 } 116 } 117 } 118 } 119 120 protected: 121 virtual QSize sizeMetrics() const 122 { 123 return d_size; 124 } 125 126 private: 127 void alignCornerRects( const QRectF &rect ) 128 { 129 for ( int i = 0; i < clipRects.size(); i++ ) 130 { 131 QRectF &r = clipRects[i]; 132 if ( r.center().x() < rect.center().x() ) 133 r.setLeft( rect.left() ); 134 else 135 r.setRight( rect.right() ); 136 137 if ( r.center().y() < rect.center().y() ) 138 r.setTop( rect.top() ); 139 else 140 r.setBottom( rect.bottom() ); 141 } 142 } 143 144 145 public: 146 QVector<QRectF> clipRects; 147 148 struct Border 149 { 150 QList<QPainterPath> pathList; 151 QList<QRectF> rectList; 152 QRegion clipRegion; 153 } border; 154 155 struct Background 156 { 157 QPainterPath path; 158 QBrush brush; 159 QPointF origin; 160 } background; 161 162 private: 163 const QSize d_size; 164 165 QPen d_pen; 166 QBrush d_brush; 167 QPointF d_origin; 18 class QwtPlotCanvas::PrivateData 19 { 20 public: 21 PrivateData() 22 : backingStore( NULL ) 23 { 24 } 25 26 ~PrivateData() 27 { 28 delete backingStore; 29 } 30 31 QwtPlotCanvas::PaintAttributes paintAttributes; 32 QPixmap* backingStore; 168 33 }; 169 34 170 static void qwtDrawBackground( QPainter *painter, QwtPlotCanvas *canvas ) 171 { 172 painter->save(); 173 174 const QPainterPath borderClip = canvas->borderPath( canvas->rect() ); 175 if ( !borderClip.isEmpty() ) 176 painter->setClipPath( borderClip, Qt::IntersectClip ); 177 178 const QBrush &brush = 179 canvas->palette().brush( canvas->backgroundRole() ); 180 181 if ( brush.style() == Qt::TexturePattern ) 182 { 183 QPixmap pm( canvas->size() ); 184 QwtPainter::fillPixmap( canvas, pm ); 185 painter->drawPixmap( 0, 0, pm ); 186 } 187 else if ( brush.gradient() ) 188 { 189 QVector<QRect> rects; 190 191 if ( brush.gradient()->coordinateMode() == QGradient::ObjectBoundingMode ) 192 { 193 rects += canvas->rect(); 194 } 195 else 196 { 197 rects = painter->clipRegion().rects(); 198 } 199 200 #if 1 201 bool useRaster = false; 202 203 if ( painter->paintEngine()->type() == QPaintEngine::X11 ) 204 { 205 // Qt 4.7.1: gradients on X11 are broken ( subrects + 206 // QGradient::StretchToDeviceMode ) and horrible slow. 207 // As workaround we have to use the raster paintengine. 208 // Even if the QImage -> QPixmap translation is slow 209 // it is three times faster, than using X11 directly 210 211 useRaster = true; 212 } 213 #endif 214 if ( useRaster ) 215 { 216 QImage::Format format = QImage::Format_RGB32; 217 218 const QGradientStops stops = brush.gradient()->stops(); 219 for ( int i = 0; i < stops.size(); i++ ) 220 { 221 if ( stops[i].second.alpha() != 255 ) 222 { 223 // don't use Format_ARGB32_Premultiplied. It's 224 // recommended by the Qt docs, but QPainter::drawImage() 225 // is horrible slow on X11. 226 227 format = QImage::Format_ARGB32; 228 break; 229 } 230 } 231 232 QImage image( canvas->size(), format ); 233 234 QPainter p( &image ); 235 p.setPen( Qt::NoPen ); 236 p.setBrush( brush ); 237 238 p.drawRects( rects ); 239 240 p.end(); 241 242 painter->drawImage( 0, 0, image ); 243 } 244 else 245 { 246 painter->setPen( Qt::NoPen ); 247 painter->setBrush( brush ); 248 249 painter->drawRects( rects ); 250 } 251 } 252 else 253 { 254 painter->setPen( Qt::NoPen ); 255 painter->setBrush( brush ); 256 257 painter->drawRects( painter->clipRegion().rects() ); 258 259 } 260 261 painter->restore(); 262 } 263 264 static inline void qwtRevertPath( QPainterPath &path ) 265 { 266 if ( path.elementCount() == 4 ) 267 { 268 QPainterPath::Element el0 = path.elementAt(0); 269 QPainterPath::Element el3 = path.elementAt(3); 270 271 path.setElementPositionAt( 0, el3.x, el3.y ); 272 path.setElementPositionAt( 3, el0.x, el0.y ); 273 } 274 } 275 276 static QPainterPath qwtCombinePathList( const QRectF &rect, 277 const QList<QPainterPath> &pathList ) 278 { 279 if ( pathList.isEmpty() ) 280 return QPainterPath(); 281 282 QPainterPath ordered[8]; // starting top left 283 284 for ( int i = 0; i < pathList.size(); i++ ) 285 { 286 int index = -1; 287 QPainterPath subPath = pathList[i]; 288 289 const QRectF br = pathList[i].controlPointRect(); 290 if ( br.center().x() < rect.center().x() ) 291 { 292 if ( br.center().y() < rect.center().y() ) 293 { 294 if ( qAbs( br.top() - rect.top() ) < 295 qAbs( br.left() - rect.left() ) ) 296 { 297 index = 1; 298 } 299 else 300 { 301 index = 0; 302 } 303 } 304 else 305 { 306 if ( qAbs( br.bottom() - rect.bottom() ) < 307 qAbs( br.left() - rect.left() ) ) 308 { 309 index = 6; 310 } 311 else 312 { 313 index = 7; 314 } 315 } 316 317 if ( subPath.currentPosition().y() > br.center().y() ) 318 qwtRevertPath( subPath ); 319 } 320 else 321 { 322 if ( br.center().y() < rect.center().y() ) 323 { 324 if ( qAbs( br.top() - rect.top() ) < 325 qAbs( br.right() - rect.right() ) ) 326 { 327 index = 2; 328 } 329 else 330 { 331 index = 3; 332 } 333 } 334 else 335 { 336 if ( qAbs( br.bottom() - rect.bottom() ) < 337 qAbs( br.right() - rect.right() ) ) 338 { 339 index = 5; 340 } 341 else 342 { 343 index = 4; 344 } 345 } 346 if ( subPath.currentPosition().y() < br.center().y() ) 347 qwtRevertPath( subPath ); 348 } 349 ordered[index] = subPath; 350 } 351 352 for ( int i = 0; i < 4; i++ ) 353 { 354 if ( ordered[ 2 * i].isEmpty() != ordered[2 * i + 1].isEmpty() ) 355 { 356 // we don't accept incomplete rounded borders 357 return QPainterPath(); 358 } 359 } 360 361 362 const QPolygonF corners( rect ); 363 364 QPainterPath path; 365 //path.moveTo( rect.topLeft() ); 366 367 for ( int i = 0; i < 4; i++ ) 368 { 369 if ( ordered[2 * i].isEmpty() ) 370 { 371 path.lineTo( corners[i] ); 372 } 373 else 374 { 375 path.connectPath( ordered[2 * i] ); 376 path.connectPath( ordered[2 * i + 1] ); 377 } 378 } 379 380 path.closeSubpath(); 381 382 #if 0 383 return path.simplified(); 384 #else 385 return path; 386 #endif 387 } 388 389 static inline void qwtDrawStyledBackground( 390 QWidget *w, QPainter *painter ) 391 { 392 QStyleOption opt; 393 opt.initFrom(w); 394 w->style()->drawPrimitive( QStyle::PE_Widget, &opt, painter, w); 395 } 396 397 static QWidget *qwtBackgroundWidget( QWidget *w ) 398 { 399 if ( w->parentWidget() == NULL ) 400 return w; 401 402 if ( w->autoFillBackground() ) 403 { 404 const QBrush brush = w->palette().brush( w->backgroundRole() ); 405 if ( brush.color().alpha() > 0 ) 406 return w; 407 } 408 409 if ( w->testAttribute( Qt::WA_StyledBackground ) ) 410 { 411 QImage image( 1, 1, QImage::Format_ARGB32 ); 412 image.fill( Qt::transparent ); 413 414 QPainter painter( &image ); 415 painter.translate( -w->rect().center() ); 416 qwtDrawStyledBackground( w, &painter ); 417 painter.end(); 418 419 if ( qAlpha( image.pixel( 0, 0 ) ) != 0 ) 420 return w; 421 } 422 423 return qwtBackgroundWidget( w->parentWidget() ); 424 } 425 426 static void qwtFillBackground( QPainter *painter, 427 QWidget *widget, const QVector<QRectF> &fillRects ) 428 { 429 if ( fillRects.isEmpty() ) 430 return; 431 432 QRegion clipRegion; 433 if ( painter->hasClipping() ) 434 clipRegion = painter->transform().map( painter->clipRegion() ); 435 else 436 clipRegion = widget->contentsRect(); 437 438 // Try to find out which widget fills 439 // the unfilled areas of the styled background 440 441 QWidget *bgWidget = qwtBackgroundWidget( widget->parentWidget() ); 442 443 for ( int i = 0; i < fillRects.size(); i++ ) 444 { 445 const QRect rect = fillRects[i].toAlignedRect(); 446 if ( clipRegion.intersects( rect ) ) 447 { 448 QPixmap pm( rect.size() ); 449 QwtPainter::fillPixmap( bgWidget, pm, widget->mapTo( bgWidget, rect.topLeft() ) ); 450 painter->drawPixmap( rect, pm ); 451 } 452 } 453 } 454 455 static void qwtFillBackground( QPainter *painter, QwtPlotCanvas *canvas ) 456 { 457 QVector<QRectF> rects; 458 459 if ( canvas->testAttribute( Qt::WA_StyledBackground ) ) 460 { 461 QwtStyleSheetRecorder recorder( canvas->size() ); 462 463 QPainter p( &recorder ); 464 qwtDrawStyledBackground( canvas, &p ); 465 p.end(); 466 467 if ( recorder.background.brush.isOpaque() ) 468 rects = recorder.clipRects; 469 else 470 rects += canvas->rect(); 471 } 472 else 473 { 474 const QRectF r = canvas->rect(); 475 const double radius = canvas->borderRadius(); 476 if ( radius > 0.0 ) 477 { 478 QSizeF sz( radius, radius ); 479 480 rects += QRectF( r.topLeft(), sz ); 481 rects += QRectF( r.topRight() - QPointF( radius, 0 ), sz ); 482 rects += QRectF( r.bottomRight() - QPointF( radius, radius ), sz ); 483 rects += QRectF( r.bottomLeft() - QPointF( 0, radius ), sz ); 484 } 485 } 486 487 qwtFillBackground( painter, canvas, rects); 488 } 489 490 491 class QwtPlotCanvas::PrivateData 492 { 493 public: 494 PrivateData(): 495 focusIndicator( NoFocusIndicator ), 496 borderRadius( 0 ), 497 paintAttributes( 0 ), 498 backingStore( NULL ) 499 { 500 styleSheet.hasBorder = false; 501 } 502 503 ~PrivateData() 504 { 505 delete backingStore; 506 } 507 508 FocusIndicator focusIndicator; 509 double borderRadius; 510 QwtPlotCanvas::PaintAttributes paintAttributes; 511 QPixmap *backingStore; 512 513 struct StyleSheet 514 { 515 bool hasBorder; 516 QPainterPath borderPath; 517 QVector<QRectF> cornerRects; 518 519 struct StyleSheetBackground 520 { 521 QBrush brush; 522 QPointF origin; 523 } background; 524 525 } styleSheet; 526 527 }; 528 529 /*! 530 \brief Constructor 531 532 \param plot Parent plot widget 533 \sa QwtPlot::setCanvas() 534 */ 535 QwtPlotCanvas::QwtPlotCanvas( QwtPlot *plot ): 536 QFrame( plot ) 537 { 538 setFrameStyle( QFrame::Panel | QFrame::Sunken ); 539 setLineWidth( 2 ); 540 541 d_data = new PrivateData; 542 543 #ifndef QT_NO_CURSOR 544 setCursor( Qt::CrossCursor ); 545 #endif 546 547 setAutoFillBackground( true ); 35 /*! 36 \brief Constructor 37 38 \param plot Parent plot widget 39 \sa QwtPlot::setCanvas() 40 */ 41 QwtPlotCanvas::QwtPlotCanvas( QwtPlot* plot ) 42 : QFrame( plot ) 43 , QwtPlotAbstractCanvas( this ) 44 { 45 m_data = new PrivateData; 46 548 47 setPaintAttribute( QwtPlotCanvas::BackingStore, true ); 549 48 setPaintAttribute( QwtPlotCanvas::Opaque, true ); 550 49 setPaintAttribute( QwtPlotCanvas::HackStyledBackground, true ); 50 51 setLineWidth( 2 ); 52 setFrameShadow( QFrame::Sunken ); 53 setFrameShape( QFrame::Panel ); 551 54 } 552 55 … … 554 57 QwtPlotCanvas::~QwtPlotCanvas() 555 58 { 556 delete d_data; 557 } 558 559 //! Return parent plot widget 560 QwtPlot *QwtPlotCanvas::plot() 561 { 562 return qobject_cast<QwtPlot *>( parent() ); 563 } 564 565 //! Return parent plot widget 566 const QwtPlot *QwtPlotCanvas::plot() const 567 { 568 return qobject_cast<const QwtPlot *>( parent() ); 569 } 570 571 /*! 572 \brief Changing the paint attributes 573 574 \param attribute Paint attribute 575 \param on On/Off 576 577 \sa testPaintAttribute(), backingStore() 578 */ 59 delete m_data; 60 } 61 62 /*! 63 \brief Changing the paint attributes 64 65 \param attribute Paint attribute 66 \param on On/Off 67 68 \sa testPaintAttribute(), backingStore() 69 */ 579 70 void QwtPlotCanvas::setPaintAttribute( PaintAttribute attribute, bool on ) 580 71 { 581 if ( bool( d_data->paintAttributes & attribute ) == on )72 if ( bool( m_data->paintAttributes & attribute ) == on ) 582 73 return; 583 74 584 75 if ( on ) 585 d_data->paintAttributes |= attribute;76 m_data->paintAttributes |= attribute; 586 77 else 587 d_data->paintAttributes &= ~attribute;78 m_data->paintAttributes &= ~attribute; 588 79 589 80 switch ( attribute ) … … 593 84 if ( on ) 594 85 { 595 if ( d_data->backingStore == NULL )596 d_data->backingStore = new QPixmap();86 if ( m_data->backingStore == NULL ) 87 m_data->backingStore = new QPixmap(); 597 88 598 89 if ( isVisible() ) 599 90 { 600 91 #if QT_VERSION >= 0x050000 601 * d_data->backingStore = grab( rect() );92 *m_data->backingStore = grab( rect() ); 602 93 #else 603 * d_data->backingStore =94 *m_data->backingStore = 604 95 QPixmap::grabWidget( this, rect() ); 605 96 #endif … … 608 99 else 609 100 { 610 delete d_data->backingStore;611 d_data->backingStore = NULL;101 delete m_data->backingStore; 102 m_data->backingStore = NULL; 612 103 } 613 104 break; … … 620 111 break; 621 112 } 622 case HackStyledBackground: 623 case ImmediatePaint: 113 default: 624 114 { 625 115 break; … … 629 119 630 120 /*! 631 Test whether a paint attribute is enabled 632 633 \param attribute Paint attribute 634 \return true, when attribute is enabled 635 \sa setPaintAttribute() 636 */ 121 Test whether a paint attribute is enabled 122 123 \param attribute Paint attribute 124 \return true, when attribute is enabled 125 \sa setPaintAttribute() 126 */ 637 127 bool QwtPlotCanvas::testPaintAttribute( PaintAttribute attribute ) const 638 128 { 639 return d_data->paintAttributes & attribute;129 return m_data->paintAttributes & attribute; 640 130 } 641 131 642 132 //! \return Backing store, might be null 643 const QPixmap *QwtPlotCanvas::backingStore() const644 { 645 return d_data->backingStore;133 const QPixmap* QwtPlotCanvas::backingStore() const 134 { 135 return m_data->backingStore; 646 136 } 647 137 … … 649 139 void QwtPlotCanvas::invalidateBackingStore() 650 140 { 651 if ( d_data->backingStore ) 652 *d_data->backingStore = QPixmap(); 653 } 654 655 /*! 656 Set the focus indicator 657 658 \sa FocusIndicator, focusIndicator() 659 */ 660 void QwtPlotCanvas::setFocusIndicator( FocusIndicator focusIndicator ) 661 { 662 d_data->focusIndicator = focusIndicator; 663 } 664 665 /*! 666 \return Focus indicator 667 668 \sa FocusIndicator, setFocusIndicator() 669 */ 670 QwtPlotCanvas::FocusIndicator QwtPlotCanvas::focusIndicator() const 671 { 672 return d_data->focusIndicator; 673 } 674 675 /*! 676 Set the radius for the corners of the border frame 677 678 \param radius Radius of a rounded corner 679 \sa borderRadius() 680 */ 681 void QwtPlotCanvas::setBorderRadius( double radius ) 682 { 683 d_data->borderRadius = qMax( 0.0, radius ); 684 } 685 686 /*! 687 \return Radius for the corners of the border frame 688 \sa setBorderRadius() 689 */ 690 double QwtPlotCanvas::borderRadius() const 691 { 692 return d_data->borderRadius; 693 } 694 695 /*! 696 Qt event handler for QEvent::PolishRequest and QEvent::StyleChange 697 698 \param event Qt Event 699 \return See QFrame::event() 700 */ 701 bool QwtPlotCanvas::event( QEvent *event ) 141 if ( m_data->backingStore ) 142 *m_data->backingStore = QPixmap(); 143 } 144 145 /*! 146 Qt event handler for QEvent::PolishRequest and QEvent::StyleChange 147 148 \param event Qt Event 149 \return See QFrame::event() 150 */ 151 bool QwtPlotCanvas::event( QEvent* event ) 702 152 { 703 153 if ( event->type() == QEvent::PolishRequest ) … … 723 173 724 174 /*! 725 Paint event 726 \param event Paint event 727 */ 728 void QwtPlotCanvas::paintEvent( QPaintEvent *event )175 Paint event 176 \param event Paint event 177 */ 178 void QwtPlotCanvas::paintEvent( QPaintEvent* event ) 729 179 { 730 180 QPainter painter( this ); … … 732 182 733 183 if ( testPaintAttribute( QwtPlotCanvas::BackingStore ) && 734 d_data->backingStore != NULL ) 735 { 736 QPixmap &bs = *d_data->backingStore; 737 738 qreal pixelRatio = 1.0; 739 740 #if QT_VERSION >= 0x050000 741 pixelRatio = bs.devicePixelRatio(); 742 #endif 743 744 if ( bs.size() != size() * pixelRatio ) 184 m_data->backingStore != NULL ) 185 { 186 QPixmap& bs = *m_data->backingStore; 187 if ( bs.size() != size() * QwtPainter::devicePixelRatio( &bs ) ) 745 188 { 746 189 bs = QwtPainter::backingStore( this, size() ); … … 749 192 { 750 193 QPainter p( &bs ); 751 qwtFillBackground( &p, this ); 752 drawCanvas( &p, true ); 194 drawStyled( &p, testPaintAttribute( HackStyledBackground ) ); 753 195 } 754 196 else 755 197 { 756 198 QPainter p; 757 if ( d_data->borderRadius <= 0.0 )199 if ( borderRadius() <= 0.0 ) 758 200 { 759 201 QwtPainter::fillPixmap( this, bs ); 760 202 p.begin( &bs ); 761 drawCanvas( &p , false);203 drawCanvas( &p ); 762 204 } 763 205 else 764 206 { 765 207 p.begin( &bs ); 766 qwtFillBackground( &p, this ); 767 drawCanvas( &p, true ); 208 drawUnstyled( &p ); 768 209 } 769 210 … … 773 214 } 774 215 775 painter.drawPixmap( 0, 0, * d_data->backingStore );216 painter.drawPixmap( 0, 0, *m_data->backingStore ); 776 217 } 777 218 else … … 781 222 if ( testAttribute( Qt::WA_OpaquePaintEvent ) ) 782 223 { 783 qwtFillBackground( &painter, this ); 784 drawCanvas( &painter, true ); 224 drawStyled( &painter, testPaintAttribute( HackStyledBackground ) ); 785 225 } 786 226 else 787 227 { 788 drawCanvas( &painter , false);228 drawCanvas( &painter ); 789 229 } 790 230 } … … 795 235 if ( autoFillBackground() ) 796 236 { 797 qwtFillBackground( &painter, this);798 qwtDrawBackground( &painter, this);237 fillBackground( &painter ); 238 drawBackground( &painter ); 799 239 } 800 240 } … … 810 250 811 251 painter.setClipPath( clipPath, Qt::IntersectClip ); 812 qwtFillBackground( &painter, this);813 qwtDrawBackground( &painter, this);252 fillBackground( &painter ); 253 drawBackground( &painter ); 814 254 815 255 painter.restore(); … … 817 257 } 818 258 819 drawCanvas( &painter , false);259 drawCanvas( &painter ); 820 260 821 261 if ( frameWidth() > 0 ) … … 828 268 } 829 269 830 void QwtPlotCanvas::drawCanvas( QPainter *painter, bool withBackground ) 831 { 832 bool hackStyledBackground = false; 833 834 if ( withBackground && testAttribute( Qt::WA_StyledBackground ) 835 && testPaintAttribute( HackStyledBackground ) ) 836 { 837 // Antialiasing rounded borders is done by 838 // inserting pixels with colors between the 839 // border color and the color on the canvas, 840 // When the border is painted before the plot items 841 // these colors are interpolated for the canvas 842 // and the plot items need to be clipped excluding 843 // the anialiased pixels. In situations, where 844 // the plot items fill the area at the rounded 845 // borders this is noticeable. 846 // The only way to avoid these annoying "artefacts" 847 // is to paint the border on top of the plot items. 848 849 if ( d_data->styleSheet.hasBorder && 850 !d_data->styleSheet.borderPath.isEmpty() ) 851 { 852 // We have a border with at least one rounded corner 853 hackStyledBackground = true; 854 } 855 } 856 857 if ( withBackground ) 858 { 859 painter->save(); 860 861 if ( testAttribute( Qt::WA_StyledBackground ) ) 862 { 863 if ( hackStyledBackground ) 864 { 865 // paint background without border 866 867 painter->setPen( Qt::NoPen ); 868 painter->setBrush( d_data->styleSheet.background.brush ); 869 painter->setBrushOrigin( d_data->styleSheet.background.origin ); 870 painter->setClipPath( d_data->styleSheet.borderPath ); 871 painter->drawRect( contentsRect() ); 872 } 873 else 874 { 875 qwtDrawStyledBackground( this, painter ); 876 } 877 } 878 else if ( autoFillBackground() ) 879 { 880 painter->setPen( Qt::NoPen ); 881 painter->setBrush( palette().brush( backgroundRole() ) ); 882 883 if ( d_data->borderRadius > 0.0 && ( rect() == frameRect() ) ) 884 { 885 if ( frameWidth() > 0 ) 886 { 887 painter->setClipPath( borderPath( rect() ) ); 888 painter->drawRect( rect() ); 889 } 890 else 891 { 892 painter->setRenderHint( QPainter::Antialiasing, true ); 893 painter->drawPath( borderPath( rect() ) ); 894 } 895 } 896 else 897 { 898 painter->drawRect( rect() ); 899 } 900 } 901 902 painter->restore(); 903 } 904 905 painter->save(); 906 907 if ( !d_data->styleSheet.borderPath.isEmpty() ) 908 { 909 painter->setClipPath( 910 d_data->styleSheet.borderPath, Qt::IntersectClip ); 911 } 912 else 913 { 914 if ( d_data->borderRadius > 0.0 ) 915 painter->setClipPath( borderPath( frameRect() ), Qt::IntersectClip ); 916 else 917 painter->setClipRect( contentsRect(), Qt::IntersectClip ); 918 } 919 920 plot()->drawCanvas( painter ); 921 922 painter->restore(); 923 924 if ( withBackground && hackStyledBackground ) 925 { 926 // Now paint the border on top 927 QStyleOptionFrame opt; 928 opt.initFrom(this); 929 style()->drawPrimitive( QStyle::PE_Frame, &opt, painter, this); 930 } 931 } 932 933 /*! 934 Draw the border of the plot canvas 935 936 \param painter Painter 937 \sa setBorderRadius() 938 */ 939 void QwtPlotCanvas::drawBorder( QPainter *painter ) 940 { 941 if ( d_data->borderRadius > 0 ) 942 { 943 if ( frameWidth() > 0 ) 944 { 945 QwtPainter::drawRoundedFrame( painter, QRectF( frameRect() ), 946 d_data->borderRadius, d_data->borderRadius, 947 palette(), frameWidth(), frameStyle() ); 948 } 949 } 950 else 951 { 952 #if QT_VERSION >= 0x040500 953 #if QT_VERSION < 0x050000 954 QStyleOptionFrameV3 opt; 955 #else 956 QStyleOptionFrame opt; 957 #endif 958 opt.init(this); 959 960 int frameShape = frameStyle() & QFrame::Shape_Mask; 961 int frameShadow = frameStyle() & QFrame::Shadow_Mask; 962 963 opt.frameShape = QFrame::Shape( int( opt.frameShape ) | frameShape ); 964 #if 0 965 opt.rect = frameRect(); 966 #endif 967 968 switch (frameShape) 969 { 970 case QFrame::Box: 971 case QFrame::HLine: 972 case QFrame::VLine: 973 case QFrame::StyledPanel: 974 case QFrame::Panel: 975 { 976 opt.lineWidth = lineWidth(); 977 opt.midLineWidth = midLineWidth(); 978 break; 979 } 980 default: 981 { 982 opt.lineWidth = frameWidth(); 983 break; 984 } 985 } 986 987 if ( frameShadow == Sunken ) 988 opt.state |= QStyle::State_Sunken; 989 else if ( frameShadow == Raised ) 990 opt.state |= QStyle::State_Raised; 991 992 style()->drawControl(QStyle::CE_ShapedFrame, &opt, painter, this); 993 #else 270 /*! 271 Draw the border of the plot canvas 272 273 \param painter Painter 274 \sa setBorderRadius() 275 */ 276 void QwtPlotCanvas::drawBorder( QPainter* painter ) 277 { 278 if ( borderRadius() <= 0 ) 279 { 994 280 drawFrame( painter ); 995 #endif 996 } 997 } 998 999 /*! 1000 Resize event 1001 \param event Resize event 1002 */ 1003 void QwtPlotCanvas::resizeEvent( QResizeEvent *event ) 281 return; 282 } 283 284 QwtPlotAbstractCanvas::drawBorder( painter ); 285 } 286 287 /*! 288 Resize event 289 \param event Resize event 290 */ 291 void QwtPlotCanvas::resizeEvent( QResizeEvent* event ) 1004 292 { 1005 293 QFrame::resizeEvent( event ); … … 1008 296 1009 297 /*! 1010 Draw the focus indication1011 \param painter Painter1012 */1013 void QwtPlotCanvas::drawFocusIndicator( QPainter *painter )1014 {1015 const int margin = 1;1016 1017 QRect focusRect = contentsRect();1018 focusRect.setRect( focusRect.x() + margin, focusRect.y() + margin,1019 focusRect.width() - 2 * margin, focusRect.height() - 2 * margin );1020 1021 QwtPainter::drawFocusRect( painter, this, focusRect );1022 }1023 1024 /*!1025 298 Invalidate the paint cache and repaint the canvas 1026 299 \sa invalidatePaintCache() 1027 */ 300 */ 1028 301 void QwtPlotCanvas::replot() 1029 302 { … … 1036 309 } 1037 310 1038 //! Update the cached information about the current style sheet1039 void QwtPlotCanvas::updateStyleSheetInfo()1040 {1041 if ( !testAttribute(Qt::WA_StyledBackground ) )1042 return;1043 1044 QwtStyleSheetRecorder recorder( size() );1045 1046 QPainter painter( &recorder );1047 1048 QStyleOption opt;1049 opt.initFrom(this);1050 style()->drawPrimitive( QStyle::PE_Widget, &opt, &painter, this);1051 1052 painter.end();1053 1054 d_data->styleSheet.hasBorder = !recorder.border.rectList.isEmpty();1055 d_data->styleSheet.cornerRects = recorder.clipRects;1056 1057 if ( recorder.background.path.isEmpty() )1058 {1059 if ( !recorder.border.rectList.isEmpty() )1060 {1061 d_data->styleSheet.borderPath =1062 qwtCombinePathList( rect(), recorder.border.pathList );1063 }1064 }1065 else1066 {1067 d_data->styleSheet.borderPath = recorder.background.path;1068 d_data->styleSheet.background.brush = recorder.background.brush;1069 d_data->styleSheet.background.origin = recorder.background.origin;1070 }1071 }1072 1073 311 /*! 1074 312 Calculate the painter path for a styled or rounded border … … 1079 317 \param rect Bounding rectangle of the canvas 1080 318 \return Painter path, that can be used for clipping 1081 */ 1082 QPainterPath QwtPlotCanvas::borderPath( const QRect &rect ) const 1083 { 1084 if ( testAttribute(Qt::WA_StyledBackground ) ) 1085 { 1086 QwtStyleSheetRecorder recorder( rect.size() ); 1087 1088 QPainter painter( &recorder ); 1089 1090 QStyleOption opt; 1091 opt.initFrom(this); 1092 opt.rect = rect; 1093 style()->drawPrimitive( QStyle::PE_Widget, &opt, &painter, this); 1094 1095 painter.end(); 1096 1097 if ( !recorder.background.path.isEmpty() ) 1098 return recorder.background.path; 1099 1100 if ( !recorder.border.rectList.isEmpty() ) 1101 return qwtCombinePathList( rect, recorder.border.pathList ); 1102 } 1103 else if ( d_data->borderRadius > 0.0 ) 1104 { 1105 double fw2 = frameWidth() * 0.5; 1106 QRectF r = QRectF(rect).adjusted( fw2, fw2, -fw2, -fw2 ); 1107 1108 QPainterPath path; 1109 path.addRoundedRect( r, d_data->borderRadius, d_data->borderRadius ); 1110 return path; 1111 } 1112 1113 return QPainterPath(); 1114 } 319 */ 320 QPainterPath QwtPlotCanvas::borderPath( const QRect& rect ) const 321 { 322 return canvasBorderPath( rect ); 323 } 324 325 #include "moc_qwt_plot_canvas.cpp"
Note:
See TracChangeset
for help on using the changeset viewer.
