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

qwt_marker.cpp

Go to the documentation of this file.
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 #include <qpainter.h>
00011 #include "qwt_painter.h"
00012 #include "qwt_marker.h"
00013 #include "qwt_math.h"
00014 
00015 static const int LabelDist = 2;
00016 
00018 QwtMarker::QwtMarker()
00019 {
00020     d_align = Qt::AlignCenter;
00021     d_style = NoLine;
00022 }
00023 
00025 QwtMarker::~QwtMarker()
00026 {
00027 }
00028 
00033 const QwtMarker& QwtMarker::operator=(const QwtMarker &m)
00034 {
00035     d_label = m.d_label;
00036     d_pen = m.d_pen;
00037     d_tPen = m.d_tPen;
00038     d_font = m.d_font;
00039     d_sym = m.d_sym;
00040     d_align = m.d_align;
00041     d_style = m.d_style;
00042 
00043     return *this;
00044 }
00045 
00046 
00054 void QwtMarker::draw(QPainter *p, int x, int y, const QRect &r)
00055 {
00056     // draw lines
00057     if (d_style != NoLine)
00058     {
00059         p->setPen(d_pen);
00060         if ((d_style == HLine) || (d_style == Cross))
00061             QwtPainter::drawLine(p, r.left(), y, r.right(), y);
00062         if ((d_style == VLine)||(d_style == Cross))
00063             QwtPainter::drawLine(p, x, r.top(), x, r.bottom());
00064     }
00065 
00066     // draw symbol
00067     QSize sSym(0, 0);
00068     if (d_sym.style() != QwtSymbol::None)
00069     {
00070         sSym = d_sym.size();
00071         d_sym.draw(p, x, y);
00072     }
00073 
00074     // draw label
00075     if (!d_label.isEmpty())
00076     {
00077         p->setPen(d_tPen);
00078         p->setFont(d_font);
00079 
00080     QFontMetrics fm(p->fontMetrics());
00081         const int th = fm.height();
00082         const int tw = fm.width(d_label);
00083         int lw = qwtMax(int(d_pen.width()), 1);
00084         int lw1;
00085 
00086         if ((d_style == VLine) || (d_style == HLine))
00087         {
00088             lw1 = (lw + 1) / 2 + LabelDist;
00089             lw = lw / 2 + LabelDist;
00090         }
00091         else 
00092         {
00093             lw1 = qwtMax((lw + 1) / 2, (sSym.width() + 1) / 2) + LabelDist;
00094             lw = qwtMax(lw / 2, (sSym.width() + 1) / 2) + LabelDist;
00095         }
00096 
00097         
00098         QRect tr;
00099         if (d_style == VLine)
00100         {
00101             if (d_align & (int) Qt::AlignTop)
00102                tr.setY(r.top() + LabelDist);
00103             else if (d_align & (int) Qt::AlignBottom)
00104                tr.setY(r.bottom() - LabelDist - th);
00105             else
00106                tr.setY(r.top() + (r.bottom() - r.top()) / 2);
00107         }
00108         else 
00109         {
00110             if (d_align & (int) Qt::AlignTop)
00111                tr.setY(y - lw - th);
00112             else if (d_align & (int) Qt::AlignBottom)
00113                tr.setY(y + lw1);
00114             else
00115                tr.setY(y - th/2);
00116         }
00117 
00118 
00119         if (d_style == HLine)
00120         {
00121             if (d_align & (int) Qt::AlignLeft)
00122                tr.setX(r.left() + LabelDist);
00123             else if (d_align & (int) Qt::AlignRight)
00124                tr.setX(r.right() - tw - LabelDist);
00125             else
00126                tr.setX(r.left() + (r.right() - r.left()) / 2);
00127         }
00128         else
00129         {
00130             if (d_align & (int) Qt::AlignLeft)
00131                tr.setX(x - tw - lw);
00132             else if (d_align & (int) Qt::AlignRight)
00133                tr.setX(x + lw1);
00134             else
00135                tr.setX(x - tw/ 2);
00136         }
00137 
00138         tr.setHeight(th);
00139         tr.setWidth(tw);    
00140 
00141         QwtPainter::drawText(p, tr, Qt::AlignTop|Qt::AlignHCenter, d_label); 
00142     }
00143 }
00144 
00150 void QwtMarker::setFont(const QFont &f)
00151 {
00152     if ( f != d_font )
00153     {
00154         d_font = f;
00155         markerChanged();
00156     }
00157 }
00158 
00163 const QFont &QwtMarker::font() const 
00164 { 
00165     return d_font; 
00166 }
00167 
00168 
00175 void QwtMarker::setLineStyle(QwtMarker::LineStyle st)
00176 {
00177     if ( st != d_style )
00178     {
00179         d_style = st;
00180         markerChanged();
00181     }
00182 }
00183 
00188 QwtMarker::LineStyle QwtMarker::lineStyle() const 
00189 { 
00190     return d_style; 
00191 }
00192 
00198 void QwtMarker::setSymbol(const QwtSymbol &s)
00199 {
00200     d_sym = s;
00201     markerChanged();
00202 }
00203 
00208 const QwtSymbol &QwtMarker::symbol() const 
00209 { 
00210     return d_sym; 
00211 }
00212 
00218 void QwtMarker::setLabel(const QString &txt)
00219 {
00220     if ( txt != d_label )
00221     {
00222         d_label = txt;
00223         markerChanged();
00224     }
00225 }
00226 
00231 const QString& QwtMarker::label() const 
00232 { 
00233     return d_label; 
00234 }
00235 
00247 void QwtMarker::setLabelAlignment(int align)
00248 {
00249     if ( align != d_align )
00250     {
00251         d_align = align;
00252         markerChanged();
00253     }
00254 }
00255 
00260 int QwtMarker::labelAlignment() const 
00261 { 
00262     return d_align; 
00263 }
00264 
00270 void QwtMarker::setLinePen(const QPen &p)
00271 {
00272     if ( p != d_pen )
00273     {
00274         d_pen = p;
00275         markerChanged();
00276     }
00277 }
00278 
00283 const QPen &QwtMarker::linePen() const 
00284 { 
00285     return d_pen; 
00286 }
00287 
00293 void QwtMarker::setLabelPen(const QPen &p)
00294 {
00295     if ( p != d_tPen )
00296     {
00297         d_tPen = p;
00298         markerChanged();
00299     }
00300 }
00301 
00306 const QPen &QwtMarker::labelPen() const 
00307 { 
00308     return d_tPen; 
00309 }
00310 
00319 void QwtMarker::markerChanged() 
00320 {
00321 }
00322 

Generated on Fri Nov 7 14:11:45 2003 for Qwt Developer's Guide by doxygen 1.3.2