@@ -1,737 +1,727 | |||
|
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 "declarativechart.h" |
|
22 | 22 | #include <QPainter> |
|
23 | 23 | #include <QDeclarativeEngine> |
|
24 | 24 | #include "declarativelineseries.h" |
|
25 | 25 | #include "declarativeareaseries.h" |
|
26 | 26 | #include "declarativebarseries.h" |
|
27 | 27 | #include "declarativepieseries.h" |
|
28 | 28 | #include "declarativesplineseries.h" |
|
29 | 29 | #include "declarativescatterseries.h" |
|
30 | 30 | #include "qbarcategoryaxis.h" |
|
31 | 31 | #include "qvalueaxis.h" |
|
32 | 32 | #include "qcategoryaxis.h" |
|
33 | 33 | #include "qabstractseries_p.h" |
|
34 | 34 | #include "declarativemargins.h" |
|
35 | 35 | #include "qchart_p.h" |
|
36 | 36 | |
|
37 | 37 | #ifndef QT_ON_ARM |
|
38 | 38 | #include "qdatetimeaxis.h" |
|
39 | 39 | #endif |
|
40 | 40 | |
|
41 | 41 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
42 | 42 | |
|
43 | 43 | /*! |
|
44 | 44 | \qmlclass ChartView DeclarativeChart |
|
45 | 45 | |
|
46 | 46 | ChartView element is the parent that is responsible for showing different chart series types. |
|
47 | 47 | |
|
48 | 48 | The following QML shows how to create a simple chart with one pie series: |
|
49 | 49 | \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 1 |
|
50 | 50 | \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 2 |
|
51 | 51 | \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 3 |
|
52 | 52 | |
|
53 | 53 | \beginfloatleft |
|
54 | 54 | \image examples_qmlpiechart.png |
|
55 | 55 | \endfloat |
|
56 | 56 | \clearfloat |
|
57 | 57 | */ |
|
58 | 58 | |
|
59 | 59 | /*! |
|
60 | 60 | \qmlproperty Theme ChartView::theme |
|
61 | 61 | Theme defines the visual appearance of the chart, including for example colors, fonts, line |
|
62 | 62 | widths and chart background. |
|
63 | 63 | */ |
|
64 | 64 | |
|
65 | 65 | /*! |
|
66 | \qmlproperty Animation ChartView::animation | |
|
66 | \qmlproperty Animation ChartView::animationOptions | |
|
67 | 67 | Animation configuration of the chart. One of ChartView.NoAnimation, ChartView.GridAxisAnimations, |
|
68 | 68 | ChartView.SeriesAnimations or ChartView.AllAnimations. |
|
69 | 69 | */ |
|
70 | 70 | |
|
71 | 71 | /*! |
|
72 | 72 | \qmlproperty Font ChartView::titleFont |
|
73 | 73 | The title font of the chart |
|
74 | 74 | |
|
75 | 75 | See the \l {Font} {QML Font Element} for detailed documentation. |
|
76 | 76 | */ |
|
77 | 77 | |
|
78 | 78 | /*! |
|
79 | 79 | \qmlproperty string ChartView::title |
|
80 | 80 | The title of the chart, shown on top of the chart. |
|
81 | 81 | \sa ChartView::titleColor |
|
82 | 82 | */ |
|
83 | 83 | |
|
84 | 84 | /*! |
|
85 |
\qmlproperty |
|
|
85 | \qmlproperty color ChartView::titleColor | |
|
86 | 86 | The color of the title text. |
|
87 | 87 | */ |
|
88 | 88 | |
|
89 | 89 | /*! |
|
90 | \qmlproperty Axis ChartView::axisX | |
|
91 | The x-axis of the chart. | |
|
92 | */ | |
|
93 | ||
|
94 | /*! | |
|
95 | \qmlproperty Axis ChartView::axisY | |
|
96 | The default y-axis of the chart. | |
|
97 | */ | |
|
98 | ||
|
99 | /*! | |
|
100 | 90 | \qmlproperty Legend ChartView::legend |
|
101 | 91 | The legend of the chart. Legend lists all the series, pie slices and bar sets added on the chart. |
|
102 | 92 | */ |
|
103 | 93 | |
|
104 | 94 | /*! |
|
105 | 95 | \qmlproperty int ChartView::count |
|
106 | 96 | The count of series added to the chart. |
|
107 | 97 | */ |
|
108 | 98 | |
|
109 | 99 | /*! |
|
110 | 100 | \qmlproperty color ChartView::backgroundColor |
|
111 | 101 | The color of the chart's background. By default background color is defined by chart theme. |
|
112 | 102 | \sa ChartView::theme |
|
113 | 103 | */ |
|
114 | 104 | |
|
115 | 105 | /*! |
|
116 | 106 | \qmlproperty bool ChartView::dropShadowEnabled |
|
117 | 107 | The chart's border drop shadow. Set to true to enable drop shadow. |
|
118 | 108 | */ |
|
119 | 109 | |
|
120 | 110 | /*! |
|
121 | 111 | \qmlproperty real ChartView::topMargin |
|
122 | 112 | Deprecated. Use minimumMargins and plotArea instead. |
|
123 | 113 | */ |
|
124 | 114 | |
|
125 | 115 | /*! |
|
126 | 116 | \qmlproperty real ChartView::bottomMargin |
|
127 | 117 | Deprecated. Use minimumMargins and plotArea instead. |
|
128 | 118 | */ |
|
129 | 119 | |
|
130 | 120 | /*! |
|
131 | 121 | \qmlproperty real ChartView::leftMargin |
|
132 | 122 | Deprecated. Use minimumMargins and plotArea instead. |
|
133 | 123 | */ |
|
134 | 124 | |
|
135 | 125 | /*! |
|
136 | 126 | \qmlproperty real ChartView::rightMargin |
|
137 | 127 | Deprecated. Use minimumMargins and plotArea instead. |
|
138 | 128 | */ |
|
139 | 129 | |
|
140 | 130 | /*! |
|
141 | 131 | \qmlproperty Margins ChartView::minimumMargins |
|
142 | 132 | The minimum margins allowed between the outer bounds and the plotArea of the ChartView. Margins |
|
143 | 133 | area of ChartView is used for drawing title, axes and legend. Please note that setting the |
|
144 | 134 | properties of minimumMargins may be bigger than the defined value, depending on other ChartView |
|
145 | 135 | properties that affect it's layout. If you need to know the actual plotting area used at any |
|
146 | 136 | given time, you can check ChartView::plotArea instead. |
|
147 | 137 | */ |
|
148 | 138 | |
|
149 | 139 | /*! |
|
150 | 140 | \qmlproperty rect ChartView::plotArea |
|
151 | 141 | The area on the ChartView that is used for drawing series. This is the ChartView rect without the |
|
152 | 142 | margins. |
|
153 | 143 | \sa ChartView::minimumMargins |
|
154 | 144 | */ |
|
155 | 145 | |
|
156 | 146 | /*! |
|
157 | 147 | \qmlmethod AbstractSeries ChartView::series(int index) |
|
158 | 148 | Returns the series with \a index on the chart. This allows you to loop through the series of a chart together with |
|
159 | 149 | the count property of the chart. |
|
160 | 150 | */ |
|
161 | 151 | |
|
162 | 152 | /*! |
|
163 | 153 | \qmlmethod AbstractSeries ChartView::series(string name) |
|
164 | 154 | Returns the first series on the chart with \a name. If there is no series with that name, returns null. |
|
165 | 155 | */ |
|
166 | 156 | |
|
167 | 157 | /*! |
|
168 | 158 | \qmlmethod AbstractSeries ChartView::createSeries(SeriesType type, string name, AbstractAxis axisX, AbstractAxis axisY) |
|
169 | 159 | Creates a series object of \a type to the chart with name \a name, optional axis \a axisX and |
|
170 | 160 | optional axis \a axisY. For example: |
|
171 | 161 | \code |
|
172 | 162 | // lineSeries is a LineSeries object that has already been added to the ChartView; re-use it's axes |
|
173 | 163 | var myAxisX = chartView.axisX(lineSeries); |
|
174 | 164 | var myAxisY = chartView.axisY(lineSeries); |
|
175 | 165 | var scatter = chartView.createSeries(ChartView.SeriesTypeScatter, "scatter series", myAxisX, myAxisY); |
|
176 | 166 | \endcode |
|
177 | 167 | */ |
|
178 | 168 | |
|
179 | 169 | /*! |
|
180 | 170 | \qmlmethod ChartView::removeSeries(AbstractSeries series) |
|
181 | 171 | Removes the \a series from the chart. The series object is also destroyed. |
|
182 | 172 | */ |
|
183 | 173 | |
|
184 | 174 | /*! |
|
185 | 175 | \qmlmethod ChartView::removeAllSeries() |
|
186 | 176 | Removes all series from the chart. All the series objects are also destroyed. |
|
187 | 177 | */ |
|
188 | 178 | |
|
189 | 179 | /*! |
|
190 | 180 | \qmlmethod Axis ChartView::axisX(AbstractSeries series) |
|
191 | 181 | The x-axis of the series. |
|
192 | 182 | */ |
|
193 | 183 | |
|
194 | 184 | /*! |
|
195 | 185 | \qmlmethod Axis ChartView::axisY(AbstractSeries series) |
|
196 | 186 | The y-axis of the series. |
|
197 | 187 | */ |
|
198 | 188 | |
|
199 | 189 | /*! |
|
200 | 190 | \qmlmethod ChartView::zoomY(real factor) |
|
201 | 191 | Zooms in by \a factor on the center of the chart. |
|
202 | 192 | */ |
|
203 | 193 | |
|
204 | 194 | /*! |
|
205 | 195 | \qmlmethod ChartView::scrollLeft(real pixels) |
|
206 | 196 | Scrolls to left by \a pixels. This is a convenience function that suits for example for key navigation. |
|
207 | 197 | \sa ValueAxis::min, ValueAxis::max, BarCategoryAxis::min, BarCategoryAxis::max |
|
208 | 198 | */ |
|
209 | 199 | |
|
210 | 200 | /*! |
|
211 | 201 | \qmlmethod ChartView::scrollRight(real pixels) |
|
212 | 202 | Scrolls to right by \a pixels. This is a convenience function that suits for example for key navigation. |
|
213 | 203 | \sa ValueAxis::min, ValueAxis::max, BarCategoryAxis::min, BarCategoryAxis::max |
|
214 | 204 | */ |
|
215 | 205 | |
|
216 | 206 | /*! |
|
217 | 207 | \qmlmethod ChartView::scrollUp(real pixels) |
|
218 | 208 | Scrolls up by \a pixels. This is a convenience function that suits for example for key navigation. |
|
219 | 209 | \sa ValueAxis::min, ValueAxis::max, BarCategoryAxis::min, BarCategoryAxis::max |
|
220 | 210 | */ |
|
221 | 211 | |
|
222 | 212 | /*! |
|
223 | 213 | \qmlmethod ChartView::scrollDown(real pixels) |
|
224 | 214 | Scrolls down by \a pixels. This is a convenience function that suits for example for key navigation. |
|
225 | 215 | \sa ValueAxis::min, ValueAxis::max, BarCategoryAxis::min, BarCategoryAxis::max |
|
226 | 216 | */ |
|
227 | 217 | |
|
228 | 218 | /*! |
|
229 | 219 | \qmlsignal ChartView::onPlotAreaChanged(rect plotArea) |
|
230 | 220 | The plot area of the chart has changed. This may happen for example, if you modify minimumMargins |
|
231 | 221 | or if you resize the chart, or if you modify font size related properties of the legend or chart |
|
232 | 222 | title. |
|
233 | 223 | */ |
|
234 | 224 | |
|
235 | 225 | /*! |
|
236 | 226 | \qmlsignal ChartView::seriesAdded(AbstractSeries series) |
|
237 | 227 | The \a series has been added to the chart. |
|
238 | 228 | */ |
|
239 | 229 | |
|
240 | 230 | /*! |
|
241 | 231 | \qmlsignal ChartView::seriesRemoved(AbstractSeries series) |
|
242 | 232 | The \a series has been removed from the chart. Please note that \a series is no longer a valid |
|
243 | 233 | object after the signal handler has completed. |
|
244 | 234 | */ |
|
245 | 235 | |
|
246 | 236 | DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent) |
|
247 | 237 | : QDeclarativeItem(parent), |
|
248 | 238 | m_chart(new QChart(this)) |
|
249 | 239 | { |
|
250 | 240 | setFlag(QGraphicsItem::ItemHasNoContents, false); |
|
251 | 241 | m_margins = new DeclarativeMargins(this); |
|
252 | 242 | m_margins->setTop(m_chart->margins().top()); |
|
253 | 243 | m_margins->setLeft(m_chart->margins().left()); |
|
254 | 244 | m_margins->setRight(m_chart->margins().right()); |
|
255 | 245 | m_margins->setBottom(m_chart->margins().bottom()); |
|
256 | 246 | connect(m_margins, SIGNAL(topChanged(int,int,int,int)), this, SLOT(changeMinimumMargins(int,int,int,int))); |
|
257 | 247 | connect(m_margins, SIGNAL(bottomChanged(int,int,int,int)), this, SLOT(changeMinimumMargins(int,int,int,int))); |
|
258 | 248 | connect(m_margins, SIGNAL(leftChanged(int,int,int,int)), this, SLOT(changeMinimumMargins(int,int,int,int))); |
|
259 | 249 | connect(m_margins, SIGNAL(rightChanged(int,int,int,int)), this, SLOT(changeMinimumMargins(int,int,int,int))); |
|
260 | 250 | connect(m_chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*)), this, SLOT(handleSeriesAdded(QAbstractSeries*,Domain*))); |
|
261 | 251 | connect(m_chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), this, SIGNAL(seriesRemoved(QAbstractSeries*))); |
|
262 | 252 | } |
|
263 | 253 | |
|
264 | 254 | void DeclarativeChart::handleSeriesAdded(QAbstractSeries *series, Domain *domain) |
|
265 | 255 | { |
|
266 | 256 | Q_UNUSED(domain) |
|
267 | 257 | emit seriesAdded(series); |
|
268 | 258 | } |
|
269 | 259 | |
|
270 | 260 | void DeclarativeChart::changeMinimumMargins(int top, int bottom, int left, int right) |
|
271 | 261 | { |
|
272 | 262 | m_chart->setMargins(QMargins(left, top, right, bottom)); |
|
273 | 263 | emit minimumMarginsChanged(); |
|
274 | 264 | emit plotAreaChanged(m_chart->plotArea()); |
|
275 | 265 | } |
|
276 | 266 | |
|
277 | 267 | DeclarativeChart::~DeclarativeChart() |
|
278 | 268 | { |
|
279 | 269 | delete m_chart; |
|
280 | 270 | } |
|
281 | 271 | |
|
282 | 272 | void DeclarativeChart::childEvent(QChildEvent *event) |
|
283 | 273 | { |
|
284 | 274 | if (event->type() == QEvent::ChildAdded) { |
|
285 | 275 | if (qobject_cast<QAbstractSeries *>(event->child())) { |
|
286 | 276 | m_chart->addSeries(qobject_cast<QAbstractSeries *>(event->child())); |
|
287 | 277 | } |
|
288 | 278 | } |
|
289 | 279 | } |
|
290 | 280 | |
|
291 | 281 | void DeclarativeChart::componentComplete() |
|
292 | 282 | { |
|
293 | 283 | foreach (QObject *child, children()) { |
|
294 | 284 | if (qobject_cast<QAbstractSeries *>(child)) { |
|
295 | 285 | // Add series to the chart |
|
296 | 286 | QAbstractSeries *series = qobject_cast<QAbstractSeries *>(child); |
|
297 | 287 | m_chart->addSeries(series); |
|
298 | 288 | |
|
299 | 289 | // Set optional user defined axes for the series and connect axis related signals |
|
300 | 290 | if (qobject_cast<DeclarativeLineSeries *>(child)) { |
|
301 | 291 | DeclarativeLineSeries *s = qobject_cast<DeclarativeLineSeries *>(child); |
|
302 | 292 | connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*))); |
|
303 | 293 | connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*))); |
|
304 | 294 | setAxisX(s->axisX(), s); |
|
305 | 295 | setAxisY(s->axisY(), s); |
|
306 | 296 | } else if (qobject_cast<DeclarativeSplineSeries *>(child)) { |
|
307 | 297 | DeclarativeSplineSeries *s = qobject_cast<DeclarativeSplineSeries *>(child); |
|
308 | 298 | connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*))); |
|
309 | 299 | connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*))); |
|
310 | 300 | setAxisX(s->axisX(), s); |
|
311 | 301 | setAxisY(s->axisY(), s); |
|
312 | 302 | } else if (qobject_cast<DeclarativeScatterSeries *>(child)) { |
|
313 | 303 | DeclarativeScatterSeries *s = qobject_cast<DeclarativeScatterSeries *>(child); |
|
314 | 304 | connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*))); |
|
315 | 305 | connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*))); |
|
316 | 306 | setAxisX(s->axisX(), s); |
|
317 | 307 | setAxisY(s->axisY(), s); |
|
318 | 308 | } else if (qobject_cast<DeclarativeAreaSeries *>(child)) { |
|
319 | 309 | DeclarativeAreaSeries *s = qobject_cast<DeclarativeAreaSeries *>(child); |
|
320 | 310 | connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*))); |
|
321 | 311 | connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*))); |
|
322 | 312 | setAxisX(s->axisX(), s); |
|
323 | 313 | setAxisY(s->axisY(), s); |
|
324 | 314 | } else if (qobject_cast<DeclarativeBarSeries *>(child)) { |
|
325 | 315 | DeclarativeBarSeries *s = qobject_cast<DeclarativeBarSeries *>(child); |
|
326 | 316 | connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*))); |
|
327 | 317 | connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*))); |
|
328 | 318 | setAxisX(s->axisX(), s); |
|
329 | 319 | setAxisY(s->axisY(), s); |
|
330 | 320 | } else if (qobject_cast<DeclarativeStackedBarSeries *>(child)) { |
|
331 | 321 | DeclarativeStackedBarSeries *s = qobject_cast<DeclarativeStackedBarSeries *>(child); |
|
332 | 322 | connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*))); |
|
333 | 323 | connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*))); |
|
334 | 324 | setAxisX(s->axisX(), s); |
|
335 | 325 | setAxisY(s->axisY(), s); |
|
336 | 326 | } else if (qobject_cast<DeclarativePercentBarSeries *>(child)) { |
|
337 | 327 | DeclarativePercentBarSeries *s = qobject_cast<DeclarativePercentBarSeries *>(child); |
|
338 | 328 | connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*))); |
|
339 | 329 | connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*))); |
|
340 | 330 | setAxisX(s->axisX(), s); |
|
341 | 331 | setAxisY(s->axisY(), s); |
|
342 | 332 | } else if (qobject_cast<DeclarativeHorizontalBarSeries *>(child)) { |
|
343 | 333 | DeclarativeHorizontalBarSeries *s = qobject_cast<DeclarativeHorizontalBarSeries *>(child); |
|
344 | 334 | connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*))); |
|
345 | 335 | connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*))); |
|
346 | 336 | setAxisX(s->axisX(), s); |
|
347 | 337 | setAxisY(s->axisY(), s); |
|
348 | 338 | } else if (qobject_cast<DeclarativeHorizontalStackedBarSeries *>(child)) { |
|
349 | 339 | DeclarativeHorizontalStackedBarSeries *s = qobject_cast<DeclarativeHorizontalStackedBarSeries *>(child); |
|
350 | 340 | connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*))); |
|
351 | 341 | connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*))); |
|
352 | 342 | setAxisX(s->axisX(), s); |
|
353 | 343 | setAxisY(s->axisY(), s); |
|
354 | 344 | } else if (qobject_cast<DeclarativeHorizontalPercentBarSeries *>(child)) { |
|
355 | 345 | DeclarativeHorizontalPercentBarSeries *s = qobject_cast<DeclarativeHorizontalPercentBarSeries *>(child); |
|
356 | 346 | connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*))); |
|
357 | 347 | connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*))); |
|
358 | 348 | setAxisX(s->axisX(), s); |
|
359 | 349 | setAxisY(s->axisY(), s); |
|
360 | 350 | } |
|
361 | 351 | } |
|
362 | 352 | } |
|
363 | 353 | |
|
364 | 354 | // Create the missing axes for the series that cannot be painted without axes |
|
365 | 355 | foreach (QAbstractSeries *chartSeries, m_chart->series()) |
|
366 | 356 | createDefaultAxes(chartSeries); |
|
367 | 357 | |
|
368 | 358 | QDeclarativeItem::componentComplete(); |
|
369 | 359 | } |
|
370 | 360 | |
|
371 | 361 | void DeclarativeChart::handleAxisXSet(QAbstractAxis *axis) |
|
372 | 362 | { |
|
373 | 363 | // qDebug() << "DeclarativeChart::handleAxisXSet" << sender() << axis; |
|
374 | 364 | if (axis && qobject_cast<QAbstractSeries *>(sender())) |
|
375 | 365 | m_chart->setAxisX(axis, qobject_cast<QAbstractSeries *>(sender())); |
|
376 | 366 | else |
|
377 | 367 | qWarning() << "Trying to set axisX to null."; |
|
378 | 368 | } |
|
379 | 369 | |
|
380 | 370 | void DeclarativeChart::handleAxisYSet(QAbstractAxis *axis) |
|
381 | 371 | { |
|
382 | 372 | // qDebug() << "DeclarativeChart::handleAxisYSet" << sender() << axis; |
|
383 | 373 | if (axis && qobject_cast<QAbstractSeries *>(sender())) |
|
384 | 374 | m_chart->setAxisY(axis, qobject_cast<QAbstractSeries *>(sender())); |
|
385 | 375 | else |
|
386 | 376 | qWarning() << "Trying to set axisY to null."; |
|
387 | 377 | } |
|
388 | 378 | |
|
389 | 379 | void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) |
|
390 | 380 | { |
|
391 | 381 | // qDebug() << "DeclarativeChart::geometryChanged" << newGeometry.width() << newGeometry.height(); |
|
392 | 382 | if (newGeometry.isValid()) { |
|
393 | 383 | if (newGeometry.width() > 0 && newGeometry.height() > 0) { |
|
394 | 384 | m_chart->resize(newGeometry.width(), newGeometry.height()); |
|
395 | 385 | } |
|
396 | 386 | } |
|
397 | 387 | QDeclarativeItem::geometryChanged(newGeometry, oldGeometry); |
|
398 | 388 | |
|
399 | 389 | // It would be better to trigger the plotAreaChanged signal from QChart::plotAreaChanged or |
|
400 | 390 | // similar. Since that kind of a signal is not clearly needed in the C++ API the work-around is |
|
401 | 391 | // to implement it here for the QML API purposes. |
|
402 | 392 | emit plotAreaChanged(m_chart->plotArea()); |
|
403 | 393 | } |
|
404 | 394 | |
|
405 | 395 | void DeclarativeChart::setTheme(DeclarativeChart::Theme theme) |
|
406 | 396 | { |
|
407 | 397 | QChart::ChartTheme chartTheme = (QChart::ChartTheme) theme; |
|
408 | 398 | if (chartTheme != m_chart->theme()) |
|
409 | 399 | m_chart->setTheme(chartTheme); |
|
410 | 400 | } |
|
411 | 401 | |
|
412 | 402 | DeclarativeChart::Theme DeclarativeChart::theme() |
|
413 | 403 | { |
|
414 | 404 | return (DeclarativeChart::Theme) m_chart->theme(); |
|
415 | 405 | } |
|
416 | 406 | |
|
417 | 407 | void DeclarativeChart::setAnimationOptions(DeclarativeChart::Animation animations) |
|
418 | 408 | { |
|
419 | 409 | QChart::AnimationOption animationOptions = (QChart::AnimationOption) animations; |
|
420 | 410 | if (animationOptions != m_chart->animationOptions()) |
|
421 | 411 | m_chart->setAnimationOptions(animationOptions); |
|
422 | 412 | } |
|
423 | 413 | |
|
424 | 414 | DeclarativeChart::Animation DeclarativeChart::animationOptions() |
|
425 | 415 | { |
|
426 | 416 | if (m_chart->animationOptions().testFlag(QChart::AllAnimations)) |
|
427 | 417 | return DeclarativeChart::AllAnimations; |
|
428 | 418 | else if (m_chart->animationOptions().testFlag(QChart::GridAxisAnimations)) |
|
429 | 419 | return DeclarativeChart::GridAxisAnimations; |
|
430 | 420 | else if (m_chart->animationOptions().testFlag(QChart::SeriesAnimations)) |
|
431 | 421 | return DeclarativeChart::SeriesAnimations; |
|
432 | 422 | else |
|
433 | 423 | return DeclarativeChart::NoAnimation; |
|
434 | 424 | } |
|
435 | 425 | |
|
436 | 426 | void DeclarativeChart::setTitle(QString title) |
|
437 | 427 | { |
|
438 | 428 | if (title != m_chart->title()) |
|
439 | 429 | m_chart->setTitle(title); |
|
440 | 430 | } |
|
441 | 431 | QString DeclarativeChart::title() |
|
442 | 432 | { |
|
443 | 433 | return m_chart->title(); |
|
444 | 434 | } |
|
445 | 435 | |
|
446 | 436 | QAbstractAxis *DeclarativeChart::axisX(QAbstractSeries *series) |
|
447 | 437 | { |
|
448 | 438 | return m_chart->axisX(series); |
|
449 | 439 | } |
|
450 | 440 | |
|
451 | 441 | QAbstractAxis *DeclarativeChart::axisY(QAbstractSeries *series) |
|
452 | 442 | { |
|
453 | 443 | return m_chart->axisY(series); |
|
454 | 444 | } |
|
455 | 445 | |
|
456 | 446 | QLegend *DeclarativeChart::legend() |
|
457 | 447 | { |
|
458 | 448 | return m_chart->legend(); |
|
459 | 449 | } |
|
460 | 450 | |
|
461 | 451 | void DeclarativeChart::setTitleColor(QColor color) |
|
462 | 452 | { |
|
463 | 453 | QBrush b = m_chart->titleBrush(); |
|
464 | 454 | if (color != b.color()) { |
|
465 | 455 | b.setColor(color); |
|
466 | 456 | m_chart->setTitleBrush(b); |
|
467 | 457 | emit titleColorChanged(color); |
|
468 | 458 | } |
|
469 | 459 | } |
|
470 | 460 | |
|
471 | 461 | QFont DeclarativeChart::titleFont() const |
|
472 | 462 | { |
|
473 | 463 | return m_chart->titleFont(); |
|
474 | 464 | } |
|
475 | 465 | |
|
476 | 466 | void DeclarativeChart::setTitleFont(const QFont &font) |
|
477 | 467 | { |
|
478 | 468 | m_chart->setTitleFont(font); |
|
479 | 469 | } |
|
480 | 470 | |
|
481 | 471 | QColor DeclarativeChart::titleColor() |
|
482 | 472 | { |
|
483 | 473 | return m_chart->titleBrush().color(); |
|
484 | 474 | } |
|
485 | 475 | |
|
486 | 476 | void DeclarativeChart::setBackgroundColor(QColor color) |
|
487 | 477 | { |
|
488 | 478 | QBrush b = m_chart->backgroundBrush(); |
|
489 | 479 | if (b.style() != Qt::SolidPattern || color != b.color()) { |
|
490 | 480 | b.setStyle(Qt::SolidPattern); |
|
491 | 481 | b.setColor(color); |
|
492 | 482 | m_chart->setBackgroundBrush(b); |
|
493 | 483 | emit backgroundColorChanged(); |
|
494 | 484 | } |
|
495 | 485 | } |
|
496 | 486 | |
|
497 | 487 | QColor DeclarativeChart::backgroundColor() |
|
498 | 488 | { |
|
499 | 489 | return m_chart->backgroundBrush().color(); |
|
500 | 490 | } |
|
501 | 491 | |
|
502 | 492 | int DeclarativeChart::count() |
|
503 | 493 | { |
|
504 | 494 | return m_chart->series().count(); |
|
505 | 495 | } |
|
506 | 496 | |
|
507 | 497 | void DeclarativeChart::setDropShadowEnabled(bool enabled) |
|
508 | 498 | { |
|
509 | 499 | if (enabled != m_chart->isDropShadowEnabled()) { |
|
510 | 500 | m_chart->setDropShadowEnabled(enabled); |
|
511 | 501 | dropShadowEnabledChanged(enabled); |
|
512 | 502 | } |
|
513 | 503 | } |
|
514 | 504 | |
|
515 | 505 | bool DeclarativeChart::dropShadowEnabled() |
|
516 | 506 | { |
|
517 | 507 | return m_chart->isDropShadowEnabled(); |
|
518 | 508 | } |
|
519 | 509 | |
|
520 | 510 | qreal DeclarativeChart::topMargin() |
|
521 | 511 | { |
|
522 | 512 | qWarning() << "ChartView.topMargin is deprecated. Use minimumMargins and plotArea instead."; |
|
523 | 513 | return m_chart->plotArea().top(); |
|
524 | 514 | } |
|
525 | 515 | |
|
526 | 516 | qreal DeclarativeChart::bottomMargin() |
|
527 | 517 | { |
|
528 | 518 | |
|
529 | 519 | qWarning() << "ChartView.bottomMargin is deprecated. Use minimumMargins and plotArea instead."; |
|
530 | 520 | return m_chart->plotArea().bottom(); |
|
531 | 521 | } |
|
532 | 522 | |
|
533 | 523 | qreal DeclarativeChart::leftMargin() |
|
534 | 524 | { |
|
535 | 525 | qWarning() << "ChartView.leftMargin is deprecated. Use minimumMargins and plotArea instead."; |
|
536 | 526 | return m_chart->plotArea().left(); |
|
537 | 527 | } |
|
538 | 528 | |
|
539 | 529 | qreal DeclarativeChart::rightMargin() |
|
540 | 530 | { |
|
541 | 531 | qWarning() << "ChartView.rightMargin is deprecated. Use minimumMargins and plotArea instead."; |
|
542 | 532 | return m_chart->plotArea().right(); |
|
543 | 533 | } |
|
544 | 534 | |
|
545 | 535 | void DeclarativeChart::zoom(qreal factor) |
|
546 | 536 | { |
|
547 | 537 | m_chart->zoom(factor); |
|
548 | 538 | } |
|
549 | 539 | |
|
550 | 540 | void DeclarativeChart::scrollLeft(qreal pixels) |
|
551 | 541 | { |
|
552 | 542 | m_chart->scroll(-pixels, 0); |
|
553 | 543 | } |
|
554 | 544 | |
|
555 | 545 | void DeclarativeChart::scrollRight(qreal pixels) |
|
556 | 546 | { |
|
557 | 547 | m_chart->scroll(pixels, 0); |
|
558 | 548 | } |
|
559 | 549 | |
|
560 | 550 | void DeclarativeChart::scrollUp(qreal pixels) |
|
561 | 551 | { |
|
562 | 552 | m_chart->scroll(0, pixels); |
|
563 | 553 | } |
|
564 | 554 | |
|
565 | 555 | void DeclarativeChart::scrollDown(qreal pixels) |
|
566 | 556 | { |
|
567 | 557 | m_chart->scroll(0, -pixels); |
|
568 | 558 | } |
|
569 | 559 | |
|
570 | 560 | QAbstractSeries *DeclarativeChart::series(int index) |
|
571 | 561 | { |
|
572 | 562 | if (index < m_chart->series().count()) { |
|
573 | 563 | return m_chart->series().at(index); |
|
574 | 564 | } |
|
575 | 565 | return 0; |
|
576 | 566 | } |
|
577 | 567 | |
|
578 | 568 | QAbstractSeries *DeclarativeChart::series(QString seriesName) |
|
579 | 569 | { |
|
580 | 570 | foreach (QAbstractSeries *series, m_chart->series()) { |
|
581 | 571 | if (series->name() == seriesName) |
|
582 | 572 | return series; |
|
583 | 573 | } |
|
584 | 574 | return 0; |
|
585 | 575 | } |
|
586 | 576 | |
|
587 | 577 | QAbstractSeries *DeclarativeChart::createSeries(DeclarativeChart::SeriesType type, QString name) |
|
588 | 578 | { |
|
589 | 579 | return createSeries(type, name, 0, 0); |
|
590 | 580 | } |
|
591 | 581 | |
|
592 | 582 | QAbstractSeries *DeclarativeChart::createSeries(DeclarativeChart::SeriesType type, QString name, QAbstractAxis *axisX, QAbstractAxis *axisY) |
|
593 | 583 | { |
|
594 | 584 | QAbstractSeries *series = 0; |
|
595 | 585 | |
|
596 | 586 | switch (type) { |
|
597 | 587 | case DeclarativeChart::SeriesTypeLine: |
|
598 | 588 | series = new DeclarativeLineSeries(); |
|
599 | 589 | break; |
|
600 | 590 | case DeclarativeChart::SeriesTypeArea: |
|
601 | 591 | series = new DeclarativeAreaSeries(); |
|
602 | 592 | break; |
|
603 | 593 | case DeclarativeChart::SeriesTypeStackedBar: |
|
604 | 594 | series = new DeclarativeStackedBarSeries(); |
|
605 | 595 | break; |
|
606 | 596 | case DeclarativeChart::SeriesTypePercentBar: |
|
607 | 597 | series = new DeclarativePercentBarSeries(); |
|
608 | 598 | break; |
|
609 | 599 | case DeclarativeChart::SeriesTypeBar: |
|
610 | 600 | series = new DeclarativeBarSeries(); |
|
611 | 601 | break; |
|
612 | 602 | case DeclarativeChart::SeriesTypeHorizontalBar: |
|
613 | 603 | series = new DeclarativeHorizontalBarSeries(); |
|
614 | 604 | break; |
|
615 | 605 | case DeclarativeChart::SeriesTypeHorizontalPercentBar: |
|
616 | 606 | series = new DeclarativeHorizontalPercentBarSeries(); |
|
617 | 607 | break; |
|
618 | 608 | case DeclarativeChart::SeriesTypeHorizontalStackedBar: |
|
619 | 609 | series = new DeclarativeHorizontalStackedBarSeries(); |
|
620 | 610 | break; |
|
621 | 611 | case DeclarativeChart::SeriesTypePie: |
|
622 | 612 | series = new DeclarativePieSeries(); |
|
623 | 613 | break; |
|
624 | 614 | case DeclarativeChart::SeriesTypeScatter: |
|
625 | 615 | series = new DeclarativeScatterSeries(); |
|
626 | 616 | break; |
|
627 | 617 | case DeclarativeChart::SeriesTypeSpline: |
|
628 | 618 | series = new DeclarativeSplineSeries(); |
|
629 | 619 | break; |
|
630 | 620 | default: |
|
631 | 621 | qWarning() << "Illegal series type"; |
|
632 | 622 | } |
|
633 | 623 | |
|
634 | 624 | if (series) { |
|
635 | 625 | series->setName(name); |
|
636 | 626 | m_chart->addSeries(series); |
|
637 | 627 | // Set possible user defined axes |
|
638 | 628 | setAxisX(axisX, series); |
|
639 | 629 | setAxisY(axisY, series); |
|
640 | 630 | // Then create the missing axes |
|
641 | 631 | createDefaultAxes(series); |
|
642 | 632 | } |
|
643 | 633 | |
|
644 | 634 | return series; |
|
645 | 635 | } |
|
646 | 636 | |
|
647 | 637 | void DeclarativeChart::removeSeries(QAbstractSeries *series) |
|
648 | 638 | { |
|
649 | 639 | if (series) |
|
650 | 640 | m_chart->removeSeries(series); |
|
651 | 641 | else |
|
652 | 642 | qWarning("removeSeries: cannot remove null"); |
|
653 | 643 | } |
|
654 | 644 | |
|
655 | 645 | void DeclarativeChart::setAxisX(QAbstractAxis *axis, QAbstractSeries *series) |
|
656 | 646 | { |
|
657 | 647 | if (axis) |
|
658 | 648 | m_chart->setAxisX(axis, series); |
|
659 | 649 | } |
|
660 | 650 | |
|
661 | 651 | void DeclarativeChart::setAxisY(QAbstractAxis *axis, QAbstractSeries *series) |
|
662 | 652 | { |
|
663 | 653 | if (axis) |
|
664 | 654 | m_chart->setAxisY(axis, series); |
|
665 | 655 | } |
|
666 | 656 | |
|
667 | 657 | void DeclarativeChart::createDefaultAxes() |
|
668 | 658 | { |
|
669 | 659 | qWarning() << "ChartView.createDefaultAxes() is deprecated. Axes are created automatically."; |
|
670 | 660 | } |
|
671 | 661 | |
|
672 | 662 | void DeclarativeChart::createDefaultAxes(QAbstractSeries *series) |
|
673 | 663 | { |
|
674 | 664 | foreach (QAbstractSeries *s, m_chart->series()) { |
|
675 | 665 | // If there is already an x axis of the correct type, re-use it |
|
676 | 666 | if (!m_chart->axisX(series) && s != series && m_chart->axisX(s) |
|
677 | 667 | && m_chart->axisX(s)->type() == series->d_ptr->defaultAxisType(Qt::Horizontal)) |
|
678 | 668 | m_chart->setAxisX(m_chart->axisX(s), series); |
|
679 | 669 | |
|
680 | 670 | // If there is already a y axis of the correct type, re-use it |
|
681 | 671 | if (!m_chart->axisY(series) && s != series && m_chart->axisY(s) |
|
682 | 672 | && m_chart->axisY(s)->type() == series->d_ptr->defaultAxisType(Qt::Vertical)) |
|
683 | 673 | m_chart->setAxisY(m_chart->axisY(s), series); |
|
684 | 674 | } |
|
685 | 675 | |
|
686 | 676 | // If no x axis of correct type was found, create a new x axis based of default axis type |
|
687 | 677 | if (!m_chart->axisX(series)) { |
|
688 | 678 | switch (series->d_ptr->defaultAxisType(Qt::Horizontal)) { |
|
689 | 679 | case QAbstractAxis::AxisTypeValue: |
|
690 | 680 | m_chart->setAxisX(new QValueAxis(this), series); |
|
691 | 681 | break; |
|
692 | 682 | case QAbstractAxis::AxisTypeBarCategory: |
|
693 | 683 | m_chart->setAxisX(new QBarCategoryAxis(this), series); |
|
694 | 684 | break; |
|
695 | 685 | case QAbstractAxis::AxisTypeCategory: |
|
696 | 686 | m_chart->setAxisX(new QCategoryAxis(this), series); |
|
697 | 687 | break; |
|
698 | 688 | #ifndef QT_ON_ARM |
|
699 | 689 | case QAbstractAxis::AxisTypeDateTime: |
|
700 | 690 | m_chart->setAxisX(new QDateTimeAxis(this), series); |
|
701 | 691 | break; |
|
702 | 692 | #endif |
|
703 | 693 | default: |
|
704 | 694 | // Do nothing, assume AxisTypeNoAxis |
|
705 | 695 | break; |
|
706 | 696 | } |
|
707 | 697 | } |
|
708 | 698 | |
|
709 | 699 | // If no y axis of correct type was found, create a new y axis based of default axis type |
|
710 | 700 | if (!m_chart->axisY(series)) { |
|
711 | 701 | switch (series->d_ptr->defaultAxisType(Qt::Vertical)) { |
|
712 | 702 | case QAbstractAxis::AxisTypeValue: |
|
713 | 703 | m_chart->setAxisY(new QValueAxis(this), series); |
|
714 | 704 | break; |
|
715 | 705 | case QAbstractAxis::AxisTypeBarCategory: |
|
716 | 706 | m_chart->setAxisY(new QBarCategoryAxis(this), series); |
|
717 | 707 | break; |
|
718 | 708 | case QAbstractAxis::AxisTypeCategory: |
|
719 | 709 | m_chart->setAxisY(new QCategoryAxis(this), series); |
|
720 | 710 | break; |
|
721 | 711 | #ifndef QT_ON_ARM |
|
722 | 712 | case QAbstractAxis::AxisTypeDateTime: |
|
723 | 713 | m_chart->setAxisY(new QDateTimeAxis(this), series); |
|
724 | 714 | break; |
|
725 | 715 | #endif |
|
726 | 716 | default: |
|
727 | 717 | // Do nothing, assume AxisTypeNoAxis |
|
728 | 718 | break; |
|
729 | 719 | } |
|
730 | 720 | } |
|
731 | 721 | |
|
732 | 722 | //qDebug() << "axis for series" << series << "x:" << m_chart->axisX(series) << "y:" << m_chart->axisY(series); |
|
733 | 723 | } |
|
734 | 724 | |
|
735 | 725 | #include "moc_declarativechart.cpp" |
|
736 | 726 | |
|
737 | 727 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,514 +1,514 | |||
|
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 "qlegend.h" |
|
22 | 22 | #include "qlegend_p.h" |
|
23 | 23 | #include "qabstractseries.h" |
|
24 | 24 | #include "qabstractseries_p.h" |
|
25 | 25 | #include "qchart_p.h" |
|
26 | 26 | #include "legendlayout_p.h" |
|
27 | 27 | #include "legendmarker_p.h" |
|
28 | 28 | #include "qxyseries.h" |
|
29 | 29 | #include "qlineseries.h" |
|
30 | 30 | #include "qareaseries.h" |
|
31 | 31 | #include "qscatterseries.h" |
|
32 | 32 | #include "qsplineseries.h" |
|
33 | 33 | #include "qabstractbarseries.h" |
|
34 | 34 | #include "qstackedbarseries.h" |
|
35 | 35 | #include "qpercentbarseries.h" |
|
36 | 36 | #include "qbarset.h" |
|
37 | 37 | #include "qpieseries.h" |
|
38 | 38 | #include "qpieseries_p.h" |
|
39 | 39 | #include "qpieslice.h" |
|
40 | 40 | #include "chartpresenter_p.h" |
|
41 | 41 | #include "chartlayout_p.h" |
|
42 | 42 | #include <QPainter> |
|
43 | 43 | #include <QPen> |
|
44 | 44 | #include <QTimer> |
|
45 | 45 | #include <QGraphicsSceneEvent> |
|
46 | 46 | |
|
47 | 47 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
48 | 48 | |
|
49 | 49 | /*! |
|
50 | 50 | \class QLegend |
|
51 | 51 | \brief Legend object |
|
52 | 52 | \mainclass |
|
53 | 53 | |
|
54 | 54 | QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when |
|
55 | 55 | series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and |
|
56 | 56 | handle the drawing manually. |
|
57 | 57 | User isn't supposed to create or delete legend objects, but can reference it via QChart class. |
|
58 | 58 | |
|
59 | 59 | \image examples_percentbarchart_legend.png |
|
60 | 60 | |
|
61 | 61 | \sa QChart |
|
62 | 62 | */ |
|
63 | 63 | /*! |
|
64 | 64 | \qmlclass Legend QLegend |
|
65 | 65 | \brief Legend is part of QtCommercial Chart QML API. |
|
66 | 66 | |
|
67 | 67 | Legend is a graphical object, whics displays legend of the chart. Legend state is updated by ChartView, when |
|
68 | 68 | series have been changed. Legend is used via ChartView class. For example: |
|
69 | 69 | \code |
|
70 | 70 | ChartView { |
|
71 | 71 | legend.visible: true |
|
72 | 72 | legend.alignment: Qt.AlignBottom |
|
73 | 73 | // Add a few series... |
|
74 | 74 | } |
|
75 | 75 | \endcode |
|
76 | 76 | |
|
77 | 77 | \image examples_percentbarchart_legend.png |
|
78 | 78 | */ |
|
79 | 79 | |
|
80 | 80 | /*! |
|
81 | 81 | \property QLegend::alignment |
|
82 | 82 | \brief The alignment of the legend. |
|
83 | 83 | |
|
84 | 84 | Legend paints on the defined position in the chart. The following alignments are supported: |
|
85 | 85 | Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result is undefined. |
|
86 | 86 | */ |
|
87 | 87 | /*! |
|
88 | 88 | \qmlproperty Qt.Alignment Legend::alignment |
|
89 | 89 | \brief The alignment of the legend. |
|
90 | 90 | |
|
91 | 91 | Legend paints on the defined position in the chart. The following alignments are supported: |
|
92 | 92 | Qt.AlignTop, Qt.AlignBottom, Qt.AlignLeft, Qt.AlignRight. If you set more than one flag the result is undefined. |
|
93 | 93 | */ |
|
94 | 94 | |
|
95 | 95 | /*! |
|
96 | 96 | \property QLegend::backgroundVisible |
|
97 | 97 | Whether the legend background is visible or not. |
|
98 | 98 | */ |
|
99 | 99 | /*! |
|
100 | 100 | \qmlproperty bool Legend::backgroundVisible |
|
101 | 101 | Whether the legend background is visible or not. |
|
102 | 102 | */ |
|
103 | 103 | |
|
104 | 104 | /*! |
|
105 | 105 | \property QLegend::color |
|
106 | 106 | The color of the legend, i.e. the background (brush) color. Note that if you change the color |
|
107 | 107 | of the legend, the style of the legend brush is set to Qt::SolidPattern. |
|
108 | 108 | */ |
|
109 | 109 | /*! |
|
110 | 110 | \qmlproperty color Legend::color |
|
111 | 111 | The color of the legend, i.e. the background (brush) color. |
|
112 | 112 | */ |
|
113 | 113 | |
|
114 | 114 | /*! |
|
115 | 115 | \property QLegend::borderColor |
|
116 | 116 | The border color of the legend, i.e. the line color. |
|
117 | 117 | */ |
|
118 | 118 | /*! |
|
119 | 119 | \qmlproperty color Legend::borderColor |
|
120 | 120 | The border color of the legend, i.e. the line color. |
|
121 | 121 | */ |
|
122 | 122 | |
|
123 | 123 | /*! |
|
124 | 124 | \property QLegend::font |
|
125 | 125 | The font of markers used by legend |
|
126 | 126 | */ |
|
127 | 127 | /*! |
|
128 |
\qmlproperty |
|
|
128 | \qmlproperty Font Legend::font | |
|
129 | 129 | The font of markers used by legend |
|
130 | 130 | */ |
|
131 | 131 | |
|
132 | 132 | /*! |
|
133 | 133 | \property QLegend::labelColor |
|
134 | 134 | The color of brush used to draw labels. |
|
135 | 135 | */ |
|
136 | 136 | /*! |
|
137 | 137 | \qmlproperty color QLegend::labelColor |
|
138 | 138 | The color of brush used to draw labels. |
|
139 | 139 | */ |
|
140 | 140 | |
|
141 | 141 | /*! |
|
142 | 142 | \fn void QLegend::backgroundVisibleChanged(bool) |
|
143 | 143 | The visibility of the legend background changed to \a visible. |
|
144 | 144 | */ |
|
145 | 145 | |
|
146 | 146 | /*! |
|
147 | 147 | \fn void QLegend::colorChanged(QColor) |
|
148 | 148 | The color of the legend background changed to \a color. |
|
149 | 149 | */ |
|
150 | 150 | |
|
151 | 151 | /*! |
|
152 | 152 | \fn void QLegend::borderColorChanged(QColor) |
|
153 | 153 | The border color of the legend background changed to \a color. |
|
154 | 154 | */ |
|
155 | 155 | |
|
156 | 156 | /*! |
|
157 | 157 | \fn void QLegend::fontChanged(QFont) |
|
158 | 158 | The font of markers of the legend changed to \a font. |
|
159 | 159 | */ |
|
160 | 160 | |
|
161 | 161 | /*! |
|
162 | 162 | \fn void QLegend::labelColorChanged(QColor color) |
|
163 | 163 | This signal is emitted when the color of brush used to draw labels has changed to \a color. |
|
164 | 164 | */ |
|
165 | 165 | |
|
166 | 166 | /*! |
|
167 | 167 | Constructs the legend object and sets the parent to \a parent |
|
168 | 168 | */ |
|
169 | 169 | |
|
170 | 170 | QLegend::QLegend(QChart *chart): QGraphicsWidget(chart), |
|
171 | 171 | d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter, chart, this)) |
|
172 | 172 | { |
|
173 | 173 | setZValue(ChartPresenter::LegendZValue); |
|
174 | 174 | setFlags(QGraphicsItem::ItemClipsChildrenToShape); |
|
175 | 175 | QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*)), d_ptr.data(), SLOT(handleSeriesAdded(QAbstractSeries*,Domain*))); |
|
176 | 176 | QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(), SLOT(handleSeriesRemoved(QAbstractSeries*))); |
|
177 | 177 | // QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*))); |
|
178 | 178 | setLayout(d_ptr->m_layout); |
|
179 | 179 | } |
|
180 | 180 | |
|
181 | 181 | /*! |
|
182 | 182 | Destroys the legend object. Legend is always owned by a QChart, so an application should never call this. |
|
183 | 183 | */ |
|
184 | 184 | QLegend::~QLegend() |
|
185 | 185 | { |
|
186 | 186 | } |
|
187 | 187 | |
|
188 | 188 | /*! |
|
189 | 189 | \internal |
|
190 | 190 | */ |
|
191 | 191 | void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
192 | 192 | { |
|
193 | 193 | Q_UNUSED(option) |
|
194 | 194 | Q_UNUSED(widget) |
|
195 | 195 | |
|
196 | 196 | if (!d_ptr->m_backgroundVisible) |
|
197 | 197 | return; |
|
198 | 198 | |
|
199 | 199 | painter->setOpacity(opacity()); |
|
200 | 200 | painter->setPen(d_ptr->m_pen); |
|
201 | 201 | painter->setBrush(d_ptr->m_brush); |
|
202 | 202 | painter->drawRoundRect(rect(), d_ptr->roundness(rect().width()), d_ptr->roundness(rect().height())); |
|
203 | 203 | } |
|
204 | 204 | |
|
205 | 205 | |
|
206 | 206 | /*! |
|
207 | 207 | Sets the \a brush of legend. Brush affects the background of legend. |
|
208 | 208 | */ |
|
209 | 209 | void QLegend::setBrush(const QBrush &brush) |
|
210 | 210 | { |
|
211 | 211 | if (d_ptr->m_brush != brush) { |
|
212 | 212 | d_ptr->m_brush = brush; |
|
213 | 213 | update(); |
|
214 | 214 | emit colorChanged(brush.color()); |
|
215 | 215 | } |
|
216 | 216 | } |
|
217 | 217 | |
|
218 | 218 | /*! |
|
219 | 219 | Returns the brush used by legend. |
|
220 | 220 | */ |
|
221 | 221 | QBrush QLegend::brush() const |
|
222 | 222 | { |
|
223 | 223 | return d_ptr->m_brush; |
|
224 | 224 | } |
|
225 | 225 | |
|
226 | 226 | void QLegend::setColor(QColor color) |
|
227 | 227 | { |
|
228 | 228 | QBrush b = d_ptr->m_brush; |
|
229 | 229 | if (b.style() != Qt::SolidPattern || b.color() != color) { |
|
230 | 230 | b.setStyle(Qt::SolidPattern); |
|
231 | 231 | b.setColor(color); |
|
232 | 232 | setBrush(b); |
|
233 | 233 | } |
|
234 | 234 | } |
|
235 | 235 | |
|
236 | 236 | QColor QLegend::color() |
|
237 | 237 | { |
|
238 | 238 | return d_ptr->m_brush.color(); |
|
239 | 239 | } |
|
240 | 240 | |
|
241 | 241 | /*! |
|
242 | 242 | Sets the \a pen of legend. Pen affects the legend borders. |
|
243 | 243 | */ |
|
244 | 244 | void QLegend::setPen(const QPen &pen) |
|
245 | 245 | { |
|
246 | 246 | if (d_ptr->m_pen != pen) { |
|
247 | 247 | d_ptr->m_pen = pen; |
|
248 | 248 | update(); |
|
249 | 249 | emit borderColorChanged(pen.color()); |
|
250 | 250 | } |
|
251 | 251 | } |
|
252 | 252 | |
|
253 | 253 | /*! |
|
254 | 254 | Returns the pen used by legend |
|
255 | 255 | */ |
|
256 | 256 | |
|
257 | 257 | QPen QLegend::pen() const |
|
258 | 258 | { |
|
259 | 259 | return d_ptr->m_pen; |
|
260 | 260 | } |
|
261 | 261 | |
|
262 | 262 | void QLegend::setFont(const QFont &font) |
|
263 | 263 | { |
|
264 | 264 | if (d_ptr->m_font != font) { |
|
265 | 265 | d_ptr->m_font = font; |
|
266 | 266 | foreach (LegendMarker *marker, d_ptr->markers()) |
|
267 | 267 | marker->setFont(d_ptr->m_font); |
|
268 | 268 | layout()->invalidate(); |
|
269 | 269 | emit fontChanged(font); |
|
270 | 270 | } |
|
271 | 271 | } |
|
272 | 272 | |
|
273 | 273 | QFont QLegend::font() const |
|
274 | 274 | { |
|
275 | 275 | return d_ptr->m_font; |
|
276 | 276 | } |
|
277 | 277 | |
|
278 | 278 | void QLegend::setBorderColor(QColor color) |
|
279 | 279 | { |
|
280 | 280 | QPen p = d_ptr->m_pen; |
|
281 | 281 | if (p.color() != color) { |
|
282 | 282 | p.setColor(color); |
|
283 | 283 | setPen(p); |
|
284 | 284 | } |
|
285 | 285 | } |
|
286 | 286 | |
|
287 | 287 | QColor QLegend::borderColor() |
|
288 | 288 | { |
|
289 | 289 | return d_ptr->m_pen.color(); |
|
290 | 290 | } |
|
291 | 291 | |
|
292 | 292 | /*! |
|
293 | 293 | Set brush used to draw labels to \a brush. |
|
294 | 294 | */ |
|
295 | 295 | void QLegend::setLabelBrush(const QBrush &brush) |
|
296 | 296 | { |
|
297 | 297 | if (d_ptr->m_labelBrush != brush) { |
|
298 | 298 | d_ptr->m_labelBrush = brush; |
|
299 | 299 | foreach (LegendMarker *marker, d_ptr->markers()) { |
|
300 | 300 | marker->setLabelBrush(d_ptr->m_labelBrush); |
|
301 | 301 | // Note: The pen of the marker rectangle could be exposed in the public QLegend API |
|
302 | 302 | // instead of mapping it from label brush color |
|
303 | 303 | marker->setPen(brush.color()); |
|
304 | 304 | } |
|
305 | 305 | emit labelColorChanged(brush.color()); |
|
306 | 306 | } |
|
307 | 307 | } |
|
308 | 308 | |
|
309 | 309 | /*! |
|
310 | 310 | Brush used to draw labels. |
|
311 | 311 | */ |
|
312 | 312 | QBrush QLegend::labelBrush() const |
|
313 | 313 | { |
|
314 | 314 | return d_ptr->m_labelBrush; |
|
315 | 315 | } |
|
316 | 316 | |
|
317 | 317 | void QLegend::setLabelColor(QColor color) |
|
318 | 318 | { |
|
319 | 319 | QBrush b = d_ptr->m_labelBrush; |
|
320 | 320 | if (b.style() != Qt::SolidPattern || b.color() != color) { |
|
321 | 321 | b.setStyle(Qt::SolidPattern); |
|
322 | 322 | b.setColor(color); |
|
323 | 323 | setLabelBrush(b); |
|
324 | 324 | } |
|
325 | 325 | } |
|
326 | 326 | |
|
327 | 327 | QColor QLegend::labelColor() const |
|
328 | 328 | { |
|
329 | 329 | return d_ptr->m_labelBrush.color(); |
|
330 | 330 | } |
|
331 | 331 | |
|
332 | 332 | |
|
333 | 333 | void QLegend::setAlignment(Qt::Alignment alignment) |
|
334 | 334 | { |
|
335 | 335 | if (d_ptr->m_alignment != alignment) { |
|
336 | 336 | d_ptr->m_alignment = alignment; |
|
337 | 337 | layout()->invalidate(); |
|
338 | 338 | } |
|
339 | 339 | } |
|
340 | 340 | |
|
341 | 341 | Qt::Alignment QLegend::alignment() const |
|
342 | 342 | { |
|
343 | 343 | return d_ptr->m_alignment; |
|
344 | 344 | } |
|
345 | 345 | |
|
346 | 346 | /*! |
|
347 | 347 | Detaches the legend from chart. Chart won't change layout of the legend. |
|
348 | 348 | */ |
|
349 | 349 | void QLegend::detachFromChart() |
|
350 | 350 | { |
|
351 | 351 | d_ptr->m_attachedToChart = false; |
|
352 | 352 | layout()->invalidate(); |
|
353 | 353 | setParent(0); |
|
354 | 354 | |
|
355 | 355 | } |
|
356 | 356 | |
|
357 | 357 | /*! |
|
358 | 358 | Attaches the legend to chart. Chart may change layout of the legend. |
|
359 | 359 | */ |
|
360 | 360 | void QLegend::attachToChart() |
|
361 | 361 | { |
|
362 | 362 | d_ptr->m_attachedToChart = true; |
|
363 | 363 | layout()->invalidate(); |
|
364 | 364 | setParent(d_ptr->m_chart); |
|
365 | 365 | } |
|
366 | 366 | |
|
367 | 367 | /*! |
|
368 | 368 | Returns true, if legend is attached to chart. |
|
369 | 369 | */ |
|
370 | 370 | bool QLegend::isAttachedToChart() |
|
371 | 371 | { |
|
372 | 372 | return d_ptr->m_attachedToChart; |
|
373 | 373 | } |
|
374 | 374 | |
|
375 | 375 | /*! |
|
376 | 376 | Sets the visibility of legend background to \a visible |
|
377 | 377 | */ |
|
378 | 378 | void QLegend::setBackgroundVisible(bool visible) |
|
379 | 379 | { |
|
380 | 380 | if (d_ptr->m_backgroundVisible != visible) { |
|
381 | 381 | d_ptr->m_backgroundVisible = visible; |
|
382 | 382 | update(); |
|
383 | 383 | emit backgroundVisibleChanged(visible); |
|
384 | 384 | } |
|
385 | 385 | } |
|
386 | 386 | |
|
387 | 387 | /*! |
|
388 | 388 | Returns the visibility of legend background |
|
389 | 389 | */ |
|
390 | 390 | bool QLegend::isBackgroundVisible() const |
|
391 | 391 | { |
|
392 | 392 | return d_ptr->m_backgroundVisible; |
|
393 | 393 | } |
|
394 | 394 | |
|
395 | 395 | /*! |
|
396 | 396 | \internal \a event see QGraphicsWidget for details |
|
397 | 397 | */ |
|
398 | 398 | void QLegend::hideEvent(QHideEvent *event) |
|
399 | 399 | { |
|
400 | 400 | if (isAttachedToChart()) |
|
401 | 401 | d_ptr->m_presenter->layout()->invalidate(); |
|
402 | 402 | QGraphicsWidget::hideEvent(event); |
|
403 | 403 | } |
|
404 | 404 | /*! |
|
405 | 405 | \internal \a event see QGraphicsWidget for details |
|
406 | 406 | */ |
|
407 | 407 | void QLegend::showEvent(QShowEvent *event) |
|
408 | 408 | { |
|
409 | 409 | if (isAttachedToChart()) { |
|
410 | 410 | d_ptr->items()->setVisible(false); |
|
411 | 411 | layout()->invalidate(); |
|
412 | 412 | } |
|
413 | 413 | QGraphicsWidget::showEvent(event); |
|
414 | 414 | //layout activation will show the items |
|
415 | 415 | } |
|
416 | 416 | |
|
417 | 417 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
418 | 418 | |
|
419 | 419 | QLegendPrivate::QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q) |
|
420 | 420 | : q_ptr(q), |
|
421 | 421 | m_presenter(presenter), |
|
422 | 422 | m_layout(new LegendLayout(q)), |
|
423 | 423 | m_chart(chart), |
|
424 | 424 | m_items(new QGraphicsItemGroup(q)), |
|
425 | 425 | m_alignment(Qt::AlignTop), |
|
426 | 426 | m_brush(QBrush()), |
|
427 | 427 | m_pen(QPen()), |
|
428 | 428 | m_labelBrush(QBrush()), |
|
429 | 429 | m_diameter(5), |
|
430 | 430 | m_attachedToChart(true), |
|
431 | 431 | m_backgroundVisible(false) |
|
432 | 432 | { |
|
433 | 433 | |
|
434 | 434 | } |
|
435 | 435 | |
|
436 | 436 | QLegendPrivate::~QLegendPrivate() |
|
437 | 437 | { |
|
438 | 438 | |
|
439 | 439 | } |
|
440 | 440 | |
|
441 | 441 | void QLegendPrivate::setOffset(qreal x, qreal y) |
|
442 | 442 | { |
|
443 | 443 | m_layout->setOffset(x, y); |
|
444 | 444 | } |
|
445 | 445 | |
|
446 | 446 | QPointF QLegendPrivate::offset() const |
|
447 | 447 | { |
|
448 | 448 | return m_layout->offset(); |
|
449 | 449 | } |
|
450 | 450 | |
|
451 | 451 | int QLegendPrivate::roundness(qreal size) |
|
452 | 452 | { |
|
453 | 453 | return 100 * m_diameter / int(size); |
|
454 | 454 | } |
|
455 | 455 | |
|
456 | 456 | void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain) |
|
457 | 457 | { |
|
458 | 458 | Q_UNUSED(domain) |
|
459 | 459 | |
|
460 | 460 | QList<LegendMarker *> markers = series->d_ptr->createLegendMarker(q_ptr); |
|
461 | 461 | |
|
462 | 462 | foreach (LegendMarker *marker, markers) { |
|
463 | 463 | marker->setFont(m_font); |
|
464 | 464 | marker->setLabelBrush(m_labelBrush); |
|
465 | 465 | marker->setVisible(series->isVisible()); |
|
466 | 466 | m_items->addToGroup(marker); |
|
467 | 467 | m_markers << marker; |
|
468 | 468 | } |
|
469 | 469 | |
|
470 | 470 | QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged())); |
|
471 | 471 | QObject::connect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged())); |
|
472 | 472 | |
|
473 | 473 | m_items->setVisible(false); |
|
474 | 474 | m_layout->invalidate(); |
|
475 | 475 | } |
|
476 | 476 | |
|
477 | 477 | void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series) |
|
478 | 478 | { |
|
479 | 479 | foreach (LegendMarker *marker, m_markers) { |
|
480 | 480 | if (marker->series() == series) { |
|
481 | 481 | delete marker; |
|
482 | 482 | m_markers.removeAll(marker); |
|
483 | 483 | } |
|
484 | 484 | } |
|
485 | 485 | |
|
486 | 486 | QObject::disconnect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged())); |
|
487 | 487 | QObject::disconnect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged())); |
|
488 | 488 | m_layout->invalidate(); |
|
489 | 489 | } |
|
490 | 490 | |
|
491 | 491 | void QLegendPrivate::handleSeriesVisibleChanged() |
|
492 | 492 | { |
|
493 | 493 | QAbstractSeries *series = qobject_cast<QAbstractSeries *> (sender()); |
|
494 | 494 | Q_ASSERT(series); |
|
495 | 495 | |
|
496 | 496 | foreach (LegendMarker *marker, m_markers) { |
|
497 | 497 | if (marker->series() == series) |
|
498 | 498 | marker->setVisible(series->isVisible()); |
|
499 | 499 | } |
|
500 | 500 | m_layout->invalidate(); |
|
501 | 501 | } |
|
502 | 502 | |
|
503 | 503 | void QLegendPrivate::handleCountChanged() |
|
504 | 504 | { |
|
505 | 505 | QAbstractSeriesPrivate *series = qobject_cast<QAbstractSeriesPrivate *> (sender()); |
|
506 | 506 | Q_ASSERT(series); |
|
507 | 507 | handleSeriesRemoved(series->q_ptr); |
|
508 | 508 | handleSeriesAdded(series->q_ptr, 0); |
|
509 | 509 | } |
|
510 | 510 | |
|
511 | 511 | #include "moc_qlegend.cpp" |
|
512 | 512 | #include "moc_qlegend_p.cpp" |
|
513 | 513 | |
|
514 | 514 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now