~ubuntu-branches/ubuntu/saucy/goldencheetah/saucy

« back to all changes in this revision

Viewing changes to qwt/src/qwt_compass_rose.cpp

  • Committer: Package Import Robot
  • Author(s): KURASHIKI Satoru
  • Date: 2013-08-18 07:02:45 UTC
  • mfrom: (4.1.8 sid)
  • Revision ID: package-import@ubuntu.com-20130818070245-zgdvb47e1k3mtgil
Tags: 3.0-3
debian/control: remove needless dependency. (Closes: #719571)

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
 * modify it under the terms of the Qwt License, Version 1.0
8
8
 *****************************************************************************/
9
9
 
10
 
#include <math.h>
 
10
#include "qwt_compass_rose.h"
 
11
#include "qwt_point_polar.h"
 
12
#include "qwt_painter.h"
11
13
#include <qpainter.h>
12
 
#include "qwt_math.h"
13
 
#include "qwt_painter.h"
14
 
#include "qwt_compass_rose.h"
15
14
 
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 )
17
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));
 
18
    const QLineF line1( p11, p12 );
 
19
    const QLineF line2( p21, p22 );
 
20
 
 
21
    QPointF pos;
 
22
    if ( line1.intersect( line2, &pos ) == QLineF::NoIntersection )
 
23
        return QPointF();
 
24
 
 
25
    return pos;
53
26
}
54
27
 
 
28
class QwtSimpleCompassRose::PrivateData
 
29
{
 
30
public:
 
31
    PrivateData():
 
32
        width( 0.2 ),
 
33
        numThorns( 8 ),
 
34
        numThornLevels( -1 ),
 
35
        shrinkFactor( 0.9 )
 
36
    {
 
37
    }
 
38
 
 
39
    double width;
 
40
    int numThorns;
 
41
    int numThornLevels;
 
42
    double shrinkFactor;
 
43
};
 
44
 
55
45
/*!
56
46
   Constructor
57
47
 
58
48
   \param numThorns Number of thorns
59
49
   \param numThornLevels Number of thorn levels
60
50
*/
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)
 
51
QwtSimpleCompassRose::QwtSimpleCompassRose(
 
52
    int numThorns, int numThornLevels )
66
53
{
67
 
    const QColor dark(128,128,255);
68
 
    const QColor light(192,255,255);
69
 
    
 
54
    d_data = new PrivateData();
 
55
    d_data->numThorns = numThorns;
 
56
    d_data->numThornLevels = numThornLevels;
 
57
 
 
58
    const QColor dark( 128, 128, 255 );
 
59
    const QColor light( 192, 255, 255 );
 
60
 
70
61
    QPalette palette;
71
62
    for ( int i = 0; i < QPalette::NColorGroups; i++ )
72
63
    {
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
 
64
        palette.setColor( ( QPalette::ColorGroup )i,
 
65
            QPalette::Dark, dark );
 
66
        palette.setColor( ( QPalette::ColorGroup )i,
 
67
            QPalette::Light, light );
84
68
    }
85
69
 
86
 
    setPalette(palette);
 
70
    setPalette( palette );
 
71
}
 
72
 
 
73
//! Destructor
 
74
QwtSimpleCompassRose::~QwtSimpleCompassRose()
 
75
{
 
76
    delete d_data;
 
77
}
 
78
 
 
79
/*!
 
80
  Set the Factor how to shrink the thorns with each level
 
81
  The default value is 0.9.
 
82
 
 
83
  \sa shrinkFactor()
 
84
*/
 
85
void QwtSimpleCompassRose::setShrinkFactor( double factor )
 
86
{
 
87
    d_data->shrinkFactor = factor;
 
88
}
 
89
 
 
90
/*!
 
91
  \return Factor how to shrink the thorns with each level
 
92
  \sa setShrinkFactor()
 
93
*/
 
94
double QwtSimpleCompassRose::shrinkFactor() const
 
95
{
 
96
    return d_data->shrinkFactor;
87
97
}
88
98
 
89
99
/*!
95
105
   \param north Position
96
106
   \param cg Color group
97
107
*/
98
 
void QwtSimpleCompassRose::draw(QPainter *painter, const QPoint &center, 
99
 
    int radius, double north, QPalette::ColorGroup cg) const
 
108
void QwtSimpleCompassRose::draw( QPainter *painter, const QPointF &center,
 
109
    double radius, double north, QPalette::ColorGroup cg ) const
100
110
{
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
111
    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
 
112
    pal.setCurrentColorGroup( cg );
 
113
 
 
114
    drawRose( painter, pal, center, radius, north, d_data->width,
 
115
        d_data->numThorns, d_data->numThornLevels, d_data->shrinkFactor );
121
116
}
122
117
 
123
118
/*!
134
129
   \param shrinkFactor Factor to shrink the thorns with each level
135
130
*/
136
131
void QwtSimpleCompassRose::drawRose(
137
 
    QPainter *painter, 
138
 
#if QT_VERSION < 0x040000
139
 
    const QColorGroup &cg,
140
 
#else
 
132
    QPainter *painter,
141
133
    const QPalette &palette,
142
 
#endif
143
 
    const QPoint &center, int radius, double north, double width,
144
 
    int numThorns, int numThornLevels, double shrinkFactor)
 
134
    const QPointF &center, double radius, double north, double width,
 
135
    int numThorns, int numThornLevels, double shrinkFactor )
145
136
{
146
137
    if ( numThorns < 4 )
147
138
        numThorns = 4;
160
151
 
161
152
    painter->save();
162
153
 
163
 
    painter->setPen(Qt::NoPen);
 
154
    painter->setPen( Qt::NoPen );
164
155
 
165
156
    for ( int j = 1; j <= numThornLevels; j++ )
166
157
    {
167
 
        double step =  pow(2.0, j) * M_PI / (double)numThorns;
 
158
        double step =  qPow( 2.0, j ) * M_PI / numThorns;
168
159
        if ( step > M_PI_2 )
169
160
            break;
170
161
 
180
171
            leafWidth = 16;
181
172
 
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 )
185
176
        {
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);
 
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 );
 
182
 
 
183
            QPainterPath darkPath;
 
184
            darkPath.moveTo( center );
 
185
            darkPath.lineTo( p );
 
186
            darkPath.lineTo( qwtIntersection( center, p3, p1, p ) );
 
187
 
 
188
            painter->setBrush( palette.brush( QPalette::Dark ) );
 
189
            painter->drawPath( darkPath );
 
190
 
 
191
            QPainterPath lightPath;
 
192
            lightPath.moveTo( center );
 
193
            lightPath.lineTo( p );
 
194
            lightPath.lineTo( qwtIntersection( center, p4, p2, p ) );
 
195
 
 
196
            painter->setBrush( palette.brush( QPalette::Light ) );
 
197
            painter->drawPath( lightPath );
214
198
        }
215
199
    }
216
200
    painter->restore();
222
206
 
223
207
   \param width Width
224
208
*/
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;
 
209
void QwtSimpleCompassRose::setWidth( double width )
 
210
{
 
211
    d_data->width = width;
 
212
    if ( d_data->width < 0.03 )
 
213
        d_data->width = 0.03;
 
214
 
 
215
    if ( d_data->width > 0.4 )
 
216
        d_data->width = 0.4;
 
217
}
 
218
 
 
219
//! \sa setWidth()
 
220
double QwtSimpleCompassRose::width() const
 
221
{
 
222
    return d_data->width;
234
223
}
235
224
 
236
225
/*!
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
239
228
 
240
229
  \param numThorns Number of thorns
241
230
  \sa numThorns(), setNumThornLevels()
242
231
*/
243
 
void QwtSimpleCompassRose::setNumThorns(int numThorns) 
 
232
void QwtSimpleCompassRose::setNumThorns( int numThorns )
244
233
{
245
234
    if ( numThorns < 4 )
246
235
        numThorns = 4;
248
237
    if ( numThorns % 4 )
249
238
        numThorns += 4 - numThorns % 4;
250
239
 
251
 
    d_numThorns = numThorns;
 
240
    d_data->numThorns = numThorns;
252
241
}
253
242
 
254
243
/*!
257
246
*/
258
247
int QwtSimpleCompassRose::numThorns() const
259
248
{
260
 
   return d_numThorns;
 
249
    return d_data->numThorns;
261
250
}
262
251
 
263
252
/*!
266
255
  \param numThornLevels Number of thorns levels
267
256
  \sa setNumThorns(), numThornLevels()
268
257
*/
269
 
void QwtSimpleCompassRose::setNumThornLevels(int numThornLevels) 
 
258
void QwtSimpleCompassRose::setNumThornLevels( int numThornLevels )
270
259
{
271
 
    d_numThornLevels = numThornLevels;
 
260
    d_data->numThornLevels = numThornLevels;
272
261
}
273
262
 
274
263
/*!
277
266
*/
278
267
int QwtSimpleCompassRose::numThornLevels() const
279
268
{
280
 
    return d_numThornLevels;
 
269
    return d_data->numThornLevels;
281
270
}