##// END OF EJS Templates
Polishing qchart class
Michal Klocek -
r742:c04f89af81a0
parent child
Show More
@@ -33,7 +33,9 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(char
33 m_chartTheme(0),
33 m_chartTheme(0),
34 m_rect(QRectF(QPoint(0,0),m_chart->size())),
34 m_rect(QRectF(QPoint(0,0),m_chart->size())),
35 m_options(QChart::NoAnimation),
35 m_options(QChart::NoAnimation),
36 m_themeForce(false)
36 m_themeForce(false),
37 m_padding(50),
38 m_backgroundPadding(10)
37 {
39 {
38 createConnections();
40 createConnections();
39 setTheme(QChart::ChartThemeDefault,false);
41 setTheme(QChart::ChartThemeDefault,false);
@@ -53,16 +55,10 void ChartPresenter::createConnections()
53 QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*)));
55 QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*)));
54 }
56 }
55
57
56
57 QRectF ChartPresenter::geometry() const
58 {
59 return m_rect;
60 }
61
62 void ChartPresenter::handleGeometryChanged()
58 void ChartPresenter::handleGeometryChanged()
63 {
59 {
64 QRectF rect(QPoint(0,0),m_chart->size());
60 QRectF rect(QPoint(0,0),m_chart->size());
65 rect.adjust(m_chart->padding(),m_chart->padding(), -m_chart->padding(), -m_chart->padding());
61 rect.adjust(m_padding,m_padding,-m_padding,-m_padding);
66
62
67 //rewrite zoom stack
63 //rewrite zoom stack
68 /*
64 /*
@@ -328,7 +324,7 void ChartPresenter::zoomIn()
328 void ChartPresenter::zoomIn(const QRectF& rect)
324 void ChartPresenter::zoomIn(const QRectF& rect)
329 {
325 {
330 QRectF r = rect.normalized();
326 QRectF r = rect.normalized();
331 r.translate(-m_chart->padding(), -m_chart->padding());
327 r.translate(-m_padding, -m_padding);
332 if(m_animator) {
328 if(m_animator) {
333
329
334 QPointF point(r.center().x()/geometry().width(),r.center().y()/geometry().height());
330 QPointF point(r.center().x()/geometry().width(),r.center().y()/geometry().height());
@@ -349,7 +345,7 void ChartPresenter::zoomOut()
349
345
350 QSizeF size = geometry().size();
346 QSizeF size = geometry().size();
351 QRectF rect = geometry();
347 QRectF rect = geometry();
352 rect.translate(-m_chart->padding(), -m_chart->padding());
348 rect.translate(-m_padding, -m_padding);
353 m_dataset->zoomOutDomain(rect.adjusted(size.width()/4,size.height()/4,-size.width()/4,-size.height()/4),size);
349 m_dataset->zoomOutDomain(rect.adjusted(size.width()/4,size.height()/4,-size.width()/4,-size.height()/4),size);
354 //m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size());
350 //m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size());
355
351
@@ -35,14 +35,13 public:
35 ChartPresenter(QChart* chart,ChartDataSet *dataset);
35 ChartPresenter(QChart* chart,ChartDataSet *dataset);
36 virtual ~ChartPresenter();
36 virtual ~ChartPresenter();
37
37
38 void setMargin(int margin);
38 int padding() const { return m_padding; }
39 int margin() const;
39 int backgroundPadding() const { return m_backgroundPadding; }
40
40 QRectF geometry() const { return m_rect; }
41 QRectF geometry() const;
41 ChartAnimator* animator() const { return m_animator; }
42
42 ChartTheme *chartTheme() const { return m_chartTheme; }
43 ChartAnimator* animator() const {return m_animator;}
43 ChartDataSet *dataSet() const { return m_dataset; }
44 ChartTheme *chartTheme() { return m_chartTheme; }
44 QGraphicsItem* rootItem() const { return m_chart; }
45 ChartDataSet *dataSet() { return m_dataset; }
46
45
47 void setTheme(QChart::ChartTheme theme,bool force = true);
46 void setTheme(QChart::ChartTheme theme,bool force = true);
48 QChart::ChartTheme theme();
47 QChart::ChartTheme theme();
@@ -50,8 +49,6 public:
50 void setAnimationOptions(QChart::AnimationOptions options);
49 void setAnimationOptions(QChart::AnimationOptions options);
51 QChart::AnimationOptions animationOptions() const;
50 QChart::AnimationOptions animationOptions() const;
52
51
53 QGraphicsItem* rootItem() const {return m_chart;};
54
55 void zoomIn();
52 void zoomIn();
56 void zoomIn(const QRectF& rect);
53 void zoomIn(const QRectF& rect);
57 void zoomOut();
54 void zoomOut();
@@ -81,6 +78,8 private:
81 QRectF m_rect;
78 QRectF m_rect;
82 QChart::AnimationOptions m_options;
79 QChart::AnimationOptions m_options;
83 bool m_themeForce;
80 bool m_themeForce;
81 int m_padding;
82 int m_backgroundPadding;
84
83
85 };
84 };
86
85
@@ -49,7 +49,6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
49 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
49 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
50 d_ptr(new QChartPrivate(this))
50 d_ptr(new QChartPrivate(this))
51 {
51 {
52
53 d_ptr->m_legend = new QLegend(this);
52 d_ptr->m_legend = new QLegend(this);
54 d_ptr->m_dataset = new ChartDataSet(this);
53 d_ptr->m_dataset = new ChartDataSet(this);
55 d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset);
54 d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset);
@@ -111,7 +110,7 void QChart::setBackgroundBrush(const QBrush& brush)
111
110
112 QBrush QChart::backgroundBrush() const
111 QBrush QChart::backgroundBrush() const
113 {
112 {
114 if(!d_ptr->m_backgroundItem) return QBrush();
113 if (!d_ptr->m_backgroundItem) return QBrush();
115 return (d_ptr->m_backgroundItem)->brush();
114 return (d_ptr->m_backgroundItem)->brush();
116 }
115 }
117
116
@@ -127,7 +126,7 void QChart::setBackgroundPen(const QPen& pen)
127
126
128 QPen QChart::backgroundPen() const
127 QPen QChart::backgroundPen() const
129 {
128 {
130 if(!d_ptr->m_backgroundItem) return QPen();
129 if (!d_ptr->m_backgroundItem) return QPen();
131 return d_ptr->m_backgroundItem->pen();
130 return d_ptr->m_backgroundItem->pen();
132 }
131 }
133
132
@@ -146,7 +145,7 void QChart::setTitle(const QString& title)
146 */
145 */
147 QString QChart::title() const
146 QString QChart::title() const
148 {
147 {
149 if(d_ptr->m_titleItem)
148 if (d_ptr->m_titleItem)
150 return d_ptr->m_titleItem->text();
149 return d_ptr->m_titleItem->text();
151 else
150 else
152 return QString();
151 return QString();
@@ -177,7 +176,7 void QChart::setTitleBrush(const QBrush &brush)
177 */
176 */
178 QBrush QChart::titleBrush() const
177 QBrush QChart::titleBrush() const
179 {
178 {
180 if(!d_ptr->m_titleItem) return QBrush();
179 if (!d_ptr->m_titleItem) return QBrush();
181 return d_ptr->m_titleItem->brush();
180 return d_ptr->m_titleItem->brush();
182 }
181 }
183
182
@@ -212,7 +211,7 void QChart::zoomIn()
212 */
211 */
213 void QChart::zoomIn(const QRectF& rect)
212 void QChart::zoomIn(const QRectF& rect)
214 {
213 {
215 if(!rect.isValid()) return;
214 if (!rect.isValid()) return;
216 d_ptr->m_presenter->zoomIn(rect);
215 d_ptr->m_presenter->zoomIn(rect);
217 }
216 }
218
217
@@ -325,35 +324,6 void QChart::scrollDown()
325 d_ptr->m_presenter->scroll(0,-d_ptr->m_presenter->geometry().width()/(axisY()->ticksCount()-1));
324 d_ptr->m_presenter->scroll(0,-d_ptr->m_presenter->geometry().width()/(axisY()->ticksCount()-1));
326 }
325 }
327
326
328 void QChart::setPadding(int padding)
329 {
330 if(d_ptr->m_padding==padding) {
331 d_ptr->m_padding = padding;
332 d_ptr->m_presenter->handleGeometryChanged();
333 d_ptr->updateLayout();
334 }
335 }
336
337 int QChart::padding() const
338 {
339 return d_ptr->m_padding;
340 }
341
342 void QChart::setBackgroundPadding(int padding)
343 {
344 if(d_ptr->m_backgroundPadding!=padding) {
345 d_ptr->m_backgroundPadding = padding;
346 d_ptr->updateLayout();
347 }
348 }
349
350 void QChart::setBackgroundDiameter(int diameter)
351 {
352 d_ptr->createChartBackgroundItem();
353 d_ptr->m_backgroundItem->setDimeter(diameter);
354 d_ptr->m_backgroundItem->update();
355 }
356
357 void QChart::setBackgroundVisible(bool visible)
327 void QChart::setBackgroundVisible(bool visible)
358 {
328 {
359 d_ptr->createChartBackgroundItem();
329 d_ptr->createChartBackgroundItem();
@@ -362,7 +332,7 void QChart::setBackgroundVisible(bool visible)
362
332
363 bool QChart::isBackgroundVisible() const
333 bool QChart::isBackgroundVisible() const
364 {
334 {
365 if(!d_ptr->m_backgroundItem) return false;
335 if (!d_ptr->m_backgroundItem) return false;
366 return d_ptr->m_backgroundItem->isVisible();
336 return d_ptr->m_backgroundItem->isVisible();
367 }
337 }
368
338
@@ -374,9 +344,7 m_backgroundItem(0),
374 m_titleItem(0),
344 m_titleItem(0),
375 m_legend(0),
345 m_legend(0),
376 m_dataset(0),
346 m_dataset(0),
377 m_presenter(0),
347 m_presenter(0)
378 m_padding(50),
379 m_backgroundPadding(10)
380 {
348 {
381
349
382 }
350 }
@@ -388,7 +356,7 QChartPrivate::~QChartPrivate()
388
356
389 void QChartPrivate::createChartBackgroundItem()
357 void QChartPrivate::createChartBackgroundItem()
390 {
358 {
391 if(!m_backgroundItem) {
359 if (!m_backgroundItem) {
392 m_backgroundItem = new ChartBackground(q_ptr);
360 m_backgroundItem = new ChartBackground(q_ptr);
393 m_backgroundItem->setPen(Qt::NoPen);
361 m_backgroundItem->setPen(Qt::NoPen);
394 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
362 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
@@ -397,7 +365,7 void QChartPrivate::createChartBackgroundItem()
397
365
398 void QChartPrivate::createChartTitleItem()
366 void QChartPrivate::createChartTitleItem()
399 {
367 {
400 if(!m_titleItem) {
368 if (!m_titleItem) {
401 m_titleItem = new QGraphicsSimpleTextItem(q_ptr);
369 m_titleItem = new QGraphicsSimpleTextItem(q_ptr);
402 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
370 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
403 }
371 }
@@ -405,26 +373,27 void QChartPrivate::createChartTitleItem()
405
373
406 void QChartPrivate::updateLegendLayout()
374 void QChartPrivate::updateLegendLayout()
407 {
375 {
408 QRectF plotRect = m_rect.adjusted(m_padding,m_padding, -m_padding, -m_padding);
376 int padding = m_presenter->padding();
377 QRectF plotRect = m_rect.adjusted(padding,padding,-padding,-padding);
409 QRectF legendRect;
378 QRectF legendRect;
410
379
411 switch (m_legend->preferredLayout())
380 switch (m_legend->preferredLayout())
412 {
381 {
413 case QLegend::PreferredLayoutTop: {
382 case QLegend::PreferredLayoutTop: {
414 // legendRect = plotRect.adjusted(m_padding,0,-m_padding,-m_padding - plotRect.height());
383 // legendRect = plotRect.adjusted(m_padding,0,-m_padding,-m_padding - plotRect.height());
415 legendRect = plotRect.adjusted(0,0,0,-m_padding - plotRect.height());
384 legendRect = plotRect.adjusted(0,0,0,-padding - plotRect.height());
416 break;
385 break;
417 }
386 }
418 case QLegend::PreferredLayoutBottom: {
387 case QLegend::PreferredLayoutBottom: {
419 legendRect = plotRect.adjusted(m_padding,m_padding + plotRect.height(),-m_padding,0);
388 legendRect = plotRect.adjusted(padding,padding + plotRect.height(),-padding,0);
420 break;
389 break;
421 }
390 }
422 case QLegend::PreferredLayoutLeft: {
391 case QLegend::PreferredLayoutLeft: {
423 legendRect = plotRect.adjusted(0,m_padding,-m_padding - plotRect.width(),-m_padding);
392 legendRect = plotRect.adjusted(0,padding,-padding - plotRect.width(),-padding);
424 break;
393 break;
425 }
394 }
426 case QLegend::PreferredLayoutRight: {
395 case QLegend::PreferredLayoutRight: {
427 legendRect = plotRect.adjusted(m_padding + plotRect.width(),m_padding,0,-m_padding);
396 legendRect = plotRect.adjusted(padding + plotRect.width(),padding,0,-padding);
428 break;
397 break;
429 }
398 }
430 default: {
399 default: {
@@ -439,19 +408,22 void QChartPrivate::updateLegendLayout()
439
408
440 void QChartPrivate::updateLayout()
409 void QChartPrivate::updateLayout()
441 {
410 {
442 if(!m_rect.isValid()) return;
411 if (!m_rect.isValid()) return;
412
413 int padding = m_presenter->padding();
414 int backgroundPadding = m_presenter->backgroundPadding();
443
415
444 QRectF rect = m_rect.adjusted(m_padding,m_padding, -m_padding, -m_padding);
416 QRectF rect = m_rect.adjusted(padding,padding,-padding,-padding);
445
417
446 // recalculate title position
418 // recalculate title position
447 if (m_titleItem) {
419 if (m_titleItem) {
448 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
420 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
449 m_titleItem->setPos(center.x(),m_rect.top()/2 + m_padding/2);
421 m_titleItem->setPos(center.x(),m_rect.top()/2 + padding/2);
450 }
422 }
451
423
452 //recalculate background gradient
424 //recalculate background gradient
453 if (m_backgroundItem) {
425 if (m_backgroundItem) {
454 m_backgroundItem->setRect(m_rect.adjusted(m_backgroundPadding,m_backgroundPadding, -m_backgroundPadding, -m_backgroundPadding));
426 m_backgroundItem->setRect(m_rect.adjusted(backgroundPadding,backgroundPadding, -backgroundPadding, -backgroundPadding));
455 }
427 }
456
428
457 // recalculate legend position
429 // recalculate legend position
@@ -1,3 +1,23
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
1 #ifndef QCHART_H
21 #ifndef QCHART_H
2 #define QCHART_H
22 #define QCHART_H
3
23
@@ -8,7 +28,6 class QGraphicsSceneResizeEvent;
8
28
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
30
11 class Axis;
12 class QSeries;
31 class QSeries;
13 class QChartAxis;
32 class QChartAxis;
14 class QLegend;
33 class QLegend;
@@ -30,13 +49,14 public:
30 ChartThemeCount
49 ChartThemeCount
31 };
50 };
32
51
33 enum AnimationOption {
52 enum AnimationOption {
34 NoAnimation = 0x0,
53 NoAnimation = 0x0,
35 GridAxisAnimations = 0x1,
54 GridAxisAnimations = 0x1,
36 SeriesAnimations =0x2,
55 SeriesAnimations =0x2,
37 AllAnimations = 0x3
56 AllAnimations = 0x3
38 };
57 };
39 Q_DECLARE_FLAGS(AnimationOptions, AnimationOption)
58
59 Q_DECLARE_FLAGS(AnimationOptions, AnimationOption)
40
60
41 public:
61 public:
42 QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
62 QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
@@ -81,20 +101,12 public:
81 QLegend* takeLegend();
101 QLegend* takeLegend();
82 void giveLegend(QLegend* legend);
102 void giveLegend(QLegend* legend);
83
103
84 int padding() const;
85
86 protected:
104 protected:
87 void resizeEvent(QGraphicsSceneResizeEvent *event);
105 void resizeEvent(QGraphicsSceneResizeEvent *event);
88
106
89 protected:
107 protected:
90 QScopedPointer<QChartPrivate> d_ptr;
108 QScopedPointer<QChartPrivate> d_ptr;
91
109 friend class QChartView;
92 private:
93 void setPadding(int padding);
94 void setBackgroundPadding(int padding);
95 void setBackgroundDiameter(int diameter);
96
97 private:
98 Q_DISABLE_COPY(QChart);
110 Q_DISABLE_COPY(QChart);
99 };
111 };
100
112
@@ -30,8 +30,6 public:
30 QLegend* m_legend;
30 QLegend* m_legend;
31 ChartDataSet *m_dataset;
31 ChartDataSet *m_dataset;
32 ChartPresenter *m_presenter;
32 ChartPresenter *m_presenter;
33 int m_padding;
34 int m_backgroundPadding;
35
33
36 Q_DECLARE_PUBLIC(QChart);
34 Q_DECLARE_PUBLIC(QChart);
37 };
35 };
@@ -1,11 +1,11
1 #include "qchartview.h"
1 #include "qchartview.h"
2 #include "qchart.h"
2 #include "qchart.h"
3 #include "qchart_p.h"
3 #include "qchartaxis.h"
4 #include "qchartaxis.h"
4 #include <QGraphicsView>
5 #include <QGraphicsView>
5 #include <QGraphicsScene>
6 #include <QGraphicsScene>
6 #include <QRubberBand>
7 #include <QRubberBand>
7 #include <QResizeEvent>
8 #include <QResizeEvent>
8 #include <QDebug>
9
9
10 /*!
10 /*!
11 \enum QChartView::RubberBandPolicy
11 \enum QChartView::RubberBandPolicy
@@ -232,7 +232,7 void QChartView::mousePressEvent(QMouseEvent *event)
232 {
232 {
233 if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
233 if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
234
234
235 int padding = m_chart->padding();
235 int padding = m_chart->d_ptr->m_presenter->padding();
236 QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding);
236 QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding);
237
237
238 if (rect.contains(event->pos())) {
238 if (rect.contains(event->pos())) {
@@ -254,7 +254,7 void QChartView::mousePressEvent(QMouseEvent *event)
254 void QChartView::mouseMoveEvent(QMouseEvent *event)
254 void QChartView::mouseMoveEvent(QMouseEvent *event)
255 {
255 {
256 if(m_rubberBand && m_rubberBand->isVisible()) {
256 if(m_rubberBand && m_rubberBand->isVisible()) {
257 int padding = m_chart->padding();
257 int padding = m_chart->d_ptr->m_presenter->padding();
258 QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding);
258 QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding);
259 int width = event->pos().x() - m_rubberBandOrigin.x();
259 int width = event->pos().x() - m_rubberBandOrigin.x();
260 int height = event->pos().y() - m_rubberBandOrigin.y();
260 int height = event->pos().y() - m_rubberBandOrigin.y();
General Comments 0
You need to be logged in to leave comments. Login now