8#include "texthtmlbuilder.h"
13#include <QTextDocument>
17class TextHTMLBuilderPrivate
34using namespace KPIMTextEdit;
35TextHTMLBuilder::TextHTMLBuilder()
37 , d_ptr(new TextHTMLBuilderPrivate(this))
41TextHTMLBuilder::~TextHTMLBuilder()
49 d->mText.append(QStringLiteral(
"<strong>"));
55 d->mText.append(QStringLiteral(
"</strong>"));
61 d->mText.append(QStringLiteral(
"<em>"));
67 d->mText.append(QStringLiteral(
"</em>"));
73 d->mText.append(QStringLiteral(
"<u>"));
79 d->mText.append(QStringLiteral(
"</u>"));
85 d->mText.append(QStringLiteral(
"<s>"));
91 d->mText.append(QStringLiteral(
"</s>"));
97 d->mText.append(QStringLiteral(
"<span style=\"color:%1;\">").arg(brush.
color().
name()));
103 d->mText.append(QStringLiteral(
"</span>"));
109 d->mText.append(QStringLiteral(
"<span style=\"background-color:%1;\">").arg(brush.
color().
name()));
115 d->mText.append(QStringLiteral(
"</span>"));
123 d->mText.append(QStringLiteral(
"<a href=\"%1\" name=\"%2\">").arg(href, name));
125 d->mText.append(QStringLiteral(
"<a href=\"%1\">").arg(href));
129 d->mText.append(QStringLiteral(
"<a name=\"%1\">").arg(name));
137 d->mText.append(QStringLiteral(
"</a>"));
143 d->mText.append(QStringLiteral(
"<span style=\"font-family:%1;\">").arg(family));
149 d->mText.append(QStringLiteral(
"</span>"));
155 d->mText.append(QStringLiteral(
"<span style=\"font-size:%1pt;\">").arg(
QString::number(size)));
161 d->mText.append(QStringLiteral(
"</span>"));
171 styleString.
append(QStringLiteral(
"margin-top:%1;").arg(topMargin));
172 styleString.
append(QStringLiteral(
"margin-bottom:%1;").arg(bottomMargin));
173 styleString.
append(QStringLiteral(
"margin-left:%1;").arg(leftMargin));
174 styleString.
append(QStringLiteral(
"margin-right:%1;").arg(rightMargin));
181 d->mText.append(QStringLiteral(
"<p align=\"right\" "));
183 d->mText.append(QStringLiteral(
"<p align=\"center\" "));
185 d->mText.append(QStringLiteral(
"<p align=\"justify\" "));
187 d->mText.append(QStringLiteral(
"<p"));
189 d->mText.append(QStringLiteral(
"<p"));
193 d->mText.append(QStringLiteral(
" style=\"") + styleString +
QLatin1Char(
'"'));
195 if (leftToRightText) {
196 d->mText.append(QStringLiteral(
" dir='rtl'"));
207 d->mText.append(QStringLiteral(
"<h1>"));
210 d->mText.append(QStringLiteral(
"<h2>"));
213 d->mText.append(QStringLiteral(
"<h3>"));
216 d->mText.append(QStringLiteral(
"<h4>"));
219 d->mText.append(QStringLiteral(
"<h5>"));
222 d->mText.append(QStringLiteral(
"<h6>"));
234 d->mText.append(QStringLiteral(
"</h1>"));
237 d->mText.append(QStringLiteral(
"</h2>"));
240 d->mText.append(QStringLiteral(
"</h3>"));
243 d->mText.append(QStringLiteral(
"</h4>"));
246 d->mText.append(QStringLiteral(
"</h5>"));
249 d->mText.append(QStringLiteral(
"</h6>"));
259 d->mText.append(QStringLiteral(
"</p>\n"));
265 d->mText.append(QStringLiteral(
"<p> "));
272 d->mText.append(QStringLiteral(
"<hr width=\"%1\" />\n").arg(width));
274 d->mText.append(QStringLiteral(
"<hr />\n"));
280 d->mText.append(QStringLiteral(
"<img src=\"%1\" ").arg(src));
282 d->mText.append(QStringLiteral(
"width=\"%2\" ").arg(width));
285 d->mText.append(QStringLiteral(
"height=\"%2\" ").arg(height));
287 d->mText.append(QStringLiteral(
"/>"));
293 d->currentListItemStyles.append(type);
296 d->mText.append(QStringLiteral(
"<ul type=\"disc\">\n"));
299 d->mText.append(QStringLiteral(
"\n<ul type=\"circle\">\n"));
302 d->mText.append(QStringLiteral(
"\n<ul type=\"square\">\n"));
305 d->mText.append(QStringLiteral(
"\n<ol type=\"1\">\n"));
308 d->mText.append(QStringLiteral(
"\n<ol type=\"a\">\n"));
311 d->mText.append(QStringLiteral(
"\n<ol type=\"A\">\n"));
314 d->mText.append(QStringLiteral(
"\n<ol type=\"i\">\n"));
317 d->mText.append(QStringLiteral(
"\n<ol type=\"I\">\n"));
326 switch (d->currentListItemStyles.last()) {
330 d->mText.append(QStringLiteral(
"</ul>\n"));
337 d->mText.append(QStringLiteral(
"</ol>\n"));
342 d->currentListItemStyles.removeLast();
347 d->mText.append(QStringLiteral(
"<li>"));
353 d->mText.append(QStringLiteral(
"</li>\n"));
359 d->mText.append(QStringLiteral(
"<sup>"));
365 d->mText.append(QStringLiteral(
"</sup>"));
371 d->mText.append(QStringLiteral(
"<sub>"));
377 d->mText.append(QStringLiteral(
"</sub>"));
383 d->mText.append(QStringLiteral(
"<table cellpadding=\"%1\" cellspacing=\"%2\" "
384 "width=\"%3\" border=\"1\">")
393 d->mText.append(QStringLiteral(
"<tr>"));
399 d->mText.append(QStringLiteral(
"<th width=\"%1\" colspan=\"%2\" rowspan=\"%3\">").arg(width).arg(colspan).arg(rowspan));
405 d->mText.append(QStringLiteral(
"<td width=\"%1\" colspan=\"%2\" rowspan=\"%3\">").arg(width).arg(colspan).arg(rowspan));
411 d->mText.append(QStringLiteral(
"</table>"));
417 d->mText.append(QStringLiteral(
"</tr>"));
423 d->mText.append(QStringLiteral(
"</th>"));
429 d->mText.append(QStringLiteral(
"</td>"));
437 for (
int i = 0, total = textEscaped.
length(); i < total; ++i) {
438 const QChar c = textEscaped.
at(i);
442 textEscapedResult += QStringLiteral(
" ");
445 textEscapedResult += QStringLiteral(
" ");
447 textEscapedResult += c;
451 textEscapedResult += QStringLiteral(
" ");
453 textEscapedResult += c;
456 d->mText.
append(textEscapedResult);
462 d->mText.append(text);
473void KPIMTextEdit::TextHTMLBuilder::addSingleBreakLine()
476 d->mText.append(
"<br />"_L1);
Interface for creating marked-up text output.
The TextHTMLBuilder creates a clean html markup output.
void beginTableCell(const QString &width, int colspan, int rowspan) override
Begin a new table cell.
void beginEmph() override
Begin an emphasised element in the markup.
void beginTable(qreal cellpadding, qreal cellspacing, const QString &width) override
Begin a table element.
void appendRawText(const QString &text) override
Append text without escaping.
void beginSubscript() override
Begin a subscript element.
void beginSuperscript() override
Begin a superscript element.
void beginTableRow() override
Begin a new table row.
void endParagraph() override
Close the paragraph in the markup.
void endSuperscript() override
End superscript element.
QString getResult() override
Return the fully marked up result of the building process.
void beginAnchor(const QString &href={}, const QString &name={}) override
Begin a url anchor element in the markup.
void endStrong() override
Close the bold element in the markup.
void endList() override
Close the list.
void endAnchor() override
Close the anchor element.
void beginListItem() override
Begin a new list item in the markup.
void endTable() override
End a table element.
void beginForeground(const QBrush &brush) override
Begin a decorarated foreground element in the markup (A text color) using brush.
void endBackground() override
Close the decorarated background element in the markup.
void beginBackground(const QBrush &brush) override
Begin a decorarated background element in the markup (A text background color) using brush.
void appendLiteralText(const QString &text) override
Reimplemented from AbstractMarkupBuilder.
void beginStrikeout() override
Begin a struck out element in the markup.
void beginHeader(int level) override
Begin a new header element.
void endHeader(int level) override
End a header element.
void beginParagraph(Qt::Alignment al=Qt::AlignLeft, qreal topMargin=0.0, qreal bottomMargin=0.0, qreal leftMargin=0.0, qreal rightMargin=0.0, bool leftToRightText=false) override
Begin a new paragraph.
void endTableHeaderCell() override
End a table header cell.
void beginTableHeaderCell(const QString &width, int colspan, int rowspan) override
Begin a new table header cell.
void endFontFamily() override
End font family element.
void endStrikeout() override
Close the struck out element in the markup.
void endFontPointSize() override
End font point size element.
void insertHorizontalRule(int width=-1) override
Insert a horizontal rule into the markup.
void endTableCell() override
End a table cell.
void endForeground() override
Close the decorarated foreground element in the markup.
void insertImage(const QString &src, qreal width, qreal height) override
Insert a new image element into the markup.
void beginStrong() override
Begin a bold element in the markup.
void beginUnderline() override
Begin an underlined element in the markup.
void beginList(QTextListFormat::Style type) override
Begin a new list element in the markup.
void beginFontFamily(const QString &family) override
Begin a new font family element in the markup.
void endUnderline() override
Close the underlined element in the markup.
void beginFontPointSize(int size) override
Begin a new font point size.
void endTableRow() override
End a table row.
void endEmph() override
Close the emphasised element in the markup.
void addNewline() override
Add a newline to the markup.
void endSubscript() override
End subscript element.
void endListItem() override
End the list item.
const QColor & color() const const
QString name(NameFormat format) const const
QString & append(QChar ch)
const QChar at(qsizetype position) const const
bool isEmpty() const const
qsizetype length() const const
QString number(double n, char format, int precision)
QString toHtmlEscaped() const const