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

qwt_compass_rose.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 <math.h>
00011 #include <qpainter.h>
00012 #include "qwt_math.h"
00013 #include "qwt_painter.h"
00014 #include "qwt_compass_rose.h"
00015 
00016 static QPoint cutPoint(QPoint p11, QPoint p12, QPoint p21, QPoint p22)
00017 {
00018   double dx1 = p12.x() - p11.x();
00019   double dy1 = p12.y() - p11.y();
00020   double dx2 = p22.x() - p21.x();
00021   double dy2 = p22.y() - p21.y();
00022 
00023   if ( dx1 == 0.0 && dx2 == 0.0 )
00024     return QPoint();
00025 
00026   if ( dx1 == 0.0 )
00027   {
00028     const double m = dy2 / dx2;
00029     const double t = p21.y() - m * p21.x();
00030     return QPoint(p11.x(), qwtInt(m * p11.x() + t));
00031   }
00032 
00033   if ( dx2 == 0 )
00034   {
00035     const double m = dy1 / dx1;
00036     const double t = p11.y() - m * p11.x();
00037     return QPoint(p21.x(), qwtInt(m * p21.x() + t));
00038   }
00039 
00040   const double m1 = dy1 / dx1;
00041   const double t1 = p11.y() - m1 * p11.x();
00042 
00043   const double m2 = dy2 / dx2;
00044   const double t2 = p21.y() - m2 * p21.x();
00045 
00046   if ( m1 == m2 )
00047     return QPoint();
00048 
00049   const double x = ( t2 - t1 ) / ( m1 - m2 );
00050   const double y = t1 + m1 * x;
00051 
00052   return QPoint(qwtInt(x), qwtInt(y));
00053 }
00054 
00055 QwtSimpleCompassRose::QwtSimpleCompassRose(int numThorns, int numThornLevels):
00056   d_width(0.2),
00057   d_numThorns(numThorns),
00058   d_numThornLevels(numThornLevels),
00059   d_shrinkFactor(0.9)
00060 {
00061     const QColor dark(128,128,255);
00062     const QColor light(192,255,255);
00063     
00064   QPalette palette;
00065     for ( int i = 0; i < QPalette::NColorGroups; i++ )
00066     {
00067         palette.setColor((QPalette::ColorGroup)i,
00068             QColorGroup::Dark, dark);
00069         palette.setColor((QPalette::ColorGroup)i,
00070             QColorGroup::Light, light);
00071     }
00072 
00073   setPalette(palette);
00074 }
00075 
00076 void QwtSimpleCompassRose::draw(QPainter *painter, const QPoint &center, 
00077   int radius, double north, QPalette::ColorGroup cg) const
00078 {
00079   QColorGroup colorGroup;
00080   switch(cg)
00081   {
00082     case QPalette::Disabled:
00083       colorGroup = palette().disabled();
00084     case QPalette::Inactive:
00085       colorGroup = palette().inactive();
00086     default:
00087       colorGroup = palette().active();
00088   }
00089 
00090   drawRose(painter, center, radius, north, d_width, 
00091     d_numThorns, d_numThornLevels, d_shrinkFactor,
00092     colorGroup.light(), colorGroup.dark());
00093 }
00094 
00095 void QwtSimpleCompassRose::drawRose(QPainter *painter, 
00096   const QPoint &center, int radius, double north, double width,
00097   int numThorns, int numThornLevels, double shrinkFactor,
00098   const QColor &light, const QColor &dark)
00099 {
00100     if ( numThorns < 4 )
00101         numThorns = 4;
00102 
00103     if ( numThorns % 4 )
00104         numThorns += 4 - numThorns % 4;
00105 
00106   if ( numThornLevels <= 0 )
00107     numThornLevels = numThorns / 4;
00108 
00109   if ( shrinkFactor >= 1.0 )
00110     shrinkFactor = 1.0;
00111 
00112   if ( shrinkFactor <= 0.5 )
00113     shrinkFactor = 0.5;
00114 
00115   painter->save();
00116 
00117   painter->setPen(Qt::NoPen);
00118 
00119   for ( int j = 1; j <= numThornLevels; j++ )
00120   {
00121     double step =  pow(2.0, j) * M_PI / (double)numThorns;
00122     if ( step > M_PI_2 )
00123       break;
00124 
00125     double r = radius;
00126     for ( int k = 0; k < 3; k++ )
00127     {
00128       if ( j + k < numThornLevels )
00129         r *= shrinkFactor;
00130     }
00131 
00132     double leafWidth = r * width;
00133     if ( 2.0 * M_PI / step > 32 )
00134       leafWidth = 16;
00135 
00136     const double origin = north / 180.0 * M_PI;
00137     for ( double angle = origin; 
00138       angle < 2.0 * M_PI + origin; angle += step)
00139     {
00140       const QPoint p = qwtPolar2Pos(center, r, angle);
00141       QPoint p1 = qwtPolar2Pos(center, leafWidth, angle + M_PI_2);
00142       QPoint p2 = qwtPolar2Pos(center, leafWidth, angle - M_PI_2);
00143 
00144       QPointArray pa(3);
00145       pa.setPoint(0, center);
00146       pa.setPoint(1, p);
00147 
00148       QPoint p3 = qwtPolar2Pos(center, r, angle + step / 2.0);
00149       p1 = cutPoint(center, p3, p1, p);
00150       pa.setPoint(2, p1);
00151       painter->setBrush(dark);
00152       painter->drawPolygon(pa);
00153 
00154       QPoint p4 = qwtPolar2Pos(center, r, angle - step / 2.0);
00155       p2 = cutPoint(center, p4, p2, p);
00156 
00157       pa.setPoint(2, p2);
00158       painter->setBrush(light);
00159       painter->drawPolygon(pa);
00160     }
00161   }
00162   painter->restore();
00163 }
00164 
00170 void QwtSimpleCompassRose::setWidth(double w) 
00171 {
00172    d_width = w;
00173    if (d_width < 0.03) 
00174     d_width = 0.03;
00175 
00176    if (d_width > 0.4) 
00177     d_width = 0.4;
00178 }
00179 
00180 void QwtSimpleCompassRose::setNumThorns(int numThorns) 
00181 {
00182   if ( numThorns < 4 )
00183     numThorns = 4;
00184 
00185   if ( numThorns % 4 )
00186     numThorns += 4 - numThorns % 4;
00187 
00188   d_numThorns = numThorns;
00189 }
00190 
00191 int QwtSimpleCompassRose::numThorns() const
00192 {
00193    return d_numThorns;
00194 }
00195 
00196 void QwtSimpleCompassRose::setNumThornLevels(int numThornLevels) 
00197 {
00198   d_numThornLevels = numThornLevels;
00199 }
00200 
00201 int QwtSimpleCompassRose::numThornLevels() const
00202 {
00203   return d_numThornLevels;
00204 }

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