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