summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAxel Rasmussen <axel.rasmussen1@gmail.com>2014-04-15 22:53:36 -0600
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-05-21 17:30:11 +0200
commit2983cb9531d47e5826540ca79e3066a8ed0db30c (patch)
tree39121af5d6e22e256a593fc94ce89c6ffeb2a78c
parentff31090d07cbbb6f67d259438939e810a0baf67f (diff)
Fix broken QPlainTextDocumentLayout after removing chars
This fixes an issue where, if characters were removed from several blocks in a single edit, the document layout would end up being corrupted since the document layout manager wouldn't re-layout the proper number of text blocks. Task-number: QTBUG-30051 Change-Id: Idf3a6f567120e6a5dbebf1f65f685d374219328a Reviewed-by: Konstantin Ritt <ritt.ks@gmail.com> Reviewed-by: Pierre Rossi <pierre.rossi@gmail.com>
-rw-r--r--src/widgets/widgets/qplaintextedit.cpp5
-rw-r--r--tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp45
2 files changed, 48 insertions, 2 deletions
diff --git a/src/widgets/widgets/qplaintextedit.cpp b/src/widgets/widgets/qplaintextedit.cpp
index 6ed1aeff72..23c1e99231 100644
--- a/src/widgets/widgets/qplaintextedit.cpp
+++ b/src/widgets/widgets/qplaintextedit.cpp
@@ -284,14 +284,15 @@ void QPlainTextDocumentLayoutPrivate::relayout()
/*! \reimp
*/
-void QPlainTextDocumentLayout::documentChanged(int from, int /*charsRemoved*/, int charsAdded)
+void QPlainTextDocumentLayout::documentChanged(int from, int charsRemoved, int charsAdded)
{
Q_D(QPlainTextDocumentLayout);
QTextDocument *doc = document();
int newBlockCount = doc->blockCount();
+ int charsChanged = qMax(charsRemoved, charsAdded);
QTextBlock changeStartBlock = doc->findBlock(from);
- QTextBlock changeEndBlock = doc->findBlock(qMax(0, from + charsAdded - 1));
+ QTextBlock changeEndBlock = doc->findBlock(qMax(0, from + charsChanged - 1));
if (changeStartBlock == changeEndBlock && newBlockCount == d->blockCount) {
QTextBlock block = changeStartBlock;
diff --git a/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp b/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp
index 61eb390fd3..c47f7b1ff6 100644
--- a/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp
+++ b/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp
@@ -154,6 +154,7 @@ private slots:
void findBackwardWithRegExp();
void findWithRegExpReturnsFalseIfNoMoreResults();
#endif
+ void layoutAfterMultiLineRemove();
private:
void createSelection();
@@ -1567,5 +1568,49 @@ void tst_QPlainTextEdit::findWithRegExpReturnsFalseIfNoMoreResults()
}
#endif
+void tst_QPlainTextEdit::layoutAfterMultiLineRemove()
+{
+ ed->setVisible(true); // The widget must be visible to reproduce this bug.
+
+ QString contents;
+ for (int i = 0; i < 5; ++i)
+ contents.append("\ttest\n");
+
+ ed->setPlainText(contents);
+
+ /*
+ * Remove the tab from the beginning of lines 2-4, in an edit block. The
+ * edit block is required for the bug to be reproduced.
+ */
+
+ QTextCursor curs = ed->textCursor();
+ curs.movePosition(QTextCursor::Start);
+ curs.movePosition(QTextCursor::NextBlock);
+
+ curs.beginEditBlock();
+ for (int i = 0; i < 3; ++i) {
+ curs.deleteChar();
+ curs.movePosition(QTextCursor::NextBlock);
+ }
+ curs.endEditBlock();
+
+ /*
+ * Now, we're going to perform the following actions:
+ *
+ * - Move to the beginning of the document.
+ * - Move down three times - this should put us at the front of block 3.
+ * - Move to the end of the line.
+ *
+ * At this point, if the document layout is behaving correctly, we should
+ * still be positioned on block 3. Verify that this is the case.
+ */
+
+ curs.movePosition(QTextCursor::Start);
+ curs.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, 3);
+ curs.movePosition(QTextCursor::EndOfLine);
+
+ QCOMPARE(curs.blockNumber(), 3);
+}
+
QTEST_MAIN(tst_QPlainTextEdit)
#include "tst_qplaintextedit.moc"