##// END OF EJS Templates
Added hover support to splinechart and updated callout example
Marek Rosa -
r2256:fb69d3a3a2fe
parent child
Show More
@@ -1,71 +1,84
1 #include "widget.h"
1 #include "widget.h"
2 #include <QGraphicsScene>
2 #include <QGraphicsScene>
3 #include <QGraphicsView>
3 #include <QGraphicsView>
4 #include <QVBoxLayout>
4 #include <QVBoxLayout>
5 #include <QChart>
5 #include <QChart>
6 #include <QLineSeries>
6 #include <QLineSeries>
7 #include <QSplineSeries>
7 #include <QGraphicsTextItem>
8 #include <QGraphicsTextItem>
8 #include <QGraphicsLineItem>
9 #include <QGraphicsLineItem>
9 #include "callout.h"
10 #include "callout.h"
10
11
11 QTCOMMERCIALCHART_USE_NAMESPACE
12 QTCOMMERCIALCHART_USE_NAMESPACE
12
13
13 Widget::Widget(QWidget *parent)
14 Widget::Widget(QWidget *parent)
14 : QWidget(parent),
15 : QWidget(parent),
15 m_scene(0),
16 m_scene(0),
16 m_chart(0),
17 m_chart(0),
17 m_view(0),
18 m_view(0),
18 m_tooltip(0)
19 m_tooltip(0)
19 {
20 {
20 // chart
21 // chart
21 m_chart = new QChart;
22 m_chart = new QChart;
22 m_chart->setMinimumSize(640, 480);
23 m_chart->setMinimumSize(640, 480);
23 m_chart->setTitle("Hover the line to show callout. Click the line to make it stay");
24 m_chart->setTitle("Hover the line to show callout. Click the line to make it stay");
24 m_chart->legend()->hide();
25 m_chart->legend()->hide();
25 QLineSeries *series = new QLineSeries;
26 QLineSeries *series = new QLineSeries;
26 series->append(1, 3);
27 series->append(1, 3);
27 series->append(4, 5);
28 series->append(4, 5);
28 series->append(5, 4.5);
29 series->append(5, 4.5);
29 series->append(7, 1);
30 series->append(7, 1);
30 series->append(11, 2);
31 series->append(11, 2);
31 m_chart->addSeries(series);
32 m_chart->addSeries(series);
33
34 QSplineSeries *series2 = new QSplineSeries;
35 series2->append(1.6, 1.4);
36 series2->append(2.4, 3.5);
37 series2->append(3.7, 2.5);
38 series2->append(7, 4);
39 series2->append(10, 2);
40 m_chart->addSeries(series2);
41
32 m_chart->createDefaultAxes();
42 m_chart->createDefaultAxes();
33
43
34 m_scene = new QGraphicsScene;
44 m_scene = new QGraphicsScene;
35 m_view = new QGraphicsView(m_scene);
45 m_view = new QGraphicsView(m_scene);
36 m_view->setRenderHint(QPainter::Antialiasing);
46 m_view->setRenderHint(QPainter::Antialiasing);
37 m_scene->addItem(m_chart);
47 m_scene->addItem(m_chart);
38
48
39 QVBoxLayout *mainLayout = new QVBoxLayout;
49 QVBoxLayout *mainLayout = new QVBoxLayout;
40 mainLayout->addWidget(m_view);
50 mainLayout->addWidget(m_view);
41 setLayout(mainLayout);
51 setLayout(mainLayout);
42
52
43 connect(series, SIGNAL(clicked(QPointF)), this, SLOT(keepCallout()));
53 connect(series, SIGNAL(clicked(QPointF)), this, SLOT(keepCallout()));
44 connect(series, SIGNAL(hovered(QPointF, bool)), this, SLOT(tooltip(QPointF,bool)));
54 connect(series, SIGNAL(hovered(QPointF, bool)), this, SLOT(tooltip(QPointF,bool)));
55
56 connect(series2, SIGNAL(clicked(QPointF)), this, SLOT(keepCallout()));
57 connect(series2, SIGNAL(hovered(QPointF, bool)), this, SLOT(tooltip(QPointF,bool)));
45 }
58 }
46
59
47 Widget::~Widget()
60 Widget::~Widget()
48 {
61 {
49
62
50 }
63 }
51
64
52 void Widget::keepCallout()
65 void Widget::keepCallout()
53 {
66 {
54 m_tooltip = new Callout(m_chart);
67 m_tooltip = new Callout(m_chart);
55 }
68 }
56
69
57 void Widget::tooltip(QPointF point, bool state)
70 void Widget::tooltip(QPointF point, bool state)
58 {
71 {
59 if (m_tooltip == 0)
72 if (m_tooltip == 0)
60 m_tooltip = new Callout(m_chart);
73 m_tooltip = new Callout(m_chart);
61
74
62 if (state) {
75 if (state) {
63 m_tooltip->setText(QString("X: %1\nY: %2").arg(point.x()).arg(point.y()));
76 m_tooltip->setText(QString("X: %1\nY: %2").arg(point.x()).arg(point.y()));
64 m_tooltip->setAnchor(m_chart->mapFromParent(m_view->mapToScene(m_view->mapFromGlobal(QCursor::pos()))));
77 m_tooltip->setAnchor(m_chart->mapFromParent(m_view->mapToScene(m_view->mapFromGlobal(QCursor::pos()))));
65 m_tooltip->setPos(m_chart->mapFromParent(m_view->mapToScene(m_view->mapFromGlobal(QCursor::pos() + QPoint(10, -50)))));
78 m_tooltip->setPos(m_chart->mapFromParent(m_view->mapToScene(m_view->mapFromGlobal(QCursor::pos() + QPoint(10, -50)))));
66 m_tooltip->setZValue(11);
79 m_tooltip->setZValue(11);
67 m_tooltip->show();
80 m_tooltip->show();
68 } else {
81 } else {
69 m_tooltip->hide();
82 m_tooltip->hide();
70 }
83 }
71 }
84 }
@@ -1,150 +1,152
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "linechartitem_p.h"
21 #include "linechartitem_p.h"
22 #include "qlineseries.h"
22 #include "qlineseries.h"
23 #include "qlineseries_p.h"
23 #include "qlineseries_p.h"
24 #include "chartpresenter_p.h"
24 #include "chartpresenter_p.h"
25 #include <QPainter>
25 #include <QPainter>
26 #include <QGraphicsSceneMouseEvent>
26 #include <QGraphicsSceneMouseEvent>
27
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
29
30 const qreal mouseEventMinWidth(12);
30 const qreal mouseEventMinWidth(12);
31
31
32 LineChartItem::LineChartItem(QLineSeries *series, ChartPresenter *presenter)
32 LineChartItem::LineChartItem(QLineSeries *series, ChartPresenter *presenter)
33 : XYChart(series, presenter),
33 : XYChart(series, presenter),
34 QGraphicsItem(presenter ? presenter->rootItem() : 0),
34 QGraphicsItem(presenter ? presenter->rootItem() : 0),
35 m_series(series),
35 m_series(series),
36 m_pointsVisible(false)
36 m_pointsVisible(false)
37 {
37 {
38 setAcceptHoverEvents(true);
38 setAcceptHoverEvents(true);
39 setZValue(ChartPresenter::LineChartZValue);
39 setZValue(ChartPresenter::LineChartZValue);
40 QObject::connect(series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated()));
40 QObject::connect(series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated()));
41 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated()));
41 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated()));
42 QObject::connect(series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated()));
42 QObject::connect(series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated()));
43 handleUpdated();
43 handleUpdated();
44 }
44 }
45
45
46 QRectF LineChartItem::boundingRect() const
46 QRectF LineChartItem::boundingRect() const
47 {
47 {
48 return m_rect;
48 return m_rect;
49 }
49 }
50
50
51 QPainterPath LineChartItem::shape() const
51 QPainterPath LineChartItem::shape() const
52 {
52 {
53 return m_path;
53 return m_path;
54 }
54 }
55
55
56 void LineChartItem::updateGeometry()
56 void LineChartItem::updateGeometry()
57 {
57 {
58 m_points = geometryPoints();
58 m_points = geometryPoints();
59
59
60 if (m_points.size() == 0) {
60 if (m_points.size() == 0) {
61 prepareGeometryChange();
61 prepareGeometryChange();
62 m_path = QPainterPath();
62 m_path = QPainterPath();
63 m_rect = QRect();
63 m_rect = QRect();
64 return;
64 return;
65 }
65 }
66
66
67 QPainterPath linePath(m_points.at(0));
67 QPainterPath linePath(m_points.at(0));
68
68
69 if (m_pointsVisible) {
69 if (m_pointsVisible) {
70
70
71 int size = m_linePen.width();
71 int size = m_linePen.width();
72 linePath.addEllipse(m_points.at(0), size, size);
72 linePath.addEllipse(m_points.at(0), size, size);
73 for (int i = 1; i < m_points.size(); i++) {
73 for (int i = 1; i < m_points.size(); i++) {
74 linePath.lineTo(m_points.at(i));
74 linePath.lineTo(m_points.at(i));
75 linePath.addEllipse(m_points.at(i), size, size);
75 linePath.addEllipse(m_points.at(i), size, size);
76 }
76 }
77
77
78 } else {
78 } else {
79 for (int i = 1; i < m_points.size(); i++)
79 for (int i = 1; i < m_points.size(); i++)
80 linePath.lineTo(m_points.at(i));
80 linePath.lineTo(m_points.at(i));
81 }
81 }
82
82
83 m_linePath = linePath;
83 m_linePath = linePath;
84
84
85 QPainterPathStroker stroker;
85 QPainterPathStroker stroker;
86 // QPainter::drawLine does not respect join styles, for example BevelJoin becomes MiterJoin.
86 // QPainter::drawLine does not respect join styles, for example BevelJoin becomes MiterJoin.
87 // This is why we are prepared for the "worst case" scenario, i.e. use always MiterJoin and
87 // This is why we are prepared for the "worst case" scenario, i.e. use always MiterJoin and
88 // multiply line width with square root of two when defining shape and bounding rectangle.
88 // multiply line width with square root of two when defining shape and bounding rectangle.
89 stroker.setWidth(m_linePen.width() * 1.42);
89 stroker.setWidth(m_linePen.width() * 1.42);
90 stroker.setJoinStyle(Qt::MiterJoin);
90 stroker.setJoinStyle(Qt::MiterJoin);
91 stroker.setCapStyle(Qt::SquareCap);
91 stroker.setCapStyle(Qt::SquareCap);
92 stroker.setMiterLimit(m_linePen.miterLimit());
92 stroker.setMiterLimit(m_linePen.miterLimit());
93
93
94 prepareGeometryChange();
94 prepareGeometryChange();
95
95
96 m_path = stroker.createStroke(linePath);
96 m_path = stroker.createStroke(linePath);
97 m_rect = m_path.boundingRect();
97 m_rect = m_path.boundingRect();
98
98
99 setPos(origin());
99 setPos(origin());
100 }
100 }
101
101
102 void LineChartItem::handleUpdated()
102 void LineChartItem::handleUpdated()
103 {
103 {
104 setVisible(m_series->isVisible());
104 setVisible(m_series->isVisible());
105 setOpacity(m_series->opacity());
105 setOpacity(m_series->opacity());
106 m_pointsVisible = m_series->pointsVisible();
106 m_pointsVisible = m_series->pointsVisible();
107 m_linePen = m_series->pen();
107 m_linePen = m_series->pen();
108 update();
108 update();
109 }
109 }
110
110
111 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
111 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
112 {
112 {
113 Q_UNUSED(widget)
113 Q_UNUSED(widget)
114 Q_UNUSED(option)
114 Q_UNUSED(option)
115
115
116 painter->save();
116 painter->save();
117 painter->setPen(m_linePen);
117 painter->setPen(m_linePen);
118 painter->setBrush(m_linePen.color());
118 painter->setBrush(m_linePen.color());
119 painter->setClipRect(clipRect());
119 painter->setClipRect(clipRect());
120
120
121 if (m_pointsVisible) {
121 if (m_pointsVisible) {
122 painter->drawPath(m_linePath);
122 painter->drawPath(m_linePath);
123 } else {
123 } else {
124 for (int i(1); i < m_points.size(); i++)
124 for (int i(1); i < m_points.size(); i++)
125 painter->drawLine(m_points.at(i - 1), m_points.at(i));
125 painter->drawLine(m_points.at(i - 1), m_points.at(i));
126 }
126 }
127 painter->restore();
127 painter->restore();
128 }
128 }
129
129
130 void LineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
130 void LineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
131 {
131 {
132 emit XYChart::clicked(calculateDomainPoint(event->pos()));
132 emit XYChart::clicked(calculateDomainPoint(event->pos()));
133 QGraphicsItem::mousePressEvent(event);
133 QGraphicsItem::mousePressEvent(event);
134 }
134 }
135
135
136 void LineChartItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
136 void LineChartItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
137 {
137 {
138 emit XYChart::hovered(calculateDomainPoint(event->pos()), true);
138 emit XYChart::hovered(calculateDomainPoint(event->pos()), true);
139 QGraphicsItem::hoverEnterEvent(event);
139 event->accept();
140 // QGraphicsItem::hoverEnterEvent(event);
140 }
141 }
141
142
142 void LineChartItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
143 void LineChartItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
143 {
144 {
144 emit XYChart::hovered(calculateDomainPoint(event->pos()), false);
145 emit XYChart::hovered(calculateDomainPoint(event->pos()), false);
145 QGraphicsItem::hoverEnterEvent(event);
146 event->accept();
147 // QGraphicsItem::hoverEnterEvent(event);
146 }
148 }
147
149
148 #include "moc_linechartitem_p.cpp"
150 #include "moc_linechartitem_p.cpp"
149
151
150 QTCOMMERCIALCHART_END_NAMESPACE
152 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,171 +1,190
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "splinechartitem_p.h"
21 #include "splinechartitem_p.h"
22 #include "qsplineseries_p.h"
22 #include "qsplineseries_p.h"
23 #include "chartpresenter_p.h"
23 #include "chartpresenter_p.h"
24 #include "splineanimation_p.h"
24 #include "splineanimation_p.h"
25 #include <QPainter>
25 #include <QPainter>
26 #include <QGraphicsSceneMouseEvent>
26 #include <QGraphicsSceneMouseEvent>
27
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
29
30 SplineChartItem::SplineChartItem(QSplineSeries *series, ChartPresenter *presenter)
30 SplineChartItem::SplineChartItem(QSplineSeries *series, ChartPresenter *presenter)
31 : XYChart(series, presenter),
31 : XYChart(series, presenter),
32 QGraphicsItem(presenter ? presenter->rootItem() : 0),
32 QGraphicsItem(presenter ? presenter->rootItem() : 0),
33 m_series(series),
33 m_series(series),
34 m_pointsVisible(false),
34 m_pointsVisible(false),
35 m_animation(0)
35 m_animation(0)
36 {
36 {
37 setAcceptHoverEvents(true);
37 setZValue(ChartPresenter::SplineChartZValue);
38 setZValue(ChartPresenter::SplineChartZValue);
38 QObject::connect(m_series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated()));
39 QObject::connect(m_series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated()));
39 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated()));
40 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated()));
40 QObject::connect(series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated()));
41 QObject::connect(series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated()));
41 handleUpdated();
42 handleUpdated();
42 }
43 }
43
44
44 QRectF SplineChartItem::boundingRect() const
45 QRectF SplineChartItem::boundingRect() const
45 {
46 {
46 return m_rect;
47 return m_rect;
47 }
48 }
48
49
49 QPainterPath SplineChartItem::shape() const
50 QPainterPath SplineChartItem::shape() const
50 {
51 {
51 return m_path;
52 QPainterPathStroker stroker;
53 return stroker.createStroke(m_path);
52 }
54 }
53
55
54 void SplineChartItem::setAnimation(SplineAnimation *animation)
56 void SplineChartItem::setAnimation(SplineAnimation *animation)
55 {
57 {
56 m_animation = animation;
58 m_animation = animation;
57 XYChart::setAnimation(animation);
59 XYChart::setAnimation(animation);
58 }
60 }
59
61
60 ChartAnimation *SplineChartItem::animation() const
62 ChartAnimation *SplineChartItem::animation() const
61 {
63 {
62 return m_animation;
64 return m_animation;
63 }
65 }
64
66
65 void SplineChartItem::setControlGeometryPoints(QVector<QPointF>& points)
67 void SplineChartItem::setControlGeometryPoints(QVector<QPointF>& points)
66 {
68 {
67 m_controlPoints = points;
69 m_controlPoints = points;
68 }
70 }
69
71
70 QVector<QPointF> SplineChartItem::controlGeometryPoints() const
72 QVector<QPointF> SplineChartItem::controlGeometryPoints() const
71 {
73 {
72 return m_controlPoints;
74 return m_controlPoints;
73 }
75 }
74
76
75 void SplineChartItem::updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, int index)
77 void SplineChartItem::updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, int index)
76 {
78 {
77 QVector<QPointF> controlPoints;
79 QVector<QPointF> controlPoints;
78
80
79 if (newPoints.count() >= 2)
81 if (newPoints.count() >= 2)
80 controlPoints.resize(newPoints.count() * 2 - 2);
82 controlPoints.resize(newPoints.count() * 2 - 2);
81
83
82 for (int i = 0; i < newPoints.size() - 1; i++) {
84 for (int i = 0; i < newPoints.size() - 1; i++) {
83 controlPoints[2 * i] = calculateGeometryControlPoint(2 * i);
85 controlPoints[2 * i] = calculateGeometryControlPoint(2 * i);
84 controlPoints[2 * i + 1] = calculateGeometryControlPoint(2 * i + 1);
86 controlPoints[2 * i + 1] = calculateGeometryControlPoint(2 * i + 1);
85 }
87 }
86
88
87 if (m_animation)
89 if (m_animation)
88 m_animation->setup(oldPoints, newPoints, m_controlPoints, controlPoints, index);
90 m_animation->setup(oldPoints, newPoints, m_controlPoints, controlPoints, index);
89
91
90 m_points = newPoints;
92 m_points = newPoints;
91 m_controlPoints = controlPoints;
93 m_controlPoints = controlPoints;
92 setDirty(false);
94 setDirty(false);
93
95
94 if (m_animation)
96 if (m_animation)
95 presenter()->startAnimation(m_animation);
97 presenter()->startAnimation(m_animation);
96 else
98 else
97 updateGeometry();
99 updateGeometry();
98 }
100 }
99
101
100 QPointF SplineChartItem::calculateGeometryControlPoint(int index) const
102 QPointF SplineChartItem::calculateGeometryControlPoint(int index) const
101 {
103 {
102 return XYChart::calculateGeometryPoint(m_series->d_func()->controlPoint(index));
104 return XYChart::calculateGeometryPoint(m_series->d_func()->controlPoint(index));
103 }
105 }
104
106
105 void SplineChartItem::updateGeometry()
107 void SplineChartItem::updateGeometry()
106 {
108 {
107 const QVector<QPointF> &points = m_points;
109 const QVector<QPointF> &points = m_points;
108 const QVector<QPointF> &controlPoints = m_controlPoints;
110 const QVector<QPointF> &controlPoints = m_controlPoints;
109
111
110 if ((points.size() < 2) || (controlPoints.size() < 2)) {
112 if ((points.size() < 2) || (controlPoints.size() < 2)) {
111 prepareGeometryChange();
113 prepareGeometryChange();
112 m_path = QPainterPath();
114 m_path = QPainterPath();
113 m_rect = QRect();
115 m_rect = QRect();
114 return;
116 return;
115 }
117 }
116
118
117 Q_ASSERT(points.count() * 2 - 2 == controlPoints.count());
119 Q_ASSERT(points.count() * 2 - 2 == controlPoints.count());
118
120
119 QPainterPath splinePath(points.at(0));
121 QPainterPath splinePath(points.at(0));
120
122
121 for (int i = 0; i < points.size() - 1; i++) {
123 for (int i = 0; i < points.size() - 1; i++) {
122 const QPointF &point = points.at(i + 1);
124 const QPointF &point = points.at(i + 1);
123 splinePath.cubicTo(controlPoints[2 * i], controlPoints[2 * i + 1], point);
125 splinePath.cubicTo(controlPoints[2 * i], controlPoints[2 * i + 1], point);
124 }
126 }
125
127
126 prepareGeometryChange();
128 prepareGeometryChange();
129 // QPainterPathStroker stroker;
130 // stroker.setWidth(m_linePen.width() / 2.0);
131 // m_path = stroker.createStroke(splinePath);
127 m_path = splinePath;
132 m_path = splinePath;
128 m_rect = splinePath.boundingRect();
133 m_rect = splinePath.boundingRect();
129 setPos(origin());
134 setPos(origin());
130 }
135 }
131
136
132 //handlers
137 //handlers
133
138
134 void SplineChartItem::handleUpdated()
139 void SplineChartItem::handleUpdated()
135 {
140 {
136 setVisible(m_series->isVisible());
141 setVisible(m_series->isVisible());
137 setOpacity(m_series->opacity());
142 setOpacity(m_series->opacity());
138 m_pointsVisible = m_series->pointsVisible();
143 m_pointsVisible = m_series->pointsVisible();
139 m_linePen = m_series->pen();
144 m_linePen = m_series->pen();
140 m_pointPen = m_series->pen();
145 m_pointPen = m_series->pen();
141 m_pointPen.setWidthF(2 * m_pointPen.width());
146 m_pointPen.setWidthF(2 * m_pointPen.width());
142 update();
147 update();
143 }
148 }
144
149
145 //painter
150 //painter
146
151
147 void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
152 void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
148 {
153 {
149 Q_UNUSED(widget)
154 Q_UNUSED(widget)
150 Q_UNUSED(option)
155 Q_UNUSED(option)
151
156
152 painter->save();
157 painter->save();
153 painter->setClipRect(clipRect());
154 painter->setPen(m_linePen);
158 painter->setPen(m_linePen);
159 // painter->setBrush(m_linePen.color());
160 painter->setClipRect(clipRect());
161
155 painter->drawPath(m_path);
162 painter->drawPath(m_path);
156 if (m_pointsVisible) {
163 if (m_pointsVisible) {
157 painter->setPen(m_pointPen);
164 painter->setPen(m_pointPen);
158 painter->drawPoints(geometryPoints());
165 painter->drawPoints(geometryPoints());
159 }
166 }
160 painter->restore();
167 painter->restore();
161 }
168 }
162
169
163 void SplineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
170 void SplineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
164 {
171 {
165 emit XYChart::clicked(calculateDomainPoint(event->pos()));
172 emit XYChart::clicked(calculateDomainPoint(event->pos()));
166 QGraphicsItem::mousePressEvent(event);
173 QGraphicsItem::mousePressEvent(event);
167 }
174 }
168
175
176 void SplineChartItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
177 {
178 emit XYChart::hovered(calculateDomainPoint(event->pos()), true);
179 event->accept();
180 }
181
182 void SplineChartItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
183 {
184 emit XYChart::hovered(calculateDomainPoint(event->pos()), false);
185 event->accept();
186 }
187
169 #include "moc_splinechartitem_p.cpp"
188 #include "moc_splinechartitem_p.cpp"
170
189
171 QTCOMMERCIALCHART_END_NAMESPACE
190 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,84 +1,86
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef SPLINECHARTITEM_P_H
30 #ifndef SPLINECHARTITEM_P_H
31 #define SPLINECHARTITEM_P_H
31 #define SPLINECHARTITEM_P_H
32
32
33 #include "qsplineseries.h"
33 #include "qsplineseries.h"
34 #include "xychart_p.h"
34 #include "xychart_p.h"
35
35
36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37
37
38 class SplineAnimation;
38 class SplineAnimation;
39
39
40 class SplineChartItem : public XYChart, public QGraphicsItem
40 class SplineChartItem : public XYChart, public QGraphicsItem
41 {
41 {
42 Q_OBJECT
42 Q_OBJECT
43 Q_INTERFACES(QGraphicsItem)
43 Q_INTERFACES(QGraphicsItem)
44 public:
44 public:
45 SplineChartItem(QSplineSeries *series, ChartPresenter *presenter);
45 SplineChartItem(QSplineSeries *series, ChartPresenter *presenter);
46
46
47 //from QGraphicsItem
47 //from QGraphicsItem
48 QRectF boundingRect() const;
48 QRectF boundingRect() const;
49 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
49 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
50 QPainterPath shape() const;
50 QPainterPath shape() const;
51
51
52 void setControlGeometryPoints(QVector<QPointF>& points);
52 void setControlGeometryPoints(QVector<QPointF>& points);
53 QVector<QPointF> controlGeometryPoints() const;
53 QVector<QPointF> controlGeometryPoints() const;
54
54
55 void setAnimation(SplineAnimation *animation);
55 void setAnimation(SplineAnimation *animation);
56 ChartAnimation *animation() const;
56 ChartAnimation *animation() const;
57
57
58 public Q_SLOTS:
58 public Q_SLOTS:
59 void handleUpdated();
59 void handleUpdated();
60
60
61 protected:
61 protected:
62 void updateGeometry();
62 void updateGeometry();
63 void updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, int index);
63 void updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, int index);
64 void mousePressEvent(QGraphicsSceneMouseEvent *event);
64 void mousePressEvent(QGraphicsSceneMouseEvent *event);
65 void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
66 void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
65
67
66 private:
68 private:
67 QPointF calculateGeometryControlPoint(int index) const;
69 QPointF calculateGeometryControlPoint(int index) const;
68
70
69 private:
71 private:
70 QSplineSeries *m_series;
72 QSplineSeries *m_series;
71 QPainterPath m_path;
73 QPainterPath m_path;
72 QRectF m_rect;
74 QRectF m_rect;
73 QPen m_linePen;
75 QPen m_linePen;
74 QPen m_pointPen;
76 QPen m_pointPen;
75 bool m_pointsVisible;
77 bool m_pointsVisible;
76 QVector<QPointF> m_controlPoints;
78 QVector<QPointF> m_controlPoints;
77 SplineAnimation *m_animation;
79 SplineAnimation *m_animation;
78
80
79 friend class SplineAnimation;
81 friend class SplineAnimation;
80 };
82 };
81
83
82 QTCOMMERCIALCHART_END_NAMESPACE
84 QTCOMMERCIALCHART_END_NAMESPACE
83
85
84 #endif // SPLINECHARTITEM_P_H
86 #endif // SPLINECHARTITEM_P_H
General Comments 0
You need to be logged in to leave comments. Login now