Changeset 11158 in ntrip for trunk/BNC/qwt/qwt_plot_legenditem.cpp
- Timestamp:
- Nov 3, 2026, 10:53:35 AM (3 weeks ago)
- File:
-
- 1 edited
-
trunk/BNC/qwt/qwt_plot_legenditem.cpp (modified) (24 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/BNC/qwt/qwt_plot_legenditem.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_legenditem.h" 11 11 #include "qwt_dyngrid_layout.h" 12 #include "qwt_scale_map.h" 13 #include "qwt_painter.h" 12 #include "qwt_text.h" 13 #include "qwt_graphic.h" 14 #include "qwt_legend_data.h" 15 #include "qwt_math.h" 16 14 17 #include <qlayoutitem.h> 15 18 #include <qpen.h> 16 19 #include <qbrush.h> 17 20 #include <qpainter.h> 18 #include <qmath.h> 19 20 class QwtLegendLayoutItem: public QLayoutItem 21 { 22 public: 23 QwtLegendLayoutItem( const QwtPlotLegendItem *, const QwtPlotItem * ); 24 virtual ~QwtLegendLayoutItem(); 25 26 const QwtPlotItem *plotItem() const; 27 28 void setData( const QwtLegendData & ); 29 const QwtLegendData &data() const; 30 31 virtual Qt::Orientations expandingDirections() const; 32 virtual QRect geometry() const; 33 virtual bool hasHeightForWidth() const; 34 virtual int heightForWidth( int w ) const; 35 virtual bool isEmpty() const; 36 virtual QSize maximumSize() const; 37 virtual int minimumHeightForWidth( int w ) const; 38 virtual QSize minimumSize() const; 39 virtual void setGeometry( const QRect & r ); 40 virtual QSize sizeHint() const; 41 42 private: 43 44 const QwtPlotLegendItem *d_legendItem; 45 const QwtPlotItem *d_plotItem; 46 QwtLegendData d_data; 47 48 QRect d_rect; 49 }; 50 51 QwtLegendLayoutItem::QwtLegendLayoutItem( 52 const QwtPlotLegendItem *legendItem, const QwtPlotItem *plotItem ): 53 d_legendItem( legendItem ), 54 d_plotItem( plotItem) 55 { 56 } 57 58 QwtLegendLayoutItem::~QwtLegendLayoutItem() 59 { 60 } 61 62 const QwtPlotItem *QwtLegendLayoutItem::plotItem() const 63 { 64 return d_plotItem; 65 } 66 67 void QwtLegendLayoutItem::setData( const QwtLegendData &data ) 68 { 69 d_data = data; 70 } 71 72 const QwtLegendData &QwtLegendLayoutItem::data() const 73 { 74 return d_data; 75 } 76 77 Qt::Orientations QwtLegendLayoutItem::expandingDirections() const 78 { 79 return Qt::Horizontal; 80 } 81 82 bool QwtLegendLayoutItem::hasHeightForWidth() const 83 { 84 return !d_data.title().isEmpty(); 85 } 86 87 int QwtLegendLayoutItem::minimumHeightForWidth( int w ) const 88 { 89 return d_legendItem->heightForWidth( d_data, w ); 90 } 91 92 int QwtLegendLayoutItem::heightForWidth( int w ) const 93 { 94 return d_legendItem->heightForWidth( d_data, w ); 95 } 96 97 bool QwtLegendLayoutItem::isEmpty() const 98 { 99 return false; 100 } 101 102 QSize QwtLegendLayoutItem::maximumSize() const 103 { 104 return QSize( QLAYOUTSIZE_MAX, QLAYOUTSIZE_MAX ); 105 } 106 107 QSize QwtLegendLayoutItem::minimumSize() const 108 { 109 return d_legendItem->minimumSize( d_data ); 110 } 111 112 QSize QwtLegendLayoutItem::sizeHint() const 113 { 114 return minimumSize(); 115 } 116 117 void QwtLegendLayoutItem::setGeometry( const QRect &rect ) 118 { 119 d_rect = rect; 120 } 121 122 QRect QwtLegendLayoutItem::geometry() const 123 { 124 return d_rect; 21 22 namespace 23 { 24 class LayoutItem QWT_FINAL : public QLayoutItem 25 { 26 public: 27 LayoutItem( const QwtPlotLegendItem*, const QwtPlotItem* ); 28 virtual ~LayoutItem(); 29 30 const QwtPlotItem* plotItem() const; 31 32 void setData( const QwtLegendData& ); 33 const QwtLegendData& data() const; 34 35 virtual Qt::Orientations expandingDirections() const QWT_OVERRIDE; 36 virtual QRect geometry() const QWT_OVERRIDE; 37 virtual bool hasHeightForWidth() const QWT_OVERRIDE; 38 virtual int heightForWidth( int ) const QWT_OVERRIDE; 39 virtual bool isEmpty() const QWT_OVERRIDE; 40 virtual QSize maximumSize() const QWT_OVERRIDE; 41 virtual int minimumHeightForWidth( int ) const QWT_OVERRIDE; 42 virtual QSize minimumSize() const QWT_OVERRIDE; 43 virtual void setGeometry( const QRect& ) QWT_OVERRIDE; 44 virtual QSize sizeHint() const QWT_OVERRIDE; 45 46 private: 47 48 const QwtPlotLegendItem* m_legendItem; 49 const QwtPlotItem* m_plotItem; 50 QwtLegendData m_data; 51 52 QRect m_rect; 53 }; 54 55 LayoutItem::LayoutItem( 56 const QwtPlotLegendItem* legendItem, const QwtPlotItem* plotItem ) 57 : m_legendItem( legendItem ) 58 , m_plotItem( plotItem) 59 { 60 } 61 62 LayoutItem::~LayoutItem() 63 { 64 } 65 66 const QwtPlotItem* LayoutItem::plotItem() const 67 { 68 return m_plotItem; 69 } 70 71 void LayoutItem::setData( const QwtLegendData& data ) 72 { 73 m_data = data; 74 } 75 76 const QwtLegendData& LayoutItem::data() const 77 { 78 return m_data; 79 } 80 81 Qt::Orientations LayoutItem::expandingDirections() const 82 { 83 return Qt::Horizontal; 84 } 85 86 bool LayoutItem::hasHeightForWidth() const 87 { 88 return !m_data.title().isEmpty(); 89 } 90 91 int LayoutItem::minimumHeightForWidth( int w ) const 92 { 93 return m_legendItem->heightForWidth( m_data, w ); 94 } 95 96 int LayoutItem::heightForWidth( int w ) const 97 { 98 return m_legendItem->heightForWidth( m_data, w ); 99 } 100 101 bool LayoutItem::isEmpty() const 102 { 103 return false; 104 } 105 106 QSize LayoutItem::maximumSize() const 107 { 108 return QSize( QLAYOUTSIZE_MAX, QLAYOUTSIZE_MAX ); 109 } 110 111 QSize LayoutItem::minimumSize() const 112 { 113 return m_legendItem->minimumSize( m_data ); 114 } 115 116 QSize LayoutItem::sizeHint() const 117 { 118 return minimumSize(); 119 } 120 121 void LayoutItem::setGeometry( const QRect& rect ) 122 { 123 m_rect = rect; 124 } 125 126 QRect LayoutItem::geometry() const 127 { 128 return m_rect; 129 } 125 130 } 126 131 127 132 class QwtPlotLegendItem::PrivateData 128 133 { 129 public: 130 PrivateData() :131 itemMargin( 4 ) ,132 itemSpacing( 4 ) ,133 borderRadius( 0.0 ) ,134 borderPen( Qt::NoPen ) ,135 backgroundBrush( Qt::NoBrush ) ,136 backgroundMode( QwtPlotLegendItem::LegendBackground ) ,137 borderDistance( 10 ),138 alignment( Qt::AlignRight | Qt::AlignBottom )139 {134 public: 135 PrivateData() 136 : itemMargin( 4 ) 137 , itemSpacing( 4 ) 138 , borderRadius( 0.0 ) 139 , borderPen( Qt::NoPen ) 140 , backgroundBrush( Qt::NoBrush ) 141 , backgroundMode( QwtPlotLegendItem::LegendBackground ) 142 , canvasAlignment( Qt::AlignRight | Qt::AlignBottom ) 143 { 144 canvasOffset[ 0 ] = canvasOffset[1] = 10; 140 145 layout = new QwtDynGridLayout(); 141 146 layout->setMaxColumns( 2 ); … … 160 165 QwtPlotLegendItem::BackgroundMode backgroundMode; 161 166 162 int borderDistance;163 Qt::Alignment alignment;164 165 QMap< const QwtPlotItem *, QList<QwtLegendLayoutItem*> > map;166 QwtDynGridLayout *layout;167 int canvasOffset[2]; 168 Qt::Alignment canvasAlignment; 169 170 QMap< const QwtPlotItem*, QList< LayoutItem* > > map; 171 QwtDynGridLayout* layout; 167 172 }; 168 173 169 174 //! Constructor 170 QwtPlotLegendItem::QwtPlotLegendItem() :171 QwtPlotItem( QwtText( "Legend" ) ) 172 { 173 d_data = new PrivateData;175 QwtPlotLegendItem::QwtPlotLegendItem() 176 : QwtPlotItem( QwtText( "Legend" ) ) 177 { 178 m_data = new PrivateData; 174 179 175 180 setItemInterest( QwtPlotItem::LegendInterest, true ); … … 181 186 { 182 187 clearLegend(); 183 delete d_data;188 delete m_data; 184 189 } 185 190 … … 191 196 192 197 /*! 193 \brief Set the alignmnet 194 195 Alignment means the position of the legend relative 196 to the geometry of the plot canvas. 197 198 \param alignment Alignment flags 199 200 \sa alignment(), setMaxColumns() 201 202 \note To align a legend with many items horizontally 198 \brief Set the alignmnet 199 200 Alignment means the position of the legend relative 201 to the geometry of the plot canvas. 202 203 \param alignment Alignment flags 204 205 \sa alignmentInCanvas(), setMaxColumns() 206 207 \note To align a legend with many items horizontally 203 208 the number of columns need to be limited 204 209 */ 205 void QwtPlotLegendItem::setAlignment( Qt::Alignment alignment ) 206 { 207 if ( d_data->alignment != alignment )208 { 209 d_data->alignment = alignment;210 itemChanged(); 211 } 212 } 213 214 /*! 215 \return Alignment flags 216 \sa setAlignment() 217 */ 218 Qt::Alignment QwtPlotLegendItem::alignment() const 219 { 220 return d_data->alignment;221 } 222 223 /*! 224 \brief Limit the number of columns 225 226 When aligning the legend horizontally ( Qt::AlignLeft, Qt::AlignRight ) 227 the number of columns needs to be limited to avoid, that 228 the width of the legend grows with an increasing number of entries. 229 230 \param maxColumns Maximum number of columns. 0 means unlimited. 231 \sa maxColumns(), QwtDynGridLayout::setMaxColumns() 210 void QwtPlotLegendItem::setAlignmentInCanvas( Qt::Alignment alignment ) 211 { 212 if ( m_data->canvasAlignment != alignment ) 213 { 214 m_data->canvasAlignment = alignment; 215 itemChanged(); 216 } 217 } 218 219 /*! 220 \return Alignment flags 221 \sa setAlignmentInCanvas() 222 */ 223 Qt::Alignment QwtPlotLegendItem::alignmentInCanvas() const 224 { 225 return m_data->canvasAlignment; 226 } 227 228 /*! 229 \brief Limit the number of columns 230 231 When aligning the legend horizontally ( Qt::AlignLeft, Qt::AlignRight ) 232 the number of columns needs to be limited to avoid, that 233 the width of the legend grows with an increasing number of entries. 234 235 \param maxColumns Maximum number of columns. 0 means unlimited. 236 \sa maxColumns(), QwtDynGridLayout::setMaxColumns() 232 237 */ 233 238 void QwtPlotLegendItem::setMaxColumns( uint maxColumns ) 234 239 { 235 if ( maxColumns != d_data->layout->maxColumns() )236 { 237 d_data->layout->setMaxColumns( maxColumns );238 itemChanged(); 239 } 240 } 241 242 /*! 243 \return Maximum number of columns 244 \sa maxColumns(), QwtDynGridLayout::maxColumns() 240 if ( maxColumns != m_data->layout->maxColumns() ) 241 { 242 m_data->layout->setMaxColumns( maxColumns ); 243 itemChanged(); 244 } 245 } 246 247 /*! 248 \return Maximum number of columns 249 \sa maxColumns(), QwtDynGridLayout::maxColumns() 245 250 */ 246 251 uint QwtPlotLegendItem::maxColumns() const 247 252 { 248 return d_data->layout->maxColumns();249 } 250 251 /*! 252 \brief Set the margin around legend items 253 254 The default setting for the margin is 0. 255 256 \param margin Margin in pixels 257 \sa margin(), setSpacing(), setItemMargin(), setItemSpacing 253 return m_data->layout->maxColumns(); 254 } 255 256 /*! 257 \brief Set the margin around legend items 258 259 The default setting for the margin is 0. 260 261 \param margin Margin in pixels 262 \sa margin(), setSpacing(), setItemMargin(), setItemSpacing 258 263 */ 259 264 void QwtPlotLegendItem::setMargin( int margin ) … … 262 267 if ( margin != this->margin() ) 263 268 { 264 d_data->layout->setContentsMargins(269 m_data->layout->setContentsMargins( 265 270 margin, margin, margin, margin ); 266 271 … … 270 275 271 276 /*! 272 \return Margin around the legend items 273 \sa setMargin(), spacing(), itemMargin(), itemSpacing() 277 \return Margin around the legend items 278 \sa setMargin(), spacing(), itemMargin(), itemSpacing() 274 279 */ 275 280 int QwtPlotLegendItem::margin() const 276 281 { 277 282 int left; 278 d_data->layout->getContentsMargins( &left, NULL, NULL, NULL );283 m_data->layout->getContentsMargins( &left, NULL, NULL, NULL ); 279 284 280 285 return left; … … 282 287 283 288 /*! 284 \brief Set the spacing between the legend items 285 286 \param spacing Spacing in pixels 287 \sa spacing(), setMargin() 288 */ 289 \brief Set the spacing between the legend items 290 291 \param spacing Spacing in pixels 292 \sa spacing(), setMargin() 293 */ 289 294 void QwtPlotLegendItem::setSpacing( int spacing ) 290 295 { 291 296 spacing = qMax( spacing, 0 ); 292 if ( spacing != d_data->layout->spacing() )293 { 294 d_data->layout->setSpacing( spacing );295 itemChanged(); 296 } 297 } 298 299 /*! 300 \return Spacing between the legend items 301 \sa setSpacing(), margin(), itemSpacing(), itemMargin() 297 if ( spacing != m_data->layout->spacing() ) 298 { 299 m_data->layout->setSpacing( spacing ); 300 itemChanged(); 301 } 302 } 303 304 /*! 305 \return Spacing between the legend items 306 \sa setSpacing(), margin(), itemSpacing(), itemMargin() 302 307 */ 303 308 int QwtPlotLegendItem::spacing() const 304 309 { 305 return d_data->layout->spacing();306 } 307 308 /*! 309 Set the margin around each item 310 311 \param margin Margin 312 \sa itemMargin(), setItemSpacing(), setMargin(), setSpacing() 310 return m_data->layout->spacing(); 311 } 312 313 /*! 314 Set the margin around each item 315 316 \param margin Margin 317 \sa itemMargin(), setItemSpacing(), setMargin(), setSpacing() 313 318 */ 314 319 void QwtPlotLegendItem::setItemMargin( int margin ) 315 320 { 316 321 margin = qMax( margin, 0 ); 317 if ( margin != d_data->itemMargin )318 { 319 d_data->itemMargin = margin;320 321 d_data->layout->invalidate();322 itemChanged(); 323 } 324 } 325 326 /*! 327 \return Margin around each item 328 \sa setItemMargin(), itemSpacing(), margin(), spacing() 329 */ 322 if ( margin != m_data->itemMargin ) 323 { 324 m_data->itemMargin = margin; 325 326 m_data->layout->invalidate(); 327 itemChanged(); 328 } 329 } 330 331 /*! 332 \return Margin around each item 333 \sa setItemMargin(), itemSpacing(), margin(), spacing() 334 */ 330 335 int QwtPlotLegendItem::itemMargin() const 331 336 { 332 return d_data->itemMargin;333 } 334 335 /*! 336 Set the spacing inside of each item 337 338 \param spacing Spacing 339 \sa itemSpacing(), setItemMargin(), setMargin(), setSpacing() 337 return m_data->itemMargin; 338 } 339 340 /*! 341 Set the spacing inside of each item 342 343 \param spacing Spacing 344 \sa itemSpacing(), setItemMargin(), setMargin(), setSpacing() 340 345 */ 341 346 void QwtPlotLegendItem::setItemSpacing( int spacing ) 342 347 { 343 348 spacing = qMax( spacing, 0 ); 344 if ( spacing != d_data->itemSpacing )345 { 346 d_data->itemSpacing = spacing;347 348 d_data->layout->invalidate();349 itemChanged(); 350 } 351 352 } 353 354 /*! 355 \return Spacing inside of each item 356 \sa setItemSpacing(), itemMargin(), margin(), spacing() 357 */ 349 if ( spacing != m_data->itemSpacing ) 350 { 351 m_data->itemSpacing = spacing; 352 353 m_data->layout->invalidate(); 354 itemChanged(); 355 } 356 357 } 358 359 /*! 360 \return Spacing inside of each item 361 \sa setItemSpacing(), itemMargin(), margin(), spacing() 362 */ 358 363 int QwtPlotLegendItem::itemSpacing() const 359 364 { 360 return d_data->itemSpacing;365 return m_data->itemSpacing; 361 366 } 362 367 … … 366 371 \param font Legend font 367 372 \sa font() 368 */ 369 void QwtPlotLegendItem::setFont( const QFont &font )370 { 371 if ( font != d_data->font )372 { 373 d_data->font = font;374 375 d_data->layout->invalidate();373 */ 374 void QwtPlotLegendItem::setFont( const QFont& font ) 375 { 376 if ( font != m_data->font ) 377 { 378 m_data->font = font; 379 380 m_data->layout->invalidate(); 376 381 itemChanged(); 377 382 } … … 381 386 \return Font used for drawing the text label 382 387 \sa setFont() 383 */ 388 */ 384 389 QFont QwtPlotLegendItem::font() const 385 390 { 386 return d_data->font; 387 } 388 389 /*! 390 \brief Set the margin between the legend and the canvas border 391 392 The default setting for the margin is 10 pixels. 393 394 \param distance Margin in pixels 395 \sa setMargin() 396 */ 397 void QwtPlotLegendItem::setBorderDistance( int distance ) 398 { 399 if ( distance < 0 ) 400 distance = -1; 401 402 if ( distance != d_data->borderDistance ) 403 { 404 d_data->borderDistance = distance; 405 itemChanged(); 406 } 407 } 408 409 /*! 410 \return Margin between the legend and the canvas border 411 \sa margin() 412 */ 413 int QwtPlotLegendItem::borderDistance() const 414 { 415 return d_data->borderDistance; 416 } 417 418 /*! 419 Set the radius for the border 420 421 \param radius A value <= 0 defines a rectangular border 422 \sa borderRadius(), setBorderPen() 391 return m_data->font; 392 } 393 394 /*! 395 \brief Set the distance between the legend and the canvas border 396 397 The default setting is 10 pixels. 398 399 \param orientations Qt::Horizontal is for the left/right, 400 Qt::Vertical for the top/bottom offset. 401 402 \param numPixels Distance in pixels 403 \sa setMargin() 404 */ 405 void QwtPlotLegendItem::setOffsetInCanvas( 406 Qt::Orientations orientations, int numPixels ) 407 { 408 if ( numPixels < 0 ) 409 numPixels = -1; 410 411 bool isChanged = false; 412 413 int* offset = m_data->canvasOffset; 414 415 if ( orientations & Qt::Horizontal ) 416 { 417 if ( numPixels != offset[0] ) 418 { 419 offset[0] = numPixels; 420 isChanged = true; 421 } 422 } 423 424 if ( orientations & Qt::Vertical ) 425 { 426 if ( numPixels != offset[1] ) 427 { 428 offset[1] = numPixels; 429 isChanged = true; 430 } 431 } 432 433 if ( isChanged ) 434 itemChanged(); 435 } 436 437 /*! 438 \param orientation Qt::Horizontal is for the left/right, 439 Qt::Vertical for the top/bottom padding. 440 441 \return Distance between the legend and the canvas border 442 \sa setOffsetInCanvas() 443 */ 444 int QwtPlotLegendItem::offsetInCanvas( 445 Qt::Orientation orientation ) const 446 { 447 const int index = ( orientation == Qt::Vertical ) ? 1 : 0; 448 return m_data->canvasOffset[index]; 449 } 450 451 /*! 452 Set the radius for the border 453 454 \param radius A value <= 0 defines a rectangular border 455 \sa borderRadius(), setBorderPen() 423 456 */ 424 457 void QwtPlotLegendItem::setBorderRadius( double radius ) 425 458 { 426 radius = q Max( 0.0, radius );427 428 if ( radius != d_data->borderRadius )429 { 430 d_data->borderRadius = radius;431 itemChanged(); 432 } 433 } 434 435 /*! 436 \return Radius of the border 437 \sa setBorderRadius(), setBorderPen() 459 radius = qwtMaxF( 0.0, radius ); 460 461 if ( radius != m_data->borderRadius ) 462 { 463 m_data->borderRadius = radius; 464 itemChanged(); 465 } 466 } 467 468 /*! 469 \return Radius of the border 470 \sa setBorderRadius(), setBorderPen() 438 471 */ 439 472 double QwtPlotLegendItem::borderRadius() const 440 473 { 441 return d_data->borderRadius;442 } 443 444 /*! 445 Set the pen for drawing the border 446 447 \param pen Border pen 448 \sa borderPen(), setBackgroundBrush() 449 */ 450 void QwtPlotLegendItem::setBorderPen( const QPen &pen )451 { 452 if ( d_data->borderPen != pen )453 { 454 d_data->borderPen = pen;455 itemChanged(); 456 } 457 } 458 459 /*! 460 \return Pen for drawing the border 461 \sa setBorderPen(), backgroundBrush() 474 return m_data->borderRadius; 475 } 476 477 /*! 478 Set the pen for drawing the border 479 480 \param pen Border pen 481 \sa borderPen(), setBackgroundBrush() 482 */ 483 void QwtPlotLegendItem::setBorderPen( const QPen& pen ) 484 { 485 if ( m_data->borderPen != pen ) 486 { 487 m_data->borderPen = pen; 488 itemChanged(); 489 } 490 } 491 492 /*! 493 \return Pen for drawing the border 494 \sa setBorderPen(), backgroundBrush() 462 495 */ 463 496 QPen QwtPlotLegendItem::borderPen() const 464 497 { 465 return d_data->borderPen;466 } 467 468 /*! 469 \brief Set the background brush 470 471 The brush is used to fill the background 472 473 \param brush Brush 474 \sa backgroundBrush(), setBackgroundMode(), drawBackground() 475 */ 476 void QwtPlotLegendItem::setBackgroundBrush( const QBrush &brush )477 { 478 if ( d_data->backgroundBrush != brush )479 { 480 d_data->backgroundBrush = brush;481 itemChanged(); 482 } 483 } 484 485 /*! 486 \return Brush is used to fill the background 487 \sa setBackgroundBrush(), backgroundMode(), drawBackground() 498 return m_data->borderPen; 499 } 500 501 /*! 502 \brief Set the background brush 503 504 The brush is used to fill the background 505 506 \param brush Brush 507 \sa backgroundBrush(), setBackgroundMode(), drawBackground() 508 */ 509 void QwtPlotLegendItem::setBackgroundBrush( const QBrush& brush ) 510 { 511 if ( m_data->backgroundBrush != brush ) 512 { 513 m_data->backgroundBrush = brush; 514 itemChanged(); 515 } 516 } 517 518 /*! 519 \return Brush is used to fill the background 520 \sa setBackgroundBrush(), backgroundMode(), drawBackground() 488 521 */ 489 522 QBrush QwtPlotLegendItem::backgroundBrush() const 490 523 { 491 return d_data->backgroundBrush;492 } 493 494 /*! 495 \brief Set the background mode 496 497 Depending on the mode the complete legend or each item 498 might have an background. 499 500 The default setting is LegendBackground. 524 return m_data->backgroundBrush; 525 } 526 527 /*! 528 \brief Set the background mode 529 530 Depending on the mode the complete legend or each item 531 might have an background. 532 533 The default setting is LegendBackground. 501 534 502 535 \sa backgroundMode(), setBackgroundBrush(), drawBackground() … … 504 537 void QwtPlotLegendItem::setBackgroundMode( BackgroundMode mode ) 505 538 { 506 if ( mode != d_data->backgroundMode )507 { 508 d_data->backgroundMode = mode;509 itemChanged(); 510 } 511 } 512 513 /*! 514 \return backgroundMode 515 \sa setBackgroundMode(), backgroundBrush(), drawBackground() 539 if ( mode != m_data->backgroundMode ) 540 { 541 m_data->backgroundMode = mode; 542 itemChanged(); 543 } 544 } 545 546 /*! 547 \return backgroundMode 548 \sa setBackgroundMode(), backgroundBrush(), drawBackground() 516 549 */ 517 550 QwtPlotLegendItem::BackgroundMode QwtPlotLegendItem::backgroundMode() const 518 551 { 519 return d_data->backgroundMode;520 } 521 522 /*! 523 \brief Set the pen for drawing text labels 524 525 \param pen Text pen 526 \sa textPen(), setFont() 527 */ 528 void QwtPlotLegendItem::setTextPen( const QPen &pen )529 { 530 if ( d_data->textPen != pen )531 { 532 d_data->textPen = pen;533 itemChanged(); 534 } 535 } 536 537 /*! 538 \return Pen for drawing text labels 539 \sa setTextPen(), font() 552 return m_data->backgroundMode; 553 } 554 555 /*! 556 \brief Set the pen for drawing text labels 557 558 \param pen Text pen 559 \sa textPen(), setFont() 560 */ 561 void QwtPlotLegendItem::setTextPen( const QPen& pen ) 562 { 563 if ( m_data->textPen != pen ) 564 { 565 m_data->textPen = pen; 566 itemChanged(); 567 } 568 } 569 570 /*! 571 \return Pen for drawing text labels 572 \sa setTextPen(), font() 540 573 */ 541 574 QPen QwtPlotLegendItem::textPen() const 542 575 { 543 return d_data->textPen;544 } 545 546 /*! 547 Draw the legend 548 549 \param painter Painter 550 \param xMap x Scale Map 551 \param yMap y Scale Map 552 \param canvasRect Contents rectangle of the canvas in painter coordinates 553 */ 554 void QwtPlotLegendItem::draw( QPainter *painter,555 const QwtScaleMap &xMap, const QwtScaleMap&yMap,556 const QRectF &canvasRect ) const576 return m_data->textPen; 577 } 578 579 /*! 580 Draw the legend 581 582 \param painter Painter 583 \param xMap x Scale Map 584 \param yMap y Scale Map 585 \param canvasRect Contents rectangle of the canvas in painter coordinates 586 */ 587 void QwtPlotLegendItem::draw( QPainter* painter, 588 const QwtScaleMap& xMap, const QwtScaleMap& yMap, 589 const QRectF& canvasRect ) const 557 590 { 558 591 Q_UNUSED( xMap ); 559 592 Q_UNUSED( yMap ); 560 593 561 d_data->layout->setGeometry( geometry( canvasRect ) );562 if ( d_data->layout->geometry().isEmpty() )594 m_data->layout->setGeometry( geometry( canvasRect ) ); 595 if ( m_data->layout->geometry().isEmpty() ) 563 596 { 564 597 // don't draw a legend when having no content … … 566 599 } 567 600 568 if ( d_data->backgroundMode == QwtPlotLegendItem::LegendBackground )569 drawBackground( painter, d_data->layout->geometry() );570 571 for ( int i = 0; i < d_data->layout->count(); i++ )572 { 573 const QwtLegendLayoutItem*layoutItem =574 static_cast< QwtLegendLayoutItem*>( d_data->layout->itemAt( i ) );575 576 if ( d_data->backgroundMode == QwtPlotLegendItem::ItemBackground )601 if ( m_data->backgroundMode == QwtPlotLegendItem::LegendBackground ) 602 drawBackground( painter, m_data->layout->geometry() ); 603 604 for ( int i = 0; i < m_data->layout->count(); i++ ) 605 { 606 const LayoutItem* layoutItem = 607 static_cast< LayoutItem* >( m_data->layout->itemAt( i ) ); 608 609 if ( m_data->backgroundMode == QwtPlotLegendItem::ItemBackground ) 577 610 drawBackground( painter, layoutItem->geometry() ); 578 611 … … 587 620 588 621 /*! 589 Draw a rounded rect 590 591 \param painter Painter 592 \param rect Bounding rectangle 593 594 \sa setBorderRadius(), setBorderPen(), 622 Draw a rounded rect 623 624 \param painter Painter 625 \param rect Bounding rectangle 626 627 \sa setBorderRadius(), setBorderPen(), 595 628 setBackgroundBrush(), setBackgroundMode() 596 629 */ 597 630 void QwtPlotLegendItem::drawBackground( 598 QPainter *painter, const QRectF&rect ) const631 QPainter* painter, const QRectF& rect ) const 599 632 { 600 633 painter->save(); 601 634 602 painter->setPen( d_data->borderPen );603 painter->setBrush( d_data->backgroundBrush );604 605 const double radius = d_data->borderRadius;635 painter->setPen( m_data->borderPen ); 636 painter->setBrush( m_data->backgroundBrush ); 637 638 const double radius = m_data->borderRadius; 606 639 painter->drawRoundedRect( rect, radius, radius ); 607 640 … … 610 643 611 644 /*! 612 Calculate the geometry of the legend on the canvas 613 614 \param canvasRect Geometry of the canvas 615 \return Geometry of the legend 616 */ 617 QRect QwtPlotLegendItem::geometry( const QRectF &canvasRect ) const645 Calculate the geometry of the legend on the canvas 646 647 \param canvasRect Geometry of the canvas 648 \return Geometry of the legend 649 */ 650 QRect QwtPlotLegendItem::geometry( const QRectF& canvasRect ) const 618 651 { 619 652 QRect rect; 620 rect.setSize( d_data->layout->sizeHint() ); 621 622 int margin = d_data->borderDistance; 623 if ( d_data->alignment & Qt::AlignHCenter ) 653 rect.setSize( m_data->layout->sizeHint() ); 654 655 if ( m_data->canvasAlignment & Qt::AlignHCenter ) 624 656 { 625 657 int x = qRound( canvasRect.center().x() ); 626 658 rect.moveCenter( QPoint( x, rect.center().y() ) ); 627 659 } 628 else if ( d_data->alignment & Qt::AlignRight ) 629 { 630 rect.moveRight( qFloor( canvasRect.right() - margin ) ); 660 else if ( m_data->canvasAlignment & Qt::AlignRight ) 661 { 662 const int offset = offsetInCanvas( Qt::Horizontal ); 663 rect.moveRight( qwtFloor( canvasRect.right() - offset ) ); 631 664 } 632 665 else 633 666 { 634 rect.moveLeft( qCeil( canvasRect.left() + margin ) ); 635 } 636 637 if ( d_data->alignment & Qt::AlignVCenter ) 667 const int offset = offsetInCanvas( Qt::Horizontal ); 668 rect.moveLeft( qwtCeil( canvasRect.left() + offset ) ); 669 } 670 671 if ( m_data->canvasAlignment & Qt::AlignVCenter ) 638 672 { 639 673 int y = qRound( canvasRect.center().y() ); 640 674 rect.moveCenter( QPoint( rect.center().x(), y ) ); 641 675 } 642 else if ( d_data->alignment & Qt::AlignBottom ) 643 { 644 rect.moveBottom( qFloor( canvasRect.bottom() - margin ) ); 676 else if ( m_data->canvasAlignment & Qt::AlignBottom ) 677 { 678 const int offset = offsetInCanvas( Qt::Vertical ); 679 rect.moveBottom( qwtFloor( canvasRect.bottom() - offset ) ); 645 680 } 646 681 else 647 682 { 648 rect.moveTop( qCeil( canvasRect.top() + margin ) ); 683 const int offset = offsetInCanvas( Qt::Vertical ); 684 rect.moveTop( qwtCeil( canvasRect.top() + offset ) ); 649 685 } 650 686 … … 653 689 654 690 /*! 655 Update the legend items according to modifications of a 656 plot item 657 658 \param plotItem Plot item 659 \param data Attributes of the legend entries 660 */ 661 void QwtPlotLegendItem::updateLegend( const QwtPlotItem *plotItem,662 const QList<QwtLegendData> &data )691 Update the legend items according to modifications of a 692 plot item 693 694 \param plotItem Plot item 695 \param data Attributes of the legend entries 696 */ 697 void QwtPlotLegendItem::updateLegend( const QwtPlotItem* plotItem, 698 const QList< QwtLegendData >& data ) 663 699 { 664 700 if ( plotItem == NULL ) 665 701 return; 666 702 667 QList< QwtLegendLayoutItem*> layoutItems;668 669 QMap<const QwtPlotItem *, QList<QwtLegendLayoutItem*> >::iterator it =670 d_data->map.find( plotItem );671 if ( it != d_data->map.end() )703 QList< LayoutItem* > layoutItems; 704 705 QMap< const QwtPlotItem*, QList< LayoutItem* > >::const_iterator it = 706 m_data->map.constFind( plotItem ); 707 if ( it != m_data->map.constEnd() ) 672 708 layoutItems = it.value(); 673 709 … … 680 716 for ( int i = 0; i < layoutItems.size(); i++ ) 681 717 { 682 d_data->layout->removeItem( layoutItems[i] );718 m_data->layout->removeItem( layoutItems[i] ); 683 719 delete layoutItems[i]; 684 720 } 685 721 layoutItems.clear(); 686 722 687 if ( it != d_data->map.end() )688 d_data->map.remove( plotItem );723 if ( it != m_data->map.constEnd() ) 724 m_data->map.remove( plotItem ); 689 725 690 726 if ( !data.isEmpty() ) 691 727 { 728 layoutItems.reserve( data.size() ); 729 692 730 for ( int i = 0; i < data.size(); i++ ) 693 731 { 694 QwtLegendLayoutItem*layoutItem =695 new QwtLegendLayoutItem( this, plotItem );696 d_data->layout->addItem( layoutItem );732 LayoutItem* layoutItem = 733 new LayoutItem( this, plotItem ); 734 m_data->layout->addItem( layoutItem ); 697 735 layoutItems += layoutItem; 698 736 } 699 737 700 d_data->map.insert( plotItem, layoutItems );738 m_data->map.insert( plotItem, layoutItems ); 701 739 } 702 740 } … … 713 751 if ( changed ) 714 752 { 715 d_data->layout->invalidate();753 m_data->layout->invalidate(); 716 754 itemChanged(); 717 755 } … … 721 759 void QwtPlotLegendItem::clearLegend() 722 760 { 723 if ( ! d_data->map.isEmpty() )724 { 725 d_data->map.clear();726 727 for ( int i = d_data->layout->count() - 1; i >= 0; i-- )728 delete d_data->layout->takeAt( i );729 730 itemChanged(); 731 } 732 } 733 734 /*! 735 Draw an entry on the legend 736 737 \param painter Qt Painter 738 \param plotItem Plot item, represented by the entry 739 \param data Attributes of the legend entry 740 \param rect Bounding rectangle for the entry 741 */ 742 void QwtPlotLegendItem::drawLegendData( QPainter *painter,743 const QwtPlotItem *plotItem, const QwtLegendData&data,744 const QRectF &rect ) const761 if ( !m_data->map.isEmpty() ) 762 { 763 m_data->map.clear(); 764 765 for ( int i = m_data->layout->count() - 1; i >= 0; i-- ) 766 delete m_data->layout->takeAt( i ); 767 768 itemChanged(); 769 } 770 } 771 772 /*! 773 Draw an entry on the legend 774 775 \param painter Qt Painter 776 \param plotItem Plot item, represented by the entry 777 \param data Attributes of the legend entry 778 \param rect Bounding rectangle for the entry 779 */ 780 void QwtPlotLegendItem::drawLegendData( QPainter* painter, 781 const QwtPlotItem* plotItem, const QwtLegendData& data, 782 const QRectF& rect ) const 745 783 { 746 784 Q_UNUSED( plotItem ); 747 785 748 const int m = d_data->itemMargin;786 const int m = m_data->itemMargin; 749 787 const QRectF r = rect.toRect().adjusted( m, m, -m, -m ); 750 788 … … 763 801 graphic.render( painter, iconRect, Qt::KeepAspectRatio ); 764 802 765 titleOff += iconRect.width() + d_data->itemSpacing;803 titleOff += iconRect.width() + m_data->itemSpacing; 766 804 } 767 805 … … 778 816 779 817 /*! 780 Minimum size hint needed to display an entry 781 782 \param data Attributes of the legend entry 783 \return Minimum size 784 */ 785 QSize QwtPlotLegendItem::minimumSize( const QwtLegendData &data ) const786 { 787 QSize size( 2 * d_data->itemMargin, 2 *d_data->itemMargin );818 Minimum size hint needed to display an entry 819 820 \param data Attributes of the legend entry 821 \return Minimum size 822 */ 823 QSize QwtPlotLegendItem::minimumSize( const QwtLegendData& data ) const 824 { 825 QSize size( 2 * m_data->itemMargin, 2 * m_data->itemMargin ); 788 826 789 827 if ( !data.isValid() ) … … 806 844 const QSizeF sz = text.textSize( font() ); 807 845 808 w += qCeil( sz.width() ); 809 h = qMax( h, qCeil( sz.height() ) ); 846 w += qwtCeil( sz.width() ); 847 h = qMax( h, qwtCeil( sz.height() ) ); 810 848 } 811 849 812 850 if ( graphic.width() > 0 && !text.isEmpty() ) 813 w += d_data->itemSpacing;851 w += m_data->itemSpacing; 814 852 815 853 size += QSize( w, h ); … … 818 856 819 857 /*! 820 \return The preferred height, for a width. 821 \param data Attributes of the legend entry 822 \param width Width 823 */ 858 \return The preferred height, for a width. 859 \param data Attributes of the legend entry 860 \param width Width 861 */ 824 862 int QwtPlotLegendItem::heightForWidth( 825 const QwtLegendData &data, int width ) const826 { 827 width -= 2 * d_data->itemMargin;863 const QwtLegendData& data, int width ) const 864 { 865 width -= 2 * m_data->itemMargin; 828 866 829 867 const QwtGraphic graphic = data.icon(); … … 834 872 835 873 if ( graphic.width() > 0 ) 836 width -= graphic.width() + d_data->itemSpacing;874 width -= graphic.width() + m_data->itemSpacing; 837 875 838 876 int h = text.heightForWidth( width, font() ); 839 h += 2 * d_data->itemMargin;877 h += 2 * m_data->itemMargin; 840 878 841 879 return qMax( graphic.height(), h ); … … 843 881 844 882 /*! 845 \return All plot items with an entry on the legend 846 \note A plot item might have more than one entry on the legend 847 */ 848 QList< const QwtPlotItem * > QwtPlotLegendItem::plotItems() const849 { 850 return d_data->map.keys();851 } 852 853 /*! 854 \return Geometries of the items of a plot item 855 \note Usually a plot item has only one entry on the legend 856 */ 883 \return All plot items with an entry on the legend 884 \note A plot item might have more than one entry on the legend 885 */ 886 QList< const QwtPlotItem* > QwtPlotLegendItem::plotItems() const 887 { 888 return m_data->map.keys(); 889 } 890 891 /*! 892 \return Geometries of the items of a plot item 893 \note Usually a plot item has only one entry on the legend 894 */ 857 895 QList< QRect > QwtPlotLegendItem::legendGeometries( 858 const QwtPlotItem *plotItem ) const859 { 860 QList< QwtLegendLayoutItem*> layoutItems;861 862 QMap<const QwtPlotItem *, QList<QwtLegendLayoutItem*> >::iterator it =863 d_data->map.find( plotItem );864 if ( it != d_data->map.end() )896 const QwtPlotItem* plotItem ) const 897 { 898 QList< LayoutItem* > layoutItems; 899 900 QMap< const QwtPlotItem*, QList< LayoutItem* > >::const_iterator it = 901 m_data->map.constFind( plotItem ); 902 if ( it != m_data->map.constEnd() ) 865 903 layoutItems = it.value(); 866 904 867 QList<QRect> geometries; 905 QList< QRect > geometries; 906 geometries.reserve(layoutItems.size() ); 907 868 908 for ( int i = 0; i < layoutItems.size(); i++ ) 869 909 geometries += layoutItems[i]->geometry();
Note:
See TracChangeset
for help on using the changeset viewer.
