Qwt User's Guide: qwt_symbol.cpp Source File

- Main Page
- Classes
- Files
- Related Pages

- File List
- File Members

# 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_polygon.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::NoSymbol)
    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
    00051 QwtSymbol *QwtSymbol::clone() const
    00052 {
    00053     QwtSymbol *other = new QwtSymbol;
    00054     *other = *this;
    00055
    00056     return other;
    00057 }
    00058
    00068 void QwtSymbol::setSize(int w, int h)
    00069 {
    00070     if ((w >= 0) && (h < 0))
    00071         h = w;
    00072     d_size = QSize(w,h);
    00073 }
    00074
    00076 void QwtSymbol::setSize(const QSize &s)
    00077 {
    00078     if (s.isValid())
    00079         d_size = s;
    00080 }
    00081
    00088 void QwtSymbol::setBrush(const QBrush &br)
    00089 {
    00090     d_brush = br;
    00091 }
    00092
    00100 void QwtSymbol::setPen(const QPen &pn)
    00101 {
    00102     d_pen = pn;
    00103 }
    00104
    00108 void QwtSymbol::draw(QPainter *painter, int x, int y) const
    00109 {
    00110     draw(painter, QPoint(x, y));
    00111 }
    00112
    00113
    00124 void QwtSymbol::draw(QPainter *painter, const QRect& r) const
    00125 {
    00126     switch(d_style)
    00127     {
    00128         case QwtSymbol::Ellipse:
    00129             QwtPainter::drawEllipse(painter, r);
    00130             break;
    00131         case QwtSymbol::Rect:
    00132             QwtPainter::drawRect(painter, r);
    00133             break;
    00134         case QwtSymbol::Diamond:
    00135         {
    00136             const int w2 = r.width() / 2;
    00137             const int h2 = r.height() / 2;
    00138
    00139             QwtPolygon pa(4);
    00140             pa.setPoint(0, r.x() + w2, r.y());
    00141             pa.setPoint(1, r.right(), r.y() + h2);
    00142             pa.setPoint(2, r.x() + w2, r.bottom());
    00143             pa.setPoint(3, r.x(), r.y() + h2);
    00144             QwtPainter::drawPolygon(painter, pa);
    00145             break;
    00146         }
    00147         case QwtSymbol::Cross:
    00148         {
    00149             const int w2 = r.width() / 2;
    00150             const int h2 = r.height() / 2;
    00151
    00152             QwtPainter::drawLine(painter, r.x() + w2, r.y(),
    00153                 r.x() + w2, r.bottom());
    00154             QwtPainter::drawLine(painter, r.x(), r.y() + h2,
    00155                 r.right(), r.y() + h2);
    00156             break;
    00157         }
    00158         case QwtSymbol::XCross:
    00159         {
    00160             QwtPainter::drawLine(painter, r.left(), r.top(),
    00161                 r.right(), r.bottom());
    00162             QwtPainter::drawLine(painter, r.left(), r.bottom(),
    00163                 r.right(), r.top());
    00164             break;
    00165         }
    00166         case QwtSymbol::Triangle:
    00167         case QwtSymbol::UTriangle:
    00168         {
    00169             const int w2 = r.width() / 2;
    00170
    00171             QwtPolygon pa(3);
    00172             pa.setPoint(0, r.x() + w2, r.y());
    00173             pa.setPoint(1, r.right(), r.bottom());
    00174             pa.setPoint(2, r.x(), r.bottom());
    00175             QwtPainter::drawPolygon(painter, pa);
    00176             break;
    00177         }
    00178         case QwtSymbol::DTriangle:
    00179         {
    00180             const int w2 = r.width() / 2;
    00181
    00182             QwtPolygon pa(3);
    00183             pa.setPoint(0, r.x(), r.y());
    00184             pa.setPoint(1, r.right(), r.y());
    00185             pa.setPoint(2, r.x() + w2, r.bottom());
    00186             QwtPainter::drawPolygon(painter, pa);
    00187             break;
    00188         }
    00189         case QwtSymbol::RTriangle:
    00190         {
    00191             const int h2 = r.height() / 2;
    00192
    00193             QwtPolygon pa(3);
    00194             pa.setPoint(0, r.x(), r.y());
    00195             pa.setPoint(1, r.right(), r.y() + h2);
    00196             pa.setPoint(2, r.x(), r.bottom());
    00197             QwtPainter::drawPolygon(painter, pa);
    00198             break;
    00199         }
    00200         case QwtSymbol::LTriangle:
    00201         {
    00202             const int h2 = r.height() / 2;
    00203
    00204             QwtPolygon pa(3);
    00205             pa.setPoint(0, r.right(), r.y());
    00206             pa.setPoint(1, r.x(), r.y() + h2);
    00207             pa.setPoint(2, r.right(), r.bottom());
    00208             QwtPainter::drawPolygon(painter, pa);
    00209             break;
    00210         }
    00211         case QwtSymbol::HLine:
    00212         {
    00213             const int h2 = r.height() / 2;
    00214             QwtPainter::drawLine(painter, r.left(), r.top() + h2,
    00215                     r.right(), r.top() + h2);
    00216             break;
    00217         }
    00218         case QwtSymbol::VLine:
    00219         {
    00220             const int w2 = r.width() / 2;
    00221             QwtPainter::drawLine(painter, r.left() + w2, r.top(),
    00222                     r.left() + w2, r.bottom());
    00223             break;
    00224         }
    00225         case QwtSymbol::Star1:
    00226         {
    00227             const double sqrt1_2 = 0.70710678118654752440; /* 1/sqrt(2) */
    00228
    00229             const int w2 = r.width() / 2;
    00230             const int h2 = r.height() / 2;
    00231             const int d1  = (int)( (double)w2 * (1.0 - sqrt1_2) );
    00232
    00233             QwtPainter::drawLine(painter, r.left() + d1, r.top() + d1,
    00234                     r.right() - d1, r.bottom() - d1);
    00235             QwtPainter::drawLine(painter, r.left() + d1, r.bottom() - d1,
    00236                     r.right() - d1, r.top() + d1);
    00237             QwtPainter::drawLine(painter, r.left() + w2, r.top(),
    00238                     r.left() + w2, r.bottom());
    00239             QwtPainter::drawLine(painter, r.left(), r.top() + h2,
    00240                     r.right(), r.top() + h2);
    00241             break;
    00242         }
    00243         case QwtSymbol::Star2:
    00244         {
    00245             const int w = r.width();
    00246             const int side = (int)(((double)r.width() * (1.0 - 0.866025)) /
    00247                 2.0);  // 0.866025 = cos(30°)
    00248             const int h4 = r.height() / 4;
    00249             const int h2 = r.height() / 2;
    00250             const int h34 = (r.height() * 3) / 4;
    00251
    00252             QwtPolygon pa(12);
    00253             pa.setPoint(0, r.left() + (w / 2), r.top());
    00254             pa.setPoint(1, r.right() - (side + (w - 2 * side) / 3),
    00255                 r.top() + h4 );
    00256             pa.setPoint(2, r.right() - side, r.top() + h4);
    00257             pa.setPoint(3, r.right() - (side + (w / 2 - side) / 3),
    00258                 r.top() + h2 );
    00259             pa.setPoint(4, r.right() - side, r.top() + h34);
    00260             pa.setPoint(5, r.right() - (side + (w - 2 * side) / 3),
    00261                 r.top() + h34 );
    00262             pa.setPoint(6, r.left() + (w / 2), r.bottom());
    00263             pa.setPoint(7, r.left() + (side + (w - 2 * side) / 3),
    00264                 r.top() + h34 );
    00265             pa.setPoint(8, r.left() + side, r.top() + h34);
    00266             pa.setPoint(9, r.left() + (side + (w / 2 - side) / 3),
    00267                 r.top() + h2 );
    00268             pa.setPoint(10, r.left() + side, r.top() + h4);
    00269             pa.setPoint(11, r.left() + (side + (w - 2 * side) / 3),
    00270                 r.top() + h4 );
    00271             QwtPainter::drawPolygon(painter, pa);
    00272             break;
    00273         }
    00274         case QwtSymbol::Hexagon:
    00275         {
    00276             const int w2 = r.width() / 2;
    00277             const int side = (int)(((double)r.width() * (1.0 - 0.866025)) /
    00278                 2.0);  // 0.866025 = cos(30°)
    00279             const int h4 = r.height() / 4;
    00280             const int h34 = (r.height() * 3) / 4;
    00281
    00282             QwtPolygon pa(6);
    00283             pa.setPoint(0, r.left() + w2, r.top());
    00284             pa.setPoint(1, r.right() - side, r.top() + h4);
    00285             pa.setPoint(2, r.right() - side, r.top() + h34);
    00286             pa.setPoint(3, r.left() + w2, r.bottom());
    00287             pa.setPoint(4, r.left() + side, r.top() + h34);
    00288             pa.setPoint(5, r.left() + side, r.top() + h4);
    00289             QwtPainter::drawPolygon(painter, pa);
    00290             break;
    00291         }
    00292         default:;
    00293     }
    00294 }
    00295
    00302 void QwtSymbol::draw(QPainter *painter, const QPoint &pos) const
    00303 {
    00304     QRect rect;
    00305     rect.setSize(QwtPainter::metricsMap().screenToLayout(d_size));
    00306     rect.moveCenter(pos);
    00307
    00308     painter->setBrush(d_brush);
    00309     painter->setPen(d_pen);
    00310
    00311     draw(painter, rect);
    00312 }
    00313
    00337 void QwtSymbol::setStyle(QwtSymbol::Style s)
    00338 {
    00339     d_style = s;
    00340 }
    00341
    00343 bool QwtSymbol::operator==(const QwtSymbol &other) const
    00344 {
    00345     return brush() == other.brush() && pen() == other.pen()
    00346             && style() == other.style() && size() == other.size();
    00347 }
    00348
    00350 bool QwtSymbol::operator!=(const QwtSymbol &other) const
    00351 {
    00352     return !(*this == other);
    00353 }