From 0bcaed279fc303ffd0fd6e77b0ebc83a4519ac74 Mon Sep 17 00:00:00 2001 From: Richard Moe Gustavsen Date: Wed, 27 Mar 2019 13:23:31 +0100 Subject: QQuickTableView: update calculateTopLeft() to take syncView into account calculateTopLeft() takes care of finding which cell should be the 'corner stone' that needs to be loaded first when doing a rebuild. When we have a syncView, the top left cell should match the top left cell of the syncView, so the logic needs to change quite a bit to take this into account. Change-Id: Ia0b621a3155bbd113fa37c2ed585f16627d46443 Reviewed-by: Mitch Curtis --- src/quick/items/qquicktableview.cpp | 129 +++++++++++++++++++++++++--------- src/quick/items/qquicktableview_p_p.h | 1 - 2 files changed, 97 insertions(+), 33 deletions(-) diff --git a/src/quick/items/qquicktableview.cpp b/src/quick/items/qquicktableview.cpp index da097f8888..c30d40138e 100644 --- a/src/quick/items/qquicktableview.cpp +++ b/src/quick/items/qquicktableview.cpp @@ -1524,48 +1524,103 @@ bool QQuickTableViewPrivate::moveToNextRebuildState() return true; } -QPoint QQuickTableViewPrivate::calculateNewTopLeft() -{ - const int firstVisibleLeft = nextVisibleEdgeIndex(Qt::RightEdge, 0); - const int firstVisibleTop = nextVisibleEdgeIndex(Qt::BottomEdge, 0); - - return QPoint(firstVisibleLeft, firstVisibleTop); -} - -void QQuickTableViewPrivate::calculateTopLeft(QPoint &topLeft, QPointF &topLeftPos) +void QQuickTableViewPrivate::calculateTopLeft(QPoint &topLeftCell, QPointF &topLeftPos) { if (tableSize.isEmpty()) { - releaseLoadedItems(QQmlTableInstanceModel::NotReusable); - topLeft = QPoint(kEdgeIndexAtEnd, kEdgeIndexAtEnd); + // There is no cell that can be top left + topLeftCell.rx() = kEdgeIndexAtEnd; + topLeftCell.ry() = kEdgeIndexAtEnd; return; } - if (rebuildOptions & RebuildOption::All) { - qCDebug(lcTableViewDelegateLifecycle()) << "RebuildOption::All"; - releaseLoadedItems(QQmlTableInstanceModel::NotReusable); - topLeft = calculateNewTopLeft(); - } else if (rebuildOptions & RebuildOption::ViewportOnly) { - qCDebug(lcTableViewDelegateLifecycle()) << "RebuildOption::ViewportOnly"; - releaseLoadedItems(reusableFlag); + if (syncHorizontally || syncVertically) { + const auto syncView_d = syncView->d_func(); - if (rebuildOptions & RebuildOption::CalculateNewTopLeftRow) { - const int newRow = int(viewportRect.y() / (averageEdgeSize.height() + cellSpacing.height())); - topLeft.ry() = qBound(0, newRow, tableSize.height() - 1); - topLeftPos.ry() = topLeft.y() * (averageEdgeSize.height() + cellSpacing.height()); - } else { - topLeft.ry() = qBound(0, topRow(), tableSize.height() - 1); - topLeftPos.ry() = loadedTableOuterRect.topLeft().y(); + if (syncView_d->loadedItems.isEmpty()) { + // The sync view contains no loaded items. This probably means + // that it has not been rebuilt yet. Which also means that + // we cannot rebuild anything before this happens. + topLeftCell.rx() = kEdgeIndexNotSet; + topLeftCell.ry() = kEdgeIndexNotSet; + return; } - if (rebuildOptions & RebuildOption::CalculateNewTopLeftColumn) { + + // Get sync view top left, and use that as our own top left (if possible) + const QPoint syncViewTopLeftCell(syncView_d->leftColumn(), syncView_d->topRow()); + const auto syncViewTopLeftFxItem = syncView_d->loadedTableItem(syncViewTopLeftCell); + const QPointF syncViewTopLeftPos = syncViewTopLeftFxItem->geometry().topLeft(); + + if (syncHorizontally) { + topLeftCell.rx() = syncViewTopLeftCell.x(); + topLeftPos.rx() = syncViewTopLeftPos.x(); + + if (topLeftCell.x() >= tableSize.width()) { + // Top left is outside our own model. + topLeftCell.rx() = kEdgeIndexAtEnd; + topLeftPos.rx() = kEdgeIndexAtEnd; + } + } + + if (syncVertically) { + topLeftCell.ry() = syncViewTopLeftCell.y(); + topLeftPos.ry() = syncViewTopLeftPos.y(); + + if (topLeftCell.y() >= tableSize.height()) { + // Top left is outside our own model. + topLeftCell.ry() = kEdgeIndexAtEnd; + topLeftPos.ry() = kEdgeIndexAtEnd; + } + } + + if (syncHorizontally && syncVertically) { + // We have a valid top left, so we're done + return; + } + } + + // Since we're not sync-ing both horizontal and vertical, calculate the missing + // dimention(s) ourself. If we rebuild all, we find the first visible top-left + // item starting from cell(0, 0). Otherwise, guesstimate which row or column that + // should be the new top-left given the geometry of the viewport. + + if (!syncHorizontally) { + if (rebuildOptions & RebuildOption::All) { + // Find the first visible column from the beginning + topLeftCell.rx() = nextVisibleEdgeIndex(Qt::RightEdge, 0); + if (topLeftCell.x() == kEdgeIndexAtEnd) { + // No visible column found + return; + } + } else if (rebuildOptions & RebuildOption::CalculateNewTopLeftColumn) { + // Guesstimate new top left const int newColumn = int(viewportRect.x() / (averageEdgeSize.width() + cellSpacing.width())); - topLeft.rx() = qBound(0, newColumn, tableSize.width() - 1); - topLeftPos.rx() = topLeft.x() * (averageEdgeSize.width() + cellSpacing.width()); + topLeftCell.rx() = qBound(0, newColumn, tableSize.width() - 1); + topLeftPos.rx() = topLeftCell.x() * (averageEdgeSize.width() + cellSpacing.width()); } else { - topLeft.rx() = qBound(0, leftColumn(), tableSize.width() - 1); + // Keep the current top left, unless it's outside model + topLeftCell.rx() = qBound(0, leftColumn(), tableSize.width() - 1); topLeftPos.rx() = loadedTableOuterRect.topLeft().x(); } - } else { - Q_TABLEVIEW_UNREACHABLE(rebuildOptions); + } + + if (!syncVertically) { + if (rebuildOptions & RebuildOption::All) { + // Find the first visible row from the beginning + topLeftCell.ry() = nextVisibleEdgeIndex(Qt::BottomEdge, 0); + if (topLeftCell.y() == kEdgeIndexAtEnd) { + // No visible row found + return; + } + } else if (rebuildOptions & RebuildOption::CalculateNewTopLeftRow) { + // Guesstimate new top left + const int newRow = int(viewportRect.y() / (averageEdgeSize.height() + cellSpacing.height())); + topLeftCell.ry() = qBound(0, newRow, tableSize.height() - 1); + topLeftPos.ry() = topLeftCell.y() * (averageEdgeSize.height() + cellSpacing.height()); + } else { + // Keep the current top left, unless it's outside model + topLeftCell.ry() = qBound(0, topRow(), tableSize.height() - 1); + topLeftPos.ry() = loadedTableOuterRect.topLeft().y(); + } } } @@ -1577,6 +1632,11 @@ void QQuickTableViewPrivate::beginRebuildTable() QPointF topLeftPos; calculateTopLeft(topLeft, topLeftPos); + if (rebuildOptions & RebuildOption::All) + releaseLoadedItems(QQmlTableInstanceModel::NotReusable); + else if (rebuildOptions & RebuildOption::ViewportOnly) + releaseLoadedItems(reusableFlag); + loadedColumns.clear(); loadedRows.clear(); loadedTableOuterRect = QRect(); @@ -1604,7 +1664,12 @@ void QQuickTableViewPrivate::beginRebuildTable() } if (topLeft.x() == kEdgeIndexAtEnd || topLeft.y() == kEdgeIndexAtEnd) { - qCDebug(lcTableViewDelegateLifecycle()) << "no visible rows or columns, leaving table empty"; + qCDebug(lcTableViewDelegateLifecycle()) << "no visible row or column found, leaving table empty"; + return; + } + + if (topLeft.x() == kEdgeIndexNotSet || topLeft.y() == kEdgeIndexNotSet) { + qCDebug(lcTableViewDelegateLifecycle()) << "could not resolve top-left item, leaving table empty"; return; } diff --git a/src/quick/items/qquicktableview_p_p.h b/src/quick/items/qquicktableview_p_p.h index 0b15e63342..ab7c0c3275 100644 --- a/src/quick/items/qquicktableview_p_p.h +++ b/src/quick/items/qquicktableview_p_p.h @@ -362,7 +362,6 @@ public: void processRebuildTable(); bool moveToNextRebuildState(); - QPoint calculateNewTopLeft(); void calculateTopLeft(QPoint &topLeft, QPointF &topLeftPos); void beginRebuildTable(); void layoutAfterLoadingInitialTable(); -- cgit v1.2.3