##// END OF EJS Templates
bugfxi minor. wrong return value for niceNumberEnabled
Michal Klocek -
r2315:5642c6ee36d3
parent child
Show More
@@ -1,409 +1,409
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 "qvalueaxis.h"
22 22 #include "qvalueaxis_p.h"
23 23 #include "chartvalueaxisx_p.h"
24 24 #include "chartvalueaxisy_p.h"
25 25 #include "abstractdomain_p.h"
26 26 #include "chartdataset_p.h"
27 27 #include "chartpresenter_p.h"
28 28 #include "charttheme_p.h"
29 29
30 30
31 31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 32 /*!
33 33 \class QValueAxis
34 34 \brief The QValueAxis class is used for manipulating chart's axis.
35 35 \mainclass
36 36
37 37 ValueAxis can be setup to show axis line with tick marks, grid lines and shades.
38 38 Values of axis are drawn to position of ticks.
39 39
40 40 Example code on how to use QValueAxis.
41 41 \code
42 42 QChartView *chartView = new QChartView;
43 43 QLineSeries *series = new QLineSeries;
44 44 // ...
45 45 chartView->chart()->addSeries(series);
46 46
47 47 QValueAxis *axisX = new QValueAxis;
48 48 axisX->setRange(10, 20.5);
49 49 axisX->setTickCount(10);
50 50 axisX->setLabelFormat("%.2f");
51 51 chartView->chart()->setAxisX(axisX, series);
52 52 \endcode
53 53 */
54 54
55 55 /*!
56 56 \qmlclass ValueAxis QValueAxis
57 57 \inherits AbstractAxis
58 58 \brief The ValueAxis element is used for manipulating chart's axes
59 59
60 60 ValueAxis can be setup to show axis line with tick marks, grid lines and shades.
61 61 Values of axis are drawn to position of ticks
62 62
63 63 To access Axes you can use ChartView API. For example:
64 64 \code
65 65 ChartView {
66 66 ValueAxis {
67 67 id: xAxis
68 68 min: 0
69 69 max: 10
70 70 }
71 71 // Add a few series...
72 72 }
73 73 \endcode
74 74 */
75 75
76 76 /*!
77 77 \property QValueAxis::min
78 78 Defines the minimum value on the axis.
79 79 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
80 80 */
81 81 /*!
82 82 \qmlproperty real ValueAxis::min
83 83 Defines the minimum value on the axis.
84 84 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
85 85 */
86 86
87 87 /*!
88 88 \property QValueAxis::max
89 89 Defines the maximum value on the axis.
90 90 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
91 91 */
92 92 /*!
93 93 \qmlproperty real ValueAxis::max
94 94 Defines the maximum value on the axis.
95 95 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
96 96 */
97 97
98 98 /*!
99 99 \property QValueAxis::tickCount
100 100 Defines the number of ticks on the axis. This indicates how many grid lines are draw on the chart.
101 101 The default value is 5, and it can not be below 2.
102 102 */
103 103 /*!
104 104 \qmlproperty real ValueAxis::tickCount
105 105 Defines the number of ticks on the axis. This indicates how many grid lines are draw on the chart.
106 106 The default value is 5, and it can not be below 2.
107 107 */
108 108
109 109 /*!
110 110 \property QValueAxis::labelFormat
111 111 Defines the label format for the axis.
112 112 Supported specifiers are: d, i, o, x, X, f, F, e, E, g, G, c
113 113 See QString::sprintf() for additional details.
114 114 */
115 115 /*!
116 116 \qmlproperty real ValueAxis::labelFormat
117 117 Defines the label format for the axis.
118 118 Supported specifiers are: d, i, o, x, X, f, F, e, E, g, G, c
119 119 See QString::sprintf() for additional details.
120 120 */
121 121
122 122 /*!
123 123 \fn void QValueAxis::minChanged(qreal min)
124 124 Axis emits signal when \a min of axis has changed.
125 125 */
126 126 /*!
127 127 \qmlsignal ValueAxis::onMinChanged(real min)
128 128 Axis emits signal when \a min of axis has changed.
129 129 */
130 130
131 131 /*!
132 132 \fn void QValueAxis::maxChanged(qreal max)
133 133 Axis emits signal when \a max of axis has changed.
134 134 */
135 135 /*!
136 136 \qmlsignal ValueAxis::onMaxChanged(real max)
137 137 Axis emits signal when \a max of axis has changed.
138 138 */
139 139
140 140 /*!
141 141 \fn void QValueAxis::tickCountChanged(int tickCount)
142 142 Axis emits signal when number of ticks on axis have changed.
143 143 */
144 144 /*!
145 145 \qmlsignal ValueAxis::tickCountChanged(int tickCount)
146 146 Axis emits signal when number of ticks on axis have changed.
147 147 */
148 148
149 149 /*!
150 150 \fn void QValueAxis::rangeChanged(qreal min, qreal max)
151 151 Axis emits signal when \a min or \a max of axis has changed.
152 152 */
153 153
154 154 /*!
155 155 \property QValueAxis::niceNumbersEnabled
156 156 Whether the nice numbers algorithm is enabled or not for the axis.
157 157 */
158 158
159 159 /*!
160 160 \qmlproperty bool ValueAxis::niceNumbersEnabled
161 161 Whether the nice numbers algorithm is enabled or not for the axis.
162 162 */
163 163
164 164 /*!
165 165 Constructs an axis object which is a child of \a parent.
166 166 */
167 167 QValueAxis::QValueAxis(QObject *parent) :
168 168 QAbstractAxis(*new QValueAxisPrivate(this), parent)
169 169 {
170 170
171 171 }
172 172
173 173 /*!
174 174 \internal
175 175 */
176 176 QValueAxis::QValueAxis(QValueAxisPrivate &d, QObject *parent)
177 177 : QAbstractAxis(d, parent)
178 178 {
179 179
180 180 }
181 181
182 182 /*!
183 183 Destroys the object
184 184 */
185 185 QValueAxis::~QValueAxis()
186 186 {
187 187 Q_D(QValueAxis);
188 188 if (d->m_chart)
189 189 d->m_chart->removeAxis(this);
190 190 }
191 191
192 192 void QValueAxis::setMin(qreal min)
193 193 {
194 194 Q_D(QValueAxis);
195 195 setRange(min, qMax(d->m_max, min));
196 196 }
197 197
198 198 qreal QValueAxis::min() const
199 199 {
200 200 Q_D(const QValueAxis);
201 201 return d->m_min;
202 202 }
203 203
204 204 void QValueAxis::setMax(qreal max)
205 205 {
206 206 Q_D(QValueAxis);
207 207 setRange(qMin(d->m_min, max), max);
208 208 }
209 209
210 210 qreal QValueAxis::max() const
211 211 {
212 212 Q_D(const QValueAxis);
213 213 return d->m_max;
214 214 }
215 215
216 216 /*!
217 217 Sets range from \a min to \a max on the axis.
218 218 If min is greater than max then this function returns without making any changes.
219 219 */
220 220 void QValueAxis::setRange(qreal min, qreal max)
221 221 {
222 222 Q_D(QValueAxis);
223 223 d->setRange(min,max);
224 224 }
225 225
226 226 void QValueAxis::setTickCount(int count)
227 227 {
228 228 Q_D(QValueAxis);
229 229 if (d->m_tickCount != count && count >= 2) {
230 230 d->m_tickCount = count;
231 231 emit tickCountChanged(count);
232 232 }
233 233 }
234 234
235 235 int QValueAxis::tickCount() const
236 236 {
237 237 Q_D(const QValueAxis);
238 238 return d->m_tickCount;
239 239 }
240 240
241 241 void QValueAxis::setNiceNumbersEnabled(bool enable)
242 242 {
243 243 Q_D(QValueAxis);
244 244 qWarning()<<"This function is depreciated, it can lead to unexpected behaviour.Use applyNiceNumbers(). ";
245 245 if(enable) {
246 246 QObject::connect(this,SIGNAL(rangeChanged(qreal,qreal)),this,SLOT(applyNiceNumbers()));
247 247 QObject::connect(this,SIGNAL(tickCountChanged(int)),this,SLOT(applyNiceNumbers()));
248 248 applyNiceNumbers();
249 249 }
250 250 else {
251 251 QObject::disconnect(this,SIGNAL(rangeChanged(qreal,qreal)),this,SLOT(applyNiceNumbers()));
252 252 QObject::disconnect(this,SIGNAL(tickCountChanged(int)),this,SLOT(applyNiceNumbers()));
253 253 }
254 d->m_niceNumbersEnabled=true;
254 d->m_niceNumbersEnabled=enable;
255 255 }
256 256
257 257 bool QValueAxis::niceNumbersEnabled() const
258 258 {
259 259 Q_D(const QValueAxis);
260 260 qWarning()<<"This function is depreciated.Use applyNiceNumbers().";
261 261 return d->m_niceNumbersEnabled;
262 262 }
263 263
264 264 void QValueAxis::setLabelFormat(const QString &format)
265 265 {
266 266 Q_D(QValueAxis);
267 267 d->m_format = format;
268 268 }
269 269
270 270 QString QValueAxis::labelFormat() const
271 271 {
272 272 Q_D(const QValueAxis);
273 273 return d->m_format;
274 274 }
275 275
276 276 /*!
277 277 Returns the type of the axis
278 278 */
279 279 QAbstractAxis::AxisType QValueAxis::type() const
280 280 {
281 281 return AxisTypeValue;
282 282 }
283 283
284 284 void QValueAxis::applyNiceNumbers()
285 285 {
286 286 Q_D(QValueAxis);
287 287 if(d->m_applying) return;
288 288 qreal min = d->m_min;
289 289 qreal max = d->m_max;
290 290 int ticks = d->m_tickCount;
291 291 AbstractDomain::looseNiceNumbers(min,max,ticks);
292 292 d->m_applying=true;
293 293 d->setRange(min,max);
294 294 setTickCount(ticks);
295 295 d->m_applying=false;
296 296 }
297 297
298 298 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
299 299
300 300 QValueAxisPrivate::QValueAxisPrivate(QValueAxis *q)
301 301 : QAbstractAxisPrivate(q),
302 302 m_min(0),
303 303 m_max(0),
304 304 m_tickCount(5),
305 305 m_format(QString::null),
306 306 m_applying(false),
307 307 m_niceNumbersEnabled(false)
308 308 {
309 309
310 310 }
311 311
312 312 QValueAxisPrivate::~QValueAxisPrivate()
313 313 {
314 314
315 315 }
316 316
317 317 void QValueAxisPrivate::setMin(const QVariant &min)
318 318 {
319 319 Q_Q(QValueAxis);
320 320 bool ok;
321 321 qreal value = min.toReal(&ok);
322 322 if (ok)
323 323 q->setMin(value);
324 324 }
325 325
326 326 void QValueAxisPrivate::setMax(const QVariant &max)
327 327 {
328 328 Q_Q(QValueAxis);
329 329 bool ok;
330 330 qreal value = max.toReal(&ok);
331 331 if (ok)
332 332 q->setMax(value);
333 333 }
334 334
335 335 void QValueAxisPrivate::setRange(const QVariant &min, const QVariant &max)
336 336 {
337 337 Q_Q(QValueAxis);
338 338 bool ok1;
339 339 bool ok2;
340 340 qreal value1 = min.toReal(&ok1);
341 341 qreal value2 = max.toReal(&ok2);
342 342 if (ok1 && ok2)
343 343 q->setRange(value1, value2);
344 344 }
345 345
346 346 void QValueAxisPrivate::setRange(qreal min, qreal max)
347 347 {
348 348 Q_Q(QValueAxis);
349 349 bool changed = false;
350 350
351 351 if (min > max)
352 352 return;
353 353
354 354 if (!qFuzzyCompare(m_min,min)) {
355 355 m_min = min;
356 356 changed = true;
357 357 emit q->minChanged(min);
358 358 }
359 359
360 360 if (!qFuzzyCompare(m_max,max)) {
361 361 m_max = max;
362 362 changed = true;
363 363 emit q->maxChanged(max);
364 364 }
365 365
366 366 if (changed) {
367 367 emit rangeChanged(min,max);
368 368 emit q->rangeChanged(min, max);
369 369 }
370 370 }
371 371
372 372 void QValueAxisPrivate::initializeGraphics(QGraphicsItem* parent)
373 373 {
374 374 Q_Q(QValueAxis);
375 375 ChartAxis* axis(0);
376 376 if (orientation() == Qt::Vertical)
377 377 axis = new ChartValueAxisY(q,parent);
378 378 if (orientation() == Qt::Horizontal)
379 379 axis = new ChartValueAxisX(q,parent);
380 380
381 381 m_item.reset(axis);
382 382 QAbstractAxisPrivate::initializeGraphics(parent);
383 383 }
384 384
385 385
386 386 void QValueAxisPrivate::initializeDomain(AbstractDomain *domain)
387 387 {
388 388 if (orientation() == Qt::Vertical) {
389 389 if(!qFuzzyIsNull(m_max - m_min)) {
390 390 domain->setRangeY(m_min, m_max);
391 391 }
392 392 else {
393 393 setRange(domain->minY(), domain->maxY());
394 394 }
395 395 }
396 396 if (orientation() == Qt::Horizontal) {
397 397 if(!qFuzzyIsNull(m_max - m_min)) {
398 398 domain->setRangeX(m_min, m_max);
399 399 }
400 400 else {
401 401 setRange(domain->minX(), domain->maxX());
402 402 }
403 403 }
404 404 }
405 405
406 406 #include "moc_qvalueaxis.cpp"
407 407 #include "moc_qvalueaxis_p.cpp"
408 408
409 409 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now