KPIMTextEdit::TextHTMLBuilder Class

class KPIMTextEdit::TextHTMLBuilder

The TextHTMLBuilder creates a clean html markup output. More...

Header: #include <KPIMTextEdit/TextHTMLBuilder>
CMake: find_package(KPim6TextEdit REQUIRED)
target_link_libraries(mytarget PRIVATE KPim6::TextEdit)
Inherits: KPIMTextEdit::AbstractMarkupBuilder

Public Functions

TextHTMLBuilder()
virtual ~TextHTMLBuilder() override

Reimplemented Public Functions

virtual void addNewline() override
virtual void addSingleBreakLine() override
virtual void appendLiteralText(const QString &text) override
virtual void appendRawText(const QString &text) override
virtual void beginAnchor(const QString &href = {}, const QString &name = {}) override
virtual void beginBackground(const QBrush &brush) override
virtual void beginEmph() override
virtual void beginFontFamily(const QString &family) override
virtual void beginFontPointSize(int size) override
virtual void beginForeground(const QBrush &brush) override
virtual void beginHeader(int level) override
virtual void beginList(QTextListFormat::Style type, int start) override
virtual void beginListItem(QTextBlockFormat::MarkerType type) override
virtual void beginParagraph(Qt::Alignment al = Qt::AlignLeft, qreal topMargin = 0.0, qreal bottomMargin = 0.0, qreal leftMargin = 0.0, qreal rightMargin = 0.0, bool rightToLeftText = false) override
virtual void beginStrikeout() override
virtual void beginStrong() override
virtual void beginSubscript() override
virtual void beginSuperscript() override
virtual void beginTable(const QTextTableFormat &format) override
virtual void beginTableCell(const QTextTableCellFormat &format, const QTextLength &width) override
virtual void beginTableHeaderCell(const QTextTableCellFormat &format, const QTextLength &width) override
virtual void beginTableRow() override
virtual void beginUnderline() override
virtual void endAnchor() override
virtual void endBackground() override
virtual void endEmph() override
virtual void endFontFamily() override
virtual void endFontPointSize() override
virtual void endForeground() override
virtual void endHeader(int level) override
virtual void endList() override
virtual void endListItem() override
virtual void endParagraph() override
virtual void endStrikeout() override
virtual void endStrong() override
virtual void endSubscript() override
virtual void endSuperscript() override
virtual void endTable() override
virtual void endTableCell() override
virtual void endTableHeaderCell() override
virtual void endTableRow() override
virtual void endUnderline() override
virtual QString getResult() override
virtual void insertHorizontalRule(const QTextLength &width = {}) override
virtual void insertImage(const QString &src, qreal width, qreal height) override

Detailed Description

This class creates html output which is as minimal as possible and restricted to the rich text features supported in %Qt. (https://doc.qt.io/qt-5/richtext-html-subset.html)

The output contains only the body content, not the head element or other metadata.

eg:

<p>
  This is some <strong>formatted content</strong> in a paragraph.
</p>

instead of the content produced by %Qt:

  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
"http://www.w3.org/TR/REC-html40/strict.dtd">
  <html><head><meta name="qrichtext" content="1" /><meta
http-equiv="Content-Type" content="text/html; charset=UTF-8" /><style
type="text/css">
  p, li { white-space: pre-wrap; }
  </style></head><body style=" font-family:'Sans Serif'; font-size:10pt;
font-weight:400; font-style:normal;">
  <p style=" margin-top:12px; margin-bottom:12px; margin-left:0px;
margin-right:0px; -qt-block-indent:0; text-indent:0px;">This is some <span
style=" font-weight:600;">formatted content</span> in a paragraph.
</p></body></html>

Such tags should be created separately. For example:

auto b = new TextHTMLBuilder();
auto md = new MarkupDirector(b);
md->constructContent();
QString cleanHtml(
  "<head>\n<title>%1</title>\n</head>\n<body>%2</body>\n</html>")
                  .arg(document.metaInformation(QTextDocument::DocumentTitle))
                  .arg(b->getOutput());
file.write(cleanHtml);

Font formatting information on elements is represented by individual span elements.

eg:

<span style"color:blue;">
  <span style="background-color:red;">
    Blue text on red background
  </span>
</span>

instead of

<span style="color:blue;background-color:red;">
  Blue text on red background
</span>

It my be possible to change this if necessary.

Author: Stephen Kelly <steveire@gmail.com>

Member Function Documentation

TextHTMLBuilder::TextHTMLBuilder()

Constructs a TextHTMLBuilder.

[override virtual noexcept] TextHTMLBuilder::~TextHTMLBuilder()

Destroys the TextHTMLBuilder.

[override virtual] void TextHTMLBuilder::addNewline()

Reimplements: AbstractMarkupBuilder::addNewline().

Adds a newline to the HTML output.

[override virtual] void TextHTMLBuilder::addSingleBreakLine()

Reimplements: AbstractMarkupBuilder::addSingleBreakLine().

Add a single line break (a br element) to the markup.

[override virtual] void TextHTMLBuilder::appendLiteralText(const QString &text)

Reimplements: AbstractMarkupBuilder::appendLiteralText(const QString &text).

Reimplemented from AbstractMarkupBuilder.

This implementation escapes the text before appending so that

A sample <b>bold</b> word.

becomes

A sample &lt;b&gt;bold&lt;/b&gt; word.

[override virtual] void TextHTMLBuilder::appendRawText(const QString &text)

Reimplements: AbstractMarkupBuilder::appendRawText(const QString &text).

Append text without escaping.

This is useful if extending MarkupDirector

[override virtual] void TextHTMLBuilder::beginAnchor(const QString &href = {}, const QString &name = {})

Reimplements: AbstractMarkupBuilder::beginAnchor(const QString &href, const QString &name).

Begins an anchor (link) HTML element with the given href and name.

[override virtual] void TextHTMLBuilder::beginBackground(const QBrush &brush)

Reimplements: AbstractMarkupBuilder::beginBackground(const QBrush &brush).

Begins a background color element with the given brush.

[override virtual] void TextHTMLBuilder::beginEmph()

Reimplements: AbstractMarkupBuilder::beginEmph().

Begins an emphasized HTML element.

[override virtual] void TextHTMLBuilder::beginFontFamily(const QString &family)

Reimplements: AbstractMarkupBuilder::beginFontFamily(const QString &family).

Begins a font family element with the given family.

[override virtual] void TextHTMLBuilder::beginFontPointSize(int size)

Reimplements: AbstractMarkupBuilder::beginFontPointSize(int size).

Begin a new font point size with the given size.

[override virtual] void TextHTMLBuilder::beginForeground(const QBrush &brush)

Reimplements: AbstractMarkupBuilder::beginForeground(const QBrush &brush).

Begins a foreground color element with the given brush.

[override virtual] void TextHTMLBuilder::beginHeader(int level)

Reimplements: AbstractMarkupBuilder::beginHeader(int level).

Begins a header element with the given level.

[override virtual] void TextHTMLBuilder::beginList(QTextListFormat::Style type, int start)

Reimplements: AbstractMarkupBuilder::beginList(QTextListFormat::Style style, int start).

Begins a list with the given type style, numbered from start.

[override virtual] void TextHTMLBuilder::beginListItem(QTextBlockFormat::MarkerType type)

Reimplements: AbstractMarkupBuilder::beginListItem(QTextBlockFormat::MarkerType type).

Begins a list item element of type type.

[override virtual] void TextHTMLBuilder::beginParagraph(Qt::Alignment al = Qt::AlignLeft, qreal topMargin = 0.0, qreal bottomMargin = 0.0, qreal leftMargin = 0.0, qreal rightMargin = 0.0, bool rightToLeftText = false)

Reimplements: AbstractMarkupBuilder::beginParagraph(Qt::Alignment a, qreal top, qreal bottom, qreal left, qreal right, bool rightToLeftText).

Begins a paragraph with the given alignment and margins. al The alignment of the paragraph topMargin The top margin bottomMargin The bottom margin leftMargin The left margin rightMargin The right margin rightToLeftText Whether the text direction of the paragraph is right-to-left

[override virtual] void TextHTMLBuilder::beginStrikeout()

Reimplements: AbstractMarkupBuilder::beginStrikeout().

Begins a strikeout HTML element.

[override virtual] void TextHTMLBuilder::beginStrong()

Reimplements: AbstractMarkupBuilder::beginStrong().

Begins a bold (strong) HTML element.

[override virtual] void TextHTMLBuilder::beginSubscript()

Reimplements: AbstractMarkupBuilder::beginSubscript().

Begins a subscript element.

[override virtual] void TextHTMLBuilder::beginSuperscript()

Reimplements: AbstractMarkupBuilder::beginSuperscript().

Begins a superscript element.

[override virtual] void TextHTMLBuilder::beginTable(const QTextTableFormat &format)

Reimplements: AbstractMarkupBuilder::beginTable(const QTextTableFormat &format).

Begins a table with the given format.

[override virtual] void TextHTMLBuilder::beginTableCell(const QTextTableCellFormat &format, const QTextLength &width)

Reimplements: AbstractMarkupBuilder::beginTableCell(const QTextTableCellFormat &format, const QTextLength &width).

Begins a table cell with the given format and column width.

[override virtual] void TextHTMLBuilder::beginTableHeaderCell(const QTextTableCellFormat &format, const QTextLength &width)

Reimplements: AbstractMarkupBuilder::beginTableHeaderCell(const QTextTableCellFormat &format, const QTextLength &width).

Begins a table header cell with the given format and column width.

[override virtual] void TextHTMLBuilder::beginTableRow()

Reimplements: AbstractMarkupBuilder::beginTableRow().

Begins a table row.

[override virtual] void TextHTMLBuilder::beginUnderline()

Reimplements: AbstractMarkupBuilder::beginUnderline().

Begins an underlined HTML element.

[override virtual] void TextHTMLBuilder::endAnchor()

Reimplements: AbstractMarkupBuilder::endAnchor().

Ends the anchor HTML element.

[override virtual] void TextHTMLBuilder::endBackground()

Reimplements: AbstractMarkupBuilder::endBackground().

Ends the background color element.

[override virtual] void TextHTMLBuilder::endEmph()

Reimplements: AbstractMarkupBuilder::endEmph().

Ends the emphasized HTML element.

[override virtual] void TextHTMLBuilder::endFontFamily()

Reimplements: AbstractMarkupBuilder::endFontFamily().

Ends the font family element.

[override virtual] void TextHTMLBuilder::endFontPointSize()

Reimplements: AbstractMarkupBuilder::endFontPointSize().

Ends the font point size element.

[override virtual] void TextHTMLBuilder::endForeground()

Reimplements: AbstractMarkupBuilder::endForeground().

Ends the foreground color element.

[override virtual] void TextHTMLBuilder::endHeader(int level)

Reimplements: AbstractMarkupBuilder::endHeader(int level).

Ends a header element with the given level.

[override virtual] void TextHTMLBuilder::endList()

Reimplements: AbstractMarkupBuilder::endList().

Ends the list element.

[override virtual] void TextHTMLBuilder::endListItem()

Reimplements: AbstractMarkupBuilder::endListItem().

Ends the list item element.

[override virtual] void TextHTMLBuilder::endParagraph()

Reimplements: AbstractMarkupBuilder::endParagraph().

Ends the paragraph element.

[override virtual] void TextHTMLBuilder::endStrikeout()

Reimplements: AbstractMarkupBuilder::endStrikeout().

Ends the strikeout HTML element.

[override virtual] void TextHTMLBuilder::endStrong()

Reimplements: AbstractMarkupBuilder::endStrong().

Ends the bold HTML element.

[override virtual] void TextHTMLBuilder::endSubscript()

Reimplements: AbstractMarkupBuilder::endSubscript().

Ends the subscript element.

[override virtual] void TextHTMLBuilder::endSuperscript()

Reimplements: AbstractMarkupBuilder::endSuperscript().

Ends the superscript element.

[override virtual] void TextHTMLBuilder::endTable()

Reimplements: AbstractMarkupBuilder::endTable().

Ends the table element.

[override virtual] void TextHTMLBuilder::endTableCell()

Reimplements: AbstractMarkupBuilder::endTableCell().

Ends the table cell element.

[override virtual] void TextHTMLBuilder::endTableHeaderCell()

Reimplements: AbstractMarkupBuilder::endTableHeaderCell().

Ends the table header cell element.

[override virtual] void TextHTMLBuilder::endTableRow()

Reimplements: AbstractMarkupBuilder::endTableRow().

Ends the table row element.

[override virtual] void TextHTMLBuilder::endUnderline()

Reimplements: AbstractMarkupBuilder::endUnderline().

Ends the underlined HTML element.

[override virtual] QString TextHTMLBuilder::getResult()

Reimplements: AbstractMarkupBuilder::getResult().

Returns the fully marked up HTML result.

[override virtual] void TextHTMLBuilder::insertHorizontalRule(const QTextLength &width = {})

Reimplements: AbstractMarkupBuilder::insertHorizontalRule(const QTextLength &width).

Inserts a horizontal rule with the given width.

[override virtual] void TextHTMLBuilder::insertImage(const QString &src, qreal width, qreal height)

Reimplements: AbstractMarkupBuilder::insertImage(const QString &url, qreal width, qreal height).

Inserts an image with the given src, width, and height.