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

qwt_symbol.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 #include <qpainter.h>
00011 #include <qapplication.h>
00012 #include "qwt_painter.h"
00013 #include "qwt_symbol.h"
00014 
00015 #if QT_VERSION < 0x040000
00016 #include <qpointarray.h>
00017 #define QwtPointArray QPointArray
00018 #else
00019 #include <qpolygon.h>
00020 #define QwtPointArray QPolygon
00021 #endif
00022 
00029 QwtSymbol::QwtSymbol(): 
00030     d_brush(Qt::gray), 
00031     d_pen(Qt::black), 
00032     d_size(0,0),
00033     d_style(QwtSymbol::None)
00034 {
00035 }
00036 
00044 QwtSymbol::QwtSymbol(QwtSymbol::Style style, const QBrush &brush, 
00045         const QPen &pen, const QSize &size): 
00046     d_brush(brush), 
00047     d_pen(pen), 
00048     d_size(size),
00049     d_style(style)
00050 {
00051 }
00052 
00054 QwtSymbol::~QwtSymbol()
00055 {
00056 }
00057 
00067 void QwtSymbol::setSize(int w, int h)
00068 {
00069     if ((w >= 0) && (h < 0)) 
00070         h = w;
00071     d_size = QSize(w,h);
00072 }
00073 
00075 void QwtSymbol::setSize(const QSize &s)
00076 {
00077     if (s.isValid()) 
00078         d_size = s;
00079 }
00080 
00087 void QwtSymbol::setBrush(const QBrush &br)
00088 {
00089     d_brush = br;
00090 }
00091 
00099 void QwtSymbol::setPen(const QPen &pn)
00100 {
00101     d_pen = pn;
00102 }
00103 
00107 void QwtSymbol::draw(QPainter *painter, int x, int y) const
00108 {
00109     draw(painter, QPoint(x, y));
00110 }
00111 
00112 
00123 void QwtSymbol::draw(QPainter *painter, const QRect& r) const
00124 {
00125     switch(d_style)
00126     {
00127         case QwtSymbol::Ellipse:
00128             QwtPainter::drawEllipse(painter, r);
00129             break;
00130         case QwtSymbol::Rect:
00131             QwtPainter::drawRect(painter, r);
00132             break;
00133         case QwtSymbol::Diamond:
00134         {
00135             const int w2 = r.width() / 2;
00136             const int h2 = r.height() / 2;
00137 
00138             QwtPointArray pa(4);
00139             pa.setPoint(0, r.x() + w2, r.y());
00140             pa.setPoint(1, r.right(), r.y() + h2);
00141             pa.setPoint(2, r.x() + w2, r.bottom());
00142             pa.setPoint(3, r.x(), r.y() + h2);
00143             QwtPainter::drawPolygon(painter, pa);
00144             break;
00145         }
00146         case QwtSymbol::Cross:
00147         {
00148             const int w2 = r.width() / 2;
00149             const int h2 = r.height() / 2;
00150 
00151             QwtPainter::drawLine(painter, r.x() + w2, r.y(), 
00152                 r.x() + w2, r.bottom());
00153             QwtPainter::drawLine(painter, r.x(), r.y() + h2, 
00154                 r.right(), r.y() + h2);
00155             break;
00156         }
00157         case QwtSymbol::XCross:
00158         {
00159             QwtPainter::drawLine(painter, r.left(), r.top(), 
00160                 r.right(), r.bottom());
00161             QwtPainter::drawLine(painter, r.left(), r.bottom(), 
00162                 r.right(), r.top());
00163             break;
00164         }
00165         case QwtSymbol::Triangle:
00166         case QwtSymbol::UTriangle:
00167         {
00168             const int w2 = r.width() / 2;
00169 
00170             QwtPointArray pa(3);
00171             pa.setPoint(0, r.x() + w2, r.y());
00172             pa.setPoint(1, r.right(), r.bottom());
00173             pa.setPoint(2, r.x(), r.bottom());
00174             QwtPainter::drawPolygon(painter, pa);
00175             break;
00176         }
00177         case QwtSymbol::DTriangle:
00178         {
00179             const int w2 = r.width() / 2;
00180 
00181             QwtPointArray pa(3);
00182             pa.setPoint(0, r.x(), r.y());
00183             pa.setPoint(1, r.right(), r.y());
00184             pa.setPoint(2, r.x() + w2, r.bottom());
00185             QwtPainter::drawPolygon(painter, pa);
00186             break;
00187         }
00188         case QwtSymbol::RTriangle:
00189         {
00190             const int h2 = r.height() / 2;
00191 
00192             QwtPointArray pa(3);
00193             pa.setPoint(0, r.x(), r.y());
00194             pa.setPoint(1, r.right(), r.y() + h2);
00195             pa.setPoint(2, r.x(), r.bottom());
00196             QwtPainter::drawPolygon(painter, pa);
00197             break;
00198         }
00199         case QwtSymbol::LTriangle:
00200         {
00201             const int h2 = r.height() / 2;
00202 
00203             QwtPointArray pa(3);
00204             pa.setPoint(0, r.right(), r.y());
00205             pa.setPoint(1, r.x(), r.y() + h2);
00206             pa.setPoint(2, r.right(), r.bottom());
00207             QwtPainter::drawPolygon(painter, pa);
00208             break;
00209         }
00210         default:;
00211     }
00212 }
00213 
00220 void QwtSymbol::draw(QPainter *painter, const QPoint &pos) const
00221 {
00222     QRect rect;
00223     rect.setSize(QwtPainter::metricsMap().screenToLayout(d_size));
00224     rect.moveCenter(pos);
00225 
00226     painter->setBrush(d_brush);
00227     painter->setPen(d_pen);
00228     
00229     draw(painter, rect);
00230 }
00231 
00249 void QwtSymbol::setStyle(QwtSymbol::Style s)
00250 {
00251     d_style = s;
00252 }
00253 
00255 bool QwtSymbol::operator==(const QwtSymbol &other) const
00256 {
00257     return brush() == other.brush() && pen() == other.pen()
00258             && style() == other.style() && size() == other.size();
00259 }
00260 
00262 bool QwtSymbol::operator!=(const QwtSymbol &other) const
00263 {
00264     return !(*this == other);
00265 }

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