diff --git a/doc/src/qml.qdoc b/doc/src/qml.qdoc index 355a321..85ce2f1 100644 --- a/doc/src/qml.qdoc +++ b/doc/src/qml.qdoc @@ -21,8 +21,7 @@ \note Since QtCommercial Charts utilizes Qt Graphics View Framework for drawing, it works best with QtQuick 1, which is based on the same framework. As of release 1.3.0, QtCommercial Charts is also usable with QtQuick 2, though the performance - is slightly worse than with QtQuick 1. Also, hovered and clicked signals of series do not work - with QtQuick 2. + is slightly worse than with QtQuick 1 due to additional rendering step that is required. \raw HTML diff --git a/plugins/declarative/declarativechart.cpp b/plugins/declarative/declarativechart.cpp index 5910979..9d3fcd0 100644 --- a/plugins/declarative/declarativechart.cpp +++ b/plugins/declarative/declarativechart.cpp @@ -41,6 +41,12 @@ #include "qdatetimeaxis.h" #endif +#ifdef CHARTS_FOR_QUICK2 +#include +#include +#include +#endif + QTCOMMERCIALCHART_BEGIN_NAMESPACE /*! @@ -261,6 +267,9 @@ void DeclarativeChart::initChart(QChart::ChartType type) setAntialiasing(QQuickItem::antialiasing()); connect(m_scene, SIGNAL(changed(QList)), this, SLOT(update())); connect(this, SIGNAL(antialiasingChanged(bool)), this, SLOT(handleAntialiasingChanged(bool))); + + setAcceptedMouseButtons(Qt::AllButtons); + setAcceptHoverEvents(true); #else if (type == QChart::ChartTypePolar) m_chart = new QPolarChart(this); @@ -386,7 +395,6 @@ void DeclarativeChart::handleAxisYRightSet(QAbstractAxis *axis) void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) { -// qDebug() << "DeclarativeChart::geometryChanged" << newGeometry.width() << newGeometry.height(); if (newGeometry.isValid()) { if (newGeometry.width() > 0 && newGeometry.height() > 0) { m_chart->resize(newGeometry.width(), newGeometry.height()); @@ -407,6 +415,78 @@ void DeclarativeChart::paint(QPainter *painter) m_scene->render(painter, renderRect, renderRect); } +void DeclarativeChart::mousePressEvent(QMouseEvent *event) +{ + m_mousePressScenePoint = event->pos(); + m_mousePressScreenPoint = event->globalPos(); + m_lastMouseMoveScenePoint = m_mousePressScenePoint; + m_lastMouseMoveScreenPoint = m_mousePressScreenPoint; + m_mousePressButton = event->button(); + m_mousePressButtons = event->buttons(); + + QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMousePress); + mouseEvent.setWidget(0); + mouseEvent.setButtonDownScenePos(m_mousePressButton, m_mousePressScenePoint); + mouseEvent.setButtonDownScreenPos(m_mousePressButton, m_mousePressScreenPoint); + mouseEvent.setScenePos(m_mousePressScenePoint); + mouseEvent.setScreenPos(m_mousePressScreenPoint); + mouseEvent.setLastScenePos(m_lastMouseMoveScenePoint); + mouseEvent.setLastScreenPos(m_lastMouseMoveScreenPoint); + mouseEvent.setButtons(m_mousePressButtons); + mouseEvent.setButton(m_mousePressButton); + mouseEvent.setModifiers(event->modifiers()); + mouseEvent.setAccepted(false); + + QApplication::sendEvent(m_scene, &mouseEvent); +} + +void DeclarativeChart::mouseReleaseEvent(QMouseEvent *event) +{ + QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMouseRelease); + mouseEvent.setWidget(0); + mouseEvent.setButtonDownScenePos(m_mousePressButton, m_mousePressScenePoint); + mouseEvent.setButtonDownScreenPos(m_mousePressButton, m_mousePressScreenPoint); + mouseEvent.setScenePos(event->pos()); + mouseEvent.setScreenPos(event->globalPos()); + mouseEvent.setLastScenePos(m_lastMouseMoveScenePoint); + mouseEvent.setLastScreenPos(m_lastMouseMoveScreenPoint); + mouseEvent.setButtons(event->buttons()); + mouseEvent.setButton(event->button()); + mouseEvent.setModifiers(event->modifiers()); + mouseEvent.setAccepted(false); + + QApplication::sendEvent(m_scene, &mouseEvent); + + m_mousePressButtons = event->buttons(); + m_mousePressButton = Qt::NoButton; +} + +void DeclarativeChart::hoverMoveEvent(QHoverEvent *event) +{ + // Convert hover move to mouse move, since we don't seem to get actual mouse move events. + // QGraphicsScene generates hover events from mouse move events, so we don't need + // to pass hover events there. + QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMouseMove); + mouseEvent.setWidget(0); + mouseEvent.setButtonDownScenePos(m_mousePressButton, m_mousePressScenePoint); + mouseEvent.setButtonDownScreenPos(m_mousePressButton, m_mousePressScreenPoint); + mouseEvent.setScenePos(event->pos()); + // Hover events do not have global pos in them, and the screen position doesn't seem to + // matter anyway in this use case, so just pass event pos instead of trying to + // calculate the real screen position. + mouseEvent.setScreenPos(event->pos()); + mouseEvent.setLastScenePos(m_lastMouseMoveScenePoint); + mouseEvent.setLastScreenPos(m_lastMouseMoveScreenPoint); + mouseEvent.setButtons(m_mousePressButtons); + mouseEvent.setButton(m_mousePressButton); + mouseEvent.setModifiers(event->modifiers()); + m_lastMouseMoveScenePoint = mouseEvent.scenePos(); + m_lastMouseMoveScreenPoint = mouseEvent.screenPos(); + mouseEvent.setAccepted(false); + + QApplication::sendEvent(m_scene, &mouseEvent); +} + void DeclarativeChart::handleAntialiasingChanged(bool enable) { setAntialiasing(enable); diff --git a/plugins/declarative/declarativechart.h b/plugins/declarative/declarativechart.h index c02434e..9fa4554 100644 --- a/plugins/declarative/declarativechart.h +++ b/plugins/declarative/declarativechart.h @@ -111,6 +111,10 @@ public: // From parent classes void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry); #ifdef CHARTS_FOR_QUICK2 void paint(QPainter *painter); +protected: + void mousePressEvent(QMouseEvent *event); + void mouseReleaseEvent(QMouseEvent *event); + void hoverMoveEvent(QHoverEvent *event); private Q_SLOTS: void handleAntialiasingChanged(bool enable); #endif @@ -198,6 +202,12 @@ private: QChart *m_chart; #ifdef CHARTS_FOR_QUICK2 QGraphicsScene *m_scene; + QPointF m_mousePressScenePoint; + QPoint m_mousePressScreenPoint; + QPointF m_lastMouseMoveScenePoint; + QPoint m_lastMouseMoveScreenPoint; + Qt::MouseButton m_mousePressButton; + Qt::MouseButtons m_mousePressButtons; #endif DeclarativeMargins *m_margins; }; diff --git a/tests/quick2chartproperties/qml/quick2chartproperties/LineChart.qml b/tests/quick2chartproperties/qml/quick2chartproperties/LineChart.qml index b2070cf..787b619 100644 --- a/tests/quick2chartproperties/qml/quick2chartproperties/LineChart.qml +++ b/tests/quick2chartproperties/qml/quick2chartproperties/LineChart.qml @@ -63,5 +63,6 @@ ChartView { XYPoint { x: 3.4; y: 2.0 } XYPoint { x: 4.1; y: 2.3 } onClicked: console.log(name + ".onClicked: " + point.x + ", " + point.y); + onHovered: console.log(name + ".onHovered: " + point.x + ", " + point.y); } } diff --git a/tests/quick2chartproperties/qml/quick2chartproperties/ScatterChart.qml b/tests/quick2chartproperties/qml/quick2chartproperties/ScatterChart.qml index a360c74..e6fb3da 100644 --- a/tests/quick2chartproperties/qml/quick2chartproperties/ScatterChart.qml +++ b/tests/quick2chartproperties/qml/quick2chartproperties/ScatterChart.qml @@ -41,6 +41,7 @@ ChartView { onVisibleChanged: console.log("scatterSeries.onVisibleChanged: " + visible); onOpacityChanged: console.log(name + ".onOpacityChanged: " + opacity); onClicked: console.log(name + ".onClicked: " + point.x + ", " + point.y); + onHovered: console.log(name + ".onHovered: " + point.x + ", " + point.y); onPointReplaced: console.log("scatterSeries.onPointReplaced: " + index); onPointRemoved: console.log("scatterSeries.onPointRemoved: " + index); onPointAdded: console.log("scatterSeries.onPointAdded: " + series.at(index).x + ", " + series.at(index).y); @@ -59,5 +60,6 @@ ChartView { XYPoint { x: 2.4; y: 2.7 } XYPoint { x: 2.67; y: 2.65 } onClicked: console.log(name + ".onClicked: " + point.x + ", " + point.y); + onHovered: console.log(name + ".onHovered: " + point.x + ", " + point.y); } } diff --git a/tests/quick2chartproperties/qml/quick2chartproperties/SplineChart.qml b/tests/quick2chartproperties/qml/quick2chartproperties/SplineChart.qml index bbeccd9..c66fc96 100644 --- a/tests/quick2chartproperties/qml/quick2chartproperties/SplineChart.qml +++ b/tests/quick2chartproperties/qml/quick2chartproperties/SplineChart.qml @@ -42,6 +42,7 @@ ChartView { onVisibleChanged: console.log("splineSeries.onVisibleChanged: " + visible); onOpacityChanged: console.log(name + ".onOpacityChanged: " + opacity); onClicked: console.log(name + ".onClicked: " + point.x + ", " + point.y); + onHovered: console.log(name + ".onHovered: " + point.x + ", " + point.y); onPointReplaced: console.log("splineSeries.onPointReplaced: " + index); onPointRemoved: console.log("splineSeries.onPointRemoved: " + index); onPointAdded: console.log("splineSeries.onPointAdded: " + series.at(index).x + ", " + series.at(index).y); @@ -61,5 +62,6 @@ ChartView { XYPoint { x: 3.4; y: 2.0 } XYPoint { x: 4.1; y: 2.3 } onClicked: console.log(name + ".onClicked: " + point.x + ", " + point.y); + onHovered: console.log(name + ".onHovered: " + point.x + ", " + point.y); } }