Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

qwt_plot.cpp

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 // vim: expandtab
00011 
00012 #include <float.h>
00013 #include <qlabel.h>
00014 #include <qpainter.h>
00015 #include <qfocusdata.h>
00016 #include <qevent.h>
00017 #include "qwt_plot.h"
00018 #include "qwt_plot_layout.h"
00019 #include "qwt_plot_dict.h"
00020 #include "qwt_rect.h"
00021 #include "qwt_scale.h"
00022 #include "qwt_scale.h"
00023 #include "qwt_legend.h"
00024 #include "qwt_dyngrid_layout.h"
00025 #include "qwt_plot_canvas.h"
00026 #include "qwt_math.h"
00027 #include "qwt_paint_buffer.h"
00028 
00035 QwtPlot::QwtPlot(QWidget *parent, const char *name) :
00036     QFrame(parent, name, Qt::WRepaintNoErase|Qt::WResizeNoErase)
00037 {
00038     initPlot();
00039 }
00040 
00041 
00048 QwtPlot::QwtPlot(const QString &title, QWidget *parent, const char *name) :
00049     QFrame(parent, name, Qt::WRepaintNoErase|Qt::WResizeNoErase)
00050 {
00051     initPlot(title);
00052 }
00053 
00055 QwtPlot::~QwtPlot()
00056 {
00057     delete d_layout;
00058     delete d_curves;
00059     delete d_markers;
00060     delete d_grid;
00061 }
00062 
00067 void QwtPlot::initPlot(const QString &title)
00068 {
00069     d_layout = new QwtPlotLayout;
00070 
00071     d_curves = new QwtCurveDict;
00072     d_markers = new QwtMarkerDict;
00073 
00074     d_autoReplot = FALSE;
00075 
00076     d_lblTitle = new QLabel(title, this);
00077     d_lblTitle->setFont(QFont(fontInfo().family(), 14, QFont::Bold));
00078     d_lblTitle->setAlignment(Qt::AlignCenter|Qt::WordBreak|Qt::ExpandTabs);
00079 
00080     d_legend = new QwtLegend(this);
00081     d_autoLegend = FALSE;
00082 
00083     d_scale[yLeft] = new QwtScale(QwtScale::Left, this, "yLeft");
00084     d_scale[yRight] = new QwtScale(QwtScale::Right, this, "yRight");
00085     d_scale[xTop] = new QwtScale(QwtScale::Top, this, "xTop");
00086     d_scale[xBottom] = new QwtScale(QwtScale::Bottom, this, "xBottom");
00087 
00088     initAxes();
00089 
00090     d_grid = new QwtPlotGrid(this);
00091     d_grid->setPen(QPen(Qt::black, 0, Qt::DotLine));
00092     d_grid->enableXMin(FALSE);
00093     d_grid->enableYMin(FALSE);
00094     d_grid->setAxis(xBottom, yLeft);
00095 
00096     d_canvas = new QwtPlotCanvas(this);
00097     d_canvas->setFrameStyle(QFrame::Panel|QFrame::Sunken);
00098     d_canvas->setLineWidth(2);
00099     d_canvas->setMidLineWidth(0);
00100 
00101 #ifndef QWT_NO_COMPAT
00102     connect(d_canvas, SIGNAL(mousePressed(const QMouseEvent &)),
00103         this, SIGNAL(plotMousePressed(const QMouseEvent &)));
00104     connect(d_canvas, SIGNAL(mouseMoved(const QMouseEvent &)),
00105         this, SIGNAL(plotMouseMoved(const QMouseEvent &)));
00106     connect(d_canvas, SIGNAL(mouseReleased(const QMouseEvent &)),
00107         this, SIGNAL(plotMouseReleased(const QMouseEvent &)));
00108 #endif
00109 
00110     updateTabOrder();
00111 
00112     QSizePolicy sp;
00113     sp.setHorData( QSizePolicy::MinimumExpanding );
00114     sp.setVerData( QSizePolicy::MinimumExpanding );
00115     setSizePolicy(sp);
00116 }
00117 
00119 void QwtPlot::initAxes()
00120 {
00121     int axis;
00122 
00123     QFont fscl(fontInfo().family(), 10);
00124     QFont fttl(fontInfo().family(), 12, QFont::Bold);
00125 
00126     for(axis = 0; axis < axisCnt; axis++)
00127     {
00128         d_scale[axis]->setFont(fscl);
00129         d_scale[axis]->setTitleFont(fttl);
00130         d_scale[axis]->setBaselineDist(2);
00131     }
00132 
00133     d_axisEnabled[yLeft] = TRUE;
00134     d_axisEnabled[yRight] = FALSE;
00135     d_axisEnabled[xBottom] = TRUE;
00136     d_axisEnabled[xTop] = FALSE;
00137 
00138     for (axis=0; axis < axisCnt; axis++)
00139     {
00140         d_as[axis].adjust(0.0,1000.0,TRUE);
00141         d_scale[axis]->setScaleDiv(d_as[axis].scaleDiv());
00142     }
00143 }
00144 
00148 bool QwtPlot::event(QEvent *e)
00149 {
00150     bool ok = QFrame::event(e);
00151     switch(e->type())
00152     {
00153 #if 0
00154         case QEvent::ChildInserted:
00155         case QEvent::ChildRemoved:
00156 #endif
00157         case QEvent::LayoutHint:
00158             updateLayout();
00159             break;
00160         default:;
00161     }
00162     return ok;
00163 }
00164 
00169 void QwtPlot::autoRefresh()
00170 {
00171     if (d_autoReplot)
00172         replot();
00173 }
00174 
00189 void QwtPlot::setAutoReplot(bool tf)
00190 {
00191     d_autoReplot = tf;
00192 }
00193 
00197 bool QwtPlot::autoReplot() const
00198 {
00199     return d_autoReplot; 
00200 }
00201 
00206 void QwtPlot::setTitle(const QString &t)
00207 {
00208     d_lblTitle->setText(t);
00209 }
00210 
00215 QString QwtPlot::title() const
00216 {
00217     return d_lblTitle->text();
00218 }
00219 
00220 
00225 void QwtPlot::setTitleFont(const QFont &f)
00226 {
00227     d_lblTitle->setFont(f);
00228 }
00229 
00233 QFont QwtPlot::titleFont() const
00234 {
00235     return d_lblTitle->font();
00236 }
00237 
00241 QwtPlotLayout *QwtPlot::plotLayout()
00242 {
00243     return d_layout;
00244 }
00245 
00249 const QwtPlotLayout *QwtPlot::plotLayout() const
00250 {
00251     return d_layout;
00252 }
00253 
00257 QLabel *QwtPlot::titleLabel()
00258 {
00259     return d_lblTitle;
00260 }
00261 
00265 const QLabel *QwtPlot::titleLabel() const
00266 {
00267     return d_lblTitle;
00268 }
00269 
00274 QwtLegend *QwtPlot::legend()
00275 { 
00276     return d_legend;
00277 }   
00278 
00283 const QwtLegend *QwtPlot::legend() const
00284 { 
00285     return d_legend;
00286 }   
00287 
00288 
00292 QwtPlotCanvas *QwtPlot::canvas()
00293 { 
00294     return d_canvas;
00295 }   
00296 
00300 const QwtPlotCanvas *QwtPlot::canvas() const
00301 { 
00302     return d_canvas;
00303 }
00304 
00310 QSize QwtPlot::sizeHint() const
00311 {
00312     int dw = 0;
00313     int dh = 0;
00314     for ( int axis = 0; axis < axisCnt; axis++ )
00315     {
00316         if ( d_axisEnabled[axis] )
00317         {   
00318             const int niceDist = 40;
00319             const QwtScale *scale = d_scale[axis];
00320             const int majCnt = scale->scaleDraw()->scaleDiv().majCnt();
00321 
00322             if ( axis == yLeft || axis == yRight )
00323             {
00324                 int hDiff = (majCnt - 1) * niceDist 
00325                     - scale->minimumSizeHint().height();
00326                 if ( hDiff > dh )
00327                     dh = hDiff;
00328             }
00329             else
00330             {
00331                 int wDiff = (majCnt - 1) * niceDist 
00332                     - scale->minimumSizeHint().width();
00333                 if ( wDiff > dw )
00334                     dw = wDiff;
00335             }
00336         }
00337     }
00338     return minimumSizeHint() + QSize(dw, dh);
00339 }
00340 
00344 QSize QwtPlot::minimumSizeHint() const
00345 {
00346     QSize hint = d_layout->minimumSizeHint(this);
00347     hint += QSize(2 * frameWidth(), 2 * frameWidth());
00348 
00349     return hint;
00350 }
00351 
00353 void QwtPlot::resizeEvent(QResizeEvent *e)
00354 {
00355     QFrame::resizeEvent(e);
00356     updateLayout();
00357 }
00358 
00369 void QwtPlot::replot()
00370 {
00371     bool doAutoReplot = autoReplot();
00372     setAutoReplot(FALSE);
00373 
00374     updateAxes();
00375 
00376     d_canvas->invalidateCache();
00377     d_canvas->repaint(d_canvas->contentsRect(), FALSE);
00378 
00379     setAutoReplot(doAutoReplot);
00380 }
00381 
00386 void QwtPlot::updateLayout()
00387 {
00388     d_layout->activate(this, contentsRect());
00389 
00390     //
00391     // resize and show the visible widgets
00392     //
00393     if (!d_lblTitle->text().isEmpty())
00394     {
00395         d_lblTitle->setGeometry(d_layout->titleRect());
00396         if (!d_lblTitle->isVisible())
00397             d_lblTitle->show();
00398     }
00399     else
00400         d_lblTitle->hide();
00401 
00402     for (int axis = 0; axis < axisCnt; axis++ )
00403     {
00404         if (d_axisEnabled[axis])
00405         {
00406             d_scale[axis]->setGeometry(d_layout->scaleRect(axis));
00407 
00408             if ( axis == xBottom || axis == xTop )
00409             {
00410                 QRegion r(d_layout->scaleRect(axis));
00411                 if ( d_axisEnabled[yLeft] )
00412                     r = r.subtract(QRegion(d_layout->scaleRect(yLeft)));
00413                 if ( d_axisEnabled[yRight] )
00414                     r = r.subtract(QRegion(d_layout->scaleRect(yRight)));
00415                 r.translate(-d_layout->scaleRect(axis).x(), 
00416                     -d_layout->scaleRect(axis).y());
00417 
00418                 d_scale[axis]->setMask(r);
00419             }
00420             if (!d_scale[axis]->isVisible())
00421                 d_scale[axis]->show();
00422         }
00423         else
00424             d_scale[axis]->hide();
00425     }
00426 
00427     if (d_legend->itemCount() > 0)
00428     {
00429         d_legend->setGeometry(d_layout->legendRect());
00430         d_legend->show();
00431     }
00432     else
00433         d_legend->hide();
00434 
00435     d_canvas->setGeometry(d_layout->canvasRect());
00436 }
00437 
00439 void QwtPlot::updateAxes()
00440 {
00441     int i;
00442     bool resetDone[axisCnt];
00443     for (i = 0; i < axisCnt; i++)
00444         resetDone[i] = FALSE;
00445 
00446     //
00447     //  Adjust autoscalers
00448     //
00449 
00450     QwtPlotCurveIterator itc = curveIterator();
00451     for (const QwtPlotCurve *c = itc.toFirst(); c != 0; c = ++itc )
00452     {
00453         const int xAxis = c->xAxis();
00454         const int yAxis = c->yAxis();
00455 
00456         if ( d_as[xAxis].autoScale() || d_as[yAxis].autoScale() )
00457         {
00458             const QwtDoubleRect rect = c->boundingRect();
00459             if ( rect.isValid() )
00460             {
00461                 if ( d_as[xAxis].autoScale() )
00462                 {
00463                     if ( !resetDone[xAxis] )
00464                     {
00465                         d_as[xAxis].reset();
00466                         resetDone[xAxis] = TRUE;
00467                     }
00468                     d_as[xAxis].adjust(rect.x1(), rect.x2());
00469                 }
00470 
00471                 if ( d_as[yAxis].autoScale() )
00472                 {
00473                     if ( !resetDone[yAxis] )
00474                     {
00475                         d_as[yAxis].reset();
00476                         resetDone[yAxis] = TRUE;
00477                     }
00478                     d_as[yAxis].adjust(rect.y1(), rect.y2());
00479                 }
00480             }
00481         }
00482     }
00483 
00484     // PyQwt adjust the autoscalers also for the markers
00485 
00486     double min[axisCnt];
00487     double max[axisCnt];
00488     for (i = 0; i < axisCnt; i++)
00489     {
00490         min[i] = DBL_MAX;
00491         max[i] = DBL_MIN;
00492     }
00493     
00494     QwtPlotMarkerIterator itm = markerIterator();
00495     for (const QwtPlotMarker *m = itm.toFirst(); m != 0; m = ++itm)
00496     {
00497         const int xAxis = m->xAxis();
00498         const int yAxis = m->yAxis();
00499         const double x = m->xValue();
00500         const double y = m->yValue();
00501 
00502         if (x<min[xAxis])
00503             min[xAxis] = x;
00504         if (x>max[xAxis])
00505             max[xAxis] = x;
00506         if (y<min[yAxis])
00507             min[yAxis] = y;
00508         if (x>max[yAxis])
00509             max[yAxis] = y;
00510     }
00511 
00512     for (i = 0; i < axisCnt; i++)
00513     {
00514         if ( (min[i] != DBL_MAX) && (max[i] != DBL_MIN) )
00515         {
00516             if ( !resetDone[i] && (min[i] != max[i]) )
00517             {
00518                 d_as[i].reset();
00519                 resetDone[i] = TRUE;
00520             }
00521             d_as[i].adjust(min[i], max[i]);
00522         }
00523     }
00524     
00525     //
00526     // Adjust scales
00527     //
00528     for (i = 0; i < axisCnt; i++)
00529     {
00530         d_scale[i]->setScaleDiv(d_as[i].scaleDiv());
00531 
00532         int startDist, endDist;
00533         d_scale[i]->minBorderDist(startDist, endDist);
00534         d_scale[i]->setBorderDist(startDist, endDist);
00535     }
00536 
00537     d_grid->setXDiv(d_as[d_grid->xAxis()].scaleDiv());
00538     d_grid->setYDiv(d_as[d_grid->yAxis()].scaleDiv());
00539 }
00540 
00542 
00543 void QwtPlot::updateTabOrder()
00544 {
00545     // Depending on the position of the legend the 
00546     // tab order will be changed that the canvas is
00547     // next to the last legend item, or directly before
00548     // the first one. The following code seems much too
00549     // complicated but is there a better implementation ?
00550 
00551     if ( d_canvas->focusPolicy() == QWidget::NoFocus || focusData() == NULL )
00552         return;
00553 
00554     // move the cursor to the canvas
00555 
00556     for ( int i = 0; i < focusData()->count(); i++ )
00557     {
00558         if ( focusData()->next() == d_canvas )
00559             break;
00560     }
00561 
00562     const bool canvasFirst = d_layout->legendPosition() == QwtPlot::Bottom ||
00563         d_layout->legendPosition() == QwtPlot::Right;
00564 
00565     for ( int j = 0; j < focusData()->count(); j++ )
00566     {
00567         QWidget *w = canvasFirst ? focusData()->next() : focusData()->prev();
00568 
00569         if ( w->focusPolicy() != QWidget::NoFocus 
00570             && w->parent() && w->parent() == d_legend->contentsWidget() )
00571         {
00572             if ( canvasFirst )
00573             {
00574                 do // go back to last non legend item
00575                 {
00576                     w = focusData()->prev(); // before the first legend item
00577                 } while ( w->focusPolicy() == QWidget::NoFocus );
00578             }
00579 
00580             if ( w != d_canvas )
00581                 setTabOrder(w, d_canvas);
00582             break;
00583         }
00584     }
00585 }
00586 
00588 // \sa QFrame::drawContents
00589 
00590 void QwtPlot::drawContents( QPainter * )
00591 {
00592     // We must erase the region that is not
00593     // occupied by our children
00594     QRegion cr( contentsRect() );
00595     cr = cr.subtract( childrenRegion() );
00596     erase( cr );
00597 }
00598 
00609 void QwtPlot::drawCanvas(QPainter *painter)
00610 {
00611     QwtArray<QwtDiMap> map(axisCnt);
00612     for ( int axis = 0; axis < axisCnt; axis++ )
00613         map[axis] = canvasMap(axis);
00614 
00615     drawCanvasItems(painter, 
00616         d_canvas->contentsRect(), map, QwtPlotPrintFilter());
00617 }
00618 
00627 void QwtPlot::drawCanvasItems(QPainter *painter, const QRect &rect, 
00628         const QwtArray<QwtDiMap> &map, const QwtPlotPrintFilter &pfilter) const
00629 {
00630     //
00631     // draw grid
00632     //
00633     if ( pfilter.options() & QwtPlotPrintFilter::PrintGrid )
00634     {
00635         if ( d_grid->enabled() )
00636         {
00637             d_grid->draw(painter, rect, 
00638                 map[d_grid->xAxis()], map[d_grid->yAxis()]);
00639         }
00640     }
00641 
00642     //
00643     // draw curves
00644     //
00645     QwtPlotCurveIterator itc = curveIterator();
00646     for (QwtPlotCurve *curve = itc.toFirst(); curve != 0; curve = ++itc )
00647     {
00648         if ( curve->enabled() )
00649         {
00650             curve->draw(painter, 
00651                 map[curve->xAxis()], map[curve->yAxis()]);
00652         }
00653     }
00654 
00655     //
00656     // draw markers
00657     //
00658     QwtPlotMarkerIterator itm = markerIterator();
00659     for (QwtPlotMarker *marker = itm.toFirst(); marker != 0; marker = ++itm )
00660     {
00661         if ( marker->enabled() )
00662         {
00663             marker->draw(painter,
00664                 map[marker->xAxis()].transform(marker->xValue()),
00665                 map[marker->yAxis()].transform(marker->yValue()),
00666                 rect);
00667         }
00668     }
00669 }
00670 
00683 void QwtPlot::drawCurve(long key, int from, int to)
00684 {
00685     QwtPlotCurve *curve = d_curves->find(key);
00686     if ( !curve )
00687         return;
00688 
00689     QPainter p(canvas());
00690 
00691     p.setClipping(TRUE);
00692     p.setClipRect(canvas()->contentsRect());
00693 
00694     curve->draw(&p,
00695         canvasMap(curve->xAxis()), canvasMap(curve->yAxis()),
00696         from, to);
00697 
00698     if ( canvas()->cacheMode() && canvas()->cache())
00699     {
00700         QPainter cachePainter(canvas()->cache());
00701         cachePainter.translate(-canvas()->contentsRect().x(),
00702             -canvas()->contentsRect().y());
00703 
00704         curve->draw(&cachePainter,
00705             canvasMap(curve->xAxis()), canvasMap(curve->yAxis()),
00706             from, to);
00707     }
00708 }
00709 
00717 QwtDiMap QwtPlot::canvasMap(int axis) const
00718 {
00719     QwtDiMap map;
00720     if ( !d_canvas )
00721         return map;
00722 
00723     const QwtScaleDiv &sd = d_as[axis].scaleDiv();
00724     map.setDblRange(sd.lBound(), sd.hBound(), sd.logScale());
00725 
00726     if ( axisEnabled(axis) )
00727     {
00728         const QwtScale *s = d_scale[axis];
00729         if ( axis == yLeft || axis == yRight )
00730         {
00731             int y = s->y() + s->startBorderDist() - d_canvas->y();
00732             int h = s->height() - s->startBorderDist() - s->endBorderDist();
00733             map.setIntRange(y + h - 1, y);
00734         }
00735         else
00736         {
00737             int x = s->x() + s->startBorderDist() - d_canvas->x();
00738             int w = s->width() - s->startBorderDist() - s->endBorderDist();
00739             map.setIntRange(x, x + w - 1);
00740         }
00741     }
00742     else
00743     {
00744         const int margin = plotLayout()->canvasMargin(axis);
00745 
00746         const QRect &canvasRect = d_canvas->contentsRect();
00747         if ( axis == yLeft || axis == yRight )
00748         {
00749             map.setIntRange(canvasRect.bottom() - margin, 
00750                 canvasRect.top() + margin);
00751         }
00752         else
00753         {
00754             map.setIntRange(canvasRect.left() + margin, 
00755                 canvasRect.right() - margin);
00756         }
00757     }
00758     return map;
00759 }
00760 
00768 void QwtPlot::setMargin(int margin)
00769 {
00770     if ( margin < 0 )
00771         margin = 0;
00772 
00773     if ( margin != d_layout->margin() )
00774     {
00775         d_layout->setMargin(margin);
00776         updateLayout();
00777     }
00778 }
00779 
00784 int QwtPlot::margin() const
00785 {
00786     return d_layout->margin();
00787 }
00788 
00797 void QwtPlot::setCanvasBackground(const QColor &c)
00798 {
00799     QPalette p = d_canvas->palette();
00800 
00801     for ( int i = 0; i < QPalette::NColorGroups; i++ )
00802         p.setColor((QPalette::ColorGroup)i, QColorGroup::Background, c);
00803 
00804     canvas()->setPalette(p);
00805 }
00806 
00813 const QColor & QwtPlot::canvasBackground() const
00814 {
00815     return canvas()->palette().color(
00816         QPalette::Normal, QColorGroup::Background);
00817 }
00818 
00825 void QwtPlot::setCanvasLineWidth(int w)
00826 {
00827     canvas()->setLineWidth(w);
00828 }
00829  
00835 int QwtPlot::canvasLineWidth() const
00836 { 
00837     return canvas()->lineWidth();
00838 }
00839 
00840 #ifndef QWT_NO_COMPAT
00841 
00856 void QwtPlot::enableOutline(bool tf)
00857 {
00858     d_canvas->enableOutline(tf);
00859 }
00860 
00894 void QwtPlot::setOutlineStyle(Qwt::Shape os)
00895 {
00896     d_canvas->setOutlineStyle(os);
00897 }
00898 
00907 void QwtPlot::setOutlinePen(const QPen &pn)
00908 {
00909     d_canvas->setOutlinePen(pn);
00910 }
00911 
00918 bool QwtPlot::outlineEnabled() const
00919 {
00920      return d_canvas->outlineEnabled();
00921 }
00922 
00929 const QPen & QwtPlot::outlinePen() const
00930 {
00931     return d_canvas->outlinePen();
00932 }
00933 
00942 Qwt::Shape QwtPlot::outlineStyle() const
00943 {
00944     return d_canvas->outlineStyle();
00945 }
00946 
00947 #endif // ! QWT_NO_COMPAT
00948 
00953 bool QwtPlot::axisValid(int axis)
00954 {
00955     return ((axis >= QwtPlot::yLeft) && (axis < QwtPlot::axisCnt));
00956 }
00957 
00963 void QwtPlot::lgdClicked()
00964 {
00965     if ( sender()->isWidgetType() )
00966     {
00967         long key = d_legend->key((QWidget *)sender());
00968         if ( key >= 0 )
00969             emit legendClicked(key);
00970     }
00971 }
00972 
00974 void QwtPlot::clear()
00975 {
00976     d_legend->clear();
00977     d_curves->clear();
00978     d_markers->clear();
00979 }
00980 
00981 
00983 void QwtPlot::removeCurves()
00984 {
00985     d_curves->clear();
00986     d_legend->clear();
00987     autoRefresh();
00988 }
00989 
00991 void QwtPlot::removeMarkers()
00992 {
00993     d_markers->clear();
00994     autoRefresh();
00995 }
00996 
01007 void QwtPlot::setAutoLegend(bool tf)
01008 {
01009     d_autoLegend = tf;
01010 }
01011 
01015 bool QwtPlot::autoLegend() const
01016 {
01017     return d_autoLegend;
01018 }
01019 
01020 
01030 void QwtPlot::enableLegend(bool enable, long curveKey)
01031 {
01032     bool isUpdateEnabled = d_legend->isUpdatesEnabled();
01033     d_legend->setUpdatesEnabled(FALSE);
01034 
01035     if ( curveKey < 0 ) // legends for all curves
01036     {
01037         if ( enable )
01038         {
01039             if ( d_legend->itemCount() < d_curves->count() )
01040             {
01041                 // not all curves have a legend
01042 
01043                 d_legend->clear();
01044 
01045                 QwtPlotCurveIterator itc = curveIterator();
01046                 for ( const QwtPlotCurve *curve = itc.toFirst();
01047                     curve != 0; curve = ++itc )
01048                 {
01049                     insertLegendItem(itc.currentKey());
01050                 }
01051             }
01052         }
01053         else
01054         {
01055             d_legend->clear();
01056         }
01057     }
01058     else
01059     {
01060         QWidget *legendItem = d_legend->findItem(curveKey);
01061         if ( enable )
01062         {
01063             if ( d_curves->find(curveKey) && !legendItem )
01064                 insertLegendItem(curveKey);
01065         }
01066         else
01067             delete legendItem;
01068     }
01069 
01070     d_legend->setUpdatesEnabled(isUpdateEnabled);
01071     updateLayout();
01072 }
01073 
01079 bool QwtPlot::legendEnabled(long curveKey) const
01080 {
01081     return d_legend->findItem(curveKey) != 0;
01082 }
01083 
01102 void QwtPlot::setLegendPosition(QwtPlot::Position pos, double ratio)
01103 {
01104     if (pos != d_layout->legendPosition())
01105     {
01106         d_layout->setLegendPosition(pos, ratio);
01107 
01108         QLayout *l = d_legend->contentsWidget()->layout();
01109         if ( l && l->inherits("QwtDynGridLayout") )
01110         {
01111             QwtDynGridLayout *tl = (QwtDynGridLayout *)l;
01112             if ( d_layout->legendPosition() == QwtPlot::Top ||
01113                 d_layout->legendPosition() == QwtPlot::Bottom )
01114             {
01115                 tl->setMaxCols(0); // unlimited
01116             }
01117             else
01118                 tl->setMaxCols(1); // 1 column: align vertical
01119         }
01120 
01121         updateLayout();
01122         updateTabOrder();
01123     }
01124 }
01125 
01138 void QwtPlot::setLegendPosition(QwtPlot::Position pos)
01139 {
01140     setLegendPosition(pos, 0.0);
01141 }
01142 
01147 QwtPlot::Position QwtPlot::legendPosition() const
01148 {
01149     return d_layout->legendPosition();
01150 }
01151 
01152 #ifndef QWT_NO_COMPAT
01153 
01173 void QwtPlot::setLegendPos(int pos, double ratio)
01174 {
01175     setLegendPosition(QwtPlot::Position(pos), ratio);
01176 }
01177 
01183 int QwtPlot::legendPos() const
01184 {
01185     return d_layout->legendPosition();
01186 }
01187 
01188 #endif // !QWT_NO_COMPAT
01189 
01194 void QwtPlot::setLegendFont(const QFont &f)
01195 {
01196     d_legend->setFont(f);
01197     if (d_legend->isVisible())
01198         updateLayout();
01199 }
01200 
01205 void QwtPlot::setLegendFrameStyle(int st)
01206 {
01207     d_legend->setFrameStyle(st);
01208     updateLayout();
01209 }
01210 
01214 int QwtPlot::legendFrameStyle() const
01215 {
01216     return d_legend->frameStyle();
01217 }
01218 
01222 const QFont QwtPlot::legendFont() const
01223 {
01224     return d_legend->font();
01225 }
01226 
01233 void QwtPlot::setLegendDisplayPolicy(
01234     QwtLegend::LegendDisplayPolicy policy, int mode)
01235 {
01236     d_legend->setDisplayPolicy(policy, mode);
01237     
01238     for (QwtPlotCurveIterator iter=curveIterator(); iter.current(); ++iter)
01239         updateLegendItem(iter.currentKey());
01240 }
01241 
01242 // Local Variables:
01243 // mode: C++
01244 // c-file-style: "stroustrup"
01245 // indent-tabs-mode: nil
01246 // End:

Generated on Sun Nov 21 11:12:43 2004 for Qwt User's Guide by doxygen 1.3.5