Changeset 11158 in ntrip for trunk/BNC/qwt/qwt_graphic.cpp
- Timestamp:
- Nov 3, 2026, 10:53:35 AM (3 weeks ago)
- File:
-
- 1 edited
-
trunk/BNC/qwt/qwt_graphic.cpp (modified) (44 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/BNC/qwt/qwt_graphic.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_graphic.h" 11 11 #include "qwt_painter_command.h" 12 #include "qwt_math.h" 13 12 14 #include <qvector.h> 13 15 #include <qpainter.h> … … 16 18 #include <qpixmap.h> 17 19 #include <qpainterpath.h> 18 #include <qmath.h> 19 20 static bool qwtHasScalablePen( const QPainter *painter ) 20 21 #if QT_VERSION >= 0x050000 22 23 #include <qguiapplication.h> 24 25 static inline qreal qwtDevicePixelRatio() 26 { 27 return qGuiApp ? qGuiApp->devicePixelRatio() : 1.0; 28 } 29 30 #endif 31 32 static bool qwtHasScalablePen( const QPainter* painter ) 21 33 { 22 34 const QPen pen = painter->pen(); … … 27 39 { 28 40 scalablePen = !pen.isCosmetic(); 41 #if QT_VERSION < 0x050000 29 42 if ( !scalablePen && pen.widthF() == 0.0 ) 30 43 { … … 33 46 scalablePen = true; 34 47 } 48 #endif 35 49 } 36 50 … … 39 53 40 54 static QRectF qwtStrokedPathRect( 41 const QPainter *painter, const QPainterPath&path )55 const QPainter* painter, const QPainterPath& path ) 42 56 { 43 57 QPainterPathStroker stroker; … … 50 64 if ( qwtHasScalablePen( painter ) ) 51 65 { 52 QPainterPath stroke = stroker.createStroke( path);53 rect = painter->transform().map( stroke).boundingRect();66 QPainterPath stroke = stroker.createStroke( path ); 67 rect = painter->transform().map( stroke ).boundingRect(); 54 68 } 55 69 else 56 70 { 57 QPainterPath mappedPath = painter->transform().map( path);71 QPainterPath mappedPath = painter->transform().map( path ); 58 72 mappedPath = stroker.createStroke( mappedPath ); 59 73 … … 65 79 66 80 static inline void qwtExecCommand( 67 QPainter *painter, const QwtPainterCommand&cmd,81 QPainter* painter, const QwtPainterCommand& cmd, 68 82 QwtGraphic::RenderHints renderHints, 69 const QTransform &transform,70 const QTransform *initialTransform )83 const QTransform& transform, 84 const QTransform* initialTransform ) 71 85 { 72 86 switch( cmd.type() ) … … 76 90 bool doMap = false; 77 91 78 if ( renderHints.testFlag( QwtGraphic::RenderPensUnscaled ) 79 && painter->transform().isScaling() ) 92 if ( painter->transform().isScaling() ) 80 93 { 81 94 bool isCosmetic = painter->pen().isCosmetic(); 95 #if QT_VERSION < 0x050000 82 96 if ( isCosmetic && painter->pen().widthF() == 0.0 ) 83 97 { … … 86 100 isCosmetic = false; 87 101 } 88 89 doMap = !isCosmetic; 102 #endif 103 104 if ( isCosmetic ) 105 { 106 // OpenGL2 seems to be buggy for cosmetic pens. 107 // It interpolates curves in too rough steps then 108 109 doMap = painter->paintEngine()->type() == QPaintEngine::OpenGL2; 110 } 111 else 112 { 113 doMap = renderHints.testFlag( QwtGraphic::RenderPensUnscaled ); 114 } 90 115 } 91 116 … … 115 140 case QwtPainterCommand::Pixmap: 116 141 { 117 const QwtPainterCommand::PixmapData *data = cmd.pixmapData();142 const QwtPainterCommand::PixmapData* data = cmd.pixmapData(); 118 143 painter->drawPixmap( data->rect, data->pixmap, data->subRect ); 119 144 break; … … 121 146 case QwtPainterCommand::Image: 122 147 { 123 const QwtPainterCommand::ImageData *data = cmd.imageData();148 const QwtPainterCommand::ImageData* data = cmd.imageData(); 124 149 painter->drawImage( data->rect, data->image, 125 150 data->subRect, data->flags ); … … 128 153 case QwtPainterCommand::State: 129 154 { 130 const QwtPainterCommand::StateData *data = cmd.stateData();155 const QwtPainterCommand::StateData* data = cmd.stateData(); 131 156 132 157 if ( data->flags & QPaintEngine::DirtyPen ) … … 156 181 painter->setClipping( data->isClipEnabled ); 157 182 158 if ( data->flags & QPaintEngine::DirtyClipRegion) 183 if ( data->flags & QPaintEngine::DirtyClipRegion ) 159 184 { 160 185 painter->setClipRegion( data->clipRegion, … … 167 192 } 168 193 169 if ( data->flags & QPaintEngine::DirtyHints) 194 if ( data->flags & QPaintEngine::DirtyHints ) 170 195 { 171 const QPainter::RenderHints hints = data->renderHints; 172 173 painter->setRenderHint( QPainter::Antialiasing, 174 hints.testFlag( QPainter::Antialiasing ) ); 175 176 painter->setRenderHint( QPainter::TextAntialiasing, 177 hints.testFlag( QPainter::TextAntialiasing ) ); 178 179 painter->setRenderHint( QPainter::SmoothPixmapTransform, 180 hints.testFlag( QPainter::SmoothPixmapTransform ) ); 181 182 painter->setRenderHint( QPainter::HighQualityAntialiasing, 183 hints.testFlag( QPainter::HighQualityAntialiasing ) ); 184 185 painter->setRenderHint( QPainter::NonCosmeticDefaultPen, 186 hints.testFlag( QPainter::NonCosmeticDefaultPen ) ); 196 for ( int i = 0; i < 8; i++ ) 197 { 198 const QPainter::RenderHint hint = static_cast< QPainter::RenderHint >( 1 << i ); 199 painter->setRenderHint( hint, data->renderHints.testFlag( hint ) ); 200 } 187 201 } 188 202 189 if ( data->flags & QPaintEngine::DirtyCompositionMode) 203 if ( data->flags & QPaintEngine::DirtyCompositionMode ) 190 204 painter->setCompositionMode( data->compositionMode ); 191 205 192 if ( data->flags & QPaintEngine::DirtyOpacity) 206 if ( data->flags & QPaintEngine::DirtyOpacity ) 193 207 painter->setOpacity( data->opacity ); 194 208 … … 198 212 break; 199 213 } 200 201 214 } 202 215 203 216 class QwtGraphic::PathInfo 204 217 { 205 public: 206 PathInfo() :207 d_scalablePen( false )218 public: 219 PathInfo() 220 : m_scalablePen( false ) 208 221 { 209 222 // QVector needs a default constructor 210 223 } 211 224 212 PathInfo( const QRectF &pointRect, 213 const QRectF &boundingRect, bool scalablePen ): 214 d_pointRect( pointRect ), 215 d_boundingRect( boundingRect ), 216 d_scalablePen( scalablePen ) 217 { 218 } 219 220 inline QRectF scaledBoundingRect( double sx, double sy, 221 bool scalePens ) const 225 PathInfo( const QRectF& pointRect, 226 const QRectF& boundingRect, bool scalablePen ) 227 : m_pointRect( pointRect ) 228 , m_boundingRect( boundingRect ) 229 , m_scalablePen( scalablePen ) 230 { 231 } 232 233 inline QRectF scaledBoundingRect( qreal sx, qreal sy, bool scalePens ) const 222 234 { 223 235 if ( sx == 1.0 && sy == 1.0 ) 224 return d_boundingRect;236 return m_boundingRect; 225 237 226 238 QTransform transform; … … 228 240 229 241 QRectF rect; 230 if ( scalePens && d_scalablePen )231 { 232 rect = transform.mapRect( d_boundingRect );242 if ( scalePens && m_scalablePen ) 243 { 244 rect = transform.mapRect( m_boundingRect ); 233 245 } 234 246 else 235 247 { 236 rect = transform.mapRect( d_pointRect );237 238 const doublel = qAbs(d_pointRect.left() -d_boundingRect.left() );239 const doubler = qAbs(d_pointRect.right() -d_boundingRect.right() );240 const doublet = qAbs(d_pointRect.top() -d_boundingRect.top() );241 const doubleb = qAbs(d_pointRect.bottom() -d_boundingRect.bottom() );248 rect = transform.mapRect( m_pointRect ); 249 250 const qreal l = qAbs( m_pointRect.left() - m_boundingRect.left() ); 251 const qreal r = qAbs( m_pointRect.right() - m_boundingRect.right() ); 252 const qreal t = qAbs( m_pointRect.top() - m_boundingRect.top() ); 253 const qreal b = qAbs( m_pointRect.bottom() - m_boundingRect.bottom() ); 242 254 243 255 rect.adjust( -l, -t, r, b ); … … 248 260 249 261 inline double scaleFactorX( const QRectF& pathRect, 250 const QRectF &targetRect, bool scalePens ) const262 const QRectF& targetRect, bool scalePens ) const 251 263 { 252 264 if ( pathRect.width() <= 0.0 ) 253 265 return 0.0; 254 266 255 const QPointF p0 = d_pointRect.center();256 257 const doublel = qAbs( pathRect.left() - p0.x() );258 const doubler = qAbs( pathRect.right() - p0.x() );259 260 const double w = 2.0 * q Min( l, r )267 const QPointF p0 = m_pointRect.center(); 268 269 const qreal l = qAbs( pathRect.left() - p0.x() ); 270 const qreal r = qAbs( pathRect.right() - p0.x() ); 271 272 const double w = 2.0 * qwtMinF( l, r ) 261 273 * targetRect.width() / pathRect.width(); 262 274 263 275 double sx; 264 if ( scalePens && d_scalablePen )265 { 266 sx = w / d_boundingRect.width();276 if ( scalePens && m_scalablePen ) 277 { 278 sx = w / m_boundingRect.width(); 267 279 } 268 280 else 269 281 { 270 const doublepw = qMax(271 qAbs( d_boundingRect.left() -d_pointRect.left() ),272 qAbs( d_boundingRect.right() -d_pointRect.right() ) );273 274 sx = ( w - 2 * pw ) / d_pointRect.width();282 const qreal pw = qwtMaxF( 283 qAbs( m_boundingRect.left() - m_pointRect.left() ), 284 qAbs( m_boundingRect.right() - m_pointRect.right() ) ); 285 286 sx = ( w - 2 * pw ) / m_pointRect.width(); 275 287 } 276 288 … … 279 291 280 292 inline double scaleFactorY( const QRectF& pathRect, 281 const QRectF &targetRect, bool scalePens ) const293 const QRectF& targetRect, bool scalePens ) const 282 294 { 283 295 if ( pathRect.height() <= 0.0 ) 284 296 return 0.0; 285 297 286 const QPointF p0 = d_pointRect.center();287 288 const doublet = qAbs( pathRect.top() - p0.y() );289 const doubleb = qAbs( pathRect.bottom() - p0.y() );290 291 const doubleh = 2.0 * qMin( t, b )298 const QPointF p0 = m_pointRect.center(); 299 300 const qreal t = qAbs( pathRect.top() - p0.y() ); 301 const qreal b = qAbs( pathRect.bottom() - p0.y() ); 302 303 const qreal h = 2.0 * qwtMinF( t, b ) 292 304 * targetRect.height() / pathRect.height(); 293 305 294 306 double sy; 295 if ( scalePens && d_scalablePen )296 { 297 sy = h / d_boundingRect.height();307 if ( scalePens && m_scalablePen ) 308 { 309 sy = h / m_boundingRect.height(); 298 310 } 299 311 else 300 312 { 301 const double pw =302 q Max( qAbs( d_boundingRect.top() -d_pointRect.top() ),303 qAbs( d_boundingRect.bottom() -d_pointRect.bottom() ) );304 305 sy = ( h - 2 * pw ) / d_pointRect.height();313 const qreal pw = qwtMaxF( 314 qAbs( m_boundingRect.top() - m_pointRect.top() ), 315 qAbs( m_boundingRect.bottom() - m_pointRect.bottom() ) ); 316 317 sy = ( h - 2 * pw ) / m_pointRect.height(); 306 318 } 307 319 … … 309 321 } 310 322 311 private: 312 QRectF d_pointRect;313 QRectF d_boundingRect;314 bool d_scalablePen;323 private: 324 QRectF m_pointRect; 325 QRectF m_boundingRect; 326 bool m_scalablePen; 315 327 }; 316 328 317 329 class QwtGraphic::PrivateData 318 330 { 319 public: 320 PrivateData(): 321 boundingRect( 0.0, 0.0, -1.0, -1.0 ), 322 pointRect( 0.0, 0.0, -1.0, -1.0 ), 323 initialTransform( NULL ) 331 public: 332 PrivateData() 333 : boundingRect( 0.0, 0.0, -1.0, -1.0 ) 334 , pointRect( 0.0, 0.0, -1.0, -1.0 ) 324 335 { 325 336 } 326 337 327 338 QSizeF defaultSize; 328 QVector<QwtPainterCommand> commands; 329 QVector<QwtGraphic::PathInfo> pathInfos; 339 QVector< QwtPainterCommand > commands; 340 QVector< QwtGraphic::PathInfo > pathInfos; 330 341 331 342 QRectF boundingRect; 332 343 QRectF pointRect; 333 344 345 QwtGraphic::CommandTypes commandTypes; 334 346 QwtGraphic::RenderHints renderHints; 335 QTransform *initialTransform;336 347 }; 337 348 338 349 /*! 339 \brief Constructor 340 341 Initializes a null graphic 342 \sa isNull() 343 */ 344 QwtGraphic::QwtGraphic(): 345 QwtNullPaintDevice() 350 \brief Constructor 351 352 Initializes a null graphic 353 \sa isNull() 354 */ 355 QwtGraphic::QwtGraphic() 346 356 { 347 357 setMode( QwtNullPaintDevice::PathMode ); 348 d_data = new PrivateData; 349 } 350 351 /*! 352 \brief Copy constructor 353 354 \param other Source 355 \sa operator=() 356 */ 357 QwtGraphic::QwtGraphic( const QwtGraphic &other ): 358 QwtNullPaintDevice() 358 m_data = new PrivateData; 359 } 360 361 /*! 362 \brief Copy constructor 363 364 \param other Source 365 \sa operator=() 366 */ 367 QwtGraphic::QwtGraphic( const QwtGraphic& other ) 359 368 { 360 369 setMode( other.mode() ); 361 d_data = new PrivateData( *other.d_data );370 m_data = new PrivateData( *other.m_data ); 362 371 } 363 372 … … 365 374 QwtGraphic::~QwtGraphic() 366 375 { 367 delete d_data;368 } 369 370 /*! 371 \brief Assignment operator 372 373 \param other Source 374 \return A reference of this object 375 */ 376 QwtGraphic& QwtGraphic::operator=(const QwtGraphic &other)376 delete m_data; 377 } 378 379 /*! 380 \brief Assignment operator 381 382 \param other Source 383 \return A reference of this object 384 */ 385 QwtGraphic& QwtGraphic::operator=( const QwtGraphic& other ) 377 386 { 378 387 setMode( other.mode() ); 379 * d_data = *other.d_data;388 *m_data = *other.m_data; 380 389 381 390 return *this; … … 383 392 384 393 /*! 385 \brief Clear all stored commands 386 \sa isNull() 394 \brief Clear all stored commands 395 \sa isNull() 387 396 */ 388 397 void QwtGraphic::reset() 389 398 { 390 d_data->commands.clear(); 391 d_data->pathInfos.clear(); 392 393 d_data->boundingRect = QRectF( 0.0, 0.0, -1.0, -1.0 ); 394 d_data->pointRect = QRectF( 0.0, 0.0, -1.0, -1.0 ); 395 d_data->defaultSize = QSizeF(); 396 397 } 398 399 /*! 400 \return True, when no painter commands have been stored 401 \sa isEmpty(), commands() 402 */ 399 m_data->commands.clear(); 400 m_data->pathInfos.clear(); 401 402 m_data->commandTypes = CommandTypes(); 403 404 m_data->boundingRect = QRectF( 0.0, 0.0, -1.0, -1.0 ); 405 m_data->pointRect = QRectF( 0.0, 0.0, -1.0, -1.0 ); 406 m_data->defaultSize = QSizeF(); 407 } 408 409 /*! 410 \return True, when no painter commands have been stored 411 \sa isEmpty(), commands() 412 */ 403 413 bool QwtGraphic::isNull() const 404 414 { 405 return d_data->commands.isEmpty();406 } 407 408 /*! 409 \return True, when the bounding rectangle is empty 410 \sa boundingRect(), isNull() 411 */ 415 return m_data->commands.isEmpty(); 416 } 417 418 /*! 419 \return True, when the bounding rectangle is empty 420 \sa boundingRect(), isNull() 421 */ 412 422 bool QwtGraphic::isEmpty() const 413 423 { 414 return d_data->boundingRect.isEmpty(); 415 } 416 417 /*! 418 Toggle an render hint 419 420 \param hint Render hint 421 \param on true/false 422 423 \sa testRenderHint(), RenderHint 424 */ 424 return m_data->boundingRect.isEmpty(); 425 } 426 427 /*! 428 \return Types of painter commands being used 429 */ 430 QwtGraphic::CommandTypes QwtGraphic::commandTypes() const 431 { 432 return m_data->commandTypes; 433 } 434 435 /*! 436 Toggle an render hint 437 438 \param hint Render hint 439 \param on true/false 440 441 \sa testRenderHint(), RenderHint 442 */ 425 443 void QwtGraphic::setRenderHint( RenderHint hint, bool on ) 426 444 { 427 445 if ( on ) 428 d_data->renderHints |= hint;446 m_data->renderHints |= hint; 429 447 else 430 d_data->renderHints &= ~hint;431 } 432 433 /*! 434 Test a render hint 435 436 \param hint Render hint 437 \return true/false 438 \sa setRenderHint(), RenderHint 439 */ 448 m_data->renderHints &= ~hint; 449 } 450 451 /*! 452 Test a render hint 453 454 \param hint Render hint 455 \return true/false 456 \sa setRenderHint(), RenderHint 457 */ 440 458 bool QwtGraphic::testRenderHint( RenderHint hint ) const 441 459 { 442 return d_data->renderHints.testFlag( hint ); 443 } 444 445 /*! 446 The bounding rectangle is the controlPointRect() 447 extended by the areas needed for rendering the outlines 448 with unscaled pens. 449 450 \return Bounding rectangle of the graphic 451 \sa controlPointRect(), scaledBoundingRect() 460 return m_data->renderHints.testFlag( hint ); 461 } 462 463 //! \return Render hints 464 QwtGraphic::RenderHints QwtGraphic::renderHints() const 465 { 466 return m_data->renderHints; 467 } 468 469 /*! 470 The bounding rectangle is the controlPointRect() 471 extended by the areas needed for rendering the outlines 472 with unscaled pens. 473 474 \return Bounding rectangle of the graphic 475 \sa controlPointRect(), scaledBoundingRect() 452 476 */ 453 477 QRectF QwtGraphic::boundingRect() const 454 478 { 455 if ( d_data->boundingRect.width() < 0 )479 if ( m_data->boundingRect.width() < 0 ) 456 480 return QRectF(); 457 481 458 return d_data->boundingRect;459 } 460 461 /*! 462 The control point rectangle is the bounding rectangle 463 of all control points of the paths and the target 464 rectangles of the images/pixmaps. 465 466 \return Control point rectangle 467 \sa boundingRect(), scaledBoundingRect() 482 return m_data->boundingRect; 483 } 484 485 /*! 486 The control point rectangle is the bounding rectangle 487 of all control points of the paths and the target 488 rectangles of the images/pixmaps. 489 490 \return Control point rectangle 491 \sa boundingRect(), scaledBoundingRect() 468 492 */ 469 493 QRectF QwtGraphic::controlPointRect() const 470 494 { 471 if ( d_data->pointRect.width() < 0 )495 if ( m_data->pointRect.width() < 0 ) 472 496 return QRectF(); 473 497 474 return d_data->pointRect;475 } 476 477 /*! 478 \brief Calculate the target rectangle for scaling the graphic 479 480 \param sx Horizontal scaling factor 481 \param sy Vertical scaling factor 482 483 \note In case of paths that are painted with a cosmetic pen 498 return m_data->pointRect; 499 } 500 501 /*! 502 \brief Calculate the target rectangle for scaling the graphic 503 504 \param sx Horizontal scaling factor 505 \param sy Vertical scaling factor 506 507 \note In case of paths that are painted with a cosmetic pen 484 508 ( see QPen::isCosmetic() ) the target rectangle is different to 485 509 multiplying the bounding rectangle. 486 510 487 \return Scaled bounding rectangle 488 \sa boundingRect(), controlPointRect() 489 */ 490 QRectF QwtGraphic::scaledBoundingRect( double sx, doublesy ) const511 \return Scaled bounding rectangle 512 \sa boundingRect(), controlPointRect() 513 */ 514 QRectF QwtGraphic::scaledBoundingRect( qreal sx, qreal sy ) const 491 515 { 492 516 if ( sx == 1.0 && sy == 1.0 ) 493 return d_data->boundingRect; 517 return m_data->boundingRect; 518 519 const bool scalePens = !( m_data->renderHints & RenderPensUnscaled ); 494 520 495 521 QTransform transform; 496 522 transform.scale( sx, sy ); 497 523 498 QRectF rect = transform.mapRect( d_data->pointRect ); 499 500 for ( int i = 0; i < d_data->pathInfos.size(); i++ ) 501 { 502 rect |= d_data->pathInfos[i].scaledBoundingRect( sx, sy, 503 !d_data->renderHints.testFlag( RenderPensUnscaled ) ); 504 } 524 QRectF rect = transform.mapRect( m_data->pointRect ); 525 526 for ( int i = 0; i < m_data->pathInfos.size(); i++ ) 527 rect |= m_data->pathInfos[i].scaledBoundingRect( sx, sy, scalePens ); 505 528 506 529 return rect; … … 511 534 { 512 535 const QSizeF sz = defaultSize(); 513 return QSize( qCeil( sz.width() ), qCeil( sz.height() ) ); 514 } 515 516 /*! 517 \brief Set a default size 518 519 The default size is used in all methods rendering the graphic, 520 where no size is explicitly specified. Assigning an empty size 521 means, that the default size will be calculated from the bounding 522 rectangle. 523 524 The default setting is an empty size. 525 526 \param size Default size 527 528 \sa defaultSize(), boundingRect() 529 */ 530 void QwtGraphic::setDefaultSize( const QSizeF &size )531 { 532 const double w = q Max( qreal( 0.0 ), size.width() );533 const double h = q Max( qreal( 0.0 ), size.height() );534 535 d_data->defaultSize = QSizeF( w, h );536 } 537 538 /*! 539 \brief Default size 540 541 When a non empty size has been assigned by setDefaultSize() this 542 size will be returned. Otherwise the default size is the size 543 of the bounding rectangle. 544 545 The default size is used in all methods rendering the graphic, 546 where no size is explicitly specified. 547 548 \return Default size 549 \sa setDefaultSize(), boundingRect() 536 return QSize( qwtCeil( sz.width() ), qwtCeil( sz.height() ) ); 537 } 538 539 /*! 540 \brief Set a default size 541 542 The default size is used in all methods rendering the graphic, 543 where no size is explicitly specified. Assigning an empty size 544 means, that the default size will be calculated from the bounding 545 rectangle. 546 547 The default setting is an empty size. 548 549 \param size Default size 550 551 \sa defaultSize(), boundingRect() 552 */ 553 void QwtGraphic::setDefaultSize( const QSizeF& size ) 554 { 555 const double w = qwtMaxF( 0.0, size.width() ); 556 const double h = qwtMaxF( 0.0, size.height() ); 557 558 m_data->defaultSize = QSizeF( w, h ); 559 } 560 561 /*! 562 \brief Default size 563 564 When a non empty size has been assigned by setDefaultSize() this 565 size will be returned. Otherwise the default size is the size 566 of the bounding rectangle. 567 568 The default size is used in all methods rendering the graphic, 569 where no size is explicitly specified. 570 571 \return Default size 572 \sa setDefaultSize(), boundingRect() 550 573 */ 551 574 QSizeF QwtGraphic::defaultSize() const 552 575 { 553 if ( ! d_data->defaultSize.isEmpty() )554 return d_data->defaultSize;576 if ( !m_data->defaultSize.isEmpty() ) 577 return m_data->defaultSize; 555 578 556 579 return boundingRect().size(); … … 558 581 559 582 /*! 560 \brief Replay all recorded painter commands 561 \param painter Qt painter 562 */ 563 void QwtGraphic::render( QPainter *painter ) const 583 Find the height for a given width 584 585 The height is calculated using the aspect ratio of defaultSize(). 586 587 \param width Width 588 589 \return Calculated height 590 \sa defaultSize() 591 */ 592 qreal QwtGraphic::heightForWidth( qreal width ) const 593 { 594 const QSizeF sz = defaultSize(); 595 if ( sz.isEmpty() ) 596 return 0.0; 597 598 return sz.height() * width / sz.width(); 599 } 600 601 /*! 602 Find the width for a given height 603 604 The width is calculated using the aspect ratio of defaultSize(). 605 606 \param height Height 607 608 \return Calculated width 609 \sa defaultSize() 610 */ 611 qreal QwtGraphic::widthForHeight( qreal height ) const 612 { 613 const QSizeF sz = defaultSize(); 614 if ( sz.isEmpty() ) 615 return 0.0; 616 617 return sz.width() * height / sz.height(); 618 } 619 620 /*! 621 \brief Replay all recorded painter commands 622 \param painter Qt painter 623 */ 624 void QwtGraphic::render( QPainter* painter ) const 625 { 626 renderGraphic( painter, NULL ); 627 } 628 629 void QwtGraphic::renderGraphic( QPainter* painter, QTransform* initialTransform ) const 564 630 { 565 631 if ( isNull() ) 566 632 return; 567 633 568 const int numCommands = d_data->commands.size();569 const QwtPainterCommand *commands =d_data->commands.constData();634 const int numCommands = m_data->commands.size(); 635 const QwtPainterCommand* commands = m_data->commands.constData(); 570 636 571 637 const QTransform transform = painter->transform(); … … 576 642 { 577 643 qwtExecCommand( painter, commands[i], 578 d_data->renderHints, transform,d_data->initialTransform );644 m_data->renderHints, transform, initialTransform ); 579 645 } 580 646 … … 583 649 584 650 /*! 585 \brief Replay all recorded painter commands 586 587 The graphic is scaled to fit into the rectangle 588 of the given size starting at ( 0, 0 ). 589 590 \param painter Qt painter 591 \param size Size for the scaled graphic 592 \param aspectRatioMode Mode how to scale - See Qt::AspectRatioMode 593 */ 594 void QwtGraphic::render( QPainter *painter, const QSizeF&size,651 \brief Replay all recorded painter commands 652 653 The graphic is scaled to fit into the rectangle 654 of the given size starting at ( 0, 0 ). 655 656 \param painter Qt painter 657 \param size Size for the scaled graphic 658 \param aspectRatioMode Mode how to scale - See Qt::AspectRatioMode 659 */ 660 void QwtGraphic::render( QPainter* painter, const QSizeF& size, 595 661 Qt::AspectRatioMode aspectRatioMode ) const 596 662 { … … 600 666 601 667 /*! 602 \brief Replay all recorded painter commands 603 604 The graphic is scaled to fit into the given rectangle 605 606 \param painter Qt painter 607 \param rect Rectangle for the scaled graphic 608 \param aspectRatioMode Mode how to scale - See Qt::AspectRatioMode 609 */ 610 void QwtGraphic::render( QPainter *painter, const QRectF&rect,668 \brief Replay all recorded painter commands 669 670 The graphic is scaled to fit into the given rectangle 671 672 \param painter Qt painter 673 \param rect Rectangle for the scaled graphic 674 \param aspectRatioMode Mode how to scale - See Qt::AspectRatioMode 675 */ 676 void QwtGraphic::render( QPainter* painter, const QRectF& rect, 611 677 Qt::AspectRatioMode aspectRatioMode ) const 612 678 { … … 617 683 double sy = 1.0; 618 684 619 if ( d_data->pointRect.width() > 0.0 ) 620 sx = rect.width() / d_data->pointRect.width(); 621 622 if ( d_data->pointRect.height() > 0.0 ) 623 sy = rect.height() / d_data->pointRect.height(); 624 625 const bool scalePens = 626 !d_data->renderHints.testFlag( RenderPensUnscaled ); 627 628 for ( int i = 0; i < d_data->pathInfos.size(); i++ ) 629 { 630 const PathInfo info = d_data->pathInfos[i]; 685 if ( m_data->pointRect.width() > 0.0 ) 686 sx = rect.width() / m_data->pointRect.width(); 687 688 if ( m_data->pointRect.height() > 0.0 ) 689 sy = rect.height() / m_data->pointRect.height(); 690 691 const bool scalePens = !m_data->renderHints.testFlag( RenderPensUnscaled ); 692 693 for ( int i = 0; i < m_data->pathInfos.size(); i++ ) 694 { 695 const PathInfo& info = m_data->pathInfos[i]; 631 696 632 697 const double ssx = info.scaleFactorX( 633 d_data->pointRect, rect, scalePens );698 m_data->pointRect, rect, scalePens ); 634 699 635 700 if ( ssx > 0.0 ) 636 sx = q Min( sx, ssx );701 sx = qwtMinF( sx, ssx ); 637 702 638 703 const double ssy = info.scaleFactorY( 639 d_data->pointRect, rect, scalePens );704 m_data->pointRect, rect, scalePens ); 640 705 641 706 if ( ssy > 0.0 ) 642 sy = q Min( sy, ssy );707 sy = qwtMinF( sy, ssy ); 643 708 } 644 709 645 710 if ( aspectRatioMode == Qt::KeepAspectRatio ) 646 711 { 647 const double s = qMin( sx, sy );712 const qreal s = qwtMinF( sx, sy ); 648 713 sx = s; 649 714 sy = s; … … 651 716 else if ( aspectRatioMode == Qt::KeepAspectRatioByExpanding ) 652 717 { 653 const double s = qMax( sx, sy );718 const qreal s = qwtMaxF( sx, sy ); 654 719 sx = s; 655 720 sy = s; … … 657 722 658 723 QTransform tr; 659 tr.translate( rect.center().x() - 0.5 * sx * d_data->pointRect.width(),660 rect.center().y() - 0.5 * sy * d_data->pointRect.height() );724 tr.translate( rect.center().x() - 0.5 * sx * m_data->pointRect.width(), 725 rect.center().y() - 0.5 * sy * m_data->pointRect.height() ); 661 726 tr.scale( sx, sy ); 662 tr.translate( - d_data->pointRect.x(), -d_data->pointRect.y() );727 tr.translate( -m_data->pointRect.x(), -m_data->pointRect.y() ); 663 728 664 729 const QTransform transform = painter->transform(); 730 731 painter->setTransform( tr, true ); 732 665 733 if ( !scalePens && transform.isScaling() ) 666 734 { … … 669 737 // painter transformation later 670 738 671 d_data->initialTransform = new QTransform(); 672 d_data->initialTransform->scale( transform.m11(), transform.m22() ); 673 } 674 675 painter->setTransform( tr, true ); 676 render( painter ); 739 QTransform initialTransform; 740 initialTransform.scale( transform.m11(), transform.m22() ); 741 742 renderGraphic( painter, &initialTransform ); 743 } 744 else 745 { 746 renderGraphic( painter, NULL ); 747 } 677 748 678 749 painter->setTransform( transform ); 679 680 delete d_data->initialTransform; 681 d_data->initialTransform = NULL; 682 } 683 684 /*! 685 \brief Replay all recorded painter commands 686 687 The graphic is scaled to the defaultSize() and aligned 688 to a position. 689 690 \param painter Qt painter 691 \param pos Reference point, where to render 692 \param alignment Flags how to align the target rectangle 693 to pos. 694 */ 695 void QwtGraphic::render( QPainter *painter, 696 const QPointF &pos, Qt::Alignment alignment ) const 750 } 751 752 /*! 753 \brief Replay all recorded painter commands 754 755 The graphic is scaled to the defaultSize() and aligned 756 to a position. 757 758 \param painter Qt painter 759 \param pos Reference point, where to render 760 \param alignment Flags how to align the target rectangle to pos. 761 */ 762 void QwtGraphic::render( QPainter* painter, 763 const QPointF& pos, Qt::Alignment alignment ) const 697 764 { 698 765 QRectF r( pos, defaultSize() ); … … 728 795 729 796 /*! 730 \brief Convert the graphic to a QPixmap 731 732 All pixels of the pixmap get initialized by Qt::transparent 733 before the graphic is scaled and rendered on it. 734 735 The size of the pixmap is the default size ( ceiled to integers ) 736 of the graphic. 737 738 \return The graphic as pixmap in default size 739 \sa defaultSize(), toImage(), render() 740 */ 741 QPixmap QwtGraphic::toPixmap() const 797 \brief Convert the graphic to a QPixmap 798 799 All pixels of the pixmap get initialized by Qt::transparent 800 before the graphic is scaled and rendered on it. 801 802 The size of the pixmap is the default size ( ceiled to integers ) 803 of the graphic. 804 805 \param devicePixelRatio Device pixel ratio for the pixmap. 806 If devicePixelRatio <= 0.0 the pixmap 807 is initialized with the system default. 808 809 \return The graphic as pixmap in default size 810 \sa defaultSize(), toImage(), render() 811 */ 812 QPixmap QwtGraphic::toPixmap( qreal devicePixelRatio ) const 742 813 { 743 814 if ( isNull() ) … … 746 817 const QSizeF sz = defaultSize(); 747 818 748 const int w = qCeil( sz.width() ); 749 const int h = qCeil( sz.height() ); 819 #if QT_VERSION >= 0x050000 820 if ( devicePixelRatio <= 0.0 ) 821 devicePixelRatio = qwtDevicePixelRatio(); 822 823 const int w = qwtCeil( sz.width() * devicePixelRatio ); 824 const int h = qwtCeil( sz.height() * devicePixelRatio ); 750 825 751 826 QPixmap pixmap( w, h ); 827 pixmap.setDevicePixelRatio( devicePixelRatio ); 828 #else 829 Q_UNUSED( devicePixelRatio ) 830 831 const int w = qwtCeil( sz.width() ); 832 const int h = qwtCeil( sz.height() ); 833 834 QPixmap pixmap( w, h ); 835 #endif 836 752 837 pixmap.fill( Qt::transparent ); 753 838 … … 762 847 763 848 /*! 764 \brief Convert the graphic to a QPixmap 765 766 All pixels of the pixmap get initialized by Qt::transparent 767 before the graphic is scaled and rendered on it. 768 769 \param size Size of the image 770 \param aspectRatioMode Aspect ratio how to scale the graphic 771 772 \return The graphic as pixmap 773 \sa toImage(), render() 774 */ 775 QPixmap QwtGraphic::toPixmap( const QSize &size, 776 Qt::AspectRatioMode aspectRatioMode ) const 777 { 849 \brief Convert the graphic to a QPixmap 850 851 All pixels of the pixmap get initialized by Qt::transparent 852 before the graphic is scaled and rendered on it. 853 854 \param size Size of the image 855 \param aspectRatioMode Aspect ratio how to scale the graphic 856 \param devicePixelRatio Device pixel ratio for the pixmap. 857 If devicePixelRatio <= 0.0 the pixmap 858 is initialized with the system default. 859 860 \return The graphic as pixmap 861 \sa toImage(), render() 862 */ 863 QPixmap QwtGraphic::toPixmap( const QSize& size, 864 Qt::AspectRatioMode aspectRatioMode, qreal devicePixelRatio ) const 865 { 866 #if QT_VERSION >= 0x050000 867 if ( devicePixelRatio <= 0.0 ) 868 devicePixelRatio = qwtDevicePixelRatio(); 869 870 const int w = qwtCeil( size.width() * devicePixelRatio ); 871 const int h = qwtCeil( size.height() * devicePixelRatio ); 872 873 QPixmap pixmap( w, h ); 874 pixmap.setDevicePixelRatio( devicePixelRatio ); 875 #else 876 Q_UNUSED( devicePixelRatio ) 778 877 QPixmap pixmap( size ); 878 #endif 779 879 pixmap.fill( Qt::transparent ); 780 880 … … 789 889 790 890 /*! 791 \brief Convert the graphic to a QImage 792 793 All pixels of the image get initialized by 0 ( transparent ) 794 before the graphic is scaled and rendered on it. 795 796 The format of the image is QImage::Format_ARGB32_Premultiplied. 797 798 \param size Size of the image 799 \param aspectRatioMode Aspect ratio how to scale the graphic 800 801 \return The graphic as image 802 \sa toPixmap(), render() 803 */ 804 QImage QwtGraphic::toImage( const QSize &size, 805 Qt::AspectRatioMode aspectRatioMode ) const 806 { 891 \brief Convert the graphic to a QImage 892 893 All pixels of the image get initialized by 0 ( transparent ) 894 before the graphic is scaled and rendered on it. 895 896 The format of the image is QImage::Format_ARGB32_Premultiplied. 897 898 \param size Size of the image ( will be multiplied by the devicePixelRatio ) 899 \param aspectRatioMode Aspect ratio how to scale the graphic 900 \param devicePixelRatio Device pixel ratio for the image. 901 If devicePixelRatio <= 0.0 the pixmap 902 is initialized with the system default. 903 904 \return The graphic as image 905 \sa toPixmap(), render() 906 */ 907 QImage QwtGraphic::toImage( const QSize& size, 908 Qt::AspectRatioMode aspectRatioMode, qreal devicePixelRatio ) const 909 { 910 #if QT_VERSION >= 0x050000 911 if ( devicePixelRatio <= 0.0 ) 912 devicePixelRatio = qwtDevicePixelRatio(); 913 914 const int w = qwtCeil( size.width() * devicePixelRatio ); 915 const int h = qwtCeil( size.height() * devicePixelRatio ); 916 917 QImage image( w, h, QImage::Format_ARGB32_Premultiplied ); 918 image.setDevicePixelRatio( devicePixelRatio ); 919 #else 920 Q_UNUSED( devicePixelRatio ) 807 921 QImage image( size, QImage::Format_ARGB32_Premultiplied ); 922 #endif 923 808 924 image.fill( 0 ); 809 925 … … 818 934 819 935 /*! 820 \brief Convert the graphic to a QImage 821 822 All pixels of the image get initialized by 0 ( transparent ) 823 before the graphic is scaled and rendered on it. 824 825 The format of the image is QImage::Format_ARGB32_Premultiplied. 826 827 The size of the image is the default size ( ceiled to integers ) 828 of the graphic. 829 830 \return The graphic as image in default size 831 \sa defaultSize(), toPixmap(), render() 832 */ 833 QImage QwtGraphic::toImage() const 936 \brief Convert the graphic to a QImage 937 938 All pixels of the image get initialized by 0 ( transparent ) 939 before the graphic is scaled and rendered on it. 940 941 The format of the image is QImage::Format_ARGB32_Premultiplied. 942 943 The size of the image is the default size ( ceiled to integers ) 944 of the graphic multiplied by the devicePixelRatio. 945 946 \param devicePixelRatio Device pixel ratio for the image. 947 If devicePixelRatio <= 0.0 the pixmap 948 is initialized with the system default. 949 950 \return The graphic as image in default size 951 \sa defaultSize(), toPixmap(), render() 952 */ 953 QImage QwtGraphic::toImage( qreal devicePixelRatio ) const 834 954 { 835 955 if ( isNull() ) … … 838 958 const QSizeF sz = defaultSize(); 839 959 840 const int w = qCeil( sz.width() ); 841 const int h = qCeil( sz.height() ); 960 #if QT_VERSION >= 0x050000 961 if ( devicePixelRatio <= 0.0 ) 962 devicePixelRatio = qwtDevicePixelRatio(); 963 964 const int w = qwtCeil( sz.width() * devicePixelRatio ); 965 const int h = qwtCeil( sz.height() * devicePixelRatio ); 842 966 843 967 QImage image( w, h, QImage::Format_ARGB32 ); 968 image.setDevicePixelRatio( devicePixelRatio ); 969 #else 970 Q_UNUSED( devicePixelRatio ) 971 972 const int w = qwtCeil( sz.width() ); 973 const int h = qwtCeil( sz.height() ); 974 975 QImage image( w, h, QImage::Format_ARGB32 ); 976 #endif 977 844 978 image.fill( 0 ); 845 979 … … 854 988 855 989 /*! 856 Store a path command in the command list 857 858 \param path Painter path 859 \sa QPaintEngine::drawPath() 860 */ 861 void QwtGraphic::drawPath( const QPainterPath &path )862 { 863 const QPainter *painter = paintEngine()->painter();990 Store a path command in the command list 991 992 \param path Painter path 993 \sa QPaintEngine::drawPath() 994 */ 995 void QwtGraphic::drawPath( const QPainterPath& path ) 996 { 997 const QPainter* painter = paintEngine()->painter(); 864 998 if ( painter == NULL ) 865 999 return; 866 1000 867 d_data->commands += QwtPainterCommand( path ); 1001 m_data->commands += QwtPainterCommand( path ); 1002 m_data->commandTypes |= QwtGraphic::VectorData; 868 1003 869 1004 if ( !path.isEmpty() ) … … 883 1018 updateBoundingRect( boundingRect ); 884 1019 885 d_data->pathInfos += PathInfo( pointRect,1020 m_data->pathInfos += PathInfo( pointRect, 886 1021 boundingRect, qwtHasScalablePen( painter ) ); 887 1022 } … … 889 1024 890 1025 /*! 891 \brief Store a pixmap command in the command list 892 893 \param rect target rectangle 894 \param pixmap Pixmap to be painted 895 \param subRect Reactangle of the pixmap to be painted 896 897 \sa QPaintEngine::drawPixmap() 898 */ 899 void QwtGraphic::drawPixmap( const QRectF &rect,900 const QPixmap &pixmap, const QRectF&subRect )901 { 902 const QPainter *painter = paintEngine()->painter();1026 \brief Store a pixmap command in the command list 1027 1028 \param rect target rectangle 1029 \param pixmap Pixmap to be painted 1030 \param subRect Reactangle of the pixmap to be painted 1031 1032 \sa QPaintEngine::drawPixmap() 1033 */ 1034 void QwtGraphic::drawPixmap( const QRectF& rect, 1035 const QPixmap& pixmap, const QRectF& subRect ) 1036 { 1037 const QPainter* painter = paintEngine()->painter(); 903 1038 if ( painter == NULL ) 904 1039 return; 905 1040 906 d_data->commands += QwtPainterCommand( rect, pixmap, subRect ); 1041 m_data->commands += QwtPainterCommand( rect, pixmap, subRect ); 1042 m_data->commandTypes |= QwtGraphic::RasterData; 907 1043 908 1044 const QRectF r = painter->transform().mapRect( rect ); … … 912 1048 913 1049 /*! 914 \brief Store a image command in the command list 915 916 \param rect t raget rectangle917 \param image Image to be painted 918 \param subRect Reactangle of the pixmap to be painted 919 \param flags Image conversion flags 920 921 \sa QPaintEngine::drawImage() 922 */ 923 void QwtGraphic::drawImage( const QRectF &rect, const QImage&image,924 const QRectF &subRect, Qt::ImageConversionFlags flags)925 { 926 const QPainter *painter = paintEngine()->painter();1050 \brief Store a image command in the command list 1051 1052 \param rect target rectangle 1053 \param image Image to be painted 1054 \param subRect Reactangle of the pixmap to be painted 1055 \param flags Image conversion flags 1056 1057 \sa QPaintEngine::drawImage() 1058 */ 1059 void QwtGraphic::drawImage( const QRectF& rect, const QImage& image, 1060 const QRectF& subRect, Qt::ImageConversionFlags flags ) 1061 { 1062 const QPainter* painter = paintEngine()->painter(); 927 1063 if ( painter == NULL ) 928 1064 return; 929 1065 930 d_data->commands += QwtPainterCommand( rect, image, subRect, flags ); 1066 m_data->commands += QwtPainterCommand( rect, image, subRect, flags ); 1067 m_data->commandTypes |= QwtGraphic::RasterData; 931 1068 932 1069 const QRectF r = painter->transform().mapRect( rect ); … … 937 1074 938 1075 /*! 939 \brief Store a state command in the command list 940 941 \param state State to be stored 942 \sa QPaintEngine::updateState() 943 */ 944 void QwtGraphic::updateState( const QPaintEngineState &state) 945 { 946 d_data->commands += QwtPainterCommand( state ); 947 } 948 949 void QwtGraphic::updateBoundingRect( const QRectF &rect ) 1076 \brief Store a state command in the command list 1077 1078 \param state State to be stored 1079 \sa QPaintEngine::updateState() 1080 */ 1081 void QwtGraphic::updateState( const QPaintEngineState& state ) 1082 { 1083 m_data->commands += QwtPainterCommand( state ); 1084 1085 if ( state.state() & QPaintEngine::DirtyTransform ) 1086 { 1087 if ( !( m_data->commandTypes & QwtGraphic::Transformation ) ) 1088 { 1089 /* 1090 QTransform::isScaling() returns true for all type 1091 of transformations beside simple translations 1092 even if it is f.e a rotation 1093 */ 1094 if ( state.transform().isScaling() ) 1095 m_data->commandTypes |= QwtGraphic::Transformation; 1096 } 1097 } 1098 } 1099 1100 void QwtGraphic::updateBoundingRect( const QRectF& rect ) 950 1101 { 951 1102 QRectF br = rect; 952 1103 953 const QPainter *painter = paintEngine()->painter();1104 const QPainter* painter = paintEngine()->painter(); 954 1105 if ( painter && painter->hasClipping() ) 955 1106 { … … 960 1111 } 961 1112 962 if ( d_data->boundingRect.width() < 0 )963 d_data->boundingRect = br;1113 if ( m_data->boundingRect.width() < 0 ) 1114 m_data->boundingRect = br; 964 1115 else 965 d_data->boundingRect |= br;966 } 967 968 void QwtGraphic::updateControlPointRect( const QRectF &rect )969 { 970 if ( d_data->pointRect.width() < 0.0 )971 d_data->pointRect = rect;1116 m_data->boundingRect |= br; 1117 } 1118 1119 void QwtGraphic::updateControlPointRect( const QRectF& rect ) 1120 { 1121 if ( m_data->pointRect.width() < 0.0 ) 1122 m_data->pointRect = rect; 972 1123 else 973 d_data->pointRect |= rect;974 } 975 976 /*! 977 \return List of recorded paint commands 978 \sa setCommands() 979 */ 980 const QVector< QwtPainterCommand > &QwtGraphic::commands() const981 { 982 return d_data->commands;983 } 984 985 /*! 986 \brief Append paint commands 987 988 \param commands Paint commands 989 \sa commands() 990 */ 991 void QwtGraphic::setCommands( QVector< QwtPainterCommand > &commands )1124 m_data->pointRect |= rect; 1125 } 1126 1127 /*! 1128 \return List of recorded paint commands 1129 \sa setCommands() 1130 */ 1131 const QVector< QwtPainterCommand >& QwtGraphic::commands() const 1132 { 1133 return m_data->commands; 1134 } 1135 1136 /*! 1137 \brief Append paint commands 1138 1139 \param commands Paint commands 1140 \sa commands() 1141 */ 1142 void QwtGraphic::setCommands( const QVector< QwtPainterCommand >& commands ) 992 1143 { 993 1144 reset(); … … 1000 1151 // the commands. 1001 1152 1002 const QwtPainterCommand *cmds = commands.constData(); 1153 const QwtPainterCommand* cmds = commands.constData(); 1154 1155 const QTransform noTransform; 1156 const RenderHints noRenderHints; 1003 1157 1004 1158 QPainter painter( this ); 1005 1159 for ( int i = 0; i < numCommands; i++ ) 1006 qwtExecCommand( &painter, cmds[i], RenderHints (), QTransform(), NULL );1160 qwtExecCommand( &painter, cmds[i], noRenderHints, noTransform, NULL ); 1007 1161 1008 1162 painter.end();
Note:
See TracChangeset
for help on using the changeset viewer.
