diff --git a/examples/widgets/richtext/textedit/example.html b/examples/widgets/richtext/textedit/example.html index e3a56d1154c..99090a697fa 100644 --- a/examples/widgets/richtext/textedit/example.html +++ b/examples/widgets/richtext/textedit/example.html @@ -1,14 +1,14 @@
QTextEdit
+The QTextEdit widget is an advanced editor that supports formatted rich text. It can be used to display HTML and other rich document formats. Internally, QTextEdit uses the QTextDocument class to describe both the high-level structure of each document and the low-level formatting of paragraphs.
If you are viewing this document in the textedit example, you can edit this document to explore Qt's rich text editing features. We have included some comments in each of the following sections to encourage you to experiment.
-Font and Paragraph Styles
+QTextEdit supports bold, italic, and underlined font styles, and can display multicolored text. Font families such as Times New Roman and Courier can also be used directly. If you place the cursor in a region of styled text, the controls in the tool bars will change to reflect the current style.
Paragraphs can be formatted so that the text is left-aligned, right-aligned, centered, or fully justified.
Try changing the alignment of some text and resize the editor to see how the text layout changes.
-Lists
+Different kinds of lists can be included in rich text documents. Standard bullet lists can be nested, using different symbols for each level of the list:
The list will automatically be renumbered if you add or remove items. Try adding new sections to the above list or removing existing item to see the numbers change.
-Images
+Inline images are treated like ordinary ranges of characters in the text editor, so they flow with the surrounding text. Images can also be selected in the same way as text, making it easy to cut, copy, and paste them.
Try to select this image by clicking and dragging over it with the mouse, or use the text cursor to select it by holding down Shift and using the arrow keys. You can then cut or copy it, and paste it into different parts of this document.
Tables
+QTextEdit can arrange and format tables, supporting features such as row and column spans, text formatting within cells, and size constraints for columns.
@@ -72,8 +72,8 @@ p, li { white-space: pre-wrap; }Try adding text to the cells in the table and experiment with the alignment of the paragraphs.
-Hyperlinks
+QTextEdit is designed to support hyperlinks between documents, and this feature is used extensively in Qt Assistant. Hyperlinks are automatically created when an HTML file is imported into an editor. Since the rich text framework supports hyperlinks natively, they can also be created programatically.
-Undo and Redo
+Full support for undo and redo operations is built into QTextEdit and the underlying rich text framework. Operations on a document can be packaged together to make editing a more comfortable experience for the user.
Try making changes to this document and press Ctrl+Z to undo them. You can always recover the original contents of the document.
diff --git a/examples/widgets/richtext/textedit/textedit.cpp b/examples/widgets/richtext/textedit/textedit.cpp index fe4ee4f4992..002574bcd7d 100644 --- a/examples/widgets/richtext/textedit/textedit.cpp +++ b/examples/widgets/richtext/textedit/textedit.cpp @@ -348,6 +348,12 @@ void TextEdit::setupTextActions() comboStyle->addItem("Ordered List (Alpha upper)"); comboStyle->addItem("Ordered List (Roman lower)"); comboStyle->addItem("Ordered List (Roman upper)"); + comboStyle->addItem("Heading 1"); + comboStyle->addItem("Heading 2"); + comboStyle->addItem("Heading 3"); + comboStyle->addItem("Heading 4"); + comboStyle->addItem("Heading 5"); + comboStyle->addItem("Heading 6"); connect(comboStyle, QOverload0 && headingLevel <= 6) + html += QLatin1String(""); else if (list) html += QLatin1String(""); - else - html += QLatin1String(""); + else { + int headingLevel = blockFormat.headingLevel(); + if (headingLevel > 0 && headingLevel <= 6) + html += QLatin1String(" '); + else + html += QLatin1String(""); + } if (list) { if (list->itemNumber(block) == list->count() - 1) { // last item? close list diff --git a/src/gui/text/qtextdocumentfragment.cpp b/src/gui/text/qtextdocumentfragment.cpp index ea37695f4e8..3ad49b3f88c 100644 --- a/src/gui/text/qtextdocumentfragment.cpp +++ b/src/gui/text/qtextdocumentfragment.cpp @@ -420,7 +420,7 @@ static QTextListFormat::Style nextListStyle(QTextListFormat::Style style) } QTextHtmlImporter::QTextHtmlImporter(QTextDocument *_doc, const QString &_html, ImportMode mode, const QTextDocument *resourceProvider) - : indent(0), compressNextWhitespace(PreserveWhiteSpace), doc(_doc), importMode(mode) + : indent(0), headingLevel(0), compressNextWhitespace(PreserveWhiteSpace), doc(_doc), importMode(mode) { cursor = QTextCursor(doc); wsm = QTextHtmlParserNode::WhiteSpaceNormal; @@ -747,8 +747,28 @@ QTextHtmlImporter::ProcessNodeResult QTextHtmlImporter::processSpecialNodes() return ContinueWithNextNode; } + case Html_h1: + headingLevel = 1; + break; + case Html_h2: + headingLevel = 2; + break; + case Html_h3: + headingLevel = 3; + break; + case Html_h4: + headingLevel = 4; + break; + case Html_h5: + headingLevel = 5; + break; + case Html_h6: + headingLevel = 6; + break; + default: break; } + return ContinueWithCurrentNode; } @@ -832,6 +852,15 @@ bool QTextHtmlImporter::closeTag() } } break; + case Html_h1: + case Html_h2: + case Html_h3: + case Html_h4: + case Html_h5: + case Html_h6: + headingLevel = 0; + blockTagClosed = true; + break; default: if (closedNode->isBlock()) blockTagClosed = true; @@ -1093,6 +1122,11 @@ QTextHtmlImporter::ProcessNodeResult QTextHtmlImporter::processBlockNode() modifiedBlockFormat = true; } + if (headingLevel) { + block.setHeadingLevel(headingLevel); + modifiedBlockFormat = true; + } + if (currentNode->blockFormat.propertyCount() > 0) { modifiedBlockFormat = true; block.merge(currentNode->blockFormat); diff --git a/src/gui/text/qtextdocumentfragment_p.h b/src/gui/text/qtextdocumentfragment_p.h index e8699545f71..02a6a429fa0 100644 --- a/src/gui/text/qtextdocumentfragment_p.h +++ b/src/gui/text/qtextdocumentfragment_p.h @@ -152,6 +152,7 @@ private: friend class QTypeInfo; QVector
lists; int indent; + int headingLevel; // insert a named anchor the next time we emit a char format, // either in a block or in regular text diff --git a/src/gui/text/qtextformat.h b/src/gui/text/qtextformat.h index 28c30355376..8f8d3d42995 100644 --- a/src/gui/text/qtextformat.h +++ b/src/gui/text/qtextformat.h @@ -175,6 +175,7 @@ public: LineHeightType = 0x1049, BlockNonBreakableLines = 0x1050, BlockTrailingHorizontalRulerWidth = 0x1060, + HeadingLevel = 0x1070, // character properties FirstFontProperty = 0x1FE0, @@ -624,6 +625,11 @@ public: inline int indent() const { return intProperty(BlockIndent); } + inline void setHeadingLevel(int alevel) + { setProperty(HeadingLevel, alevel); } + inline int headingLevel() const + { return intProperty(HeadingLevel); } + inline void setLineHeight(qreal height, int heightType) { setProperty(LineHeight, height); setProperty(LineHeightType, heightType); } inline qreal lineHeight(qreal scriptLineHeight, qreal scaling) const;