summaryrefslogtreecommitdiffstats
path: root/tests/auto/widgets/itemviews/qtableview/tst_qtableview.cpp
diff options
context:
space:
mode:
authorMitch Curtis <mitch.curtis@digia.com>2014-06-04 14:39:49 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-06-06 15:50:58 +0200
commita18e3a3ceff970250498b38ec85ae43c67b7993f (patch)
tree15511aa252134e665b459168da97532cae33f7b4 /tests/auto/widgets/itemviews/qtableview/tst_qtableview.cpp
parent5395180fcb9a68b1590ce4bf29ef26745161403f (diff)
Fix key navigation through cells with spans in QTableView.
When navigating with the directional keys or tab/backtab, there are certain situations where the cell that is edited is incorrect. For example, consider the table below. '^' represents the starting cell and the direction of navigation. 'c' represents the index that is arrived at as the currentIndex prior to this patch as reported by view.selectionModel()->currentIndex(). 'x' is the cell that should be edited: +---+---+---+---+ | | | e | | +---+---+---+---+ | | x | | +---+ +---+ | | c | | +---+---+---+---+ | | | ^ | | +---+---+---+---+ Before this patch, the cell that will actually be edited is c, rather than x, so after editing the cell and pressing enter, the previous contents of the cell will still be shown. With this patch, currentIndex() will be changed after every call to cursorMove(). Navigation into and out of cells is not affected because the visualCursor member in the QTableViewPrivate tracks the keyboard navigation entry point. If after the up navigation into the span, the user presses up, the cell entered is 'e', not the cell above 'x'. Task-number: QTBUG-29239 [ChangeLog][QtWidgets][QTableView][QTableWidget] currentIndex() now reflects the top left cell when in a span. Change-Id: I3dc3db46ebba340102860fc4ad98fcaf91484983 Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
Diffstat (limited to 'tests/auto/widgets/itemviews/qtableview/tst_qtableview.cpp')
-rw-r--r--tests/auto/widgets/itemviews/qtableview/tst_qtableview.cpp163
1 files changed, 159 insertions, 4 deletions
diff --git a/tests/auto/widgets/itemviews/qtableview/tst_qtableview.cpp b/tests/auto/widgets/itemviews/qtableview/tst_qtableview.cpp
index 38367fb4ee..33af273284 100644
--- a/tests/auto/widgets/itemviews/qtableview/tst_qtableview.cpp
+++ b/tests/auto/widgets/itemviews/qtableview/tst_qtableview.cpp
@@ -178,6 +178,8 @@ private slots:
void spansAfterColumnInsertion();
void spansAfterRowRemoval();
void spansAfterColumnRemoval();
+ void editSpanFromDirections_data();
+ void editSpanFromDirections();
void checkHeaderReset();
void checkHeaderMinSize();
@@ -1240,28 +1242,28 @@ void tst_QTableView::moveCursorStrikesBack_data()
<< IntList()
<< QRect(1, 2, 2, 3)
<< 2 << 0 << (IntList() << int(QtTestTableView::MoveNext))
- << 2 << 2;
+ << 2 << 1;
QTest::newRow("Span, anchor column disabled") << -1 << -1
<< IntList()
<< (IntList() << 1)
<< QRect(1, 2, 2, 3)
<< 2 << 0 << (IntList() << int(QtTestTableView::MoveNext))
- << 2 << 2;
+ << 2 << 1;
QTest::newRow("Span, anchor row hidden") << 2 << -1
<< IntList()
<< IntList()
<< QRect(1, 2, 2, 3)
<< 1 << 2 << (IntList() << int(QtTestTableView::MoveDown))
- << 3 << 2;
+ << 2 << 1;
QTest::newRow("Span, anchor row disabled") << -1 << -1
<< (IntList() << 2)
<< IntList()
<< QRect(1, 2, 2, 3)
<< 1 << 2 << (IntList() << int(QtTestTableView::MoveDown))
- << 3 << 2;
+ << 2 << 1;
QTest::newRow("Move through span right") << -1 << -1
<< IntList()
@@ -3278,6 +3280,159 @@ void tst_QTableView::spansAfterColumnRemoval()
VERIFY_SPANS_CONSISTENCY(&view);
}
+Q_DECLARE_METATYPE(Qt::Key)
+
+void tst_QTableView::editSpanFromDirections_data()
+{
+ QTest::addColumn<QList<Qt::Key> >("keyPresses");
+ QTest::addColumn<QSharedPointer<QStandardItemModel> >("model");
+ QTest::addColumn<int>("row");
+ QTest::addColumn<int>("column");
+ QTest::addColumn<int>("rowSpan");
+ QTest::addColumn<int>("columnSpan");
+ QTest::addColumn<QModelIndex>("expectedVisualCursorIndex");
+ QTest::addColumn<QModelIndex>("expectedEditedIndex");
+
+ /* x = the cell that should be edited
+ c = the cell that should actually be the current index
+ +---+---+
+ | | |
+ +---+---+
+ | | x |
+ +---+ +
+ | | c |
+ +---+---+
+ | | ^ |
+ +---+---+ */
+ QList<Qt::Key> keyPresses;
+ keyPresses << Qt::Key_Right << Qt::Key_PageDown << Qt::Key_Up;
+ QSharedPointer<QStandardItemModel> model(new QStandardItemModel(4, 2));
+ QTest::newRow("row span, bottom up")
+ << keyPresses << model << 1 << 1 << 2 << 1 << model->index(2, 1) << model->index(1, 1);
+
+ /* +---+---+
+ | | v |
+ +---+---+
+ | |x,c|
+ +---+ +
+ | | |
+ +---+---+
+ | | |
+ +---+---+ */
+ keyPresses.clear();
+ keyPresses << Qt::Key_Right << Qt::Key_Down;
+ model.reset(new QStandardItemModel(4, 2));
+ QTest::newRow("row span, top down")
+ << keyPresses << model << 1 << 1 << 2 << 1 << model->index(1, 1) << model->index(1, 1);
+
+ /* +---+---+---+
+ | | | |
+ +---+---+---+
+ | |x,c| < |
+ +---+ +---+
+ | | | |
+ +---+---+---+ */
+ keyPresses.clear();
+ keyPresses << Qt::Key_End << Qt::Key_Down << Qt::Key_Left;
+ model.reset(new QStandardItemModel(3, 3));
+ QTest::newRow("row span, right to left")
+ << keyPresses << model << 1 << 1 << 2 << 1 << model->index(1, 1) << model->index(1, 1);
+
+ /* +---+---+---+
+ | | | |
+ +---+---+---+
+ | | x | |
+ +---+ +---+
+ | > | c | |
+ +---+---+---+ */
+ keyPresses.clear();
+ keyPresses << Qt::Key_PageDown << Qt::Key_Right;
+ model.reset(new QStandardItemModel(3, 3));
+ QTest::newRow("row span, left to right")
+ << keyPresses << model << 1 << 1 << 2 << 1 << model->index(2, 1) << model->index(1, 1);
+
+ /* +---+---+---+
+ | | | |
+ +---+---+---+
+ |x,c |
+ +---+---+---+
+ | ^ | | |
+ +---+---+---+ */
+ keyPresses.clear();
+ keyPresses << Qt::Key_PageDown << Qt::Key_Up;
+ model.reset(new QStandardItemModel(3, 3));
+ QTest::newRow("col span, bottom up")
+ << keyPresses << model << 1 << 0 << 1 << 3 << model->index(1, 0) << model->index(1, 0);
+
+ /* +---+---+---+
+ | | | |
+ +---+---+---+
+ | x c |
+ +---+---+---+
+ | | ^ | |
+ +---+---+---+ */
+ keyPresses.clear();
+ keyPresses << Qt::Key_PageDown << Qt::Key_Right << Qt::Key_Up;
+ model.reset(new QStandardItemModel(3, 3));
+ QTest::newRow("col span, bottom up #2")
+ << keyPresses << model << 1 << 0 << 1 << 3 << model->index(1, 1) << model->index(1, 0);
+
+ /* +---+---+---+
+ | | | v |
+ +---+---+---+
+ | x c |
+ +---+---+---+
+ | | | |
+ +---+---+---+ */
+ keyPresses.clear();
+ keyPresses << Qt::Key_End << Qt::Key_Down;
+ model.reset(new QStandardItemModel(3, 3));
+ QTest::newRow("col span, top down")
+ << keyPresses << model << 1 << 0 << 1 << 3 << model->index(1, 2) << model->index(1, 0);
+}
+
+class TableViewWithCursorExposed : public QTableView
+{
+public:
+ TableViewWithCursorExposed() :
+ QTableView() {
+ }
+
+public:
+ QModelIndex visualCursorIndex() {
+ QTableViewPrivate *d = static_cast<QTableViewPrivate*>(qt_widget_private(this));
+ return d->model->index(d->visualCursor.y(), d->visualCursor.x());
+ }
+};
+
+void tst_QTableView::editSpanFromDirections()
+{
+ QFETCH(QList<Qt::Key>, keyPresses);
+ QFETCH(QSharedPointer<QStandardItemModel>, model);
+ QFETCH(int, row);
+ QFETCH(int, column);
+ QFETCH(int, rowSpan);
+ QFETCH(int, columnSpan);
+ QFETCH(QModelIndex, expectedVisualCursorIndex);
+ QFETCH(QModelIndex, expectedEditedIndex);
+
+ TableViewWithCursorExposed view;
+ view.setModel(model.data());
+ view.setSpan(row, column, rowSpan, columnSpan);
+ view.show();
+ QVERIFY(QTest::qWaitForWindowActive(&view));
+
+ foreach (Qt::Key key, keyPresses) {
+ QTest::keyClick(&view, key);
+ }
+ QCOMPARE(view.visualCursorIndex(), expectedVisualCursorIndex);
+ QCOMPARE(view.selectionModel()->currentIndex(), expectedEditedIndex);
+
+ QTest::keyClick(&view, Qt::Key_X);
+ QTest::keyClick(QApplication::focusWidget(), Qt::Key_Enter);
+ QTRY_COMPARE(view.model()->data(expectedEditedIndex).toString(), QLatin1String("x"));
+}
+
class Model : public QAbstractTableModel {
Q_OBJECT