Cutelee 6.1.0
bbcodebuilder.cpp
1/*
2 This file is part of the Cutelee template system.
3
4 Copyright (c) 2008 Stephen Kelly <steveire@gmail.com>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either version
9 2.1 of the Licence, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library. If not, see <http://www.gnu.org/licenses/>.
18
19*/
20
21#include "bbcodebuilder.h"
22
23using namespace Cutelee;
24
25BBCodeBuilder::BBCodeBuilder() : m_currentAlignment(Qt::AlignLeft) {}
26
27BBCodeBuilder::~BBCodeBuilder() {}
28
29void BBCodeBuilder::beginStrong() { m_text.append(QStringLiteral("[B]")); }
30void BBCodeBuilder::endStrong() { m_text.append(QStringLiteral("[/B]")); }
31void BBCodeBuilder::beginEmph() { m_text.append(QStringLiteral("[I]")); }
32void BBCodeBuilder::endEmph() { m_text.append(QStringLiteral("[/I]")); }
33void BBCodeBuilder::beginUnderline() { m_text.append(QStringLiteral("[U]")); }
34void BBCodeBuilder::endUnderline() { m_text.append(QStringLiteral("[/U]")); }
35void BBCodeBuilder::beginStrikeout() { m_text.append(QStringLiteral("[S]")); }
36void BBCodeBuilder::endStrikeout() { m_text.append(QStringLiteral("[/S]")); }
37void BBCodeBuilder::beginForeground(const QBrush &brush)
38{
39 m_text.append(QStringLiteral("[COLOR=%1]").arg(brush.color().name()));
40}
42{
43 m_text.append(QStringLiteral("[/COLOR]"));
44}
45
46// Background colour not supported by BBCode.
47
48void BBCodeBuilder::beginAnchor(const QString &href, const QString &name)
49{
50 Q_UNUSED(name)
51 m_text.append(QStringLiteral("[URL=%1]").arg(href));
52}
53void BBCodeBuilder::endAnchor() { m_text.append(QStringLiteral("[/URL]")); }
54
55// Font family not supported by BBCode.
56
58{
59 m_text.append(QStringLiteral("[SIZE=%1]").arg(QString::number(size)));
60}
62{
63 m_text.append(QStringLiteral("[/SIZE]"));
64}
65
66void BBCodeBuilder::beginParagraph(Qt::Alignment a, qreal top, qreal bottom,
67 qreal left, qreal right)
68{
69 Q_UNUSED(top);
70 Q_UNUSED(bottom);
71 Q_UNUSED(left);
72 Q_UNUSED(right);
73 if (a & Qt::AlignRight) {
74 m_text.append(QStringLiteral("\n[Right]"));
75 } else if (a & Qt::AlignHCenter) {
76 m_text.append(QStringLiteral("\n[CENTER]"));
77 }
78 // LEFT is also supported in BBCode, but ignored.
79 m_currentAlignment = a;
80}
81
83{
84 if (m_currentAlignment & Qt::AlignRight) {
85 m_text.append(QStringLiteral("\n[/Right]\n"));
86 } else if (m_currentAlignment & Qt::AlignHCenter) {
87 m_text.append(QStringLiteral("\n[/CENTER]\n"));
88 } else {
89 m_text.append(QLatin1Char('\n'));
90 }
91 m_currentAlignment = Qt::AlignLeft;
92}
93
94void BBCodeBuilder::addNewline() { m_text.append(QLatin1Char('\n')); }
95
96void BBCodeBuilder::insertImage(const QString &src, qreal width, qreal height)
97{
98 Q_UNUSED(width);
99 Q_UNUSED(height);
100 m_text.append(QStringLiteral("[IMG]%1[/IMG]").arg(src));
101}
102
103void BBCodeBuilder::beginList(QTextListFormat::Style type)
104{
105 switch (type) {
106 case QTextListFormat::ListDisc:
107 case QTextListFormat::ListCircle:
108 case QTextListFormat::ListSquare:
109 m_text.append(QStringLiteral(
110 "[LIST]\n")); // Unordered lists are all disc type in BBCode.
111 break;
112 case QTextListFormat::ListDecimal:
113 m_text.append(QStringLiteral("[LIST=1]\n"));
114 break;
115 case QTextListFormat::ListLowerAlpha:
116 m_text.append(QStringLiteral("[LIST=a]\n"));
117 break;
118 case QTextListFormat::ListUpperAlpha:
119 m_text.append(QStringLiteral("[LIST=A]\n"));
120 break;
121 default:
122 break;
123 }
124}
125
126void BBCodeBuilder::endList() { m_text.append(QStringLiteral("[/LIST]\n")); }
127
128void BBCodeBuilder::beginListItem() { m_text.append(QStringLiteral("[*] ")); }
129
131{
132 m_text.append(QStringLiteral("[SUP]"));
133}
134
136{
137 m_text.append(QStringLiteral("[/SUP]"));
138}
139
140void BBCodeBuilder::beginSubscript() { m_text.append(QStringLiteral("[SUB]")); }
141
142void BBCodeBuilder::endSubscript() { m_text.append(QStringLiteral("[/SUB]")); }
143
144void BBCodeBuilder::beginTable(qreal, qreal, const QString &)
145{
146 m_text.append(QStringLiteral("[TABLE]\n"));
147}
148
150{
151 m_text.append(QStringLiteral("[/TABLE]"));
152}
153
155{
156 m_text.append(escape(text));
157}
158
160{
161 if (s.contains(QLatin1Char('['))) {
162 return QStringLiteral("[NOPARSE]") + s + QStringLiteral("[/NOPARSE]");
163 }
164 return s;
165}
166
168{
169 auto ret = m_text;
170 m_text.clear();
171 return ret;
172}
void endSubscript() override
void endFontPointSize() override
void endParagraph() override
void beginSubscript() override
void beginForeground(const QBrush &brush) override
void beginList(QTextListFormat::Style type) override
const QString escape(const QString &text) const
void beginParagraph(Qt::Alignment a=Qt::AlignLeft, qreal top=0.0, qreal bottom=0.0, qreal left=0.0, qreal right=0.0) override
void beginFontPointSize(int size) override
void beginTableRow() override
void beginSuperscript() override
void beginStrikeout() override
void endUnderline() override
void insertImage(const QString &src, qreal width, qreal height) override
void addNewline() override
void endEmph() override
void endStrong() override
void beginStrong() override
QString getResult() override
void endForeground() override
void endStrikeout() override
void beginTable(qreal, qreal, const QString &) override
void beginAnchor(const QString &href={}, const QString &name={}) override
void endSuperscript() override
void endAnchor() override
void appendLiteralText(const QString &text) override
void beginListItem() override
void beginUnderline() override
void beginEmph() override
The Cutelee namespace holds all public Cutelee API.
Definition Mainpage.dox:8