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

qwt_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_marker.h"
00015 #include "qwt_math.h"
00016 
00017 static const int LabelDist = 2;
00018 
00020 QwtMarker::QwtMarker()
00021 {
00022     // QwtMarker.draw() assumes that d_label->alignment() == Qt::AlignCenter. 
00023     d_label = QwtText::makeText(QString::null, Qt::AlignCenter, QFont());
00024     d_align = Qt::AlignCenter;
00025     d_style = NoLine;
00026 }
00027 
00029 QwtMarker::~QwtMarker()
00030 {
00031     delete d_label;
00032 }
00033 
00038 QwtMarker::QwtMarker(const QwtMarker &m)
00039 {
00040     d_label = m.d_label->clone();
00041     d_align = m.d_align;
00042     d_pen = m.d_pen;
00043     d_sym = m.d_sym;
00044     d_style = m.d_style;
00045 }
00046 
00051 QwtMarker& QwtMarker::operator=(const QwtMarker &m)
00052 {
00053     if (this != &m)
00054     {
00055         d_label = m.d_label->clone();
00056         d_align = m.d_align;
00057         d_pen = m.d_pen;
00058         d_sym = m.d_sym;
00059         d_style = m.d_style;
00060     }
00061 
00062     return *this;
00063 }
00064 
00072 void QwtMarker::draw(QPainter *p, int x, int y, const QRect &r)
00073 {
00074     // QwtMarker.draw() assumes that d_label->alignment() == Qt::AlignCenter. 
00075 
00076     // draw lines
00077     if (d_style != NoLine)
00078     {
00079         p->setPen(d_pen);
00080         if ((d_style == HLine) || (d_style == Cross))
00081             QwtPainter::drawLine(p, r.left(), y, r.right(), y);
00082         if ((d_style == VLine)||(d_style == Cross))
00083             QwtPainter::drawLine(p, x, r.top(), x, r.bottom());
00084     }
00085 
00086     // draw symbol
00087     QSize sSym(0, 0);
00088     if (d_sym.style() != QwtSymbol::None)
00089     {
00090         sSym = d_sym.size();
00091         d_sym.draw(p, x, y);
00092     }
00093 
00094     // draw label
00095     if (!d_label->text().isEmpty())
00096     {
00097         int xlw = qwtMax(int(d_pen.width()), 1);
00098         int ylw = xlw;
00099         int xlw1;
00100         int ylw1;
00101 
00102         const int xLabelDist = 
00103             QwtPainter::metricsMap().screenToLayoutX(LabelDist);
00104         const int yLabelDist = 
00105             QwtPainter::metricsMap().screenToLayoutY(LabelDist);
00106 
00107         if ((d_style == VLine) || (d_style == HLine))
00108         {
00109             xlw1 = (xlw + 1) / 2 + xLabelDist;
00110             xlw = xlw / 2 + xLabelDist;
00111             ylw1 = (ylw + 1) / 2 + yLabelDist;
00112             ylw = ylw / 2 + yLabelDist;
00113         }
00114         else 
00115         {
00116             xlw1 = qwtMax((xlw + 1) / 2, (sSym.width() + 1) / 2) + xLabelDist;
00117             xlw = qwtMax(xlw / 2, (sSym.width() + 1) / 2) + xLabelDist;
00118             ylw1 = qwtMax((ylw + 1) / 2, (sSym.height() + 1) / 2) + yLabelDist;
00119             ylw = qwtMax(ylw / 2, (sSym. height() + 1) / 2) + yLabelDist;
00120         }
00121 
00122         // tr is offset with respect to (0, 0) obeying the immutable flags 
00123         // Qt::AlignCenter. Hence: 
00124         // tr.x() == -tr.width()/2
00125         // tr.y() == -tr.height()/2
00126         QRect tr = d_label->boundingRect(p);
00127 
00128         int dx = x;
00129         int dy = y;
00130 
00131         if (d_style == VLine)
00132         {
00133             if (d_align & (int) Qt::AlignTop)
00134                 dy = r.top() + yLabelDist - tr.y();
00135             else if (d_align & (int) Qt::AlignBottom)
00136                 dy = r.bottom() - yLabelDist + tr.y();
00137             else
00138                 dy = r.top() + r.height() / 2;
00139         }
00140         else
00141         {
00142             if (d_align & (int) Qt::AlignTop)
00143                 dy += tr.y() - ylw1;
00144             else if (d_align & (int) Qt::AlignBottom)
00145                 dy -= tr.y() - ylw1;
00146         }
00147 
00148 
00149         if (d_style == HLine)
00150         {
00151             if (d_align & (int) Qt::AlignLeft)
00152                 dx = r.left() + xLabelDist - tr.x();
00153             else if (d_align & (int) Qt::AlignRight)
00154                 dx = r.right() - xLabelDist + tr.x();
00155             else
00156                 dx = r.left() + r.width() / 2;
00157         }
00158         else
00159         {
00160             if (d_align & (int) Qt::AlignLeft)
00161                 dx += tr.x() - xlw1;
00162             else if (d_align & (int) Qt::AlignRight)
00163                 dx -= tr.x() - xlw1;
00164         }
00165 
00166         tr.moveBy(dx, dy);
00167         d_label->draw(p, tr);
00168     }
00169 }
00170 
00176 void QwtMarker::setFont(const QFont &f)
00177 {
00178     if ( f == d_label->font() )
00179         return;
00180     
00181     d_label->setFont(f);
00182     markerChanged();
00183 }
00184 
00189 const QFont QwtMarker::font() const 
00190 { 
00191     return d_label->font(); 
00192 }
00193 
00194 
00201 void QwtMarker::setLineStyle(QwtMarker::LineStyle st)
00202 {
00203     if ( st != d_style )
00204     {
00205         d_style = st;
00206         markerChanged();
00207     }
00208 }
00209 
00214 QwtMarker::LineStyle QwtMarker::lineStyle() const 
00215 { 
00216     return d_style; 
00217 }
00218 
00224 void QwtMarker::setSymbol(const QwtSymbol &s)
00225 {
00226     d_sym = s;
00227     markerChanged();
00228 }
00229 
00234 const QwtSymbol &QwtMarker::symbol() const 
00235 { 
00236     return d_sym; 
00237 }
00238 
00244 void QwtMarker::setLabelText(const QString &text)
00245 {
00246     setLabel(text, d_label->font(), d_label->color(), 
00247         d_label->rectPen(), d_label->rectBrush());
00248 }
00249 
00259 void QwtMarker::setLabel(const QString &text, const QFont &font,
00260     const QColor &color, const QPen &pen, const QBrush &brush)
00261 {
00262     if ( text == d_label->text()
00263          && font == d_label->font()
00264          && color == d_label->color()
00265          && pen == d_label->rectPen()
00266          && brush == d_label->rectBrush() )
00267         return;
00268     
00269     QwtText *label = QwtText::makeText(
00270         text, d_label->alignment(), font, color, pen, brush);
00271 
00272     delete d_label;
00273     d_label = label;
00274 
00275     markerChanged();
00276 }
00277 
00282 const QString QwtMarker::label() const 
00283 { 
00284     return d_label->text(); 
00285 }
00286 
00298 void QwtMarker::setLabelAlignment(int align)
00299 {
00300     if ( align == d_align )
00301         return;
00302     
00303     d_align = align;
00304     markerChanged();
00305 }
00306 
00311 int QwtMarker::labelAlignment() const 
00312 { 
00313     return d_align; 
00314 }
00315 
00321 void QwtMarker::setLinePen(const QPen &p)
00322 {
00323     if ( p != d_pen )
00324     {
00325         d_pen = p;
00326         markerChanged();
00327     }
00328 }
00329 
00334 const QPen &QwtMarker::linePen() const 
00335 { 
00336     return d_pen; 
00337 }
00338 
00344 void QwtMarker::setLabelColor(const QColor &color)
00345 {
00346     if ( color == d_label->color() )
00347         return;
00348     
00349     d_label->setColor(color);
00350     markerChanged();
00351 }
00352 
00357 const QColor QwtMarker::labelColor() const 
00358 { 
00359     return d_label->color(); 
00360 }
00361 
00368 void QwtMarker::setLabelPen(const QPen &p)
00369 {
00370     if ( p == QPen(d_label->color()) )
00371         return;
00372     
00373     d_label->setColor(p.color());
00374     markerChanged();
00375 }
00376 
00382 const QPen QwtMarker::labelPen() const 
00383 { 
00384     return QPen(d_label->color()); 
00385 }
00386 
00395 void QwtMarker::markerChanged() 
00396 {
00397 }
00398 
00399 // Local Variables:
00400 // mode: C++
00401 // c-file-style: "stroustrup"
00402 // indent-tabs-mode: nil
00403 // End:
00404 

Generated on Sun Nov 21 11:12:43 2004 for Qwt User's Guide by doxygen 1.3.5