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

qwt_plot_curve.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_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 &s)
00381 {
00382     QwtPlotCurve *c = d_curves->find(key);
00383     if ( !c )
00384         return FALSE;
00385 
00386     c->setSymbol(s);
00387     updateLegendItem(key);
00388 
00389     return TRUE;
00390 }
00391 
00407 bool QwtPlot::setCurveRawData(long key, 
00408     const double *xdat, const double *ydat, int size)
00409 {
00410     QwtPlotCurve *c = d_curves->find(key);
00411     if ( !c )
00412         return FALSE;
00413 
00414     c->setRawData(xdat, ydat, size);
00415     return TRUE;
00416 }
00417 
00423 bool QwtPlot::setCurveTitle(long key, const QString &s)
00424 {
00425     QwtPlotCurve *c = d_curves->find(key);
00426     if ( !c )
00427         return FALSE;
00428 
00429     c->setTitle(s);
00430     updateLegendItem(key);
00431 
00432     return TRUE;
00433 }
00434 
00446 bool QwtPlot::setCurveData(long key, 
00447     const double *xdat, const double *ydat, int size)
00448 {
00449     QwtPlotCurve *c = d_curves->find(key);
00450     if ( !c )
00451         return FALSE;
00452 
00453     c->setData(xdat, ydat, size);
00454     return TRUE;
00455 }
00456     
00465 bool QwtPlot::setCurveStyle(long key, int s, int options)
00466 {
00467     QwtPlotCurve *c = d_curves->find(key);
00468     if ( !c )
00469         return FALSE;
00470 
00471     c->setStyle(s, options);
00472     updateLegendItem(key);
00473 
00474     return TRUE;
00475 }
00476 
00484 bool QwtPlot::setCurveOptions(long key, int opt)
00485 {
00486     QwtPlotCurve *c = d_curves->find(key);
00487     if ( !c )
00488         return FALSE;
00489 
00490     c->setOptions(opt);
00491     return TRUE;
00492 }
00493 
00500 bool QwtPlot::setCurveSplineSize(long key, int s)
00501 {
00502     QwtPlotCurve *c = d_curves->find(key);
00503     if ( !c )
00504         return FALSE;
00505 
00506     c->setSplineSize(s);
00507     return TRUE;
00508 }
00509 
00510 
00516 bool QwtPlot::setCurveXAxis(long key, int axis)
00517 {
00518     QwtPlotCurve *c = d_curves->find(key);
00519     if ( !c )
00520         return FALSE;
00521 
00522     c->setXAxis(axis);
00523     return TRUE;
00524 }
00525 
00531 bool QwtPlot::setCurveYAxis(long key, int axis)
00532 {
00533     QwtPlotCurve *c = d_curves->find(key);
00534     if ( !c )
00535         return FALSE;
00536 
00537     c->setYAxis(axis);
00538     return TRUE;
00539 }
00540 
00541 
00550 bool QwtPlot::setCurveBaseline(long key, double ref)
00551 {
00552     QwtPlotCurve *c = d_curves->find(key);
00553     if ( !c )
00554         return FALSE;
00555 
00556     c->setBaseline(ref);
00557     return TRUE;
00558 }
00559 
00567 double QwtPlot::curveBaseline(long key) const
00568 {
00569     double rv = 0.0;
00570     QwtPlotCurve *c;
00571     if ((c = d_curves->find(key)))
00572         rv = c->baseline();
00573     return rv;
00574 }
00575 
00581 void QwtPlot::insertLegendItem(long curveKey)
00582 {
00583     QwtLegendButton *button = 
00584         new QwtLegendButton(d_legend->contentsWidget());
00585 
00586     connect(button, SIGNAL(clicked()), SLOT(lgdClicked()));
00587     d_legend->insertItem(button, curveKey);
00588 
00589     updateLegendItem(curveKey);
00590 }
00591 
00597 void QwtPlot::updateLegendItem(long curveKey)
00598 {
00599     const QwtPlotCurve *curve = d_curves->find(curveKey);
00600     if ( !curve )
00601         return;
00602 
00603     QWidget *item = d_legend->findItem(curveKey);
00604     if (item && item->inherits("QwtLegendButton")) {
00605         QwtLegendButton *button = (QwtLegendButton *)item;
00606 
00607         const bool doUpdate = button->isUpdatesEnabled();
00608         button->setUpdatesEnabled(FALSE);
00609 
00610   int policy = d_legend->displayPolicy();
00611 
00612   if (policy == QwtLegend::Fixed) {
00613       int mode = d_legend->identifierMode();
00614 
00615       if (mode & QwtLegendButton::ShowLine) {
00616     button->setCurvePen(curve->pen());
00617       }
00618       if (mode & QwtLegendButton::ShowSymbol) {
00619     button->setSymbol(curve->symbol());
00620       }
00621       if (mode & QwtLegendButton::ShowText) {
00622     button->setText(curve->title());
00623       } else {
00624     button->setText(QString::null);
00625       }
00626       button->setIdentifierMode(mode);
00627   } else if (policy == QwtLegend::Auto) {
00628       int mode = 0;
00629 
00630       if (QwtCurve::NoCurve != curve->style()) {
00631     button->setCurvePen(curve->pen());
00632     mode |= QwtLegendButton::ShowLine;
00633       }
00634       if (QwtSymbol::None != curve->symbol().style()) {
00635     button->setSymbol(curve->symbol());
00636     mode |= QwtLegendButton::ShowSymbol;
00637       }
00638       if (curve->title()) {
00639     button->setText(curve->title());
00640     mode |= QwtLegendButton::ShowText;
00641       } else {
00642     button->setText(QString::null);
00643       }
00644       button->setIdentifierMode(mode);
00645   }
00646 
00647   button->setUpdatesEnabled(doUpdate);
00648   button->update();
00649     }
00650 }

Generated on Fri Nov 7 14:11:45 2003 for Qwt Developer's Guide by doxygen 1.3.2