File: qwt__compass__rose_8cpp-source.html

package info (click to toggle)
libqwt 4.2.0-4.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 9,832 kB
  • ctags: 5,512
  • sloc: cpp: 22,973; ansic: 244; makefile: 66
file content (210 lines) | stat: -rw-r--r-- 11,454 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>Qwt User&apos;s Guide: qwt_compass_rose.cpp Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.3.8 -->
<div class="qindex"><a class="qindex" href="index.html">Main&nbsp;Page</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical&nbsp;List</a> | <a class="qindex" href="annotated.html">Class&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Class&nbsp;Members</a> | <a class="qindex" href="globals.html">File&nbsp;Members</a> | <a class="qindex" href="pages.html">Related&nbsp;Pages</a></div>
<h1>qwt_compass_rose.cpp</h1><pre class="fragment"><div>00001 <span class="comment">/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************</span>
00002 <span class="comment"> * Qwt Widget Library</span>
00003 <span class="comment"> * Copyright (C) 1997   Josef Wilgen</span>
00004 <span class="comment"> * Copyright (C) 2002   Uwe Rathmann</span>
00005 <span class="comment"> *</span>
00006 <span class="comment"> * This library is free software; you can redistribute it and/or</span>
00007 <span class="comment"> * modify it under the terms of the Qwt License, Version 1.0</span>
00008 <span class="comment"> *****************************************************************************/</span>
00009 
00010 <span class="preprocessor">#include &lt;math.h&gt;</span>
00011 <span class="preprocessor">#include &lt;qpainter.h&gt;</span>
00012 <span class="preprocessor">#include "<a class="code" href="qwt__math_8h.html">qwt_math.h</a>"</span>
00013 <span class="preprocessor">#include "qwt_painter.h"</span>
00014 <span class="preprocessor">#include "qwt_compass_rose.h"</span>
00015 
00016 <span class="keyword">static</span> QPoint cutPoint(QPoint p11, QPoint p12, QPoint p21, QPoint p22)
00017 {
00018     <span class="keywordtype">double</span> dx1 = p12.x() - p11.x();
00019     <span class="keywordtype">double</span> dy1 = p12.y() - p11.y();
00020     <span class="keywordtype">double</span> dx2 = p22.x() - p21.x();
00021     <span class="keywordtype">double</span> dy2 = p22.y() - p21.y();
00022 
00023     <span class="keywordflow">if</span> ( dx1 == 0.0 &amp;&amp; dx2 == 0.0 )
00024         <span class="keywordflow">return</span> QPoint();
00025 
00026     <span class="keywordflow">if</span> ( dx1 == 0.0 )
00027     {
00028         <span class="keyword">const</span> <span class="keywordtype">double</span> m = dy2 / dx2;
00029         <span class="keyword">const</span> <span class="keywordtype">double</span> t = p21.y() - m * p21.x();
00030         <span class="keywordflow">return</span> QPoint(p11.x(), qwtInt(m * p11.x() + t));
00031     }
00032 
00033     <span class="keywordflow">if</span> ( dx2 == 0 )
00034     {
00035         <span class="keyword">const</span> <span class="keywordtype">double</span> m = dy1 / dx1;
00036         <span class="keyword">const</span> <span class="keywordtype">double</span> t = p11.y() - m * p11.x();
00037         <span class="keywordflow">return</span> QPoint(p21.x(), qwtInt(m * p21.x() + t));
00038     }
00039 
00040     <span class="keyword">const</span> <span class="keywordtype">double</span> m1 = dy1 / dx1;
00041     <span class="keyword">const</span> <span class="keywordtype">double</span> t1 = p11.y() - m1 * p11.x();
00042 
00043     <span class="keyword">const</span> <span class="keywordtype">double</span> m2 = dy2 / dx2;
00044     <span class="keyword">const</span> <span class="keywordtype">double</span> t2 = p21.y() - m2 * p21.x();
00045 
00046     <span class="keywordflow">if</span> ( m1 == m2 )
00047         <span class="keywordflow">return</span> QPoint();
00048 
00049     <span class="keyword">const</span> <span class="keywordtype">double</span> x = ( t2 - t1 ) / ( m1 - m2 );
00050     <span class="keyword">const</span> <span class="keywordtype">double</span> y = t1 + m1 * x;
00051 
00052     <span class="keywordflow">return</span> QPoint(qwtInt(x), qwtInt(y));
00053 }
00054 
00055 QwtSimpleCompassRose::QwtSimpleCompassRose(<span class="keywordtype">int</span> numThorns, <span class="keywordtype">int</span> numThornLevels):
00056     d_width(0.2),
00057     d_numThorns(numThorns),
00058     d_numThornLevels(numThornLevels),
00059     d_shrinkFactor(0.9)
00060 {
00061     <span class="keyword">const</span> QColor dark(128,128,255);
00062     <span class="keyword">const</span> QColor light(192,255,255);
00063     
00064     QPalette palette;
00065     <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i = 0; i &lt; QPalette::NColorGroups; i++ )
00066     {
00067         palette.setColor((QPalette::ColorGroup)i,
00068             QColorGroup::Dark, dark);
00069         palette.setColor((QPalette::ColorGroup)i,
00070             QColorGroup::Light, light);
00071     }
00072 
00073     setPalette(palette);
00074 }
00075 
00076 <span class="keywordtype">void</span> QwtSimpleCompassRose::draw(QPainter *painter, <span class="keyword">const</span> QPoint &amp;center, 
00077     <span class="keywordtype">int</span> radius, <span class="keywordtype">double</span> north, QPalette::ColorGroup cg)<span class="keyword"> const</span>
00078 <span class="keyword"></span>{
00079     QColorGroup colorGroup;
00080     <span class="keywordflow">switch</span>(cg)
00081     {
00082         <span class="keywordflow">case</span> QPalette::Disabled:
00083             colorGroup = palette().disabled();
00084         <span class="keywordflow">case</span> QPalette::Inactive:
00085             colorGroup = palette().inactive();
00086         <span class="keywordflow">default</span>:
00087             colorGroup = palette().active();
00088     }
00089 
00090     <a class="code" href="class_qwt_compass.html#b1">drawRose</a>(painter, colorGroup, center, radius, north, d_width, 
00091         d_numThorns, d_numThornLevels, d_shrinkFactor);
00092 }
00093 
00094 <span class="keywordtype">void</span> QwtSimpleCompassRose::drawRose(
00095     QPainter *painter, <span class="keyword">const</span> QColorGroup &amp;cg,
00096     <span class="keyword">const</span> QPoint &amp;center, <span class="keywordtype">int</span> radius, <span class="keywordtype">double</span> north, <span class="keywordtype">double</span> width,
00097     <span class="keywordtype">int</span> numThorns, <span class="keywordtype">int</span> numThornLevels, <span class="keywordtype">double</span> shrinkFactor)
00098 {
00099     <span class="keywordflow">if</span> ( numThorns &lt; 4 )
00100         numThorns = 4;
00101 
00102     <span class="keywordflow">if</span> ( numThorns % 4 )
00103         numThorns += 4 - numThorns % 4;
00104 
00105     <span class="keywordflow">if</span> ( numThornLevels &lt;= 0 )
00106         numThornLevels = numThorns / 4;
00107 
00108     <span class="keywordflow">if</span> ( shrinkFactor &gt;= 1.0 )
00109         shrinkFactor = 1.0;
00110 
00111     <span class="keywordflow">if</span> ( shrinkFactor &lt;= 0.5 )
00112         shrinkFactor = 0.5;
00113 
00114     painter-&gt;save();
00115 
00116     painter-&gt;setPen(Qt::NoPen);
00117 
00118     <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> j = 1; j &lt;= numThornLevels; j++ )
00119     {
00120         <span class="keywordtype">double</span> <a class="code" href="class_qwt_dbl_range.html#a10">step</a> =  pow(2.0, j) * M_PI / (<span class="keywordtype">double</span>)numThorns;
00121         <span class="keywordflow">if</span> ( step &gt; M_PI_2 )
00122             <span class="keywordflow">break</span>;
00123 
00124         <span class="keywordtype">double</span> r = radius;
00125         <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> k = 0; k &lt; 3; k++ )
00126         {
00127             <span class="keywordflow">if</span> ( j + k &lt; numThornLevels )
00128                 r *= shrinkFactor;
00129         }
00130 
00131         <span class="keywordtype">double</span> leafWidth = r * width;
00132         <span class="keywordflow">if</span> ( 2.0 * M_PI / step &gt; 32 )
00133             leafWidth = 16;
00134 
00135         <span class="keyword">const</span> <span class="keywordtype">double</span> <a class="code" href="class_qwt_dial.html#a19">origin</a> = north / 180.0 * M_PI;
00136         <span class="keywordflow">for</span> ( <span class="keywordtype">double</span> angle = origin; 
00137             angle &lt; 2.0 * M_PI + origin; angle += step)
00138         {
00139             <span class="keyword">const</span> QPoint p = qwtPolar2Pos(center, r, angle);
00140             QPoint p1 = qwtPolar2Pos(center, leafWidth, angle + M_PI_2);
00141             QPoint p2 = qwtPolar2Pos(center, leafWidth, angle - M_PI_2);
00142 
00143             QPointArray pa(3);
00144             pa.setPoint(0, center);
00145             pa.setPoint(1, p);
00146 
00147             QPoint p3 = qwtPolar2Pos(center, r, angle + step / 2.0);
00148             p1 = cutPoint(center, p3, p1, p);
00149             pa.setPoint(2, p1);
00150             painter-&gt;setBrush(cg.brush(QColorGroup::Dark));
00151             painter-&gt;drawPolygon(pa);
00152 
00153             QPoint p4 = qwtPolar2Pos(center, r, angle - step / 2.0);
00154             p2 = cutPoint(center, p4, p2, p);
00155 
00156             pa.setPoint(2, p2);
00157             painter-&gt;setBrush(cg.brush(QColorGroup::Light));
00158             painter-&gt;drawPolygon(pa);
00159         }
00160     }
00161     painter-&gt;restore();
00162 }
00163 
00169 <span class="keywordtype">void</span> QwtSimpleCompassRose::setWidth(<span class="keywordtype">double</span> w) 
00170 {
00171    d_width = w;
00172    <span class="keywordflow">if</span> (d_width &lt; 0.03) 
00173         d_width = 0.03;
00174 
00175    <span class="keywordflow">if</span> (d_width &gt; 0.4) 
00176         d_width = 0.4;
00177 }
00178 
00179 <span class="keywordtype">void</span> QwtSimpleCompassRose::setNumThorns(<span class="keywordtype">int</span> numThorns) 
00180 {
00181     <span class="keywordflow">if</span> ( numThorns &lt; 4 )
00182         numThorns = 4;
00183 
00184     <span class="keywordflow">if</span> ( numThorns % 4 )
00185         numThorns += 4 - numThorns % 4;
00186 
00187     d_numThorns = numThorns;
00188 }
00189 
00190 <span class="keywordtype">int</span> QwtSimpleCompassRose::numThorns()<span class="keyword"> const</span>
00191 <span class="keyword"></span>{
00192    <span class="keywordflow">return</span> d_numThorns;
00193 }
00194 
00195 <span class="keywordtype">void</span> QwtSimpleCompassRose::setNumThornLevels(<span class="keywordtype">int</span> numThornLevels) 
00196 {
00197     d_numThornLevels = numThornLevels;
00198 }
00199 
00200 <span class="keywordtype">int</span> QwtSimpleCompassRose::numThornLevels()<span class="keyword"> const</span>
00201 <span class="keyword"></span>{
00202     <span class="keywordflow">return</span> d_numThornLevels;
00203 }
</div></pre><hr size="1"><address style="align: right;"><small>Generated on Tue Nov 16 21:12:20 2004 for Qwt User's Guide by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0 ></a> 1.3.8 </small></address>
</body>
</html>