@@ -1,533 +1,532 | |||
|
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 "qchart.h" |
|
22 | 22 | #include "qchart_p.h" |
|
23 | 23 | #include "qchartaxis.h" |
|
24 | 24 | #include "chartpresenter_p.h" |
|
25 | 25 | #include "chartdataset_p.h" |
|
26 | 26 | #include "charttheme_p.h" |
|
27 | 27 | #include "chartanimator_p.h" |
|
28 | 28 | //series |
|
29 | 29 | #include "qbarseries.h" |
|
30 | 30 | #include "qstackedbarseries.h" |
|
31 | 31 | #include "qpercentbarseries.h" |
|
32 | 32 | #include "qlineseries.h" |
|
33 | 33 | #include "qareaseries.h" |
|
34 | 34 | #include "qpieseries.h" |
|
35 | 35 | #include "qscatterseries.h" |
|
36 | 36 | #include "qsplineseries.h" |
|
37 | 37 | //items |
|
38 | 38 | #include "axisitem_p.h" |
|
39 | 39 | #include "areachartitem_p.h" |
|
40 | 40 | #include "barchartitem_p.h" |
|
41 | 41 | #include "stackedbarchartitem_p.h" |
|
42 | 42 | #include "percentbarchartitem_p.h" |
|
43 | 43 | #include "linechartitem_p.h" |
|
44 | 44 | #include "piechartitem_p.h" |
|
45 | 45 | #include "scatterchartitem_p.h" |
|
46 | 46 | #include "splinechartitem_p.h" |
|
47 | 47 | |
|
48 | 48 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
49 | 49 | |
|
50 | 50 | ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart), |
|
51 | 51 | m_chart(chart), |
|
52 | 52 | m_animator(0), |
|
53 | 53 | m_dataset(dataset), |
|
54 | 54 | m_chartTheme(0), |
|
55 | 55 | m_chartRect(QRectF(QPoint(0,0),m_chart->size())), |
|
56 | 56 | m_options(QChart::NoAnimation), |
|
57 | 57 | m_themeForce(false), |
|
58 | 58 | m_minLeftMargin(0), |
|
59 | 59 | m_minBottomMargin(0), |
|
60 | 60 | m_backgroundItem(0), |
|
61 | 61 | m_titleItem(0), |
|
62 | 62 | m_marginBig(60), |
|
63 | 63 | m_marginSmall(20), |
|
64 | 64 | m_marginTiny(10), |
|
65 | 65 | m_chartMargins(QRect(m_marginBig,m_marginBig,0,0)) |
|
66 | 66 | { |
|
67 | 67 | createConnections(); |
|
68 | setTheme(QChart::ChartThemeLight, false); | |
|
69 | 68 | } |
|
70 | 69 | |
|
71 | 70 | ChartPresenter::~ChartPresenter() |
|
72 | 71 | { |
|
73 | 72 | delete m_chartTheme; |
|
74 | 73 | } |
|
75 | 74 | |
|
76 | 75 | void ChartPresenter::createConnections() |
|
77 | 76 | { |
|
78 | 77 | QObject::connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),this,SLOT(handleSeriesAdded(QSeries*,Domain*))); |
|
79 | 78 | QObject::connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),this,SLOT(handleSeriesRemoved(QSeries*))); |
|
80 | 79 | QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*,Domain*)),this,SLOT(handleAxisAdded(QChartAxis*,Domain*))); |
|
81 | 80 | QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*))); |
|
82 | 81 | } |
|
83 | 82 | |
|
84 | 83 | void ChartPresenter::setGeometry(const QRectF& rect) |
|
85 | 84 | { |
|
86 | 85 | m_rect = rect; |
|
87 | 86 | Q_ASSERT(m_rect.isValid()); |
|
88 | 87 | updateLayout(); |
|
89 | 88 | } |
|
90 | 89 | |
|
91 | 90 | void ChartPresenter::setMinimumMarginWidth(Axis* axis, qreal width) |
|
92 | 91 | { |
|
93 | 92 | switch(axis->axisType()){ |
|
94 | 93 | case Axis::X_AXIS: |
|
95 | 94 | { |
|
96 | 95 | if(width>m_chartRect.width()+ m_chartMargins.left()) { |
|
97 | 96 | m_minLeftMargin= width - m_chartRect.width(); |
|
98 | 97 | updateLayout(); |
|
99 | 98 | } |
|
100 | 99 | break; |
|
101 | 100 | } |
|
102 | 101 | case Axis::Y_AXIS: |
|
103 | 102 | { |
|
104 | 103 | |
|
105 | 104 | if(m_minLeftMargin!=width){ |
|
106 | 105 | m_minLeftMargin= width; |
|
107 | 106 | updateLayout(); |
|
108 | 107 | } |
|
109 | 108 | break; |
|
110 | 109 | } |
|
111 | 110 | |
|
112 | 111 | } |
|
113 | 112 | } |
|
114 | 113 | |
|
115 | 114 | void ChartPresenter::setMinimumMarginHeight(Axis* axis, qreal height) |
|
116 | 115 | { |
|
117 | 116 | switch(axis->axisType()){ |
|
118 | 117 | case Axis::X_AXIS: |
|
119 | 118 | { |
|
120 | 119 | if(m_minBottomMargin!=height) { |
|
121 | 120 | m_minBottomMargin= height; |
|
122 | 121 | updateLayout(); |
|
123 | 122 | } |
|
124 | 123 | break; |
|
125 | 124 | } |
|
126 | 125 | case Axis::Y_AXIS: |
|
127 | 126 | { |
|
128 | 127 | |
|
129 | 128 | if(height>m_chartMargins.bottom()+m_chartRect.height()){ |
|
130 | 129 | m_minBottomMargin= height - m_chartRect.height(); |
|
131 | 130 | updateLayout(); |
|
132 | 131 | } |
|
133 | 132 | break; |
|
134 | 133 | } |
|
135 | 134 | |
|
136 | 135 | } |
|
137 | 136 | } |
|
138 | 137 | |
|
139 | 138 | void ChartPresenter::handleAxisAdded(QChartAxis* axis,Domain* domain) |
|
140 | 139 | { |
|
141 | 140 | Axis* item = new Axis(axis,this,axis==m_dataset->axisX()?Axis::X_AXIS : Axis::Y_AXIS); |
|
142 | 141 | |
|
143 | 142 | if(m_options.testFlag(QChart::GridAxisAnimations)){ |
|
144 | 143 | m_animator->addAnimation(item); |
|
145 | 144 | } |
|
146 | 145 | |
|
147 | 146 | if(axis==m_dataset->axisX()){ |
|
148 | 147 | m_chartTheme->decorate(axis,true,m_themeForce); |
|
149 | 148 | QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int))); |
|
150 | 149 | //initialize |
|
151 | 150 | item->handleRangeChanged(domain->minX(),domain->maxX(),domain->tickXCount()); |
|
152 | 151 | |
|
153 | 152 | } |
|
154 | 153 | else{ |
|
155 | 154 | m_chartTheme->decorate(axis,false,m_themeForce); |
|
156 | 155 | QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int))); |
|
157 | 156 | //initialize |
|
158 | 157 | item->handleRangeChanged(domain->minY(),domain->maxY(),domain->tickYCount()); |
|
159 | 158 | } |
|
160 | 159 | |
|
161 | 160 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
162 | 161 | //initialize |
|
163 | 162 | item->handleGeometryChanged(m_chartRect); |
|
164 | 163 | m_axisItems.insert(axis, item); |
|
165 | 164 | } |
|
166 | 165 | |
|
167 | 166 | void ChartPresenter::handleAxisRemoved(QChartAxis* axis) |
|
168 | 167 | { |
|
169 | 168 | Axis* item = m_axisItems.take(axis); |
|
170 | 169 | Q_ASSERT(item); |
|
171 | 170 | if(m_animator) m_animator->removeAnimation(item); |
|
172 | 171 | delete item; |
|
173 | 172 | } |
|
174 | 173 | |
|
175 | 174 | |
|
176 | 175 | void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) |
|
177 | 176 | { |
|
178 | 177 | Chart *item = 0 ; |
|
179 | 178 | |
|
180 | 179 | switch(series->type()) |
|
181 | 180 | { |
|
182 | 181 | case QSeries::SeriesTypeLine: { |
|
183 | 182 | |
|
184 | 183 | QLineSeries* lineSeries = static_cast<QLineSeries*>(series); |
|
185 | 184 | LineChartItem* line = new LineChartItem(lineSeries,this); |
|
186 | 185 | if(m_options.testFlag(QChart::SeriesAnimations)) { |
|
187 | 186 | m_animator->addAnimation(line); |
|
188 | 187 | } |
|
189 | 188 | m_chartTheme->decorate(lineSeries, m_dataset->seriesIndex(series),m_themeForce); |
|
190 | 189 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),line,SLOT(handleGeometryChanged(const QRectF&))); |
|
191 | 190 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),line,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); |
|
192 | 191 | item = line; |
|
193 | 192 | break; |
|
194 | 193 | } |
|
195 | 194 | |
|
196 | 195 | case QSeries::SeriesTypeArea: { |
|
197 | 196 | |
|
198 | 197 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); |
|
199 | 198 | AreaChartItem* area = new AreaChartItem(areaSeries,this); |
|
200 | 199 | if(m_options.testFlag(QChart::SeriesAnimations)) { |
|
201 | 200 | m_animator->addAnimation(area->upperLineItem()); |
|
202 | 201 | if(areaSeries->lowerSeries()) m_animator->addAnimation(area->lowerLineItem()); |
|
203 | 202 | } |
|
204 | 203 | m_chartTheme->decorate(areaSeries, m_dataset->seriesIndex(series),m_themeForce); |
|
205 | 204 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),area,SLOT(handleGeometryChanged(const QRectF&))); |
|
206 | 205 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),area,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); |
|
207 | 206 | item=area; |
|
208 | 207 | break; |
|
209 | 208 | } |
|
210 | 209 | |
|
211 | 210 | case QSeries::SeriesTypeBar: { |
|
212 | 211 | QBarSeries* barSeries = static_cast<QBarSeries*>(series); |
|
213 | 212 | BarChartItem* bar = new BarChartItem(barSeries,this); |
|
214 | 213 | if(m_options.testFlag(QChart::SeriesAnimations)) { |
|
215 | 214 | m_animator->addAnimation(bar); |
|
216 | 215 | } |
|
217 | 216 | m_chartTheme->decorate(barSeries, m_dataset->seriesIndex(barSeries),m_themeForce); |
|
218 | 217 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&))); |
|
219 | 218 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); |
|
220 | 219 | item=bar; |
|
221 | 220 | break; |
|
222 | 221 | } |
|
223 | 222 | |
|
224 | 223 | case QSeries::SeriesTypeStackedBar: { |
|
225 | 224 | QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); |
|
226 | 225 | StackedBarChartItem* bar = new StackedBarChartItem(stackedBarSeries,this); |
|
227 | 226 | if(m_options.testFlag(QChart::SeriesAnimations)) { |
|
228 | 227 | m_animator->addAnimation(bar); |
|
229 | 228 | } |
|
230 | 229 | m_chartTheme->decorate(stackedBarSeries, m_dataset->seriesIndex(stackedBarSeries),m_themeForce); |
|
231 | 230 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&))); |
|
232 | 231 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); |
|
233 | 232 | item=bar; |
|
234 | 233 | break; |
|
235 | 234 | } |
|
236 | 235 | |
|
237 | 236 | case QSeries::SeriesTypePercentBar: { |
|
238 | 237 | QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); |
|
239 | 238 | PercentBarChartItem* bar = new PercentBarChartItem(percentBarSeries,this); |
|
240 | 239 | if(m_options.testFlag(QChart::SeriesAnimations)) { |
|
241 | 240 | m_animator->addAnimation(bar); |
|
242 | 241 | } |
|
243 | 242 | m_chartTheme->decorate(percentBarSeries, m_dataset->seriesIndex(percentBarSeries),m_themeForce); |
|
244 | 243 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&))); |
|
245 | 244 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); |
|
246 | 245 | item=bar; |
|
247 | 246 | break; |
|
248 | 247 | } |
|
249 | 248 | |
|
250 | 249 | case QSeries::SeriesTypeScatter: { |
|
251 | 250 | QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series); |
|
252 | 251 | ScatterChartItem *scatter = new ScatterChartItem(scatterSeries,this); |
|
253 | 252 | if(m_options.testFlag(QChart::SeriesAnimations)) { |
|
254 | 253 | m_animator->addAnimation(scatter); |
|
255 | 254 | } |
|
256 | 255 | m_chartTheme->decorate(scatterSeries, m_dataset->seriesIndex(series),m_themeForce); |
|
257 | 256 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),scatter,SLOT(handleGeometryChanged(const QRectF&))); |
|
258 | 257 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),scatter,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); |
|
259 | 258 | item = scatter; |
|
260 | 259 | break; |
|
261 | 260 | } |
|
262 | 261 | |
|
263 | 262 | case QSeries::SeriesTypePie: { |
|
264 | 263 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); |
|
265 | 264 | PieChartItem* pie = new PieChartItem(pieSeries, this); |
|
266 | 265 | if(m_options.testFlag(QChart::SeriesAnimations)) { |
|
267 | 266 | m_animator->addAnimation(pie); |
|
268 | 267 | } |
|
269 | 268 | m_chartTheme->decorate(pieSeries, m_dataset->seriesIndex(series),m_themeForce); |
|
270 | 269 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),pie,SLOT(handleGeometryChanged(const QRectF&))); |
|
271 | 270 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),pie,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); |
|
272 | 271 | // Hide all from background when there is only piechart |
|
273 | 272 | // TODO: refactor this ugly code... should be one setting for this |
|
274 | 273 | if (m_chartItems.count() == 0) { |
|
275 | 274 | m_chart->axisX()->hide(); |
|
276 | 275 | m_chart->axisY()->hide(); |
|
277 | 276 | } |
|
278 | 277 | item=pie; |
|
279 | 278 | break; |
|
280 | 279 | } |
|
281 | 280 | |
|
282 | 281 | case QSeries::SeriesTypeSpline: { |
|
283 | 282 | QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series); |
|
284 | 283 | SplineChartItem* spline = new SplineChartItem(splineSeries, this); |
|
285 | 284 | if(m_options.testFlag(QChart::SeriesAnimations)) { |
|
286 | 285 | m_animator->addAnimation(spline); |
|
287 | 286 | } |
|
288 | 287 | m_chartTheme->decorate(splineSeries, m_dataset->seriesIndex(series),m_themeForce); |
|
289 | 288 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),spline,SLOT(handleGeometryChanged(const QRectF&))); |
|
290 | 289 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),spline,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); |
|
291 | 290 | item=spline; |
|
292 | 291 | break; |
|
293 | 292 | } |
|
294 | 293 | default: { |
|
295 | 294 | qDebug()<< "Series type" << series->type() << "not implemented."; |
|
296 | 295 | break; |
|
297 | 296 | } |
|
298 | 297 | } |
|
299 | 298 | |
|
300 | 299 | //initialize |
|
301 | 300 | item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY()); |
|
302 | 301 | if(m_chartRect.isValid()) item->handleGeometryChanged(m_chartRect); |
|
303 | 302 | m_chartItems.insert(series,item); |
|
304 | 303 | } |
|
305 | 304 | |
|
306 | 305 | void ChartPresenter::handleSeriesRemoved(QSeries* series) |
|
307 | 306 | { |
|
308 | 307 | Chart* item = m_chartItems.take(series); |
|
309 | 308 | Q_ASSERT(item); |
|
310 | 309 | if(m_animator) { |
|
311 | 310 | //small hack to handle area animations |
|
312 | 311 | if(series->type()==QSeries::SeriesTypeArea){ |
|
313 | 312 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); |
|
314 | 313 | AreaChartItem* area = static_cast<AreaChartItem*>(item); |
|
315 | 314 | m_animator->removeAnimation(area->upperLineItem()); |
|
316 | 315 | if(areaSeries->lowerSeries()) m_animator->removeAnimation(area->lowerLineItem()); |
|
317 | 316 | }else |
|
318 | 317 | m_animator->removeAnimation(item); |
|
319 | 318 | } |
|
320 | 319 | delete item; |
|
321 | 320 | } |
|
322 | 321 | |
|
323 | 322 | void ChartPresenter::setTheme(QChart::ChartTheme theme,bool force) |
|
324 | 323 | { |
|
325 | 324 | if(m_chartTheme && m_chartTheme->id() == theme) return; |
|
326 | 325 | delete m_chartTheme; |
|
327 | 326 | m_themeForce = force; |
|
328 | 327 | m_chartTheme = ChartTheme::createTheme(theme); |
|
329 | 328 | m_chartTheme->decorate(m_chart,m_themeForce); |
|
330 | 329 | m_chartTheme->decorate(m_chart->legend(),m_themeForce); |
|
331 | 330 | resetAllElements(); |
|
332 | 331 | } |
|
333 | 332 | |
|
334 | 333 | QChart::ChartTheme ChartPresenter::theme() |
|
335 | 334 | { |
|
336 | 335 | return m_chartTheme->id(); |
|
337 | 336 | } |
|
338 | 337 | |
|
339 | 338 | void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options) |
|
340 | 339 | { |
|
341 | 340 | if(m_options!=options) { |
|
342 | 341 | |
|
343 | 342 | m_options=options; |
|
344 | 343 | |
|
345 | 344 | if(m_options!=QChart::NoAnimation && !m_animator) { |
|
346 | 345 | m_animator= new ChartAnimator(this); |
|
347 | 346 | |
|
348 | 347 | } |
|
349 | 348 | resetAllElements(); |
|
350 | 349 | } |
|
351 | 350 | |
|
352 | 351 | } |
|
353 | 352 | |
|
354 | 353 | void ChartPresenter::resetAllElements() |
|
355 | 354 | { |
|
356 | 355 | QList<QChartAxis*> axisList = m_axisItems.uniqueKeys(); |
|
357 | 356 | QList<QSeries*> seriesList = m_chartItems.uniqueKeys(); |
|
358 | 357 | |
|
359 | 358 | foreach(QChartAxis* axis, axisList) { |
|
360 | 359 | handleAxisRemoved(axis); |
|
361 | 360 | handleAxisAdded(axis,m_dataset->domain(axis)); |
|
362 | 361 | } |
|
363 | 362 | foreach(QSeries* series, seriesList) { |
|
364 | 363 | handleSeriesRemoved(series); |
|
365 | 364 | handleSeriesAdded(series,m_dataset->domain(series)); |
|
366 | 365 | } |
|
367 | 366 | } |
|
368 | 367 | |
|
369 | 368 | void ChartPresenter::zoomIn() |
|
370 | 369 | { |
|
371 | 370 | QRectF rect = chartGeometry(); |
|
372 | 371 | rect.setWidth(rect.width()/2); |
|
373 | 372 | rect.setHeight(rect.height()/2); |
|
374 | 373 | rect.moveCenter(chartGeometry().center()); |
|
375 | 374 | zoomIn(rect); |
|
376 | 375 | } |
|
377 | 376 | |
|
378 | 377 | void ChartPresenter::zoomIn(const QRectF& rect) |
|
379 | 378 | { |
|
380 | 379 | QRectF r = rect.normalized(); |
|
381 | 380 | r.translate(-m_chartMargins.topLeft()); |
|
382 | 381 | if(m_animator) { |
|
383 | 382 | |
|
384 | 383 | QPointF point(r.center().x()/chartGeometry().width(),r.center().y()/chartGeometry().height()); |
|
385 | 384 | m_animator->setState(ChartAnimator::ZoomInState,point); |
|
386 | 385 | } |
|
387 | 386 | m_dataset->zoomInDomain(r,chartGeometry().size()); |
|
388 | 387 | if(m_animator) { |
|
389 | 388 | m_animator->setState(ChartAnimator::ShowState); |
|
390 | 389 | } |
|
391 | 390 | } |
|
392 | 391 | |
|
393 | 392 | void ChartPresenter::zoomOut() |
|
394 | 393 | { |
|
395 | 394 | if(m_animator) |
|
396 | 395 | { |
|
397 | 396 | m_animator->setState(ChartAnimator::ZoomOutState); |
|
398 | 397 | } |
|
399 | 398 | |
|
400 | 399 | QSizeF size = chartGeometry().size(); |
|
401 | 400 | QRectF rect = chartGeometry(); |
|
402 | 401 | rect.translate(-m_chartMargins.topLeft()); |
|
403 | 402 | m_dataset->zoomOutDomain(rect.adjusted(size.width()/4,size.height()/4,-size.width()/4,-size.height()/4),size); |
|
404 | 403 | //m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size()); |
|
405 | 404 | |
|
406 | 405 | if(m_animator){ |
|
407 | 406 | m_animator->setState(ChartAnimator::ShowState); |
|
408 | 407 | } |
|
409 | 408 | } |
|
410 | 409 | |
|
411 | 410 | void ChartPresenter::scroll(int dx,int dy) |
|
412 | 411 | { |
|
413 | 412 | if(m_animator){ |
|
414 | 413 | if(dx<0) m_animator->setState(ChartAnimator::ScrollLeftState,QPointF()); |
|
415 | 414 | if(dx>0) m_animator->setState(ChartAnimator::ScrollRightState,QPointF()); |
|
416 | 415 | if(dy<0) m_animator->setState(ChartAnimator::ScrollUpState,QPointF()); |
|
417 | 416 | if(dy>0) m_animator->setState(ChartAnimator::ScrollDownState,QPointF()); |
|
418 | 417 | } |
|
419 | 418 | |
|
420 | 419 | m_dataset->scrollDomain(dx,dy,chartGeometry().size()); |
|
421 | 420 | |
|
422 | 421 | if(m_animator){ |
|
423 | 422 | m_animator->setState(ChartAnimator::ShowState); |
|
424 | 423 | } |
|
425 | 424 | } |
|
426 | 425 | |
|
427 | 426 | QChart::AnimationOptions ChartPresenter::animationOptions() const |
|
428 | 427 | { |
|
429 | 428 | return m_options; |
|
430 | 429 | } |
|
431 | 430 | |
|
432 | 431 | void ChartPresenter::updateLayout() |
|
433 | 432 | { |
|
434 | 433 | if (!m_rect.isValid()) return; |
|
435 | 434 | |
|
436 | 435 | // recalculate title size |
|
437 | 436 | |
|
438 | 437 | QSize titleSize; |
|
439 | 438 | int titlePadding=0; |
|
440 | 439 | |
|
441 | 440 | if (m_titleItem) { |
|
442 | 441 | titleSize= m_titleItem->boundingRect().size().toSize(); |
|
443 | 442 | } |
|
444 | 443 | |
|
445 | 444 | //defaults |
|
446 | 445 | m_chartMargins = QRect(QPoint(m_minLeftMargin>m_marginBig?m_minLeftMargin:m_marginBig,m_marginBig),QPoint(m_marginBig,m_minBottomMargin>m_marginBig?m_minBottomMargin:m_marginBig)); |
|
447 | 446 | titlePadding = m_chartMargins.top()/2; |
|
448 | 447 | |
|
449 | 448 | QLegend* legend = m_chart->d_ptr->m_legend; |
|
450 | 449 | |
|
451 | 450 | // recalculate legend position |
|
452 | 451 | if (legend->isAttachedToChart() && legend->isEnabled()) { |
|
453 | 452 | |
|
454 | 453 | QRect legendRect; |
|
455 | 454 | |
|
456 | 455 | // Reserve some space for legend |
|
457 | 456 | switch (legend->alignment()) { |
|
458 | 457 | |
|
459 | 458 | case QLegend::AlignmentTop: { |
|
460 | 459 | int ledgendSize = legend->minHeight(); |
|
461 | 460 | int topPadding = 2*m_marginTiny + titleSize.height() + ledgendSize + m_marginTiny; |
|
462 | 461 | m_chartMargins = QRect(QPoint(m_chartMargins.left(),topPadding),QPoint(m_chartMargins.right(),m_chartMargins.bottom())); |
|
463 | 462 | m_legendMargins = QRect(QPoint(m_chartMargins.left(),topPadding - (ledgendSize + m_marginTiny)),QPoint(m_chartMargins.right(),m_rect.height()-topPadding + m_marginTiny)); |
|
464 | 463 | titlePadding = m_marginTiny + m_marginTiny; |
|
465 | 464 | break; |
|
466 | 465 | } |
|
467 | 466 | case QLegend::AlignmentBottom: { |
|
468 | 467 | int ledgendSize = legend->minHeight(); |
|
469 | 468 | int bottomPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minBottomMargin; |
|
470 | 469 | m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(m_chartMargins.right(),bottomPadding)); |
|
471 | 470 | m_legendMargins = QRect(QPoint(m_chartMargins.left(),m_rect.height()-bottomPadding + m_marginTiny + m_minBottomMargin),QPoint(m_chartMargins.right(),m_marginTiny + m_marginSmall)); |
|
472 | 471 | titlePadding = m_chartMargins.top()/2; |
|
473 | 472 | break; |
|
474 | 473 | } |
|
475 | 474 | case QLegend::AlignmentLeft: { |
|
476 | 475 | int ledgendSize = legend->minWidht(); |
|
477 | 476 | int leftPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minLeftMargin; |
|
478 | 477 | m_chartMargins = QRect(QPoint(leftPadding,m_chartMargins.top()),QPoint(m_chartMargins.right(),m_chartMargins.bottom())); |
|
479 | 478 | m_legendMargins = QRect(QPoint(m_marginTiny + m_marginSmall,m_chartMargins.top()),QPoint(m_rect.width()-leftPadding + m_marginTiny + m_minLeftMargin,m_chartMargins.bottom())); |
|
480 | 479 | titlePadding = m_chartMargins.top()/2; |
|
481 | 480 | break; |
|
482 | 481 | } |
|
483 | 482 | case QLegend::AlignmentRight: { |
|
484 | 483 | int ledgendSize = legend->minWidht(); |
|
485 | 484 | int rightPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny; |
|
486 | 485 | m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(rightPadding,m_chartMargins.bottom())); |
|
487 | 486 | m_legendMargins = QRect(QPoint(m_rect.width()- rightPadding+ m_marginTiny ,m_chartMargins.top()),QPoint(m_marginTiny + m_marginSmall,m_chartMargins.bottom())); |
|
488 | 487 | titlePadding = m_chartMargins.top()/2; |
|
489 | 488 | break; |
|
490 | 489 | } |
|
491 | 490 | default: { |
|
492 | 491 | break; |
|
493 | 492 | } |
|
494 | 493 | } |
|
495 | 494 | } |
|
496 | 495 | |
|
497 | 496 | // recalculate title position |
|
498 | 497 | if (m_titleItem) { |
|
499 | 498 | QPointF center = m_rect.center() -m_titleItem->boundingRect().center(); |
|
500 | 499 | m_titleItem->setPos(center.x(),titlePadding); |
|
501 | 500 | } |
|
502 | 501 | |
|
503 | 502 | //recalculate background gradient |
|
504 | 503 | if (m_backgroundItem) { |
|
505 | 504 | m_backgroundItem->setRect(m_rect.adjusted(m_marginTiny,m_marginTiny, -m_marginTiny, -m_marginTiny)); |
|
506 | 505 | } |
|
507 | 506 | |
|
508 | 507 | m_chartRect = m_rect.adjusted(m_chartMargins.left(),m_chartMargins.top(),-m_chartMargins.right(),-m_chartMargins.bottom()); |
|
509 | 508 | |
|
510 | 509 | emit geometryChanged(m_chartRect); |
|
511 | 510 | legend->setGeometry(m_rect.adjusted(m_legendMargins.left(),m_legendMargins.top(),-m_legendMargins.right(),-m_legendMargins.bottom())); |
|
512 | 511 | } |
|
513 | 512 | |
|
514 | 513 | void ChartPresenter::createChartBackgroundItem() |
|
515 | 514 | { |
|
516 | 515 | if (!m_backgroundItem) { |
|
517 | 516 | m_backgroundItem = new ChartBackground(rootItem()); |
|
518 | 517 | m_backgroundItem->setPen(Qt::NoPen); |
|
519 | 518 | m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue); |
|
520 | 519 | } |
|
521 | 520 | } |
|
522 | 521 | |
|
523 | 522 | void ChartPresenter::createChartTitleItem() |
|
524 | 523 | { |
|
525 | 524 | if (!m_titleItem) { |
|
526 | 525 | m_titleItem = new QGraphicsSimpleTextItem(rootItem()); |
|
527 | 526 | m_titleItem->setZValue(ChartPresenter::BackgroundZValue); |
|
528 | 527 | } |
|
529 | 528 | } |
|
530 | 529 | |
|
531 | 530 | #include "moc_chartpresenter_p.cpp" |
|
532 | 531 | |
|
533 | 532 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,351 +1,351 | |||
|
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 "qchart.h" |
|
22 | 22 | #include "qchart_p.h" |
|
23 | 23 | #include <QGraphicsScene> |
|
24 | 24 | #include <QGraphicsSceneResizeEvent> |
|
25 | 25 | |
|
26 | 26 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
27 | 27 | |
|
28 | 28 | /*! |
|
29 | 29 | \enum QChart::ChartTheme |
|
30 | 30 | |
|
31 | 31 | This enum describes the theme used by the chart. |
|
32 | 32 | |
|
33 | 33 | \value ChartThemeLight The default theme |
|
34 | 34 | \value ChartThemeBlueCerulean |
|
35 | 35 | \value ChartThemeDark |
|
36 | 36 | \value ChartThemeBrownSand |
|
37 | 37 | \value ChartThemeBlueNcs |
|
38 | 38 | \value ChartThemeHighContrast |
|
39 | 39 | \value ChartThemeBlueIcy |
|
40 | 40 | \value ChartThemeCount Not really a theme; the total count of themes. |
|
41 | 41 | */ |
|
42 | 42 | |
|
43 | 43 | /*! |
|
44 | 44 | \enum QChart::AnimationOption |
|
45 | 45 | |
|
46 | 46 | For enabling/disabling animations. Defaults to NoAnimation. |
|
47 | 47 | |
|
48 | 48 | \value NoAnimation |
|
49 | 49 | \value GridAxisAnimations |
|
50 | 50 | \value SeriesAnimations |
|
51 | 51 | \value AllAnimations |
|
52 | 52 | */ |
|
53 | 53 | |
|
54 | 54 | /*! |
|
55 | 55 | \class QChart |
|
56 | 56 | \brief QtCommercial chart API. |
|
57 | 57 | |
|
58 | 58 | QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical |
|
59 | 59 | representation of different types of QChartSeries and other chart related objects like |
|
60 | 60 | QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the |
|
61 | 61 | convenience class QChartView instead of QChart. |
|
62 | 62 | \sa QChartView |
|
63 | 63 | */ |
|
64 | 64 | |
|
65 | 65 | /*! |
|
66 | 66 | Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor. |
|
67 | 67 | */ |
|
68 | 68 | QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags), |
|
69 | 69 | d_ptr(new QChartPrivate()) |
|
70 | 70 | { |
|
71 | 71 | d_ptr->m_legend = new QLegend(this); |
|
72 | 72 | d_ptr->m_dataset = new ChartDataSet(this); |
|
73 | 73 | d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset); |
|
74 |
d_ptr->m_presenter->setTheme(QChart::ChartTheme |
|
|
74 | d_ptr->m_presenter->setTheme(QChart::ChartThemeLight, false); | |
|
75 | 75 | //TODO:fix me setMinimumSize(d_ptr->m_padding.left() * 3, d_ptr->m_padding.top() * 3); |
|
76 | 76 | connect(d_ptr->m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),d_ptr->m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*))); |
|
77 | 77 | connect(d_ptr->m_dataset,SIGNAL(seriesRemoved(QSeries*)),d_ptr->m_legend,SLOT(handleSeriesRemoved(QSeries*))); |
|
78 | 78 | } |
|
79 | 79 | |
|
80 | 80 | /*! |
|
81 | 81 | Destroys the object and it's children, like QChartSeries and QChartAxis object added to it. |
|
82 | 82 | */ |
|
83 | 83 | QChart::~QChart() |
|
84 | 84 | { |
|
85 | 85 | //delete first presenter , since this is a root of all the graphical items |
|
86 | 86 | delete d_ptr->m_presenter; |
|
87 | 87 | d_ptr->m_presenter=0; |
|
88 | 88 | } |
|
89 | 89 | |
|
90 | 90 | /*! |
|
91 | 91 | Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects. |
|
92 | 92 | If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and |
|
93 | 93 | the y axis). |
|
94 | 94 | */ |
|
95 | 95 | void QChart::addSeries(QSeries* series, QChartAxis* axisY) |
|
96 | 96 | { |
|
97 | 97 | d_ptr->m_dataset->addSeries(series, axisY); |
|
98 | 98 | } |
|
99 | 99 | |
|
100 | 100 | /*! |
|
101 | 101 | Removes the \a series specified in a perameter from the QChartView. |
|
102 | 102 | It releses its ownership of the specified QChartSeries object. |
|
103 | 103 | It does not delete the pointed QChartSeries data object |
|
104 | 104 | \sa addSeries(), removeAllSeries() |
|
105 | 105 | */ |
|
106 | 106 | void QChart::removeSeries(QSeries* series) |
|
107 | 107 | { |
|
108 | 108 | d_ptr->m_dataset->removeSeries(series); |
|
109 | 109 | } |
|
110 | 110 | |
|
111 | 111 | /*! |
|
112 | 112 | Removes all the QChartSeries that have been added to the QChartView |
|
113 | 113 | It also deletes the pointed QChartSeries data objects |
|
114 | 114 | \sa addSeries(), removeSeries() |
|
115 | 115 | */ |
|
116 | 116 | void QChart::removeAllSeries() |
|
117 | 117 | { |
|
118 | 118 | d_ptr->m_dataset->removeAllSeries(); |
|
119 | 119 | } |
|
120 | 120 | |
|
121 | 121 | /*! |
|
122 | 122 | Sets the \a brush that is used for painting the background of the chart area. |
|
123 | 123 | */ |
|
124 | 124 | void QChart::setBackgroundBrush(const QBrush& brush) |
|
125 | 125 | { |
|
126 | 126 | d_ptr->m_presenter->createChartBackgroundItem(); |
|
127 | 127 | d_ptr->m_presenter->m_backgroundItem->setBrush(brush); |
|
128 | 128 | d_ptr->m_presenter->m_backgroundItem->update(); |
|
129 | 129 | } |
|
130 | 130 | |
|
131 | 131 | QBrush QChart::backgroundBrush() const |
|
132 | 132 | { |
|
133 | 133 | if (!d_ptr->m_presenter->m_backgroundItem) return QBrush(); |
|
134 | 134 | return (d_ptr->m_presenter->m_backgroundItem)->brush(); |
|
135 | 135 | } |
|
136 | 136 | |
|
137 | 137 | /*! |
|
138 | 138 | Sets the \a pen that is used for painting the background of the chart area. |
|
139 | 139 | */ |
|
140 | 140 | void QChart::setBackgroundPen(const QPen& pen) |
|
141 | 141 | { |
|
142 | 142 | d_ptr->m_presenter->createChartBackgroundItem(); |
|
143 | 143 | d_ptr->m_presenter->m_backgroundItem->setPen(pen); |
|
144 | 144 | d_ptr->m_presenter->m_backgroundItem->update(); |
|
145 | 145 | } |
|
146 | 146 | |
|
147 | 147 | QPen QChart::backgroundPen() const |
|
148 | 148 | { |
|
149 | 149 | if (!d_ptr->m_presenter->m_backgroundItem) return QPen(); |
|
150 | 150 | return d_ptr->m_presenter->m_backgroundItem->pen(); |
|
151 | 151 | } |
|
152 | 152 | |
|
153 | 153 | /*! |
|
154 | 154 | Sets the chart \a title. The description text that is drawn above the chart. |
|
155 | 155 | */ |
|
156 | 156 | void QChart::setTitle(const QString& title) |
|
157 | 157 | { |
|
158 | 158 | d_ptr->m_presenter->createChartTitleItem(); |
|
159 | 159 | d_ptr->m_presenter->m_titleItem->setText(title); |
|
160 | 160 | d_ptr->m_presenter->updateLayout(); |
|
161 | 161 | } |
|
162 | 162 | |
|
163 | 163 | /*! |
|
164 | 164 | Returns the chart title. The description text that is drawn above the chart. |
|
165 | 165 | */ |
|
166 | 166 | QString QChart::title() const |
|
167 | 167 | { |
|
168 | 168 | if (d_ptr->m_presenter->m_titleItem) |
|
169 | 169 | return d_ptr->m_presenter->m_titleItem->text(); |
|
170 | 170 | else |
|
171 | 171 | return QString(); |
|
172 | 172 | } |
|
173 | 173 | |
|
174 | 174 | /*! |
|
175 | 175 | Sets the \a font that is used for rendering the description text that is rendered above the chart. |
|
176 | 176 | */ |
|
177 | 177 | void QChart::setTitleFont(const QFont& font) |
|
178 | 178 | { |
|
179 | 179 | d_ptr->m_presenter->createChartTitleItem(); |
|
180 | 180 | d_ptr->m_presenter->m_titleItem->setFont(font); |
|
181 | 181 | d_ptr->m_presenter->updateLayout(); |
|
182 | 182 | } |
|
183 | 183 | |
|
184 | 184 | /*! |
|
185 | 185 | Sets the \a brush used for rendering the title text. |
|
186 | 186 | */ |
|
187 | 187 | void QChart::setTitleBrush(const QBrush &brush) |
|
188 | 188 | { |
|
189 | 189 | d_ptr->m_presenter->createChartTitleItem(); |
|
190 | 190 | d_ptr->m_presenter->m_titleItem->setBrush(brush); |
|
191 | 191 | d_ptr->m_presenter->updateLayout(); |
|
192 | 192 | } |
|
193 | 193 | |
|
194 | 194 | /*! |
|
195 | 195 | Returns the brush used for rendering the title text. |
|
196 | 196 | */ |
|
197 | 197 | QBrush QChart::titleBrush() const |
|
198 | 198 | { |
|
199 | 199 | if (!d_ptr->m_presenter->m_titleItem) return QBrush(); |
|
200 | 200 | return d_ptr->m_presenter->m_titleItem->brush(); |
|
201 | 201 | } |
|
202 | 202 | |
|
203 | 203 | /*! |
|
204 | 204 | Sets the \a theme used by the chart for rendering the graphical representation of the data |
|
205 | 205 | \sa ChartTheme, chartTheme() |
|
206 | 206 | */ |
|
207 | 207 | void QChart::setTheme(QChart::ChartTheme theme) |
|
208 | 208 | { |
|
209 | 209 | d_ptr->m_presenter->setTheme(theme); |
|
210 | 210 | } |
|
211 | 211 | |
|
212 | 212 | /*! |
|
213 | 213 | Returns the theme enum used by the chart. |
|
214 | 214 | \sa ChartTheme, setChartTheme() |
|
215 | 215 | */ |
|
216 | 216 | QChart::ChartTheme QChart::theme() const |
|
217 | 217 | { |
|
218 | 218 | return d_ptr->m_presenter->theme(); |
|
219 | 219 | } |
|
220 | 220 | |
|
221 | 221 | /*! |
|
222 | 222 | Zooms in the view by a factor of 2 |
|
223 | 223 | */ |
|
224 | 224 | void QChart::zoomIn() |
|
225 | 225 | { |
|
226 | 226 | d_ptr->m_presenter->zoomIn(); |
|
227 | 227 | } |
|
228 | 228 | |
|
229 | 229 | /*! |
|
230 | 230 | Zooms in the view to a maximum level at which \a rect is still fully visible. |
|
231 | 231 | */ |
|
232 | 232 | void QChart::zoomIn(const QRectF& rect) |
|
233 | 233 | { |
|
234 | 234 | if (!rect.isValid()) return; |
|
235 | 235 | d_ptr->m_presenter->zoomIn(rect); |
|
236 | 236 | } |
|
237 | 237 | |
|
238 | 238 | /*! |
|
239 | 239 | Restores the view zoom level to the previous one. |
|
240 | 240 | */ |
|
241 | 241 | void QChart::zoomOut() |
|
242 | 242 | { |
|
243 | 243 | d_ptr->m_presenter->zoomOut(); |
|
244 | 244 | } |
|
245 | 245 | |
|
246 | 246 | /*! |
|
247 | 247 | Returns the pointer to the x axis object of the chart |
|
248 | 248 | */ |
|
249 | 249 | QChartAxis* QChart::axisX() const |
|
250 | 250 | { |
|
251 | 251 | return d_ptr->m_dataset->axisX(); |
|
252 | 252 | } |
|
253 | 253 | |
|
254 | 254 | /*! |
|
255 | 255 | Returns the pointer to the y axis object of the chart |
|
256 | 256 | */ |
|
257 | 257 | QChartAxis* QChart::axisY() const |
|
258 | 258 | { |
|
259 | 259 | return d_ptr->m_dataset->axisY(); |
|
260 | 260 | } |
|
261 | 261 | |
|
262 | 262 | /*! |
|
263 | 263 | Returns the legend object of the chart. Ownership stays in chart. |
|
264 | 264 | */ |
|
265 | 265 | QLegend* QChart::legend() const |
|
266 | 266 | { |
|
267 | 267 | return d_ptr->m_legend; |
|
268 | 268 | } |
|
269 | 269 | |
|
270 | 270 | QRect QChart::margins() const |
|
271 | 271 | { |
|
272 | 272 | return d_ptr->m_presenter->margins(); |
|
273 | 273 | } |
|
274 | 274 | |
|
275 | 275 | |
|
276 | 276 | /*! |
|
277 | 277 | Resizes and updates the chart area using the \a event data |
|
278 | 278 | */ |
|
279 | 279 | void QChart::resizeEvent(QGraphicsSceneResizeEvent *event) |
|
280 | 280 | { |
|
281 | 281 | d_ptr->m_rect = QRectF(QPoint(0,0),event->newSize()); |
|
282 | 282 | QGraphicsWidget::resizeEvent(event); |
|
283 | 283 | d_ptr->m_presenter->setGeometry(d_ptr->m_rect); |
|
284 | 284 | } |
|
285 | 285 | |
|
286 | 286 | /*! |
|
287 | 287 | Sets animation \a options for the chart |
|
288 | 288 | */ |
|
289 | 289 | void QChart::setAnimationOptions(AnimationOptions options) |
|
290 | 290 | { |
|
291 | 291 | d_ptr->m_presenter->setAnimationOptions(options); |
|
292 | 292 | } |
|
293 | 293 | |
|
294 | 294 | /*! |
|
295 | 295 | Returns animation options for the chart |
|
296 | 296 | */ |
|
297 | 297 | QChart::AnimationOptions QChart::animationOptions() const |
|
298 | 298 | { |
|
299 | 299 | return d_ptr->m_presenter->animationOptions(); |
|
300 | 300 | } |
|
301 | 301 | |
|
302 | 302 | void QChart::scrollLeft() |
|
303 | 303 | { |
|
304 | 304 | d_ptr->m_presenter->scroll(-d_ptr->m_presenter->chartGeometry().width()/(axisX()->ticksCount()-1),0); |
|
305 | 305 | } |
|
306 | 306 | |
|
307 | 307 | void QChart::scrollRight() |
|
308 | 308 | { |
|
309 | 309 | d_ptr->m_presenter->scroll(d_ptr->m_presenter->chartGeometry().width()/(axisX()->ticksCount()-1),0); |
|
310 | 310 | } |
|
311 | 311 | |
|
312 | 312 | void QChart::scrollUp() |
|
313 | 313 | { |
|
314 | 314 | d_ptr->m_presenter->scroll(0,d_ptr->m_presenter->chartGeometry().width()/(axisY()->ticksCount()-1)); |
|
315 | 315 | } |
|
316 | 316 | |
|
317 | 317 | void QChart::scrollDown() |
|
318 | 318 | { |
|
319 | 319 | d_ptr->m_presenter->scroll(0,-d_ptr->m_presenter->chartGeometry().width()/(axisY()->ticksCount()-1)); |
|
320 | 320 | } |
|
321 | 321 | |
|
322 | 322 | void QChart::setBackgroundVisible(bool visible) |
|
323 | 323 | { |
|
324 | 324 | d_ptr->m_presenter->createChartBackgroundItem(); |
|
325 | 325 | d_ptr->m_presenter->m_backgroundItem->setVisible(visible); |
|
326 | 326 | } |
|
327 | 327 | |
|
328 | 328 | bool QChart::isBackgroundVisible() const |
|
329 | 329 | { |
|
330 | 330 | if (!d_ptr->m_presenter->m_backgroundItem) return false; |
|
331 | 331 | return d_ptr->m_presenter->m_backgroundItem->isVisible(); |
|
332 | 332 | } |
|
333 | 333 | |
|
334 | 334 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
335 | 335 | |
|
336 | 336 | QChartPrivate::QChartPrivate(): |
|
337 | 337 | m_legend(0), |
|
338 | 338 | m_dataset(0), |
|
339 | 339 | m_presenter(0) |
|
340 | 340 | { |
|
341 | 341 | |
|
342 | 342 | } |
|
343 | 343 | |
|
344 | 344 | QChartPrivate::~QChartPrivate() |
|
345 | 345 | { |
|
346 | 346 | |
|
347 | 347 | } |
|
348 | 348 | |
|
349 | 349 | #include "moc_qchart.cpp" |
|
350 | 350 | |
|
351 | 351 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,153 +1,152 | |||
|
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 | #ifndef QLEGEND_H |
|
22 | 22 | #define QLEGEND_H |
|
23 | 23 | |
|
24 | 24 | #include <QChartGlobal> |
|
25 | 25 | #include <QGraphicsWidget> |
|
26 | 26 | #include <QPen> |
|
27 | 27 | #include <QBrush> |
|
28 | 28 | |
|
29 | 29 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
30 | 30 | |
|
31 | 31 | class Domain; |
|
32 | 32 | class LegendMarker; |
|
33 | 33 | class QPieSlice; |
|
34 | 34 | class QXYSeries; |
|
35 | 35 | class QBarSet; |
|
36 | 36 | class QBarSeries; |
|
37 | 37 | class QPieSeries; |
|
38 | 38 | class QAreaSeries; |
|
39 | 39 | class LegendScrollButton; |
|
40 | 40 | class QSeries; |
|
41 | 41 | class QChart; |
|
42 | 42 | class Scroller; |
|
43 | 43 | |
|
44 | 44 | class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget |
|
45 | 45 | { |
|
46 | 46 | Q_OBJECT |
|
47 | 47 | public: |
|
48 | 48 | |
|
49 | 49 | // We only support these alignments (for now) |
|
50 | 50 | enum Alignment { |
|
51 | 51 | AlignmentTop = Qt::AlignTop, |
|
52 | 52 | AlignmentBottom = Qt::AlignBottom, |
|
53 | 53 | AlignmentLeft = Qt::AlignLeft, |
|
54 | 54 | AlignmentRight = Qt::AlignRight |
|
55 | 55 | }; |
|
56 | 56 | |
|
57 | 57 | Q_DECLARE_FLAGS(Alignments, Alignment) |
|
58 | 58 | |
|
59 | 59 | private: |
|
60 | 60 | explicit QLegend(QChart *chart); |
|
61 | 61 | |
|
62 | 62 | public: |
|
63 | 63 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); |
|
64 | 64 | QRectF boundingRect() const; |
|
65 | 65 | |
|
66 | 66 | void setBrush(const QBrush &brush); |
|
67 | 67 | QBrush brush() const; |
|
68 | 68 | |
|
69 | 69 | void setPen(const QPen &pen); |
|
70 | 70 | QPen pen() const; |
|
71 | 71 | |
|
72 | 72 | void setAlignmnent(QLegend::Alignments alignment); |
|
73 | 73 | QLegend::Alignments alignment() const; |
|
74 | 74 | |
|
75 | 75 | |
|
76 | 76 | void detachFromChart(); |
|
77 | 77 | void attachToChart(); |
|
78 | 78 | bool isAttachedToChart(); |
|
79 | 79 | |
|
80 | 80 | qreal minWidht() const { return m_minWidth;} |
|
81 | 81 | qreal minHeight() const { return m_minHeight;} |
|
82 | 82 | |
|
83 | 83 | void setBackgroundVisible(bool visible); |
|
84 | 84 | bool isBackgroundVisible() const; |
|
85 | 85 | |
|
86 | 86 | void setOffset(const QPointF& point); |
|
87 | 87 | QPointF offset() const; |
|
88 | 88 | |
|
89 | 89 | protected: |
|
90 | 90 | void resizeEvent(QGraphicsSceneResizeEvent *event); |
|
91 | 91 | void hideEvent(QHideEvent *event); |
|
92 | 92 | void showEvent(QShowEvent *event); |
|
93 | 93 | void mousePressEvent(QGraphicsSceneMouseEvent* event); |
|
94 | 94 | void mouseMoveEvent(QGraphicsSceneMouseEvent* event); |
|
95 | 95 | void mouseReleaseEvent(QGraphicsSceneMouseEvent* event); |
|
96 | 96 | |
|
97 | 97 | public Q_SLOTS: |
|
98 | 98 | // PIMPL ---> |
|
99 | 99 | void handleSeriesAdded(QSeries *series, Domain *domain); |
|
100 | 100 | void handleSeriesRemoved(QSeries *series); |
|
101 | 101 | void handleAdded(QList<QPieSlice *> slices); |
|
102 | 102 | void handleRemoved(QList<QPieSlice *> slices); |
|
103 | 103 | void handleMarkerDestroyed(); |
|
104 | 104 | |
|
105 | 105 | // PIMPL <--- |
|
106 | 106 | |
|
107 | 107 | private: |
|
108 | 108 | // PIMPL ---> |
|
109 | 109 | void appendMarkers(QAreaSeries *series); |
|
110 | 110 | void appendMarkers(QXYSeries *series); |
|
111 | 111 | void appendMarkers(QBarSeries *series); |
|
112 | 112 | void appendMarkers(QPieSeries *series); |
|
113 | 113 | void deleteMarkers(QSeries *series); |
|
114 | 114 | |
|
115 | 115 | |
|
116 | 116 | |
|
117 | 117 | |
|
118 | 118 | private Q_SLOTS: |
|
119 | 119 | void updateLayout(); |
|
120 | void scroll(); | |
|
121 | 120 | |
|
122 | 121 | private: |
|
123 | 122 | qreal m_margin; |
|
124 | 123 | |
|
125 | 124 | QRectF m_rect; |
|
126 | 125 | qreal m_offsetX; |
|
127 | 126 | qreal m_offsetY; |
|
128 | 127 | |
|
129 | 128 | //QList<LegendMarker *> m_markers; |
|
130 | 129 | |
|
131 | 130 | QBrush m_brush; |
|
132 | 131 | QPen m_pen; |
|
133 | 132 | QLegend::Alignments m_alignment; |
|
134 | 133 | QGraphicsItemGroup* m_markers; |
|
135 | 134 | |
|
136 | 135 | |
|
137 | 136 | bool m_attachedToChart; |
|
138 | 137 | |
|
139 | 138 | QChart *m_chart; |
|
140 | 139 | qreal m_minWidth; |
|
141 | 140 | qreal m_minHeight; |
|
142 | 141 | qreal m_width; |
|
143 | 142 | qreal m_height; |
|
144 | 143 | bool m_visible; |
|
145 | 144 | bool m_dirty; |
|
146 | 145 | Scroller* m_scroller; |
|
147 | 146 | friend class QChart; |
|
148 | 147 | // <--- PIMPL |
|
149 | 148 | }; |
|
150 | 149 | |
|
151 | 150 | QTCOMMERCIALCHART_END_NAMESPACE |
|
152 | 151 | |
|
153 | 152 | #endif // QLEGEND_H |
General Comments 0
You need to be logged in to leave comments.
Login now