From 576d577438f1193bbc934e904b809d5b23b8d54e Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Fri, 13 May 2011 15:28:30 +1000 Subject: ListViews loses items if all visible items are removed. Occurs when at end of list and all visible items are removed in multiple steps, without entering the event loop. We were not updating visibleIndex if there were no visible items when handling itemsRemoved(). Also avoid skipping items in refill if there are no valid visible items for reference. Change-Id: I2ff58fb191f6b053f33d5446220d597eb15b66d4 Task-number: QTBUG-19198 Reviewed-by: Bea Lam --- src/declarative/graphicsitems/qdeclarativelistview.cpp | 11 +++++++---- .../qdeclarativelistview/tst_qdeclarativelistview.cpp | 10 ++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/declarative/graphicsitems/qdeclarativelistview.cpp b/src/declarative/graphicsitems/qdeclarativelistview.cpp index cb40384..79d67e7 100644 --- a/src/declarative/graphicsitems/qdeclarativelistview.cpp +++ b/src/declarative/graphicsitems/qdeclarativelistview.cpp @@ -733,6 +733,7 @@ void QDeclarativeListViewPrivate::refill(qreal from, qreal to, bool doBuffer) if (doBuffer && (bufferMode & BufferBefore)) fillFrom = bufferFrom; + bool haveValidItems = false; int modelIndex = visibleIndex; qreal itemEnd = visiblePos-1; if (!visibleItems.isEmpty()) { @@ -741,11 +742,13 @@ void QDeclarativeListViewPrivate::refill(qreal from, qreal to, bool doBuffer) int i = visibleItems.count() - 1; while (i > 0 && visibleItems.at(i)->index == -1) --i; - if (visibleItems.at(i)->index != -1) + if (visibleItems.at(i)->index != -1) { + haveValidItems = true; modelIndex = visibleItems.at(i)->index + 1; + } } - if (visibleItems.count() && (fillFrom > itemEnd+averageSize+spacing + if (haveValidItems && (fillFrom > itemEnd+averageSize+spacing || fillTo < visiblePos - averageSize - spacing)) { // We've jumped more than a page. Estimate which items are now // visible and fill from there. @@ -3406,9 +3409,9 @@ void QDeclarativeListView::itemsRemoved(int modelIndex, int count) } } - if (removedVisible && !haveVisibleIndex) { + if (!haveVisibleIndex) { d->timeline.clear(); - if (d->itemCount == 0) { + if (removedVisible && d->itemCount == 0) { d->visibleIndex = 0; d->visiblePos = d->header ? d->header->size() : 0; d->setPosition(0); diff --git a/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp b/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp index 8b90030..23ac523 100644 --- a/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp +++ b/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp @@ -714,6 +714,16 @@ void tst_QDeclarativeListView::removed(bool animated) QTRY_VERIFY(name = findItem(contentItem, "textName", model.count()-1)); QCOMPARE(name->text(), QString("New")); + // Add some more items so that we don't run out + for (int i = 50; i < 100; i++) + model.addItem("Item" + QString::number(i), ""); + + // QTBUG-19198 move to end and remove all visible items one at a time. + listview->positionViewAtEnd(); + for (int i = 0; i < 18; ++i) + model.removeItems(model.count() - 1, 1); + QTRY_VERIFY(findItems(contentItem, "wrapper").count() > 16); + delete canvas; } -- cgit v0.12 From 80db0a1e59658f9e445219fc48d9236a79edca72 Mon Sep 17 00:00:00 2001 From: Caio Marcelo de Oliveira Filho Date: Fri, 13 May 2011 17:01:28 +1000 Subject: QDeclarativeMouseArea: block context menu events If the MouseArea accepts the same button used to trigger a context menu event, it should not let the event to be delivered to item behind. This is important for items that do implement contextMenuEvent(), like QDeclarativeWebView. When there's a mouse area on top of this item and that accepts the right click (in Linux), the event was still being delivered and the WebView menu was incorrectly appearing. QtWebKit bug https://bugs.webkit.org/show_bug.cgi?id=56526 documents this problem. Change-Id: I386fac6c96f47b8616e2eeb7e5f97043ba418980 Merge-request: 1185 Reviewed-by: Martin Jones --- .../graphicsitems/qdeclarativemousearea.cpp | 26 +++++++++ .../graphicsitems/qdeclarativemousearea_p.h | 3 ++ .../data/preventContextMenu.qml | 22 ++++++++ .../tst_qdeclarativemousearea.cpp | 63 ++++++++++++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 tests/auto/declarative/qdeclarativemousearea/data/preventContextMenu.qml diff --git a/src/declarative/graphicsitems/qdeclarativemousearea.cpp b/src/declarative/graphicsitems/qdeclarativemousearea.cpp index d4e7f7b..6633256 100644 --- a/src/declarative/graphicsitems/qdeclarativemousearea.cpp +++ b/src/declarative/graphicsitems/qdeclarativemousearea.cpp @@ -660,6 +660,32 @@ void QDeclarativeMouseArea::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) setHovered(false); } +#ifndef QT_NO_CONTEXTMENU +void QDeclarativeMouseArea::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) +{ + bool acceptsContextMenuButton; +#if defined(Q_OS_SYMBIAN) + // In Symbian a Long Tap on the screen will trigger. See QSymbianControl::HandleLongTapEventL(). + acceptsContextMenuButton = acceptedButtons() & Qt::LeftButton; +#elif defined(Q_WS_WINCE) + // ### WinCE can trigger context menu event with a gesture in the left button or a + // click with the right button. Since we have no way here to differentiate them when + // event happens, accepting either of the them will block the event. + acceptsContextMenuButton = acceptedButtons() & (Qt::LeftButton | Qt::RightButton); +#else + acceptsContextMenuButton = acceptedButtons() & Qt::RightButton; +#endif + + if (isEnabled() && event->reason() == QGraphicsSceneContextMenuEvent::Mouse + && acceptsContextMenuButton) { + // Do not let the context menu event propagate to items behind. + return; + } + + QDeclarativeItem::contextMenuEvent(event); +} +#endif // QT_NO_CONTEXTMENU + bool QDeclarativeMouseArea::sceneEvent(QEvent *event) { bool rv = QDeclarativeItem::sceneEvent(event); diff --git a/src/declarative/graphicsitems/qdeclarativemousearea_p.h b/src/declarative/graphicsitems/qdeclarativemousearea_p.h index 351d4de..0fe8c6a 100644 --- a/src/declarative/graphicsitems/qdeclarativemousearea_p.h +++ b/src/declarative/graphicsitems/qdeclarativemousearea_p.h @@ -190,6 +190,9 @@ protected: void hoverEnterEvent(QGraphicsSceneHoverEvent *event); void hoverMoveEvent(QGraphicsSceneHoverEvent *event); void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); +#ifndef QT_NO_CONTEXTMENU + void contextMenuEvent(QGraphicsSceneContextMenuEvent *event); +#endif // QT_NO_CONTEXTMENU bool sceneEvent(QEvent *); bool sendMouseEvent(QGraphicsSceneMouseEvent *event); bool sceneEventFilter(QGraphicsItem *i, QEvent *e); diff --git a/tests/auto/declarative/qdeclarativemousearea/data/preventContextMenu.qml b/tests/auto/declarative/qdeclarativemousearea/data/preventContextMenu.qml new file mode 100644 index 0000000..dcbb5d7 --- /dev/null +++ b/tests/auto/declarative/qdeclarativemousearea/data/preventContextMenu.qml @@ -0,0 +1,22 @@ +import QtQuick 1.1 +import Test 1.0 + +Item { + width: 200 + height: 200 + + property alias mouseAreaEnabled: mouseArea.enabled + property alias eventsReceived: receiver.eventCount + + ContextMenuEventReceiver { + id: receiver + anchors.fill: parent + } + + MouseArea { + id: mouseArea + anchors.fill: parent + acceptedButtons: Qt.LeftButton | Qt.MiddleButton | Qt.RightButton + enabled: true + } +} diff --git a/tests/auto/declarative/qdeclarativemousearea/tst_qdeclarativemousearea.cpp b/tests/auto/declarative/qdeclarativemousearea/tst_qdeclarativemousearea.cpp index e1c34fc..fea7865 100644 --- a/tests/auto/declarative/qdeclarativemousearea/tst_qdeclarativemousearea.cpp +++ b/tests/auto/declarative/qdeclarativemousearea/tst_qdeclarativemousearea.cpp @@ -47,6 +47,7 @@ #include #include #include +#include #ifdef Q_OS_SYMBIAN // In Symbian OS test data is located in applications private dir @@ -70,6 +71,9 @@ private slots: void preventStealing(); void testQtQuick11Attributes(); void testQtQuick11Attributes_data(); +#ifndef QT_NO_CONTEXTMENU + void preventContextMenu(); +#endif // QT_NO_CONTEXTMENU private: QDeclarativeView *createView(); @@ -637,6 +641,65 @@ void tst_QDeclarativeMouseArea::testQtQuick11Attributes_data() << ":1 \"MouseArea.preventStealing\" is not available in QtQuick 1.0.\n"; } +#ifndef QT_NO_CONTEXTMENU +class ContextMenuEventReceiver : public QDeclarativeItem +{ + Q_OBJECT + Q_PROPERTY(int eventCount READ eventCount NOTIFY eventCountChanged); +public: + ContextMenuEventReceiver(QDeclarativeItem *parent = 0) : QDeclarativeItem(parent), m_eventCount(0) { } + int eventCount() const { return m_eventCount; } +signals: + void eventCountChanged(int); +protected: + void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) { + if (event->reason() == QGraphicsSceneContextMenuEvent::Mouse) { + m_eventCount++; + emit eventCountChanged(m_eventCount); + } + } +private: + int m_eventCount; +}; + +void tst_QDeclarativeMouseArea::preventContextMenu() +{ + // A MouseArea accepting Left, Middle and Right buttons should prevent context menu + // events with "Mouse" reason to hit the Item below. + + qmlRegisterType("Test", 1, 0, "ContextMenuEventReceiver"); + + QDeclarativeView *view = createView(); + view->setSource(QUrl::fromLocalFile(SRCDIR "/data/preventContextMenu.qml")); + view->show(); + QVERIFY(view->rootObject() != 0); + + QDeclarativeProperty mouseAreaEnabled(view->rootObject(), "mouseAreaEnabled"); + QVERIFY(mouseAreaEnabled.read().toBool()); + + QDeclarativeProperty eventsReceived(view->rootObject(), "eventsReceived"); + QCOMPARE(eventsReceived.read().toInt(), 0); + + QPoint targetPoint = view->mapFromScene(QPoint(80, 80)); + + QContextMenuEvent fakeEvent1(QContextMenuEvent::Mouse, targetPoint); + QApplication::sendEvent(view->viewport(), &fakeEvent1); + QCOMPARE(eventsReceived.read().toInt(), 0); + + mouseAreaEnabled.write(false); + QVERIFY(!mouseAreaEnabled.read().toBool()); + QContextMenuEvent fakeEvent2(QContextMenuEvent::Mouse, targetPoint); + QApplication::sendEvent(view->viewport(), &fakeEvent2); + QCOMPARE(eventsReceived.read().toInt(), 1); + + mouseAreaEnabled.write(true); + QVERIFY(mouseAreaEnabled.read().toBool()); + QContextMenuEvent fakeEvent3(QContextMenuEvent::Mouse, targetPoint); + QApplication::sendEvent(view->viewport(), &fakeEvent3); + QCOMPARE(eventsReceived.read().toInt(), 1); +} +#endif // QT_NO_CONTEXTMENU + QTEST_MAIN(tst_QDeclarativeMouseArea) #include "tst_qdeclarativemousearea.moc" -- cgit v0.12