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         d_data->footerLabel->hide();
00609 
00610     for ( int axisId = 0; axisId < axisCnt; axisId++ )
00611     {
00612         if ( axisEnabled( axisId ) )
00613         {
00614             axisWidget( axisId )->setGeometry( scaleRect[axisId] );
00615 
00616 #if 1
00617             if ( axisId == xBottom || axisId == xTop )
00618             {
00619                 // do we need this code any longer ???
00620 
00621                 QRegion r( scaleRect[axisId] );
00622                 if ( axisEnabled( yLeft ) )
00623                     r = r.subtracted( QRegion( scaleRect[yLeft] ) );
00624                 if ( axisEnabled( yRight ) )
00625                     r = r.subtracted( QRegion( scaleRect[yRight] ) );
00626                 r.translate( -scaleRect[ axisId ].x(),
00627                     -scaleRect[axisId].y() );
00628 
00629                 axisWidget( axisId )->setMask( r );
00630             }
00631 #endif
00632             if ( !axisWidget( axisId )->isVisibleTo( this ) )
00633                 axisWidget( axisId )->show();
00634         }
00635         else
00636             axisWidget( axisId )->hide();
00637     }
00638 
00639     if ( d_data->legend )
00640     {
00641         if ( d_data->legend->isEmpty() )
00642         {
00643             d_data->legend->hide();
00644         }
00645         else
00646         {
00647             d_data->legend->setGeometry( legendRect );
00648             d_data->legend->show();
00649         }
00650     }
00651 
00652     d_data->canvas->setGeometry( canvasRect );
00653 }
00654 
00670 void QwtPlot::getCanvasMarginsHint(
00671     const QwtScaleMap maps[], const QRectF &canvasRect,
00672     double &left, double &top, double &right, double &bottom) const
00673 {
00674     left = top = right = bottom = -1.0;
00675 
00676     const QwtPlotItemList& itmList = itemList();
00677     for ( QwtPlotItemIterator it = itmList.begin();
00678         it != itmList.end(); ++it )
00679     {
00680         const QwtPlotItem *item = *it;
00681         if ( item->testItemAttribute( QwtPlotItem::Margins ) )
00682         {
00683             double m[ QwtPlot::axisCnt ];
00684             item->getCanvasMarginHint(
00685                 maps[ item->xAxis() ], maps[ item->yAxis() ],
00686                 canvasRect, m[yLeft], m[xTop], m[yRight], m[xBottom] );
00687 
00688             left = qMax( left, m[yLeft] );
00689             top = qMax( top, m[xTop] );
00690             right = qMax( right, m[yRight] );
00691             bottom = qMax( bottom, m[xBottom] );
00692         }
00693     }
00694 }
00695 
00704 void QwtPlot::updateCanvasMargins()
00705 {
00706     QwtScaleMap maps[axisCnt];
00707     for ( int axisId = 0; axisId < axisCnt; axisId++ )
00708         maps[axisId] = canvasMap( axisId );
00709 
00710     double margins[axisCnt];
00711     getCanvasMarginsHint( maps, canvas()->contentsRect(),
00712         margins[yLeft], margins[xTop], margins[yRight], margins[xBottom] );
00713     
00714     bool doUpdate = false;
00715     for ( int axisId = 0; axisId < axisCnt; axisId++ )
00716     {
00717         if ( margins[axisId] >= 0.0 )
00718         {
00719             const int m = qCeil( margins[axisId] );
00720             plotLayout()->setCanvasMargin( m, axisId);
00721             doUpdate = true;
00722         }
00723     }
00724 
00725     if ( doUpdate )
00726         updateLayout();
00727 }
00728 
00738 void QwtPlot::drawCanvas( QPainter *painter )
00739 {
00740     QwtScaleMap maps[axisCnt];
00741     for ( int axisId = 0; axisId < axisCnt; axisId++ )
00742         maps[axisId] = canvasMap( axisId );
00743 
00744     drawItems( painter, d_data->canvas->contentsRect(), maps );
00745 }
00746 
00760 void QwtPlot::drawItems( QPainter *painter, const QRectF &canvasRect,
00761         const QwtScaleMap maps[axisCnt] ) const
00762 {
00763     const QwtPlotItemList& itmList = itemList();
00764     for ( QwtPlotItemIterator it = itmList.begin();
00765         it != itmList.end(); ++it )
00766     {
00767         QwtPlotItem *item = *it;
00768         if ( item && item->isVisible() )
00769         {
00770             painter->save();
00771 
00772             painter->setRenderHint( QPainter::Antialiasing,
00773                 item->testRenderHint( QwtPlotItem::RenderAntialiased ) );
00774             painter->setRenderHint( QPainter::HighQualityAntialiasing,
00775                 item->testRenderHint( QwtPlotItem::RenderAntialiased ) );
00776 
00777             item->draw( painter,
00778                 maps[item->xAxis()], maps[item->yAxis()],
00779                 canvasRect );
00780 
00781             painter->restore();
00782         }
00783     }
00784 }
00785 
00793 QwtScaleMap QwtPlot::canvasMap( int axisId ) const
00794 {
00795     QwtScaleMap map;
00796     if ( !d_data->canvas )
00797         return map;
00798 
00799     map.setTransformation( axisScaleEngine( axisId )->transformation() );
00800 
00801     const QwtScaleDiv &sd = axisScaleDiv( axisId );
00802     map.setScaleInterval( sd.lowerBound(), sd.upperBound() );
00803 
00804     if ( axisEnabled( axisId ) )
00805     {
00806         const QwtScaleWidget *s = axisWidget( axisId );
00807         if ( axisId == yLeft || axisId == yRight )
00808         {
00809             double y = s->y() + s->startBorderDist() - d_data->canvas->y();
00810             double h = s->height() - s->startBorderDist() - s->endBorderDist();
00811             map.setPaintInterval( y + h, y );
00812         }
00813         else
00814         {
00815             double x = s->x() + s->startBorderDist() - d_data->canvas->x();
00816             double w = s->width() - s->startBorderDist() - s->endBorderDist();
00817             map.setPaintInterval( x, x + w );
00818         }
00819     }
00820     else
00821     {
00822         const QRect &canvasRect = d_data->canvas->contentsRect();
00823         if ( axisId == yLeft || axisId == yRight )
00824         {
00825             int top = 0;
00826             if ( !plotLayout()->alignCanvasToScale( xTop ) )
00827                 top = plotLayout()->canvasMargin( xTop );
00828 
00829             int bottom = 0;
00830             if ( !plotLayout()->alignCanvasToScale( xBottom ) )
00831                 bottom = plotLayout()->canvasMargin( xBottom );
00832 
00833             map.setPaintInterval( canvasRect.bottom() - bottom,
00834                 canvasRect.top() + top );
00835         }
00836         else
00837         {
00838             int left = 0;
00839             if ( !plotLayout()->alignCanvasToScale( yLeft ) )
00840                 left = plotLayout()->canvasMargin( yLeft );
00841 
00842             int right = 0;
00843             if ( !plotLayout()->alignCanvasToScale( yRight ) )
00844                 right = plotLayout()->canvasMargin( yRight );
00845 
00846             map.setPaintInterval( canvasRect.left() + left,
00847                 canvasRect.right() - right );
00848         }
00849     }
00850 
00851     return map;
00852 }
00853 
00864 void QwtPlot::setCanvasBackground( const QBrush &brush )
00865 {
00866     QPalette pal = d_data->canvas->palette();
00867     pal.setBrush( QPalette::Window, brush );
00868 
00869     canvas()->setPalette( pal );
00870 }
00871 
00879 QBrush QwtPlot::canvasBackground() const
00880 {
00881     return canvas()->palette().brush(
00882         QPalette::Normal, QPalette::Window );
00883 }
00884 
00889 bool QwtPlot::axisValid( int axisId )
00890 {
00891     return ( ( axisId >= QwtPlot::yLeft ) && ( axisId < QwtPlot::axisCnt ) );
00892 }
00893 
00927 void QwtPlot::insertLegend( QwtAbstractLegend *legend,
00928     QwtPlot::LegendPosition pos, double ratio )
00929 {
00930     d_data->layout->setLegendPosition( pos, ratio );
00931 
00932     if ( legend != d_data->legend )
00933     {
00934         if ( d_data->legend && d_data->legend->parent() == this )
00935             delete d_data->legend;
00936 
00937         d_data->legend = legend;
00938 
00939         if ( d_data->legend )
00940         {
00941             connect( this, 
00942                 SIGNAL( legendDataChanged( 
00943                     const QVariant &, const QList<QwtLegendData> & ) ),
00944                 d_data->legend, 
00945                 SLOT( updateLegend( 
00946                     const QVariant &, const QList<QwtLegendData> & ) ) 
00947             );
00948 
00949             if ( d_data->legend->parent() != this )
00950                 d_data->legend->setParent( this );
00951 
00952             qwtEnableLegendItems( this, false );
00953             updateLegend();
00954             qwtEnableLegendItems( this, true );
00955 
00956             QwtLegend *lgd = qobject_cast<QwtLegend *>( legend );
00957             if ( lgd )
00958             {
00959                 switch ( d_data->layout->legendPosition() )
00960                 {
00961                     case LeftLegend:
00962                     case RightLegend:
00963                     {
00964                         if ( lgd->maxColumns() == 0     )
00965                             lgd->setMaxColumns( 1 ); // 1 column: align vertical
00966                         break;
00967                     }
00968                     case TopLegend:
00969                     case BottomLegend:
00970                     {
00971                         lgd->setMaxColumns( 0 ); // unlimited
00972                         break;
00973                     }
00974                     default:
00975                         break;
00976                 }
00977             }
00978 
00979             QWidget *previousInChain = NULL;
00980             switch ( d_data->layout->legendPosition() )
00981             {
00982                 case LeftLegend:
00983                 {
00984                     previousInChain = axisWidget( QwtPlot::xTop );
00985                     break;
00986                 }
00987                 case TopLegend:
00988                 {
00989                     previousInChain = this;
00990                     break;
00991                 }
00992                 case RightLegend:
00993                 {
00994                     previousInChain = axisWidget( QwtPlot::yRight );
00995                     break;
00996                 }
00997                 case BottomLegend:
00998                 {
00999                     previousInChain = footerLabel();
01000                     break;
01001                 }
01002             }
01003 
01004             if ( previousInChain )
01005                 qwtSetTabOrder( previousInChain, legend, true );
01006         }
01007     }
01008 
01009     updateLayout();
01010 }
01011 
01017 void QwtPlot::updateLegend()
01018 {
01019     const QwtPlotItemList& itmList = itemList();
01020     for ( QwtPlotItemIterator it = itmList.begin();
01021         it != itmList.end(); ++it )
01022     {
01023         updateLegend( *it );
01024     }
01025 }
01026 
01033 void QwtPlot::updateLegend( const QwtPlotItem *plotItem )
01034 {
01035     if ( plotItem == NULL )
01036         return;
01037 
01038     QList<QwtLegendData> legendData;
01039 
01040     if ( plotItem->testItemAttribute( QwtPlotItem::Legend ) )
01041         legendData = plotItem->legendData();
01042 
01043     const QVariant itemInfo = itemToInfo( const_cast< QwtPlotItem *>( plotItem) );
01044     Q_EMIT legendDataChanged( itemInfo, legendData );
01045 }
01046 
01059 void QwtPlot::updateLegendItems( const QVariant &itemInfo,
01060     const QList<QwtLegendData> &legendData )
01061 {
01062     QwtPlotItem *plotItem = infoToItem( itemInfo );
01063     if ( plotItem )
01064     {
01065         const QwtPlotItemList& itmList = itemList();
01066         for ( QwtPlotItemIterator it = itmList.begin();
01067             it != itmList.end(); ++it )
01068         {
01069             QwtPlotItem *item = *it;
01070             if ( item->testItemInterest( QwtPlotItem::LegendInterest ) )
01071                 item->updateLegend( plotItem, legendData );
01072         }
01073     }
01074 }
01075 
01082 void QwtPlot::attachItem( QwtPlotItem *plotItem, bool on )
01083 {
01084     if ( plotItem->testItemInterest( QwtPlotItem::LegendInterest ) )
01085     {
01086         // plotItem is some sort of legend
01087 
01088         const QwtPlotItemList& itmList = itemList();
01089         for ( QwtPlotItemIterator it = itmList.begin();
01090             it != itmList.end(); ++it )
01091         {
01092             QwtPlotItem *item = *it;
01093 
01094             QList<QwtLegendData> legendData;
01095             if ( on && item->testItemAttribute( QwtPlotItem::Legend ) )
01096             {
01097                 legendData = item->legendData();
01098                 plotItem->updateLegend( item, legendData );
01099             }
01100         }
01101     }
01102 
01103     if ( on )
01104         insertItem( plotItem );
01105     else 
01106         removeItem( plotItem );
01107 
01108     Q_EMIT itemAttached( plotItem, on );
01109 
01110     if ( plotItem->testItemAttribute( QwtPlotItem::Legend ) )
01111     {
01112         // the item wants to be represented on the legend
01113 
01114         if ( on )
01115         {
01116             updateLegend( plotItem );
01117         }
01118         else
01119         {
01120             const QVariant itemInfo = itemToInfo( plotItem );
01121             Q_EMIT legendDataChanged( itemInfo, QList<QwtLegendData>() );
01122         }
01123     }
01124 
01125     autoRefresh();
01126 }
01127 
01145 QVariant QwtPlot::itemToInfo( QwtPlotItem *plotItem ) const
01146 {
01147     QVariant itemInfo;
01148     qVariantSetValue( itemInfo, plotItem );
01149 
01150     return itemInfo;
01151 }
01152 
01168 QwtPlotItem *QwtPlot::infoToItem( const QVariant &itemInfo ) const
01169 {
01170     if ( itemInfo.canConvert<QwtPlotItem *>() )
01171         return qvariant_cast<QwtPlotItem *>( itemInfo );
01172 
01173     return NULL;
01174 }
01175 
01176 


plotjuggler
Author(s): Davide Faconti
autogenerated on Fri Sep 1 2017 02:41:56