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     }
00032 
00033     QwtText label;
00034 #if QT_VERSION < 0x040000
00035     int align;
00036 #else
00037     Qt::Alignment align;
00038 #endif
00039     QPen pen;
00040     QwtSymbol sym;
00041     LineStyle style;
00042 
00043     double xValue;
00044     double yValue;
00045 };
00046 
00048 QwtPlotMarker::QwtPlotMarker()
00049 {
00050     d_data = new PrivateData;
00051     setZ(30.0);
00052 }
00053 
00055 QwtPlotMarker::~QwtPlotMarker()
00056 {
00057     delete d_data;
00058 }
00059 
00064 QwtPlotMarker::QwtPlotMarker(const QwtPlotMarker &m):
00065     QwtPlotItem(m)
00066 {
00067     *this = m;
00068 }
00069 
00074 QwtPlotMarker& QwtPlotMarker::operator=(const QwtPlotMarker &m)
00075 {
00076     if (this != &m)
00077     {
00078         QwtPlotItem::operator=((const QwtPlotItem &)m);
00079 
00080         d_data->label = m.d_data->label;
00081         d_data->align = m.d_data->align;
00082         d_data->pen = m.d_data->pen;
00083         d_data->sym = m.d_data->sym;
00084         d_data->style = m.d_data->style;
00085         d_data->xValue = m.d_data->xValue;
00086         d_data->yValue = m.d_data->yValue;
00087 
00088         itemChanged(); 
00089     }
00090 
00091     return *this;
00092 }
00093 
00094 int QwtPlotMarker::rtti() const
00095 {
00096     return QwtPlotItem::Rtti_PlotMarker;
00097 }
00098 
00100 QwtDoublePoint QwtPlotMarker::value() const
00101 {
00102     return QwtDoublePoint(d_data->xValue, d_data->yValue);
00103 }
00104 
00106 double QwtPlotMarker::xValue() const 
00107 { 
00108     return d_data->xValue; 
00109 }
00110 
00112 double QwtPlotMarker::yValue() const 
00113 { 
00114     return d_data->yValue; 
00115 }
00116 
00118 void QwtPlotMarker::setValue(const QwtDoublePoint& pos)
00119 {
00120     setValue(pos.x(), pos.y());
00121 }
00122 
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 
00153 void QwtPlotMarker::draw(QPainter *p,
00154     const QwtScaleMap &xMap, const QwtScaleMap &yMap,
00155     const QRect &r) const
00156 {
00157     const int x = xMap.transform(d_data->xValue);
00158     const int y = yMap.transform(d_data->yValue);
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.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         QRect tr(QPoint(0, 0), d_data->label.textSize(p->font()));
00207         tr.moveCenter(QPoint(0, 0));
00208 
00209         int dx = x;
00210         int dy = y;
00211 
00212         if (d_data->style == VLine)
00213         {
00214             if (d_data->align & (int) Qt::AlignTop)
00215                 dy = r.top() + yLabelDist - tr.y();
00216             else if (d_data->align & (int) Qt::AlignBottom)
00217                 dy = r.bottom() - yLabelDist + tr.y();
00218             else
00219                 dy = r.top() + r.height() / 2;
00220         }
00221         else
00222         {
00223             if (d_data->align & (int) Qt::AlignTop)
00224                 dy += tr.y() - ylw1;
00225             else if (d_data->align & (int) Qt::AlignBottom)
00226                 dy -= tr.y() - ylw1;
00227         }
00228 
00229 
00230         if (d_data->style == HLine)
00231         {
00232             if (d_data->align & (int) Qt::AlignLeft)
00233                 dx = r.left() + xLabelDist - tr.x();
00234             else if (d_data->align & (int) Qt::AlignRight)
00235                 dx = r.right() - xLabelDist + tr.x();
00236             else
00237                 dx = r.left() + r.width() / 2;
00238         }
00239         else
00240         {
00241             if (d_data->align & (int) Qt::AlignLeft)
00242                 dx += tr.x() - xlw1;
00243             else if (d_data->align & (int) Qt::AlignRight)
00244                 dx -= tr.x() - xlw1;
00245         }
00246 
00247 #if QT_VERSION < 0x040000
00248         tr.moveBy(dx, dy);
00249 #else
00250         tr.translate(dx, dy);
00251 #endif
00252         d_data->label.draw(p, tr);
00253     }
00254 }
00255 
00262 void QwtPlotMarker::setLineStyle(QwtPlotMarker::LineStyle st)
00263 {
00264     if ( st != d_data->style )
00265     {
00266         d_data->style = st;
00267         itemChanged();
00268     }
00269 }
00270 
00275 QwtPlotMarker::LineStyle QwtPlotMarker::lineStyle() const 
00276 { 
00277     return d_data->style; 
00278 }
00279 
00285 void QwtPlotMarker::setSymbol(const QwtSymbol &s)
00286 {
00287     d_data->sym = s;
00288     itemChanged();
00289 }
00290 
00295 const QwtSymbol &QwtPlotMarker::symbol() const 
00296 { 
00297     return d_data->sym; 
00298 }
00299 
00305 void QwtPlotMarker::setLabel(const QwtText& label)
00306 {
00307     if ( label != d_data->label )
00308     {
00309         d_data->label = label;
00310         itemChanged();
00311     }
00312 }
00313 
00318 QwtText QwtPlotMarker::label() const 
00319 { 
00320     return d_data->label; 
00321 }
00322 
00334 #if QT_VERSION < 0x040000
00335 void QwtPlotMarker::setLabelAlignment(int align)
00336 #else
00337 void QwtPlotMarker::setLabelAlignment(Qt::Alignment align)
00338 #endif
00339 {
00340     if ( align == d_data->align )
00341         return;
00342     
00343     d_data->align = align;
00344     itemChanged();
00345 }
00346 
00351 #if QT_VERSION < 0x040000
00352 int QwtPlotMarker::labelAlignment() const 
00353 #else
00354 Qt::Alignment QwtPlotMarker::labelAlignment() const 
00355 #endif
00356 { 
00357     return d_data->align; 
00358 }
00359 
00365 void QwtPlotMarker::setLinePen(const QPen &p)
00366 {
00367     if ( p != d_data->pen )
00368     {
00369         d_data->pen = p;
00370         itemChanged();
00371     }
00372 }
00373 
00378 const QPen &QwtPlotMarker::linePen() const 
00379 { 
00380     return d_data->pen; 
00381 }
00382 
00383 QwtDoubleRect QwtPlotMarker::boundingRect() const
00384 {
00385     return QwtDoubleRect(d_data->xValue, d_data->yValue, 0.0, 0.0);
00386 }

Generated on Mon Jan 30 22:16:25 2006 for Qwt User's Guide by  doxygen 1.4.4