##// END OF EJS Templates
Refactor , move dataset legend presentr connections to one place
Michal Klocek -
r871:b32d31ef1d27
parent child
Show More
@@ -1,115 +1,115
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 "chartview.h"
22 22 #include <QLineSeries>
23 23 #include <QScatterSeries>
24 24 #include <QSplineSeries>
25 25 #include <QAreaSeries>
26 26 #include <QTime>
27 27
28 28 ChartView::ChartView(QChart* chart,QWidget* parent):QChartView(chart,parent),
29 29 m_index(-1),m_chart(chart)
30 30 {
31 31 m_chart->setTitle("Charts presenter");
32 32 QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(handleTimeout()));
33 33 m_timer.setInterval(3000);
34 34
35 35 //![1]
36 36 QLineSeries* series0 = new QLineSeries(this);
37 37 QPen blue(Qt::blue);
38 38 blue.setWidth(3);
39 39 series0->setPen(blue);
40 40
41 41 QScatterSeries* series1 = new QScatterSeries(this);
42 42 QPen red(Qt::red);
43 43 red.setWidth(3);
44 44 series1->setPen(red);
45 45 series1->setBrush(Qt::white);
46 46
47 47 QSplineSeries* series2 = new QSplineSeries(this);
48 48 QPen green(Qt::green);
49 49 green.setWidth(3);
50 50 series2->setPen(green);
51 51
52 52 QAreaSeries* series3 = new QAreaSeries(series0);
53 53 QPen yellow(Qt::black);
54 54 yellow.setWidth(3);
55 55 series3->setPen(yellow);
56 56 series3->setBrush(Qt::yellow);
57 57 //![1]
58 58
59 59 //![2]
60 60 int numPoints = 10;
61 61
62 62 for (int x = 0; x <= numPoints; ++x) {
63 63 qreal y = qrand() % 100;
64 64 series0->append(x,y);
65 65 series1->append(x,y);
66 66 series2->append(x,y);
67 67 }
68 68 //![2]
69 69
70 70 //![3]
71 71 m_series<<series0;
72 72 m_titles<< m_chart->title()+": LineChart";
73 73 m_series<<series1;
74 74 m_titles<< m_chart->title()+": ScatterChart";
75 75 m_series<<series2;
76 76 m_titles<< m_chart->title()+": SplineChart";
77 77 m_series<<series3;
78 78 m_titles<< m_chart->title()+": AreaChart";
79 79 //![3]
80 80
81 81 //![4]
82 82 foreach (QSeries* series, m_series) {
83 83 QObject::connect(series,SIGNAL(clicked(const QPointF&)),this,SLOT(handlePointClicked(const QPointF&)));
84 84 }
85 85 //![4]
86 QTimer::singleShot(0,this,SLOT(handleTimeout()));
87 86 m_timer.start();
87 handleTimeout();
88 88 }
89 89
90 90 ChartView::~ChartView()
91 91 {
92 92 if(m_series.size()==0) return;
93 93 m_chart->removeSeries(m_series.at(m_index));
94 94 qDeleteAll(m_series);
95 95 }
96 96
97 97 //![5]
98 98 void ChartView::handleTimeout()
99 99 {
100 100 if(m_series.size()==0) return;
101 101 if(m_index>=0)
102 102 m_chart->removeSeries(m_series.at(m_index));
103 103 m_index++;
104 104 m_index=m_index%m_series.size();
105 105 m_chart->addSeries(m_series.at(m_index));
106 106 m_chart->setTitle(m_titles.at(m_index));
107 107 }
108 108 //![5]
109 109
110 110 //![6]
111 111 void ChartView::handlePointClicked(const QPointF& point)
112 112 {
113 113 m_chart->setTitle(m_titles.at(m_index) + QString(" x: %1 y: %2").arg(point.x()).arg(point.y()));
114 114 }
115 115 //![6]
@@ -1,536 +1,529
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 createConnections();
68 67 }
69 68
70 69 ChartPresenter::~ChartPresenter()
71 70 {
72 71 delete m_chartTheme;
73 72 }
74 73
75 void ChartPresenter::createConnections()
76 {
77 QObject::connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),this,SLOT(handleSeriesAdded(QSeries*,Domain*)));
78 QObject::connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),this,SLOT(handleSeriesRemoved(QSeries*)));
79 QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*,Domain*)),this,SLOT(handleAxisAdded(QChartAxis*,Domain*)));
80 QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*)));
81 }
82
83 74 void ChartPresenter::setGeometry(const QRectF& rect)
84 75 {
85 76 m_rect = rect;
86 77 Q_ASSERT(m_rect.isValid());
87 78 updateLayout();
88 79 }
89 80
90 81 void ChartPresenter::setMinimumMarginWidth(Axis* axis, qreal width)
91 82 {
92 83 switch(axis->axisType()){
93 84 case Axis::X_AXIS:
94 85 {
95 86 if(width>m_chartRect.width()+ m_chartMargins.left()) {
96 87 m_minLeftMargin= width - m_chartRect.width();
97 88 updateLayout();
98 89 }
99 90 break;
100 91 }
101 92 case Axis::Y_AXIS:
102 93 {
103 94
104 95 if(m_minLeftMargin!=width){
105 96 m_minLeftMargin= width;
106 97 updateLayout();
107 98 }
108 99 break;
109 100 }
110 101
111 102 }
112 103 }
113 104
114 105 void ChartPresenter::setMinimumMarginHeight(Axis* axis, qreal height)
115 106 {
116 107 switch(axis->axisType()){
117 108 case Axis::X_AXIS:
118 109 {
119 110 if(m_minBottomMargin!=height) {
120 111 m_minBottomMargin= height;
121 112 updateLayout();
122 113 }
123 114 break;
124 115 }
125 116 case Axis::Y_AXIS:
126 117 {
127 118
128 119 if(height>m_chartMargins.bottom()+m_chartRect.height()){
129 120 m_minBottomMargin= height - m_chartRect.height();
130 121 updateLayout();
131 122 }
132 123 break;
133 124 }
134 125
135 126 }
136 127 }
137 128
138 129 void ChartPresenter::handleAxisAdded(QChartAxis* axis,Domain* domain)
139 130 {
140 131 Axis* item = new Axis(axis,this,axis==m_dataset->axisX()?Axis::X_AXIS : Axis::Y_AXIS);
141 132
142 133 if(m_options.testFlag(QChart::GridAxisAnimations)){
143 134 m_animator->addAnimation(item);
144 135 }
145 136
146 137 if(axis==m_dataset->axisX()){
147 138 m_chartTheme->decorate(axis,true,m_themeForce);
148 139 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
149 140 //initialize
150 141 item->handleRangeChanged(domain->minX(),domain->maxX(),domain->tickXCount());
151 142
152 143 }
153 144 else{
154 145 m_chartTheme->decorate(axis,false,m_themeForce);
155 146 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
156 147 //initialize
157 148 item->handleRangeChanged(domain->minY(),domain->maxY(),domain->tickYCount());
158 149 }
159 150
160 151 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
161 152 //initialize
162 153 item->handleGeometryChanged(m_chartRect);
163 154 m_axisItems.insert(axis, item);
164 155 }
165 156
166 157 void ChartPresenter::handleAxisRemoved(QChartAxis* axis)
167 158 {
168 159 Axis* item = m_axisItems.take(axis);
169 160 Q_ASSERT(item);
170 161 if(m_animator) m_animator->removeAnimation(item);
171 162 delete item;
172 163 }
173 164
174 165
175 166 void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain)
176 167 {
177 168 Chart *item = 0 ;
178 169
179 170 switch(series->type())
180 171 {
181 172 case QSeries::SeriesTypeLine: {
182 173
183 174 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
184 175 LineChartItem* line = new LineChartItem(lineSeries,this);
185 176 if(m_options.testFlag(QChart::SeriesAnimations)) {
186 177 m_animator->addAnimation(line);
187 178 }
188 179 m_chartTheme->decorate(lineSeries, m_dataset->seriesIndex(series),m_themeForce);
189 180 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),line,SLOT(handleGeometryChanged(const QRectF&)));
190 181 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),line,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
191 182 item = line;
192 183 break;
193 184 }
194 185
195 186 case QSeries::SeriesTypeArea: {
196 187
197 188 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
198 189 AreaChartItem* area = new AreaChartItem(areaSeries,this);
199 190 if(m_options.testFlag(QChart::SeriesAnimations)) {
200 191 m_animator->addAnimation(area->upperLineItem());
201 192 if(areaSeries->lowerSeries()) m_animator->addAnimation(area->lowerLineItem());
202 193 }
203 194 m_chartTheme->decorate(areaSeries, m_dataset->seriesIndex(series),m_themeForce);
204 195 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),area,SLOT(handleGeometryChanged(const QRectF&)));
205 196 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),area,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
206 197 item=area;
207 198 break;
208 199 }
209 200
210 201 case QSeries::SeriesTypeBar: {
211 202 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
212 203 BarChartItem* bar = new BarChartItem(barSeries,this);
213 204 if(m_options.testFlag(QChart::SeriesAnimations)) {
214 205 m_animator->addAnimation(bar);
215 206 }
216 207 m_chartTheme->decorate(barSeries, m_dataset->seriesIndex(barSeries),m_themeForce);
217 208 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&)));
218 209 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
219 210 item=bar;
220 211 break;
221 212 }
222 213
223 214 case QSeries::SeriesTypeStackedBar: {
224 215 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
225 216 StackedBarChartItem* bar = new StackedBarChartItem(stackedBarSeries,this);
226 217 if(m_options.testFlag(QChart::SeriesAnimations)) {
227 218 m_animator->addAnimation(bar);
228 219 }
229 220 m_chartTheme->decorate(stackedBarSeries, m_dataset->seriesIndex(stackedBarSeries),m_themeForce);
230 221 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&)));
231 222 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
232 223 item=bar;
233 224 break;
234 225 }
235 226
236 227 case QSeries::SeriesTypePercentBar: {
237 228 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
238 229 PercentBarChartItem* bar = new PercentBarChartItem(percentBarSeries,this);
239 230 if(m_options.testFlag(QChart::SeriesAnimations)) {
240 231 m_animator->addAnimation(bar);
241 232 }
242 233 m_chartTheme->decorate(percentBarSeries, m_dataset->seriesIndex(percentBarSeries),m_themeForce);
243 234 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&)));
244 235 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
245 236 item=bar;
246 237 break;
247 238 }
248 239
249 240 case QSeries::SeriesTypeScatter: {
250 241
251 242 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
252 243 ScatterChartItem *scatter = new ScatterChartItem(scatterSeries,this);
253 244 if(m_options.testFlag(QChart::SeriesAnimations)) {
254 245 m_animator->addAnimation(scatter);
255 246 }
256 247 m_chartTheme->decorate(scatterSeries, m_dataset->seriesIndex(series),m_themeForce);
257 248 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),scatter,SLOT(handleGeometryChanged(const QRectF&)));
258 249 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),scatter,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
259 250 item = scatter;
260 251 break;
261 252 }
262 253
263 254 case QSeries::SeriesTypePie: {
264 255 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
265 256 PieChartItem* pie = new PieChartItem(pieSeries, this);
266 257 if(m_options.testFlag(QChart::SeriesAnimations)) {
267 258 m_animator->addAnimation(pie);
268 259 }
269 260 m_chartTheme->decorate(pieSeries, m_dataset->seriesIndex(series),m_themeForce);
270 261 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),pie,SLOT(handleGeometryChanged(const QRectF&)));
271 262 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),pie,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
272 263 // Hide all from background when there is only piechart
273 264 // TODO: refactor this ugly code... should be one setting for this
274 265 if (m_chartItems.count() == 0) {
275 266 m_chart->axisX()->hide();
276 267 m_chart->axisY()->hide();
277 268 }
278 269 item=pie;
279 270 break;
280 271 }
281 272
282 273 case QSeries::SeriesTypeSpline: {
283 274 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
284 275 SplineChartItem* spline = new SplineChartItem(splineSeries, this);
285 276 if(m_options.testFlag(QChart::SeriesAnimations)) {
286 277 m_animator->addAnimation(spline);
287 278 }
288 279 m_chartTheme->decorate(splineSeries, m_dataset->seriesIndex(series),m_themeForce);
289 280 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),spline,SLOT(handleGeometryChanged(const QRectF&)));
290 281 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),spline,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
291 282 item=spline;
292 283 break;
293 284 }
294 285 default: {
295 286 qDebug()<< "Series type" << series->type() << "not implemented.";
296 287 break;
297 288 }
298 289 }
299 290
300 291 //initialize
301 292 item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY());
302 293 if(m_chartRect.isValid()) item->handleGeometryChanged(m_chartRect);
303 294 m_chartItems.insert(series,item);
304 295 }
305 296
306 297 void ChartPresenter::handleSeriesRemoved(QSeries* series)
307 298 {
308 299 Chart* item = m_chartItems.take(series);
309 300 Q_ASSERT(item);
310 301 if(m_animator) {
311 302 //small hack to handle area animations
312 303 if(series->type()==QSeries::SeriesTypeArea){
313 304 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
314 305 AreaChartItem* area = static_cast<AreaChartItem*>(item);
315 306 m_animator->removeAnimation(area->upperLineItem());
316 307 if(areaSeries->lowerSeries()) m_animator->removeAnimation(area->lowerLineItem());
317 308 }else
318 309 m_animator->removeAnimation(item);
319 310 }
320 311 delete item;
321 312 }
322 313
323 314 void ChartPresenter::setTheme(QChart::ChartTheme theme,bool force)
324 315 {
325 316 if(m_chartTheme && m_chartTheme->id() == theme) return;
326 317 delete m_chartTheme;
327 318 m_themeForce = force;
328 319 m_chartTheme = ChartTheme::createTheme(theme);
329 320 m_chartTheme->decorate(m_chart,m_themeForce);
330 321 m_chartTheme->decorate(m_chart->legend(),m_themeForce);
331 322 resetAllElements();
332 323 }
333 324
334 325 QChart::ChartTheme ChartPresenter::theme()
335 326 {
336 327 return m_chartTheme->id();
337 328 }
338 329
339 330 void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options)
340 331 {
341 332 if(m_options!=options) {
342 333
343 334 m_options=options;
344 335
345 336 if(m_options!=QChart::NoAnimation && !m_animator) {
346 337 m_animator= new ChartAnimator(this);
347 338 }
348 339 resetAllElements();
349 340 }
350 341
351 342 }
352 343
353 344 void ChartPresenter::resetAllElements()
354 345 {
355 346 QList<QChartAxis*> axisList = m_axisItems.uniqueKeys();
356 347 QList<QSeries*> seriesList = m_chartItems.uniqueKeys();
357 348
358 349 foreach(QChartAxis* axis, axisList) {
359 350 handleAxisRemoved(axis);
360 351 handleAxisAdded(axis,m_dataset->domain(axis));
361 352 }
362 353 foreach(QSeries* series, seriesList) {
363 354 handleSeriesRemoved(series);
364 355 handleSeriesAdded(series,m_dataset->domain(series));
365 356 }
366 357 }
367 358
368 359 void ChartPresenter::zoomIn()
369 360 {
370 361 QRectF rect = chartGeometry();
371 362 rect.setWidth(rect.width()/2);
372 363 rect.setHeight(rect.height()/2);
373 364 rect.moveCenter(chartGeometry().center());
374 365 zoomIn(rect);
375 366 }
376 367
377 368 void ChartPresenter::zoomIn(const QRectF& rect)
378 369 {
379 370 QRectF r = rect.normalized();
380 371 r.translate(-m_chartMargins.topLeft());
381 372 if(m_animator) {
382 373
383 374 QPointF point(r.center().x()/chartGeometry().width(),r.center().y()/chartGeometry().height());
384 375 m_animator->setState(ChartAnimator::ZoomInState,point);
385 376 }
386 377 m_dataset->zoomInDomain(r,chartGeometry().size());
387 378 if(m_animator) {
388 379 m_animator->setState(ChartAnimator::ShowState);
389 380 }
390 381 }
391 382
392 383 void ChartPresenter::zoomOut()
393 384 {
394 385 if(m_animator)
395 386 {
396 387 m_animator->setState(ChartAnimator::ZoomOutState);
397 388 }
398 389
399 390 QSizeF size = chartGeometry().size();
400 391 QRectF rect = chartGeometry();
401 392 rect.translate(-m_chartMargins.topLeft());
402 393 m_dataset->zoomOutDomain(rect.adjusted(size.width()/4,size.height()/4,-size.width()/4,-size.height()/4),size);
403 394 //m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size());
404 395
405 396 if(m_animator){
406 397 m_animator->setState(ChartAnimator::ShowState);
407 398 }
408 399 }
409 400
410 401 void ChartPresenter::scroll(int dx,int dy)
411 402 {
412 403 if(m_animator){
413 404 if(dx<0) m_animator->setState(ChartAnimator::ScrollLeftState,QPointF());
414 405 if(dx>0) m_animator->setState(ChartAnimator::ScrollRightState,QPointF());
415 406 if(dy<0) m_animator->setState(ChartAnimator::ScrollUpState,QPointF());
416 407 if(dy>0) m_animator->setState(ChartAnimator::ScrollDownState,QPointF());
417 408 }
418 409
419 410 m_dataset->scrollDomain(dx,dy,chartGeometry().size());
420 411
421 412 if(m_animator){
422 413 m_animator->setState(ChartAnimator::ShowState);
423 414 }
424 415 }
425 416
426 417 QChart::AnimationOptions ChartPresenter::animationOptions() const
427 418 {
428 419 return m_options;
429 420 }
430 421
431 422 void ChartPresenter::updateLayout()
432 423 {
433 424 if (!m_rect.isValid()) return;
434 425
435 426 // recalculate title size
436 427
437 428 QSize titleSize;
438 429 int titlePadding=0;
439 430
440 431 if (m_titleItem) {
441 432 titleSize= m_titleItem->boundingRect().size().toSize();
442 433 }
443 434
444 435 //defaults
445 436 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));
446 437 titlePadding = m_chartMargins.top()/2;
447 438
448 439 QLegend* legend = m_chart->d_ptr->m_legend;
449 440
450 441 // recalculate legend position
451 442 if (legend->isAttachedToChart() && legend->isEnabled()) {
452 443
453 444 QRect legendRect;
454 445
455 446 // Reserve some space for legend
456 447 switch (legend->alignment()) {
457 448
458 449 case QLegend::AlignmentTop: {
459 450 int ledgendSize = legend->minHeight();
460 451 int topPadding = 2*m_marginTiny + titleSize.height() + ledgendSize + m_marginTiny;
461 452 m_chartMargins = QRect(QPoint(m_chartMargins.left(),topPadding),QPoint(m_chartMargins.right(),m_chartMargins.bottom()));
462 453 m_legendMargins = QRect(QPoint(m_chartMargins.left(),topPadding - (ledgendSize + m_marginTiny)),QPoint(m_chartMargins.right(),m_rect.height()-topPadding + m_marginTiny));
463 454 titlePadding = m_marginTiny + m_marginTiny;
464 455 break;
465 456 }
466 457 case QLegend::AlignmentBottom: {
467 458 int ledgendSize = legend->minHeight();
468 459 int bottomPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minBottomMargin;
469 460 m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(m_chartMargins.right(),bottomPadding));
470 461 m_legendMargins = QRect(QPoint(m_chartMargins.left(),m_rect.height()-bottomPadding + m_marginTiny + m_minBottomMargin),QPoint(m_chartMargins.right(),m_marginTiny + m_marginSmall));
471 462 titlePadding = m_chartMargins.top()/2;
472 463 break;
473 464 }
474 465 case QLegend::AlignmentLeft: {
475 466 int ledgendSize = legend->minWidht();
476 467 int leftPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minLeftMargin;
477 468 m_chartMargins = QRect(QPoint(leftPadding,m_chartMargins.top()),QPoint(m_chartMargins.right(),m_chartMargins.bottom()));
478 469 m_legendMargins = QRect(QPoint(m_marginTiny + m_marginSmall,m_chartMargins.top()),QPoint(m_rect.width()-leftPadding + m_marginTiny + m_minLeftMargin,m_chartMargins.bottom()));
479 470 titlePadding = m_chartMargins.top()/2;
480 471 break;
481 472 }
482 473 case QLegend::AlignmentRight: {
483 474 int ledgendSize = legend->minWidht();
484 475 int rightPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny;
485 476 m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(rightPadding,m_chartMargins.bottom()));
486 477 m_legendMargins = QRect(QPoint(m_rect.width()- rightPadding+ m_marginTiny ,m_chartMargins.top()),QPoint(m_marginTiny + m_marginSmall,m_chartMargins.bottom()));
487 478 titlePadding = m_chartMargins.top()/2;
488 479 break;
489 480 }
490 481 default: {
491 482 break;
492 483 }
493 484 }
494 485 }
495 486
496 487 // recalculate title position
497 488 if (m_titleItem) {
498 489 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
499 490 m_titleItem->setPos(center.x(),titlePadding);
500 491 }
501 492
502 493 //recalculate background gradient
503 494 if (m_backgroundItem) {
504 495 m_backgroundItem->setRect(m_rect.adjusted(m_marginTiny,m_marginTiny, -m_marginTiny, -m_marginTiny));
505 496 }
506 497
507 498 QRectF chartRect = m_rect.adjusted(m_chartMargins.left(),m_chartMargins.top(),-m_chartMargins.right(),-m_chartMargins.bottom());
508 499
500 legend->setGeometry(m_rect.adjusted(m_legendMargins.left(),m_legendMargins.top(),-m_legendMargins.right(),-m_legendMargins.bottom()));
501
509 502 if(m_chartRect!=chartRect){
510 503 m_chartRect=chartRect;
511 504 emit geometryChanged(m_chartRect);
512 505 }
513 506
514 legend->setGeometry(m_rect.adjusted(m_legendMargins.left(),m_legendMargins.top(),-m_legendMargins.right(),-m_legendMargins.bottom()));
507
515 508 }
516 509
517 510 void ChartPresenter::createChartBackgroundItem()
518 511 {
519 512 if (!m_backgroundItem) {
520 513 m_backgroundItem = new ChartBackground(rootItem());
521 514 m_backgroundItem->setPen(Qt::NoPen);
522 515 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
523 516 }
524 517 }
525 518
526 519 void ChartPresenter::createChartTitleItem()
527 520 {
528 521 if (!m_titleItem) {
529 522 m_titleItem = new QGraphicsSimpleTextItem(rootItem());
530 523 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
531 524 }
532 525 }
533 526
534 527 #include "moc_chartpresenter_p.cpp"
535 528
536 529 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,127 +1,126
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 CHARTPRESENTER_H_
22 22 #define CHARTPRESENTER_H_
23 23
24 24 #include "qchartglobal.h"
25 25 #include "chartbackground_p.h" //TODO fix me
26 26 #include "qchart.h" //becouse of QChart::ChartThemeId //TODO
27 27 #include "qchartaxis.h"
28 28 #include <QRectF>
29 29
30 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 31
32 32 class Chart;
33 33 class QSeries;
34 34 class ChartDataSet;
35 35 class Domain;
36 36 class Axis;
37 37 class ChartTheme;
38 38 class ChartAnimator;
39 39
40 40 class ChartPresenter: public QObject
41 41 {
42 42 Q_OBJECT
43 43 public:
44 44 enum ZValues {
45 45 BackgroundZValue = -1,
46 46 ShadesZValue,
47 47 GridZValue,
48 48 LineChartZValue,
49 49 BarSeriesZValue,
50 50 ScatterSeriesZValue,
51 51 PieSeriesZValue,
52 52 AxisZValue,
53 53 LegendZValue
54 54 };
55 55
56 56 ChartPresenter(QChart* chart,ChartDataSet *dataset);
57 57 virtual ~ChartPresenter();
58 58
59 59 ChartAnimator* animator() const { return m_animator; }
60 60 ChartTheme *chartTheme() const { return m_chartTheme; }
61 61 ChartDataSet *dataSet() const { return m_dataset; }
62 62 QGraphicsItem* rootItem() const { return m_chart; }
63 63
64 64 void setTheme(QChart::ChartTheme theme,bool force = true);
65 65 QChart::ChartTheme theme();
66 66
67 67 void setAnimationOptions(QChart::AnimationOptions options);
68 68 QChart::AnimationOptions animationOptions() const;
69 69
70 70 void zoomIn();
71 71 void zoomIn(const QRectF& rect);
72 72 void zoomOut();
73 73 void scroll(int dx,int dy);
74 74
75 75 void setGeometry(const QRectF& rect);
76 76 QRectF chartGeometry() const { return m_chartRect; }
77 77
78 78 void setMinimumMarginHeight(Axis* axis, qreal height);
79 79 void setMinimumMarginWidth(Axis* axis, qreal width);
80 80 qreal minimumLeftMargin() const { return m_minLeftMargin; }
81 81 qreal minimumBottomMargin() const { return m_minBottomMargin; }
82 82
83 83 public: //TODO: fix me
84 void createConnections();
85 84 void resetAllElements();
86 85 void createChartBackgroundItem();
87 86 void createChartTitleItem();
88 87 QRect margins() const { return m_chartMargins;}
89 88
90 89 public Q_SLOTS:
91 90 void handleSeriesAdded(QSeries* series,Domain* domain);
92 91 void handleSeriesRemoved(QSeries* series);
93 92 void handleAxisAdded(QChartAxis* axis,Domain* domain);
94 93 void handleAxisRemoved(QChartAxis* axis);
95 94 void updateLayout();
96 95
97 96 Q_SIGNALS:
98 97 void geometryChanged(const QRectF& rect);
99 98
100 99
101 100 private:
102 101 QChart* m_chart;
103 102 ChartAnimator* m_animator;
104 103 ChartDataSet* m_dataset;
105 104 ChartTheme *m_chartTheme;
106 105 QMap<QSeries*,Chart*> m_chartItems;
107 106 QMap<QChartAxis*,Axis*> m_axisItems;
108 107 QRectF m_rect;
109 108 QRectF m_chartRect;
110 109 QChart::AnimationOptions m_options;
111 110 bool m_themeForce;
112 111 qreal m_minLeftMargin;
113 112 qreal m_minBottomMargin;
114 113 public: //TODO: fixme
115 114 ChartBackground* m_backgroundItem;
116 115 QGraphicsSimpleTextItem* m_titleItem;
117 116 int m_marginBig;
118 117 int m_marginSmall;
119 118 int m_marginTiny;
120 119 QRect m_chartMargins;
121 120 QRect m_legendMargins;
122 121
123 122 };
124 123
125 124 QTCOMMERCIALCHART_END_NAMESPACE
126 125
127 126 #endif /* CHARTPRESENTER_H_ */
@@ -1,351 +1,360
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 ScrolledQLegend(this);
72 72 d_ptr->m_dataset = new ChartDataSet(this);
73 73 d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset);
74 74 d_ptr->m_presenter->setTheme(QChart::ChartThemeLight, false);
75 d_ptr->createConnections();
75 76 //TODO:fix me setMinimumSize(d_ptr->m_padding.left() * 3, d_ptr->m_padding.top() * 3);
76 connect(d_ptr->m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),d_ptr->m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
77 connect(d_ptr->m_dataset,SIGNAL(seriesRemoved(QSeries*)),d_ptr->m_legend,SLOT(handleSeriesRemoved(QSeries*)));
78 77 }
79 78
80 79 /*!
81 80 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
82 81 */
83 82 QChart::~QChart()
84 83 {
85 84 //delete first presenter , since this is a root of all the graphical items
86 85 delete d_ptr->m_presenter;
87 86 d_ptr->m_presenter=0;
88 87 }
89 88
90 89 /*!
91 90 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
92 91 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
93 92 the y axis).
94 93 */
95 94 void QChart::addSeries(QSeries* series, QChartAxis* axisY)
96 95 {
97 96 d_ptr->m_dataset->addSeries(series, axisY);
98 97 }
99 98
100 99 /*!
101 100 Removes the \a series specified in a perameter from the QChartView.
102 101 It releses its ownership of the specified QChartSeries object.
103 102 It does not delete the pointed QChartSeries data object
104 103 \sa addSeries(), removeAllSeries()
105 104 */
106 105 void QChart::removeSeries(QSeries* series)
107 106 {
108 107 d_ptr->m_dataset->removeSeries(series);
109 108 }
110 109
111 110 /*!
112 111 Removes all the QChartSeries that have been added to the QChartView
113 112 It also deletes the pointed QChartSeries data objects
114 113 \sa addSeries(), removeSeries()
115 114 */
116 115 void QChart::removeAllSeries()
117 116 {
118 117 d_ptr->m_dataset->removeAllSeries();
119 118 }
120 119
121 120 /*!
122 121 Sets the \a brush that is used for painting the background of the chart area.
123 122 */
124 123 void QChart::setBackgroundBrush(const QBrush& brush)
125 124 {
126 125 d_ptr->m_presenter->createChartBackgroundItem();
127 126 d_ptr->m_presenter->m_backgroundItem->setBrush(brush);
128 127 d_ptr->m_presenter->m_backgroundItem->update();
129 128 }
130 129
131 130 QBrush QChart::backgroundBrush() const
132 131 {
133 132 if (!d_ptr->m_presenter->m_backgroundItem) return QBrush();
134 133 return (d_ptr->m_presenter->m_backgroundItem)->brush();
135 134 }
136 135
137 136 /*!
138 137 Sets the \a pen that is used for painting the background of the chart area.
139 138 */
140 139 void QChart::setBackgroundPen(const QPen& pen)
141 140 {
142 141 d_ptr->m_presenter->createChartBackgroundItem();
143 142 d_ptr->m_presenter->m_backgroundItem->setPen(pen);
144 143 d_ptr->m_presenter->m_backgroundItem->update();
145 144 }
146 145
147 146 QPen QChart::backgroundPen() const
148 147 {
149 148 if (!d_ptr->m_presenter->m_backgroundItem) return QPen();
150 149 return d_ptr->m_presenter->m_backgroundItem->pen();
151 150 }
152 151
153 152 /*!
154 153 Sets the chart \a title. The description text that is drawn above the chart.
155 154 */
156 155 void QChart::setTitle(const QString& title)
157 156 {
158 157 d_ptr->m_presenter->createChartTitleItem();
159 158 d_ptr->m_presenter->m_titleItem->setText(title);
160 159 d_ptr->m_presenter->updateLayout();
161 160 }
162 161
163 162 /*!
164 163 Returns the chart title. The description text that is drawn above the chart.
165 164 */
166 165 QString QChart::title() const
167 166 {
168 167 if (d_ptr->m_presenter->m_titleItem)
169 168 return d_ptr->m_presenter->m_titleItem->text();
170 169 else
171 170 return QString();
172 171 }
173 172
174 173 /*!
175 174 Sets the \a font that is used for rendering the description text that is rendered above the chart.
176 175 */
177 176 void QChart::setTitleFont(const QFont& font)
178 177 {
179 178 d_ptr->m_presenter->createChartTitleItem();
180 179 d_ptr->m_presenter->m_titleItem->setFont(font);
181 180 d_ptr->m_presenter->updateLayout();
182 181 }
183 182
184 183 /*!
185 184 Sets the \a brush used for rendering the title text.
186 185 */
187 186 void QChart::setTitleBrush(const QBrush &brush)
188 187 {
189 188 d_ptr->m_presenter->createChartTitleItem();
190 189 d_ptr->m_presenter->m_titleItem->setBrush(brush);
191 190 d_ptr->m_presenter->updateLayout();
192 191 }
193 192
194 193 /*!
195 194 Returns the brush used for rendering the title text.
196 195 */
197 196 QBrush QChart::titleBrush() const
198 197 {
199 198 if (!d_ptr->m_presenter->m_titleItem) return QBrush();
200 199 return d_ptr->m_presenter->m_titleItem->brush();
201 200 }
202 201
203 202 /*!
204 203 Sets the \a theme used by the chart for rendering the graphical representation of the data
205 204 \sa ChartTheme, chartTheme()
206 205 */
207 206 void QChart::setTheme(QChart::ChartTheme theme)
208 207 {
209 208 d_ptr->m_presenter->setTheme(theme);
210 209 }
211 210
212 211 /*!
213 212 Returns the theme enum used by the chart.
214 213 \sa ChartTheme, setChartTheme()
215 214 */
216 215 QChart::ChartTheme QChart::theme() const
217 216 {
218 217 return d_ptr->m_presenter->theme();
219 218 }
220 219
221 220 /*!
222 221 Zooms in the view by a factor of 2
223 222 */
224 223 void QChart::zoomIn()
225 224 {
226 225 d_ptr->m_presenter->zoomIn();
227 226 }
228 227
229 228 /*!
230 229 Zooms in the view to a maximum level at which \a rect is still fully visible.
231 230 */
232 231 void QChart::zoomIn(const QRectF& rect)
233 232 {
234 233 if (!rect.isValid()) return;
235 234 d_ptr->m_presenter->zoomIn(rect);
236 235 }
237 236
238 237 /*!
239 238 Restores the view zoom level to the previous one.
240 239 */
241 240 void QChart::zoomOut()
242 241 {
243 242 d_ptr->m_presenter->zoomOut();
244 243 }
245 244
246 245 /*!
247 246 Returns the pointer to the x axis object of the chart
248 247 */
249 248 QChartAxis* QChart::axisX() const
250 249 {
251 250 return d_ptr->m_dataset->axisX();
252 251 }
253 252
254 253 /*!
255 254 Returns the pointer to the y axis object of the chart
256 255 */
257 256 QChartAxis* QChart::axisY() const
258 257 {
259 258 return d_ptr->m_dataset->axisY();
260 259 }
261 260
262 261 /*!
263 262 Returns the legend object of the chart. Ownership stays in chart.
264 263 */
265 264 QLegend* QChart::legend() const
266 265 {
267 266 return d_ptr->m_legend;
268 267 }
269 268
270 269 QRect QChart::margins() const
271 270 {
272 271 return d_ptr->m_presenter->margins();
273 272 }
274 273
275 274
276 275 /*!
277 276 Resizes and updates the chart area using the \a event data
278 277 */
279 278 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
280 279 {
281 280 d_ptr->m_rect = QRectF(QPoint(0,0),event->newSize());
282 281 QGraphicsWidget::resizeEvent(event);
283 282 d_ptr->m_presenter->setGeometry(d_ptr->m_rect);
284 283 }
285 284
286 285 /*!
287 286 Sets animation \a options for the chart
288 287 */
289 288 void QChart::setAnimationOptions(AnimationOptions options)
290 289 {
291 290 d_ptr->m_presenter->setAnimationOptions(options);
292 291 }
293 292
294 293 /*!
295 294 Returns animation options for the chart
296 295 */
297 296 QChart::AnimationOptions QChart::animationOptions() const
298 297 {
299 298 return d_ptr->m_presenter->animationOptions();
300 299 }
301 300
302 301 void QChart::scrollLeft()
303 302 {
304 303 d_ptr->m_presenter->scroll(-d_ptr->m_presenter->chartGeometry().width()/(axisX()->ticksCount()-1),0);
305 304 }
306 305
307 306 void QChart::scrollRight()
308 307 {
309 308 d_ptr->m_presenter->scroll(d_ptr->m_presenter->chartGeometry().width()/(axisX()->ticksCount()-1),0);
310 309 }
311 310
312 311 void QChart::scrollUp()
313 312 {
314 313 d_ptr->m_presenter->scroll(0,d_ptr->m_presenter->chartGeometry().width()/(axisY()->ticksCount()-1));
315 314 }
316 315
317 316 void QChart::scrollDown()
318 317 {
319 318 d_ptr->m_presenter->scroll(0,-d_ptr->m_presenter->chartGeometry().width()/(axisY()->ticksCount()-1));
320 319 }
321 320
322 321 void QChart::setBackgroundVisible(bool visible)
323 322 {
324 323 d_ptr->m_presenter->createChartBackgroundItem();
325 324 d_ptr->m_presenter->m_backgroundItem->setVisible(visible);
326 325 }
327 326
328 327 bool QChart::isBackgroundVisible() const
329 328 {
330 329 if (!d_ptr->m_presenter->m_backgroundItem) return false;
331 330 return d_ptr->m_presenter->m_backgroundItem->isVisible();
332 331 }
333 332
334 333 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
335 334
336 335 QChartPrivate::QChartPrivate():
337 336 m_legend(0),
338 337 m_dataset(0),
339 338 m_presenter(0)
340 339 {
341 340
342 341 }
343 342
344 343 QChartPrivate::~QChartPrivate()
345 344 {
346 345
347 346 }
348 347
348 void QChartPrivate::createConnections()
349 {
350 QObject::connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
351 QObject::connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*)));
352 QObject::connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_presenter,SLOT(handleSeriesAdded(QSeries*,Domain*)));
353 QObject::connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_presenter,SLOT(handleSeriesRemoved(QSeries*)));
354 QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*,Domain*)),m_presenter,SLOT(handleAxisAdded(QChartAxis*,Domain*)));
355 QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),m_presenter,SLOT(handleAxisRemoved(QChartAxis*)));
356 }
357
349 358 #include "moc_qchart.cpp"
350 359
351 360 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,53 +1,55
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 // W A R N I N G
22 22 // -------------
23 23 //
24 24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 25 // implementation detail. This header file may change from version to
26 26 // version without notice, or even be removed.
27 27 //
28 28 // We mean it.
29 29
30 30 #ifndef QCHART_P_H
31 31 #define QCHART_P_H
32 32
33 33 #include "qlegend.h"
34 34 #include "chartpresenter_p.h"
35 35 #include "chartdataset_p.h"
36 36
37 37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38 38
39 39 class QChart;
40 40
41 41 struct QChartPrivate
42 42 {
43 43 QChartPrivate();
44 44 ~QChartPrivate();
45 45 QRectF m_rect;
46 46 QLegend* m_legend;
47 47 ChartDataSet *m_dataset;
48 48 ChartPresenter *m_presenter;
49 49
50 void createConnections();
51
50 52 };
51 53
52 54 QTCOMMERCIALCHART_END_NAMESPACE
53 55 #endif
@@ -1,529 +1,523
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 "qchart_p.h"
23 23 #include "qseries.h"
24 24 #include "legendmarker_p.h"
25 25 #include "qxyseries.h"
26 26 #include "qlineseries.h"
27 27 #include "qareaseries.h"
28 28 #include "qscatterseries.h"
29 29 #include "qsplineseries.h"
30 30 #include "qbarseries.h"
31 31 #include "qstackedbarseries.h"
32 32 #include "qpercentbarseries.h"
33 33 #include "qbarset.h"
34 34 #include "qpieseries.h"
35 35 #include "qpieslice.h"
36 36 #include "chartpresenter_p.h"
37 37 #include <QPainter>
38 38 #include <QPen>
39 39 #include <QTimer>
40 40
41 41 #include <QGraphicsSceneEvent>
42 42
43 43 QTCOMMERCIALCHART_BEGIN_NAMESPACE
44 44
45 45 /*!
46 46 \class QLegend
47 47 \brief part of QtCommercial chart API.
48 48
49 49 QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when
50 50 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
51 51 handle the drawing manually.
52 52 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
53 53
54 54 \mainclass
55 55
56 56 \sa QChart, QSeries
57 57 */
58 58
59 59 /*!
60 60 \enum QLegend::Layout
61 61
62 62 This enum describes the possible position for legend inside chart.
63 63
64 64 \value LayoutTop
65 65 \value LayoutBottom
66 66 \value LayoutLeft
67 67 \value LayoutRight
68 68 */
69 69
70 70
71 71 /*!
72 72 \fn void QLegend::clicked(QSeries* series, Qt::MouseButton button)
73 73 \brief Notifies when series has been clicked on legend \a series \a button
74 74 */
75 75
76 76 /*!
77 77 \fn void QLegend::clicked(QBarSet* barset, Qt::MouseButton button)
78 78 \brief Notifies when barset has been clicked on legend \a barset \a button
79 79 */
80 80
81 81 /*!
82 82 \fn void QLegend::clicked(QPieSlice* slice, Qt::MouseButton button)
83 83 \brief Notifies when pie slice has been clicked on legend \a slice \a button
84 84 */
85 85
86 86 /*!
87 87 Constructs the legend object and sets the parent to \a parent
88 88 */
89 89
90 90 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
91 91 m_margin(5),
92 92 m_offsetX(0),
93 93 m_offsetY(0),
94 94 m_brush(Qt::darkGray), // TODO: default should come from theme
95 95 m_alignment(QLegend::AlignmentTop),
96 96 m_markers(new QGraphicsItemGroup(this)),
97 97 m_attachedToChart(true),
98 98 m_chart(chart),
99 99 m_minWidth(0),
100 100 m_minHeight(0),
101 101 m_width(0),
102 102 m_height(0),
103 m_visible(false),
104 m_dirty(false)
103 m_visible(false)
105 104 {
106 105 setZValue(ChartPresenter::LegendZValue);
107 106 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
108 107 }
109 108
110 109 /*!
111 110 Paints the legend to given \a painter. Paremeters \a option and \a widget arent used.
112 111 */
113 112
114 113 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
115 114 {
116 115 Q_UNUSED(option)
117 116 Q_UNUSED(widget)
118 117 if(!m_visible) return;
119 118
120 119 painter->setOpacity(opacity());
121 120 painter->setPen(m_pen);
122 121 painter->setBrush(m_brush);
123 122 painter->drawRect(boundingRect());
124 123 }
125 124
126 125 /*!
127 126 Bounding rect of legend.
128 127 */
129 128
130 129 QRectF QLegend::boundingRect() const
131 130 {
132 131 return m_rect;
133 132 }
134 133
135 134 /*!
136 135 Sets the \a brush of legend. Brush affects the background of legend.
137 136 */
138 137 void QLegend::setBrush(const QBrush &brush)
139 138 {
140 139 if (m_brush != brush) {
141 140 m_brush = brush;
142 141 update();
143 142 }
144 143 }
145 144
146 145 /*!
147 146 Returns the brush used by legend.
148 147 */
149 148 QBrush QLegend::brush() const
150 149 {
151 150 return m_brush;
152 151 }
153 152
154 153 /*!
155 154 Sets the \a pen of legend. Pen affects the legend borders.
156 155 */
157 156 void QLegend::setPen(const QPen &pen)
158 157 {
159 158 if (m_pen != pen) {
160 159 m_pen = pen;
161 160 update();
162 161 }
163 162 }
164 163
165 164 /*!
166 165 Returns the pen used by legend
167 166 */
168 167
169 168 QPen QLegend::pen() const
170 169 {
171 170 return m_pen;
172 171 }
173 172
174 173 /*!
175 174 Sets the \a preferred layout for legend. Legend tries to paint itself on the defined position in chart.
176 175 \sa QLegend::Layout
177 176 */
178 177 void QLegend::setAlignmnent(QLegend::Alignments alignment)
179 178 {
180 179 if(m_alignment!=alignment && m_attachedToChart) {
181 180 m_alignment = alignment;
182 181 updateLayout();
183 182 }
184 183 }
185 184
186 185 /*!
187 186 Returns the preferred layout for legend
188 187 */
189 188 QLegend::Alignments QLegend::alignment() const
190 189 {
191 190 return m_alignment;
192 191 }
193 192
194 193 /*!
195 194 \internal \a series \a domain Should be called when series is added to chart.
196 195 */
197 196 void QLegend::handleSeriesAdded(QSeries *series, Domain *domain)
198 197 {
199 198 Q_UNUSED(domain)
200 199
201 200 switch (series->type())
202 201 {
203 202 case QSeries::SeriesTypeLine: {
204 203 QLineSeries *lineSeries = static_cast<QLineSeries *>(series);
205 204 appendMarkers(lineSeries);
206 205 break;
207 206 }
208 207 case QSeries::SeriesTypeArea: {
209 208 QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series);
210 209 appendMarkers(areaSeries);
211 210 break;
212 211 }
213 212 case QSeries::SeriesTypeBar: {
214 213 QBarSeries *barSeries = static_cast<QBarSeries *>(series);
215 214 appendMarkers(barSeries);
216 215 break;
217 216 }
218 217 case QSeries::SeriesTypeStackedBar: {
219 218 QStackedBarSeries *stackedBarSeries = static_cast<QStackedBarSeries *>(series);
220 219 appendMarkers(stackedBarSeries);
221 220 break;
222 221 }
223 222 case QSeries::SeriesTypePercentBar: {
224 223 QPercentBarSeries *percentBarSeries = static_cast<QPercentBarSeries *>(series);
225 224 appendMarkers(percentBarSeries);
226 225 break;
227 226 }
228 227 case QSeries::SeriesTypeScatter: {
229 228 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
230 229 appendMarkers(scatterSeries);
231 230 break;
232 231 }
233 232 case QSeries::SeriesTypePie: {
234 233 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
235 234 appendMarkers(pieSeries);
236 235 connect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>)));
237 236 break;
238 237 }
239 238 case QSeries::SeriesTypeSpline: {
240 239 QSplineSeries *splineSeries = static_cast<QSplineSeries *>(series);
241 240 appendMarkers(splineSeries);
242 241 break;
243 242 }
244 243 default: {
245 244 qWarning()<< "QLegend::handleSeriesAdded" << series->type() << "unknown series type.";
246 245 break;
247 246 }
248 247 }
249 248
250 // wait for all series added
251 if(!m_dirty){
252 QTimer::singleShot(0,this,SLOT(updateLayout()));
253 m_dirty=true;
254 }
249 updateLayout();
255 250 }
256 251
257 252 /*!
258 253 \internal \a series Should be called when series is removed from chart.
259 254 */
260 255 void QLegend::handleSeriesRemoved(QSeries *series)
261 256 {
262 257 switch (series->type())
263 258 {
264 259 case QSeries::SeriesTypeArea: {
265 260 QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series);
266 261 deleteMarkers(areaSeries);
267 262 break;
268 263 }
269 264 case QSeries::SeriesTypePie: {
270 265 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
271 266 disconnect(pieSeries, SIGNAL(added(QList<QPieSlice *>)), this, SLOT(handleAdded(QList<QPieSlice *>)));
272 267 deleteMarkers(series);
273 268 break;
274 269 }
275 270 default: {
276 271 // All other types
277 272 deleteMarkers(series);
278 273 break;
279 274 }
280 275 }
281 276
282 277 updateLayout();
283 278 }
284 279
285 280 /*!
286 281 \internal \a slices Should be called when slices are added to pie chart.
287 282 */
288 283 void QLegend::handleAdded(QList<QPieSlice *> slices)
289 284 {
290 285 QPieSeries* series = static_cast<QPieSeries *> (sender());
291 286 foreach(QPieSlice* slice, slices) {
292 287 PieLegendMarker* marker = new PieLegendMarker(series,slice, this);
293 288 m_markers->addToGroup(marker);
294 289 }
295 290 updateLayout();
296 291 }
297 292
298 293 /*!
299 294 \internal \a slices Should be called when slices are removed from pie chart. Currently unused,
300 295 because removed slices are also deleted and we listen destroyed signal
301 296 */
302 297 void QLegend::handleRemoved(QList<QPieSlice *> slices)
303 298 {
304 299 Q_UNUSED(slices)
305 300 }
306 301
307 302 /*!
308 303 Detaches the legend from chart. Chart won't change layout of the legend.
309 304 */
310 305 void QLegend::detachFromChart()
311 306 {
312 307 m_attachedToChart = false;
313 308 }
314 309
315 310 /*!
316 311 Attaches the legend to chart. Chart may change layout of the legend.
317 312 */
318 313 void QLegend::attachToChart()
319 314 {
320 315 m_attachedToChart = true;
321 316 }
322 317
323 318 /*!
324 319 Returns true, if legend is attached to chart.
325 320 */
326 321 bool QLegend::isAttachedToChart()
327 322 {
328 323 return m_attachedToChart;
329 324 }
330 325
331 326 /*!
332 327 \internal Helper function. Appends markers from \a series to legend.
333 328 */
334 329 void QLegend::appendMarkers(QAreaSeries* series)
335 330 {
336 331 AreaLegendMarker* marker = new AreaLegendMarker(series,this);
337 332 m_markers->addToGroup(marker);
338 333 }
339 334
340 335 /*!
341 336 \internal Helper function. Appends markers from \a series to legend.
342 337 */
343 338 void QLegend::appendMarkers(QXYSeries* series)
344 339 {
345 340 XYLegendMarker* marker = new XYLegendMarker(series,this);
346 341 m_markers->addToGroup(marker);
347 342 }
348 343
349 344 /*!
350 345 \internal Helper function. Appends markers from \a series to legend.
351 346 */
352 347 void QLegend::appendMarkers(QBarSeries *series)
353 348 {
354 349 foreach(QBarSet* set, series->barSets()) {
355 350 BarLegendMarker* marker = new BarLegendMarker(series,set, this);
356 351 m_markers->addToGroup(marker);
357 352 }
358 353 }
359 354
360 355 /*!
361 356 \internal Helper function. Appends markers from \a series to legend.
362 357 */
363 358 void QLegend::appendMarkers(QPieSeries *series)
364 359 {
365 360 foreach(QPieSlice* slice, series->slices()) {
366 361 PieLegendMarker* marker = new PieLegendMarker(series,slice, this);
367 362 m_markers->addToGroup(marker);
368 363 }
369 364 }
370 365
371 366 /*!
372 367 \internal Deletes all markers that are created from \a series
373 368 */
374 369 void QLegend::deleteMarkers(QSeries *series)
375 370 {
376 371 // Search all markers that belong to given series and delete them.
377 372
378 373 QList<QGraphicsItem *> items = m_markers->childItems();
379 374
380 375 foreach (QGraphicsItem *markers, items) {
381 376 LegendMarker *marker = static_cast<LegendMarker*>(markers);
382 377 if (marker->series() == series) {
383 378 delete marker;
384 379 }
385 380 }
386 381 }
387 382
388 383 /*!
389 384 \internal Updates layout of legend. Tries to fit as many markers as possible up to the maximum size of legend.
390 385 If items don't fit, sets the visibility of scroll buttons accordingly.
391 386 Causes legend to be resized.
392 387 */
393 388
394 389 void QLegend::setOffset(const QPointF& point)
395 390 {
396 391
397 392 switch(m_alignment) {
398 393
399 394 case AlignmentTop:
400 395 case AlignmentBottom: {
401 396 if(m_width<=m_rect.width()) return;
402 397
403 398 if (point.x() != m_offsetX) {
404 399 m_offsetX = qBound(0.0, point.x(), m_width - m_rect.width());
405 400 m_markers->setPos(-m_offsetX,m_rect.top());
406 401 }
407 402 break;
408 403 }
409 404 case AlignmentLeft:
410 405 case AlignmentRight: {
411 406
412 407 if(m_height<=m_rect.height()) return;
413 408
414 409 if (point.y() != m_offsetY) {
415 410 m_offsetY = qBound(0.0, point.y(), m_height - m_rect.height());
416 411 m_markers->setPos(m_rect.left(),-m_offsetY);
417 412 }
418 413 break;
419 414 }
420 415 }
421 416 }
422 417
423 418 QPointF QLegend::offset() const
424 419 {
425 420 return QPointF(m_offsetX,m_offsetY);
426 421 }
427 422
428 423 // this function runs first to set min max values
429 424 void QLegend::updateLayout()
430 425 {
431 m_dirty=false;
432 426 m_offsetX=0;
433 427 QList<QGraphicsItem *> items = m_markers->childItems();
434 428
435 429 if(items.isEmpty()) return;
436 430
437 431 m_minWidth=0;
438 432 m_minHeight=0;
439 433
440 434 switch(m_alignment) {
441 435
442 436 case AlignmentTop:
443 437 case AlignmentBottom: {
444 438 QPointF point = m_rect.topLeft();
445 439 m_width = 0;
446 440 foreach (QGraphicsItem *item, items) {
447 441 item->setPos(point.x(),m_rect.height()/2 -item->boundingRect().height()/2);
448 442 const QRectF& rect = item->boundingRect();
449 443 qreal w = rect.width();
450 444 m_minWidth=qMax(m_minWidth,w);
451 445 m_minHeight=qMax(m_minHeight,rect.height());
452 446 m_width+=w;
453 447 point.setX(point.x() + w);
454 448 }
455 449 if(m_width<m_rect.width()){
456 450 m_markers->setPos(m_rect.width()/2-m_width/2,m_rect.top());
457 451 }else{
458 452 m_markers->setPos(m_rect.topLeft());
459 453 }
460 454 m_height=m_minHeight;
461 455 }
462 456 break;
463 457 case AlignmentLeft:
464 458 case AlignmentRight:{
465 459 QPointF point = m_rect.topLeft();
466 460 m_height = 0;
467 461 foreach (QGraphicsItem *item, items) {
468 462 item->setPos(point);
469 463 const QRectF& rect = item->boundingRect();
470 464 qreal h = rect.height();
471 465 m_minWidth=qMax(m_minWidth,rect.width());
472 466 m_minHeight=qMax(m_minHeight,h);
473 467 m_height+=h;
474 468 point.setY(point.y() + h);
475 469 }
476 470 if(m_height<m_rect.height()){
477 471 m_markers->setPos(m_rect.left(),m_rect.height()/2-m_height/2);
478 472 }else{
479 473 m_markers->setPos(m_rect.topLeft());
480 474 }
481 475 m_width=m_minWidth;
482 476 }
483 477 break;
484 478 }
485 479
486 480 m_chart->d_ptr->m_presenter->updateLayout(); //TODO fixme;
487 481 }
488 482
489 483 void QLegend::setBackgroundVisible(bool visible)
490 484 {
491 485 if(m_visible!=visible)
492 486 {
493 487 m_visible=visible;
494 488 update();
495 489 }
496 490 }
497 491
498 492 bool QLegend::isBackgroundVisible() const
499 493 {
500 494 return m_visible;
501 495 }
502 496
503 497 void QLegend::resizeEvent(QGraphicsSceneResizeEvent *event)
504 498 {
505 499 const QRectF& rect = QRectF(QPoint(0,0),event->newSize());
506 500 QGraphicsWidget::resizeEvent(event);
507 501 if(m_rect != rect){
508 502 m_rect = rect;
509 503 updateLayout();
510 504 }
511 505 }
512 506
513 507 void QLegend::hideEvent(QHideEvent *event)
514 508 {
515 509 QGraphicsWidget::hideEvent(event);
516 510 setEnabled(false);
517 511 updateLayout();
518 512 }
519 513
520 514 void QLegend::showEvent(QShowEvent *event)
521 515 {
522 516 QGraphicsWidget::showEvent(event);
523 517 setEnabled(true);
524 518 updateLayout();
525 519 }
526 520
527 521 #include "moc_qlegend.cpp"
528 522
529 523 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,177 +1,171
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 #include "private/scroller_p.h" //TODO fixme
29 29
30 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 31
32 32 class Domain;
33 33 class LegendMarker;
34 34 class QPieSlice;
35 35 class QXYSeries;
36 36 class QBarSet;
37 37 class QBarSeries;
38 38 class QPieSeries;
39 39 class QAreaSeries;
40 40 class LegendScrollButton;
41 41 class QSeries;
42 42 class QChart;
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
94 94 public Q_SLOTS:
95 95 // PIMPL --->
96 96 void handleSeriesAdded(QSeries *series, Domain *domain);
97 97 void handleSeriesRemoved(QSeries *series);
98 98 void handleAdded(QList<QPieSlice *> slices);
99 99 void handleRemoved(QList<QPieSlice *> slices);
100 100 // PIMPL <---
101 101
102 102 private:
103 103 // PIMPL --->
104 104 void appendMarkers(QAreaSeries *series);
105 105 void appendMarkers(QXYSeries *series);
106 106 void appendMarkers(QBarSeries *series);
107 107 void appendMarkers(QPieSeries *series);
108 108 void deleteMarkers(QSeries *series);
109
110
111
112
113 private Q_SLOTS:
114 109 void updateLayout();
115 110
116 111 private:
117 112 qreal m_margin;
118 113
119 114 QRectF m_rect;
120 115 qreal m_offsetX;
121 116 qreal m_offsetY;
122 117
123 118 //QList<LegendMarker *> m_markers;
124 119
125 120 QBrush m_brush;
126 121 QPen m_pen;
127 122 QLegend::Alignments m_alignment;
128 123 QGraphicsItemGroup* m_markers;
129 124
130 125
131 126 bool m_attachedToChart;
132 127
133 128 QChart *m_chart;
134 129 qreal m_minWidth;
135 130 qreal m_minHeight;
136 131 qreal m_width;
137 132 qreal m_height;
138 133 bool m_visible;
139 bool m_dirty;
140 134 friend class ScrolledQLegend;
141 135 // <--- PIMPL
142 136 };
143 137
144 138 class ScrolledQLegend: public QLegend, public Scroller
145 139 {
146 140
147 141 public:
148 142 ScrolledQLegend(QChart *chart):QLegend(chart)
149 143 {
150 144 }
151 145
152 146 void setOffset(const QPointF& point)
153 147 {
154 148 QLegend::setOffset(point);
155 149 }
156 150 QPointF offset() const
157 151 {
158 152 return QLegend::offset();
159 153 }
160 154
161 155 void mousePressEvent(QGraphicsSceneMouseEvent* event){
162 156 Scroller::mousePressEvent(event);
163 157 //QLegend::mousePressEvent(event);
164 158 }
165 159 void mouseMoveEvent(QGraphicsSceneMouseEvent* event){
166 160 Scroller::mouseMoveEvent(event);
167 161 //QLegend::mouseMoveEvent(event);
168 162 }
169 163 void mouseReleaseEvent(QGraphicsSceneMouseEvent* event){
170 164 Scroller::mouseReleaseEvent(event);
171 165 //QLegend::mouseReleaseEvent(event);
172 166 }
173 167 };
174 168
175 169 QTCOMMERCIALCHART_END_NAMESPACE
176 170
177 171 #endif // QLEGEND_H
General Comments 0
You need to be logged in to leave comments. Login now