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

# qwt_plot_curve.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 #include "qwt_plot.h"
    00011 #include "qwt_plot_dict.h"
    00012 #include "qwt_math.h"
    00013 #include "qwt_legend.h"
    00014
    00016 QwtPlotCurveIterator QwtPlot::curveIterator() const
    00017 {
    00018     return QwtPlotCurveIterator(*d_curves);
    00019 }
    00020
    00030 long QwtPlot::closestCurve(int xpos, int ypos, int &dist) const
    00031 {
    00032     double x,y;
    00033     int index;
    00034     return closestCurve(xpos, ypos, dist, x,y, index);
    00035 }
    00036
    00050 long QwtPlot::closestCurve(int xpos, int ypos, int &dist, double &xval,
    00051                            double &yval, int &index) const
    00052 {
    00053     QwtDiMap map[axisCnt];
    00054     for ( int axis = 0; axis < axisCnt; axis++ )
    00055         map[axis] = canvasMap(axis);
    00056
    00057     long rv = 0;
    00058     double dmin = 1.0e10;
    00059
    00060     QwtPlotCurveIterator itc = curveIterator();
    00061     for (QwtPlotCurve *c = itc.toFirst(); c != 0; c = ++itc )
    00062     {
    00063         for (int i=0; i<c->dataSize(); i++)
    00064         {
    00065             double cx = map[c->xAxis()].xTransform(c->x(i)) - double(xpos);
    00066             double cy = map[c->yAxis()].xTransform(c->y(i)) - double(ypos);
    00067
    00068             double f = qwtSqr(cx) + qwtSqr(cy);
    00069             if (f < dmin)
    00070             {
    00071                 dmin = f;
    00072                 rv = itc.currentKey();
    00073                 xval = c->x(i);
    00074                 yval = c->y(i);
    00075                 index = i;
    00076             }
    00077         }
    00078     }
    00079
    00080     dist = int(sqrt(dmin));
    00081     return rv;
    00082 }
    00083
    00084
    00085
    00091 int QwtPlot::curveStyle(long key) const
    00092 {
    00093     QwtPlotCurve *c = d_curves->find(key);
    00094     return c ? c->style() : 0;
    00095 }
    00096
    00103 QwtSymbol QwtPlot::curveSymbol(long key) const
    00104 {
    00105     QwtPlotCurve *c = d_curves->find(key);
    00106     return c ? c->symbol() : QwtSymbol();
    00107 }
    00108
    00113 QPen QwtPlot::curvePen(long key) const
    00114 {
    00115     QwtPlotCurve *c = d_curves->find(key);
    00116     return c ? c->pen() : QPen();
    00117 }
    00118
    00124 QBrush QwtPlot::curveBrush(long key) const
    00125 {
    00126     QwtPlotCurve *c = d_curves->find(key);
    00127     return c ? c->brush() : QBrush();
    00128 }
    00133 int QwtPlot::curveOptions(long key) const
    00134 {
    00135     QwtPlotCurve *c = d_curves->find(key);
    00136     return c ? c->options() : 0;
    00137 }
    00138
    00143 int QwtPlot::curveSplineSize(long key) const
    00144 {
    00145     QwtPlotCurve *c = d_curves->find(key);
    00146     return c ? c->splineSize() : 0;
    00147 }
    00148
    00153 QString QwtPlot::curveTitle(long key) const
    00154 {
    00155     QwtPlotCurve *c = d_curves->find(key);
    00156     return c ? c->title() : QString::null;
    00157 }
    00158
    00162 QwtArray<long> QwtPlot::curveKeys() const
    00163 {
    00164     QwtArray<long> keys(d_curves->count());
    00165
    00166     int i = 0;
    00167
    00168     QwtPlotCurveIterator itc = curveIterator();
    00169     for (const QwtPlotCurve *c = itc.toFirst(); c != 0; c = ++itc, i++ )
    00170         keys[i] = itc.currentKey();
    00171
    00172     return keys;
    00173 }
    00174
    00180 int QwtPlot::curveXAxis(long key) const
    00181 {
    00182     QwtPlotCurve *c = d_curves->find(key);
    00183     return c ? c->xAxis() : -1;
    00184 }
    00185
    00186
    00192 int QwtPlot::curveYAxis(long key) const
    00193 {
    00194     QwtPlotCurve *c = d_curves->find(key);
    00195     return c ? c->yAxis() : -1;
    00196 }
    00197
    00198
    00203 long QwtPlot::newCurveKey()
    00204 {
    00205     long newkey = d_curves->count() + 1;
    00206
    00207     if (newkey > 1)                     // size > 0: check if key exists
    00208     {
    00209         if (d_curves->find(newkey))     // key size+1 exists => there must be a
    00210                                         // free key <= size
    00211         {
    00212             // find the first available key <= size
    00213             newkey = 1;
    00214             while (newkey <= long(d_curves->count()))
    00215             {
    00216                 if (d_curves->find(newkey))
    00217                    newkey++;
    00218                 else
    00219                    break;
    00220             }
    00221
    00222             // This can't happen. Just paranoia.
    00223             if (newkey > long(d_curves->count()))
    00224             {
    00225                 while (!d_curves->find(newkey))
    00226                 {
    00227                     newkey++;
    00228                     if (newkey > 10000) // prevent infinite loop
    00229                     {
    00230                         newkey = 0;
    00231                         break;
    00232                     }
    00233                 }
    00234             }
    00235         }
    00236     }
    00237     return newkey;
    00238 }
    00239
    00247 long QwtPlot::insertCurve(QwtPlotCurve *curve)
    00248 {
    00249     if (curve == 0)
    00250         return 0;
    00251
    00252     long key = newCurveKey();
    00253     if (key == 0)
    00254         return 0;
    00255
    00256     curve->reparent(this);
    00257
    00258     d_curves->insert(key, curve);
    00259     if (d_autoLegend)
    00260     {
    00261         insertLegendItem(key);
    00262         updateLayout();
    00263     }
    00264
    00265     return key;
    00266 }
    00267
    00276 long QwtPlot::insertCurve(const QString &title, int xAxis, int yAxis)
    00277 {
    00278     QwtPlotCurve *curve = new QwtPlotCurve(this);
    00279     if (!curve)
    00280         return 0;
    00281
    00282     curve->setAxis(xAxis, yAxis);
    00283     curve->setTitle(title);
    00284
    00285     long key = insertCurve(curve);
    00286     if ( key == 0 )
    00287         delete curve;
    00288
    00289     return key;
    00290 }
    00291
    00297 QwtPlotCurve *QwtPlot::curve(long key)
    00298 {
    00299     return d_curves->find(key);
    00300 }
    00301
    00307 const QwtPlotCurve *QwtPlot::curve(long key) const
    00308 {
    00309     return d_curves->find(key);
    00310 }
    00311
    00312
    00317 bool QwtPlot::removeCurve(long key)
    00318 {
    00319     bool ok = d_curves->remove(key);
    00320     if ( !ok )
    00321         return FALSE;
    00322
    00323     QWidget *item = d_legend->findItem(key);
    00324     if ( item )
    00325     {
    00326         delete item;
    00327         updateLayout();
    00328     }
    00329
    00330     autoRefresh();
    00331     return TRUE;
    00332 }
    00333
    00340 bool QwtPlot::setCurvePen(long key, const QPen &pen)
    00341 {
    00342     QwtPlotCurve *c = d_curves->find(key);
    00343     if ( !c )
    00344         return FALSE;
    00345
    00346     c->setPen(pen);
    00347     updateLegendItem(key);
    00348
    00349     return TRUE;
    00350 }
    00351
    00362 bool QwtPlot::setCurveBrush(long key, const QBrush &brush)
    00363 {
    00364     QwtPlotCurve *c = d_curves->find(key);
    00365     if ( !c )
    00366         return FALSE;
    00367
    00368     c->setBrush(brush);
    00369     updateLegendItem(key);
    00370
    00371     return TRUE;
    00372 }
    00373
    00380 bool QwtPlot::setCurveSymbol(long key, const QwtSymbol &symbol)
    00381 {
    00382     QwtPlotCurve *c = d_curves->find(key);
    00383     if ( !c )
    00384         return FALSE;
    00385
    00386     c->setSymbol(symbol);
    00387     updateLegendItem(key);
    00388
    00389     return TRUE;
    00390 }
    00391
    00407 bool QwtPlot::setCurveRawData(long key,
    00408     const double *xData, const double *yData, int size)
    00409 {
    00410     QwtPlotCurve *c = d_curves->find(key);
    00411     if ( !c )
    00412         return FALSE;
    00413
    00414     c->setRawData(xData, yData, size);
    00415     return TRUE;
    00416 }
    00417
    00424 bool QwtPlot::setCurveTitle(long key, const QString &title)
    00425 {
    00426     QwtPlotCurve *c = d_curves->find(key);
    00427     if ( !c )
    00428         return FALSE;
    00429
    00430     c->setTitle(title);
    00431     updateLegendItem(key);
    00432
    00433     return TRUE;
    00434 }
    00435
    00449 bool QwtPlot::setCurveData(long key,
    00450     const double *xData, const double *yData, int size)
    00451 {
    00452     QwtPlotCurve *c = d_curves->find(key);
    00453     if ( !c )
    00454         return FALSE;
    00455
    00456     c->setData(xData, yData, size);
    00457     return TRUE;
    00458 }
    00459
    00468 bool QwtPlot::setCurveData(long key,
    00469     const QwtArray<double> &xData, const QwtArray<double> &yData)
    00470 {
    00471     QwtPlotCurve *c = d_curves->find(key);
    00472     if ( !c )
    00473         return FALSE;
    00474
    00475     c->setData(xData, yData);
    00476     return TRUE;
    00477 }
    00478
    00486 bool QwtPlot::setCurveData(long key, const QwtArray<QwtDoublePoint> &data)
    00487 {
    00488     QwtPlotCurve *c = d_curves->find(key);
    00489     if ( !c )
    00490         return FALSE;
    00491
    00492     c->setData(data);
    00493     return TRUE;
    00494 }
    00495
    00503 bool QwtPlot::setCurveData(long key, const QwtData &data)
    00504 {
    00505     QwtPlotCurve *c = d_curves->find(key);
    00506     if ( !c )
    00507         return FALSE;
    00508
    00509     c->setData(data);
    00510     return TRUE;
    00511 }
    00512
    00521 bool QwtPlot::setCurveStyle(long key, int s, int options)
    00522 {
    00523     QwtPlotCurve *c = d_curves->find(key);
    00524     if ( !c )
    00525         return FALSE;
    00526
    00527     c->setStyle(s, options);
    00528     updateLegendItem(key);
    00529
    00530     return TRUE;
    00531 }
    00532
    00540 bool QwtPlot::setCurveOptions(long key, int opt)
    00541 {
    00542     QwtPlotCurve *c = d_curves->find(key);
    00543     if ( !c )
    00544         return FALSE;
    00545
    00546     c->setOptions(opt);
    00547     return TRUE;
    00548 }
    00549
    00556 bool QwtPlot::setCurveSplineSize(long key, int s)
    00557 {
    00558     QwtPlotCurve *c = d_curves->find(key);
    00559     if ( !c )
    00560         return FALSE;
    00561
    00562     c->setSplineSize(s);
    00563     return TRUE;
    00564 }
    00565
    00566
    00573 bool QwtPlot::setCurveXAxis(long key, int axis)
    00574 {
    00575     QwtPlotCurve *c = d_curves->find(key);
    00576     if ( !c )
    00577         return FALSE;
    00578
    00579     c->setXAxis(axis);
    00580     return TRUE;
    00581 }
    00582
    00589 bool QwtPlot::setCurveYAxis(long key, int axis)
    00590 {
    00591     QwtPlotCurve *c = d_curves->find(key);
    00592     if ( !c )
    00593         return FALSE;
    00594
    00595     c->setYAxis(axis);
    00596     return TRUE;
    00597 }
    00598
    00599
    00609 bool QwtPlot::setCurveBaseline(long key, double ref)
    00610 {
    00611     QwtPlotCurve *c = d_curves->find(key);
    00612     if ( !c )
    00613         return FALSE;
    00614
    00615     c->setBaseline(ref);
    00616     return TRUE;
    00617 }
    00618
    00626 double QwtPlot::curveBaseline(long key) const
    00627 {
    00628     double rv = 0.0;
    00629     QwtPlotCurve *c;
    00630     if ((c = d_curves->find(key)))
    00631         rv = c->baseline();
    00632     return rv;
    00633 }
    00634
    00644 void QwtPlot::insertLegendItem(long curveKey)
    00645 {
    00646     if ( d_legend->isReadOnly() )
    00647     {
    00648         QwtLegendLabel *label =
    00649             new QwtLegendLabel(d_legend->contentsWidget());
    00650         d_legend->insertItem(label, curveKey);
    00651     }
    00652     else
    00653     {
    00654         QwtLegendButton *button =
    00655             new QwtLegendButton(d_legend->contentsWidget());
    00656         connect(button, SIGNAL(clicked()), SLOT(lgdClicked()));
    00657
    00658         d_legend->insertItem(button, curveKey);
    00659     }
    00660
    00661     updateLegendItem(curveKey);
    00662 }
    00663
    00669 void QwtPlot::updateLegendItem(long curveKey)
    00670 {
    00671     const QwtPlotCurve *curve = d_curves->find(curveKey);
    00672     if ( !curve )
    00673         return;
    00674
    00675     QWidget *item = d_legend->findItem(curveKey);
    00676     if (item && item->inherits("QwtLegendButton"))
    00677     {
    00678         QwtLegendButton *button = (QwtLegendButton *)item;
    00679
    00680         const bool doUpdate = button->isUpdatesEnabled();
    00681         button->setUpdatesEnabled(FALSE);
    00682
    00683         updateLegendItem(curve, button);
    00684
    00685         button->setUpdatesEnabled(doUpdate);
    00686         button->update();
    00687     }
    00688     if (item && item->inherits("QwtLegendLabel"))
    00689     {
    00690         QwtLegendLabel *label = (QwtLegendLabel *)item;
    00691
    00692         const bool doUpdate = label->isUpdatesEnabled();
    00693         label->setUpdatesEnabled(FALSE);
    00694
    00695         updateLegendItem(curve, label);
    00696
    00697         label->setUpdatesEnabled(doUpdate);
    00698         label->update();
    00699     }
    00700 }
    00701
    00708 void QwtPlot::updateLegendItem(
    00709     const QwtPlotCurve *curve, QwtLegendItem *item)
    00710 {
    00711     if ( !curve || !item )
    00712         return;
    00713
    00714     int policy = d_legend->displayPolicy();
    00715
    00716     if (policy == QwtLegend::Fixed)
    00717     {
    00718         int mode = d_legend->identifierMode();
    00719
    00720         if (mode & QwtLegendButton::ShowLine)
    00721             item->setCurvePen(curve->pen());
    00722
    00723         if (mode & QwtLegendButton::ShowSymbol)
    00724             item->setSymbol(curve->symbol());
    00725
    00726         if (mode & QwtLegendButton::ShowText)
    00727             item->setTitle(curve->title());
    00728         else
    00729             item->setTitle(QString::null);
    00730
    00731         item->setIdentifierMode(mode);
    00732     }
    00733     else if (policy == QwtLegend::Auto)
    00734     {
    00735         int mode = 0;
    00736
    00737         if (QwtCurve::NoCurve != curve->style())
    00738         {
    00739             item->setCurvePen(curve->pen());
    00740             mode |= QwtLegendButton::ShowLine;
    00741         }
    00742         if (QwtSymbol::None != curve->symbol().style())
    00743         {
    00744             item->setSymbol(curve->symbol());
    00745             mode |= QwtLegendButton::ShowSymbol;
    00746         }
    00747         if ( !curve->title().isEmpty() )
    00748         {
    00749             item->setTitle(curve->title());
    00750             mode |= QwtLegendButton::ShowText;
    00751         }
    00752         else
    00753         {
    00754             item->setTitle(QString::null);
    00755         }
    00756         item->setIdentifierMode(mode);
    00757     }
    00758 }
    00759
    00760 // Local Variables:
    00761 // mode: C++
    00762 // c-file-style: "stroustrup"
    00763 // indent-tabs-mode: nil
    00764 // End: