- Main Page
- Classes
- Files
- 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_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
    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     switch(d_style)
    00119     {
    00120         case QwtSymbol::Ellipse:
    00121             QwtPainter::drawEllipse(painter, r);
    00122             break;
    00123         case QwtSymbol::Rect:
    00124             QwtPainter::drawRect(painter, r);
    00125             break;
    00126         case QwtSymbol::Diamond:
    00127         {
    00128             const int w2 = r.width() / 2;
    00129             const int h2 = r.height() / 2;
    00130
    00131             QwtPolygon 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         {
    00141             const int w2 = r.width() / 2;
    00142             const int h2 = r.height() / 2;
    00143
    00144             QwtPainter::drawLine(painter, r.x() + w2, r.y(),
    00145                 r.x() + w2, r.bottom());
    00146             QwtPainter::drawLine(painter, r.x(), r.y() + h2,
    00147                 r.right(), r.y() + h2);
    00148             break;
    00149         }
    00150         case QwtSymbol::XCross:
    00151         {
    00152             QwtPainter::drawLine(painter, r.left(), r.top(),
    00153                 r.right(), r.bottom());
    00154             QwtPainter::drawLine(painter, r.left(), r.bottom(),
    00155                 r.right(), r.top());
    00156             break;
    00157         }
    00158         case QwtSymbol::Triangle:
    00159         case QwtSymbol::UTriangle:
    00160         {
    00161             const int w2 = r.width() / 2;
    00162
    00163             QwtPolygon pa(3);
    00164             pa.setPoint(0, r.x() + w2, r.y());
    00165             pa.setPoint(1, r.right(), r.bottom());
    00166             pa.setPoint(2, r.x(), r.bottom());
    00167             QwtPainter::drawPolygon(painter, pa);
    00168             break;
    00169         }
    00170         case QwtSymbol::DTriangle:
    00171         {
    00172             const int w2 = r.width() / 2;
    00173
    00174             QwtPolygon pa(3);
    00175             pa.setPoint(0, r.x(), r.y());
    00176             pa.setPoint(1, r.right(), r.y());
    00177             pa.setPoint(2, r.x() + w2, r.bottom());
    00178             QwtPainter::drawPolygon(painter, pa);
    00179             break;
    00180         }
    00181         case QwtSymbol::RTriangle:
    00182         {
    00183             const int h2 = r.height() / 2;
    00184
    00185             QwtPolygon pa(3);
    00186             pa.setPoint(0, r.x(), r.y());
    00187             pa.setPoint(1, r.right(), r.y() + h2);
    00188             pa.setPoint(2, r.x(), r.bottom());
    00189             QwtPainter::drawPolygon(painter, pa);
    00190             break;
    00191         }
    00192         case QwtSymbol::LTriangle:
    00193         {
    00194             const int h2 = r.height() / 2;
    00195
    00196             QwtPolygon pa(3);
    00197             pa.setPoint(0, r.right(), r.y());
    00198             pa.setPoint(1, r.x(), r.y() + h2);
    00199             pa.setPoint(2, r.right(), r.bottom());
    00200             QwtPainter::drawPolygon(painter, pa);
    00201             break;
    00202         }
    00203         case QwtSymbol::HLine:
    00204         {
    00205             const int h2 = r.height() / 2;
    00206             QwtPainter::drawLine(painter, r.left(), r.top() + h2,
    00207                     r.right(), r.top() + h2);
    00208             break;
    00209         }
    00210         case QwtSymbol::VLine:
    00211         {
    00212             const int w2 = r.width() / 2;
    00213             QwtPainter::drawLine(painter, r.left() + w2, r.top(),
    00214                     r.left() + w2, r.bottom());
    00215             break;
    00216         }
    00217         case QwtSymbol::Star1:
    00218         {
    00219             const double sqrt1_2 = 0.70710678118654752440; /* 1/sqrt(2) */
    00220
    00221             const int w2 = r.width() / 2;
    00222             const int h2 = r.height() / 2;
    00223             const int d1  = (int)( (double)w2 * (1.0 - sqrt1_2) );
    00224
    00225             QwtPainter::drawLine(painter, r.left() + d1, r.top() + d1,
    00226                     r.right() - d1, r.bottom() - d1);
    00227             QwtPainter::drawLine(painter, r.left() + d1, r.bottom() - d1,
    00228                     r.right() - d1, r.top() + d1);
    00229             QwtPainter::drawLine(painter, r.left() + w2, r.top(),
    00230                     r.left() + w2, r.bottom());
    00231             QwtPainter::drawLine(painter, r.left(), r.top() + h2,
    00232                     r.right(), r.top() + h2);
    00233             break;
    00234         }
    00235         case QwtSymbol::Star2:
    00236         {
    00237             const int w = r.width();
    00238             const int side = (int)(((double)r.width() * (1.0 - 0.866025)) /
    00239                 2.0);  // 0.866025 = cos(30°)
    00240             const int h4 = r.height() / 4;
    00241             const int h2 = r.height() / 2;
    00242             const int h34 = (r.height() * 3) / 4;
    00243
    00244             QwtPolygon pa(12);
    00245             pa.setPoint(0, r.left() + (w / 2), r.top());
    00246             pa.setPoint(1, r.right() - (side + (w - 2 * side) / 3),
    00247                 r.top() + h4 );
    00248             pa.setPoint(2, r.right() - side, r.top() + h4);
    00249             pa.setPoint(3, r.right() - (side + (w / 2 - side) / 3),
    00250                 r.top() + h2 );
    00251             pa.setPoint(4, r.right() - side, r.top() + h34);
    00252             pa.setPoint(5, r.right() - (side + (w - 2 * side) / 3),
    00253                 r.top() + h34 );
    00254             pa.setPoint(6, r.left() + (w / 2), r.bottom());
    00255             pa.setPoint(7, r.left() + (side + (w - 2 * side) / 3),
    00256                 r.top() + h34 );
    00257             pa.setPoint(8, r.left() + side, r.top() + h34);
    00258             pa.setPoint(9, r.left() + (side + (w / 2 - side) / 3),
    00259                 r.top() + h2 );
    00260             pa.setPoint(10, r.left() + side, r.top() + h4);
    00261             pa.setPoint(11, r.left() + (side + (w - 2 * side) / 3),
    00262                 r.top() + h4 );
    00263             QwtPainter::drawPolygon(painter, pa);
    00264             break;
    00265         }
    00266         case QwtSymbol::Hexagon:
    00267         {
    00268             const int w2 = r.width() / 2;
    00269             const int side = (int)(((double)r.width() * (1.0 - 0.866025)) /
    00270                 2.0);  // 0.866025 = cos(30°)
    00271             const int h4 = r.height() / 4;
    00272             const int h34 = (r.height() * 3) / 4;
    00273
    00274             QwtPolygon pa(6);
    00275             pa.setPoint(0, r.left() + w2, r.top());
    00276             pa.setPoint(1, r.right() - side, r.top() + h4);
    00277             pa.setPoint(2, r.right() - side, r.top() + h34);
    00278             pa.setPoint(3, r.left() + w2, r.bottom());
    00279             pa.setPoint(4, r.left() + side, r.top() + h34);
    00280             pa.setPoint(5, r.left() + side, r.top() + h4);
    00281             QwtPainter::drawPolygon(painter, pa);
    00282             break;
    00283         }
    00284         default:;
    00285     }
    00286 }
    00287
    00294 void QwtSymbol::draw(QPainter *painter, const QPoint &pos) const
    00295 {
    00296     QRect rect;
    00297     rect.setSize(QwtPainter::metricsMap().screenToLayout(d_size));
    00298     rect.moveCenter(pos);
    00299
    00300     painter->setBrush(d_brush);
    00301     painter->setPen(d_pen);
    00302
    00303     draw(painter, rect);
    00304 }
    00305
    00329 void QwtSymbol::setStyle(QwtSymbol::Style s)
    00330 {
    00331     d_style = s;
    00332 }
    00333
    00335 bool QwtSymbol::operator==(const QwtSymbol &other) const
    00336 {
    00337     return brush() == other.brush() && pen() == other.pen()
    00338             && style() == other.style() && size() == other.size();
    00339 }
    00340
    00342 bool QwtSymbol::operator!=(const QwtSymbol &other) const
    00343 {
    00344     return !(*this == other);
    00345 }