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

qwt_symbol.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 <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 
00102 void QwtSymbol::draw(QPainter *p, int x, int y) const
00103 {
00104     // Implicit scaling to the device resolution of the paint device
00105     // Qt scales texts, fill patterns, implicitely, others like
00106     // rects not. We like to behave symbols like texts.
00107 
00108     QPaintDeviceMetrics m1(QApplication::desktop());
00109     QPaintDeviceMetrics m2(p->device());
00110 
00111     const double scaleMetricsX = 
00112         double(m2.logicalDpiX()) / double(m1.logicalDpiX());
00113     const double scaleMetricsY = 
00114         double(m2.logicalDpiY()) / double(m1.logicalDpiY());
00115 
00116     int w = (int)(d_size.width() * scaleMetricsX);
00117     int h = (int)(d_size.height() * scaleMetricsY);
00118 
00119     draw(p, QRect(x - w/2 , y - h/2, w, h));
00120 }
00121 
00122 
00133 void QwtSymbol::draw(QPainter *p, const QRect& r) const
00134 {
00135     const int w2 = r.width() / 2;
00136     const int h2 = r.height() / 2;
00137 
00138     p->setBrush(d_brush);
00139     p->setPen(d_pen);
00140     
00141     switch(d_style)
00142     {
00143         case QwtSymbol::Ellipse:
00144             QwtPainter::drawEllipse(p, r);
00145             break;
00146         case QwtSymbol::Rect:
00147             QwtPainter::drawRect(p, r);
00148             break;
00149         case QwtSymbol::Diamond:
00150         {
00151             QPointArray pa(4);
00152             pa.setPoint(0, r.x() + w2, r.y());
00153             pa.setPoint(1, r.right(), r.y() + h2);
00154             pa.setPoint(2, r.x() + w2, r.bottom());
00155             pa.setPoint(3, r.x(), r.y() + h2);
00156             QwtPainter::drawPolygon(p, pa);
00157             break;
00158         }
00159         case QwtSymbol::Cross:
00160             QwtPainter::drawLine(p, r.x() + w2, r.y(), r.x() + w2, r.bottom());
00161             QwtPainter::drawLine(p, r.x(), r.y() + h2, r.right(), r.y() + h2);
00162             break;
00163         case QwtSymbol::XCross:
00164             QwtPainter::drawLine(p, r.x(), r.y(), r.right(), r.bottom());
00165             QwtPainter::drawLine(p, r.x(), r.bottom(), r.right(), r.top());
00166             break;
00167         case QwtSymbol::Triangle:
00168         case QwtSymbol::UTriangle:
00169         {
00170             QPointArray 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(p, pa);
00175             break;
00176         }
00177         case QwtSymbol::DTriangle:
00178         {
00179             QPointArray pa(3);
00180             pa.setPoint(0, r.x(), r.y());
00181             pa.setPoint(1, r.right(), r.y());
00182             pa.setPoint(2, r.x() +  w2, r.bottom());
00183             QwtPainter::drawPolygon(p, pa);
00184             break;
00185         }
00186         case QwtSymbol::LTriangle:
00187         {
00188             QPointArray pa(3);
00189             pa.setPoint(0, r.x(), r.y());
00190             pa.setPoint(1, r.right(), r.y() + h2);
00191             pa.setPoint(2, r.x(), r.bottom());
00192             QwtPainter::drawPolygon(p, pa);
00193             break;
00194         }
00195         case QwtSymbol::RTriangle:
00196         {
00197             QPointArray pa(3);
00198             pa.setPoint(0, r.right(), r.y());
00199             pa.setPoint(1, r.x(), r.y() + h2);
00200             pa.setPoint(2, r.right(), r.bottom());
00201             QwtPainter::drawPolygon(p, pa);
00202             break;
00203         }
00204         default:;
00205     }
00206 }
00207 
00217 void QwtSymbol::draw(QPainter *p, const QPoint &pt) const
00218 {
00219     draw(p, pt.x(), pt.y());
00220 }
00221 
00239 void QwtSymbol::setStyle(QwtSymbol::Style s)
00240 {
00241     d_style = s;
00242 }
00243 
00245 bool QwtSymbol::operator==(const QwtSymbol &other) const
00246 {
00247     return brush() == other.brush() && pen() == other.pen()
00248             && style() == other.style() && size() == other.size();
00249 }
00250 
00252 bool QwtSymbol::operator!=(const QwtSymbol &other) const
00253 {
00254     return !(*this == other);
00255 }

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