diff --git a/examples/callout/widget.cpp b/examples/callout/widget.cpp index 5174834..6640db8 100644 --- a/examples/callout/widget.cpp +++ b/examples/callout/widget.cpp @@ -14,13 +14,15 @@ Widget::Widget(QWidget *parent) : QWidget(parent), m_scene(0), m_chart(0), - m_view(0) + m_view(0), + m_tooltip(0) { // chart m_chart = new QChart; m_chart->setMinimumSize(640, 480); + m_chart->setTitle("Hover the line to show callout. Click the line to make it stay"); + m_chart->legend()->hide(); QLineSeries *series = new QLineSeries; - series->setName("Click the line to create a movable callout"); series->append(1, 3); series->append(4, 5); series->append(5, 4.5); @@ -38,7 +40,8 @@ Widget::Widget(QWidget *parent) mainLayout->addWidget(m_view); setLayout(mainLayout); - connect(series, SIGNAL(clicked(QPointF)), this, SLOT(addCallout(QPointF))); + connect(series, SIGNAL(clicked(QPointF)), this, SLOT(keepCallout())); + connect(series, SIGNAL(hovered(QPointF, bool)), this, SLOT(tooltip(QPointF,bool))); } Widget::~Widget() @@ -46,11 +49,23 @@ Widget::~Widget() } -void Widget::addCallout(QPointF point) +void Widget::keepCallout() { - Callout *label = new Callout(m_chart); - label->setText(QString("X: %1\nY: %2").arg(point.x()).arg(point.y())); - label->setAnchor(m_chart->mapFromParent(m_view->mapToScene(m_view->mapFromGlobal(QCursor::pos())))); - label->setPos(m_chart->mapFromParent(m_view->mapToScene(m_view->mapFromGlobal(QCursor::pos() + QPoint(10, -50))))); - label->setZValue(11); + m_tooltip = new Callout(m_chart); +} + +void Widget::tooltip(QPointF point, bool state) +{ + if (m_tooltip == 0) + m_tooltip = new Callout(m_chart); + + if (state) { + m_tooltip->setText(QString("X: %1\nY: %2").arg(point.x()).arg(point.y())); + m_tooltip->setAnchor(m_chart->mapFromParent(m_view->mapToScene(m_view->mapFromGlobal(QCursor::pos())))); + m_tooltip->setPos(m_chart->mapFromParent(m_view->mapToScene(m_view->mapFromGlobal(QCursor::pos() + QPoint(10, -50))))); + m_tooltip->setZValue(11); + m_tooltip->show(); + } else { + m_tooltip->hide(); + } } diff --git a/examples/callout/widget.h b/examples/callout/widget.h index 5cf0733..7c5041a 100644 --- a/examples/callout/widget.h +++ b/examples/callout/widget.h @@ -6,6 +6,7 @@ class QGraphicsScene; class QGraphicsView; +class Callout; QTCOMMERCIALCHART_USE_NAMESPACE @@ -18,12 +19,14 @@ public: ~Widget(); public slots: - void addCallout(QPointF point); + void keepCallout(); + void tooltip(QPointF point, bool state); private: QGraphicsScene *m_scene; QChart *m_chart; QGraphicsView *m_view; + Callout *m_tooltip; }; #endif // WIDGET_H diff --git a/src/linechart/linechartitem.cpp b/src/linechart/linechartitem.cpp index 0a6db1a..4a05dfe 100644 --- a/src/linechart/linechartitem.cpp +++ b/src/linechart/linechartitem.cpp @@ -35,6 +35,7 @@ LineChartItem::LineChartItem(QLineSeries *series, ChartPresenter *presenter) m_series(series), m_pointsVisible(false) { + setAcceptHoverEvents(true); setZValue(ChartPresenter::LineChartZValue); QObject::connect(series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated())); QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); @@ -132,6 +133,18 @@ void LineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event) QGraphicsItem::mousePressEvent(event); } +void LineChartItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) +{ + emit XYChart::hovered(calculateDomainPoint(event->pos()), true); + QGraphicsItem::hoverEnterEvent(event); +} + +void LineChartItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) +{ + emit XYChart::hovered(calculateDomainPoint(event->pos()), false); + QGraphicsItem::hoverEnterEvent(event); +} + #include "moc_linechartitem_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/linechart/linechartitem_p.h b/src/linechart/linechartitem_p.h index 7727b31..383ed41 100644 --- a/src/linechart/linechartitem_p.h +++ b/src/linechart/linechartitem_p.h @@ -60,6 +60,8 @@ public Q_SLOTS: protected: void updateGeometry(); void mousePressEvent(QGraphicsSceneMouseEvent *event); + void hoverEnterEvent(QGraphicsSceneHoverEvent *event); + void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); private: QLineSeries *m_series; diff --git a/src/xychart/qxyseries.h b/src/xychart/qxyseries.h index 6146772..71d95f4 100644 --- a/src/xychart/qxyseries.h +++ b/src/xychart/qxyseries.h @@ -76,6 +76,7 @@ public: Q_SIGNALS: void clicked(const QPointF &point); + void hovered(const QPointF &point, bool state); void pointReplaced(int index); void pointRemoved(int index); void pointAdded(int index); diff --git a/src/xychart/xychart.cpp b/src/xychart/xychart.cpp index d7d65f8..f7356b1 100644 --- a/src/xychart/xychart.cpp +++ b/src/xychart/xychart.cpp @@ -47,6 +47,7 @@ XYChart::XYChart(QXYSeries *series, ChartPresenter *presenter) QObject::connect(series, SIGNAL(pointAdded(int)), this, SLOT(handlePointAdded(int))); QObject::connect(series, SIGNAL(pointRemoved(int)), this, SLOT(handlePointRemoved(int))); QObject::connect(this, SIGNAL(clicked(QPointF)), series, SIGNAL(clicked(QPointF))); + QObject::connect(this, SIGNAL(hovered(QPointF,bool)), series, SIGNAL(hovered(QPointF,bool))); } void XYChart::setGeometryPoints(const QVector& points) diff --git a/src/xychart/xychart_p.h b/src/xychart/xychart_p.h index 7d4b155..2cb780d 100644 --- a/src/xychart/xychart_p.h +++ b/src/xychart/xychart_p.h @@ -74,6 +74,7 @@ public Q_SLOTS: Q_SIGNALS: void clicked(const QPointF &point); + void hovered(const QPointF &point, bool state); protected: virtual void updateChart(QVector &oldPoints, QVector &newPoints, int index = -1);