From 844009f8e0c5d87fda378a6ead1331813a372496 Mon Sep 17 00:00:00 2001 From: Mikolaj Boc Date: Thu, 29 Jun 2023 10:10:10 +0200 Subject: [PATCH] Immediately drag when start distance is 0 in abstract item view This change makes system drag become initiated right after dragging is detected when drag start distance from QApplication::startDragDistance is 0. Otherwise the starting distance is ignored and the value of 1 is implicitly used. The startDragDistance of 0 is needed on WASM, as the native dragstart event arrives exactly after the first mousemove event. With this change, it is possible to set up drag correctly prior to the native dragstart event arriving. Fixes: QTBUG-114947 Change-Id: I112d97d251c9e9b1a39196ddcc39a37024b441f6 Reviewed-by: Volker Hilsheimer --- src/widgets/itemviews/qabstractitemview.cpp | 30 ++++++++++++--------- src/widgets/itemviews/qabstractitemview_p.h | 1 + 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/widgets/itemviews/qabstractitemview.cpp b/src/widgets/itemviews/qabstractitemview.cpp index 5f3861202b..ac1eb0c78d 100644 --- a/src/widgets/itemviews/qabstractitemview.cpp +++ b/src/widgets/itemviews/qabstractitemview.cpp @@ -1857,7 +1857,6 @@ void QAbstractItemView::mousePressEvent(QMouseEvent *event) void QAbstractItemView::mouseMoveEvent(QMouseEvent *event) { Q_D(QAbstractItemView); - QPoint topLeft; QPoint bottomRight = event->position().toPoint(); d->draggedPosition = bottomRight + d->offset(); @@ -1867,13 +1866,7 @@ void QAbstractItemView::mouseMoveEvent(QMouseEvent *event) #if QT_CONFIG(draganddrop) if (state() == DraggingState) { - topLeft = d->pressedPosition - d->offset(); - if ((topLeft - bottomRight).manhattanLength() > QApplication::startDragDistance()) { - d->pressedIndex = QModelIndex(); - startDrag(d->model->supportedDragActions()); - setState(NoState); // the startDrag will return when the dnd operation is done - stopAutoScroll(); - } + d->maybeStartDrag(bottomRight); return; } #endif // QT_CONFIG(draganddrop) @@ -1884,10 +1877,8 @@ void QAbstractItemView::mouseMoveEvent(QMouseEvent *event) || edit(index, NoEditTriggers, event)) return; - if (d->selectionMode != SingleSelection) - topLeft = d->pressedPosition - d->offset(); - else - topLeft = bottomRight; + const QPoint topLeft = + d->selectionMode != SingleSelection ? d->pressedPosition - d->offset() : bottomRight; d->checkMouseMove(index); @@ -1898,6 +1889,7 @@ void QAbstractItemView::mouseMoveEvent(QMouseEvent *event) && (event->buttons() != Qt::NoButton) && !d->selectedDraggableIndexes().isEmpty()) { setState(DraggingState); + d->maybeStartDrag(bottomRight); return; } #endif @@ -4701,6 +4693,20 @@ QModelIndexList QAbstractItemViewPrivate::selectedDraggableIndexes() const indexes.removeIf(isNotDragEnabled); return indexes; } + +void QAbstractItemViewPrivate::maybeStartDrag(QPoint eventPosition) +{ + Q_Q(QAbstractItemView); + + const QPoint topLeft = pressedPosition - offset(); + if ((topLeft - eventPosition).manhattanLength() > QApplication::startDragDistance()) { + pressedIndex = QModelIndex(); + q->startDrag(model->supportedDragActions()); + q->setState(QAbstractItemView::NoState); // the startDrag will return when the dnd operation + // is done + q->stopAutoScroll(); + } +} #endif /*! diff --git a/src/widgets/itemviews/qabstractitemview_p.h b/src/widgets/itemviews/qabstractitemview_p.h index c3cc80e139..bd859e1332 100644 --- a/src/widgets/itemviews/qabstractitemview_p.h +++ b/src/widgets/itemviews/qabstractitemview_p.h @@ -309,6 +309,7 @@ public: #if QT_CONFIG(draganddrop) QModelIndexList selectedDraggableIndexes() const; + void maybeStartDrag(QPoint eventPoint); #endif void doDelayedReset()