qwt_compass_rose.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Qwt Widget Library
3  * Copyright (C) 1997 Josef Wilgen
4  * Copyright (C) 2002 Uwe Rathmann
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the Qwt License, Version 1.0
8  *****************************************************************************/
9 
10 #include "qwt_compass_rose.h"
11 #include "qwt_point_polar.h"
12 
13 #include <qpainter.h>
14 #include <qpainterpath.h>
15 
16 static QPointF qwtIntersection(
17  QPointF p11, QPointF p12, QPointF p21, QPointF p22 )
18 {
19  const QLineF line1( p11, p12 );
20  const QLineF line2( p21, p22 );
21 
22  QPointF pos;
23 #if QT_VERSION >= 0x050e00
24  if ( line1.intersects( line2, &pos ) == QLineF::NoIntersection )
25 #else
26  if ( line1.intersect( line2, &pos ) == QLineF::NoIntersection )
27 #endif
28  return QPointF();
29 
30  return pos;
31 }
32 
35 {
36 }
37 
40 {
41 }
42 
44 void QwtCompassRose::setPalette( const QPalette& p )
45 {
46  m_palette = p;
47 }
48 
50 const QPalette& QwtCompassRose::palette() const
51 {
52  return m_palette;
53 }
54 
56 {
57  public:
59  : width( 0.2 )
60  , numThorns( 8 )
61  , numThornLevels( -1 )
62  , shrinkFactor( 0.9 )
63  {
64  }
65 
66  double width;
67  int numThorns;
69  double shrinkFactor;
70 };
71 
79  int numThorns, int numThornLevels )
80 {
81  m_data = new PrivateData();
84 
85  const QColor dark( 128, 128, 255 );
86  const QColor light( 192, 255, 255 );
87 
88  QPalette palette;
89  palette.setColor( QPalette::Dark, dark );
90  palette.setColor( QPalette::Light, light );
91 
93 }
94 
97 {
98  delete m_data;
99 }
100 
109 {
110  m_data->shrinkFactor = factor;
111 }
112 
118 {
119  return m_data->shrinkFactor;
120 }
121 
131 void QwtSimpleCompassRose::draw( QPainter* painter, const QPointF& center,
132  double radius, double north, QPalette::ColorGroup cg ) const
133 {
134  QPalette pal = palette();
135  pal.setCurrentColorGroup( cg );
136 
137  drawRose( painter, pal, center, radius, north, m_data->width,
139 }
140 
155  QPainter* painter,
156  const QPalette& palette,
157  const QPointF& center, double radius, double north, double width,
158  int numThorns, int numThornLevels, double shrinkFactor )
159 {
160  if ( numThorns < 4 )
161  numThorns = 4;
162 
163  if ( numThorns % 4 )
164  numThorns += 4 - numThorns % 4;
165 
166  if ( numThornLevels <= 0 )
168 
169  if ( shrinkFactor >= 1.0 )
170  shrinkFactor = 1.0;
171 
172  if ( shrinkFactor <= 0.5 )
173  shrinkFactor = 0.5;
174 
175  painter->save();
176 
177  painter->setPen( Qt::NoPen );
178 
179  for ( int j = 1; j <= numThornLevels; j++ )
180  {
181  double step = std::pow( 2.0, j ) * M_PI / numThorns;
182  if ( step > M_PI_2 )
183  break;
184 
185  double r = radius;
186  for ( int k = 0; k < 3; k++ )
187  {
188  if ( j + k < numThornLevels )
189  r *= shrinkFactor;
190  }
191 
192  double leafWidth = r * width;
193  if ( 2.0 * M_PI / step > 32 )
194  leafWidth = 16;
195 
196  const double origin = qwtRadians( north );
197  for ( double angle = origin;
198  angle < 2.0 * M_PI + origin; angle += step )
199  {
200  const QPointF p = qwtPolar2Pos( center, r, angle );
201  const QPointF p1 = qwtPolar2Pos( center, leafWidth, angle + M_PI_2 );
202  const QPointF p2 = qwtPolar2Pos( center, leafWidth, angle - M_PI_2 );
203  const QPointF p3 = qwtPolar2Pos( center, r, angle + step / 2.0 );
204  const QPointF p4 = qwtPolar2Pos( center, r, angle - step / 2.0 );
205 
206  QPainterPath darkPath;
207  darkPath.moveTo( center );
208  darkPath.lineTo( p );
209  darkPath.lineTo( qwtIntersection( center, p3, p1, p ) );
210 
211  painter->setBrush( palette.brush( QPalette::Dark ) );
212  painter->drawPath( darkPath );
213 
214  QPainterPath lightPath;
215  lightPath.moveTo( center );
216  lightPath.lineTo( p );
217  lightPath.lineTo( qwtIntersection( center, p4, p2, p ) );
218 
219  painter->setBrush( palette.brush( QPalette::Light ) );
220  painter->drawPath( lightPath );
221  }
222  }
223  painter->restore();
224 }
225 
232 void QwtSimpleCompassRose::setWidth( double width )
233 {
234  m_data->width = width;
235  if ( m_data->width < 0.03 )
236  m_data->width = 0.03;
237 
238  if ( m_data->width > 0.4 )
239  m_data->width = 0.4;
240 }
241 
247 {
248  return m_data->width;
249 }
250 
259 {
260  if ( numThorns < 4 )
261  numThorns = 4;
262 
263  if ( numThorns % 4 )
264  numThorns += 4 - numThorns % 4;
265 
267 }
268 
274 {
275  return m_data->numThorns;
276 }
277 
284 void QwtSimpleCompassRose::setNumThornLevels( int numThornLevels )
285 {
287 }
288 
294 {
295  return m_data->numThornLevels;
296 }
QwtSimpleCompassRose::m_data
PrivateData * m_data
Definition: qwt_compass_rose.h:79
QwtSimpleCompassRose::numThornLevels
int numThornLevels() const
Definition: qwt_compass_rose.cpp:293
QwtSimpleCompassRose::PrivateData::shrinkFactor
double shrinkFactor
Definition: qwt_compass_rose.cpp:69
QwtCompassRose::~QwtCompassRose
virtual ~QwtCompassRose()
Destructor.
Definition: qwt_compass_rose.cpp:39
qwt_compass_rose.h
QwtSimpleCompassRose::QwtSimpleCompassRose
QwtSimpleCompassRose(int numThorns=8, int numThornLevels=-1)
Definition: qwt_compass_rose.cpp:78
QwtSimpleCompassRose::numThorns
int numThorns() const
Definition: qwt_compass_rose.cpp:273
QwtSimpleCompassRose::width
double width() const
Definition: qwt_compass_rose.cpp:246
qwtPolar2Pos
QPoint qwtPolar2Pos(const QPoint &pole, double radius, double angle)
Definition: qwt_point_polar.h:137
QwtSimpleCompassRose::shrinkFactor
double shrinkFactor() const
Definition: qwt_compass_rose.cpp:117
QwtSimpleCompassRose::~QwtSimpleCompassRose
virtual ~QwtSimpleCompassRose()
Destructor.
Definition: qwt_compass_rose.cpp:96
qwtIntersection
static QPointF qwtIntersection(QPointF p11, QPointF p12, QPointF p21, QPointF p22)
Definition: qwt_compass_rose.cpp:16
QwtSimpleCompassRose::setShrinkFactor
void setShrinkFactor(double factor)
Definition: qwt_compass_rose.cpp:108
QwtSimpleCompassRose::setNumThorns
void setNumThorns(int)
Definition: qwt_compass_rose.cpp:258
M_PI
#define M_PI
Definition: qwt_math.h:56
QwtCompassRose::setPalette
virtual void setPalette(const QPalette &)
Assign a palette.
Definition: qwt_compass_rose.cpp:44
QwtCompassRose::QwtCompassRose
QwtCompassRose()
Constructor.
Definition: qwt_compass_rose.cpp:34
QwtSimpleCompassRose::PrivateData::numThorns
int numThorns
Definition: qwt_compass_rose.cpp:67
M_PI_2
#define M_PI_2
Definition: qwt_math.h:60
QwtSimpleCompassRose::PrivateData::numThornLevels
int numThornLevels
Definition: qwt_compass_rose.cpp:68
qwt_point_polar.h
QwtCompassRose::m_palette
QPalette m_palette
Definition: qwt_compass_rose.h:46
QwtCompassRose::palette
const QPalette & palette() const
Definition: qwt_compass_rose.cpp:50
QwtSimpleCompassRose::PrivateData::width
double width
Definition: qwt_compass_rose.cpp:66
qwtRadians
double qwtRadians(double degrees)
Translate degrees into radians.
Definition: qwt_math.h:251
QwtSimpleCompassRose::PrivateData::PrivateData
PrivateData()
Definition: qwt_compass_rose.cpp:58
QwtSimpleCompassRose::draw
virtual void draw(QPainter *, const QPointF &center, double radius, double north, QPalette::ColorGroup=QPalette::Active) const QWT_OVERRIDE
Definition: qwt_compass_rose.cpp:131
QwtSimpleCompassRose::PrivateData
Definition: qwt_compass_rose.cpp:55
QwtSimpleCompassRose::setWidth
void setWidth(double)
Definition: qwt_compass_rose.cpp:232
QwtSimpleCompassRose::drawRose
static void drawRose(QPainter *, const QPalette &, const QPointF &center, double radius, double north, double width, int numThorns, int numThornLevels, double shrinkFactor)
Definition: qwt_compass_rose.cpp:154
QwtSimpleCompassRose::setNumThornLevels
void setNumThornLevels(int)
Definition: qwt_compass_rose.cpp:284


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:45