@@ -1,277 +1,287 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include "domain_p.h" |
|
22 | 22 | #include <cmath> |
|
23 | 23 | |
|
24 | 24 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
25 | 25 | |
|
26 | 26 | Domain::Domain(QObject* parent):QObject(parent), |
|
27 | 27 | m_minX(0), |
|
28 | 28 | m_maxX(0), |
|
29 | 29 | m_minY(0), |
|
30 | 30 | m_maxY(0), |
|
31 | 31 | m_tickXCount(5), |
|
32 | 32 | m_tickYCount(5), |
|
33 | 33 | m_niceNumbers(false) |
|
34 | 34 | { |
|
35 | 35 | } |
|
36 | 36 | |
|
37 | 37 | Domain::~Domain() |
|
38 | 38 | { |
|
39 | 39 | } |
|
40 | 40 | |
|
41 | 41 | void Domain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY) |
|
42 | 42 | { |
|
43 | 43 | setRange(minX, maxX, minY, maxY,m_tickXCount,m_tickYCount); |
|
44 | 44 | } |
|
45 | 45 | |
|
46 | 46 | void Domain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY,int tickXCount,int tickYCount) |
|
47 | 47 | { |
|
48 | 48 | bool domainChanged = false; |
|
49 | 49 | bool tickXChanged = false; |
|
50 | 50 | bool tickYChanged = false; |
|
51 | 51 | |
|
52 | 52 | if(m_tickXCount!=tickXCount) { |
|
53 | 53 | m_tickXCount=tickXCount; |
|
54 | 54 | tickXChanged=true; |
|
55 | 55 | } |
|
56 | 56 | |
|
57 | 57 | if(m_tickYCount!=tickYCount) { |
|
58 | 58 | m_tickYCount=tickYCount; |
|
59 | 59 | tickYChanged=true; |
|
60 | 60 | } |
|
61 | 61 | |
|
62 | 62 | if (!qFuzzyIsNull(m_minX - minX) || !qFuzzyIsNull(m_maxX - maxX)) { |
|
63 | 63 | if(m_niceNumbers) looseNiceNumbers(minX, maxX, m_tickXCount); |
|
64 | 64 | m_minX=minX; |
|
65 | 65 | m_maxX=maxX; |
|
66 | 66 | domainChanged=true; |
|
67 | 67 | tickXChanged=false; |
|
68 | 68 | emit rangeXChanged(minX,maxX, m_tickXCount); |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | if (!qFuzzyIsNull(m_minY - minY) || !qFuzzyIsNull(m_maxY - maxY)) { |
|
72 | 72 | if(m_niceNumbers) looseNiceNumbers(minY, maxY, m_tickYCount); |
|
73 | 73 | m_minY=minY; |
|
74 | 74 | m_maxY=maxY; |
|
75 | 75 | domainChanged=true; |
|
76 | 76 | tickYChanged=false; |
|
77 | 77 | emit rangeYChanged(minY,maxY, m_tickYCount); |
|
78 | 78 | } |
|
79 | 79 | |
|
80 | 80 | if(domainChanged) { |
|
81 | 81 | emit this->domainChanged(m_minX, m_maxX, m_minY, m_maxY); |
|
82 | 82 | } |
|
83 | 83 | |
|
84 | 84 | if(tickXChanged) { |
|
85 | 85 | emit rangeXChanged(minX,maxX, m_tickXCount); |
|
86 | 86 | } |
|
87 | 87 | |
|
88 | 88 | if(tickYChanged) { |
|
89 | 89 | emit rangeYChanged(minY,maxY, m_tickYCount); |
|
90 | 90 | } |
|
91 | 91 | } |
|
92 | 92 | |
|
93 | 93 | void Domain::setRangeX(qreal min, qreal max) |
|
94 | 94 | { |
|
95 | 95 | setRange(min,max,m_minY, m_maxY); |
|
96 | 96 | } |
|
97 | 97 | |
|
98 | 98 | void Domain::setRangeX(qreal min, qreal max, int tickCount) |
|
99 | 99 | { |
|
100 | 100 | setRange(min,max,m_minY, m_maxY,tickCount,m_tickYCount); |
|
101 | 101 | } |
|
102 | 102 | |
|
103 | 103 | void Domain::setRangeY(qreal min, qreal max) |
|
104 | 104 | { |
|
105 | 105 | setRange(m_minX, m_maxX, min, max); |
|
106 | 106 | } |
|
107 | 107 | |
|
108 | 108 | void Domain::setRangeY(qreal min, qreal max,int tickCount) |
|
109 | 109 | { |
|
110 | 110 | setRange(m_minX, m_maxX, min, max,m_tickXCount,tickCount); |
|
111 | 111 | } |
|
112 | 112 | |
|
113 | 113 | void Domain::setMinX(qreal min) |
|
114 | 114 | { |
|
115 | 115 | setRange(min, m_maxX, m_minY, m_maxY); |
|
116 | 116 | } |
|
117 | 117 | |
|
118 | 118 | void Domain::setMaxX(qreal max) |
|
119 | 119 | { |
|
120 | 120 | setRange(m_minX, max, m_minY, m_maxY); |
|
121 | 121 | } |
|
122 | 122 | |
|
123 | 123 | void Domain::setMinY(qreal min) |
|
124 | 124 | { |
|
125 | 125 | setRange(m_minX, m_maxX, min, m_maxY); |
|
126 | 126 | } |
|
127 | 127 | |
|
128 | 128 | void Domain::setMaxY(qreal max) |
|
129 | 129 | { |
|
130 | 130 | setRange(m_minX, m_maxX, m_minY, max); |
|
131 | 131 | } |
|
132 | 132 | |
|
133 | 133 | qreal Domain::spanX() const |
|
134 | 134 | { |
|
135 | 135 | Q_ASSERT(m_maxX >= m_minX); |
|
136 | 136 | return m_maxX - m_minX; |
|
137 | 137 | } |
|
138 | 138 | |
|
139 | 139 | qreal Domain::spanY() const |
|
140 | 140 | { |
|
141 | 141 | Q_ASSERT(m_maxY >= m_minY); |
|
142 | 142 | return m_maxY - m_minY; |
|
143 | 143 | } |
|
144 | 144 | |
|
145 | 145 | bool Domain::isEmpty() const |
|
146 | 146 | { |
|
147 | 147 | return qFuzzyIsNull(spanX()) || qFuzzyIsNull(spanY()); |
|
148 | 148 | } |
|
149 | 149 | |
|
150 | 150 | void Domain::zoomIn(const QRectF& rect, const QSizeF& size) |
|
151 | 151 | { |
|
152 | 152 | qreal dx = spanX() / size.width(); |
|
153 | 153 | qreal dy = spanY() / size.height(); |
|
154 | 154 | |
|
155 | 155 | m_maxX = m_minX + dx * rect.right(); |
|
156 | 156 | m_minX = m_minX + dx * rect.left(); |
|
157 | 157 | m_minY = m_maxY - dy * rect.bottom(); |
|
158 | 158 | m_maxY = m_maxY - dy * rect.top(); |
|
159 | 159 | |
|
160 | 160 | if(m_niceNumbers) { |
|
161 | 161 | looseNiceNumbers(m_minX, m_maxX, m_tickXCount); |
|
162 | 162 | looseNiceNumbers(m_minY, m_maxY, m_tickYCount); |
|
163 | 163 | } |
|
164 | 164 | |
|
165 | 165 | emit domainChanged(m_minX, m_maxX, m_minY, m_maxY); |
|
166 | 166 | emit rangeXChanged(m_minX, m_maxX, m_tickXCount); |
|
167 | 167 | emit rangeYChanged(m_minY, m_maxY, m_tickYCount); |
|
168 | 168 | } |
|
169 | 169 | |
|
170 | 170 | void Domain::zoomOut(const QRectF& rect, const QSizeF& size) |
|
171 | 171 | { |
|
172 | 172 | qreal dx = spanX() / rect.width(); |
|
173 | 173 | qreal dy = spanY() / rect.height(); |
|
174 | 174 | |
|
175 | 175 | m_minX = m_maxX - dx * rect.right(); |
|
176 | 176 | m_maxX = m_minX + dx * size.width(); |
|
177 | 177 | m_maxY = m_minY + dy * rect.bottom(); |
|
178 | 178 | m_minY = m_maxY - dy * size.height(); |
|
179 | 179 | |
|
180 | 180 | if(m_niceNumbers) { |
|
181 | 181 | looseNiceNumbers(m_minX, m_maxX, m_tickXCount); |
|
182 | 182 | looseNiceNumbers(m_minY, m_maxY, m_tickYCount); |
|
183 | 183 | } |
|
184 | 184 | |
|
185 | 185 | emit domainChanged(m_minX, m_maxX, m_minY, m_maxY); |
|
186 | 186 | emit rangeXChanged(m_minX, m_maxX, m_tickXCount); |
|
187 | 187 | emit rangeYChanged(m_minY, m_maxY, m_tickYCount); |
|
188 | 188 | } |
|
189 | 189 | |
|
190 | 190 | void Domain::move(int dx,int dy,const QSizeF& size) |
|
191 | 191 | { |
|
192 | 192 | qreal x = spanX() / size.width(); |
|
193 | 193 | qreal y = spanY() / size.height(); |
|
194 | 194 | |
|
195 | 195 | if(dx!=0) { |
|
196 | 196 | m_minX = m_minX + x * dx; |
|
197 | 197 | m_maxX = m_maxX + x * dx; |
|
198 | 198 | emit rangeXChanged(m_minX, m_maxX, m_tickXCount); |
|
199 | 199 | } |
|
200 | 200 | if(dy!=0) { |
|
201 | 201 | m_minY = m_minY + y * dy; |
|
202 | 202 | m_maxY = m_maxY + y * dy; |
|
203 | 203 | emit rangeYChanged(m_minY, m_maxY, m_tickYCount); |
|
204 | 204 | } |
|
205 | 205 | |
|
206 | 206 | emit domainChanged(m_minX, m_maxX, m_minY, m_maxY); |
|
207 | 207 | } |
|
208 | 208 | |
|
209 | 209 | void Domain::handleAxisXChanged(qreal min,qreal max,int tickXCount,bool niceNumbers) |
|
210 | 210 | { |
|
211 |
m_niceNumbers=niceNumbers |
|
|
211 | if (niceNumbers && m_niceNumbers != niceNumbers) { | |
|
212 | m_niceNumbers = niceNumbers; | |
|
213 | //force recalculation | |
|
214 | m_minX = 0; | |
|
215 | m_maxX = 0; | |
|
216 | } | |
|
212 | 217 | setRange(min,max,m_minY, m_maxY,tickXCount,m_tickYCount); |
|
213 | 218 | } |
|
214 | 219 | |
|
215 | 220 | void Domain::handleAxisYChanged(qreal min,qreal max,int tickYCount,bool niceNumbers) |
|
216 | 221 | { |
|
217 |
m_niceNumbers=niceNumbers |
|
|
222 | if (niceNumbers && m_niceNumbers != niceNumbers) { | |
|
223 | m_niceNumbers = niceNumbers; | |
|
224 | //force recalculation | |
|
225 | m_minY = 0; | |
|
226 | m_maxY = 0; | |
|
227 | } | |
|
218 | 228 | setRange(m_minX, m_maxX, min, max,m_tickXCount,tickYCount); |
|
219 | 229 | } |
|
220 | 230 | |
|
221 | 231 | //algorithm defined by Paul S.Heckbert GraphicalGems I |
|
222 | 232 | |
|
223 | 233 | void Domain::looseNiceNumbers(qreal &min, qreal &max, int &ticksCount) |
|
224 | 234 | { |
|
225 | 235 | qreal range = niceNumber(max-min,true); //range with ceiling |
|
226 | 236 | qreal step = niceNumber(range/(ticksCount-1),false); |
|
227 | 237 | min = floor(min/step); |
|
228 | 238 | max = ceil(max/step); |
|
229 | 239 | ticksCount = int(max-min) +1; |
|
230 | 240 | min*=step; |
|
231 | 241 | max*=step; |
|
232 | 242 | } |
|
233 | 243 | |
|
234 | 244 | //nice numbers can be expressed as form of 1*10^n, 2* 10^n or 5*10^n |
|
235 | 245 | |
|
236 | 246 | qreal Domain::niceNumber(qreal x,bool ceiling) |
|
237 | 247 | { |
|
238 | 248 | qreal z = pow(10,floor(log10(x))); //find corresponding number of the form of 10^n than is smaller than x |
|
239 | 249 | qreal q = x/z;//q<10 && q>=1; |
|
240 | 250 | |
|
241 | 251 | if(ceiling) { |
|
242 | 252 | if(q <= 1.0) q=1; |
|
243 | 253 | else if(q <= 2.0) q=2; |
|
244 | 254 | else if(q <= 5.0) q=5; |
|
245 | 255 | else q=10; |
|
246 | 256 | } |
|
247 | 257 | else { |
|
248 | 258 | if(q < 1.5) q=1; |
|
249 | 259 | else if(q < 3.0) q=2; |
|
250 | 260 | else if(q < 7.0) q=5; |
|
251 | 261 | else q=10; |
|
252 | 262 | } |
|
253 | 263 | return q*z; |
|
254 | 264 | } |
|
255 | 265 | |
|
256 | 266 | bool operator== (const Domain &domain1, const Domain &domain2) |
|
257 | 267 | { |
|
258 | 268 | return (qFuzzyIsNull(domain1.m_maxX - domain2.m_maxX) && |
|
259 | 269 | qFuzzyIsNull(domain1.m_maxY - domain2.m_maxY) && |
|
260 | 270 | qFuzzyIsNull(domain1.m_minX - domain2.m_minX) && |
|
261 | 271 | qFuzzyIsNull(domain1.m_minY - domain2.m_minY)); |
|
262 | 272 | } |
|
263 | 273 | |
|
264 | 274 | bool operator!= (const Domain &domain1, const Domain &domain2) |
|
265 | 275 | { |
|
266 | 276 | return !(domain1 == domain2); |
|
267 | 277 | } |
|
268 | 278 | |
|
269 | 279 | QDebug operator<<(QDebug dbg, const Domain &domain) |
|
270 | 280 | { |
|
271 | 281 | dbg.nospace() << "Domain("<<domain.m_minX<<','<<domain.m_maxX<<','<<domain.m_minY<<','<<domain.m_maxY<<')' << domain.m_tickXCount << "," << domain.m_tickYCount ; |
|
272 | 282 | return dbg.maybeSpace(); |
|
273 | 283 | } |
|
274 | 284 | |
|
275 | 285 | #include "moc_domain_p.cpp" |
|
276 | 286 | |
|
277 | 287 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now