##// END OF EJS Templates
Fixes tst_qchartview
Michal Klocek -
r1652:ea4070861878
parent child
Show More
@@ -1,384 +1,381
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 "qbarcategoriesaxis.h"
21 #include "qbarcategoriesaxis.h"
22 #include "qbarcategoriesaxis_p.h"
22 #include "qbarcategoriesaxis_p.h"
23 #include "chartcategoriesaxisx_p.h"
23 #include "chartcategoriesaxisx_p.h"
24 #include "chartcategoriesaxisy_p.h"
24 #include "chartcategoriesaxisy_p.h"
25 #include <qmath.h>
25 #include <qmath.h>
26 #include <QDebug>
26 #include <QDebug>
27
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 /*!
29 /*!
30 \class QBarCategoriesAxis
30 \class QBarCategoriesAxis
31 \brief The QBarCategoriesAxis class is used for manipulating chart's axis.
31 \brief The QBarCategoriesAxis class is used for manipulating chart's axis.
32 \mainclass
32 \mainclass
33
33
34 BarCategoriesAxis can be setup to show axis line with tick marks, grid lines and shades.
34 BarCategoriesAxis can be setup to show axis line with tick marks, grid lines and shades.
35 Categories are drawn between ticks. Note that you can use this also with lineseries too.
35 Categories are drawn between ticks. Note that you can use this also with lineseries too.
36 See the \l {Line and BarChart Example} {Line and BarChart Example} to learn how to do that.
36 See the \l {Line and BarChart Example} {Line and BarChart Example} to learn how to do that.
37 */
37 */
38
38
39 /*!
39 /*!
40 \qmlclass BarCategoriesAxis QBarCategoriesAxis
40 \qmlclass BarCategoriesAxis QBarCategoriesAxis
41 \brief The Axis element is used for manipulating chart's axes.
41 \brief The Axis element is used for manipulating chart's axes.
42
42
43 Axis can be setup to show axis line with tick marks, grid lines and shades.
43 Axis can be setup to show axis line with tick marks, grid lines and shades.
44 Categories are drawn between ticks. Note that you can use this also with lineseries too.
44 Categories are drawn between ticks. Note that you can use this also with lineseries too.
45
45
46 To access BarCategoriesAxis you can use ChartView API. For example:
46 To access BarCategoriesAxis you can use ChartView API. For example:
47 \code
47 \code
48 ChartView {
48 ChartView {
49 BarCategoriesAxis {
49 BarCategoriesAxis {
50 id: categoryAxis
50 id: categoryAxis
51 categories: ["Jan", "Feb", "Mar", "Apr", "May", "Jun" ]
51 categories: ["Jan", "Feb", "Mar", "Apr", "May", "Jun" ]
52 }
52 }
53 // Add a few series...
53 // Add a few series...
54 }
54 }
55 \endcode
55 \endcode
56 */
56 */
57
57
58 /*!
58 /*!
59 \property QBarCategoriesAxis::categories
59 \property QBarCategoriesAxis::categories
60 Defines the categories of axis
60 Defines the categories of axis
61 */
61 */
62 /*!
62 /*!
63 \qmlproperty QStringList BarCategoriesAxis::categories
63 \qmlproperty QStringList BarCategoriesAxis::categories
64 Defines the categories of axis
64 Defines the categories of axis
65 */
65 */
66
66
67 /*!
67 /*!
68 \property QBarCategoriesAxis::min
68 \property QBarCategoriesAxis::min
69 Defines the minimum value on the axis.
69 Defines the minimum value on the axis.
70 */
70 */
71 /*!
71 /*!
72 \qmlproperty real BarCategoriesAxis::min
72 \qmlproperty real BarCategoriesAxis::min
73 Defines the minimum value on the axis.
73 Defines the minimum value on the axis.
74 */
74 */
75
75
76 /*!
76 /*!
77 \property QBarCategoriesAxis::max
77 \property QBarCategoriesAxis::max
78 Defines the maximum value on the axis.
78 Defines the maximum value on the axis.
79 */
79 */
80 /*!
80 /*!
81 \qmlproperty real BarCategoriesAxis::max
81 \qmlproperty real BarCategoriesAxis::max
82 Defines the maximum value on the axis.
82 Defines the maximum value on the axis.
83 */
83 */
84
84
85 /*!
85 /*!
86 \fn void QBarCategoriesAxis::minChanged(const QString &min)
86 \fn void QBarCategoriesAxis::minChanged(const QString &min)
87 Axis emits signal when \a min of axis has changed.
87 Axis emits signal when \a min of axis has changed.
88 */
88 */
89
89
90 /*!
90 /*!
91 \fn void QBarCategoriesAxis::maxChanged(const QString &max)
91 \fn void QBarCategoriesAxis::maxChanged(const QString &max)
92 Axis emits signal when \a max of axis has changed.
92 Axis emits signal when \a max of axis has changed.
93 */
93 */
94
94
95 /*!
95 /*!
96 \fn void QBarCategoriesAxis::rangeChanged(const QString &min, const QString &max)
96 \fn void QBarCategoriesAxis::rangeChanged(const QString &min, const QString &max)
97 Axis emits signal when \a min or \a max of axis has changed.
97 Axis emits signal when \a min or \a max of axis has changed.
98 */
98 */
99
99
100 /*!
100 /*!
101 Constructs an axis object which is a child of \a parent.
101 Constructs an axis object which is a child of \a parent.
102 */
102 */
103 QBarCategoriesAxis::QBarCategoriesAxis(QObject *parent):
103 QBarCategoriesAxis::QBarCategoriesAxis(QObject *parent):
104 QAbstractAxis(*new QBarCategoriesAxisPrivate(this),parent)
104 QAbstractAxis(*new QBarCategoriesAxisPrivate(this),parent)
105 {
105 {
106 }
106 }
107
107
108 /*!
108 /*!
109 Destroys the object
109 Destroys the object
110 */
110 */
111 QBarCategoriesAxis::~QBarCategoriesAxis()
111 QBarCategoriesAxis::~QBarCategoriesAxis()
112 {
112 {
113 }
113 }
114
114
115 /*!
115 /*!
116 \internal
116 \internal
117 */
117 */
118 QBarCategoriesAxis::QBarCategoriesAxis(QBarCategoriesAxisPrivate &d,QObject *parent):QAbstractAxis(d,parent)
118 QBarCategoriesAxis::QBarCategoriesAxis(QBarCategoriesAxisPrivate &d,QObject *parent):QAbstractAxis(d,parent)
119 {
119 {
120
120
121 }
121 }
122
122
123 /*!
123 /*!
124 Appends \a categories to axis
124 Appends \a categories to axis
125 */
125 */
126 void QBarCategoriesAxis::append(const QStringList &categories)
126 void QBarCategoriesAxis::append(const QStringList &categories)
127 {
127 {
128 if(categories.isEmpty()) return;
128 if(categories.isEmpty()) return;
129
129
130 Q_D(QBarCategoriesAxis);
130 Q_D(QBarCategoriesAxis);
131 if (d->m_categories.isEmpty()) {
131 if (d->m_categories.isEmpty()) {
132 d->m_categories.append(categories);
132 d->m_categories.append(categories);
133 setRange(categories.first(),categories.last());
133 setRange(categories.first(),categories.last());
134 }else{
134 }else{
135 d->m_categories.append(categories);
135 d->m_categories.append(categories);
136 }
136 }
137 emit d->updated();
137 emit d->updated();
138 emit categoriesChanged();
138 emit categoriesChanged();
139 }
139 }
140
140
141 /*!
141 /*!
142 Appends \a category to axis
142 Appends \a category to axis
143 */
143 */
144 void QBarCategoriesAxis::append(const QString &category)
144 void QBarCategoriesAxis::append(const QString &category)
145 {
145 {
146 Q_D(QBarCategoriesAxis);
146 Q_D(QBarCategoriesAxis);
147 if (d->m_categories.isEmpty()) {
147 if (d->m_categories.isEmpty()) {
148 d->m_categories.append(category);
148 d->m_categories.append(category);
149 setRange(category,category);
149 setRange(category,category);
150 }else{
150 }else{
151 d->m_categories.append(category);
151 d->m_categories.append(category);
152 }
152 }
153 emit d->updated();
153 emit d->updated();
154 emit categoriesChanged();
154 emit categoriesChanged();
155 }
155 }
156
156
157 /*!
157 /*!
158 Removes \a category from axis
158 Removes \a category from axis
159 */
159 */
160 void QBarCategoriesAxis::remove(const QString &category)
160 void QBarCategoriesAxis::remove(const QString &category)
161 {
161 {
162 Q_D(QBarCategoriesAxis);
162 Q_D(QBarCategoriesAxis);
163 if (d->m_categories.contains(category)) {
163 if (d->m_categories.contains(category)) {
164 d->m_categories.removeAt(d->m_categories.indexOf(category));
164 d->m_categories.removeAt(d->m_categories.indexOf(category));
165 setRange(d->m_categories.first(),d->m_categories.last());
165 setRange(d->m_categories.first(),d->m_categories.last());
166 emit d->updated();
166 emit d->updated();
167 emit categoriesChanged();
167 emit categoriesChanged();
168 }
168 }
169 }
169 }
170
170
171 /*!
171 /*!
172 Inserts \a category to axis at \a index
172 Inserts \a category to axis at \a index
173 */
173 */
174 void QBarCategoriesAxis::insert(int index, const QString &category)
174 void QBarCategoriesAxis::insert(int index, const QString &category)
175 {
175 {
176 Q_D(QBarCategoriesAxis);
176 Q_D(QBarCategoriesAxis);
177 if (d->m_categories.isEmpty()) {
177 if (d->m_categories.isEmpty()) {
178 d->m_categories.insert(index,category);
178 d->m_categories.insert(index,category);
179 setRange(category,category);
179 setRange(category,category);
180 }else{
180 }else{
181 d->m_categories.insert(index,category);
181 d->m_categories.insert(index,category);
182 }
182 }
183 emit d->updated();
183 emit d->updated();
184 emit categoriesChanged();
184 emit categoriesChanged();
185 }
185 }
186
186
187 /*!
187 /*!
188 Removes all categories.
188 Removes all categories.
189 */
189 */
190 void QBarCategoriesAxis::clear()
190 void QBarCategoriesAxis::clear()
191 {
191 {
192 Q_D(QBarCategoriesAxis);
192 Q_D(QBarCategoriesAxis);
193 d->m_categories.clear();
193 d->m_categories.clear();
194 setRange(QString::null,QString::null);
194 setRange(QString::null,QString::null);
195 emit d->updated();
195 emit d->updated();
196 emit categoriesChanged();
196 emit categoriesChanged();
197 }
197 }
198
198
199 void QBarCategoriesAxis::setCategories(const QStringList &categories)
199 void QBarCategoriesAxis::setCategories(const QStringList &categories)
200 {
200 {
201 Q_D(QBarCategoriesAxis);
201 Q_D(QBarCategoriesAxis);
202 if(d->m_categories!=categories){
202 if(d->m_categories!=categories){
203 d->m_categories = categories;
203 d->m_categories = categories;
204 setRange(categories.first(),categories.last());
204 setRange(categories.first(),categories.last());
205 emit d->updated();
205 emit d->updated();
206 emit categoriesChanged();
206 emit categoriesChanged();
207 }
207 }
208 }
208 }
209
209
210 QStringList QBarCategoriesAxis::categories()
210 QStringList QBarCategoriesAxis::categories()
211 {
211 {
212 Q_D(QBarCategoriesAxis);
212 Q_D(QBarCategoriesAxis);
213 return d->m_categories;
213 return d->m_categories;
214 }
214 }
215
215
216 /*!
216 /*!
217 Returns number of categories.
217 Returns number of categories.
218 */
218 */
219 int QBarCategoriesAxis::count() const
219 int QBarCategoriesAxis::count() const
220 {
220 {
221 Q_D(const QBarCategoriesAxis);
221 Q_D(const QBarCategoriesAxis);
222 return d->m_categories.count();
222 return d->m_categories.count();
223 }
223 }
224
224
225 /*!
225 /*!
226 Returns category at \a index. Index must be valid.
226 Returns category at \a index. Index must be valid.
227 */
227 */
228 QString QBarCategoriesAxis::at(int index) const
228 QString QBarCategoriesAxis::at(int index) const
229 {
229 {
230 Q_D(const QBarCategoriesAxis);
230 Q_D(const QBarCategoriesAxis);
231 return d->m_categories.at(index);
231 return d->m_categories.at(index);
232 }
232 }
233
233
234 /*!
234 /*!
235 Sets minimum category to \a min.
235 Sets minimum category to \a min.
236 */
236 */
237 void QBarCategoriesAxis::setMin(const QString& min)
237 void QBarCategoriesAxis::setMin(const QString& min)
238 {
238 {
239 Q_D(QBarCategoriesAxis);
239 Q_D(QBarCategoriesAxis);
240 setRange(min,d->m_maxCategory);
240 setRange(min,d->m_maxCategory);
241 }
241 }
242
242
243 /*!
243 /*!
244 Returns minimum category.
244 Returns minimum category.
245 */
245 */
246 QString QBarCategoriesAxis::min() const
246 QString QBarCategoriesAxis::min() const
247 {
247 {
248 Q_D(const QBarCategoriesAxis);
248 Q_D(const QBarCategoriesAxis);
249 return d->m_minCategory;
249 return d->m_minCategory;
250 }
250 }
251
251
252 /*!
252 /*!
253 Sets maximum category to \a max.
253 Sets maximum category to \a max.
254 */
254 */
255 void QBarCategoriesAxis::setMax(const QString& max)
255 void QBarCategoriesAxis::setMax(const QString& max)
256 {
256 {
257 Q_D(QBarCategoriesAxis);
257 Q_D(QBarCategoriesAxis);
258 setRange(d->m_minCategory,max);
258 setRange(d->m_minCategory,max);
259 }
259 }
260
260
261 /*!
261 /*!
262 Returns maximum category
262 Returns maximum category
263 */
263 */
264 QString QBarCategoriesAxis::max() const
264 QString QBarCategoriesAxis::max() const
265 {
265 {
266 Q_D(const QBarCategoriesAxis);
266 Q_D(const QBarCategoriesAxis);
267 return d->m_maxCategory;
267 return d->m_maxCategory;
268 }
268 }
269
269
270 /*!
270 /*!
271 Sets range from \a minCategory to \a maxCategory
271 Sets range from \a minCategory to \a maxCategory
272 */
272 */
273 void QBarCategoriesAxis::setRange(const QString& minCategory, const QString& maxCategory)
273 void QBarCategoriesAxis::setRange(const QString& minCategory, const QString& maxCategory)
274 {
274 {
275 Q_D(QBarCategoriesAxis);
275 Q_D(QBarCategoriesAxis);
276
276
277 int minIndex = d->m_categories.indexOf(minCategory);
277 int minIndex = d->m_categories.indexOf(minCategory);
278 if (minIndex == -1) {
278 if (minIndex == -1) {
279 return;
279 return;
280 }
280 }
281 int maxIndex = d->m_categories.indexOf(maxCategory);
281 int maxIndex = d->m_categories.indexOf(maxCategory);
282 if (maxIndex == -1) {
282 if (maxIndex == -1) {
283 return;
283 return;
284 }
284 }
285
285
286 if (maxIndex <= minIndex) {
286 if (maxIndex <= minIndex) {
287 // max must be greater than min
287 // max must be greater than min
288 return;
288 return;
289 }
289 }
290
290
291 bool changed = false;
291 bool changed = false;
292 if (!qFuzzyIsNull(d->m_min - (minIndex))||d->m_minCategory!=minCategory) {
292 if (!qFuzzyIsNull(d->m_min - (minIndex))||d->m_minCategory!=minCategory) {
293 d->m_minCategory = minCategory;
293 d->m_minCategory = minCategory;
294 d->m_min = minIndex;
294 d->m_min = minIndex;
295 emit minChanged(minCategory);
295 emit minChanged(minCategory);
296 changed = true;
296 changed = true;
297 }
297 }
298
298
299 if (!qFuzzyIsNull(d->m_max - (maxIndex))||d->m_maxCategory!=maxCategory ) {
299 if (!qFuzzyIsNull(d->m_max - (maxIndex))||d->m_maxCategory!=maxCategory ) {
300 d->m_max = maxIndex;
300 d->m_max = maxIndex;
301 d->m_maxCategory = maxCategory;
301 d->m_maxCategory = maxCategory;
302 emit maxChanged(maxCategory);
302 emit maxChanged(maxCategory);
303 changed = true;
303 changed = true;
304 }
304 }
305
305
306 if (changed) {
306 if (changed) {
307 d->emitRange();
307 d->emitRange();
308 }
308 }
309 }
309 }
310
310
311 /*!
311 /*!
312 Returns the type of the axis
312 Returns the type of the axis
313 */
313 */
314 QAbstractAxis::AxisType QBarCategoriesAxis::type() const
314 QAbstractAxis::AxisType QBarCategoriesAxis::type() const
315 {
315 {
316 return AxisTypeCategories;
316 return AxisTypeCategories;
317 }
317 }
318
318
319 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
319 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
320
320
321 QBarCategoriesAxisPrivate::QBarCategoriesAxisPrivate(QBarCategoriesAxis* q):
321 QBarCategoriesAxisPrivate::QBarCategoriesAxisPrivate(QBarCategoriesAxis* q):
322 QAbstractAxisPrivate(q)
322 QAbstractAxisPrivate(q)
323 {
323 {
324
324
325 }
325 }
326
326
327 QBarCategoriesAxisPrivate::~QBarCategoriesAxisPrivate()
327 QBarCategoriesAxisPrivate::~QBarCategoriesAxisPrivate()
328 {
328 {
329
329
330 }
330 }
331
331
332 void QBarCategoriesAxisPrivate::setMin(const QVariant &min)
332 void QBarCategoriesAxisPrivate::setMin(const QVariant &min)
333 {
333 {
334 setRange(min,m_maxCategory);
334 setRange(min,m_maxCategory);
335 }
335 }
336
336
337 void QBarCategoriesAxisPrivate::setMax(const QVariant &max)
337 void QBarCategoriesAxisPrivate::setMax(const QVariant &max)
338 {
338 {
339 setRange(m_minCategory,max);
339 setRange(m_minCategory,max);
340 }
340 }
341
341
342 void QBarCategoriesAxisPrivate::setRange(const QVariant &min, const QVariant &max)
342 void QBarCategoriesAxisPrivate::setRange(const QVariant &min, const QVariant &max)
343 {
343 {
344 Q_Q(QBarCategoriesAxis);
344 Q_Q(QBarCategoriesAxis);
345 QString value1 = min.toString();
345 QString value1 = min.toString();
346 QString value2 = max.toString();
346 QString value2 = max.toString();
347 q->setRange(value1,value2);
347 q->setRange(value1,value2);
348 }
348 }
349
349
350 int QBarCategoriesAxisPrivate::ticksCount() const
350 int QBarCategoriesAxisPrivate::ticksCount() const
351 {
351 {
352 return m_categories.count()+1;
352 return m_categories.count()+1;
353 }
353 }
354
354
355 void QBarCategoriesAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count)
355 void QBarCategoriesAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count)
356 {
356 {
357 m_min = min;
357 m_min = min;
358 m_max = max;
358 m_max = max;
359 m_ticksCount = count;
359 m_ticksCount = count;
360 }
360 }
361
361
362 ChartAxis* QBarCategoriesAxisPrivate::createGraphics(ChartPresenter* presenter)
362 ChartAxis* QBarCategoriesAxisPrivate::createGraphics(ChartPresenter* presenter)
363 {
363 {
364 Q_Q( QBarCategoriesAxis);
364 Q_Q( QBarCategoriesAxis);
365 if(m_orientation == Qt::Vertical){
365 if(m_orientation == Qt::Vertical){
366 return new ChartCategoriesAxisY(q,presenter);
366 return new ChartCategoriesAxisY(q,presenter);
367 }else{
367 }else{
368 return new ChartCategoriesAxisX(q,presenter);
368 return new ChartCategoriesAxisX(q,presenter);
369 }
369 }
370 }
370 }
371
371
372 void QBarCategoriesAxisPrivate::emitRange()
372 void QBarCategoriesAxisPrivate::emitRange()
373 {
373 {
374 Q_Q( QBarCategoriesAxis);
374 emit changed(m_min -0.5, m_max +0.5, qCeil(m_max + 0.5) -qCeil(m_min - 0.5) +1, false);
375 if(!q->signalsBlocked()) {
376 emit changed(m_min -0.5, m_max +0.5, qCeil(m_max + 0.5) -qCeil(m_min - 0.5) +1, false);
377 }
378 }
375 }
379
376
380
377
381 #include "moc_qbarcategoriesaxis.cpp"
378 #include "moc_qbarcategoriesaxis.cpp"
382 #include "moc_qbarcategoriesaxis_p.cpp"
379 #include "moc_qbarcategoriesaxis_p.cpp"
383
380
384 QTCOMMERCIALCHART_END_NAMESPACE
381 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,304 +1,303
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 "qvaluesaxis.h"
21 #include "qvaluesaxis.h"
22 #include "qvaluesaxis_p.h"
22 #include "qvaluesaxis_p.h"
23 #include "chartvaluesaxisx_p.h"
23 #include "chartvaluesaxisx_p.h"
24 #include "chartvaluesaxisy_p.h"
24 #include "chartvaluesaxisy_p.h"
25 #include <QDebug>
25 #include <QDebug>
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 /*!
28 /*!
29 \class QValuesAxis
29 \class QValuesAxis
30 \brief The QValuesAxis class is used for manipulating chart's axis.
30 \brief The QValuesAxis class is used for manipulating chart's axis.
31 \mainclass
31 \mainclass
32
32
33 ValuesAxis can be setup to show axis line with tick marks, grid lines and shades.
33 ValuesAxis can be setup to show axis line with tick marks, grid lines and shades.
34 Values of axis are drawn to position of ticks
34 Values of axis are drawn to position of ticks
35 */
35 */
36
36
37 /*!
37 /*!
38 \qmlclass ValuesAxis QValuesAxis
38 \qmlclass ValuesAxis QValuesAxis
39 \brief The ValuesAxis element is used for manipulating chart's axes
39 \brief The ValuesAxis element is used for manipulating chart's axes
40
40
41 ValueAxis can be setup to show axis line with tick marks, grid lines and shades.
41 ValueAxis can be setup to show axis line with tick marks, grid lines and shades.
42 Values of axis are drawn to position of ticks
42 Values of axis are drawn to position of ticks
43
43
44 To access Axes you can use ChartView API. For example:
44 To access Axes you can use ChartView API. For example:
45 \code
45 \code
46 ChartView {
46 ChartView {
47 ValuesAxis {
47 ValuesAxis {
48 id: xAxis
48 id: xAxis
49 min: 0
49 min: 0
50 max: 10
50 max: 10
51 }
51 }
52 // Add a few series...
52 // Add a few series...
53 }
53 }
54 \endcode
54 \endcode
55 */
55 */
56
56
57 /*!
57 /*!
58 \property QValuesAxis::min
58 \property QValuesAxis::min
59 Defines the minimum value on the axis.
59 Defines the minimum value on the axis.
60 */
60 */
61 /*!
61 /*!
62 \qmlproperty real ValuesAxis::min
62 \qmlproperty real ValuesAxis::min
63 Defines the minimum value on the axis.
63 Defines the minimum value on the axis.
64 */
64 */
65
65
66 /*!
66 /*!
67 \property QValuesAxis::max
67 \property QValuesAxis::max
68 Defines the maximum value on the axis.
68 Defines the maximum value on the axis.
69 */
69 */
70 /*!
70 /*!
71 \qmlproperty real ValuesAxis::max
71 \qmlproperty real ValuesAxis::max
72 Defines the maximum value on the axis.
72 Defines the maximum value on the axis.
73 */
73 */
74
74
75 /*!
75 /*!
76 \fn void QValuesAxis::minChanged(qreal min)
76 \fn void QValuesAxis::minChanged(qreal min)
77 Axis emits signal when \a min of axis has changed.
77 Axis emits signal when \a min of axis has changed.
78 */
78 */
79
79
80 /*!
80 /*!
81 \fn void QValuesAxis::maxChanged(qreal max)
81 \fn void QValuesAxis::maxChanged(qreal max)
82 Axis emits signal when \a max of axis has changed.
82 Axis emits signal when \a max of axis has changed.
83 */
83 */
84
84
85 /*!
85 /*!
86 \fn void QValuesAxis::rangeChanged(qreal min, qreal max)
86 \fn void QValuesAxis::rangeChanged(qreal min, qreal max)
87 Axis emits signal when \a min or \a max of axis has changed.
87 Axis emits signal when \a min or \a max of axis has changed.
88 */
88 */
89
89
90 /*!
90 /*!
91 \property QValuesAxis::ticksCount
91 \property QValuesAxis::ticksCount
92 The number of tick marks for the axis.
92 The number of tick marks for the axis.
93 */
93 */
94
94
95 /*!
95 /*!
96 \qmlproperty int ValuesAxis::ticksCount
96 \qmlproperty int ValuesAxis::ticksCount
97 The number of tick marks for the axis.
97 The number of tick marks for the axis.
98 */
98 */
99
99
100 /*!
100 /*!
101 \property QValuesAxis::niceNumbersEnabled
101 \property QValuesAxis::niceNumbersEnabled
102 Whether the nice numbers algorithm is enabled or not for the axis.
102 Whether the nice numbers algorithm is enabled or not for the axis.
103 */
103 */
104
104
105 /*!
105 /*!
106 \qmlproperty bool ValuesAxis::niceNumbersEnabled
106 \qmlproperty bool ValuesAxis::niceNumbersEnabled
107 Whether the nice numbers algorithm is enabled or not for the axis.
107 Whether the nice numbers algorithm is enabled or not for the axis.
108 */
108 */
109
109
110 /*!
110 /*!
111 Constructs an axis object which is a child of \a parent.
111 Constructs an axis object which is a child of \a parent.
112 */
112 */
113 QValuesAxis::QValuesAxis(QObject *parent) :
113 QValuesAxis::QValuesAxis(QObject *parent) :
114 QAbstractAxis(*new QValuesAxisPrivate(this),parent)
114 QAbstractAxis(*new QValuesAxisPrivate(this),parent)
115 {
115 {
116
116
117 }
117 }
118
118
119 /*!
119 /*!
120 \internal
120 \internal
121 */
121 */
122 QValuesAxis::QValuesAxis(QValuesAxisPrivate &d,QObject *parent) : QAbstractAxis(d,parent)
122 QValuesAxis::QValuesAxis(QValuesAxisPrivate &d,QObject *parent) : QAbstractAxis(d,parent)
123 {
123 {
124
124
125 }
125 }
126
126
127 /*!
127 /*!
128 Destroys the object
128 Destroys the object
129 */
129 */
130 QValuesAxis::~QValuesAxis()
130 QValuesAxis::~QValuesAxis()
131 {
131 {
132
132
133 }
133 }
134
134
135 void QValuesAxis::setMin(qreal min)
135 void QValuesAxis::setMin(qreal min)
136 {
136 {
137 Q_D(QValuesAxis);
137 Q_D(QValuesAxis);
138 setRange(min,d->m_max);
138 setRange(min,d->m_max);
139 }
139 }
140
140
141 qreal QValuesAxis::min() const
141 qreal QValuesAxis::min() const
142 {
142 {
143 Q_D(const QValuesAxis);
143 Q_D(const QValuesAxis);
144 return d->m_min;
144 return d->m_min;
145 }
145 }
146
146
147 void QValuesAxis::setMax(qreal max)
147 void QValuesAxis::setMax(qreal max)
148 {
148 {
149 Q_D(QValuesAxis);
149 Q_D(QValuesAxis);
150 setRange(d->m_min,max);
150 setRange(d->m_min,max);
151 }
151 }
152
152
153 qreal QValuesAxis::max() const
153 qreal QValuesAxis::max() const
154 {
154 {
155 Q_D(const QValuesAxis);
155 Q_D(const QValuesAxis);
156 return d->m_max;
156 return d->m_max;
157 }
157 }
158
158
159 /*!
159 /*!
160 Sets range from \a min to \a max on the axis.
160 Sets range from \a min to \a max on the axis.
161 */
161 */
162 void QValuesAxis::setRange(qreal min, qreal max)
162 void QValuesAxis::setRange(qreal min, qreal max)
163 {
163 {
164 Q_D(QValuesAxis);
164 Q_D(QValuesAxis);
165
165 bool changed = false;
166 bool changed = false;
166 if (!qFuzzyIsNull(d->m_min - min)) {
167 if (!qFuzzyIsNull(d->m_min - min)) {
167 d->m_min = min;
168 d->m_min = min;
168 changed = true;
169 changed = true;
169 emit minChanged(min);
170 emit minChanged(min);
170 }
171 }
171
172
172 if (!qFuzzyIsNull(d->m_max - max)) {
173 if (!qFuzzyIsNull(d->m_max - max)) {
173 d->m_max = max;
174 d->m_max = max;
174 changed = true;
175 changed = true;
175 emit maxChanged(max);
176 emit maxChanged(max);
176 }
177 }
177
178
178 if (changed) {
179 if (changed) {
179 d->emitRange();
180 d->emitRange();
180 emit rangeChanged(d->m_min,d->m_max);
181 emit rangeChanged(d->m_min,d->m_max);
181 }
182 }
182 }
183 }
183
184
184 /*!
185 /*!
185 Sets \a count for ticks on the axis.
186 Sets \a count for ticks on the axis.
186 */
187 */
187 void QValuesAxis::setTicksCount(int count)
188 void QValuesAxis::setTicksCount(int count)
188 {
189 {
189 Q_D(QValuesAxis);
190 Q_D(QValuesAxis);
190 if (d->m_ticksCount != count) {
191 if (d->m_ticksCount != count) {
191 d->m_ticksCount = count;
192 d->m_ticksCount = count;
192 emit d->changed(d->m_min, d->m_max, d->m_ticksCount, d->m_niceNumbers);
193 emit d->changed(d->m_min, d->m_max, d->m_ticksCount, d->m_niceNumbers);
193 }
194 }
194 }
195 }
195
196
196 /*!
197 /*!
197 \fn int QValuesAxis::ticksCount() const
198 \fn int QValuesAxis::ticksCount() const
198 Return number of ticks on the axis
199 Return number of ticks on the axis
199 */
200 */
200 int QValuesAxis::ticksCount() const
201 int QValuesAxis::ticksCount() const
201 {
202 {
202 Q_D(const QValuesAxis);
203 Q_D(const QValuesAxis);
203 return d->m_ticksCount;
204 return d->m_ticksCount;
204 }
205 }
205
206
206 void QValuesAxis::setNiceNumbersEnabled(bool enable)
207 void QValuesAxis::setNiceNumbersEnabled(bool enable)
207 {
208 {
208 Q_D(QValuesAxis);
209 Q_D(QValuesAxis);
209 if (d->m_niceNumbers != enable){
210 if (d->m_niceNumbers != enable){
210 d->m_niceNumbers = enable;
211 d->m_niceNumbers = enable;
211 emit d->changed(d->m_min, d->m_max, d->m_ticksCount, d->m_niceNumbers);
212 emit d->changed(d->m_min, d->m_max, d->m_ticksCount, d->m_niceNumbers);
212 }
213 }
213 }
214 }
214
215
215 bool QValuesAxis::niceNumbersEnabled() const
216 bool QValuesAxis::niceNumbersEnabled() const
216 {
217 {
217 Q_D(const QValuesAxis);
218 Q_D(const QValuesAxis);
218 return d->m_niceNumbers;
219 return d->m_niceNumbers;
219 }
220 }
220
221
221 /*!
222 /*!
222 Returns the type of the axis
223 Returns the type of the axis
223 */
224 */
224 QAbstractAxis::AxisType QValuesAxis::type() const
225 QAbstractAxis::AxisType QValuesAxis::type() const
225 {
226 {
226 return AxisTypeValues;
227 return AxisTypeValues;
227 }
228 }
228
229
229 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
230 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
230
231
231 QValuesAxisPrivate::QValuesAxisPrivate(QValuesAxis* q):
232 QValuesAxisPrivate::QValuesAxisPrivate(QValuesAxis* q):
232 QAbstractAxisPrivate(q),
233 QAbstractAxisPrivate(q),
233 m_niceNumbers(false)
234 m_niceNumbers(false)
234 {
235 {
235
236
236 }
237 }
237
238
238 QValuesAxisPrivate::~QValuesAxisPrivate()
239 QValuesAxisPrivate::~QValuesAxisPrivate()
239 {
240 {
240
241
241 }
242 }
242
243
243 void QValuesAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count)
244 void QValuesAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count)
244 {
245 {
245 Q_Q(QValuesAxis);
246 Q_Q(QValuesAxis);
246 q->setRange(min,max);
247 q->setRange(min,max);
247 q->setTicksCount(count);
248 q->setTicksCount(count);
248 }
249 }
249
250
250
251
251 void QValuesAxisPrivate::setMin(const QVariant &min)
252 void QValuesAxisPrivate::setMin(const QVariant &min)
252 {
253 {
253 Q_Q(QValuesAxis);
254 Q_Q(QValuesAxis);
254 bool ok;
255 bool ok;
255 qreal value = min.toReal(&ok);
256 qreal value = min.toReal(&ok);
256 if(ok) q->setMin(value);
257 if(ok) q->setMin(value);
257 }
258 }
258
259
259 void QValuesAxisPrivate::setMax(const QVariant &max)
260 void QValuesAxisPrivate::setMax(const QVariant &max)
260 {
261 {
262
261 Q_Q(QValuesAxis);
263 Q_Q(QValuesAxis);
262 bool ok;
264 bool ok;
263 qreal value = max.toReal(&ok);
265 qreal value = max.toReal(&ok);
264 if(ok) q->setMax(value);
266 if(ok) q->setMax(value);
265 }
267 }
266
268
267 void QValuesAxisPrivate::setRange(const QVariant &min, const QVariant &max)
269 void QValuesAxisPrivate::setRange(const QVariant &min, const QVariant &max)
268 {
270 {
269 Q_Q(QValuesAxis);
271 Q_Q(QValuesAxis);
270 bool ok1;
272 bool ok1;
271 bool ok2;
273 bool ok2;
272 qreal value1 = min.toReal(&ok1);
274 qreal value1 = min.toReal(&ok1);
273 qreal value2 = max.toReal(&ok2);
275 qreal value2 = max.toReal(&ok2);
274 if(ok1&&ok2) q->setRange(value1,value2);
276 if(ok1&&ok2) q->setRange(value1,value2);
275 }
277 }
276
278
277 int QValuesAxisPrivate::ticksCount() const
279 int QValuesAxisPrivate::ticksCount() const
278 {
280 {
279 return m_ticksCount;
281 return m_ticksCount;
280 }
282 }
281
283
282 ChartAxis* QValuesAxisPrivate::createGraphics(ChartPresenter* presenter)
284 ChartAxis* QValuesAxisPrivate::createGraphics(ChartPresenter* presenter)
283 {
285 {
284 Q_Q(QValuesAxis);
286 Q_Q(QValuesAxis);
285 if(m_orientation == Qt::Vertical){
287 if(m_orientation == Qt::Vertical){
286 return new ChartValuesAxisY(q,presenter);
288 return new ChartValuesAxisY(q,presenter);
287 }else{
289 }else{
288 return new ChartValuesAxisX(q,presenter);
290 return new ChartValuesAxisX(q,presenter);
289 }
291 }
290
292
291 }
293 }
292
294
293 void QValuesAxisPrivate::emitRange()
295 void QValuesAxisPrivate::emitRange()
294 {
296 {
295 Q_Q(QValuesAxis);
297 emit changed(m_min, m_max, m_ticksCount, m_niceNumbers);
296 if(!q->signalsBlocked()) {
297 emit changed(m_min, m_max, m_ticksCount, m_niceNumbers);
298 }
299 }
298 }
300
299
301 #include "moc_qvaluesaxis.cpp"
300 #include "moc_qvaluesaxis.cpp"
302 #include "moc_qvaluesaxis_p.cpp"
301 #include "moc_qvaluesaxis_p.cpp"
303
302
304 QTCOMMERCIALCHART_END_NAMESPACE
303 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,464 +1,465
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 "chartdataset_p.h"
21 #include "chartdataset_p.h"
22 #include "qchart.h"
22 #include "qchart.h"
23 #include "qvaluesaxis.h"
23 #include "qvaluesaxis.h"
24 #include "qbarcategoriesaxis.h"
24 #include "qbarcategoriesaxis.h"
25 #include "qvaluesaxis_p.h"
25 #include "qvaluesaxis_p.h"
26 #include "qabstractseries_p.h"
26 #include "qabstractseries_p.h"
27 #include "qabstractbarseries.h"
27 #include "qabstractbarseries.h"
28 #include "qstackedbarseries.h"
28 #include "qstackedbarseries.h"
29 #include "qpercentbarseries.h"
29 #include "qpercentbarseries.h"
30 #include "qpieseries.h"
30 #include "qpieseries.h"
31
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 ChartDataSet::ChartDataSet(QChart *parent):QObject(parent),
34 ChartDataSet::ChartDataSet(QChart *parent):QObject(parent),
35 m_domainIndex(0)
35 m_domainIndex(0)
36 {
36 {
37
37
38 }
38 }
39
39
40 ChartDataSet::~ChartDataSet()
40 ChartDataSet::~ChartDataSet()
41 {
41 {
42 removeAllSeries();
42 removeAllSeries();
43 }
43 }
44
44
45 void ChartDataSet::addSeries(QAbstractSeries* series)
45 void ChartDataSet::addSeries(QAbstractSeries* series)
46 {
46 {
47 Domain* domain = m_seriesDomainMap.value(series);
47 Domain* domain = m_seriesDomainMap.value(series);
48
48
49 if(domain) {
49 if(domain) {
50 qWarning() << "Can not add series. Series already on the chart";
50 qWarning() << "Can not add series. Series already on the chart";
51 return;
51 return;
52 }
52 }
53
53
54 series->setParent(this); // take ownership
54 series->setParent(this); // take ownership
55
55
56 domain = new Domain(series);
56 domain = new Domain(series);
57
57
58 m_seriesDomainMap.insert(series,domain);
58 m_seriesDomainMap.insert(series,domain);
59
59
60 series->d_ptr->scaleDomain(*domain);
60 series->d_ptr->scaleDomain(*domain);
61
61
62 QMapIterator<int, QAbstractSeries*> i(m_indexSeriesMap);
62 QMapIterator<int, QAbstractSeries*> i(m_indexSeriesMap);
63
63
64 int key=0;
64 int key=0;
65 while (i.hasNext()) {
65 while (i.hasNext()) {
66 i.next();
66 i.next();
67 if(i.key()!=key) {
67 if(i.key()!=key) {
68 break;
68 break;
69 }
69 }
70 key++;
70 key++;
71 }
71 }
72
72
73 m_indexSeriesMap.insert(key,series);
73 m_indexSeriesMap.insert(key,series);
74
74
75 series->d_ptr->m_chart = qobject_cast<QChart*>(parent());
75 series->d_ptr->m_chart = qobject_cast<QChart*>(parent());
76 series->d_ptr->m_dataset = this;
76 series->d_ptr->m_dataset = this;
77
77
78 emit seriesAdded(series,domain);
78 emit seriesAdded(series,domain);
79
79
80 }
80 }
81
81
82 void ChartDataSet::createDefaultAxes()
82 void ChartDataSet::createDefaultAxes()
83 {
83 {
84
84
85 if(m_seriesDomainMap.isEmpty()) return;
85 if(m_seriesDomainMap.isEmpty()) return;
86
86
87 QAbstractAxis::AxisTypes typeX(0);
87 QAbstractAxis::AxisTypes typeX(0);
88 QAbstractAxis::AxisTypes typeY(0);
88 QAbstractAxis::AxisTypes typeY(0);
89
89
90 QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap);
90 QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap);
91 while (i.hasNext()) {
91 while (i.hasNext()) {
92 i.next();
92 i.next();
93 removeAxes(i.key());
93 removeAxes(i.key());
94 }
94 }
95
95
96 i.toFront();
96 i.toFront();
97
97
98 while (i.hasNext()) {
98 while (i.hasNext()) {
99 i.next();
99 i.next();
100 QAbstractAxis* axisX = m_seriesAxisXMap.value(i.key());
100 QAbstractAxis* axisX = m_seriesAxisXMap.value(i.key());
101 QAbstractAxis* axisY = m_seriesAxisYMap.value(i.key());
101 QAbstractAxis* axisY = m_seriesAxisYMap.value(i.key());
102 if(axisX) typeX&=axisX->type();
102 if(axisX) typeX&=axisX->type();
103 else typeX|=i.key()->d_ptr->defaultAxisXType();
103 else typeX|=i.key()->d_ptr->defaultAxisXType();
104 if(axisY) typeY&=axisY->type();
104 if(axisY) typeY&=axisY->type();
105 else typeY|=i.key()->d_ptr->defaultAxisYType();
105 else typeY|=i.key()->d_ptr->defaultAxisYType();
106 }
106 }
107
107
108
108
109 if(typeX.testFlag(QAbstractAxis::AxisTypeValues) && typeX.testFlag(QAbstractAxis::AxisTypeCategories))
109 if(typeX.testFlag(QAbstractAxis::AxisTypeValues) && typeX.testFlag(QAbstractAxis::AxisTypeCategories))
110 {
110 {
111 i.toFront();
111 i.toFront();
112 while (i.hasNext()) {
112 while (i.hasNext()) {
113 i.next();
113 i.next();
114 QAbstractAxis* axis = createAxis(i.key()->d_ptr->defaultAxisXType());
114 QAbstractAxis* axis = createAxis(i.key()->d_ptr->defaultAxisXType());
115 if(!axis) continue;
115 if(!axis) continue;
116 i.key()->d_ptr->initializeAxisX(axis);
116 i.key()->d_ptr->initializeAxisX(axis);
117 addAxisX(axis,i.key());
117 addAxisX(axis,i.key());
118 emit axisAdded(axis,i.value());
118 emit axisAdded(axis,i.value());
119 }
119 }
120
120
121 }else if(!typeY.testFlag(QAbstractAxis::AxisTypeNoAxis)){
121 }else if(!typeY.testFlag(QAbstractAxis::AxisTypeNoAxis)){
122 QAbstractAxis* axis = createAxis(QAbstractAxis::AxisType(int(typeX)));
122 QAbstractAxis* axis = createAxis(QAbstractAxis::AxisType(int(typeX)));
123 i.toFront();
123 i.toFront();
124 while (i.hasNext()) {
124 while (i.hasNext()) {
125 i.next();
125 i.next();
126 i.key()->d_ptr->initializeAxisX(axis);
126 i.key()->d_ptr->initializeAxisX(axis);
127 addAxisX(axis,i.key());
127 addAxisX(axis,i.key());
128 }
128 }
129 emit axisAdded(axis,i.value());
129 emit axisAdded(axis,i.value());
130
130
131 }
131 }
132
132
133 if(typeY.testFlag(QAbstractAxis::AxisTypeValues) && typeY.testFlag(QAbstractAxis::AxisTypeCategories))
133 if(typeY.testFlag(QAbstractAxis::AxisTypeValues) && typeY.testFlag(QAbstractAxis::AxisTypeCategories))
134 {
134 {
135 i.toFront();
135 i.toFront();
136 while (i.hasNext()) {
136 while (i.hasNext()) {
137 i.next();
137 i.next();
138 QAbstractAxis* axis = createAxis(i.key()->d_ptr->defaultAxisYType());
138 QAbstractAxis* axis = createAxis(i.key()->d_ptr->defaultAxisYType());
139 i.key()->d_ptr->initializeAxisY(axis);
139 i.key()->d_ptr->initializeAxisY(axis);
140 addAxisY(axis,i.key());
140 addAxisY(axis,i.key());
141 emit axisAdded(axis,i.value());
141 emit axisAdded(axis,i.value());
142 }
142 }
143
143
144 }else if(!typeY.testFlag(QAbstractAxis::AxisTypeNoAxis)){
144 }else if(!typeY.testFlag(QAbstractAxis::AxisTypeNoAxis)){
145 QAbstractAxis* axis = createAxis(QAbstractAxis::AxisType(int(typeY)));
145 QAbstractAxis* axis = createAxis(QAbstractAxis::AxisType(int(typeY)));
146 i.toFront();
146 i.toFront();
147 while (i.hasNext()) {
147 while (i.hasNext()) {
148 i.next();
148 i.next();
149 i.key()->d_ptr->initializeAxisY(axis);
149 i.key()->d_ptr->initializeAxisY(axis);
150 addAxisY(axis,i.key());
150 addAxisY(axis,i.key());
151 }
151 }
152 emit axisAdded(axis,i.value());
152 emit axisAdded(axis,i.value());
153
153
154 }
154 }
155 }
155 }
156
156
157
157
158 QAbstractAxis* ChartDataSet::createAxis(QAbstractAxis::AxisType type)
158 QAbstractAxis* ChartDataSet::createAxis(QAbstractAxis::AxisType type)
159 {
159 {
160 QAbstractAxis* axis =0;
160 QAbstractAxis* axis =0;
161
161
162 switch(type) {
162 switch(type) {
163 case QAbstractAxis::AxisTypeValues:
163 case QAbstractAxis::AxisTypeValues:
164 axis = new QValuesAxis(this);
164 axis = new QValuesAxis(this);
165 break;
165 break;
166 case QAbstractAxis::AxisTypeCategories:
166 case QAbstractAxis::AxisTypeCategories:
167 axis = new QBarCategoriesAxis(this);
167 axis = new QBarCategoriesAxis(this);
168 break;
168 break;
169 default:
169 default:
170 axis = 0;
170 axis = 0;
171 break;
171 break;
172 }
172 }
173
173
174 return axis;
174 return axis;
175 }
175 }
176
176
177 void ChartDataSet::addAxisX(QAbstractAxis* axis,QAbstractSeries* series) {
177 void ChartDataSet::addAxisX(QAbstractAxis* axis,QAbstractSeries* series) {
178 Domain* domain = m_seriesDomainMap.value(series);
178 Domain* domain = m_seriesDomainMap.value(series);
179 QObject::connect(axis->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int,bool)));
179 QObject::connect(axis->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int,bool)));
180 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),axis->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int)));
180 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),axis->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int)));
181 axis->d_ptr->m_orientation=Qt::Horizontal;
181 axis->d_ptr->m_orientation=Qt::Horizontal;
182 m_seriesAxisXMap.insert(series,axis);
182 m_seriesAxisXMap.insert(series,axis);
183 }
183 }
184
184
185 void ChartDataSet::addAxisY(QAbstractAxis* axis,QAbstractSeries* series) {
185 void ChartDataSet::addAxisY(QAbstractAxis* axis,QAbstractSeries* series) {
186 Domain* domain = m_seriesDomainMap.value(series);
186 Domain* domain = m_seriesDomainMap.value(series);
187 QObject::connect(axis->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool)));
187 QObject::connect(axis->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool)));
188 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),axis->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int)));
188 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),axis->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int)));
189 axis->d_ptr->m_orientation=Qt::Vertical;
189 axis->d_ptr->m_orientation=Qt::Vertical;
190 m_seriesAxisYMap.insert(series,axis);
190 m_seriesAxisYMap.insert(series,axis);
191 }
191 }
192
192
193 void ChartDataSet::removeSeries(QAbstractSeries* series)
193 void ChartDataSet::removeSeries(QAbstractSeries* series)
194 {
194 {
195 Domain* domain = m_seriesDomainMap.take(series);
195 Domain* domain = m_seriesDomainMap.take(series);
196
196
197 if(!domain) {
197 if(!domain) {
198 qWarning()<<"Can not remove series. Series not found on the chart.";
198 qWarning()<<"Can not remove series. Series not found on the chart.";
199 }
199 }
200
200
201 emit seriesRemoved(series);
201 emit seriesRemoved(series);
202
202
203 delete domain;
203 delete domain;
204 domain = 0;
204 domain = 0;
205
205
206 int key = seriesIndex(series);
206 int key = seriesIndex(series);
207 Q_ASSERT(key!=-1);
207 Q_ASSERT(key!=-1);
208
208
209 m_indexSeriesMap.remove(key);
209 m_indexSeriesMap.remove(key);
210
210
211 series->setParent(0);
211 series->setParent(0);
212 series->d_ptr->m_chart = 0;
212 series->d_ptr->m_chart = 0;
213 series->d_ptr->m_dataset = 0;
213 series->d_ptr->m_dataset = 0;
214
214
215 removeAxes(series);
215 removeAxes(series);
216 }
216 }
217
217
218 void ChartDataSet::removeAxes(QAbstractSeries* series)
218 void ChartDataSet::removeAxes(QAbstractSeries* series)
219 {
219 {
220 QAbstractAxis* axisX = m_seriesAxisXMap.take(series);
220 QAbstractAxis* axisX = m_seriesAxisXMap.take(series);
221
221
222 if(axisX) {
222 if(axisX) {
223 QList<QAbstractAxis*> axesX = m_seriesAxisXMap.values();
223 QList<QAbstractAxis*> axesX = m_seriesAxisXMap.values();
224 int x = axesX.indexOf(axisX);
224 int x = axesX.indexOf(axisX);
225
225
226 if(x==-1) {
226 if(x==-1) {
227 emit axisRemoved(axisX);
227 emit axisRemoved(axisX);
228 axisX->deleteLater();
228 axisX->deleteLater();
229 }
229 }
230 }
230 }
231
231
232 QAbstractAxis* axisY = m_seriesAxisYMap.take(series);
232 QAbstractAxis* axisY = m_seriesAxisYMap.take(series);
233
233
234 if(axisY) {
234 if(axisY) {
235 QList<QAbstractAxis*> axesY = m_seriesAxisYMap.values();
235 QList<QAbstractAxis*> axesY = m_seriesAxisYMap.values();
236
236
237 int y = axesY.indexOf(axisY);
237 int y = axesY.indexOf(axisY);
238
238
239 if(y==-1) {
239 if(y==-1) {
240 emit axisRemoved(axisY);
240 emit axisRemoved(axisY);
241 axisY->deleteLater();
241 axisY->deleteLater();
242 }
242 }
243 }
243 }
244 }
244 }
245
245
246 void ChartDataSet::removeAllSeries()
246 void ChartDataSet::removeAllSeries()
247 {
247 {
248 QList<QAbstractSeries*> series = m_seriesDomainMap.keys();
248 QList<QAbstractSeries*> series = m_seriesDomainMap.keys();
249 foreach(QAbstractSeries *s , series) {
249 foreach(QAbstractSeries *s , series) {
250 removeSeries(s);
250 removeSeries(s);
251 }
251 }
252
252
253 Q_ASSERT(m_seriesAxisXMap.count()==0);
253 Q_ASSERT(m_seriesAxisXMap.count()==0);
254 Q_ASSERT(m_seriesAxisXMap.count()==0);
254 Q_ASSERT(m_seriesAxisXMap.count()==0);
255 Q_ASSERT(m_seriesDomainMap.count()==0);
255 Q_ASSERT(m_seriesDomainMap.count()==0);
256
256
257 qDeleteAll(series);
257 qDeleteAll(series);
258 }
258 }
259
259
260 void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size)
260 void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size)
261 {
261 {
262 //for performance reasons block, signals and scale "full" domain one by one. Gives twice less screen updates
262 //for performance reasons block, signals and scale "full" domain one by one. Gives twice less screen updates
263
263
264
264 blockAxisSignals(true);
265 blockAxisSignals(true);
265
266
266 QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap);
267 QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap);
267
268
268 while (i.hasNext()) {
269 while (i.hasNext()) {
269 i.next();
270 i.next();
270 i.value()->zoomIn(rect,size);
271 i.value()->zoomIn(rect,size);
271 }
272 }
272
273
273 blockAxisSignals(false);
274 blockAxisSignals(false);
274
275
275 }
276 }
276
277
277 void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size)
278 void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size)
278 {
279 {
279 //for performance reasons block, signals and scale "full" domain one by one. Gives twice less screen updates
280 //for performance reasons block, signals and scale "full" domain one by one. Gives twice less screen updates
280
281
281 blockAxisSignals(true);
282 blockAxisSignals(true);
282
283
283 QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap);
284 QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap);
284
285
285 while (i.hasNext()) {
286 while (i.hasNext()) {
286 i.next();
287 i.next();
287 i.value()->zoomOut(rect,size);
288 i.value()->zoomOut(rect,size);
288 }
289 }
289
290
290 blockAxisSignals(false);
291 blockAxisSignals(false);
291 }
292 }
292
293
293 void ChartDataSet::blockAxisSignals(bool enabled)
294 void ChartDataSet::blockAxisSignals(bool enabled)
294 {
295 {
295 QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap);
296 QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap);
296
297 Q_UNUSED(enabled);
297 while (i.hasNext()) {
298 while (i.hasNext()) {
298 i.next();
299 i.next();
299 QAbstractAxis* axisX = m_seriesAxisXMap.value(i.key());
300 QAbstractAxis* axisX = m_seriesAxisXMap.value(i.key());
300 QAbstractAxis* axisY = m_seriesAxisYMap.value(i.key());
301 QAbstractAxis* axisY = m_seriesAxisYMap.value(i.key());
301 if(axisX) axisX->blockSignals(enabled);
302 if(axisX) axisX->d_ptr->blockSignals(true);
302 if(axisY) axisY->blockSignals(enabled);
303 if(axisY) axisY->d_ptr->blockSignals(true);
303 }
304 }
304 }
305 }
305
306
306 int ChartDataSet::seriesCount(QAbstractSeries::SeriesType type)
307 int ChartDataSet::seriesCount(QAbstractSeries::SeriesType type)
307 {
308 {
308 int count=0;
309 int count=0;
309 QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap);
310 QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap);
310 while (i.hasNext()) {
311 while (i.hasNext()) {
311 i.next();
312 i.next();
312 if(i.key()->type()==type) count++;
313 if(i.key()->type()==type) count++;
313 }
314 }
314 return count;
315 return count;
315 }
316 }
316
317
317 int ChartDataSet::seriesIndex(QAbstractSeries *series)
318 int ChartDataSet::seriesIndex(QAbstractSeries *series)
318 {
319 {
319 QMapIterator<int, QAbstractSeries*> i(m_indexSeriesMap);
320 QMapIterator<int, QAbstractSeries*> i(m_indexSeriesMap);
320 while (i.hasNext()) {
321 while (i.hasNext()) {
321 i.next();
322 i.next();
322 if (i.value() == series)
323 if (i.value() == series)
323 return i.key();
324 return i.key();
324 }
325 }
325 return -1;
326 return -1;
326 }
327 }
327
328
328 QAbstractAxis* ChartDataSet::axisX(QAbstractSeries *series) const
329 QAbstractAxis* ChartDataSet::axisX(QAbstractSeries *series) const
329 {
330 {
330 if(series == 0) {
331 if(series == 0) {
331
332
332 QMapIterator<QAbstractSeries*, QAbstractAxis *> i(m_seriesAxisXMap);
333 QMapIterator<QAbstractSeries*, QAbstractAxis *> i(m_seriesAxisXMap);
333
334
334 while (i.hasNext()) {
335 while (i.hasNext()) {
335 i.next();
336 i.next();
336 if(i.value()->isVisible()) return i.value();
337 if(i.value()->isVisible()) return i.value();
337 }
338 }
338 return 0;
339 return 0;
339 }
340 }
340 return m_seriesAxisXMap.value(series);
341 return m_seriesAxisXMap.value(series);
341 }
342 }
342
343
343 QAbstractAxis* ChartDataSet::axisY(QAbstractSeries *series) const
344 QAbstractAxis* ChartDataSet::axisY(QAbstractSeries *series) const
344 {
345 {
345 if(series == 0) {
346 if(series == 0) {
346 QMapIterator<QAbstractSeries*, QAbstractAxis *> i(m_seriesAxisYMap);
347 QMapIterator<QAbstractSeries*, QAbstractAxis *> i(m_seriesAxisYMap);
347
348
348 while (i.hasNext()) {
349 while (i.hasNext()) {
349 i.next();
350 i.next();
350 if(i.value()->isVisible()) return i.value();
351 if(i.value()->isVisible()) return i.value();
351 }
352 }
352 return 0;
353 return 0;
353 }
354 }
354 return m_seriesAxisYMap.value(series);
355 return m_seriesAxisYMap.value(series);
355 }
356 }
356
357
357 void ChartDataSet::setAxisX(QAbstractSeries *series, QAbstractAxis *axis)
358 void ChartDataSet::setAxisX(QAbstractSeries *series, QAbstractAxis *axis)
358 {
359 {
359 Q_ASSERT(axis);
360 Q_ASSERT(axis);
360 Domain* domain = m_seriesDomainMap.value(series);
361 Domain* domain = m_seriesDomainMap.value(series);
361
362
362 if(!domain) {
363 if(!domain) {
363 qWarning() << "Series not found on the chart.";
364 qWarning() << "Series not found on the chart.";
364 return;
365 return;
365 }
366 }
366
367
367 if(axis->d_ptr->m_orientation==Qt::Vertical) {
368 if(axis->d_ptr->m_orientation==Qt::Vertical) {
368 qWarning()<<"Axis already defined as axis Y";
369 qWarning()<<"Axis already defined as axis Y";
369 return;
370 return;
370 }
371 }
371
372
372 QAbstractAxis *oldAxis = m_seriesAxisXMap.take(series);
373 QAbstractAxis *oldAxis = m_seriesAxisXMap.take(series);
373 QList<QAbstractAxis*> axesX = m_seriesAxisXMap.values();
374 QList<QAbstractAxis*> axesX = m_seriesAxisXMap.values();
374
375
375 if(oldAxis) {
376 if(oldAxis) {
376
377
377 int x = axesX.indexOf(oldAxis);
378 int x = axesX.indexOf(oldAxis);
378 if(x==-1) {
379 if(x==-1) {
379 emit axisRemoved(oldAxis);
380 emit axisRemoved(oldAxis);
380 oldAxis->deleteLater();
381 oldAxis->deleteLater();
381 }
382 }
382 }
383 }
383
384
384 QObject::connect(axis->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int,bool)));
385 QObject::connect(axis->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int,bool)));
385 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),axis->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int)));
386 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),axis->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int)));
386
387
387 int x = axesX.indexOf(axis);
388 int x = axesX.indexOf(axis);
388 if(x==-1) {
389 if(x==-1) {
389 axis->d_ptr->m_orientation=Qt::Horizontal;
390 axis->d_ptr->m_orientation=Qt::Horizontal;
390 emit axisAdded(axis,domain);
391 emit axisAdded(axis,domain);
391 }
392 }
392
393
393 m_seriesAxisXMap.insert(series,axis);
394 m_seriesAxisXMap.insert(series,axis);
394 axis->d_ptr->emitRange();
395 axis->d_ptr->emitRange();
395 }
396 }
396
397
397 void ChartDataSet::setAxisY(QAbstractSeries *series, QAbstractAxis *axis)
398 void ChartDataSet::setAxisY(QAbstractSeries *series, QAbstractAxis *axis)
398 {
399 {
399 Q_ASSERT(axis);
400 Q_ASSERT(axis);
400 Domain* domain = m_seriesDomainMap.value(series);
401 Domain* domain = m_seriesDomainMap.value(series);
401
402
402 if(!domain) {
403 if(!domain) {
403 qWarning() << "Series not found on the chart.";
404 qWarning() << "Series not found on the chart.";
404 return;
405 return;
405 }
406 }
406
407
407 if(axis->d_ptr->m_orientation==Qt::Horizontal) {
408 if(axis->d_ptr->m_orientation==Qt::Horizontal) {
408 qWarning()<<"Axis already defined as axis X";
409 qWarning()<<"Axis already defined as axis X";
409 return;
410 return;
410 }
411 }
411
412
412 QAbstractAxis *oldAxis = m_seriesAxisYMap.take(series);
413 QAbstractAxis *oldAxis = m_seriesAxisYMap.take(series);
413 QList<QAbstractAxis*> axesY = m_seriesAxisYMap.values();
414 QList<QAbstractAxis*> axesY = m_seriesAxisYMap.values();
414
415
415 if(oldAxis) {
416 if(oldAxis) {
416 int y = axesY.indexOf(oldAxis);
417 int y = axesY.indexOf(oldAxis);
417 if(y==-1) {
418 if(y==-1) {
418 emit axisRemoved(oldAxis);
419 emit axisRemoved(oldAxis);
419 oldAxis->deleteLater();
420 oldAxis->deleteLater();
420 }
421 }
421 }
422 }
422
423
423 QObject::connect(axis->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool)));
424 QObject::connect(axis->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool)));
424 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),axis->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int)));
425 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),axis->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int)));
425
426
426 int y = axesY.indexOf(axis);
427 int y = axesY.indexOf(axis);
427 if(y==-1) {
428 if(y==-1) {
428 axis->d_ptr->m_orientation=Qt::Vertical;
429 axis->d_ptr->m_orientation=Qt::Vertical;
429 emit axisAdded(axis,domain);
430 emit axisAdded(axis,domain);
430 }
431 }
431
432
432 m_seriesAxisYMap.insert(series,axis);
433 m_seriesAxisYMap.insert(series,axis);
433 axis->d_ptr->emitRange();
434 axis->d_ptr->emitRange();
434 }
435 }
435
436
436 Domain* ChartDataSet::domain(QAbstractSeries *series) const
437 Domain* ChartDataSet::domain(QAbstractSeries *series) const
437 {
438 {
438 return m_seriesDomainMap.value(series);
439 return m_seriesDomainMap.value(series);
439 }
440 }
440
441
441 void ChartDataSet::scrollDomain(qreal dx,qreal dy,const QSizeF& size)
442 void ChartDataSet::scrollDomain(qreal dx,qreal dy,const QSizeF& size)
442 {
443 {
443 blockAxisSignals(true);
444 blockAxisSignals(true);
444 QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap);
445 QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap);
445 while (i.hasNext()) {
446 while (i.hasNext()) {
446 i.next();
447 i.next();
447 i.value()->move(dx,dy,size);
448 i.value()->move(dx,dy,size);
448 }
449 }
449 blockAxisSignals(false);
450 blockAxisSignals(false);
450 }
451 }
451
452
452 QList<QAbstractSeries*> ChartDataSet::series() const
453 QList<QAbstractSeries*> ChartDataSet::series() const
453 {
454 {
454 return m_seriesAxisXMap.keys();
455 return m_seriesAxisXMap.keys();
455 }
456 }
456
457
457 void ChartDataSet::updateSeries(QAbstractSeries *series)
458 void ChartDataSet::updateSeries(QAbstractSeries *series)
458 {
459 {
459 emit seriesUpdated(series);
460 emit seriesUpdated(series);
460 }
461 }
461
462
462 #include "moc_chartdataset_p.cpp"
463 #include "moc_chartdataset_p.cpp"
463
464
464 QTCOMMERCIALCHART_END_NAMESPACE
465 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,178 +1,183
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 <QtTest/QtTest>
21 #include <QtTest/QtTest>
22 #include <qchartview.h>
22 #include <qchartview.h>
23 #include <qlineseries.h>
23 #include <qlineseries.h>
24 #include <cmath>
24 #include <cmath>
25 #include <tst_definitions.h>
25 #include <tst_definitions.h>
26 #include <qvaluesaxis.h>
26
27
27 QTCOMMERCIALCHART_USE_NAMESPACE
28 QTCOMMERCIALCHART_USE_NAMESPACE
28
29
29
30
30 Q_DECLARE_METATYPE(QChart*)
31 Q_DECLARE_METATYPE(QChart*)
31 Q_DECLARE_METATYPE(QChartView::RubberBands)
32 Q_DECLARE_METATYPE(QChartView::RubberBands)
32 Q_DECLARE_METATYPE(Qt::Key)
33 Q_DECLARE_METATYPE(Qt::Key)
33
34
34 class tst_QChartView : public QObject
35 class tst_QChartView : public QObject
35 {
36 {
36 Q_OBJECT
37 Q_OBJECT
37
38
38 public Q_SLOTS:
39 public Q_SLOTS:
39 void initTestCase();
40 void initTestCase();
40 void cleanupTestCase();
41 void cleanupTestCase();
41 void init();
42 void init();
42 void cleanup();
43 void cleanup();
43
44
44 private Q_SLOTS:
45 private Q_SLOTS:
45 void qchartview_data();
46 void qchartview_data();
46 void qchartview();
47 void qchartview();
47 void chart_data();
48 void chart_data();
48 void chart();
49 void chart();
49 void rubberBand_data();
50 void rubberBand_data();
50 void rubberBand();
51 void rubberBand();
51
52
52 private:
53 private:
53 QChartView* m_view;
54 QChartView* m_view;
54 };
55 };
55
56
56 void tst_QChartView::initTestCase()
57 void tst_QChartView::initTestCase()
57 {
58 {
58 //test tracks mouse, give a while to user to relese it
59 //test tracks mouse, give a while to user to relese it
59 QTest::qWait(1000);
60 QTest::qWait(1000);
60 }
61 }
61
62
62 void tst_QChartView::cleanupTestCase()
63 void tst_QChartView::cleanupTestCase()
63 {
64 {
64 }
65 }
65
66
66 void tst_QChartView::init()
67 void tst_QChartView::init()
67 {
68 {
68 m_view = new QChartView(new QChart());
69 m_view = new QChartView(new QChart());
69 m_view->chart()->legend()->setVisible(false);
70 m_view->chart()->legend()->setVisible(false);
70 }
71 }
71
72
72 void tst_QChartView::cleanup()
73 void tst_QChartView::cleanup()
73 {
74 {
74 delete m_view;
75 delete m_view;
75 m_view =0;
76 m_view =0;
76 }
77 }
77
78
78 void tst_QChartView::qchartview_data()
79 void tst_QChartView::qchartview_data()
79 {
80 {
80
81
81 }
82 }
82
83
83 void tst_QChartView::qchartview()
84 void tst_QChartView::qchartview()
84 {
85 {
85 QVERIFY(m_view->chart());
86 QVERIFY(m_view->chart());
86 QCOMPARE(m_view->rubberBand(), QChartView::NoRubberBand);
87 QCOMPARE(m_view->rubberBand(), QChartView::NoRubberBand);
87 m_view->show();
88 m_view->show();
88 QTest::qWaitForWindowShown(m_view);
89 QTest::qWaitForWindowShown(m_view);
89
90
90 delete(new QChartView());
91 delete(new QChartView());
91
92
92 QChartView view;
93 QChartView view;
93 QVERIFY(view.chart());
94 QVERIFY(view.chart());
94
95
95 }
96 }
96
97
97 void tst_QChartView::chart_data()
98 void tst_QChartView::chart_data()
98 {
99 {
99
100
100 QTest::addColumn<QChart*>("chart");
101 QTest::addColumn<QChart*>("chart");
101 QTest::newRow("qchart") << new QChart();
102 QTest::newRow("qchart") << new QChart();
102 }
103 }
103
104
104 void tst_QChartView::chart()
105 void tst_QChartView::chart()
105 {
106 {
106 QFETCH(QChart*, chart);
107 QFETCH(QChart*, chart);
107 QChartView* view = new QChartView(chart);
108 QChartView* view = new QChartView(chart);
108 QCOMPARE(view->chart(), chart);
109 QCOMPARE(view->chart(), chart);
109 delete view;
110 delete view;
110 }
111 }
111
112
112 void tst_QChartView::rubberBand_data()
113 void tst_QChartView::rubberBand_data()
113 {
114 {
114 QTest::addColumn<QChartView::RubberBands>("rubberBand");
115 QTest::addColumn<QChartView::RubberBands>("rubberBand");
115 QTest::addColumn<int>("Xcount");
116 QTest::addColumn<int>("Xcount");
116 QTest::addColumn<int>("Ycount");
117 QTest::addColumn<int>("Ycount");
117
118
118 QTest::addColumn<int>("minX");
119 QTest::addColumn<int>("minX");
119 QTest::addColumn<int>("maxX");
120 QTest::addColumn<int>("maxX");
120 QTest::addColumn<int>("minY");
121 QTest::addColumn<int>("minY");
121 QTest::addColumn<int>("maxY");
122 QTest::addColumn<int>("maxY");
122
123
123 QTest::newRow("HorizonalRubberBand") << QChartView::RubberBands(QChartView::HorizonalRubberBand) << 0 << 1 << 20 << 180 << 0<< 200;
124 QTest::newRow("HorizonalRubberBand") << QChartView::RubberBands(QChartView::HorizonalRubberBand) << 0 << 1 << 20 << 180 << 0<< 200;
124 QTest::newRow("VerticalRubberBand") << QChartView::RubberBands(QChartView::VerticalRubberBand) << 1 << 0 << 0 << 200 << 20<< 180;
125 QTest::newRow("VerticalRubberBand") << QChartView::RubberBands(QChartView::VerticalRubberBand) << 1 << 0 << 0 << 200 << 20<< 180;
125 QTest::newRow("RectangleRubberBand") << QChartView::RubberBands(QChartView::RectangleRubberBand) << 1 << 1 <<20 << 180 << 20<< 180;
126 QTest::newRow("RectangleRubberBand") << QChartView::RubberBands(QChartView::RectangleRubberBand) << 1 << 1 <<20 << 180 << 20<< 180;
126 }
127 }
127
128
128 void tst_QChartView::rubberBand()
129 void tst_QChartView::rubberBand()
129 {
130 {
130 QFETCH(QChartView::RubberBands, rubberBand);
131 QFETCH(QChartView::RubberBands, rubberBand);
131 QFETCH(int, Xcount);
132 QFETCH(int, Xcount);
132 QFETCH(int, Ycount);
133 QFETCH(int, Ycount);
133 QFETCH(int, minX);
134 QFETCH(int, minX);
134 QFETCH(int, maxX);
135 QFETCH(int, maxX);
135 QFETCH(int, minY);
136 QFETCH(int, minY);
136 QFETCH(int, maxY);
137 QFETCH(int, maxY);
137
138
138 m_view->setRubberBand(rubberBand);
139 m_view->setRubberBand(rubberBand);
139 QRectF padding = m_view->chart()->margins();
140 QRectF padding = m_view->chart()->margins();
140 QCOMPARE(m_view->rubberBand(), rubberBand);
141 QCOMPARE(m_view->rubberBand(), rubberBand);
141
142
142 QLineSeries* line = new QLineSeries();
143 QLineSeries* line = new QLineSeries();
143 *line << QPointF(0, 0) << QPointF(200, 200);
144 *line << QPointF(0, 0) << QPointF(200, 200);
144
145
145 m_view->chart()->addSeries(line);
146 m_view->chart()->addSeries(line);
147 m_view->chart()->createDefaultAxes();
146 m_view->resize(200 + padding.left() + padding.right(), 200 + padding.top()+ padding.bottom());
148 m_view->resize(200 + padding.left() + padding.right(), 200 + padding.top()+ padding.bottom());
147 m_view->show();
149 m_view->show();
148
150
149 //this is hack since view does not get events otherwise
151 //this is hack since view does not get events otherwise
150 m_view->setMouseTracking(true);
152 m_view->setMouseTracking(true);
151
153
152 QAbstractAxis* axisY = m_view->chart()->axisY();
154 QAbstractAxis* axisY = m_view->chart()->axisY();
153 QSignalSpy spy0(axisY, SIGNAL(rangeChanged(qreal,qreal)));
155 QSignalSpy spy0(axisY, SIGNAL(rangeChanged(qreal,qreal)));
154 QAbstractAxis* axisX = m_view->chart()->axisX();
156 QAbstractAxis* axisX = m_view->chart()->axisX();
155 QSignalSpy spy1(axisX, SIGNAL(rangeChanged(qreal,qreal)));
157 QSignalSpy spy1(axisX, SIGNAL(rangeChanged(qreal,qreal)));
156
158
157 QTest::qWaitForWindowShown(m_view);
159 QTest::qWaitForWindowShown(m_view);
158 QTest::mouseMove(m_view->viewport(), QPoint(minX, minY) + padding.topLeft().toPoint());
160 QTest::mouseMove(m_view->viewport(), QPoint(minX, minY) + padding.topLeft().toPoint());
159 QTest::mousePress(m_view->viewport(), Qt::LeftButton, 0, QPoint(minX, minY) + padding.topLeft().toPoint());
161 QTest::mousePress(m_view->viewport(), Qt::LeftButton, 0, QPoint(minX, minY) + padding.topLeft().toPoint());
160 QTest::mouseMove(m_view->viewport(), QPoint(maxX, maxY) + padding.topLeft().toPoint());
162 QTest::mouseMove(m_view->viewport(), QPoint(maxX, maxY) + padding.topLeft().toPoint());
161 QTest::mouseRelease(m_view->viewport(), Qt::LeftButton, 0, QPoint(maxX, maxY)+ padding.topLeft().toPoint());
163 QTest::mouseRelease(m_view->viewport(), Qt::LeftButton, 0, QPoint(maxX, maxY)+ padding.topLeft().toPoint());
162
164
163 TRY_COMPARE(spy0.count(), Xcount);
165 TRY_COMPARE(spy0.count(), Xcount);
164 TRY_COMPARE(spy1.count(), Ycount);
166 TRY_COMPARE(spy1.count(), Ycount);
165
167
166 //this is hack since view does not get events otherwise
168 //this is hack since view does not get events otherwise
167 m_view->setMouseTracking(false);
169 m_view->setMouseTracking(false);
168
170
169 //TODO: QVERIFY(axisX->min() - minX < 1);
171 QValuesAxis* vaxisX = qobject_cast<QValuesAxis*>(axisX);
170 //TODO: QVERIFY(axisX->max() - maxX < 1);
172 QValuesAxis* vaxisY = qobject_cast<QValuesAxis*>(axisY);
171 //TODO: QVERIFY(axisY->min() - minY < 1);
173
172 //TODO: QVERIFY(axisY->max() - maxY < 1);
174 QVERIFY(vaxisX->min() - minX < 1);
173 qFatal("implement TODO");
175 QVERIFY(vaxisX->max() - maxX < 1);
176 QVERIFY(vaxisY->min() - minY < 1);
177 QVERIFY(vaxisY->max() - maxY < 1);
178
174 }
179 }
175
180
176 QTEST_MAIN(tst_QChartView)
181 QTEST_MAIN(tst_QChartView)
177 #include "tst_qchartview.moc"
182 #include "tst_qchartview.moc"
178
183
General Comments 0
You need to be logged in to leave comments. Login now