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