##// END OF EJS Templates
Restored initialising of domain to zero values
Tero Ahola -
r1163:707a11810f5e
parent child
Show More
@@ -1,302 +1,302
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 Domain::Domain(QObject* parent):QObject(parent),
27 m_minX(0),
28 m_maxX(1),
29 m_minY(0),
30 m_maxY(1),
31 m_tickXCount(5),
32 m_tickYCount(5),
33 m_niceXNumbers(false),
34 m_niceYNumbers(false)
26 Domain::Domain(QObject* parent) : QObject(parent),
27 m_minX(0),
28 m_maxX(0),
29 m_minY(0),
30 m_maxY(0),
31 m_tickXCount(5),
32 m_tickYCount(5),
33 m_niceXNumbers(false),
34 m_niceYNumbers(false)
35 35 {
36 36 }
37 37
38 38 Domain::~Domain()
39 39 {
40 40 }
41 41
42 42 void Domain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY)
43 43 {
44 44 setRange(minX, maxX, minY, maxY,m_tickXCount,m_tickYCount);
45 45 }
46 46
47 47 void Domain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY,int tickXCount,int tickYCount)
48 48 {
49 49 bool axisXChanged = false;
50 50 bool axisYChanged = false;
51 51
52 52 if(m_tickXCount!=tickXCount) {
53 53 m_tickXCount=tickXCount;
54 54 axisXChanged=true;
55 55 }
56 56
57 57 if(m_tickYCount!=tickYCount) {
58 58 m_tickYCount=tickYCount;
59 59 axisYChanged=true;
60 60 }
61 61
62 62 if (!qFuzzyIsNull(m_minX - minX) || !qFuzzyIsNull(m_maxX - maxX)) {
63 63 if(m_niceXNumbers) looseNiceNumbers(minX, maxX, m_tickXCount);
64 64 m_minX=minX;
65 65 m_maxX=maxX;
66 66 axisXChanged=true;
67 67 }
68 68
69 69 if (!qFuzzyIsNull(m_minY - minY) || !qFuzzyIsNull(m_maxY - maxY)) {
70 70 if(m_niceYNumbers) looseNiceNumbers(minY, maxY, m_tickYCount);
71 71 m_minY=minY;
72 72 m_maxY=maxY;
73 73 axisYChanged=true;
74 74 }
75 75
76 76 if(axisXChanged || axisYChanged) {
77 77 emit this->domainChanged(m_minX, m_maxX, m_minY, m_maxY);
78 78 }
79 79
80 80 if(axisXChanged) {
81 81 emit rangeXChanged(minX,maxX, m_tickXCount);
82 82 }
83 83
84 84 if(axisYChanged) {
85 85 emit rangeYChanged(minY,maxY, m_tickYCount);
86 86 }
87 87 }
88 88
89 89 void Domain::setRangeX(qreal min, qreal max)
90 90 {
91 91 setRange(min,max,m_minY, m_maxY);
92 92 }
93 93
94 94 void Domain::setRangeX(qreal min, qreal max, int tickCount)
95 95 {
96 96 setRange(min,max,m_minY, m_maxY,tickCount,m_tickYCount);
97 97 }
98 98
99 99 void Domain::setRangeY(qreal min, qreal max)
100 100 {
101 101 setRange(m_minX, m_maxX, min, max);
102 102 }
103 103
104 104 void Domain::setRangeY(qreal min, qreal max,int tickCount)
105 105 {
106 106 setRange(m_minX, m_maxX, min, max,m_tickXCount,tickCount);
107 107 }
108 108
109 109 void Domain::setMinX(qreal min)
110 110 {
111 111 setRange(min, m_maxX, m_minY, m_maxY);
112 112 }
113 113
114 114 void Domain::setMaxX(qreal max)
115 115 {
116 116 setRange(m_minX, max, m_minY, m_maxY);
117 117 }
118 118
119 119 void Domain::setMinY(qreal min)
120 120 {
121 121 setRange(m_minX, m_maxX, min, m_maxY);
122 122 }
123 123
124 124 void Domain::setMaxY(qreal max)
125 125 {
126 126 setRange(m_minX, m_maxX, m_minY, max);
127 127 }
128 128
129 129 qreal Domain::spanX() const
130 130 {
131 131 Q_ASSERT(m_maxX >= m_minX);
132 132 return m_maxX - m_minX;
133 133 }
134 134
135 135 qreal Domain::spanY() const
136 136 {
137 137 Q_ASSERT(m_maxY >= m_minY);
138 138 return m_maxY - m_minY;
139 139 }
140 140
141 141 bool Domain::isEmpty() const
142 142 {
143 143 return qFuzzyIsNull(spanX()) || qFuzzyIsNull(spanY());
144 144 }
145 145
146 146 void Domain::zoomIn(const QRectF& rect, const QSizeF& size)
147 147 {
148 148 qreal dx = spanX() / size.width();
149 149 qreal dy = spanY() / size.height();
150 150
151 151 qreal maxX = m_maxX;
152 152 qreal minX = m_minX;
153 153 qreal minY = m_minY;
154 154 qreal maxY = m_maxY;
155 155
156 156 maxX = minX + dx * rect.right();
157 157 minX = minX + dx * rect.left();
158 158 minY = maxY - dy * rect.bottom();
159 159 maxY = maxY - dy * rect.top();
160 160
161 161 int tickXCount = m_tickXCount;
162 162 int tickYCount = m_tickYCount;
163 163
164 164 if(m_niceXNumbers) {
165 165 looseNiceNumbers(minX, maxX, tickXCount);
166 166 }
167 167 if(m_niceYNumbers) {
168 168 looseNiceNumbers(minY, maxY, tickYCount);
169 169 }
170 170 setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
171 171 }
172 172
173 173 void Domain::zoomOut(const QRectF& rect, const QSizeF& size)
174 174 {
175 175 qreal dx = spanX() / rect.width();
176 176 qreal dy = spanY() / rect.height();
177 177
178 178 qreal maxX = m_maxX;
179 179 qreal minX = m_minX;
180 180 qreal minY = m_minY;
181 181 qreal maxY = m_maxY;
182 182
183 183 minX = maxX - dx * rect.right();
184 184 maxX = minX + dx * size.width();
185 185 maxY = minY + dy * rect.bottom();
186 186 minY = maxY - dy * size.height();
187 187
188 188 int tickXCount = m_tickXCount;
189 189 int tickYCount = m_tickYCount;
190 190
191 191 if(m_niceXNumbers) {
192 192 looseNiceNumbers(minX, maxX, tickXCount);
193 193 }
194 194 if(m_niceYNumbers) {
195 195 looseNiceNumbers(minY, maxY, tickYCount);
196 196 }
197 197 setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
198 198 }
199 199
200 200 void Domain::move(int dx,int dy,const QSizeF& size)
201 201 {
202 202 qreal x = spanX() / size.width();
203 203 qreal y = spanY() / size.height();
204 204
205 205 qreal maxX = m_maxX;
206 206 qreal minX = m_minX;
207 207 qreal minY = m_minY;
208 208 qreal maxY = m_maxY;
209 209
210 210 if(dx!=0) {
211 211 minX = minX + x * dx;
212 212 maxX = maxX + x * dx;
213 213 }
214 214 if(dy!=0) {
215 215 minY = minY + y * dy;
216 216 maxY = maxY + y * dy;
217 217 }
218 218 setRange(minX,maxX,minY,maxY);
219 219 }
220 220
221 221 void Domain::handleAxisXChanged(qreal min,qreal max,int tickXCount,bool niceNumbers)
222 222 {
223 223 if (m_niceXNumbers != niceNumbers) {
224 224 m_niceXNumbers = niceNumbers;
225 225 //force recalculation
226 226 m_minX = 0;
227 227 m_maxX = 0;
228 228 }
229 229 setRange(min,max,m_minY, m_maxY,tickXCount,m_tickYCount);
230 230 }
231 231
232 232 void Domain::handleAxisYChanged(qreal min,qreal max,int tickYCount,bool niceNumbers)
233 233 {
234 234 if (m_niceYNumbers != niceNumbers) {
235 235 m_niceYNumbers = niceNumbers;
236 236 //force recalculation
237 237 m_minY = 0;
238 238 m_maxY = 0;
239 239 }
240 240 setRange(m_minX, m_maxX, min, max,m_tickXCount,tickYCount);
241 241 }
242 242
243 243 //algorithm defined by Paul S.Heckbert GraphicalGems I
244 244
245 245 void Domain::looseNiceNumbers(qreal &min, qreal &max, int &ticksCount) const
246 246 {
247 247 qreal range = niceNumber(max-min,true); //range with ceiling
248 248 qreal step = niceNumber(range/(ticksCount-1),false);
249 249 min = floor(min/step);
250 250 max = ceil(max/step);
251 251 ticksCount = int(max-min) +1;
252 252 min*=step;
253 253 max*=step;
254 254 }
255 255
256 256 //nice numbers can be expressed as form of 1*10^n, 2* 10^n or 5*10^n
257 257
258 258 qreal Domain::niceNumber(qreal x,bool ceiling) const
259 259 {
260 260 qreal z = pow(10,floor(log10(x))); //find corresponding number of the form of 10^n than is smaller than x
261 261 qreal q = x/z;//q<10 && q>=1;
262 262
263 263 if(ceiling) {
264 264 if(q <= 1.0) q=1;
265 265 else if(q <= 2.0) q=2;
266 266 else if(q <= 5.0) q=5;
267 267 else q=10;
268 268 }
269 269 else {
270 270 if(q < 1.5) q=1;
271 271 else if(q < 3.0) q=2;
272 272 else if(q < 7.0) q=5;
273 273 else q=10;
274 274 }
275 275 return q*z;
276 276 }
277 277
278 278
279 279 bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator== (const Domain &domain1, const Domain &domain2)
280 280 {
281 281 return (qFuzzyIsNull(domain1.m_maxX - domain2.m_maxX) &&
282 282 qFuzzyIsNull(domain1.m_maxY - domain2.m_maxY) &&
283 283 qFuzzyIsNull(domain1.m_minX - domain2.m_minX) &&
284 284 qFuzzyIsNull(domain1.m_minY - domain2.m_minY));
285 285 }
286 286
287 287
288 288 bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator!= (const Domain &domain1, const Domain &domain2)
289 289 {
290 290 return !(domain1 == domain2);
291 291 }
292 292
293 293
294 294 QDebug QTCOMMERCIALCHART_AUTOTEST_EXPORT operator<<(QDebug dbg, const Domain &domain)
295 295 {
296 296 dbg.nospace() << "Domain("<<domain.m_minX<<','<<domain.m_maxX<<','<<domain.m_minY<<','<<domain.m_maxY<<')' << domain.m_tickXCount << "," << domain.m_tickYCount ;
297 297 return dbg.maybeSpace();
298 298 }
299 299
300 300 #include "moc_domain_p.cpp"
301 301
302 302 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now