7
7
* modify it under the terms of the Qwt License, Version 1.0
8
8
*****************************************************************************/
10
#include "qwt_compass_rose.h"
11
#include "qwt_point_polar.h"
12
#include "qwt_painter.h"
11
13
#include <qpainter.h>
13
#include "qwt_painter.h"
14
#include "qwt_compass_rose.h"
16
static QPoint cutPoint(QPoint p11, QPoint p12, QPoint p21, QPoint p22)
15
static QPointF qwtIntersection(
16
QPointF p11, QPointF p12, QPointF p21, QPointF p22 )
18
double dx1 = p12.x() - p11.x();
19
double dy1 = p12.y() - p11.y();
20
double dx2 = p22.x() - p21.x();
21
double dy2 = p22.y() - p21.y();
23
if ( dx1 == 0.0 && dx2 == 0.0 )
28
const double m = dy2 / dx2;
29
const double t = p21.y() - m * p21.x();
30
return QPoint(p11.x(), qRound(m * p11.x() + t));
35
const double m = dy1 / dx1;
36
const double t = p11.y() - m * p11.x();
37
return QPoint(p21.x(), qRound(m * p21.x() + t));
40
const double m1 = dy1 / dx1;
41
const double t1 = p11.y() - m1 * p11.x();
43
const double m2 = dy2 / dx2;
44
const double t2 = p21.y() - m2 * p21.x();
49
const double x = ( t2 - t1 ) / ( m1 - m2 );
50
const double y = t1 + m1 * x;
52
return QPoint(qRound(x), qRound(y));
18
const QLineF line1( p11, p12 );
19
const QLineF line2( p21, p22 );
22
if ( line1.intersect( line2, &pos ) == QLineF::NoIntersection )
28
class QwtSimpleCompassRose::PrivateData
58
48
\param numThorns Number of thorns
59
49
\param numThornLevels Number of thorn levels
61
QwtSimpleCompassRose::QwtSimpleCompassRose(int numThorns, int numThornLevels):
63
d_numThorns(numThorns),
64
d_numThornLevels(numThornLevels),
51
QwtSimpleCompassRose::QwtSimpleCompassRose(
52
int numThorns, int numThornLevels )
67
const QColor dark(128,128,255);
68
const QColor light(192,255,255);
54
d_data = new PrivateData();
55
d_data->numThorns = numThorns;
56
d_data->numThornLevels = numThornLevels;
58
const QColor dark( 128, 128, 255 );
59
const QColor light( 192, 255, 255 );
71
62
for ( int i = 0; i < QPalette::NColorGroups; i++ )
73
#if QT_VERSION < 0x040000
74
palette.setColor((QPalette::ColorGroup)i,
75
QColorGroup::Dark, dark);
76
palette.setColor((QPalette::ColorGroup)i,
77
QColorGroup::Light, light);
79
palette.setColor((QPalette::ColorGroup)i,
80
QPalette::Dark, dark);
81
palette.setColor((QPalette::ColorGroup)i,
82
QPalette::Light, light);
64
palette.setColor( ( QPalette::ColorGroup )i,
65
QPalette::Dark, dark );
66
palette.setColor( ( QPalette::ColorGroup )i,
67
QPalette::Light, light );
70
setPalette( palette );
74
QwtSimpleCompassRose::~QwtSimpleCompassRose()
80
Set the Factor how to shrink the thorns with each level
81
The default value is 0.9.
85
void QwtSimpleCompassRose::setShrinkFactor( double factor )
87
d_data->shrinkFactor = factor;
91
\return Factor how to shrink the thorns with each level
94
double QwtSimpleCompassRose::shrinkFactor() const
96
return d_data->shrinkFactor;
95
105
\param north Position
96
106
\param cg Color group
98
void QwtSimpleCompassRose::draw(QPainter *painter, const QPoint ¢er,
99
int radius, double north, QPalette::ColorGroup cg) const
108
void QwtSimpleCompassRose::draw( QPainter *painter, const QPointF ¢er,
109
double radius, double north, QPalette::ColorGroup cg ) const
101
#if QT_VERSION < 0x040000
102
QColorGroup colorGroup;
105
case QPalette::Disabled:
106
colorGroup = palette().disabled();
107
case QPalette::Inactive:
108
colorGroup = palette().inactive();
110
colorGroup = palette().active();
113
drawRose(painter, colorGroup, center, radius, north, d_width,
114
d_numThorns, d_numThornLevels, d_shrinkFactor);
116
111
QPalette pal = palette();
117
pal.setCurrentColorGroup(cg);
118
drawRose(painter, pal, center, radius, north, d_width,
119
d_numThorns, d_numThornLevels, d_shrinkFactor);
112
pal.setCurrentColorGroup( cg );
114
drawRose( painter, pal, center, radius, north, d_data->width,
115
d_data->numThorns, d_data->numThornLevels, d_data->shrinkFactor );
134
129
\param shrinkFactor Factor to shrink the thorns with each level
136
131
void QwtSimpleCompassRose::drawRose(
138
#if QT_VERSION < 0x040000
139
const QColorGroup &cg,
141
133
const QPalette &palette,
143
const QPoint ¢er, int radius, double north, double width,
144
int numThorns, int numThornLevels, double shrinkFactor)
134
const QPointF ¢er, double radius, double north, double width,
135
int numThorns, int numThornLevels, double shrinkFactor )
146
137
if ( numThorns < 4 )
182
173
const double origin = north / 180.0 * M_PI;
183
for ( double angle = origin;
184
angle < 2.0 * M_PI + origin; angle += step)
174
for ( double angle = origin;
175
angle < 2.0 * M_PI + origin; angle += step )
186
const QPoint p = qwtPolar2Pos(center, r, angle);
187
QPoint p1 = qwtPolar2Pos(center, leafWidth, angle + M_PI_2);
188
QPoint p2 = qwtPolar2Pos(center, leafWidth, angle - M_PI_2);
191
pa.setPoint(0, center);
194
QPoint p3 = qwtPolar2Pos(center, r, angle + step / 2.0);
195
p1 = cutPoint(center, p3, p1, p);
197
#if QT_VERSION < 0x040000
198
painter->setBrush(cg.brush(QColorGroup::Dark));
200
painter->setBrush(palette.brush(QPalette::Dark));
202
painter->drawPolygon(pa);
204
QPoint p4 = qwtPolar2Pos(center, r, angle - step / 2.0);
205
p2 = cutPoint(center, p4, p2, p);
208
#if QT_VERSION < 0x040000
209
painter->setBrush(cg.brush(QColorGroup::Light));
211
painter->setBrush(palette.brush(QPalette::Light));
213
painter->drawPolygon(pa);
177
const QPointF p = qwtPolar2Pos( center, r, angle );
178
const QPointF p1 = qwtPolar2Pos( center, leafWidth, angle + M_PI_2 );
179
const QPointF p2 = qwtPolar2Pos( center, leafWidth, angle - M_PI_2 );
180
const QPointF p3 = qwtPolar2Pos( center, r, angle + step / 2.0 );
181
const QPointF p4 = qwtPolar2Pos( center, r, angle - step / 2.0 );
183
QPainterPath darkPath;
184
darkPath.moveTo( center );
185
darkPath.lineTo( p );
186
darkPath.lineTo( qwtIntersection( center, p3, p1, p ) );
188
painter->setBrush( palette.brush( QPalette::Dark ) );
189
painter->drawPath( darkPath );
191
QPainterPath lightPath;
192
lightPath.moveTo( center );
193
lightPath.lineTo( p );
194
lightPath.lineTo( qwtIntersection( center, p4, p2, p ) );
196
painter->setBrush( palette.brush( QPalette::Light ) );
197
painter->drawPath( lightPath );
216
200
painter->restore();
223
207
\param width Width
226
void QwtSimpleCompassRose::setWidth(double width)
209
void QwtSimpleCompassRose::setWidth( double width )
211
d_data->width = width;
212
if ( d_data->width < 0.03 )
213
d_data->width = 0.03;
215
if ( d_data->width > 0.4 )
220
double QwtSimpleCompassRose::width() const
222
return d_data->width;
237
226
Set the number of thorns on one level
238
The number is aligned to a multiple of 4, with a minimum of 4
227
The number is aligned to a multiple of 4, with a minimum of 4
240
229
\param numThorns Number of thorns
241
230
\sa numThorns(), setNumThornLevels()
243
void QwtSimpleCompassRose::setNumThorns(int numThorns)
232
void QwtSimpleCompassRose::setNumThorns( int numThorns )
245
234
if ( numThorns < 4 )