From 605634a88a05f175de77e1c7af59fd2a2e0cbc41 Mon Sep 17 00:00:00 2001 From: Kai Koehne Date: Mon, 25 Mar 2019 14:44:14 +0100 Subject: Doc: Note Q[Plain]TextEdit keeping formatting in some cases Fixes: QTBUG-72427 Change-Id: Ifddabb175c480b64282bd8c8fdb9edab4c7ecf44 Reviewed-by: Venugopal Shivashankar --- src/widgets/widgets/qtextedit.cpp | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'src/widgets/widgets/qtextedit.cpp') diff --git a/src/widgets/widgets/qtextedit.cpp b/src/widgets/widgets/qtextedit.cpp index e3a45680ef..ab636660d7 100644 --- a/src/widgets/widgets/qtextedit.cpp +++ b/src/widgets/widgets/qtextedit.cpp @@ -548,7 +548,8 @@ void QTextEditPrivate::_q_ensureVisible(const QRectF &_rect) This property gets and sets the text editor's contents as plain text. Previous contents are removed and undo/redo history is reset - when the property is set. + when the property is set. currentCharFormat() is also reset, unless + textCursor() is already at the beginning of the document. If the text edit has another content type, it will not be replaced by plain text if you call toPlainText(). The only exception to this @@ -1026,7 +1027,12 @@ void QTextEdit::paste() /*! Deletes all the text in the text edit. - Note that the undo/redo history is cleared by this function. + Notes: + \list + \li The undo/redo history is also cleared. + \li currentCharFormat() is reset, unless textCursor() + is already at the beginning of the document. + \endlist \sa cut(), setPlainText(), setHtml() */ @@ -1131,9 +1137,13 @@ void QTextEdit::timerEvent(QTimerEvent *e) Changes the text of the text edit to the string \a text. Any previous text is removed. - \a text is interpreted as plain text. - - Note that the undo/redo history is cleared by this function. + Notes: + \list + \li \a text is interpreted as plain text. + \li The undo/redo history is also cleared. + \li currentCharFormat() is reset, unless textCursor() + is already at the beginning of the document. + \endlist \sa toPlainText() */ @@ -1167,7 +1177,8 @@ QString QTextEdit::toPlainText() const setHtml() changes the text of the text edit. Any previous text is removed and the undo/redo history is cleared. The input text is - interpreted as rich text in html format. + interpreted as rich text in html format. currentCharFormat() is also + reset, unless textCursor() is already at the beginning of the document. \note It is the responsibility of the caller to make sure that the text is correctly decoded when a QString containing HTML is created -- cgit v1.2.3 From 65314b6ce88cdbb28a22be0cab9856ec9bc9604b Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Mon, 18 Dec 2017 08:55:18 +0100 Subject: Add QTextMarkdownImporter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This provides the ability to read from a Markdown string or file into a QTextDocument, such that the formatting will be recognized and can be rendered. - Add QTextDocument::setMarkdown(QString) - Add QTextEdit::setMarkdown(QString) - Add TextFormat::MarkdownText - QWidgetTextControl::setContent() calls QTextDocument::setMarkdown() if that's the format Fixes: QTBUG-72349 Change-Id: Ief2ad71bf840666c64145d58e9ca71d05fad5659 Reviewed-by: Lisandro Damián Nicanor Pérez Meyer Reviewed-by: Gatis Paeglis --- src/widgets/widgets/qtextedit.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src/widgets/widgets/qtextedit.cpp') diff --git a/src/widgets/widgets/qtextedit.cpp b/src/widgets/widgets/qtextedit.cpp index 920133d493..4a06f58b4a 100644 --- a/src/widgets/widgets/qtextedit.cpp +++ b/src/widgets/widgets/qtextedit.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2019 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtWidgets module of the Qt Toolkit. @@ -1202,6 +1202,13 @@ QString QTextEdit::toHtml() const } #endif +#if QT_CONFIG(textmarkdownreader) +void QTextEdit::setMarkdown(const QString &text) +{ + Q_D(const QTextEdit); + d->control->setMarkdown(text); +} +#endif /*! \reimp */ -- cgit v1.2.3 From 23c2da3cc23a2e04a0b3b3c8ad7fa9cc6126ff23 Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Tue, 19 Dec 2017 15:25:55 +0100 Subject: Add QTextMarkdownWriter, QTextEdit::markdown property etc. A QTextDocument can now be written out in Markdown format. - Add the QTextMarkdownWriter as a private class for now - Add QTextDocument::toMarkdown() - QTextDocumentWriter uses QTextMarkdownWriter if setFormat("markdown") is called or if the file suffix is .md or .mkd - Add QTextEdit::toMarkdown() and the markdown property [ChangeLog][QtGui][Text] Markdown (CommonMark or GitHub dialect) is now a supported format for reading into and writing from QTextDocument. Change-Id: I663a77017fac7ae1b3f9a400f5cd357bb40750af Reviewed-by: Gatis Paeglis --- src/widgets/widgets/qtextedit.cpp | 62 +++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 6 deletions(-) (limited to 'src/widgets/widgets/qtextedit.cpp') diff --git a/src/widgets/widgets/qtextedit.cpp b/src/widgets/widgets/qtextedit.cpp index 9e134493b5..5f734258b2 100644 --- a/src/widgets/widgets/qtextedit.cpp +++ b/src/widgets/widgets/qtextedit.cpp @@ -366,8 +366,8 @@ void QTextEditPrivate::_q_ensureVisible(const QRectF &_rect) \section1 Introduction and Concepts QTextEdit is an advanced WYSIWYG viewer/editor supporting rich - text formatting using HTML-style tags. It is optimized to handle - large documents and to respond quickly to user input. + text formatting using HTML-style tags, or Markdown format. It is optimized + to handle large documents and to respond quickly to user input. QTextEdit works on paragraphs and characters. A paragraph is a formatted string which is word-wrapped to fit into the width of @@ -381,7 +381,7 @@ void QTextEditPrivate::_q_ensureVisible(const QRectF &_rect) QTextEdit can display images, lists and tables. If the text is too large to view within the text edit's viewport, scroll bars will appear. The text edit can load both plain text and rich text files. - Rich text is described using a subset of HTML 4 markup, refer to the + Rich text can be described using a subset of HTML 4 markup; refer to the \l {Supported HTML Subset} page for more information. If you just need to display a small piece of rich text use QLabel. @@ -401,12 +401,19 @@ void QTextEditPrivate::_q_ensureVisible(const QRectF &_rect) QTextEdit can display a large HTML subset, including tables and images. - The text is set or replaced using setHtml() which deletes any + The text can be set or replaced using \l setHtml() which deletes any existing text and replaces it with the text passed in the setHtml() call. If you call setHtml() with legacy HTML, and then call toHtml(), the text that is returned may have different markup, but will render the same. The entire text can be deleted with clear(). + Text can also be set or replaced using \l setMarkdown(), and the same + caveats apply: if you then call \l toMarkdown(), the text that is returned + may be different, but the meaning is preserved as much as possible. + Markdown with some embedded HTML can be parsed, with the same limitations + that \l setHtml() has; but \l toMarkdown() only writes "pure" Markdown, + without any embedded HTML. + Text itself can be inserted using the QTextCursor class or using the convenience functions insertHtml(), insertPlainText(), append() or paste(). QTextCursor is also able to insert complex objects like tables @@ -1213,11 +1220,54 @@ QString QTextEdit::toHtml() const } #endif +#if QT_CONFIG(textmarkdownreader) && QT_CONFIG(textmarkdownwriter) +/*! + \property QTextEdit::markdown + + This property provides a Markdown interface to the text of the text edit. + + \c toMarkdown() returns the text of the text edit as "pure" Markdown, + without any embedded HTML formatting. Some features that QTextDocument + supports (such as the use of specific colors and named fonts) cannot be + expressed in "pure" Markdown, and they will be omitted. + + \c setMarkdown() changes the text of the text edit. Any previous text is + removed and the undo/redo history is cleared. The input text is + interpreted as rich text in Markdown format. + + Parsing of HTML included in the \a markdown string is handled in the same + way as in \l setHtml; however, Markdown formatting inside HTML blocks is + not supported. + + Some features of the parser can be enabled or disabled via the \a features + argument: + + \value MarkdownNoHTML + Any HTML tags in the Markdown text will be discarded + \value MarkdownDialectCommonMark + The parser supports only the features standardized by CommonMark + \value MarkdownDialectGitHub + The parser supports the GitHub dialect + + The default is \c MarkdownDialectGitHub. + + \sa plainText, html, QTextDocument::toMarkdown(), QTextDocument::setMarkdown() +*/ +#endif + #if QT_CONFIG(textmarkdownreader) -void QTextEdit::setMarkdown(const QString &text) +void QTextEdit::setMarkdown(const QString &markdown) +{ + Q_D(const QTextEdit); + d->control->setMarkdown(markdown); +} +#endif + +#if QT_CONFIG(textmarkdownwriter) +QString QTextEdit::toMarkdown(QTextDocument::MarkdownFeatures features) const { Q_D(const QTextEdit); - d->control->setMarkdown(text); + return d->control->toMarkdown(features); } #endif -- cgit v1.2.3