Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | 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 <qpainter.h>
00013 #include "qwt_painter.h"
00014 #include "qwt_scale_map.h"
00015 #include "qwt_plot_marker.h"
00016 #include "qwt_symbol.h"
00017 #include "qwt_text.h"
00018 #include "qwt_math.h"
00019 
00020 static const int LabelDist = 2;
00021 
00022 class QwtPlotMarker::PrivateData
00023 {
00024 public:
00025     PrivateData():
00026         align(Qt::AlignCenter),
00027         style(NoLine),
00028         xValue(0.0),
00029         yValue(0.0)
00030     {
00031         // QwtPlotMarker.draw() assumes that 
00032         // label->alignment() == Qt::AlignCenter. 
00033 
00034         label = QwtText::makeText(QString::null, Qt::AlignCenter, QFont());
00035     }
00036 
00037     ~PrivateData()
00038     {
00039         delete label;
00040     }
00041 
00042     QwtText *label;
00043     int align;
00044     QPen pen;
00045     QwtSymbol sym;
00046     LineStyle style;
00047 
00048     double xValue;
00049     double yValue;
00050 };
00051 
00053 QwtPlotMarker::QwtPlotMarker()
00054 {
00055     d_data = new PrivateData;
00056     setZ(30.0);
00057 }
00058 
00060 QwtPlotMarker::~QwtPlotMarker()
00061 {
00062     delete d_data;
00063 }
00064 
00069 QwtPlotMarker::QwtPlotMarker(const QwtPlotMarker &m):
00070     QwtPlotItem(m)
00071 {
00072     *this = m;
00073 }
00074 
00079 QwtPlotMarker& QwtPlotMarker::operator=(const QwtPlotMarker &m)
00080 {
00081     if (this != &m)
00082     {
00083         QwtPlotItem::operator=((const QwtPlotItem &)m);
00084 
00085         d_data->label = m.d_data->label->clone();
00086         d_data->align = m.d_data->align;
00087         d_data->pen = m.d_data->pen;
00088         d_data->sym = m.d_data->sym;
00089         d_data->style = m.d_data->style;
00090         d_data->xValue = m.d_data->xValue;
00091         d_data->yValue = m.d_data->yValue;
00092     }
00093 
00094     return *this;
00095 }
00096 
00097 int QwtPlotMarker::rtti() const
00098 {
00099     return QwtPlotItem::Rtti_PlotMarker;
00100 }
00101 
00102 QwtDoublePoint QwtPlotMarker::value() const
00103 {
00104     return QwtDoublePoint(d_data->xValue, d_data->yValue);
00105 }
00106 
00108 double QwtPlotMarker::xValue() const 
00109 { 
00110     return d_data->xValue; 
00111 }
00112 
00114 double QwtPlotMarker::yValue() const 
00115 { 
00116     return d_data->yValue; 
00117 }
00118 
00119 void QwtPlotMarker::setValue(const QwtDoublePoint& pos)
00120 {
00121     setValue(pos.x(), pos.y());
00122 }
00123 
00124 void QwtPlotMarker::setValue(double x, double y) 
00125 {
00126     if ( x != d_data->xValue || y != d_data->yValue )
00127     {
00128         d_data->xValue = x; 
00129         d_data->yValue = y; 
00130         itemChanged(); 
00131     }
00132 }
00133 
00135 void QwtPlotMarker::setXValue(double x) 
00136 { 
00137     setValue(x, d_data->yValue);
00138 }
00139 
00141 void QwtPlotMarker::setYValue(double y) 
00142 { 
00143     setValue(d_data->xValue, y);
00144 }
00145 
00150 void QwtPlotMarker::draw(QPainter *p,
00151     const QwtScaleMap &xMap, const QwtScaleMap &yMap,
00152     const QRect &r) const
00153 {
00154     const int x = xMap.transform(d_data->xValue);
00155     const int y = yMap.transform(d_data->yValue);
00156 
00157     // QwtPlotMarker.draw() assumes that 
00158     // d_data->label->alignment() == Qt::AlignCenter. 
00159 
00160     // draw lines
00161     if (d_data->style != NoLine)
00162     {
00163         p->setPen(d_data->pen);
00164         if ((d_data->style == HLine) || (d_data->style == Cross))
00165             QwtPainter::drawLine(p, r.left(), y, r.right(), y);
00166         if ((d_data->style == VLine)||(d_data->style == Cross))
00167             QwtPainter::drawLine(p, x, r.top(), x, r.bottom());
00168     }
00169 
00170     // draw symbol
00171     QSize sSym(0, 0);
00172     if (d_data->sym.style() != QwtSymbol::None)
00173     {
00174         sSym = d_data->sym.size();
00175         d_data->sym.draw(p, x, y);
00176     }
00177 
00178     // draw label
00179     if (!d_data->label->text().isEmpty())
00180     {
00181         int xlw = qwtMax(int(d_data->pen.width()), 1);
00182         int ylw = xlw;
00183         int xlw1;
00184         int ylw1;
00185 
00186         const int xLabelDist = 
00187             QwtPainter::metricsMap().screenToLayoutX(LabelDist);
00188         const int yLabelDist = 
00189             QwtPainter::metricsMap().screenToLayoutY(LabelDist);
00190 
00191         if ((d_data->style == VLine) || (d_data->style == HLine))
00192         {
00193             xlw1 = (xlw + 1) / 2 + xLabelDist;
00194             xlw = xlw / 2 + xLabelDist;
00195             ylw1 = (ylw + 1) / 2 + yLabelDist;
00196             ylw = ylw / 2 + yLabelDist;
00197         }
00198         else 
00199         {
00200             xlw1 = qwtMax((xlw + 1) / 2, (sSym.width() + 1) / 2) + xLabelDist;
00201             xlw = qwtMax(xlw / 2, (sSym.width() + 1) / 2) + xLabelDist;
00202             ylw1 = qwtMax((ylw + 1) / 2, (sSym.height() + 1) / 2) + yLabelDist;
00203             ylw = qwtMax(ylw / 2, (sSym. height() + 1) / 2) + yLabelDist;
00204         }
00205 
00206         // tr is offset with respect to (0, 0) obeying the immutable flags 
00207         // Qt::AlignCenter. Hence: 
00208         // tr.x() == -tr.width()/2
00209         // tr.y() == -tr.height()/2
00210         QRect tr = d_data->label->boundingRect(p);
00211 
00212         int dx = x;
00213         int dy = y;
00214 
00215         if (d_data->style == VLine)
00216         {
00217             if (d_data->align & (int) Qt::AlignTop)
00218                 dy = r.top() + yLabelDist - tr.y();
00219             else if (d_data->align & (int) Qt::AlignBottom)
00220                 dy = r.bottom() - yLabelDist + tr.y();
00221             else
00222                 dy = r.top() + r.height() / 2;
00223         }
00224         else
00225         {
00226             if (d_data->align & (int) Qt::AlignTop)
00227                 dy += tr.y() - ylw1;
00228             else if (d_data->align & (int) Qt::AlignBottom)
00229                 dy -= tr.y() - ylw1;
00230         }
00231 
00232 
00233         if (d_data->style == HLine)
00234         {
00235             if (d_data->align & (int) Qt::AlignLeft)
00236                 dx = r.left() + xLabelDist - tr.x();
00237             else if (d_data->align & (int) Qt::AlignRight)
00238                 dx = r.right() - xLabelDist + tr.x();
00239             else
00240                 dx = r.left() + r.width() / 2;
00241         }
00242         else
00243         {
00244             if (d_data->align & (int) Qt::AlignLeft)
00245                 dx += tr.x() - xlw1;
00246             else if (d_data->align & (int) Qt::AlignRight)
00247                 dx -= tr.x() - xlw1;
00248         }
00249 
00250 #if QT_VERSION < 0x040000
00251         tr.moveBy(dx, dy);
00252 #else
00253         tr.translate(dx, dy);
00254 #endif
00255         d_data->label->draw(p, tr);
00256     }
00257 }
00258 
00264 void QwtPlotMarker::setFont(const QFont &f)
00265 {
00266     if ( f == d_data->label->font() )
00267         return;
00268     
00269     d_data->label->setFont(f);
00270     itemChanged();
00271 }
00272 
00277 const QFont QwtPlotMarker::font() const 
00278 { 
00279     return d_data->label->font(); 
00280 }
00281 
00282 
00289 void QwtPlotMarker::setLineStyle(QwtPlotMarker::LineStyle st)
00290 {
00291     if ( st != d_data->style )
00292     {
00293         d_data->style = st;
00294         itemChanged();
00295     }
00296 }
00297 
00302 QwtPlotMarker::LineStyle QwtPlotMarker::lineStyle() const 
00303 { 
00304     return d_data->style; 
00305 }
00306 
00312 void QwtPlotMarker::setSymbol(const QwtSymbol &s)
00313 {
00314     d_data->sym = s;
00315     itemChanged();
00316 }
00317 
00322 const QwtSymbol &QwtPlotMarker::symbol() const 
00323 { 
00324     return d_data->sym; 
00325 }
00326 
00332 void QwtPlotMarker::setLabelText(const QString &text)
00333 {
00334     setLabel(text, d_data->label->font(), d_data->label->color(), 
00335         d_data->label->rectPen(), d_data->label->rectBrush());
00336 }
00337 
00347 void QwtPlotMarker::setLabel(const QString &text, const QFont &font,
00348     const QColor &color, const QPen &pen, const QBrush &brush)
00349 {
00350     if ( text == d_data->label->text()
00351          && font == d_data->label->font()
00352          && color == d_data->label->color()
00353          && pen == d_data->label->rectPen()
00354          && brush == d_data->label->rectBrush() )
00355         return;
00356     
00357     QwtText *label = QwtText::makeText(
00358         text, d_data->label->flags(), font, color, pen, brush);
00359 
00360     delete d_data->label;
00361     d_data->label = label;
00362 
00363     itemChanged();
00364 }
00365 
00370 const QString QwtPlotMarker::label() const 
00371 { 
00372     return d_data->label->text(); 
00373 }
00374 
00386 void QwtPlotMarker::setLabelAlignment(int align)
00387 {
00388     if ( align == d_data->align )
00389         return;
00390     
00391     d_data->align = align;
00392     itemChanged();
00393 }
00394 
00399 int QwtPlotMarker::labelAlignment() const 
00400 { 
00401     return d_data->align; 
00402 }
00403 
00409 void QwtPlotMarker::setLinePen(const QPen &p)
00410 {
00411     if ( p != d_data->pen )
00412     {
00413         d_data->pen = p;
00414         itemChanged();
00415     }
00416 }
00417 
00422 const QPen &QwtPlotMarker::linePen() const 
00423 { 
00424     return d_data->pen; 
00425 }
00426 
00432 void QwtPlotMarker::setLabelColor(const QColor &color)
00433 {
00434     if ( color == d_data->label->color() )
00435         return;
00436     
00437     d_data->label->setColor(color);
00438     itemChanged();
00439 }
00440 
00445 const QColor QwtPlotMarker::labelColor() const 
00446 { 
00447     return d_data->label->color(); 
00448 }
00449 
00456 void QwtPlotMarker::setLabelPen(const QPen &p)
00457 {
00458     if ( p == QPen(d_data->label->color()) )
00459         return;
00460     
00461     d_data->label->setColor(p.color());
00462     itemChanged();
00463 }
00464 
00470 const QPen QwtPlotMarker::labelPen() const 
00471 { 
00472     return QPen(d_data->label->color()); 
00473 }
00474 
00475 QwtDoubleRect QwtPlotMarker::boundingRect() const
00476 {
00477     return QwtDoubleRect(d_data->xValue, d_data->yValue, 0.0, 0.0);
00478 }

Generated on Wed Aug 31 23:02:29 2005 for Qwt User's Guide by  doxygen 1.4.1