Qwt User's Guide: qwt_compass_rose.cpp Source File

Qwt User's Guide 6.3.0

Loading...
Searching...
No Matches
qwt_compass_rose.cpp
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
16static 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
34QwtCompassRose::QwtCompassRose()
35{
36}
37
39QwtCompassRose::~QwtCompassRose()
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
55class QwtSimpleCompassRose::PrivateData
56{
57 public:
58 PrivateData()
59 : width( 0.2 )
60 , numThorns( 8 )
61 , numThornLevels( -1 )
62 , shrinkFactor( 0.9 )
63 {
64 }
65
66 double width;
67 int numThorns;
68 int numThornLevels;
69 double shrinkFactor;
70};
71
78QwtSimpleCompassRose::QwtSimpleCompassRose(
79 int numThorns, int numThornLevels )
80{
81 m_data = new PrivateData();
82 m_data->numThorns = numThorns;
83 m_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
96QwtSimpleCompassRose::~QwtSimpleCompassRose()
97{
98 delete m_data;
99}
100
108 void QwtSimpleCompassRose::setShrinkFactor( double factor )
109{
110 m_data->shrinkFactor = factor;
111}
112
117 double QwtSimpleCompassRose::shrinkFactor() const
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,
138 m_data->numThorns, m_data->numThornLevels, m_data->shrinkFactor );
139}
140
154 void QwtSimpleCompassRose::drawRose(
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 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
246 double QwtSimpleCompassRose::width() const
247{
248 return m_data->width;
249}
250
258 void QwtSimpleCompassRose::setNumThorns( int numThorns )
259{
260 if ( numThorns < 4 )
261 numThorns = 4;
262
263 if ( numThorns % 4 )
264 numThorns += 4 - numThorns % 4;
265
266 m_data->numThorns = numThorns;
267}
268
273 int QwtSimpleCompassRose::numThorns() const
274{
275 return m_data->numThorns;
276}
277
284 void QwtSimpleCompassRose::setNumThornLevels( int numThornLevels )
285{
286 m_data->numThornLevels = numThornLevels;
287}
288
293 int QwtSimpleCompassRose::numThornLevels() const
294{
295 return m_data->numThornLevels;
296}
QwtCompassRose::~QwtCompassRose
virtual ~QwtCompassRose()
Destructor.
Definition qwt_compass_rose.cpp:39
QwtCompassRose::QwtCompassRose
QwtCompassRose()
Constructor.
Definition qwt_compass_rose.cpp:34
QwtCompassRose::setPalette
virtual void setPalette(const QPalette &)
Assign a palette.
Definition qwt_compass_rose.cpp:44
QwtCompassRose::palette
const QPalette & palette() const
Definition qwt_compass_rose.cpp:50
QwtSimpleCompassRose::setWidth
void setWidth(double)
Definition qwt_compass_rose.cpp:232
QwtSimpleCompassRose::~QwtSimpleCompassRose
virtual ~QwtSimpleCompassRose()
Destructor.
Definition qwt_compass_rose.cpp:96
QwtSimpleCompassRose::numThorns
int numThorns() const
Definition qwt_compass_rose.cpp:273
QwtSimpleCompassRose::width
double width() const
Definition qwt_compass_rose.cpp:246
QwtSimpleCompassRose::setNumThornLevels
void setNumThornLevels(int)
Definition qwt_compass_rose.cpp:284
QwtSimpleCompassRose::shrinkFactor
double shrinkFactor() const
Definition qwt_compass_rose.cpp:117
QwtSimpleCompassRose::QwtSimpleCompassRose
QwtSimpleCompassRose(int numThorns=8, int numThornLevels=-1)
Definition qwt_compass_rose.cpp:78
QwtSimpleCompassRose::numThornLevels
int numThornLevels() const
Definition qwt_compass_rose.cpp:293
QwtSimpleCompassRose::draw
virtual void draw(QPainter *, const QPointF &center, double radius, double north, QPalette::ColorGroup=QPalette::Active) const override
Definition qwt_compass_rose.cpp:131
QwtSimpleCompassRose::setNumThorns
void setNumThorns(int)
Definition qwt_compass_rose.cpp:258
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::setShrinkFactor
void setShrinkFactor(double factor)
Definition qwt_compass_rose.cpp:108