diff options
author | Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@nokia.com> | 2012-07-12 10:55:04 (GMT) |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-07-13 08:02:33 (GMT) |
commit | ea718a55f119381df25fc9d62126bb76a677ba95 (patch) | |
tree | 24410d26a0a45b880516742bfc04dfb3a67e6b73 | |
parent | aca359dcc14a8ed6bcebdf2ba887dc0fc43f6e01 (diff) | |
download | Qt-ea718a55f119381df25fc9d62126bb76a677ba95.zip Qt-ea718a55f119381df25fc9d62126bb76a677ba95.tar.gz Qt-ea718a55f119381df25fc9d62126bb76a677ba95.tar.bz2 |
Fix crash when column is inserted before rowspanned cell
When you're inserting a column in front of a rowspanned cell
and this cell is not the first in the rowspan, we would get
the wrong logical index of the new cell (putting it in
front of the initial cell with the rowspan). If the cell
does not span all rows, the table will get into a broken state
and trigger asserts in update(). To fix this, we search for
the first cell after the insertion point which has a logical
index higher than the cell directly before the insertion point.
Change-Id: Ic6fb66bc25ad91f3534886f964173ead924429e2
Reviewed-by: Simon Hausmann <simon.hausmann@nokia.com>
-rw-r--r-- | src/gui/text/qtexttable.cpp | 26 | ||||
-rw-r--r-- | tests/auto/qtexttable/tst_qtexttable.cpp | 29 |
2 files changed, 52 insertions, 3 deletions
diff --git a/src/gui/text/qtexttable.cpp b/src/gui/text/qtexttable.cpp index 64b2881..cfbf426 100644 --- a/src/gui/text/qtexttable.cpp +++ b/src/gui/text/qtexttable.cpp @@ -757,10 +757,30 @@ void QTextTable::insertColumns(int pos, int num) QList<int> extendedSpans; for (int i = 0; i < d->nRows; ++i) { int cell; - if (i == d->nRows - 1 && pos == d->nCols) + if (i == d->nRows - 1 && pos == d->nCols) { cell = d->fragment_end; - else - cell = d->grid[i*d->nCols + pos]; + } else { + int logicalGridIndexBeforePosition = pos > 0 + ? d->findCellIndex(d->grid[i*d->nCols + pos - 1]) + : -1; + + // Search for the logical insertion point by skipping past cells which are not the first + // cell in a rowspan. This means any cell for which the logical grid index is + // less than the logical cell index of the cell before the insertion. + int logicalGridIndex; + int gridArrayOffset = i*d->nCols + pos; + do { + cell = d->grid[gridArrayOffset]; + logicalGridIndex = d->findCellIndex(cell); + gridArrayOffset++; + } while (logicalGridIndex < logicalGridIndexBeforePosition + && gridArrayOffset < d->nRows*d->nCols); + + if (logicalGridIndex < logicalGridIndexBeforePosition + && gridArrayOffset == d->nRows*d->nCols) + cell = d->fragment_end; + } + if (pos > 0 && pos < d->nCols && cell == d->grid[i*d->nCols + pos - 1]) { // cell spans the insertion place, extend it if (!extendedSpans.contains(cell)) { diff --git a/tests/auto/qtexttable/tst_qtexttable.cpp b/tests/auto/qtexttable/tst_qtexttable.cpp index 0d8d9ac..8dfa280 100644 --- a/tests/auto/qtexttable/tst_qtexttable.cpp +++ b/tests/auto/qtexttable/tst_qtexttable.cpp @@ -102,6 +102,7 @@ private slots: void removeColumnsInTableWithMergedRows(); void QTBUG11282_insertBeforeMergedEnding_data(); void QTBUG11282_insertBeforeMergedEnding(); + void QTBUG22011_insertBeforeRowSpan(); private: QTextTable *create2x2Table(); @@ -1000,5 +1001,33 @@ void tst_QTextTable::QTBUG11282_insertBeforeMergedEnding() delete textEdit; } +void tst_QTextTable::QTBUG22011_insertBeforeRowSpan() +{ + QTextDocument doc; + QTextCursor cursor(&doc); + QTextTable *table = cursor.insertTable(1,1); // 1x1 + + table->appendColumns(1); // 1x2 + table->appendRows(1); // 2x2 + table->mergeCells(0, 0, 2, 1); // 2x2 + table->insertColumns(1, 1); // 2x3 + table->mergeCells(0, 1, 1, 2); // 2x3 + table->appendRows(1); // 3x3 + table->mergeCells(0, 0, 3, 1); // 3x3 + table->appendRows(1); // 4x3 + table->insertColumns(1, 1); // 4x4 + table->mergeCells(0, 1, 1, 3); + table->mergeCells(1, 1, 1, 2); + table->mergeCells(2, 1, 1, 2); + table->mergeCells(3, 0, 1, 2); + table->insertColumns(3, 1); // 4x5 + table->mergeCells(0, 1, 1, 4); + + table->appendColumns(1); // 4x6 + + QCOMPARE(table->rows(), 4); + QCOMPARE(table->columns(), 6); +} + QTEST_MAIN(tst_QTextTable) #include "tst_qtexttable.moc" |