source: ntrip/trunk/BNC/qwt/qwt_compass_rose.cpp@ 8941

Last change on this file since 8941 was 8127, checked in by stoecker, 8 years ago

update qwt and qwtpolar, many QT5 fixes (unfinished)

File size: 6.5 KB
Line 
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
15static 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
28class QwtSimpleCompassRose::PrivateData
29{
30public:
31 PrivateData():
32 width( 0.2 ),
33 numThorns( 8 ),
34 numThornLevels( -1 ),
35 shrinkFactor( 0.9 )
36 {
37 }
38
39 double width;
40 int numThorns;
41 int numThornLevels;
42 double shrinkFactor;
43};
44
45/*!
46 Constructor
47
48 \param numThorns Number of thorns
49 \param numThornLevels Number of thorn levels
50*/
51QwtSimpleCompassRose::QwtSimpleCompassRose(
52 int numThorns, int numThornLevels )
53{
54 d_data = new PrivateData();
55 d_data->numThorns = numThorns;
56 d_data->numThornLevels = numThornLevels;
57
58 const QColor dark( 128, 128, 255 );
59 const QColor light( 192, 255, 255 );
60
61 QPalette palette;
62 palette.setColor( QPalette::Dark, dark );
63 palette.setColor( QPalette::Light, light );
64
65 setPalette( palette );
66}
67
68//! Destructor
69QwtSimpleCompassRose::~QwtSimpleCompassRose()
70{
71 delete d_data;
72}
73
74/*!
75 Set the Factor how to shrink the thorns with each level
76 The default value is 0.9.
77
78 \param factor Shrink factor
79 \sa shrinkFactor()
80*/
81void QwtSimpleCompassRose::setShrinkFactor( double factor )
82{
83 d_data->shrinkFactor = factor;
84}
85
86/*!
87 \return Factor how to shrink the thorns with each level
88 \sa setShrinkFactor()
89*/
90double QwtSimpleCompassRose::shrinkFactor() const
91{
92 return d_data->shrinkFactor;
93}
94
95/*!
96 Draw the rose
97
98 \param painter Painter
99 \param center Center point
100 \param radius Radius of the rose
101 \param north Position
102 \param cg Color group
103*/
104void QwtSimpleCompassRose::draw( QPainter *painter, const QPointF &center,
105 double radius, double north, QPalette::ColorGroup cg ) const
106{
107 QPalette pal = palette();
108 pal.setCurrentColorGroup( cg );
109
110 drawRose( painter, pal, center, radius, north, d_data->width,
111 d_data->numThorns, d_data->numThornLevels, d_data->shrinkFactor );
112}
113
114/*!
115 Draw the rose
116
117 \param painter Painter
118 \param palette Palette
119 \param center Center of the rose
120 \param radius Radius of the rose
121 \param north Position pointing to north
122 \param width Width of the rose
123 \param numThorns Number of thorns
124 \param numThornLevels Number of thorn levels
125 \param shrinkFactor Factor to shrink the thorns with each level
126*/
127void QwtSimpleCompassRose::drawRose(
128 QPainter *painter,
129 const QPalette &palette,
130 const QPointF &center, double radius, double north, double width,
131 int numThorns, int numThornLevels, double shrinkFactor )
132{
133 if ( numThorns < 4 )
134 numThorns = 4;
135
136 if ( numThorns % 4 )
137 numThorns += 4 - numThorns % 4;
138
139 if ( numThornLevels <= 0 )
140 numThornLevels = numThorns / 4;
141
142 if ( shrinkFactor >= 1.0 )
143 shrinkFactor = 1.0;
144
145 if ( shrinkFactor <= 0.5 )
146 shrinkFactor = 0.5;
147
148 painter->save();
149
150 painter->setPen( Qt::NoPen );
151
152 for ( int j = 1; j <= numThornLevels; j++ )
153 {
154 double step = qPow( 2.0, j ) * M_PI / numThorns;
155 if ( step > M_PI_2 )
156 break;
157
158 double r = radius;
159 for ( int k = 0; k < 3; k++ )
160 {
161 if ( j + k < numThornLevels )
162 r *= shrinkFactor;
163 }
164
165 double leafWidth = r * width;
166 if ( 2.0 * M_PI / step > 32 )
167 leafWidth = 16;
168
169 const double origin = qwtRadians( north );
170 for ( double angle = origin;
171 angle < 2.0 * M_PI + origin; angle += step )
172 {
173 const QPointF p = qwtPolar2Pos( center, r, angle );
174 const QPointF p1 = qwtPolar2Pos( center, leafWidth, angle + M_PI_2 );
175 const QPointF p2 = qwtPolar2Pos( center, leafWidth, angle - M_PI_2 );
176 const QPointF p3 = qwtPolar2Pos( center, r, angle + step / 2.0 );
177 const QPointF p4 = qwtPolar2Pos( center, r, angle - step / 2.0 );
178
179 QPainterPath darkPath;
180 darkPath.moveTo( center );
181 darkPath.lineTo( p );
182 darkPath.lineTo( qwtIntersection( center, p3, p1, p ) );
183
184 painter->setBrush( palette.brush( QPalette::Dark ) );
185 painter->drawPath( darkPath );
186
187 QPainterPath lightPath;
188 lightPath.moveTo( center );
189 lightPath.lineTo( p );
190 lightPath.lineTo( qwtIntersection( center, p4, p2, p ) );
191
192 painter->setBrush( palette.brush( QPalette::Light ) );
193 painter->drawPath( lightPath );
194 }
195 }
196 painter->restore();
197}
198
199/*!
200 Set the width of the rose heads. Lower value make thinner heads.
201 The range is limited from 0.03 to 0.4.
202
203 \param width Width
204*/
205void QwtSimpleCompassRose::setWidth( double width )
206{
207 d_data->width = width;
208 if ( d_data->width < 0.03 )
209 d_data->width = 0.03;
210
211 if ( d_data->width > 0.4 )
212 d_data->width = 0.4;
213}
214
215/*!
216 \return Width of the rose
217 \sa setWidth()
218 */
219double QwtSimpleCompassRose::width() const
220{
221 return d_data->width;
222}
223
224/*!
225 Set the number of thorns on one level
226 The number is aligned to a multiple of 4, with a minimum of 4
227
228 \param numThorns Number of thorns
229 \sa numThorns(), setNumThornLevels()
230*/
231void QwtSimpleCompassRose::setNumThorns( int numThorns )
232{
233 if ( numThorns < 4 )
234 numThorns = 4;
235
236 if ( numThorns % 4 )
237 numThorns += 4 - numThorns % 4;
238
239 d_data->numThorns = numThorns;
240}
241
242/*!
243 \return Number of thorns
244 \sa setNumThorns(), setNumThornLevels()
245*/
246int QwtSimpleCompassRose::numThorns() const
247{
248 return d_data->numThorns;
249}
250
251/*!
252 Set the of thorns levels
253
254 \param numThornLevels Number of thorns levels
255 \sa setNumThorns(), numThornLevels()
256*/
257void QwtSimpleCompassRose::setNumThornLevels( int numThornLevels )
258{
259 d_data->numThornLevels = numThornLevels;
260}
261
262/*!
263 \return Number of thorn levels
264 \sa setNumThorns(), setNumThornLevels()
265*/
266int QwtSimpleCompassRose::numThornLevels() const
267{
268 return d_data->numThornLevels;
269}
Note: See TracBrowser for help on using the repository browser.