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

qwt_plot_marker.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 
00015 QwtPlotMarkerIterator QwtPlot::markerIterator() const
00016 {
00017     return QwtPlotMarkerIterator(*d_markers);
00018 }
00019 
00027 long QwtPlot::closestMarker(int xpos, int ypos, int &dist) const
00028 {
00029     QwtDiMap map[axisCnt];
00030     for ( int axis = 0; axis < axisCnt; axis++ )
00031         map[axis] = canvasMap(axis);
00032 
00033     long rv = 0;
00034     double dmin = 1.0e10;
00035     
00036     QwtPlotMarkerIterator itm = markerIterator();
00037     for (QwtPlotMarker *m = itm.toFirst(); m != 0; m = ++itm )
00038     {
00039         double cx = map[m->xAxis()].xTransform(m->xValue());
00040         double cy = map[m->yAxis()].xTransform(m->yValue());
00041 
00042         if (m->lineStyle() == QwtMarker::HLine)
00043         {
00044             if (m->symbol().style() == QwtSymbol::None)
00045                cx = double(xpos);
00046         }
00047         else if (m->lineStyle() == QwtMarker::VLine)
00048         {
00049             if (m->symbol().style() == QwtSymbol::None)
00050                cy = double(ypos);
00051         }
00052         
00053         double f = qwtSqr(cx - double(xpos)) + qwtSqr(cy - double(ypos));
00054         if (f < dmin)
00055         {
00056             dmin = f;
00057             rv = itm.currentKey();
00058         }
00059     }
00060 
00061     dist = int(sqrt(dmin));
00062     return rv;
00063 }
00064 
00066 long QwtPlot::newMarkerKey()
00067 {
00068     long newkey = d_markers->count() + 1;
00069 
00070     if (newkey > 1)                     // count > 0
00071     {
00072         if (d_markers->find(newkey))    // key count+1 exists => there must be a
00073                                         // free key <= count
00074         {
00075             // find the first available key <= count
00076             newkey = 1;
00077             while (newkey <= long(d_markers->count()))
00078             {
00079                 if (d_markers->find(newkey))
00080                    newkey++;
00081                 else
00082                    break;
00083             }
00084 
00085             // This can't happen. Just paranoia.
00086             if (newkey > long(d_markers->count()))
00087             {
00088                 while (!d_markers->find(newkey))
00089                 {
00090                     newkey++;
00091                     if (newkey > 10000) // prevent infinite loop
00092                     {
00093                         newkey = 0;
00094                         break;
00095                     }
00096                 }
00097             }
00098         }
00099     }
00100     return newkey;
00101     
00102 }
00103 
00111 long QwtPlot::insertLineMarker(const QString &label, int axis)
00112 {
00113     QwtMarker::LineStyle lineStyle = QwtMarker::NoLine;
00114     int xAxis = QwtPlot::xBottom;
00115     int yAxis = QwtPlot::yLeft;
00116 
00117     switch(axis)
00118     {
00119         case yLeft:
00120         case yRight:
00121             yAxis = axis;
00122             lineStyle = QwtMarker::HLine;
00123             break;
00124         case xTop:
00125         case xBottom:
00126             xAxis = axis;
00127             lineStyle = QwtMarker::VLine;
00128             break;
00129     }
00130 
00131     QwtPlotMarker *marker = new QwtPlotMarker(this);
00132     if ( marker == 0 )
00133         return 0;
00134 
00135     marker->setAxis(xAxis, yAxis);
00136     marker->setLabel(label);
00137     marker->setLineStyle(lineStyle);
00138     marker->setLabelAlignment(AlignRight|AlignTop);
00139 
00140     long key = insertMarker(marker);
00141     if ( key == 0 )
00142         delete marker;
00143 
00144     return key;
00145 }
00146 
00154 long QwtPlot::insertMarker(const QString &label, int xAxis, int yAxis)
00155 {
00156     QwtPlotMarker *marker = new QwtPlotMarker(this);
00157     if ( marker == 0 )
00158         return 0;
00159 
00160     marker->setAxis(xAxis, yAxis);
00161     marker->setLabel(label);
00162 
00163     long key = insertMarker(marker);
00164     if ( key == 0 )
00165         delete marker;
00166 
00167     return key;
00168 }
00169 
00175 long QwtPlot::insertMarker(QwtPlotMarker *marker)
00176 {
00177     if ( marker == 0 )
00178         return 0;
00179 
00180     long key = newMarkerKey();
00181     if ( key == 0 )
00182         return 0;
00183 
00184     marker->reparent(this);
00185     d_markers->insert(key, marker);
00186 
00187     autoRefresh();
00188 
00189     return key;
00190 }
00191 
00198 QwtPlotMarker *QwtPlot::marker(long key)
00199 {
00200     return d_markers->find(key);
00201 }
00202 
00209 const QwtPlotMarker *QwtPlot::marker(long key) const
00210 {
00211     return d_markers->find(key);
00212 }
00213 
00217 QwtArray<long> QwtPlot::markerKeys() const
00218 {
00219     QwtArray<long> keys(d_markers->count());
00220 
00221     int i = 0;
00222 
00223     QwtPlotMarkerIterator itm = markerIterator();
00224     for (const QwtPlotMarker *m = itm.toFirst(); m != 0; m = ++itm, i++ )
00225         keys[i] = itm.currentKey();
00226 
00227     return keys;
00228 }
00229 
00233 QFont QwtPlot::markerFont(long key) const
00234 {
00235     QwtPlotMarker *m = d_markers->find(key);
00236     if (m)
00237         return m->font();
00238     else
00239         return QFont();
00240 }
00241 
00246 const QString& QwtPlot::markerLabel(long key) const
00247 {
00248     QwtPlotMarker *m = d_markers->find(key);
00249     if (m)
00250         return m->label();
00251     else
00252         return QString::null;
00253 }
00254 
00259 int QwtPlot::markerLabelAlign(long key) const
00260 {
00261     QwtPlotMarker *m = d_markers->find(key);
00262     if (m)
00263         return m->labelAlignment();
00264     else
00265         return 0;
00266 }
00267 
00272 QPen QwtPlot::markerLabelPen(long key) const
00273 {
00274     QwtPlotMarker *m = d_markers->find(key);
00275     if (m)
00276         return m->labelPen();
00277     else
00278         return QPen();
00279     
00280 }
00281 
00286 QPen QwtPlot::markerLinePen(long key) const 
00287 {
00288     QwtPlotMarker *m = d_markers->find(key);
00289     if (m)
00290         return m->linePen();
00291     else
00292         return QPen();
00293     
00294 }
00295 
00300 QwtMarker::LineStyle QwtPlot::markerLineStyle(long key) const
00301 {
00302     QwtPlotMarker *m = d_markers->find(key);
00303     if (m)
00304         return m->lineStyle();
00305     else
00306         return QwtMarker::NoLine;
00307 }
00308 
00316 void QwtPlot::markerPos(long key, double &mx, double &my ) const
00317 {
00318     QwtPlotMarker *m = d_markers->find(key);
00319     if (m)
00320     {
00321         mx = m->xValue();
00322         my = m->yValue();
00323     }
00324     else
00325     {
00326         mx = 0;
00327         my = 0;
00328     }
00329 }
00330 
00335 QwtSymbol QwtPlot::markerSymbol(long key) const
00336 {
00337     QwtPlotMarker *m = d_markers->find(key);
00338     if (m)
00339         return m->symbol();
00340     else
00341         return QwtSymbol();
00342 }
00343 
00344 
00349 int QwtPlot::markerXAxis(long key) const
00350 {
00351     QwtPlotMarker *m = d_markers->find(key);
00352     if (m)
00353         return m->xAxis();
00354     else
00355         return -1;
00356     
00357 }
00358 
00359 
00364 int QwtPlot::markerYAxis(long key) const
00365 {
00366     QwtPlotMarker *m = d_markers->find(key);
00367     if (m)
00368         return m->yAxis();
00369     else
00370         return -1;
00371     
00372 }
00373 
00378 bool QwtPlot::removeMarker(long key)
00379 {
00380     if (d_markers->remove(key))
00381     {
00382         autoRefresh();
00383         return TRUE;
00384     }
00385     else
00386        return FALSE;
00387 }
00388 
00389 
00394 bool QwtPlot::setMarkerXAxis(long key, int axis)
00395 {
00396     QwtPlotMarker *m;
00397     if ((m = d_markers->find(key)))
00398     {
00399         m->setXAxis(axis);
00400         return TRUE;
00401     }
00402     else
00403        return FALSE;
00404 }
00405 
00412 bool QwtPlot::setMarkerYAxis(long key, int axis)
00413 {
00414     QwtPlotMarker *m;
00415     if ((m = d_markers->find(key)))
00416     {
00417         m->setYAxis(axis);
00418         return TRUE;
00419     }
00420     else
00421        return FALSE;
00422 }
00423 
00430 bool QwtPlot::setMarkerFont(long key, const QFont &f)
00431 {
00432     int rv = FALSE;
00433     
00434     QwtPlotMarker *m;
00435     if ((m = d_markers->find(key)))
00436     {
00437         m->setFont(f);
00438         rv = TRUE;
00439     }
00440     return rv;
00441 }
00442 
00449 bool QwtPlot::setMarkerLinePen(long key, const QPen &p)
00450 {
00451     int rv = FALSE;
00452     
00453     QwtPlotMarker *m;
00454     if ((m = d_markers->find(key)))
00455     {
00456         m->setLinePen(p);
00457         rv = TRUE;
00458     }
00459     return rv;
00460 
00461 }
00462 
00463 
00471 bool QwtPlot::setMarkerLineStyle(long key, QwtMarker::LineStyle st)
00472 {
00473     int rv = FALSE;
00474     QwtPlotMarker *m;
00475     if ((m = d_markers->find(key)))
00476     {
00477         m->setLineStyle(st);
00478         rv = TRUE;
00479     }
00480     return rv;
00481 }
00482 
00489 bool QwtPlot::setMarkerPen(long key, const QPen &p)
00490 {
00491     int rv = FALSE;
00492     
00493     QwtPlotMarker *m;
00494     if ((m = d_markers->find(key)))
00495     {
00496         m->setLinePen(p);
00497         m->setLabelPen(p);
00498         rv = TRUE;
00499     }
00500     return rv;
00501 }
00502 
00503 
00511 bool QwtPlot::setMarkerPos(long key, double xval, double yval)
00512 {
00513     int rv = FALSE;
00514     
00515     QwtPlotMarker *m;
00516     if ((m = d_markers->find(key)))
00517     {
00518         m->setXValue(xval);
00519         m->setYValue(yval);
00520         rv = TRUE;
00521     }
00522     return rv;
00523 }
00524 
00531 bool QwtPlot::setMarkerXPos(long key, double val)
00532 {
00533     int rv = FALSE;
00534     
00535     QwtPlotMarker *m;
00536     if ((m = d_markers->find(key)))
00537     {
00538         m->setXValue(val);
00539         rv = TRUE;
00540     }
00541     return rv;
00542 }
00543 
00550 bool QwtPlot::setMarkerYPos(long key, double val)
00551 {
00552     int rv = FALSE;
00553     
00554     QwtPlotMarker *m;
00555     if ((m = d_markers->find(key)))
00556     {
00557         m->setYValue(val);
00558         rv = TRUE;
00559     }
00560     return rv;
00561 }
00562 
00569 bool QwtPlot::setMarkerSymbol(long key, const QwtSymbol &s)
00570 {
00571     int rv = FALSE;
00572     QwtPlotMarker *m;
00573     if ((m = d_markers->find(key)))
00574     {
00575         m->setSymbol(s);
00576         rv = TRUE;
00577     }
00578     return rv;
00579 }
00580 
00587 bool QwtPlot::setMarkerLabel(long key, const QString &txt)
00588 {
00589     int rv = FALSE;
00590     QwtPlotMarker *m;
00591     if ((m = d_markers->find(key)))
00592     {
00593         m->setLabel(txt);
00594         rv = TRUE;
00595     }
00596     return rv;
00597 }
00598 
00610 bool QwtPlot::setMarkerLabelAlign(long key, int align)
00611 {
00612     int rv = FALSE;
00613     QwtPlotMarker *m;
00614     if ((m = d_markers->find(key)))
00615     {
00616         m->setLabelAlignment(align);
00617         rv = TRUE;
00618     }
00619     return rv;
00620 }
00621 
00628 bool QwtPlot::setMarkerLabelPen(long key, const QPen &p)
00629 {
00630     int rv = FALSE;
00631     QwtPlotMarker *m;
00632     if ((m = d_markers->find(key)))
00633     {
00634         m->setLabelPen(p);
00635         rv = TRUE;
00636     }
00637     return rv;
00638 }
00639 
00640 
00641 
00642 

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