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