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     QwtPlotItem(QwtText("Marker"))
00050 {
00051     d_data = new PrivateData;
00052     setZ(30.0);
00053 }
00054 
00056 QwtPlotMarker::~QwtPlotMarker()
00057 {
00058     delete d_data;
00059 }
00060 
00062 int QwtPlotMarker::rtti() const
00063 {
00064     return QwtPlotItem::Rtti_PlotMarker;
00065 }
00066 
00068 QwtDoublePoint QwtPlotMarker::value() const
00069 {
00070     return QwtDoublePoint(d_data->xValue, d_data->yValue);
00071 }
00072 
00074 double QwtPlotMarker::xValue() const 
00075 { 
00076     return d_data->xValue; 
00077 }
00078 
00080 double QwtPlotMarker::yValue() const 
00081 { 
00082     return d_data->yValue; 
00083 }
00084 
00086 void QwtPlotMarker::setValue(const QwtDoublePoint& pos)
00087 {
00088     setValue(pos.x(), pos.y());
00089 }
00090 
00092 void QwtPlotMarker::setValue(double x, double y) 
00093 {
00094     if ( x != d_data->xValue || y != d_data->yValue )
00095     {
00096         d_data->xValue = x; 
00097         d_data->yValue = y; 
00098         itemChanged(); 
00099     }
00100 }
00101 
00103 void QwtPlotMarker::setXValue(double x) 
00104 { 
00105     setValue(x, d_data->yValue);
00106 }
00107 
00109 void QwtPlotMarker::setYValue(double y) 
00110 { 
00111     setValue(d_data->xValue, y);
00112 }
00113 
00121 void QwtPlotMarker::draw(QPainter *p,
00122     const QwtScaleMap &xMap, const QwtScaleMap &yMap,
00123     const QRect &r) const
00124 {
00125     const int x = xMap.transform(d_data->xValue);
00126     const int y = yMap.transform(d_data->yValue);
00127 
00128     // draw lines
00129     if (d_data->style != NoLine)
00130     {
00131         p->setPen(d_data->pen);
00132         if ((d_data->style == HLine) || (d_data->style == Cross))
00133             QwtPainter::drawLine(p, r.left(), y, r.right(), y);
00134         if ((d_data->style == VLine)||(d_data->style == Cross))
00135             QwtPainter::drawLine(p, x, r.top(), x, r.bottom());
00136     }
00137 
00138     // draw symbol
00139     QSize sSym(0, 0);
00140     if (d_data->sym.style() != QwtSymbol::NoSymbol)
00141     {
00142         sSym = d_data->sym.size();
00143         d_data->sym.draw(p, x, y);
00144     }
00145 
00146     // draw label
00147     if (!d_data->label.isEmpty())
00148     {
00149         int xlw = qwtMax(int(d_data->pen.width()), 1);
00150         int ylw = xlw;
00151         int xlw1;
00152         int ylw1;
00153 
00154         const int xLabelDist = 
00155             QwtPainter::metricsMap().screenToLayoutX(LabelDist);
00156         const int yLabelDist = 
00157             QwtPainter::metricsMap().screenToLayoutY(LabelDist);
00158 
00159         if ((d_data->style == VLine) || (d_data->style == HLine))
00160         {
00161             xlw1 = (xlw + 1) / 2 + xLabelDist;
00162             xlw = xlw / 2 + xLabelDist;
00163             ylw1 = (ylw + 1) / 2 + yLabelDist;
00164             ylw = ylw / 2 + yLabelDist;
00165         }
00166         else 
00167         {
00168             xlw1 = qwtMax((xlw + 1) / 2, (sSym.width() + 1) / 2) + xLabelDist;
00169             xlw = qwtMax(xlw / 2, (sSym.width() + 1) / 2) + xLabelDist;
00170             ylw1 = qwtMax((ylw + 1) / 2, (sSym.height() + 1) / 2) + yLabelDist;
00171             ylw = qwtMax(ylw / 2, (sSym. height() + 1) / 2) + yLabelDist;
00172         }
00173 
00174         QRect tr(QPoint(0, 0), d_data->label.textSize(p->font()));
00175         tr.moveCenter(QPoint(0, 0));
00176 
00177         int dx = x;
00178         int dy = y;
00179 
00180         if (d_data->style == VLine)
00181         {
00182             if (d_data->align & (int) Qt::AlignTop)
00183                 dy = r.top() + yLabelDist - tr.y();
00184             else if (d_data->align & (int) Qt::AlignBottom)
00185                 dy = r.bottom() - yLabelDist + tr.y();
00186             else
00187                 dy = r.top() + r.height() / 2;
00188         }
00189         else
00190         {
00191             if (d_data->align & (int) Qt::AlignTop)
00192                 dy += tr.y() - ylw1;
00193             else if (d_data->align & (int) Qt::AlignBottom)
00194                 dy -= tr.y() - ylw1;
00195         }
00196 
00197 
00198         if (d_data->style == HLine)
00199         {
00200             if (d_data->align & (int) Qt::AlignLeft)
00201                 dx = r.left() + xLabelDist - tr.x();
00202             else if (d_data->align & (int) Qt::AlignRight)
00203                 dx = r.right() - xLabelDist + tr.x();
00204             else
00205                 dx = r.left() + r.width() / 2;
00206         }
00207         else
00208         {
00209             if (d_data->align & (int) Qt::AlignLeft)
00210                 dx += tr.x() - xlw1;
00211             else if (d_data->align & (int) Qt::AlignRight)
00212                 dx -= tr.x() - xlw1;
00213         }
00214 
00215 #if QT_VERSION < 0x040000
00216         tr.moveBy(dx, dy);
00217 #else
00218         tr.translate(dx, dy);
00219 #endif
00220         d_data->label.draw(p, tr);
00221     }
00222 }
00223 
00230 void QwtPlotMarker::setLineStyle(QwtPlotMarker::LineStyle st)
00231 {
00232     if ( st != d_data->style )
00233     {
00234         d_data->style = st;
00235         itemChanged();
00236     }
00237 }
00238 
00243 QwtPlotMarker::LineStyle QwtPlotMarker::lineStyle() const 
00244 { 
00245     return d_data->style; 
00246 }
00247 
00253 void QwtPlotMarker::setSymbol(const QwtSymbol &s)
00254 {
00255     d_data->sym = s;
00256     itemChanged();
00257 }
00258 
00263 const QwtSymbol &QwtPlotMarker::symbol() const 
00264 { 
00265     return d_data->sym; 
00266 }
00267 
00273 void QwtPlotMarker::setLabel(const QwtText& label)
00274 {
00275     if ( label != d_data->label )
00276     {
00277         d_data->label = label;
00278         itemChanged();
00279     }
00280 }
00281 
00286 QwtText QwtPlotMarker::label() const 
00287 { 
00288     return d_data->label; 
00289 }
00290 
00302 #if QT_VERSION < 0x040000
00303 void QwtPlotMarker::setLabelAlignment(int align)
00304 #else
00305 void QwtPlotMarker::setLabelAlignment(Qt::Alignment align)
00306 #endif
00307 {
00308     if ( align == d_data->align )
00309         return;
00310     
00311     d_data->align = align;
00312     itemChanged();
00313 }
00314 
00319 #if QT_VERSION < 0x040000
00320 int QwtPlotMarker::labelAlignment() const 
00321 #else
00322 Qt::Alignment QwtPlotMarker::labelAlignment() const 
00323 #endif
00324 { 
00325     return d_data->align; 
00326 }
00327 
00333 void QwtPlotMarker::setLinePen(const QPen &p)
00334 {
00335     if ( p != d_data->pen )
00336     {
00337         d_data->pen = p;
00338         itemChanged();
00339     }
00340 }
00341 
00346 const QPen &QwtPlotMarker::linePen() const 
00347 { 
00348     return d_data->pen; 
00349 }
00350 
00351 QwtDoubleRect QwtPlotMarker::boundingRect() const
00352 {
00353     return QwtDoubleRect(d_data->xValue, d_data->yValue, 0.0, 0.0);
00354 }

Generated on Sun Jul 22 11:26:54 2007 for Qwt User's Guide by  doxygen 1.5.2