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 <martin.jones@nokia.com>
This commit is contained in:
parent
b4ad46cdce
commit
d0d12171b0
|
@ -1994,6 +1994,36 @@ QString QQuickTextEdit::getText(int start, int end) const
|
|||
return cursor.selectedText();
|
||||
}
|
||||
|
||||
/*!
|
||||
\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)
|
||||
|
||||
|
|
|
@ -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 &);
|
||||
|
|
|
@ -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<QString>("text");
|
||||
QTest::addColumn<QQuickTextEdit::TextFormat>("textFormat");
|
||||
QTest::addColumn<int>("start");
|
||||
QTest::addColumn<int>("end");
|
||||
QTest::addColumn<QString>("expectedText");
|
||||
|
||||
const QString richBoldText = QStringLiteral("This is some <b>bold</b> 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<QQuickTextEdit*>(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<QString>("text");
|
||||
|
|
Loading…
Reference in New Issue