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 #include "qwt_painter.h"
13 #include <qpainter.h>
14 
15 static QPointF qwtIntersection(
16  QPointF p11, QPointF p12, QPointF p21, QPointF p22 )
17 {
18  const QLineF line1( p11, p12 );
19  const QLineF line2( p21, p22 );
20 
21  QPointF pos;
22  if ( line1.intersect( line2, &pos ) == QLineF::NoIntersection )
23  return QPointF();
24 
25  return pos;
26 }
27 
30 {
31 }
32 
35 {
36 }
37 
39 void QwtCompassRose::setPalette( const QPalette &p )
40 {
41  d_palette = p;
42 }
43 
45 const QPalette &QwtCompassRose::palette() const
46 {
47  return d_palette;
48 }
49 
51 {
52 public:
54  width( 0.2 ),
55  numThorns( 8 ),
56  numThornLevels( -1 ),
57  shrinkFactor( 0.9 )
58  {
59  }
60 
61  double width;
62  int numThorns;
64  double shrinkFactor;
65 };
66 
74  int numThorns, int numThornLevels )
75 {
76  d_data = new PrivateData();
77  d_data->numThorns = numThorns;
78  d_data->numThornLevels = numThornLevels;
79 
80  const QColor dark( 128, 128, 255 );
81  const QColor light( 192, 255, 255 );
82 
83  QPalette palette;
84  palette.setColor( QPalette::Dark, dark );
85  palette.setColor( QPalette::Light, light );
86 
87  setPalette( palette );
88 }
89 
92 {
93  delete d_data;
94 }
95 
104 {
105  d_data->shrinkFactor = factor;
106 }
107 
113 {
114  return d_data->shrinkFactor;
115 }
116 
126 void QwtSimpleCompassRose::draw( QPainter *painter, const QPointF &center,
127  double radius, double north, QPalette::ColorGroup cg ) const
128 {
129  QPalette pal = palette();
130  pal.setCurrentColorGroup( cg );
131 
132  drawRose( painter, pal, center, radius, north, d_data->width,
133  d_data->numThorns, d_data->numThornLevels, d_data->shrinkFactor );
134 }
135 
150  QPainter *painter,
151  const QPalette &palette,
152  const QPointF &center, double radius, double north, double width,
153  int numThorns, int numThornLevels, double shrinkFactor )
154 {
155  if ( numThorns < 4 )
156  numThorns = 4;
157 
158  if ( numThorns % 4 )
159  numThorns += 4 - numThorns % 4;
160 
161  if ( numThornLevels <= 0 )
162  numThornLevels = numThorns / 4;
163 
164  if ( shrinkFactor >= 1.0 )
165  shrinkFactor = 1.0;
166 
167  if ( shrinkFactor <= 0.5 )
168  shrinkFactor = 0.5;
169 
170  painter->save();
171 
172  painter->setPen( Qt::NoPen );
173 
174  for ( int j = 1; j <= numThornLevels; j++ )
175  {
176  double step = qPow( 2.0, j ) * M_PI / numThorns;
177  if ( step > M_PI_2 )
178  break;
179 
180  double r = radius;
181  for ( int k = 0; k < 3; k++ )
182  {
183  if ( j + k < numThornLevels )
184  r *= shrinkFactor;
185  }
186 
187  double leafWidth = r * width;
188  if ( 2.0 * M_PI / step > 32 )
189  leafWidth = 16;
190 
191  const double origin = qwtRadians( north );
192  for ( double angle = origin;
193  angle < 2.0 * M_PI + origin; angle += step )
194  {
195  const QPointF p = qwtPolar2Pos( center, r, angle );
196  const QPointF p1 = qwtPolar2Pos( center, leafWidth, angle + M_PI_2 );
197  const QPointF p2 = qwtPolar2Pos( center, leafWidth, angle - M_PI_2 );
198  const QPointF p3 = qwtPolar2Pos( center, r, angle + step / 2.0 );
199  const QPointF p4 = qwtPolar2Pos( center, r, angle - step / 2.0 );
200 
201  QPainterPath darkPath;
202  darkPath.moveTo( center );
203  darkPath.lineTo( p );
204  darkPath.lineTo( qwtIntersection( center, p3, p1, p ) );
205 
206  painter->setBrush( palette.brush( QPalette::Dark ) );
207  painter->drawPath( darkPath );
208 
209  QPainterPath lightPath;
210  lightPath.moveTo( center );
211  lightPath.lineTo( p );
212  lightPath.lineTo( qwtIntersection( center, p4, p2, p ) );
213 
214  painter->setBrush( palette.brush( QPalette::Light ) );
215  painter->drawPath( lightPath );
216  }
217  }
218  painter->restore();
219 }
220 
227 void QwtSimpleCompassRose::setWidth( double width )
228 {
229  d_data->width = width;
230  if ( d_data->width < 0.03 )
231  d_data->width = 0.03;
232 
233  if ( d_data->width > 0.4 )
234  d_data->width = 0.4;
235 }
236 
242 {
243  return d_data->width;
244 }
245 
254 {
255  if ( numThorns < 4 )
256  numThorns = 4;
257 
258  if ( numThorns % 4 )
259  numThorns += 4 - numThorns % 4;
260 
261  d_data->numThorns = numThorns;
262 }
263 
269 {
270  return d_data->numThorns;
271 }
272 
279 void QwtSimpleCompassRose::setNumThornLevels( int numThornLevels )
280 {
281  d_data->numThornLevels = numThornLevels;
282 }
283 
289 {
290  return d_data->numThornLevels;
291 }
virtual ~QwtCompassRose()
Destructor.
const QPalette & palette() const
static QPointF qwtIntersection(QPointF p11, QPointF p12, QPointF p21, QPointF p22)
virtual void setPalette(const QPalette &)
Assign a palette.
void setNumThorns(int count)
QPoint qwtPolar2Pos(const QPoint &pole, double radius, double angle)
QwtCompassRose()
Constructor.
#define M_PI_2
Definition: qwt_math.h:32
TFSIMD_FORCE_INLINE tfScalar angle(const Quaternion &q1, const Quaternion &q2)
double qwtRadians(double degrees)
Translate degrees into radians.
Definition: qwt_math.h:144
QwtSimpleCompassRose(int numThorns=8, int numThornLevels=-1)
double shrinkFactor() const
virtual void draw(QPainter *, const QPointF &center, double radius, double north, QPalette::ColorGroup=QPalette::Active) const
void setNumThornLevels(int count)
unsigned int step
virtual ~QwtSimpleCompassRose()
Destructor.
void setShrinkFactor(double factor)
static void drawRose(QPainter *, const QPalette &, const QPointF &center, double radius, double origin, double width, int numThorns, int numThornLevels, double shrinkFactor)


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:17