1
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
3
* Copyright (C) 1997 Josef Wilgen
4
* Copyright (C) 2002 Uwe Rathmann
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
*****************************************************************************/
13
#include "qwt_painter.h"
14
#include "qwt_compass_rose.h"
16
static QPoint cutPoint(QPoint p11, QPoint p12, QPoint p21, QPoint 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));
58
\param numThorns Number of thorns
59
\param numThornLevels Number of thorn levels
61
QwtSimpleCompassRose::QwtSimpleCompassRose(int numThorns, int numThornLevels):
63
d_numThorns(numThorns),
64
d_numThornLevels(numThornLevels),
67
const QColor dark(128,128,255);
68
const QColor light(192,255,255);
71
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);
92
\param painter Painter
93
\param center Center point
94
\param radius Radius of the rose
98
void QwtSimpleCompassRose::draw(QPainter *painter, const QPoint ¢er,
99
int 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
QPalette pal = palette();
117
pal.setCurrentColorGroup(cg);
118
drawRose(painter, pal, center, radius, north, d_width,
119
d_numThorns, d_numThornLevels, d_shrinkFactor);
126
\param painter Painter
127
\param palette Palette
128
\param center Center of the rose
129
\param radius Radius of the rose
130
\param north Position pointing to north
131
\param width Width of the rose
132
\param numThorns Number of thorns
133
\param numThornLevels Number of thorn levels
134
\param shrinkFactor Factor to shrink the thorns with each level
136
void QwtSimpleCompassRose::drawRose(
138
#if QT_VERSION < 0x040000
139
const QColorGroup &cg,
141
const QPalette &palette,
143
const QPoint ¢er, int radius, double north, double width,
144
int numThorns, int numThornLevels, double shrinkFactor)
150
numThorns += 4 - numThorns % 4;
152
if ( numThornLevels <= 0 )
153
numThornLevels = numThorns / 4;
155
if ( shrinkFactor >= 1.0 )
158
if ( shrinkFactor <= 0.5 )
163
painter->setPen(Qt::NoPen);
165
for ( int j = 1; j <= numThornLevels; j++ )
167
double step = pow(2.0, j) * M_PI / (double)numThorns;
172
for ( int k = 0; k < 3; k++ )
174
if ( j + k < numThornLevels )
178
double leafWidth = r * width;
179
if ( 2.0 * M_PI / step > 32 )
182
const double origin = north / 180.0 * M_PI;
183
for ( double angle = origin;
184
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);
220
Set the width of the rose heads. Lower value make thinner heads.
221
The range is limited from 0.03 to 0.4.
226
void QwtSimpleCompassRose::setWidth(double width)
237
Set the number of thorns on one level
238
The number is aligned to a multiple of 4, with a minimum of 4
240
\param numThorns Number of thorns
241
\sa numThorns(), setNumThornLevels()
243
void QwtSimpleCompassRose::setNumThorns(int numThorns)
249
numThorns += 4 - numThorns % 4;
251
d_numThorns = numThorns;
255
\return Number of thorns
256
\sa setNumThorns(), setNumThornLevels()
258
int QwtSimpleCompassRose::numThorns() const
264
Set the of thorns levels
266
\param numThornLevels Number of thorns levels
267
\sa setNumThorns(), numThornLevels()
269
void QwtSimpleCompassRose::setNumThornLevels(int numThornLevels)
271
d_numThornLevels = numThornLevels;
275
\return Number of thorn levels
276
\sa setNumThorns(), setNumThornLevels()
278
int QwtSimpleCompassRose::numThornLevels() const
280
return d_numThornLevels;