@@ -14,13 +14,15 Widget::Widget(QWidget *parent) | |||||
14 | : QWidget(parent), |
|
14 | : QWidget(parent), | |
15 | m_scene(0), |
|
15 | m_scene(0), | |
16 | m_chart(0), |
|
16 | m_chart(0), | |
17 | m_view(0) |
|
17 | m_view(0), | |
|
18 | m_tooltip(0) | |||
18 | { |
|
19 | { | |
19 | // chart |
|
20 | // chart | |
20 | m_chart = new QChart; |
|
21 | m_chart = new QChart; | |
21 | m_chart->setMinimumSize(640, 480); |
|
22 | 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->legend()->hide(); | |||
22 | QLineSeries *series = new QLineSeries; |
|
25 | QLineSeries *series = new QLineSeries; | |
23 | series->setName("Click the line to create a movable callout"); |
|
|||
24 | series->append(1, 3); |
|
26 | series->append(1, 3); | |
25 | series->append(4, 5); |
|
27 | series->append(4, 5); | |
26 | series->append(5, 4.5); |
|
28 | series->append(5, 4.5); | |
@@ -38,7 +40,8 Widget::Widget(QWidget *parent) | |||||
38 | mainLayout->addWidget(m_view); |
|
40 | mainLayout->addWidget(m_view); | |
39 | setLayout(mainLayout); |
|
41 | setLayout(mainLayout); | |
40 |
|
42 | |||
41 |
connect(series, SIGNAL(clicked(QPointF)), this, SLOT( |
|
43 | connect(series, SIGNAL(clicked(QPointF)), this, SLOT(keepCallout())); | |
|
44 | connect(series, SIGNAL(hovered(QPointF, bool)), this, SLOT(tooltip(QPointF,bool))); | |||
42 | } |
|
45 | } | |
43 |
|
46 | |||
44 | Widget::~Widget() |
|
47 | Widget::~Widget() | |
@@ -46,11 +49,23 Widget::~Widget() | |||||
46 |
|
49 | |||
47 | } |
|
50 | } | |
48 |
|
51 | |||
49 |
void Widget:: |
|
52 | void Widget::keepCallout() | |
50 | { |
|
53 | { | |
51 |
|
|
54 | m_tooltip = new Callout(m_chart); | |
52 | label->setText(QString("X: %1\nY: %2").arg(point.x()).arg(point.y())); |
|
55 | } | |
53 | label->setAnchor(m_chart->mapFromParent(m_view->mapToScene(m_view->mapFromGlobal(QCursor::pos())))); |
|
56 | ||
54 | label->setPos(m_chart->mapFromParent(m_view->mapToScene(m_view->mapFromGlobal(QCursor::pos() + QPoint(10, -50))))); |
|
57 | void Widget::tooltip(QPointF point, bool state) | |
55 | label->setZValue(11); |
|
58 | { | |
|
59 | if (m_tooltip == 0) | |||
|
60 | m_tooltip = new Callout(m_chart); | |||
|
61 | ||||
|
62 | if (state) { | |||
|
63 | 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())))); | |||
|
65 | m_tooltip->setPos(m_chart->mapFromParent(m_view->mapToScene(m_view->mapFromGlobal(QCursor::pos() + QPoint(10, -50))))); | |||
|
66 | m_tooltip->setZValue(11); | |||
|
67 | m_tooltip->show(); | |||
|
68 | } else { | |||
|
69 | m_tooltip->hide(); | |||
|
70 | } | |||
56 | } |
|
71 | } |
@@ -6,6 +6,7 | |||||
6 |
|
6 | |||
7 | class QGraphicsScene; |
|
7 | class QGraphicsScene; | |
8 | class QGraphicsView; |
|
8 | class QGraphicsView; | |
|
9 | class Callout; | |||
9 |
|
10 | |||
10 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
11 | QTCOMMERCIALCHART_USE_NAMESPACE | |
11 |
|
12 | |||
@@ -18,12 +19,14 public: | |||||
18 | ~Widget(); |
|
19 | ~Widget(); | |
19 |
|
20 | |||
20 | public slots: |
|
21 | public slots: | |
21 |
void |
|
22 | void keepCallout(); | |
|
23 | void tooltip(QPointF point, bool state); | |||
22 |
|
24 | |||
23 | private: |
|
25 | private: | |
24 | QGraphicsScene *m_scene; |
|
26 | QGraphicsScene *m_scene; | |
25 | QChart *m_chart; |
|
27 | QChart *m_chart; | |
26 | QGraphicsView *m_view; |
|
28 | QGraphicsView *m_view; | |
|
29 | Callout *m_tooltip; | |||
27 | }; |
|
30 | }; | |
28 |
|
31 | |||
29 | #endif // WIDGET_H |
|
32 | #endif // WIDGET_H |
@@ -35,6 +35,7 LineChartItem::LineChartItem(QLineSeries *series, ChartPresenter *presenter) | |||||
35 | m_series(series), |
|
35 | m_series(series), | |
36 | m_pointsVisible(false) |
|
36 | m_pointsVisible(false) | |
37 | { |
|
37 | { | |
|
38 | setAcceptHoverEvents(true); | |||
38 | setZValue(ChartPresenter::LineChartZValue); |
|
39 | setZValue(ChartPresenter::LineChartZValue); | |
39 | QObject::connect(series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated())); |
|
40 | QObject::connect(series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated())); | |
40 | QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); |
|
41 | QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); | |
@@ -132,6 +133,18 void LineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event) | |||||
132 | QGraphicsItem::mousePressEvent(event); |
|
133 | QGraphicsItem::mousePressEvent(event); | |
133 | } |
|
134 | } | |
134 |
|
135 | |||
|
136 | void LineChartItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) | |||
|
137 | { | |||
|
138 | emit XYChart::hovered(calculateDomainPoint(event->pos()), true); | |||
|
139 | QGraphicsItem::hoverEnterEvent(event); | |||
|
140 | } | |||
|
141 | ||||
|
142 | void LineChartItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) | |||
|
143 | { | |||
|
144 | emit XYChart::hovered(calculateDomainPoint(event->pos()), false); | |||
|
145 | QGraphicsItem::hoverEnterEvent(event); | |||
|
146 | } | |||
|
147 | ||||
135 | #include "moc_linechartitem_p.cpp" |
|
148 | #include "moc_linechartitem_p.cpp" | |
136 |
|
149 | |||
137 | QTCOMMERCIALCHART_END_NAMESPACE |
|
150 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -60,6 +60,8 public Q_SLOTS: | |||||
60 | protected: |
|
60 | protected: | |
61 | void updateGeometry(); |
|
61 | void updateGeometry(); | |
62 | void mousePressEvent(QGraphicsSceneMouseEvent *event); |
|
62 | void mousePressEvent(QGraphicsSceneMouseEvent *event); | |
|
63 | void hoverEnterEvent(QGraphicsSceneHoverEvent *event); | |||
|
64 | void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); | |||
63 |
|
65 | |||
64 | private: |
|
66 | private: | |
65 | QLineSeries *m_series; |
|
67 | QLineSeries *m_series; |
@@ -76,6 +76,7 public: | |||||
76 |
|
76 | |||
77 | Q_SIGNALS: |
|
77 | Q_SIGNALS: | |
78 | void clicked(const QPointF &point); |
|
78 | void clicked(const QPointF &point); | |
|
79 | void hovered(const QPointF &point, bool state); | |||
79 | void pointReplaced(int index); |
|
80 | void pointReplaced(int index); | |
80 | void pointRemoved(int index); |
|
81 | void pointRemoved(int index); | |
81 | void pointAdded(int index); |
|
82 | void pointAdded(int index); |
@@ -47,6 +47,7 XYChart::XYChart(QXYSeries *series, ChartPresenter *presenter) | |||||
47 | QObject::connect(series, SIGNAL(pointAdded(int)), this, SLOT(handlePointAdded(int))); |
|
47 | QObject::connect(series, SIGNAL(pointAdded(int)), this, SLOT(handlePointAdded(int))); | |
48 | QObject::connect(series, SIGNAL(pointRemoved(int)), this, SLOT(handlePointRemoved(int))); |
|
48 | QObject::connect(series, SIGNAL(pointRemoved(int)), this, SLOT(handlePointRemoved(int))); | |
49 | QObject::connect(this, SIGNAL(clicked(QPointF)), series, SIGNAL(clicked(QPointF))); |
|
49 | QObject::connect(this, SIGNAL(clicked(QPointF)), series, SIGNAL(clicked(QPointF))); | |
|
50 | QObject::connect(this, SIGNAL(hovered(QPointF,bool)), series, SIGNAL(hovered(QPointF,bool))); | |||
50 | } |
|
51 | } | |
51 |
|
52 | |||
52 | void XYChart::setGeometryPoints(const QVector<QPointF>& points) |
|
53 | void XYChart::setGeometryPoints(const QVector<QPointF>& points) |
@@ -74,6 +74,7 public Q_SLOTS: | |||||
74 |
|
74 | |||
75 | Q_SIGNALS: |
|
75 | Q_SIGNALS: | |
76 | void clicked(const QPointF &point); |
|
76 | void clicked(const QPointF &point); | |
|
77 | void hovered(const QPointF &point, bool state); | |||
77 |
|
78 | |||
78 | protected: |
|
79 | protected: | |
79 | virtual void updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, int index = -1); |
|
80 | virtual void updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, int index = -1); |
General Comments 0
You need to be logged in to leave comments.
Login now