##// END OF EJS Templates
Fixed QML ChartView background color property
Tero Ahola -
r1473:a0b240042977
parent child
Show More
@@ -1,355 +1,378
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 "declarativechart.h"
21 #include "declarativechart.h"
22 #include <QPainter>
22 #include <QPainter>
23 #include "declarativelineseries.h"
23 #include "declarativelineseries.h"
24 #include "declarativeareaseries.h"
24 #include "declarativeareaseries.h"
25 #include "declarativebarseries.h"
25 #include "declarativebarseries.h"
26 #include "declarativepieseries.h"
26 #include "declarativepieseries.h"
27 #include "declarativesplineseries.h"
27 #include "declarativesplineseries.h"
28 #include "declarativescatterseries.h"
28 #include "declarativescatterseries.h"
29
29
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
31
32 /*!
32 /*!
33 \qmlclass ChartView DeclarativeChart
33 \qmlclass ChartView DeclarativeChart
34
34
35 ChartView element is the parent that is responsible for showing different chart series types.
35 ChartView element is the parent that is responsible for showing different chart series types.
36
36
37 \section1 Example Usage
37 \section1 Example Usage
38
38
39 \beginfloatleft
39 \beginfloatleft
40 \image demos_qmlchart1.png
40 \image demos_qmlchart1.png
41 \endfloat
41 \endfloat
42 \clearfloat
42 \clearfloat
43 */
43 */
44
44
45 /*!
45 /*!
46 \qmlproperty Theme ChartView::theme
46 \qmlproperty Theme ChartView::theme
47 Theme defines the visual appearance of the chart, including for example colors, fonts, line
47 Theme defines the visual appearance of the chart, including for example colors, fonts, line
48 widths and chart background.
48 widths and chart background.
49 */
49 */
50
50
51 /*!
51 /*!
52 \qmlproperty Animation ChartView::animation
52 \qmlproperty Animation ChartView::animation
53 Animation configuration of the chart. One of ChartView.NoAnimation, ChartView.GridAxisAnimations,
53 Animation configuration of the chart. One of ChartView.NoAnimation, ChartView.GridAxisAnimations,
54 ChartView.SeriesAnimations or ChartView.AllAnimations.
54 ChartView.SeriesAnimations or ChartView.AllAnimations.
55 */
55 */
56
56
57 /*!
57 /*!
58 \qmlproperty string ChartView::title
58 \qmlproperty string ChartView::title
59 The title of the chart, shown on top of the chart.
59 The title of the chart, shown on top of the chart.
60 \sa ChartView::titleColor
60 */
61 */
61
62
62 /*!
63 /*!
63 \qmlproperty string ChartView::titleColor
64 \qmlproperty string ChartView::titleColor
64 The color of the title text.
65 The color of the title text.
65 */
66 */
66
67
67 /*!
68 /*!
68 \qmlproperty Axis ChartView::axisX
69 \qmlproperty Axis ChartView::axisX
69 The x-axis of the chart.
70 The x-axis of the chart.
70 */
71 */
71
72
72 /*!
73 /*!
73 \qmlproperty Axis ChartView::axisY
74 \qmlproperty Axis ChartView::axisY
74 The default y-axis of the chart.
75 The default y-axis of the chart.
75 */
76 */
76
77
77 /*!
78 /*!
79 \qmlproperty Legend ChartView::legend
80 The legend of the chart. Legend lists all the series, pie slices and bar sets added on the chart.
81 */
82
83 /*!
84 \qmlproperty int ChartView::count
85 The count of series added to the chart.
86 */
87
88 /*!
89 \qmlproperty color ChartView::backgroundColor
90 The color of the chart's background. By default background color is defined by chart theme.
91 \sa ChartView::theme
92 */
93
94 /*!
95 \qmlproperty bool ChartView::dropShadowEnabled
96 The chart's border drop shadow. Set to true to enable drop shadow.
97 */
98
99 /*!
78 \qmlmethod Axis ChartView::axisY(QAbstractSeries *series)
100 \qmlmethod Axis ChartView::axisY(QAbstractSeries *series)
79 The y-axis of the series. This is the same as the default y-axis of the chart, unless you have
101 The y-axis of the series. This is the same as the default y-axis of the chart, unless you have
80 explicitly defined the series to have a non-default y-axis.
102 explicitly defined the series to have a non-default y-axis.
81 */
103 */
82
104
83 DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent)
105 DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent)
84 : QDeclarativeItem(parent),
106 : QDeclarativeItem(parent),
85 m_chart(new QChart(this))
107 m_chart(new QChart(this))
86 {
108 {
87 setFlag(QGraphicsItem::ItemHasNoContents, false);
109 setFlag(QGraphicsItem::ItemHasNoContents, false);
88 // m_chart->axisX()->setNiceNumbersEnabled(false);
110 // m_chart->axisX()->setNiceNumbersEnabled(false);
89 }
111 }
90
112
91 DeclarativeChart::~DeclarativeChart()
113 DeclarativeChart::~DeclarativeChart()
92 {
114 {
93 delete m_chart;
115 delete m_chart;
94 }
116 }
95
117
96 void DeclarativeChart::childEvent(QChildEvent *event)
118 void DeclarativeChart::childEvent(QChildEvent *event)
97 {
119 {
98 if (event->type() == QEvent::ChildAdded) {
120 if (event->type() == QEvent::ChildAdded) {
99 if (qobject_cast<QAbstractSeries *>(event->child())) {
121 if (qobject_cast<QAbstractSeries *>(event->child())) {
100 m_chart->addSeries(qobject_cast<QAbstractSeries *>(event->child()));
122 m_chart->addSeries(qobject_cast<QAbstractSeries *>(event->child()));
101 }
123 }
102 }
124 }
103 }
125 }
104
126
105 void DeclarativeChart::componentComplete()
127 void DeclarativeChart::componentComplete()
106 {
128 {
107 foreach(QObject *child, children()) {
129 foreach(QObject *child, children()) {
108 if (qobject_cast<QAbstractSeries *>(child)) {
130 if (qobject_cast<QAbstractSeries *>(child)) {
109 // qDebug() << "DeclarativeChart::componentComplete(), add: " << child;
131 // qDebug() << "DeclarativeChart::componentComplete(), add: " << child;
110 // TODO: how about optional y-axis?
132 // TODO: how about optional y-axis?
111 m_chart->addSeries(qobject_cast<QAbstractSeries *>(child));
133 m_chart->addSeries(qobject_cast<QAbstractSeries *>(child));
112 }
134 }
113 }
135 }
114 QDeclarativeItem::componentComplete();
136 QDeclarativeItem::componentComplete();
115 }
137 }
116
138
117 void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
139 void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
118 {
140 {
119 // qDebug() << "DeclarativeChart::geometryChanged" << newGeometry.width() << newGeometry.height();
141 // qDebug() << "DeclarativeChart::geometryChanged" << newGeometry.width() << newGeometry.height();
120 if (newGeometry.isValid()) {
142 if (newGeometry.isValid()) {
121 if (newGeometry.width() > 0 && newGeometry.height() > 0) {
143 if (newGeometry.width() > 0 && newGeometry.height() > 0) {
122 m_chart->resize(newGeometry.width(), newGeometry.height());
144 m_chart->resize(newGeometry.width(), newGeometry.height());
123 }
145 }
124 }
146 }
125 QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
147 QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
126 }
148 }
127
149
128 void DeclarativeChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
150 void DeclarativeChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
129 {
151 {
130 Q_UNUSED(option)
152 Q_UNUSED(option)
131 Q_UNUSED(widget)
153 Q_UNUSED(widget)
132
154
133 // TODO: optimized?
155 // TODO: optimized?
134 painter->setRenderHint(QPainter::Antialiasing, true);
156 painter->setRenderHint(QPainter::Antialiasing, true);
135 }
157 }
136
158
137 void DeclarativeChart::setTheme(DeclarativeChart::Theme theme)
159 void DeclarativeChart::setTheme(DeclarativeChart::Theme theme)
138 {
160 {
139 QChart::ChartTheme chartTheme = (QChart::ChartTheme) theme;
161 QChart::ChartTheme chartTheme = (QChart::ChartTheme) theme;
140 if (chartTheme != m_chart->theme()) {
162 if (chartTheme != m_chart->theme()) {
141 m_chart->setTheme(chartTheme);
163 m_chart->setTheme(chartTheme);
142 themeChanged();
164 themeChanged();
143 }
165 }
144 }
166 }
145
167
146 DeclarativeChart::Theme DeclarativeChart::theme()
168 DeclarativeChart::Theme DeclarativeChart::theme()
147 {
169 {
148 return (DeclarativeChart::Theme) m_chart->theme();
170 return (DeclarativeChart::Theme) m_chart->theme();
149 }
171 }
150
172
151 void DeclarativeChart::setAnimationOptions(DeclarativeChart::Animation animations)
173 void DeclarativeChart::setAnimationOptions(DeclarativeChart::Animation animations)
152 {
174 {
153 QChart::AnimationOption animationOptions = (QChart::AnimationOption) animations;
175 QChart::AnimationOption animationOptions = (QChart::AnimationOption) animations;
154 if (animationOptions != m_chart->animationOptions()) {
176 if (animationOptions != m_chart->animationOptions()) {
155 m_chart->setAnimationOptions(animationOptions);
177 m_chart->setAnimationOptions(animationOptions);
156 animationOptionsChanged();
178 animationOptionsChanged();
157 }
179 }
158 }
180 }
159
181
160 DeclarativeChart::Animation DeclarativeChart::animationOptions()
182 DeclarativeChart::Animation DeclarativeChart::animationOptions()
161 {
183 {
162 if (m_chart->animationOptions().testFlag(QChart::AllAnimations))
184 if (m_chart->animationOptions().testFlag(QChart::AllAnimations))
163 return DeclarativeChart::AllAnimations;
185 return DeclarativeChart::AllAnimations;
164 else if (m_chart->animationOptions().testFlag(QChart::GridAxisAnimations))
186 else if (m_chart->animationOptions().testFlag(QChart::GridAxisAnimations))
165 return DeclarativeChart::GridAxisAnimations;
187 return DeclarativeChart::GridAxisAnimations;
166 else if (m_chart->animationOptions().testFlag(QChart::SeriesAnimations))
188 else if (m_chart->animationOptions().testFlag(QChart::SeriesAnimations))
167 return DeclarativeChart::SeriesAnimations;
189 return DeclarativeChart::SeriesAnimations;
168 else
190 else
169 return DeclarativeChart::NoAnimation;
191 return DeclarativeChart::NoAnimation;
170 }
192 }
171
193
172 void DeclarativeChart::setTitle(QString title)
194 void DeclarativeChart::setTitle(QString title)
173 {
195 {
174 if (title != m_chart->title()) {
196 if (title != m_chart->title()) {
175 m_chart->setTitle(title);
197 m_chart->setTitle(title);
176 emit titleChanged();
198 emit titleChanged();
177 }
199 }
178 }
200 }
179 QString DeclarativeChart::title()
201 QString DeclarativeChart::title()
180 {
202 {
181 return m_chart->title();
203 return m_chart->title();
182 }
204 }
183
205
184 QAxis *DeclarativeChart::axisX()
206 QAxis *DeclarativeChart::axisX()
185 {
207 {
186 return m_chart->axisX();
208 return m_chart->axisX();
187 }
209 }
188
210
189 QAxis *DeclarativeChart::axisY(QAbstractSeries *series)
211 QAxis *DeclarativeChart::axisY(QAbstractSeries *series)
190 {
212 {
191 return m_chart->axisY(series);
213 return m_chart->axisY(series);
192 }
214 }
193
215
194 QLegend *DeclarativeChart::legend()
216 QLegend *DeclarativeChart::legend()
195 {
217 {
196 return m_chart->legend();
218 return m_chart->legend();
197 }
219 }
198
220
199 QVariantList DeclarativeChart::axisXLabels()
221 QVariantList DeclarativeChart::axisXLabels()
200 {
222 {
201 QVariantList labels;
223 QVariantList labels;
202 foreach (qreal value, m_chart->axisX()->categories()->values()) {
224 foreach (qreal value, m_chart->axisX()->categories()->values()) {
203 labels.append(value);
225 labels.append(value);
204 labels.append(m_chart->axisX()->categories()->label(value));
226 labels.append(m_chart->axisX()->categories()->label(value));
205 }
227 }
206 return labels;
228 return labels;
207 }
229 }
208
230
209 void DeclarativeChart::setAxisXLabels(QVariantList list)
231 void DeclarativeChart::setAxisXLabels(QVariantList list)
210 {
232 {
211 QVariant value(QVariant::Invalid);
233 QVariant value(QVariant::Invalid);
212 foreach (QVariant element, list) {
234 foreach (QVariant element, list) {
213 if (value.isValid() && element.type() == QVariant::String) {
235 if (value.isValid() && element.type() == QVariant::String) {
214 m_chart->axisX()->categories()->insert(value.toDouble(), element.toString());
236 m_chart->axisX()->categories()->insert(value.toDouble(), element.toString());
215 value = QVariant(QVariant::Invalid);
237 value = QVariant(QVariant::Invalid);
216 } else {
238 } else {
217 if (element.canConvert(QVariant::Double))
239 if (element.canConvert(QVariant::Double))
218 value = element;
240 value = element;
219 }
241 }
220 }
242 }
221 emit axisLabelsChanged();
243 emit axisLabelsChanged();
222 }
244 }
223
245
224 void DeclarativeChart::setTitleColor(QColor color)
246 void DeclarativeChart::setTitleColor(QColor color)
225 {
247 {
226 QBrush b = m_chart->titleBrush();
248 QBrush b = m_chart->titleBrush();
227 if (color != b.color()) {
249 if (color != b.color()) {
228 b.setColor(color);
250 b.setColor(color);
229 m_chart->setTitleBrush(b);
251 m_chart->setTitleBrush(b);
230 emit titleColorChanged();
252 emit titleColorChanged();
231 }
253 }
232 }
254 }
233
255
234 QColor DeclarativeChart::titleColor()
256 QColor DeclarativeChart::titleColor()
235 {
257 {
236 return m_chart->titleBrush().color();
258 return m_chart->titleBrush().color();
237 }
259 }
238
260
239 void DeclarativeChart::setBackgroundColor(QColor color)
261 void DeclarativeChart::setBackgroundColor(QColor color)
240 {
262 {
241 QBrush b = m_chart->backgroundBrush();
263 QBrush b = m_chart->backgroundBrush();
242 if (color != b.color()) {
264 if (b.style() != Qt::SolidPattern || color != b.color()) {
265 b.setStyle(Qt::SolidPattern);
243 b.setColor(color);
266 b.setColor(color);
244 m_chart->setBackgroundBrush(b);
267 m_chart->setBackgroundBrush(b);
245 emit backgroundColorChanged();
268 emit backgroundColorChanged();
246 }
269 }
247 }
270 }
248
271
249 QColor DeclarativeChart::backgroundColor()
272 QColor DeclarativeChart::backgroundColor()
250 {
273 {
251 return m_chart->backgroundBrush().color();
274 return m_chart->backgroundBrush().color();
252 }
275 }
253
276
254 int DeclarativeChart::count()
277 int DeclarativeChart::count()
255 {
278 {
256 return m_chart->series().count();
279 return m_chart->series().count();
257 }
280 }
258
281
259 void DeclarativeChart::setDropShadowEnabled(bool enabled)
282 void DeclarativeChart::setDropShadowEnabled(bool enabled)
260 {
283 {
261 if (enabled != m_chart->isDropShadowEnabled()) {
284 if (enabled != m_chart->isDropShadowEnabled()) {
262 m_chart->setDropShadowEnabled(enabled);
285 m_chart->setDropShadowEnabled(enabled);
263 dropShadowEnabledChanged(enabled);
286 dropShadowEnabledChanged(enabled);
264 }
287 }
265 }
288 }
266
289
267 bool DeclarativeChart::dropShadowEnabled()
290 bool DeclarativeChart::dropShadowEnabled()
268 {
291 {
269 return m_chart->isDropShadowEnabled();
292 return m_chart->isDropShadowEnabled();
270 }
293 }
271
294
272 void DeclarativeChart::zoom(qreal factor)
295 void DeclarativeChart::zoom(qreal factor)
273 {
296 {
274 m_chart->zoom(factor);
297 m_chart->zoom(factor);
275 }
298 }
276
299
277 void DeclarativeChart::scrollLeft(qreal pixels)
300 void DeclarativeChart::scrollLeft(qreal pixels)
278 {
301 {
279 m_chart->scroll(QPointF(pixels, 0));
302 m_chart->scroll(QPointF(pixels, 0));
280 }
303 }
281
304
282 void DeclarativeChart::scrollRight(qreal pixels)
305 void DeclarativeChart::scrollRight(qreal pixels)
283 {
306 {
284 m_chart->scroll(QPointF(-pixels, 0));
307 m_chart->scroll(QPointF(-pixels, 0));
285 }
308 }
286
309
287 void DeclarativeChart::scrollUp(qreal pixels)
310 void DeclarativeChart::scrollUp(qreal pixels)
288 {
311 {
289 m_chart->scroll(QPointF(0, pixels));
312 m_chart->scroll(QPointF(0, pixels));
290 }
313 }
291
314
292 void DeclarativeChart::scrollDown(qreal pixels)
315 void DeclarativeChart::scrollDown(qreal pixels)
293 {
316 {
294 m_chart->scroll(QPointF(0, -pixels));
317 m_chart->scroll(QPointF(0, -pixels));
295 }
318 }
296
319
297 QAbstractSeries *DeclarativeChart::series(int index)
320 QAbstractSeries *DeclarativeChart::series(int index)
298 {
321 {
299 if (index < m_chart->series().count()) {
322 if (index < m_chart->series().count()) {
300 return m_chart->series().at(index);
323 return m_chart->series().at(index);
301 }
324 }
302 return 0;
325 return 0;
303 }
326 }
304
327
305 QAbstractSeries *DeclarativeChart::series(QString seriesName)
328 QAbstractSeries *DeclarativeChart::series(QString seriesName)
306 {
329 {
307 foreach(QAbstractSeries *series, m_chart->series()) {
330 foreach(QAbstractSeries *series, m_chart->series()) {
308 if (series->name() == seriesName)
331 if (series->name() == seriesName)
309 return series;
332 return series;
310 }
333 }
311 return 0;
334 return 0;
312 }
335 }
313
336
314 QAbstractSeries *DeclarativeChart::createSeries(DeclarativeChart::SeriesType type, QString name)
337 QAbstractSeries *DeclarativeChart::createSeries(DeclarativeChart::SeriesType type, QString name)
315 {
338 {
316 QAbstractSeries *series = 0;
339 QAbstractSeries *series = 0;
317 switch (type) {
340 switch (type) {
318 case DeclarativeChart::SeriesTypeLine:
341 case DeclarativeChart::SeriesTypeLine:
319 series = new DeclarativeLineSeries();
342 series = new DeclarativeLineSeries();
320 break;
343 break;
321 case DeclarativeChart::SeriesTypeArea:
344 case DeclarativeChart::SeriesTypeArea:
322 series = new DeclarativeAreaSeries();
345 series = new DeclarativeAreaSeries();
323 break;
346 break;
324 case DeclarativeChart::SeriesTypeBar:
347 case DeclarativeChart::SeriesTypeBar:
325 series = new DeclarativeBarSeries();
348 series = new DeclarativeBarSeries();
326 break;
349 break;
327 case DeclarativeChart::SeriesTypeStackedBar:
350 case DeclarativeChart::SeriesTypeStackedBar:
328 // TODO
351 // TODO
329 break;
352 break;
330 case DeclarativeChart::SeriesTypePercentBar:
353 case DeclarativeChart::SeriesTypePercentBar:
331 // TODO
354 // TODO
332 break;
355 break;
333 case DeclarativeChart::SeriesTypeGroupedBar:
356 case DeclarativeChart::SeriesTypeGroupedBar:
334 series = new DeclarativeGroupedBarSeries();
357 series = new DeclarativeGroupedBarSeries();
335 break;
358 break;
336 case DeclarativeChart::SeriesTypePie:
359 case DeclarativeChart::SeriesTypePie:
337 series = new DeclarativePieSeries();
360 series = new DeclarativePieSeries();
338 break;
361 break;
339 case DeclarativeChart::SeriesTypeScatter:
362 case DeclarativeChart::SeriesTypeScatter:
340 series = new DeclarativeScatterSeries();
363 series = new DeclarativeScatterSeries();
341 break;
364 break;
342 case DeclarativeChart::SeriesTypeSpline:
365 case DeclarativeChart::SeriesTypeSpline:
343 series = new DeclarativeSplineSeries();
366 series = new DeclarativeSplineSeries();
344 break;
367 break;
345 default:
368 default:
346 qWarning() << "Illegal series type";
369 qWarning() << "Illegal series type";
347 }
370 }
348 series->setName(name);
371 series->setName(name);
349 m_chart->addSeries(series);
372 m_chart->addSeries(series);
350 return series;
373 return series;
351 }
374 }
352
375
353 #include "moc_declarativechart.cpp"
376 #include "moc_declarativechart.cpp"
354
377
355 QTCOMMERCIALCHART_END_NAMESPACE
378 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now