qwt_plot.cpp
Go to the documentation of this file.
00001 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
00002  * Qwt Widget Library
00003  * Copyright (C) 1997   Josef Wilgen
00004  * Copyright (C) 2002   Uwe Rathmann
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the Qwt License, Version 1.0
00008  *****************************************************************************/
00009 
00010 #include "qwt_plot.h"
00011 #include "qwt_plot_dict.h"
00012 #include "qwt_plot_layout.h"
00013 #include "qwt_scale_widget.h"
00014 #include "qwt_scale_engine.h"
00015 #include "qwt_text_label.h"
00016 #include "qwt_legend.h"
00017 #include "qwt_legend_data.h"
00018 #include "qwt_plot_canvas.h"
00019 #include <qmath.h>
00020 #include <qpainter.h>
00021 #include <qpointer.h>
00022 #include <qpaintengine.h>
00023 #include <qapplication.h>
00024 #include <qevent.h>
00025 
00026 static inline void qwtEnableLegendItems( QwtPlot *plot, bool on )
00027 {
00028     if ( on )
00029     {
00030         QObject::connect( 
00031             plot, SIGNAL( legendDataChanged(
00032                 const QVariant &, const QList<QwtLegendData> & ) ),
00033             plot, SLOT( updateLegendItems( 
00034                 const QVariant &, const QList<QwtLegendData> & ) ) );
00035     }
00036     else
00037     {
00038         QObject::disconnect( 
00039             plot, SIGNAL( legendDataChanged(
00040                 const QVariant &, const QList<QwtLegendData> & ) ),
00041             plot, SLOT( updateLegendItems( 
00042                 const QVariant &, const QList<QwtLegendData> & ) ) );
00043     }
00044 }
00045 
00046 static void qwtSetTabOrder( 
00047     QWidget *first, QWidget *second, bool withChildren )
00048 {
00049     QList<QWidget *> tabChain;
00050     tabChain += first;
00051     tabChain += second;
00052 
00053     if ( withChildren )
00054     {
00055         QList<QWidget *> children = second->findChildren<QWidget *>();
00056 
00057         QWidget *w = second->nextInFocusChain();
00058         while ( children.contains( w ) )
00059         {
00060             children.removeAll( w );
00061 
00062             tabChain += w;
00063             w = w->nextInFocusChain();
00064         }
00065     }
00066 
00067     for ( int i = 0; i < tabChain.size() - 1; i++ )
00068     {
00069         QWidget *from = tabChain[i];
00070         QWidget *to = tabChain[i+1];
00071 
00072         const Qt::FocusPolicy policy1 = from->focusPolicy();
00073         const Qt::FocusPolicy policy2 = to->focusPolicy();
00074 
00075         QWidget *proxy1 = from->focusProxy();
00076         QWidget *proxy2 = to->focusProxy();
00077 
00078         from->setFocusPolicy( Qt::TabFocus );
00079         from->setFocusProxy( NULL);
00080 
00081         to->setFocusPolicy( Qt::TabFocus );
00082         to->setFocusProxy( NULL);
00083 
00084         QWidget::setTabOrder( from, to );
00085 
00086         from->setFocusPolicy( policy1 );
00087         from->setFocusProxy( proxy1);
00088 
00089         to->setFocusPolicy( policy2 );
00090         to->setFocusProxy( proxy2 );
00091     }
00092 }
00093 
00094 class QwtPlot::PrivateData
00095 {
00096 public:
00097     QPointer<QwtTextLabel> titleLabel;
00098     QPointer<QwtTextLabel> footerLabel;
00099     QPointer<QWidget> canvas;
00100     QPointer<QwtAbstractLegend> legend;
00101     QwtPlotLayout *layout;
00102 
00103     bool autoReplot;
00104 };
00105 
00110 QwtPlot::QwtPlot( QWidget *parent ):
00111     QFrame( parent )
00112 {
00113     initPlot( QwtText() );
00114 }
00115 
00121 QwtPlot::QwtPlot( const QwtText &title, QWidget *parent ):
00122     QFrame( parent )
00123 {
00124     initPlot( title );
00125 }
00126 
00128 QwtPlot::~QwtPlot()
00129 {
00130     setAutoReplot( false );
00131     detachItems( QwtPlotItem::Rtti_PlotItem, autoDelete() );
00132 
00133     delete d_data->layout;
00134     deleteAxesData();
00135     delete d_data;
00136 }
00137 
00142 void QwtPlot::initPlot( const QwtText &title )
00143 {
00144     d_data = new PrivateData;
00145 
00146     d_data->layout = new QwtPlotLayout;
00147     d_data->autoReplot = false;
00148 
00149     // title
00150     d_data->titleLabel = new QwtTextLabel( this );
00151     d_data->titleLabel->setObjectName( "QwtPlotTitle" );
00152     d_data->titleLabel->setFont( QFont( fontInfo().family(), 14, QFont::Bold ) );
00153 
00154     QwtText text( title );
00155     text.setRenderFlags( Qt::AlignCenter | Qt::TextWordWrap );
00156     d_data->titleLabel->setText( text );
00157 
00158     // footer
00159     d_data->footerLabel = new QwtTextLabel( this );
00160     d_data->footerLabel->setObjectName( "QwtPlotFooter" );
00161 
00162     QwtText footer;
00163     footer.setRenderFlags( Qt::AlignCenter | Qt::TextWordWrap );
00164     d_data->footerLabel->setText( footer );
00165 
00166     // legend
00167     d_data->legend = NULL;
00168 
00169     // axis
00170     initAxesData();
00171 
00172     // canvas
00173     d_data->canvas = new QwtPlotCanvas( this );
00174     d_data->canvas->setObjectName( "QwtPlotCanvas" );
00175     d_data->canvas->installEventFilter( this );
00176 
00177     setSizePolicy( QSizePolicy::MinimumExpanding,
00178         QSizePolicy::MinimumExpanding );
00179 
00180     resize( 200, 200 );
00181 
00182     QList<QWidget *> focusChain;
00183     focusChain << this << d_data->titleLabel << axisWidget( xTop )
00184         << axisWidget( yLeft ) << d_data->canvas << axisWidget( yRight )
00185         << axisWidget( xBottom ) << d_data->footerLabel;
00186 
00187     for ( int i = 0; i < focusChain.size() - 1; i++ )
00188         qwtSetTabOrder( focusChain[i], focusChain[i+1], false );
00189 
00190     qwtEnableLegendItems( this, true );
00191 }
00192 
00217 void QwtPlot::setCanvas( QWidget *canvas )
00218 {
00219     if ( canvas == d_data->canvas )
00220         return;
00221 
00222     delete d_data->canvas;
00223     d_data->canvas = canvas;
00224 
00225     if ( canvas )
00226     {
00227         canvas->setParent( this );
00228         canvas->installEventFilter( this );
00229 
00230         if ( isVisible() )
00231             canvas->show();
00232     }
00233 }
00234 
00241 bool QwtPlot::event( QEvent *event )
00242 {
00243     bool ok = QFrame::event( event );
00244     switch ( event->type() )
00245     {
00246         case QEvent::LayoutRequest:
00247             updateLayout();
00248             break;
00249         case QEvent::PolishRequest:
00250             replot();
00251             break;
00252         default:;
00253     }
00254     return ok;
00255 }
00256 
00275 bool QwtPlot::eventFilter( QObject *object, QEvent *event )
00276 {
00277     if ( object == d_data->canvas )
00278     {
00279         if ( event->type() == QEvent::Resize )
00280         {
00281             updateCanvasMargins();
00282         }
00283         else if ( event->type() == QEvent::ContentsRectChange )
00284         {
00285             updateLayout();
00286         }
00287     }
00288 
00289     return QFrame::eventFilter( object, event );
00290 }
00291 
00293 void QwtPlot::autoRefresh()
00294 {
00295     if ( d_data->autoReplot )
00296         replot();
00297 }
00298 
00314 void QwtPlot::setAutoReplot( bool tf )
00315 {
00316     d_data->autoReplot = tf;
00317 }
00318 
00323 bool QwtPlot::autoReplot() const
00324 {
00325     return d_data->autoReplot;
00326 }
00327 
00332 void QwtPlot::setTitle( const QString &title )
00333 {
00334     if ( title != d_data->titleLabel->text().text() )
00335     {
00336         d_data->titleLabel->setText( title );
00337         updateLayout();
00338     }
00339 }
00340 
00345 void QwtPlot::setTitle( const QwtText &title )
00346 {
00347     if ( title != d_data->titleLabel->text() )
00348     {
00349         d_data->titleLabel->setText( title );
00350         updateLayout();
00351     }
00352 }
00353 
00355 QwtText QwtPlot::title() const
00356 {
00357     return d_data->titleLabel->text();
00358 }
00359 
00361 QwtTextLabel *QwtPlot::titleLabel()
00362 {
00363     return d_data->titleLabel;
00364 }
00365 
00367 const QwtTextLabel *QwtPlot::titleLabel() const
00368 {
00369     return d_data->titleLabel;
00370 }
00371 
00376 void QwtPlot::setFooter( const QString &text )
00377 {
00378     if ( text != d_data->footerLabel->text().text() )
00379     {
00380         d_data->footerLabel->setText( text );
00381         updateLayout();
00382     }
00383 }
00384 
00389 void QwtPlot::setFooter( const QwtText &text )
00390 {
00391     if ( text != d_data->footerLabel->text() )
00392     {
00393         d_data->footerLabel->setText( text );
00394         updateLayout();
00395     }
00396 }
00397 
00399 QwtText QwtPlot::footer() const
00400 {
00401     return d_data->footerLabel->text();
00402 }
00403 
00405 QwtTextLabel *QwtPlot::footerLabel()
00406 {
00407     return d_data->footerLabel;
00408 }
00409 
00411 const QwtTextLabel *QwtPlot::footerLabel() const
00412 {
00413     return d_data->footerLabel;
00414 }
00415 
00422 void QwtPlot::setPlotLayout( QwtPlotLayout *layout )
00423 {
00424     if ( layout != d_data->layout )
00425     {
00426         delete d_data->layout;
00427         d_data->layout = layout;
00428 
00429         updateLayout();
00430     }
00431 }
00432 
00434 QwtPlotLayout *QwtPlot::plotLayout()
00435 {
00436     return d_data->layout;
00437 }
00438 
00440 const QwtPlotLayout *QwtPlot::plotLayout() const
00441 {
00442     return d_data->layout;
00443 }
00444 
00449 QwtAbstractLegend *QwtPlot::legend()
00450 {
00451     return d_data->legend;
00452 }
00453 
00458 const QwtAbstractLegend *QwtPlot::legend() const
00459 {
00460     return d_data->legend;
00461 }
00462 
00463 
00467 QWidget *QwtPlot::canvas()
00468 {
00469     return d_data->canvas;
00470 }
00471 
00475 const QWidget *QwtPlot::canvas() const
00476 {
00477     return d_data->canvas;
00478 }
00479 
00484 QSize QwtPlot::sizeHint() const
00485 {
00486     int dw = 0;
00487     int dh = 0;
00488     for ( int axisId = 0; axisId < axisCnt; axisId++ )
00489     {
00490         if ( axisEnabled( axisId ) )
00491         {
00492             const int niceDist = 40;
00493             const QwtScaleWidget *scaleWidget = axisWidget( axisId );
00494             const QwtScaleDiv &scaleDiv = scaleWidget->scaleDraw()->scaleDiv();
00495             const int majCnt = scaleDiv.ticks( QwtScaleDiv::MajorTick ).count();
00496 
00497             if ( axisId == yLeft || axisId == yRight )
00498             {
00499                 int hDiff = ( majCnt - 1 ) * niceDist
00500                     - scaleWidget->minimumSizeHint().height();
00501                 if ( hDiff > dh )
00502                     dh = hDiff;
00503             }
00504             else
00505             {
00506                 int wDiff = ( majCnt - 1 ) * niceDist
00507                     - scaleWidget->minimumSizeHint().width();
00508                 if ( wDiff > dw )
00509                     dw = wDiff;
00510             }
00511         }
00512     }
00513     return minimumSizeHint() + QSize( dw, dh );
00514 }
00515 
00519 QSize QwtPlot::minimumSizeHint() const
00520 {
00521     QSize hint = d_data->layout->minimumSizeHint( this );
00522     hint += QSize( 2 * frameWidth(), 2 * frameWidth() );
00523 
00524     return hint;
00525 }
00526 
00531 void QwtPlot::resizeEvent( QResizeEvent *e )
00532 {
00533     QFrame::resizeEvent( e );
00534     updateLayout();
00535 }
00536 
00546 void QwtPlot::replot()
00547 {
00548     bool doAutoReplot = autoReplot();
00549     setAutoReplot( false );
00550 
00551     updateAxes();
00552 
00553     /*
00554       Maybe the layout needs to be updated, because of changed
00555       axes labels. We need to process them here before painting
00556       to avoid that scales and canvas get out of sync.
00557      */
00558     QApplication::sendPostedEvents( this, QEvent::LayoutRequest );
00559 
00560     if ( d_data->canvas )
00561     {
00562         const bool ok = QMetaObject::invokeMethod( 
00563             d_data->canvas, "replot", Qt::DirectConnection );
00564         if ( !ok )
00565         {
00566             // fallback, when canvas has no a replot method
00567             d_data->canvas->update( d_data->canvas->contentsRect() );
00568         }
00569     }
00570 
00571     setAutoReplot( doAutoReplot );
00572 }
00573 
00578 void QwtPlot::updateLayout()
00579 {
00580     d_data->layout->activate( this, contentsRect() );
00581 
00582     QRect titleRect = d_data->layout->titleRect().toRect();
00583     QRect footerRect = d_data->layout->footerRect().toRect();
00584     QRect scaleRect[QwtPlot::axisCnt];
00585     for ( int axisId = 0; axisId < axisCnt; axisId++ )
00586         scaleRect[axisId] = d_data->layout->scaleRect( axisId ).toRect();
00587     QRect legendRect = d_data->layout->legendRect().toRect();
00588     QRect canvasRect = d_data->layout->canvasRect().toRect();
00589 
00590     // resize and show the visible widgets
00591 
00592     if ( !d_data->titleLabel->text().isEmpty() )
00593     {
00594         d_data->titleLabel->setGeometry( titleRect );
00595         if ( !d_data->titleLabel->isVisibleTo( this ) )
00596             d_data->titleLabel->show();
00597     }
00598     else
00599         d_data->titleLabel->hide();
00600 
00601     if ( !d_data->footerLabel->text().isEmpty() )
00602     {
00603         d_data->footerLabel->setGeometry( footerRect );
00604         if ( !d_data->footerLabel->isVisibleTo( this ) )
00605             d_data->footerLabel->show();
00606     }
00607     else
00608     {
00609         d_data->footerLabel->hide();
00610     }
00611 
00612     for ( int axisId = 0; axisId < axisCnt; axisId++ )
00613     {
00614         QwtScaleWidget* scaleWidget = axisWidget( axisId );
00615 
00616         if ( axisEnabled( axisId ) )
00617         {
00618             if ( scaleRect[axisId] != scaleWidget->geometry() )
00619             {
00620                 scaleWidget->setGeometry( scaleRect[axisId] );
00621 
00622                 int startDist, endDist;
00623                 scaleWidget->getBorderDistHint( startDist, endDist );
00624                 scaleWidget->setBorderDist( startDist, endDist );
00625             }
00626 
00627             if ( !scaleWidget->isVisibleTo( this ) )
00628                 scaleWidget->show();
00629         }
00630         else
00631         {
00632             scaleWidget->hide();
00633         }
00634     }
00635 
00636     if ( d_data->legend )
00637     {
00638         if ( d_data->legend->isEmpty() )
00639         {
00640             d_data->legend->hide();
00641         }
00642         else
00643         {
00644             d_data->legend->setGeometry( legendRect );
00645             d_data->legend->show();
00646         }
00647     }
00648 
00649     d_data->canvas->setGeometry( canvasRect );
00650 }
00651 
00667 void QwtPlot::getCanvasMarginsHint(
00668     const QwtScaleMap maps[], const QRectF &canvasRect,
00669     double &left, double &top, double &right, double &bottom) const
00670 {
00671     left = top = right = bottom = -1.0;
00672 
00673     const QwtPlotItemList& itmList = itemList();
00674     for ( QwtPlotItemIterator it = itmList.begin();
00675         it != itmList.end(); ++it )
00676     {
00677         const QwtPlotItem *item = *it;
00678         if ( item->testItemAttribute( QwtPlotItem::Margins ) )
00679         {
00680             double m[ QwtPlot::axisCnt ];
00681             item->getCanvasMarginHint(
00682                 maps[ item->xAxis() ], maps[ item->yAxis() ],
00683                 canvasRect, m[yLeft], m[xTop], m[yRight], m[xBottom] );
00684 
00685             left = qMax( left, m[yLeft] );
00686             top = qMax( top, m[xTop] );
00687             right = qMax( right, m[yRight] );
00688             bottom = qMax( bottom, m[xBottom] );
00689         }
00690     }
00691 }
00692 
00701 void QwtPlot::updateCanvasMargins()
00702 {
00703     QwtScaleMap maps[axisCnt];
00704     for ( int axisId = 0; axisId < axisCnt; axisId++ )
00705         maps[axisId] = canvasMap( axisId );
00706 
00707     double margins[axisCnt];
00708     getCanvasMarginsHint( maps, canvas()->contentsRect(),
00709         margins[yLeft], margins[xTop], margins[yRight], margins[xBottom] );
00710     
00711     bool doUpdate = false;
00712     for ( int axisId = 0; axisId < axisCnt; axisId++ )
00713     {
00714         if ( margins[axisId] >= 0.0 )
00715         {
00716             const int m = qCeil( margins[axisId] );
00717             plotLayout()->setCanvasMargin( m, axisId);
00718             doUpdate = true;
00719         }
00720     }
00721 
00722     if ( doUpdate )
00723         updateLayout();
00724 }
00725 
00735 void QwtPlot::drawCanvas( QPainter *painter )
00736 {
00737     QwtScaleMap maps[axisCnt];
00738     for ( int axisId = 0; axisId < axisCnt; axisId++ )
00739         maps[axisId] = canvasMap( axisId );
00740 
00741     drawItems( painter, d_data->canvas->contentsRect(), maps );
00742 }
00743 
00757 void QwtPlot::drawItems( QPainter *painter, const QRectF &canvasRect,
00758         const QwtScaleMap maps[axisCnt] ) const
00759 {
00760     const QwtPlotItemList& itmList = itemList();
00761     for ( QwtPlotItemIterator it = itmList.begin();
00762         it != itmList.end(); ++it )
00763     {
00764         QwtPlotItem *item = *it;
00765         if ( item && item->isVisible() )
00766         {
00767             painter->save();
00768 
00769             painter->setRenderHint( QPainter::Antialiasing,
00770                 item->testRenderHint( QwtPlotItem::RenderAntialiased ) );
00771             painter->setRenderHint( QPainter::HighQualityAntialiasing,
00772                 item->testRenderHint( QwtPlotItem::RenderAntialiased ) );
00773 
00774             item->draw( painter,
00775                 maps[item->xAxis()], maps[item->yAxis()],
00776                 canvasRect );
00777 
00778             painter->restore();
00779         }
00780     }
00781 }
00782 
00790 QwtScaleMap QwtPlot::canvasMap( int axisId ) const
00791 {
00792     QwtScaleMap map;
00793     if ( !d_data->canvas )
00794         return map;
00795 
00796     map.setTransformation( axisScaleEngine( axisId )->transformation() );
00797 
00798     const QwtScaleDiv &sd = axisScaleDiv( axisId );
00799     map.setScaleInterval( sd.lowerBound(), sd.upperBound() );
00800 
00801     if ( axisEnabled( axisId ) )
00802     {
00803         const QwtScaleWidget *s = axisWidget( axisId );
00804         if ( axisId == yLeft || axisId == yRight )
00805         {
00806             double y = s->y() + s->startBorderDist() - d_data->canvas->y();
00807             double h = s->height() - s->startBorderDist() - s->endBorderDist();
00808             map.setPaintInterval( y + h, y );
00809         }
00810         else
00811         {
00812             double x = s->x() + s->startBorderDist() - d_data->canvas->x();
00813             double w = s->width() - s->startBorderDist() - s->endBorderDist();
00814             map.setPaintInterval( x, x + w );
00815         }
00816     }
00817     else
00818     {
00819         const QRect &canvasRect = d_data->canvas->contentsRect();
00820         if ( axisId == yLeft || axisId == yRight )
00821         {
00822             int top = 0;
00823             if ( !plotLayout()->alignCanvasToScale( xTop ) )
00824                 top = plotLayout()->canvasMargin( xTop );
00825 
00826             int bottom = 0;
00827             if ( !plotLayout()->alignCanvasToScale( xBottom ) )
00828                 bottom = plotLayout()->canvasMargin( xBottom );
00829 
00830             map.setPaintInterval( canvasRect.bottom() - bottom,
00831                 canvasRect.top() + top );
00832         }
00833         else
00834         {
00835             int left = 0;
00836             if ( !plotLayout()->alignCanvasToScale( yLeft ) )
00837                 left = plotLayout()->canvasMargin( yLeft );
00838 
00839             int right = 0;
00840             if ( !plotLayout()->alignCanvasToScale( yRight ) )
00841                 right = plotLayout()->canvasMargin( yRight );
00842 
00843             map.setPaintInterval( canvasRect.left() + left,
00844                 canvasRect.right() - right );
00845         }
00846     }
00847 
00848     return map;
00849 }
00850 
00861 void QwtPlot::setCanvasBackground( const QBrush &brush )
00862 {
00863     QPalette pal = d_data->canvas->palette();
00864     pal.setBrush( QPalette::Window, brush );
00865 
00866     canvas()->setPalette( pal );
00867 }
00868 
00876 QBrush QwtPlot::canvasBackground() const
00877 {
00878     return canvas()->palette().brush(
00879         QPalette::Normal, QPalette::Window );
00880 }
00881 
00886 bool QwtPlot::axisValid( int axisId )
00887 {
00888     return ( ( axisId >= QwtPlot::yLeft ) && ( axisId < QwtPlot::axisCnt ) );
00889 }
00890 
00924 void QwtPlot::insertLegend( QwtAbstractLegend *legend,
00925     QwtPlot::LegendPosition pos, double ratio )
00926 {
00927     d_data->layout->setLegendPosition( pos, ratio );
00928 
00929     if ( legend != d_data->legend )
00930     {
00931         if ( d_data->legend && d_data->legend->parent() == this )
00932             delete d_data->legend;
00933 
00934         d_data->legend = legend;
00935 
00936         if ( d_data->legend )
00937         {
00938             connect( this, 
00939                 SIGNAL( legendDataChanged( 
00940                     const QVariant &, const QList<QwtLegendData> & ) ),
00941                 d_data->legend, 
00942                 SLOT( updateLegend( 
00943                     const QVariant &, const QList<QwtLegendData> & ) ) 
00944             );
00945 
00946             if ( d_data->legend->parent() != this )
00947                 d_data->legend->setParent( this );
00948 
00949             qwtEnableLegendItems( this, false );
00950             updateLegend();
00951             qwtEnableLegendItems( this, true );
00952 
00953             QwtLegend *lgd = qobject_cast<QwtLegend *>( legend );
00954             if ( lgd )
00955             {
00956                 switch ( d_data->layout->legendPosition() )
00957                 {
00958                     case LeftLegend:
00959                     case RightLegend:
00960                     {
00961                         if ( lgd->maxColumns() == 0     )
00962                             lgd->setMaxColumns( 1 ); // 1 column: align vertical
00963                         break;
00964                     }
00965                     case TopLegend:
00966                     case BottomLegend:
00967                     {
00968                         lgd->setMaxColumns( 0 ); // unlimited
00969                         break;
00970                     }
00971                     default:
00972                         break;
00973                 }
00974             }
00975 
00976             QWidget *previousInChain = NULL;
00977             switch ( d_data->layout->legendPosition() )
00978             {
00979                 case LeftLegend:
00980                 {
00981                     previousInChain = axisWidget( QwtPlot::xTop );
00982                     break;
00983                 }
00984                 case TopLegend:
00985                 {
00986                     previousInChain = this;
00987                     break;
00988                 }
00989                 case RightLegend:
00990                 {
00991                     previousInChain = axisWidget( QwtPlot::yRight );
00992                     break;
00993                 }
00994                 case BottomLegend:
00995                 {
00996                     previousInChain = footerLabel();
00997                     break;
00998                 }
00999             }
01000 
01001             if ( previousInChain )
01002                 qwtSetTabOrder( previousInChain, legend, true );
01003         }
01004     }
01005 
01006     updateLayout();
01007 }
01008 
01014 void QwtPlot::updateLegend()
01015 {
01016     const QwtPlotItemList& itmList = itemList();
01017     for ( QwtPlotItemIterator it = itmList.begin();
01018         it != itmList.end(); ++it )
01019     {
01020         updateLegend( *it );
01021     }
01022 }
01023 
01030 void QwtPlot::updateLegend( const QwtPlotItem *plotItem )
01031 {
01032     if ( plotItem == NULL )
01033         return;
01034 
01035     QList<QwtLegendData> legendData;
01036 
01037     if ( plotItem->testItemAttribute( QwtPlotItem::Legend ) )
01038         legendData = plotItem->legendData();
01039 
01040     const QVariant itemInfo = itemToInfo( const_cast< QwtPlotItem *>( plotItem) );
01041     Q_EMIT legendDataChanged( itemInfo, legendData );
01042 }
01043 
01056 void QwtPlot::updateLegendItems( const QVariant &itemInfo,
01057     const QList<QwtLegendData> &legendData )
01058 {
01059     QwtPlotItem *plotItem = infoToItem( itemInfo );
01060     if ( plotItem )
01061     {
01062         const QwtPlotItemList& itmList = itemList();
01063         for ( QwtPlotItemIterator it = itmList.begin();
01064             it != itmList.end(); ++it )
01065         {
01066             QwtPlotItem *item = *it;
01067             if ( item->testItemInterest( QwtPlotItem::LegendInterest ) )
01068                 item->updateLegend( plotItem, legendData );
01069         }
01070     }
01071 }
01072 
01079 void QwtPlot::attachItem( QwtPlotItem *plotItem, bool on )
01080 {
01081     if ( plotItem->testItemInterest( QwtPlotItem::LegendInterest ) )
01082     {
01083         // plotItem is some sort of legend
01084 
01085         const QwtPlotItemList& itmList = itemList();
01086         for ( QwtPlotItemIterator it = itmList.begin();
01087             it != itmList.end(); ++it )
01088         {
01089             QwtPlotItem *item = *it;
01090 
01091             QList<QwtLegendData> legendData;
01092             if ( on && item->testItemAttribute( QwtPlotItem::Legend ) )
01093             {
01094                 legendData = item->legendData();
01095                 plotItem->updateLegend( item, legendData );
01096             }
01097         }
01098     }
01099 
01100     if ( on )
01101         insertItem( plotItem );
01102     else 
01103         removeItem( plotItem );
01104 
01105     Q_EMIT itemAttached( plotItem, on );
01106 
01107     if ( plotItem->testItemAttribute( QwtPlotItem::Legend ) )
01108     {
01109         // the item wants to be represented on the legend
01110 
01111         if ( on )
01112         {
01113             updateLegend( plotItem );
01114         }
01115         else
01116         {
01117             const QVariant itemInfo = itemToInfo( plotItem );
01118             Q_EMIT legendDataChanged( itemInfo, QList<QwtLegendData>() );
01119         }
01120     }
01121 
01122     autoRefresh();
01123 }
01124 
01142 QVariant QwtPlot::itemToInfo( QwtPlotItem *plotItem ) const
01143 {
01144     QVariant itemInfo;
01145     qVariantSetValue( itemInfo, plotItem );
01146 
01147     return itemInfo;
01148 }
01149 
01165 QwtPlotItem *QwtPlot::infoToItem( const QVariant &itemInfo ) const
01166 {
01167     if ( itemInfo.canConvert<QwtPlotItem *>() )
01168         return qvariant_cast<QwtPlotItem *>( itemInfo );
01169 
01170     return NULL;
01171 }
01172 
01173 


plotjuggler
Author(s): Davide Faconti
autogenerated on Wed Jul 3 2019 19:28:04