qwt_compass_rose.cpp
Go to the documentation of this file.
1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
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  d_palette = p;
47 }
48 
50 const QPalette &QwtCompassRose::palette() const
51 {
52  return d_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  d_data = new PrivateData();
82  d_data->numThorns = numThorns;
83  d_data->numThornLevels = numThornLevels;
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 
92  setPalette( palette );
93 }
94 
97 {
98  delete d_data;
99 }
100 
109 {
110  d_data->shrinkFactor = factor;
111 }
112 
118 {
119  return d_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, d_data->width,
138  d_data->numThorns, d_data->numThornLevels, d_data->shrinkFactor );
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 )
167  numThornLevels = numThorns / 4;
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  d_data->width = width;
235  if ( d_data->width < 0.03 )
236  d_data->width = 0.03;
237 
238  if ( d_data->width > 0.4 )
239  d_data->width = 0.4;
240 }
241 
247 {
248  return d_data->width;
249 }
250 
259 {
260  if ( numThorns < 4 )
261  numThorns = 4;
262 
263  if ( numThorns % 4 )
264  numThorns += 4 - numThorns % 4;
265 
266  d_data->numThorns = numThorns;
267 }
268 
274 {
275  return d_data->numThorns;
276 }
277 
284 void QwtSimpleCompassRose::setNumThornLevels( int numThornLevels )
285 {
286  d_data->numThornLevels = numThornLevels;
287 }
288 
294 {
295  return d_data->numThornLevels;
296 }
virtual ~QwtCompassRose()
Destructor.
const QPalette & palette() const
static void drawRose(QPainter *, const QPalette &, const QPointF &center, double radius, double north, double width, int numThorns, int numThornLevels, double shrinkFactor)
static QPointF qwtIntersection(QPointF p11, QPointF p12, QPointF p21, QPointF p22)
virtual void setPalette(const QPalette &)
Assign a palette.
QPoint qwtPolar2Pos(const QPoint &pole, double radius, double angle)
QwtCompassRose()
Constructor.
#define M_PI_2
Definition: qwt_math.h:60
double qwtRadians(double degrees)
Translate degrees into radians.
Definition: qwt_math.h:247
QwtSimpleCompassRose(int numThorns=8, int numThornLevels=-1)
double shrinkFactor() const
#define M_PI
Definition: qwt_math.h:56
virtual ~QwtSimpleCompassRose()
Destructor.
void setShrinkFactor(double factor)
virtual void draw(QPainter *, const QPointF &center, double radius, double north, QPalette::ColorGroup=QPalette::Active) const QWT_OVERRIDE


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:10