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 
23 using namespace Cutelee;
24 
25 BBCodeBuilder::BBCodeBuilder() : m_currentAlignment(Qt::AlignLeft) {}
26 
27 BBCodeBuilder::~BBCodeBuilder() {}
28 
29 void BBCodeBuilder::beginStrong() { m_text.append(QStringLiteral("[B]")); }
30 void BBCodeBuilder::endStrong() { m_text.append(QStringLiteral("[/B]")); }
31 void BBCodeBuilder::beginEmph() { m_text.append(QStringLiteral("[I]")); }
32 void BBCodeBuilder::endEmph() { m_text.append(QStringLiteral("[/I]")); }
33 void BBCodeBuilder::beginUnderline() { m_text.append(QStringLiteral("[U]")); }
34 void BBCodeBuilder::endUnderline() { m_text.append(QStringLiteral("[/U]")); }
35 void BBCodeBuilder::beginStrikeout() { m_text.append(QStringLiteral("[S]")); }
36 void BBCodeBuilder::endStrikeout() { m_text.append(QStringLiteral("[/S]")); }
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 
48 void BBCodeBuilder::beginAnchor(const QString &href, const QString &name)
49 {
50  Q_UNUSED(name)
51  m_text.append(QStringLiteral("[URL=%1]").arg(href));
52 }
53 void 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 
66 void 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 
94 void BBCodeBuilder::addNewline() { m_text.append(QLatin1Char('\n')); }
95 
96 void 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 
104 {
105  switch (type) {
109  m_text.append(QStringLiteral(
110  "[LIST]\n")); // Unordered lists are all disc type in BBCode.
111  break;
113  m_text.append(QStringLiteral("[LIST=1]\n"));
114  break;
116  m_text.append(QStringLiteral("[LIST=a]\n"));
117  break;
119  m_text.append(QStringLiteral("[LIST=A]\n"));
120  break;
121  default:
122  break;
123  }
124 }
125 
126 void BBCodeBuilder::endList() { m_text.append(QStringLiteral("[/LIST]\n")); }
127 
128 void 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 
140 void BBCodeBuilder::beginSubscript() { m_text.append(QStringLiteral("[SUB]")); }
141 
142 void BBCodeBuilder::endSubscript() { m_text.append(QStringLiteral("[/SUB]")); }
143 
144 void 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 
159 const QString BBCodeBuilder::escape(const QString &s) const
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
QString & append(QChar ch)
void beginAnchor(const QString &href={}, const QString &name={}) override
void beginForeground(const QBrush &brush) override
void endUnderline() override
void endStrong() override
void beginList(QTextListFormat::Style type) override
QString name() const const
The Cutelee namespace holds all public Cutelee API.
Definition: Mainpage.dox:7
void endEmph() override
void beginListItem() override
void endAnchor() override
void beginTableRow() override
typedef Alignment
void beginStrikeout() override
void beginSuperscript() override
void clear()
void beginTable(qreal, qreal, const QString &) override
const QColor & color() const const
void endParagraph() override
const QString escape(const QString &text) const
QString number(int n, int base)
void beginUnderline() override
void beginSubscript() override
void endStrikeout() override
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
void endFontPointSize() override
void appendLiteralText(const QString &text) override
void beginEmph() override
QString getResult() override
void beginFontPointSize(int size) override
void addNewline() override
void endForeground() override
void endSuperscript() override
void insertImage(const QString &src, qreal width, qreal height) override
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 endList() override
void beginStrong() override