~ubuntu-branches/ubuntu/oneiric/qwt/oneiric-proposed

« back to all changes in this revision

Viewing changes to qwt-5.1.2/src/qwt_compass_rose.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2009-04-12 23:25:58 UTC
  • mfrom: (1.1.4 upstream) (2.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090412232558-3bl06x785yr8xm8u
Tags: 5.1.2-1
* New upstream release.
* Bump compat/debhelper to 7.
* Bump Standards-Version to 3.8.1. No changes needed.
* Invert Maintainers and Uploaders field.
* Fix lintian warnings:
  - dh_clean _k deprecated.
  - missing dependency on libc.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <math.h>
 
11
#include <qpainter.h>
 
12
#include "qwt_math.h"
 
13
#include "qwt_painter.h"
 
14
#include "qwt_compass_rose.h"
 
15
 
 
16
static QPoint cutPoint(QPoint p11, QPoint p12, QPoint p21, QPoint p22)
 
17
{
 
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();
 
22
 
 
23
    if ( dx1 == 0.0 && dx2 == 0.0 )
 
24
        return QPoint();
 
25
 
 
26
    if ( dx1 == 0.0 )
 
27
    {
 
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));
 
31
    }
 
32
 
 
33
    if ( dx2 == 0 )
 
34
    {
 
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));
 
38
    }
 
39
 
 
40
    const double m1 = dy1 / dx1;
 
41
    const double t1 = p11.y() - m1 * p11.x();
 
42
 
 
43
    const double m2 = dy2 / dx2;
 
44
    const double t2 = p21.y() - m2 * p21.x();
 
45
 
 
46
    if ( m1 == m2 )
 
47
        return QPoint();
 
48
 
 
49
    const double x = ( t2 - t1 ) / ( m1 - m2 );
 
50
    const double y = t1 + m1 * x;
 
51
 
 
52
    return QPoint(qRound(x), qRound(y));
 
53
}
 
54
 
 
55
/*!
 
56
   Constructor
 
57
 
 
58
   \param numThorns Number of thorns
 
59
   \param numThornLevels Number of thorn levels
 
60
*/
 
61
QwtSimpleCompassRose::QwtSimpleCompassRose(int numThorns, int numThornLevels):
 
62
    d_width(0.2),
 
63
    d_numThorns(numThorns),
 
64
    d_numThornLevels(numThornLevels),
 
65
    d_shrinkFactor(0.9)
 
66
{
 
67
    const QColor dark(128,128,255);
 
68
    const QColor light(192,255,255);
 
69
    
 
70
    QPalette palette;
 
71
    for ( int i = 0; i < QPalette::NColorGroups; i++ )
 
72
    {
 
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);
 
78
#else
 
79
        palette.setColor((QPalette::ColorGroup)i,
 
80
            QPalette::Dark, dark);
 
81
        palette.setColor((QPalette::ColorGroup)i,
 
82
            QPalette::Light, light);
 
83
#endif
 
84
    }
 
85
 
 
86
    setPalette(palette);
 
87
}
 
88
 
 
89
/*!
 
90
   Draw the rose
 
91
 
 
92
   \param painter Painter
 
93
   \param center Center point
 
94
   \param radius Radius of the rose
 
95
   \param north Position
 
96
   \param cg Color group
 
97
*/
 
98
void QwtSimpleCompassRose::draw(QPainter *painter, const QPoint &center, 
 
99
    int radius, double north, QPalette::ColorGroup cg) const
 
100
{
 
101
#if QT_VERSION < 0x040000
 
102
    QColorGroup colorGroup;
 
103
    switch(cg)
 
104
    {
 
105
        case QPalette::Disabled:
 
106
            colorGroup = palette().disabled();
 
107
        case QPalette::Inactive:
 
108
            colorGroup = palette().inactive();
 
109
        default:
 
110
            colorGroup = palette().active();
 
111
    }
 
112
 
 
113
    drawRose(painter, colorGroup, center, radius, north, d_width, 
 
114
        d_numThorns, d_numThornLevels, d_shrinkFactor);
 
115
#else
 
116
    QPalette pal = palette();
 
117
    pal.setCurrentColorGroup(cg);
 
118
    drawRose(painter, pal, center, radius, north, d_width, 
 
119
        d_numThorns, d_numThornLevels, d_shrinkFactor);
 
120
#endif
 
121
}
 
122
 
 
123
/*!
 
124
   Draw the rose
 
125
 
 
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
 
135
*/
 
136
void QwtSimpleCompassRose::drawRose(
 
137
    QPainter *painter, 
 
138
#if QT_VERSION < 0x040000
 
139
    const QColorGroup &cg,
 
140
#else
 
141
    const QPalette &palette,
 
142
#endif
 
143
    const QPoint &center, int radius, double north, double width,
 
144
    int numThorns, int numThornLevels, double shrinkFactor)
 
145
{
 
146
    if ( numThorns < 4 )
 
147
        numThorns = 4;
 
148
 
 
149
    if ( numThorns % 4 )
 
150
        numThorns += 4 - numThorns % 4;
 
151
 
 
152
    if ( numThornLevels <= 0 )
 
153
        numThornLevels = numThorns / 4;
 
154
 
 
155
    if ( shrinkFactor >= 1.0 )
 
156
        shrinkFactor = 1.0;
 
157
 
 
158
    if ( shrinkFactor <= 0.5 )
 
159
        shrinkFactor = 0.5;
 
160
 
 
161
    painter->save();
 
162
 
 
163
    painter->setPen(Qt::NoPen);
 
164
 
 
165
    for ( int j = 1; j <= numThornLevels; j++ )
 
166
    {
 
167
        double step =  pow(2.0, j) * M_PI / (double)numThorns;
 
168
        if ( step > M_PI_2 )
 
169
            break;
 
170
 
 
171
        double r = radius;
 
172
        for ( int k = 0; k < 3; k++ )
 
173
        {
 
174
            if ( j + k < numThornLevels )
 
175
                r *= shrinkFactor;
 
176
        }
 
177
 
 
178
        double leafWidth = r * width;
 
179
        if ( 2.0 * M_PI / step > 32 )
 
180
            leafWidth = 16;
 
181
 
 
182
        const double origin = north / 180.0 * M_PI;
 
183
        for ( double angle = origin; 
 
184
            angle < 2.0 * M_PI + origin; angle += step)
 
185
        {
 
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);
 
189
 
 
190
            QwtPolygon pa(3);
 
191
            pa.setPoint(0, center);
 
192
            pa.setPoint(1, p);
 
193
 
 
194
            QPoint p3 = qwtPolar2Pos(center, r, angle + step / 2.0);
 
195
            p1 = cutPoint(center, p3, p1, p);
 
196
            pa.setPoint(2, p1);
 
197
#if QT_VERSION < 0x040000
 
198
            painter->setBrush(cg.brush(QColorGroup::Dark));
 
199
#else
 
200
            painter->setBrush(palette.brush(QPalette::Dark));
 
201
#endif
 
202
            painter->drawPolygon(pa);
 
203
 
 
204
            QPoint p4 = qwtPolar2Pos(center, r, angle - step / 2.0);
 
205
            p2 = cutPoint(center, p4, p2, p);
 
206
 
 
207
            pa.setPoint(2, p2);
 
208
#if QT_VERSION < 0x040000
 
209
            painter->setBrush(cg.brush(QColorGroup::Light));
 
210
#else
 
211
            painter->setBrush(palette.brush(QPalette::Light));
 
212
#endif
 
213
            painter->drawPolygon(pa);
 
214
        }
 
215
    }
 
216
    painter->restore();
 
217
}
 
218
 
 
219
/*!
 
220
   Set the width of the rose heads. Lower value make thinner heads.
 
221
   The range is limited from 0.03 to 0.4.
 
222
 
 
223
   \param width Width
 
224
*/
 
225
 
 
226
void QwtSimpleCompassRose::setWidth(double width) 
 
227
{
 
228
   d_width = width;
 
229
   if (d_width < 0.03) 
 
230
        d_width = 0.03;
 
231
 
 
232
   if (d_width > 0.4) 
 
233
        d_width = 0.4;
 
234
}
 
235
 
 
236
/*!
 
237
  Set the number of thorns on one level
 
238
  The number is aligned to a multiple of 4, with a minimum of 4 
 
239
 
 
240
  \param numThorns Number of thorns
 
241
  \sa numThorns(), setNumThornLevels()
 
242
*/
 
243
void QwtSimpleCompassRose::setNumThorns(int numThorns) 
 
244
{
 
245
    if ( numThorns < 4 )
 
246
        numThorns = 4;
 
247
 
 
248
    if ( numThorns % 4 )
 
249
        numThorns += 4 - numThorns % 4;
 
250
 
 
251
    d_numThorns = numThorns;
 
252
}
 
253
 
 
254
/*!
 
255
   \return Number of thorns
 
256
   \sa setNumThorns(), setNumThornLevels()
 
257
*/
 
258
int QwtSimpleCompassRose::numThorns() const
 
259
{
 
260
   return d_numThorns;
 
261
}
 
262
 
 
263
/*!
 
264
  Set the of thorns levels
 
265
 
 
266
  \param numThornLevels Number of thorns levels
 
267
  \sa setNumThorns(), numThornLevels()
 
268
*/
 
269
void QwtSimpleCompassRose::setNumThornLevels(int numThornLevels) 
 
270
{
 
271
    d_numThornLevels = numThornLevels;
 
272
}
 
273
 
 
274
/*!
 
275
   \return Number of thorn levels
 
276
   \sa setNumThorns(), setNumThornLevels()
 
277
*/
 
278
int QwtSimpleCompassRose::numThornLevels() const
 
279
{
 
280
    return d_numThornLevels;
 
281
}