Main Page | Class Hierarchy | Alphabetical List | Class List | 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 <qpaintdevicemetrics.h>
    00012 #include <qapplication.h>
    00013 #include "qwt_painter.h"
    00014 #include "qwt_symbol.h"
    00015
    00022 QwtSymbol::QwtSymbol():
    00023     d_brush(Qt::gray),
    00024     d_pen(Qt::black),
    00025     d_size(0,0),
    00026     d_style(QwtSymbol::None)
    00027 {
    00028 }
    00029
    00037 QwtSymbol::QwtSymbol(QwtSymbol::Style style, const QBrush &brush,
    00038         const QPen &pen, const QSize &size):
    00039     d_brush(brush),
    00040     d_pen(pen),
    00041     d_size(size),
    00042     d_style(style)
    00043 {
    00044 }
    00045
    00047 QwtSymbol::~QwtSymbol()
    00048 {
    00049 }
    00050
    00060 void QwtSymbol::setSize(int w, int h)
    00061 {
    00062     if ((w >= 0) && (h < 0))
    00063         h = w;
    00064     d_size = QSize(w,h);
    00065 }
    00066
    00068 void QwtSymbol::setSize(const QSize &s)
    00069 {
    00070     if (s.isValid())
    00071         d_size = s;
    00072 }
    00073
    00080 void QwtSymbol::setBrush(const QBrush &br)
    00081 {
    00082     d_brush = br;
    00083 }
    00084
    00092 void QwtSymbol::setPen(const QPen &pn)
    00093 {
    00094     d_pen = pn;
    00095 }
    00096
    00100 void QwtSymbol::draw(QPainter *painter, int x, int y) const
    00101 {
    00102     draw(painter, QPoint(x, y));
    00103 }
    00104
    00105
    00116 void QwtSymbol::draw(QPainter *painter, const QRect& r) const
    00117 {
    00118     const int w2 = r.width() / 2;
    00119     const int h2 = r.height() / 2;
    00120
    00121     switch(d_style)
    00122     {
    00123         case QwtSymbol::Ellipse:
    00124             QwtPainter::drawEllipse(painter, r);
    00125             break;
    00126         case QwtSymbol::Rect:
    00127             QwtPainter::drawRect(painter, r);
    00128             break;
    00129         case QwtSymbol::Diamond:
    00130         {
    00131             QPointArray pa(4);
    00132             pa.setPoint(0, r.x() + w2, r.y());
    00133             pa.setPoint(1, r.right(), r.y() + h2);
    00134             pa.setPoint(2, r.x() + w2, r.bottom());
    00135             pa.setPoint(3, r.x(), r.y() + h2);
    00136             QwtPainter::drawPolygon(painter, pa);
    00137             break;
    00138         }
    00139         case QwtSymbol::Cross:
    00140             QwtPainter::drawLine(painter, r.x() + w2, r.y(),
    00141                 r.x() + w2, r.bottom());
    00142             QwtPainter::drawLine(painter, r.x(), r.y() + h2,
    00143                 r.right(), r.y() + h2);
    00144             break;
    00145         case QwtSymbol::XCross:
    00146             QwtPainter::drawLine(painter, r.left(), r.top(),
    00147                 r.right(), r.bottom());
    00148             QwtPainter::drawLine(painter, r.left(), r.bottom(),
    00149                 r.right(), r.top());
    00150             break;
    00151         case QwtSymbol::Triangle:
    00152         case QwtSymbol::UTriangle:
    00153         {
    00154             QPointArray pa(3);
    00155             pa.setPoint(0, r.x() + w2, r.y());
    00156             pa.setPoint(1, r.right(), r.bottom());
    00157             pa.setPoint(2, r.x(), r.bottom());
    00158             QwtPainter::drawPolygon(painter, pa);
    00159             break;
    00160         }
    00161         case QwtSymbol::DTriangle:
    00162         {
    00163             QPointArray pa(3);
    00164             pa.setPoint(0, r.x(), r.y());
    00165             pa.setPoint(1, r.right(), r.y());
    00166             pa.setPoint(2, r.x() +  w2, r.bottom());
    00167             QwtPainter::drawPolygon(painter, pa);
    00168             break;
    00169         }
    00170         case QwtSymbol::RTriangle:
    00171         {
    00172             QPointArray pa(3);
    00173             pa.setPoint(0, r.x(), r.y());
    00174             pa.setPoint(1, r.right(), r.y() + h2);
    00175             pa.setPoint(2, r.x(), r.bottom());
    00176             QwtPainter::drawPolygon(painter, pa);
    00177             break;
    00178         }
    00179         case QwtSymbol::LTriangle:
    00180         {
    00181             QPointArray pa(3);
    00182             pa.setPoint(0, r.right(), r.y());
    00183             pa.setPoint(1, r.x(), r.y() + h2);
    00184             pa.setPoint(2, r.right(), r.bottom());
    00185             QwtPainter::drawPolygon(painter, pa);
    00186             break;
    00187         }
    00188         default:;
    00189     }
    00190 }
    00191
    00198 void QwtSymbol::draw(QPainter *painter, const QPoint &pos) const
    00199 {
    00200     QRect rect;
    00201     rect.setSize(QwtPainter::metricsMap().screenToLayout(d_size));
    00202     rect.moveCenter(pos);
    00203
    00204     painter->setBrush(d_brush);
    00205     painter->setPen(d_pen);
    00206
    00207     draw(painter, rect);
    00208 }
    00209
    00227 void QwtSymbol::setStyle(QwtSymbol::Style s)
    00228 {
    00229     d_style = s;
    00230 }
    00231
    00233 bool QwtSymbol::operator==(const QwtSymbol &other) const
    00234 {
    00235     return brush() == other.brush() && pen() == other.pen()
    00236             && style() == other.style() && size() == other.size();
    00237 }
    00238
    00240 bool QwtSymbol::operator!=(const QwtSymbol &other) const
    00241 {
    00242     return !(*this == other);
    00243 }