From 3f2c9e5253317d1cd97453f668098b2becb41165 Mon Sep 17 00:00:00 2001 From: Richard Moe Gustavsen Date: Thu, 17 Feb 2022 15:27:09 +0100 Subject: QQuickTableView: refactor cancelOvershoot() Since Flickable already has horizontalOvershoot() and verticalOvershoot() functions, there is no reason to calculate this again in TableView. This patch will refactor the overshoot code to use the already available API in Flickable. Task-number: QTBUG-100696 Change-Id: Ifc666e079b0147c3e1cfc6bba2beb287977b303e Reviewed-by: Mitch Curtis --- src/quick/items/qquicktableview.cpp | 67 +++++++++-------------------------- src/quick/items/qquicktableview_p_p.h | 7 ++-- 2 files changed, 19 insertions(+), 55 deletions(-) diff --git a/src/quick/items/qquicktableview.cpp b/src/quick/items/qquicktableview.cpp index ff3ef6638d..adde42321c 100644 --- a/src/quick/items/qquicktableview.cpp +++ b/src/quick/items/qquicktableview.cpp @@ -2420,15 +2420,8 @@ void QQuickTableViewPrivate::processRebuildTable() return; } - if (rebuildState == RebuildState::CancelOvershootBottomRight) { - cancelOvershootBottomRight(); - loadAndUnloadVisibleEdges(); - if (!moveToNextRebuildState()) - return; - } - - if (rebuildState == RebuildState::CancelOvershootTopLeft) { - cancelOvershootTopLeft(); + if (rebuildState == RebuildState::CancelOvershoot) { + cancelOvershootAfterLayout(); loadAndUnloadVisibleEdges(); if (!moveToNextRebuildState()) return; @@ -2770,54 +2763,28 @@ void QQuickTableViewPrivate::adjustViewportYAccordingToAlignment() syncViewportRect(); } -void QQuickTableViewPrivate::cancelOvershootBottomRight() -{ - // After doing a layout with positioning specified, the table might end up in an - // overshooting state (meaning that the table is dragged beyond the bounds - // of the viewport, with the result that it will bounce back in once you touch it). - // This happens because the layouting might try to e.g position the last row in the - // model in the center of the viewport, which will leave a big blank space from the - // last row towards the bottom. - // This space will be detected by updateExtents(), which will adjust the extents so - // that the superfluous blank area ends up as an overshoot. But we shouldn't leave the - // table in an overshooting state after a rebuild, so we clamp it here so thatit's - // either aligned to the top/left or to the bottom/right of the viewport. - // An exception is if we're not rebuilding because of positionViewAtCell(), since - // the app is allowed to set contentX and contentY at start-up. A second exception is - // if this view is a sync child, since then we can end up with extra space at the end - // if our model has fewer rows/columns than the syncView. - const qreal blankSpaceRight = viewportRect.right() - loadedTableOuterRect.right(); - const qreal blankSpaceBottom = viewportRect.bottom() - loadedTableOuterRect.bottom(); - const bool positionVertically = rebuildOptions.testFlag(RebuildOption::PositionViewAtRow); - const bool positionHorizontally = rebuildOptions.testFlag(RebuildOption::PositionViewAtColumn); - - if (positionVertically && !syncVertically && viewportRect.top() > 0 && blankSpaceBottom > 0) { - qCDebug(lcTableViewDelegateLifecycle()) << "cancelling overshoot at bottom:" << blankSpaceBottom; - setLocalViewportY(viewportRect.y() - blankSpaceBottom); - syncViewportRect(); - } - - if (positionHorizontally && !syncHorizontally && viewportRect.left() > 0 && blankSpaceRight > 0) { - qCDebug(lcTableViewDelegateLifecycle()) << "cancelling overshoot at right:" << blankSpaceRight; - setLocalViewportX(viewportRect.x() - blankSpaceRight); - syncViewportRect(); - } -} - -void QQuickTableViewPrivate::cancelOvershootTopLeft() +void QQuickTableViewPrivate::cancelOvershootAfterLayout() { + Q_Q(QQuickTableView); + + // Note: we only want to cancel overshoot from a rebuild if we're supposed to position + // the view on a specific cell. The app is allowed to overshoot by setting contentX and + // contentY manually. Also, if this view is a sync child, we should always stay in sync + // with the syncView, so then we don't do anything. const bool positionVertically = rebuildOptions.testFlag(RebuildOption::PositionViewAtRow); const bool positionHorizontally = rebuildOptions.testFlag(RebuildOption::PositionViewAtColumn); + const bool cancelVertically = positionVertically && !syncVertically; + const bool cancelHorizontally = positionHorizontally && !syncHorizontally; - if (positionVertically && !syncVertically && viewportRect.top() < 0) { - qCDebug(lcTableViewDelegateLifecycle()) << "cancelling overshoot at top:" << viewportRect.top(); - setLocalViewportY(0); + if (cancelHorizontally && !qFuzzyIsNull(q->horizontalOvershoot())) { + qCDebug(lcTableViewDelegateLifecycle()) << "cancelling overshoot horizontally:" << q->horizontalOvershoot(); + setLocalViewportX(q->horizontalOvershoot() < 0 ? -q->minXExtent() : -q->maxXExtent()); syncViewportRect(); } - if (positionHorizontally && !syncHorizontally && viewportRect.left() < 0) { - qCDebug(lcTableViewDelegateLifecycle()) << "cancelling overshoot at left:" << viewportRect.left(); - setLocalViewportX(0); + if (cancelVertically && !qFuzzyIsNull(q->verticalOvershoot())) { + qCDebug(lcTableViewDelegateLifecycle()) << "cancelling overshoot vertically:" << q->verticalOvershoot(); + setLocalViewportY(q->verticalOvershoot() < 0 ? -q->minYExtent() : -q->maxYExtent()); syncViewportRect(); } } diff --git a/src/quick/items/qquicktableview_p_p.h b/src/quick/items/qquicktableview_p_p.h index 141d0cb3d3..5bbdd07528 100644 --- a/src/quick/items/qquicktableview_p_p.h +++ b/src/quick/items/qquicktableview_p_p.h @@ -204,8 +204,7 @@ public: VerifyTable, LayoutTable, LoadAndUnloadAfterLayout, - CancelOvershootBottomRight, - CancelOvershootTopLeft, + CancelOvershoot, PreloadColumns, PreloadRows, MovePreloadedItemsToPool, @@ -439,9 +438,7 @@ public: void layoutAfterLoadingInitialTable(); void adjustViewportXAccordingToAlignment(); void adjustViewportYAccordingToAlignment(); - - void cancelOvershootBottomRight(); - void cancelOvershootTopLeft(); + void cancelOvershootAfterLayout(); void scheduleRebuildTable(QQuickTableViewPrivate::RebuildOptions options); -- cgit v1.2.3