From afc10fe7bbe683d4107cc6439c783b4604320414 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 6 Mar 2014 13:37:41 +0100 Subject: [PATCH] Fix TextEdit::lineCount to update as appropriate The total line count needs to be updated when geometry, document size or wrapping mode changes. Task-number: QTBUG-37263 Change-Id: If58a2f77022475e8bcb7cca0f2a091ee837b39a5 Reviewed-by: Pierre Rossi Reviewed-by: Caroline Chao --- src/quick/items/qquicktextedit.cpp | 2 +- .../quick/qquicktextedit/tst_qquicktextedit.cpp | 51 ++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/quick/items/qquicktextedit.cpp b/src/quick/items/qquicktextedit.cpp index 810a47c..6655720 100644 --- a/src/quick/items/qquicktextedit.cpp +++ b/src/quick/items/qquicktextedit.cpp @@ -2029,7 +2029,6 @@ void QQuickTextEdit::q_textChanged() d->determineHorizontalAlignment(); d->updateDefaultTextOption(); updateSize(); - updateTotalLines(); emit textChanged(); } @@ -2225,6 +2224,7 @@ void QQuickTextEdit::updateSize() if (d->contentSize != size) { d->contentSize = size; emit contentSizeChanged(); + updateTotalLines(); } } diff --git a/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp b/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp index e6a83b3..83fa496 100644 --- a/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp +++ b/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp @@ -108,6 +108,8 @@ private slots: void width(); void wrap(); void textFormat(); + void lineCount_data(); + void lineCount(); // ### these tests may be trivial void hAlign(); @@ -613,6 +615,55 @@ void tst_qquicktextedit::textFormat() } } +static int calcLineCount(QTextDocument* doc) +{ + int subLines = 0; + for (QTextBlock it = doc->begin(); it != doc->end(); it = it.next()) { + QTextLayout *layout = it.layout(); + if (!layout) + continue; + subLines += layout->lineCount()-1; + } + return doc->lineCount() + subLines; +} + +void tst_qquicktextedit::lineCount_data() +{ + QTest::addColumn("texts"); + QTest::newRow("plaintext") << standard; + QTest::newRow("richtext") << richText; +} + +void tst_qquicktextedit::lineCount() +{ + QFETCH(QStringList, texts); + + foreach (const QString& text, texts) { + QQmlComponent component(&engine); + component.setData("import QtQuick 2.0\nTextEdit { }", QUrl()); + + QQuickTextEdit *textedit = qobject_cast(component.create()); + QVERIFY(textedit); + + QTextDocument *doc = QQuickTextEditPrivate::get(textedit)->document; + QVERIFY(doc); + + textedit->setText(text); + + textedit->setWidth(100.0); + QCOMPARE(textedit->lineCount(), calcLineCount(doc)); + + textedit->setWrapMode(QQuickTextEdit::Wrap); + QCOMPARE(textedit->lineCount(), calcLineCount(doc)); + + textedit->setWidth(50.0); + QCOMPARE(textedit->lineCount(), calcLineCount(doc)); + + textedit->setWrapMode(QQuickTextEdit::NoWrap); + QCOMPARE(textedit->lineCount(), calcLineCount(doc)); + } +} + //the alignment tests may be trivial o.oa void tst_qquicktextedit::hAlign() { -- 2.7.4