aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Moe Gustavsen <richard.gustavsen@qt.io>2022-05-20 09:52:59 +0200
committerRichard Moe Gustavsen <richard.gustavsen@qt.io>2022-05-31 13:05:08 +0200
commita1297194ca0b679eedf03a133fa1785a68bcc662 (patch)
tree8af1d9f7c608eb9c1a9f6cb3011a3c63f0f7dcf3
parent065fb607863e6df5d079b27dd275790e64bd982c (diff)
QQuickTableView: ensure we use the correct margins during key navigation
Ensure we use the correct margins when navigating with the arrow keys at the beginning and end of the table. When e.g navigating to the first column in the model, we want to flick the view all the way to the start, margins included. It should already have worked like this, but must have broke after earlier copy/pase operations. Change-Id: I438b20518602e4cc5b49e137f633a7f550a9dec8 Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
-rw-r--r--src/quick/items/qquicktableview.cpp8
-rw-r--r--tests/auto/quick/qquicktableview/tst_qquicktableview.cpp51
2 files changed, 55 insertions, 4 deletions
diff --git a/src/quick/items/qquicktableview.cpp b/src/quick/items/qquicktableview.cpp
index 899616f3d8..2e8d794385 100644
--- a/src/quick/items/qquicktableview.cpp
+++ b/src/quick/items/qquicktableview.cpp
@@ -4931,7 +4931,7 @@ void QQuickTableView::keyPressEvent(QKeyEvent *e)
const int nextRow = d->nextVisibleEdgeIndex(Qt::TopEdge, currentCell.y() - 1);
if (nextRow == kEdgeIndexAtEnd)
break;
- const qreal marginY = d->atTableEnd(Qt::TopEdge, nextRow - 1) ? topMargin() : 0;
+ const qreal marginY = d->atTableEnd(Qt::TopEdge, nextRow - 1) ? -topMargin() : 0;
positionViewAtRow(nextRow, Contain, marginY);
endMoveCurrentIndex({currentCell.x(), nextRow});
break; }
@@ -4940,7 +4940,7 @@ void QQuickTableView::keyPressEvent(QKeyEvent *e)
const int nextRow = d->nextVisibleEdgeIndex(Qt::BottomEdge, currentCell.y() + 1);
if (nextRow == kEdgeIndexAtEnd)
break;
- const qreal marginY = d->atTableEnd(Qt::TopEdge, nextRow + 1) ? bottomMargin() : 0;
+ const qreal marginY = d->atTableEnd(Qt::BottomEdge, nextRow + 1) ? bottomMargin() : 0;
positionViewAtRow(nextRow, Contain, marginY);
endMoveCurrentIndex({currentCell.x(), nextRow});
break; }
@@ -4949,7 +4949,7 @@ void QQuickTableView::keyPressEvent(QKeyEvent *e)
const int nextColumn = d->nextVisibleEdgeIndex(Qt::LeftEdge, currentCell.x() - 1);
if (nextColumn == kEdgeIndexAtEnd)
break;
- const qreal marginX = d->atTableEnd(Qt::LeftEdge, nextColumn - 1) ? leftMargin() : 0;
+ const qreal marginX = d->atTableEnd(Qt::LeftEdge, nextColumn - 1) ? -leftMargin() : 0;
positionViewAtColumn(nextColumn, Contain, marginX);
endMoveCurrentIndex({nextColumn, currentCell.y()});
break; }
@@ -4958,7 +4958,7 @@ void QQuickTableView::keyPressEvent(QKeyEvent *e)
const int nextColumn = d->nextVisibleEdgeIndex(Qt::RightEdge, currentCell.x() + 1);
if (nextColumn == kEdgeIndexAtEnd)
break;
- const qreal marginX = d->atTableEnd(Qt::LeftEdge, nextColumn + 1) ? leftMargin() : 0;
+ const qreal marginX = d->atTableEnd(Qt::RightEdge, nextColumn + 1) ? rightMargin() : 0;
positionViewAtColumn(nextColumn, Contain, marginX);
endMoveCurrentIndex({nextColumn, currentCell.y()});
break; }
diff --git a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp
index 7b5b5e0f70..c219665959 100644
--- a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp
+++ b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp
@@ -227,6 +227,7 @@ private slots:
void setCurrentIndexOnFirstKeyPress_data();
void setCurrentIndexOnFirstKeyPress();
void setCurrentIndexFromMouse();
+ void showMarginsWhenNavigatingToEnd();
void disableKeyNavigation();
void disablePointerNavigation();
void selectUsingArrowKeys();
@@ -4699,6 +4700,56 @@ void tst_QQuickTableView::setCurrentIndexFromMouse()
QCOMPARE(tableView->currentRow(), cellAtEnd.y());
}
+void tst_QQuickTableView::showMarginsWhenNavigatingToEnd()
+{
+ LOAD_TABLEVIEW("plaintableview.qml");
+
+ TestModel model(40, 40);
+ QItemSelectionModel selectionModel(&model);
+
+ tableView->setModel(QVariant::fromValue(&model));
+ tableView->setSelectionModel(&selectionModel);
+ tableView->setAnimate(false);
+ tableView->setFocus(true);
+
+ QQuickWindow *window = tableView->window();
+
+ WAIT_UNTIL_POLISHED;
+
+ const qreal margin = 10;
+ tableView->setLeftMargin(margin);
+ tableView->setRightMargin(margin);
+ tableView->setTopMargin(margin);
+ tableView->setBottomMargin(margin);
+
+ selectionModel.setCurrentIndex(tableView->modelIndex(QPoint(1, 1)), QItemSelectionModel::NoUpdate);
+
+ // move to cell 0, 1
+ QCOMPARE(tableView->contentX(), 0);
+ QTest::keyPress(window, Qt::Key_Left);
+ QCOMPARE(tableView->contentX(), -margin);
+
+ // move to cell 0, 0
+ QCOMPARE(tableView->contentY(), 0);
+ QTest::keyPress(window, Qt::Key_Up);
+ QCOMPARE(tableView->contentY(), -margin);
+
+ selectionModel.setCurrentIndex(tableView->modelIndex(QPoint(38, 38)), QItemSelectionModel::NoUpdate);
+ tableView->positionViewAtCell(tableView->cellAtIndex(selectionModel.currentIndex()), QQuickTableView::Contain);
+
+ WAIT_UNTIL_POLISHED;
+
+ // move to cell 39, 38
+ QTest::keyPress(window, Qt::Key_Right);
+ const qreal cellRightEdge = tableViewPrivate->loadedTableOuterRect.right();
+ QCOMPARE(tableView->contentX(), cellRightEdge + margin - tableView->width());
+
+ // move to cell 39, 39
+ QTest::keyPress(window, Qt::Key_Down);
+ const qreal cellBottomEdge = tableViewPrivate->loadedTableOuterRect.bottom();
+ QCOMPARE(tableView->contentY(), cellBottomEdge + margin - tableView->height());
+}
+
void tst_QQuickTableView::disablePointerNavigation()
{
LOAD_TABLEVIEW("tableviewwithselected1.qml");