diff options
author | Shawn Rutledge <shawn.rutledge@qt.io> | 2022-06-17 23:03:06 +0200 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2022-07-09 09:38:36 +0200 |
commit | 7e6db64d4370ee2bf30572aebf1905729cf594dc (patch) | |
tree | 218fd7cbfcbacde83e070f7ac2e0c87392056850 /src/quick/items/qquicktextedit.cpp | |
parent | e4c15239a1c0baaa58dc67384b2038d141860488 (diff) |
Deal with markdown fragments in TextEdit
- as with rich text, append() and insert() assume that if the text
is already markdown, the text to be inserted is too
- as with rich text, text() and getFormattedText() return text with
markdown formatting, whereas getText() and selectedText() convert it
to plain text
- Component.onCompleted markdown initialization is ok
- pasting HTML from the clipboard preserves its formatting, and
text() and getFormattedText() return text with markdown formatting
- selectAll() and copy() make HTML available on the system clipboard
We're beginning to use the goodies from qtbase
7c7606460403e6495b860134f28e8d4d45c6c810
[ChangeLog][QtQuick][TextEdit] TextEdit.insert(), append(), copy(),
paste() etc. are now working with MarkdownText.
Done-with: Ivan Solovev
Pick-to: 6.4
Fixes: QTBUG-94462
Task-number: QTBUG-76105
Change-Id: I01049c769e0e9bfccaf96bf90cb2fcc066276e7c
Reviewed-by: Oliver Eftevaag <oliver.eftevaag@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Diffstat (limited to 'src/quick/items/qquicktextedit.cpp')
-rw-r--r-- | src/quick/items/qquicktextedit.cpp | 53 |
1 files changed, 43 insertions, 10 deletions
diff --git a/src/quick/items/qquicktextedit.cpp b/src/quick/items/qquicktextedit.cpp index ce3b4cbd89..6d8d31febc 100644 --- a/src/quick/items/qquicktextedit.cpp +++ b/src/quick/items/qquicktextedit.cpp @@ -477,21 +477,31 @@ void QQuickTextEdit::setTextFormat(TextFormat format) if (format == d->format) return; - bool wasRich = d->richText; + const bool wasRich = d->richText; + const bool wasMarkdown = d->markdownText; d->richText = format == RichText || (format == AutoText && (wasRich || Qt::mightBeRichText(text()))); d->markdownText = format == MarkdownText; -#if QT_CONFIG(texthtmlparser) if (isComponentComplete()) { - if (wasRich && !d->richText) { +#if QT_CONFIG(texthtmlparser) + if (wasRich && !d->richText && !d->markdownText) { d->control->setPlainText(!d->textCached ? d->control->toHtml() : d->text); updateSize(); } else if (!wasRich && d->richText) { d->control->setHtml(!d->textCached ? d->control->toPlainText() : d->text); updateSize(); } - } #endif +#if QT_CONFIG(textmarkdownwriter) && QT_CONFIG(textmarkdownreader) + if (wasMarkdown && !d->markdownText && !d->richText) { + d->control->setPlainText(!d->textCached ? d->control->toMarkdown() : d->text); + updateSize(); + } else if (!wasMarkdown && d->markdownText) { + d->control->setMarkdownText(!d->textCached ? d->control->toPlainText() : d->text); + updateSize(); + } +#endif + } d->format = format; d->control->setAcceptRichText(d->format != PlainText); @@ -1324,7 +1334,7 @@ QString QQuickTextEdit::selectedText() const { Q_D(const QQuickTextEdit); #if QT_CONFIG(texthtmlparser) - return d->richText + return d->richText || d->markdownText ? d->control->textCursor().selectedText() : d->control->textCursor().selection().toPlainText(); #else @@ -1495,6 +1505,11 @@ void QQuickTextEdit::componentComplete() d->control->setHtml(d->text); else #endif +#if QT_CONFIG(textmarkdownreader) + if (d->markdownText) + d->control->setMarkdownText(d->text); + else +#endif if (!d->text.isEmpty()) { if (d->markdownText) d->control->setMarkdownText(d->text); @@ -2904,7 +2919,7 @@ QString QQuickTextEdit::getText(int start, int end) const cursor.setPosition(start, QTextCursor::MoveAnchor); cursor.setPosition(end, QTextCursor::KeepAnchor); #if QT_CONFIG(texthtmlparser) - return d->richText + return d->richText || d->markdownText ? cursor.selectedText() : cursor.selection().toPlainText(); #else @@ -2937,6 +2952,12 @@ QString QQuickTextEdit::getFormattedText(int start, int end) const #else return cursor.selection().toPlainText(); #endif + } else if (d->markdownText) { +#if QT_CONFIG(textmarkdownwriter) + return cursor.selection().toMarkdown(); +#else + return cursor.selection().toPlainText(); +#endif } else { return cursor.selection().toPlainText(); } @@ -2961,6 +2982,12 @@ void QQuickTextEdit::insert(int position, const QString &text) #else cursor.insertText(text); #endif + } else if (d->markdownText) { +#if QT_CONFIG(textmarkdownreader) + cursor.insertMarkdown(text); +#else + cursor.insertText(text); +#endif } else { cursor.insertText(text); } @@ -3103,15 +3130,21 @@ void QQuickTextEdit::append(const QString &text) if (!d->document->isEmpty()) cursor.insertBlock(); -#if QT_CONFIG(texthtmlparser) if (d->format == RichText || (d->format == AutoText && Qt::mightBeRichText(text))) { +#if QT_CONFIG(texthtmlparser) cursor.insertHtml(text); +#else + cursor.insertText(text); +#endif + } else if (d->format == MarkdownText) { +#if QT_CONFIG(textmarkdownreader) + cursor.insertMarkdown(text); +#else + cursor.insertText(text); +#endif } else { cursor.insertText(text); } -#else - cursor.insertText(text); -#endif // texthtmlparser cursor.endEditBlock(); d->control->updateCursorRectangle(false); |