From ca36badc8925788e06910fab5200633d40e0f328 Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Mon, 13 Sep 2010 15:16:57 +1000 Subject: When onDoubleClicked: is handled don't emit a second onPressed/onClicked Task-number: QTBUG-12250 Reviewed-by: Michael Brasser --- .../graphicsitems/qdeclarativemousearea.cpp | 17 +++++++----- .../graphicsitems/qdeclarativemousearea_p_p.h | 6 ++++ .../qdeclarativemousearea/data/doubleclick.qml | 14 ++++++++++ .../tst_qdeclarativemousearea.cpp | 32 ++++++++++++++++++++++ 4 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 tests/auto/declarative/qdeclarativemousearea/data/doubleclick.qml diff --git a/src/declarative/graphicsitems/qdeclarativemousearea.cpp b/src/declarative/graphicsitems/qdeclarativemousearea.cpp index 5516611..4685e65 100644 --- a/src/declarative/graphicsitems/qdeclarativemousearea.cpp +++ b/src/declarative/graphicsitems/qdeclarativemousearea.cpp @@ -303,7 +303,9 @@ QDeclarativeMouseAreaPrivate::~QDeclarativeMouseAreaPrivate() The \l {MouseEvent}{mouse} parameter provides information about the click, including the x and y position of the release of the click, and whether the click was held. - The \e accepted property of the MouseEvent parameter is ignored in this handler. + If the \e accepted property of the \l {MouseEvent}{mouse} parameter is set to false + in the handler, the onPressed/onReleased/onClicked handlers will be called for the second + click; otherwise they are supressed. The accepted property defaults to true. */ /*! @@ -525,12 +527,13 @@ void QDeclarativeMouseArea::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *even if (!d->absorb) { QDeclarativeItem::mouseDoubleClickEvent(event); } else { - QDeclarativeItem::mouseDoubleClickEvent(event); - if (event->isAccepted()) { - // Only deliver the event if we have accepted the press. - d->saveEvent(event); - QDeclarativeMouseEvent me(d->lastPos.x(), d->lastPos.y(), d->lastButton, d->lastButtons, d->lastModifiers, true, false); - emit this->doubleClicked(&me); + d->saveEvent(event); + QDeclarativeMouseEvent me(d->lastPos.x(), d->lastPos.y(), d->lastButton, d->lastButtons, d->lastModifiers, true, false); + me.setAccepted(d->isDoubleClickConnected()); + emit this->doubleClicked(&me); + if (!me.isAccepted()) { + // Only deliver the press event if we haven't accepted the double click. + QDeclarativeItem::mouseDoubleClickEvent(event); } } } diff --git a/src/declarative/graphicsitems/qdeclarativemousearea_p_p.h b/src/declarative/graphicsitems/qdeclarativemousearea_p_p.h index cf9dc18..48a56d9 100644 --- a/src/declarative/graphicsitems/qdeclarativemousearea_p_p.h +++ b/src/declarative/graphicsitems/qdeclarativemousearea_p_p.h @@ -95,6 +95,12 @@ public: return QObjectPrivate::get(q)->isSignalConnected(idx); } + bool isDoubleClickConnected() { + Q_Q(QDeclarativeMouseArea); + static int idx = QObjectPrivate::get(q)->signalIndex("doubleClicked(QDeclarativeMouseEvent*)"); + return QObjectPrivate::get(q)->isSignalConnected(idx); + } + bool absorb : 1; bool hovered : 1; bool pressed : 1; diff --git a/tests/auto/declarative/qdeclarativemousearea/data/doubleclick.qml b/tests/auto/declarative/qdeclarativemousearea/data/doubleclick.qml new file mode 100644 index 0000000..9cddf1b --- /dev/null +++ b/tests/auto/declarative/qdeclarativemousearea/data/doubleclick.qml @@ -0,0 +1,14 @@ +import Qt 4.7 + +Item { + id: root + property int clicked: 0 + property int doubleClicked: 0 + + MouseArea { + width: 200; height: 200 + onClicked: { root.clicked++ } + onDoubleClicked: { root.doubleClicked++ } + } +} + diff --git a/tests/auto/declarative/qdeclarativemousearea/tst_qdeclarativemousearea.cpp b/tests/auto/declarative/qdeclarativemousearea/tst_qdeclarativemousearea.cpp index c9bb467..e4ec01f 100644 --- a/tests/auto/declarative/qdeclarativemousearea/tst_qdeclarativemousearea.cpp +++ b/tests/auto/declarative/qdeclarativemousearea/tst_qdeclarativemousearea.cpp @@ -62,6 +62,7 @@ private slots: void updateMouseAreaPosOnResize(); void noOnClickedWithPressAndHold(); void onMousePressRejected(); + void doubleClick(); private: QDeclarativeView *createView(); @@ -390,6 +391,37 @@ void tst_QDeclarativeMouseArea::onMousePressRejected() QVERIFY(!canvas->rootObject()->property("mr2_released").toBool()); } +void tst_QDeclarativeMouseArea::doubleClick() +{ + QDeclarativeView *canvas = createView(); + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/doubleclick.qml")); + canvas->show(); + canvas->setFocus(); + QVERIFY(canvas->rootObject() != 0); + + QGraphicsScene *scene = canvas->scene(); + QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress); + pressEvent.setScenePos(QPointF(100, 100)); + pressEvent.setButton(Qt::LeftButton); + pressEvent.setButtons(Qt::LeftButton); + QApplication::sendEvent(scene, &pressEvent); + + QGraphicsSceneMouseEvent releaseEvent(QEvent::GraphicsSceneMouseRelease); + releaseEvent.setScenePos(QPointF(100, 100)); + releaseEvent.setButton(Qt::LeftButton); + releaseEvent.setButtons(Qt::LeftButton); + QApplication::sendEvent(scene, &releaseEvent); + + QGraphicsSceneMouseEvent dblClickEvent(QEvent::GraphicsSceneMouseDoubleClick); + dblClickEvent.setScenePos(QPointF(100, 100)); + dblClickEvent.setButton(Qt::LeftButton); + dblClickEvent.setButtons(Qt::LeftButton); + QApplication::sendEvent(scene, &dblClickEvent); + + QCOMPARE(canvas->rootObject()->property("clicked").toInt(), 1); + QCOMPARE(canvas->rootObject()->property("doubleClicked").toInt(), 1); +} + QTEST_MAIN(tst_QDeclarativeMouseArea) #include "tst_qdeclarativemousearea.moc" -- cgit v0.12