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