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

# qwt_plot_marker.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 "qwt_plot.h"
    00013 #include "qwt_plot_dict.h"
    00014 #include "qwt_math.h"
    00015
    00017 QwtPlotMarkerIterator QwtPlot::markerIterator() const
    00018 {
    00019     return QwtPlotMarkerIterator(*d_markers);
    00020 }
    00021
    00029 long QwtPlot::closestMarker(int xpos, int ypos, int &dist) const
    00030 {
    00031     QwtDiMap map[axisCnt];
    00032     for ( int axis = 0; axis < axisCnt; axis++ )
    00033         map[axis] = canvasMap(axis);
    00034
    00035     long rv = 0;
    00036     double dmin = 1.0e10;
    00037
    00038     QwtPlotMarkerIterator itm = markerIterator();
    00039     for (QwtPlotMarker *m = itm.toFirst(); m != 0; m = ++itm )
    00040     {
    00041         double cx = map[m->xAxis()].xTransform(m->xValue());
    00042         double cy = map[m->yAxis()].xTransform(m->yValue());
    00043
    00044         if (m->lineStyle() == QwtMarker::HLine)
    00045         {
    00046             if (m->symbol().style() == QwtSymbol::None)
    00047                cx = double(xpos);
    00048         }
    00049         else if (m->lineStyle() == QwtMarker::VLine)
    00050         {
    00051             if (m->symbol().style() == QwtSymbol::None)
    00052                cy = double(ypos);
    00053         }
    00054
    00055         double f = qwtSqr(cx - double(xpos)) + qwtSqr(cy - double(ypos));
    00056         if (f < dmin)
    00057         {
    00058             dmin = f;
    00059             rv = itm.currentKey();
    00060         }
    00061     }
    00062
    00063     dist = int(sqrt(dmin));
    00064     return rv;
    00065 }
    00066
    00068 long QwtPlot::newMarkerKey()
    00069 {
    00070     long newkey = d_markers->count() + 1;
    00071
    00072     if (newkey > 1)                     // count > 0
    00073     {
    00074         if (d_markers->find(newkey))    // key count+1 exists => there must be a
    00075                                         // free key <= count
    00076         {
    00077             // find the first available key <= count
    00078             newkey = 1;
    00079             while (newkey <= long(d_markers->count()))
    00080             {
    00081                 if (d_markers->find(newkey))
    00082                    newkey++;
    00083                 else
    00084                    break;
    00085             }
    00086
    00087             // This can't happen. Just paranoia.
    00088             if (newkey > long(d_markers->count()))
    00089             {
    00090                 while (!d_markers->find(newkey))
    00091                 {
    00092                     newkey++;
    00093                     if (newkey > 10000) // prevent infinite loop
    00094                     {
    00095                         newkey = 0;
    00096                         break;
    00097                     }
    00098                 }
    00099             }
    00100         }
    00101     }
    00102     return newkey;
    00103
    00104 }
    00105
    00113 long QwtPlot::insertLineMarker(const QString &label, int axis)
    00114 {
    00115     QwtMarker::LineStyle lineStyle = QwtMarker::NoLine;
    00116     int xAxis = QwtPlot::xBottom;
    00117     int yAxis = QwtPlot::yLeft;
    00118
    00119     switch(axis)
    00120     {
    00121         case yLeft:
    00122         case yRight:
    00123             yAxis = axis;
    00124             lineStyle = QwtMarker::HLine;
    00125             break;
    00126         case xTop:
    00127         case xBottom:
    00128             xAxis = axis;
    00129             lineStyle = QwtMarker::VLine;
    00130             break;
    00131     }
    00132
    00133     QwtPlotMarker *marker = new QwtPlotMarker(this);
    00134     if ( marker == 0 )
    00135         return 0;
    00136
    00137     marker->setAxis(xAxis, yAxis);
    00138     marker->setLabel(label);
    00139     marker->setLineStyle(lineStyle);
    00140     marker->setLabelAlignment(Qt::AlignRight|Qt::AlignTop);
    00141
    00142     long key = insertMarker(marker);
    00143     if ( key == 0 )
    00144         delete marker;
    00145
    00146     return key;
    00147 }
    00148
    00156 long QwtPlot::insertMarker(const QString &label, int xAxis, int yAxis)
    00157 {
    00158     QwtPlotMarker *marker = new QwtPlotMarker(this);
    00159     if ( marker == 0 )
    00160         return 0;
    00161
    00162     marker->setAxis(xAxis, yAxis);
    00163     marker->setLabel(label);
    00164
    00165     long key = insertMarker(marker);
    00166     if ( key == 0 )
    00167         delete marker;
    00168
    00169     return key;
    00170 }
    00171
    00177 long QwtPlot::insertMarker(QwtPlotMarker *marker)
    00178 {
    00179     if ( marker == 0 )
    00180         return 0;
    00181
    00182     long key = newMarkerKey();
    00183     if ( key == 0 )
    00184         return 0;
    00185
    00186     marker->reparent(this);
    00187     d_markers->insert(key, marker);
    00188
    00189     autoRefresh();
    00190
    00191     return key;
    00192 }
    00193
    00200 QwtPlotMarker *QwtPlot::marker(long key)
    00201 {
    00202     return d_markers->find(key);
    00203 }
    00204
    00211 const QwtPlotMarker *QwtPlot::marker(long key) const
    00212 {
    00213     return d_markers->find(key);
    00214 }
    00215
    00219 QwtArray<long> QwtPlot::markerKeys() const
    00220 {
    00221     QwtArray<long> keys(d_markers->count());
    00222
    00223     int i = 0;
    00224
    00225     QwtPlotMarkerIterator itm = markerIterator();
    00226     for (const QwtPlotMarker *m = itm.toFirst(); m != 0; m = ++itm, i++ )
    00227         keys[i] = itm.currentKey();
    00228
    00229     return keys;
    00230 }
    00231
    00235 QFont QwtPlot::markerFont(long key) const
    00236 {
    00237     QwtPlotMarker *m = d_markers->find(key);
    00238     if (m)
    00239         return m->font();
    00240     else
    00241         return QFont();
    00242 }
    00243
    00248 const QString QwtPlot::markerLabel(long key) const
    00249 {
    00250     QwtPlotMarker *m = d_markers->find(key);
    00251     if (m)
    00252         return m->label();
    00253     else
    00254         return QString::null;
    00255 }
    00256
    00261 int QwtPlot::markerLabelAlign(long key) const
    00262 {
    00263     QwtPlotMarker *m = d_markers->find(key);
    00264     if (m)
    00265         return m->labelAlignment();
    00266     else
    00267         return 0;
    00268 }
    00269
    00274 QPen QwtPlot::markerLabelPen(long key) const
    00275 {
    00276     QwtPlotMarker *m = d_markers->find(key);
    00277     if (m)
    00278         return m->labelPen();
    00279     else
    00280         return QPen();
    00281
    00282 }
    00283
    00288 QPen QwtPlot::markerLinePen(long key) const
    00289 {
    00290     QwtPlotMarker *m = d_markers->find(key);
    00291     if (m)
    00292         return m->linePen();
    00293     else
    00294         return QPen();
    00295
    00296 }
    00297
    00302 QwtMarker::LineStyle QwtPlot::markerLineStyle(long key) const
    00303 {
    00304     QwtPlotMarker *m = d_markers->find(key);
    00305     if (m)
    00306         return m->lineStyle();
    00307     else
    00308         return QwtMarker::NoLine;
    00309 }
    00310
    00318 void QwtPlot::markerPos(long key, double &mx, double &my ) const
    00319 {
    00320     QwtPlotMarker *m = d_markers->find(key);
    00321     if (m)
    00322     {
    00323         mx = m->xValue();
    00324         my = m->yValue();
    00325     }
    00326     else
    00327     {
    00328         mx = 0;
    00329         my = 0;
    00330     }
    00331 }
    00332
    00337 QwtSymbol QwtPlot::markerSymbol(long key) const
    00338 {
    00339     QwtPlotMarker *m = d_markers->find(key);
    00340     if (m)
    00341         return m->symbol();
    00342     else
    00343         return QwtSymbol();
    00344 }
    00345
    00346
    00351 int QwtPlot::markerXAxis(long key) const
    00352 {
    00353     QwtPlotMarker *m = d_markers->find(key);
    00354     if (m)
    00355         return m->xAxis();
    00356     else
    00357         return -1;
    00358
    00359 }
    00360
    00361
    00366 int QwtPlot::markerYAxis(long key) const
    00367 {
    00368     QwtPlotMarker *m = d_markers->find(key);
    00369     if (m)
    00370         return m->yAxis();
    00371     else
    00372         return -1;
    00373
    00374 }
    00375
    00380 bool QwtPlot::removeMarker(long key)
    00381 {
    00382     if (d_markers->remove(key))
    00383     {
    00384         autoRefresh();
    00385         return TRUE;
    00386     }
    00387     else
    00388        return FALSE;
    00389 }
    00390
    00391
    00396 bool QwtPlot::setMarkerXAxis(long key, int axis)
    00397 {
    00398     QwtPlotMarker *m;
    00399     if ((m = d_markers->find(key)))
    00400     {
    00401         m->setXAxis(axis);
    00402         return TRUE;
    00403     }
    00404     else
    00405        return FALSE;
    00406 }
    00407
    00414 bool QwtPlot::setMarkerYAxis(long key, int axis)
    00415 {
    00416     QwtPlotMarker *m;
    00417     if ((m = d_markers->find(key)))
    00418     {
    00419         m->setYAxis(axis);
    00420         return TRUE;
    00421     }
    00422     else
    00423        return FALSE;
    00424 }
    00425
    00432 bool QwtPlot::setMarkerFont(long key, const QFont &f)
    00433 {
    00434     int rv = FALSE;
    00435
    00436     QwtPlotMarker *m;
    00437     if ((m = d_markers->find(key)))
    00438     {
    00439         m->setFont(f);
    00440         rv = TRUE;
    00441     }
    00442     return rv;
    00443 }
    00444
    00451 bool QwtPlot::setMarkerLinePen(long key, const QPen &p)
    00452 {
    00453     int rv = FALSE;
    00454
    00455     QwtPlotMarker *m;
    00456     if ((m = d_markers->find(key)))
    00457     {
    00458         m->setLinePen(p);
    00459         rv = TRUE;
    00460     }
    00461     return rv;
    00462
    00463 }
    00464
    00465
    00473 bool QwtPlot::setMarkerLineStyle(long key, QwtMarker::LineStyle st)
    00474 {
    00475     int rv = FALSE;
    00476     QwtPlotMarker *m;
    00477     if ((m = d_markers->find(key)))
    00478     {
    00479         m->setLineStyle(st);
    00480         rv = TRUE;
    00481     }
    00482     return rv;
    00483 }
    00484
    00491 bool QwtPlot::setMarkerPen(long key, const QPen &p)
    00492 {
    00493     int rv = FALSE;
    00494
    00495     QwtPlotMarker *m;
    00496     if ((m = d_markers->find(key)))
    00497     {
    00498         m->setLinePen(p);
    00499         m->setLabelPen(p);
    00500         rv = TRUE;
    00501     }
    00502     return rv;
    00503 }
    00504
    00505
    00513 bool QwtPlot::setMarkerPos(long key, double xval, double yval)
    00514 {
    00515     int rv = FALSE;
    00516
    00517     QwtPlotMarker *m;
    00518     if ((m = d_markers->find(key)))
    00519     {
    00520         m->setXValue(xval);
    00521         m->setYValue(yval);
    00522         rv = TRUE;
    00523     }
    00524     return rv;
    00525 }
    00526
    00533 bool QwtPlot::setMarkerXPos(long key, double val)
    00534 {
    00535     int rv = FALSE;
    00536
    00537     QwtPlotMarker *m;
    00538     if ((m = d_markers->find(key)))
    00539     {
    00540         m->setXValue(val);
    00541         rv = TRUE;
    00542     }
    00543     return rv;
    00544 }
    00545
    00552 bool QwtPlot::setMarkerYPos(long key, double val)
    00553 {
    00554     int rv = FALSE;
    00555
    00556     QwtPlotMarker *m;
    00557     if ((m = d_markers->find(key)))
    00558     {
    00559         m->setYValue(val);
    00560         rv = TRUE;
    00561     }
    00562     return rv;
    00563 }
    00564
    00571 bool QwtPlot::setMarkerSymbol(long key, const QwtSymbol &s)
    00572 {
    00573     int rv = FALSE;
    00574     QwtPlotMarker *m;
    00575     if ((m = d_markers->find(key)))
    00576     {
    00577         m->setSymbol(s);
    00578         rv = TRUE;
    00579     }
    00580     return rv;
    00581 }
    00582
    00589 bool QwtPlot::setMarkerLabelText(long key, const QString &text)
    00590 {
    00591     QwtPlotMarker *m;
    00592     if ((m = d_markers->find(key)))
    00593     {
    00594         m->setLabelText(text);
    00595         return TRUE;
    00596     }
    00597     return FALSE;
    00598 }
    00599
    00610 bool QwtPlot::setMarkerLabel(long key, const QString &text, const QFont &font,
    00611     const QColor &color, const QPen &pen, const QBrush &brush)
    00612 {
    00613     QwtPlotMarker *m;
    00614     if ((m = d_markers->find(key)))
    00615     {
    00616         m->setLabel(text, font, color, pen, brush);
    00617         return TRUE;
    00618     }
    00619     return FALSE;
    00620 }
    00621
    00633 bool QwtPlot::setMarkerLabelAlign(long key, int align)
    00634 {
    00635     int rv = FALSE;
    00636     QwtPlotMarker *m;
    00637     if ((m = d_markers->find(key)))
    00638     {
    00639         m->setLabelAlignment(align);
    00640         rv = TRUE;
    00641     }
    00642     return rv;
    00643 }
    00644
    00651 bool QwtPlot::setMarkerLabelPen(long key, const QPen &p)
    00652 {
    00653     int rv = FALSE;
    00654     QwtPlotMarker *m;
    00655     if ((m = d_markers->find(key)))
    00656     {
    00657         m->setLabelPen(p);
    00658         rv = TRUE;
    00659     }
    00660     return rv;
    00661 }
    00662
    00663
    00664
    00665