##// END OF EJS Templates
Added hover support to splinechart and updated callout example
Marek Rosa -
r2256:fb69d3a3a2fe
parent child
Show More
@@ -4,6 +4,7
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"
@@ -29,6 +30,15 Widget::Widget(QWidget *parent)
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;
@@ -42,6 +52,9 Widget::Widget(QWidget *parent)
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()
@@ -136,13 +136,15 void LineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
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"
@@ -34,6 +34,7 SplineChartItem::SplineChartItem(QSplineSeries *series, ChartPresenter *presente
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()));
@@ -48,7 +49,8 QRectF SplineChartItem::boundingRect() const
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)
@@ -124,6 +126,9 void SplineChartItem::updateGeometry()
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());
@@ -150,8 +155,10 void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *o
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);
@@ -166,6 +173,18 void SplineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
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
@@ -62,6 +62,8 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;
General Comments 0
You need to be logged in to leave comments. Login now