From: Andrew den Exter Date: Mon, 28 Nov 2011 04:03:34 +0000 (+1000) Subject: Add a getFormattedText function to TextEdit. X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=d0d12171b06d568bfcc7717471f4af5b877cfc1f;p=konrad%2Fqtdeclarative.git Add a getFormattedText function to TextEdit. The same as getText except it include formatting tags if the TextEdit has a rich text format set. Change-Id: I601e8d396254ab6105aa7d105e25b14fcf69c4e5 Reviewed-by: Martin Jones --- diff --git a/src/declarative/items/qquicktextedit.cpp b/src/declarative/items/qquicktextedit.cpp index 1a467c5..2e79920 100644 --- a/src/declarative/items/qquicktextedit.cpp +++ b/src/declarative/items/qquicktextedit.cpp @@ -1995,6 +1995,36 @@ QString QQuickTextEdit::getText(int start, int end) const } /*! + \qmlmethod string QtQuick2::TextEdit::getFormattedText(int start, int end) + + Returns the section of text that is between the \a start and \a end positions. + + The returned text will be formatted according the \l textFormat property. +*/ + +QString QQuickTextEdit::getFormattedText(int start, int end) const +{ + Q_D(const QQuickTextEdit); + + start = qBound(0, start, d->document->characterCount() - 1); + end = qBound(0, end, d->document->characterCount() - 1); + + QTextCursor cursor(d->document); + cursor.setPosition(start, QTextCursor::MoveAnchor); + cursor.setPosition(end, QTextCursor::KeepAnchor); + + if (d->richText) { +#ifndef QT_NO_TEXTHTMLPARSER + return cursor.selection().toHtml(); +#else + return cursor.selection().toPlainText(); +#endif + } else { + return cursor.selection().toPlainText(); + } +} + +/*! \qmlmethod void QtQuick2::TextEdit::insert(int position, string text) Inserts \a text into the TextEdit at position. diff --git a/src/declarative/items/qquicktextedit_p.h b/src/declarative/items/qquicktextedit_p.h index f2bec6c..66b68ff 100644 --- a/src/declarative/items/qquicktextedit_p.h +++ b/src/declarative/items/qquicktextedit_p.h @@ -218,6 +218,7 @@ public: bool isInputMethodComposing() const; Q_INVOKABLE QString getText(int start, int end) const; + Q_INVOKABLE QString getFormattedText(int start, int end) const; Q_SIGNALS: void textChanged(const QString &); diff --git a/tests/auto/declarative/qquicktextedit/tst_qquicktextedit.cpp b/tests/auto/declarative/qquicktextedit/tst_qquicktextedit.cpp index ae1e4b4..9b53c56 100644 --- a/tests/auto/declarative/qquicktextedit/tst_qquicktextedit.cpp +++ b/tests/auto/declarative/qquicktextedit/tst_qquicktextedit.cpp @@ -159,6 +159,8 @@ private slots: void getText_data(); void getText(); + void getFormattedText_data(); + void getFormattedText(); void insert_data(); void insert(); void remove_data(); @@ -2411,7 +2413,7 @@ void tst_qquicktextedit::getText_data() << 0 << plainBoldText.length() << plainBoldText; - QTest::newRow("rick text sub string") + QTest::newRow("rich text sub string") << richBoldText << 14 << 21 << plainBoldText.mid(14, 7); @@ -2433,6 +2435,115 @@ void tst_qquicktextedit::getText() QCOMPARE(textEdit->getText(start, end), expectedText); } +void tst_qquicktextedit::getFormattedText_data() +{ + QTest::addColumn("text"); + QTest::addColumn("textFormat"); + QTest::addColumn("start"); + QTest::addColumn("end"); + QTest::addColumn("expectedText"); + + const QString richBoldText = QStringLiteral("This is some bold text"); + const QString plainBoldText = QStringLiteral("This is some bold text"); + + QTest::newRow("all plain text") + << standard.at(0) + << QQuickTextEdit::PlainText + << 0 << standard.at(0).length() + << standard.at(0); + + QTest::newRow("plain text sub string") + << standard.at(0) + << QQuickTextEdit::PlainText + << 0 << 12 + << standard.at(0).mid(0, 12); + + QTest::newRow("plain text sub string reversed") + << standard.at(0) + << QQuickTextEdit::PlainText + << 12 << 0 + << standard.at(0).mid(0, 12); + + QTest::newRow("plain text cropped beginning") + << standard.at(0) + << QQuickTextEdit::PlainText + << -3 << 4 + << standard.at(0).mid(0, 4); + + QTest::newRow("plain text cropped end") + << standard.at(0) + << QQuickTextEdit::PlainText + << 23 << standard.at(0).length() + 8 + << standard.at(0).mid(23); + + QTest::newRow("plain text cropped beginning and end") + << standard.at(0) + << QQuickTextEdit::PlainText + << -9 << standard.at(0).length() + 4 + << standard.at(0); + + QTest::newRow("all rich (Auto) text") + << richBoldText + << QQuickTextEdit::AutoText + << 0 << plainBoldText.length() + << QString("This is some \\<.*\\>bold\\ text"); + + QTest::newRow("all rich (Rich) text") + << richBoldText + << QQuickTextEdit::RichText + << 0 << plainBoldText.length() + << QString("This is some \\<.*\\>bold\\ text"); + + QTest::newRow("all rich (Plain) text") + << richBoldText + << QQuickTextEdit::PlainText + << 0 << richBoldText.length() + << richBoldText; + + QTest::newRow("rich (Auto) text sub string") + << richBoldText + << QQuickTextEdit::AutoText + << 14 << 21 + << QString("\\<.*\\>old\\ tex"); + + QTest::newRow("rich (Rich) text sub string") + << richBoldText + << QQuickTextEdit::RichText + << 14 << 21 + << QString("\\<.*\\>old\\ tex"); + + QTest::newRow("rich (Plain) text sub string") + << richBoldText + << QQuickTextEdit::PlainText + << 17 << 27 + << richBoldText.mid(17, 10); +} + +void tst_qquicktextedit::getFormattedText() +{ + QFETCH(QString, text); + QFETCH(QQuickTextEdit::TextFormat, textFormat); + QFETCH(int, start); + QFETCH(int, end); + QFETCH(QString, expectedText); + + QString componentStr = "import QtQuick 2.0\nTextEdit {}"; + QDeclarativeComponent textEditComponent(&engine); + textEditComponent.setData(componentStr.toLatin1(), QUrl()); + QQuickTextEdit *textEdit = qobject_cast(textEditComponent.create()); + QVERIFY(textEdit != 0); + + textEdit->setTextFormat(textFormat); + textEdit->setText(text); + + if (textFormat == QQuickTextEdit::RichText + || (textFormat == QQuickTextEdit::AutoText && Qt::mightBeRichText(text))) { + QVERIFY(textEdit->getFormattedText(start, end).contains(QRegExp(expectedText))); + } else { + QCOMPARE(textEdit->getFormattedText(start, end), expectedText); + } +} + void tst_qquicktextedit::insert_data() { QTest::addColumn("text");