• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.4 API Reference
  • KDE Home
  • Contact Us
 

kpimtextedit/richtextbuilders

  • kpimtextedit
  • richtextbuilders
kplaintextmarkupbuilder.cpp
1 /*
2  This file is part of KDE.
3 
4  Copyright (c) 2008 Stephen Kelly <steveire@gmail.com>
5 
6  This library is free software; you can redistribute it and/or modify it
7  under the terms of the GNU Library General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or (at your
9  option) any later version.
10 
11  This library is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14  License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301, USA.
20 */
21 
22 #include "kplaintextmarkupbuilder.h"
23 
24 #include <KLocale>
25 
26 class KPlainTextMarkupBuilderPrivate
27 {
28 public:
29  KPlainTextMarkupBuilderPrivate(KPlainTextMarkupBuilder *b) : q_ptr(b) {
30 
31  }
32 
41  QString getLetterString(int itemNumber);
42 
47  QString getReferences();
48 
49  QStringList m_urls;
50  QList<QTextListFormat::Style> currentListItemStyles;
51  QList<int> currentListItemNumbers;
52 
53  QString activeLink;
54 
55  QString m_text;
56 
57  KPlainTextMarkupBuilder *q_ptr;
58 
59  Q_DECLARE_PUBLIC(KPlainTextMarkupBuilder)
60 
61 };
62 
63 QString KPlainTextMarkupBuilderPrivate::getLetterString(int itemNumber)
64 {
65  QString letterString;
66  while (true) {
67  // Create the letter string by prepending one char at a time.
68  // The itemNumber is converted to a number in the base 36 (number of letters in the
69  // alphabet plus 10) after being increased by 10 (to pass out the digits 0 to 9).
70  letterString.prepend(QString("%1").arg((itemNumber % LETTERSINALPHABET) + DIGITSOFFSET,
71  0, // no padding while building this string.
72  LETTERSINALPHABET + DIGITSOFFSET));
73  if ((itemNumber >= LETTERSINALPHABET)) {
74  itemNumber = itemNumber / LETTERSINALPHABET;
75  itemNumber--;
76  } else {
77  break;
78  }
79  }
80  return letterString;
81 }
82 
83 QString KPlainTextMarkupBuilderPrivate::getReferences()
84 {
85  QString refs;
86  if (!m_urls.isEmpty()) {
87  refs.append(i18nc("Beginning of the references section, which lists all external references",
88  "\n---- References ----\n"));
89 
90  int index = 1;
91  while (!m_urls.isEmpty()) {
92  refs.append(QString("[%1] %2\n").arg(index++).arg(m_urls.takeFirst()));
93  }
94  }
95  return refs;
96 }
97 
98 KPlainTextMarkupBuilder::KPlainTextMarkupBuilder() : d_ptr(new KPlainTextMarkupBuilderPrivate(this))
99 {
100  Q_D(KPlainTextMarkupBuilder);
101  d->m_urls = QStringList();
102 }
103 
104 void KPlainTextMarkupBuilder::beginStrong()
105 {
106  Q_D(KPlainTextMarkupBuilder);
107  d->m_text.append("*");
108 }
109 void KPlainTextMarkupBuilder::endStrong()
110 {
111  Q_D(KPlainTextMarkupBuilder);
112  d->m_text.append("*");
113 }
114 void KPlainTextMarkupBuilder::beginEmph()
115 {
116  Q_D(KPlainTextMarkupBuilder);
117  d->m_text.append("/");
118 }
119 void KPlainTextMarkupBuilder::endEmph()
120 {
121  Q_D(KPlainTextMarkupBuilder);
122  d->m_text.append("/");
123 }
124 void KPlainTextMarkupBuilder::beginUnderline()
125 {
126  Q_D(KPlainTextMarkupBuilder);
127  d->m_text.append("_");
128 }
129 void KPlainTextMarkupBuilder::endUnderline()
130 {
131  Q_D(KPlainTextMarkupBuilder);
132  d->m_text.append("_");
133 }
134 void KPlainTextMarkupBuilder::beginStrikeout()
135 {
136  Q_D(KPlainTextMarkupBuilder);
137  d->m_text.append("-");
138 }
139 void KPlainTextMarkupBuilder::endStrikeout()
140 {
141  Q_D(KPlainTextMarkupBuilder);
142  d->m_text.append("-");
143 }
144 
145 void KPlainTextMarkupBuilder::beginAnchor(const QString &href, const QString &name)
146 {
147  Q_D(KPlainTextMarkupBuilder);
148  Q_UNUSED(name);
149  if (!d->m_urls.contains(href)) {
150 
151  d->m_urls.append(href);
152  }
153  d->activeLink = href;
154 }
155 
156 void KPlainTextMarkupBuilder::endAnchor()
157 {
158  Q_D(KPlainTextMarkupBuilder);
159  d->m_text.append(QString("[%1]").arg(d->m_urls.indexOf(d->activeLink) + 1));
160 }
161 
162 void KPlainTextMarkupBuilder::endParagraph()
163 {
164  Q_D(KPlainTextMarkupBuilder);
165  d->m_text.append("\n");
166 }
167 
168 void KPlainTextMarkupBuilder::addNewline()
169 {
170  Q_D(KPlainTextMarkupBuilder);
171  d->m_text.append("\n");
172 }
173 
174 void KPlainTextMarkupBuilder::insertHorizontalRule(int width)
175 {
176  Q_UNUSED(width)
177  Q_D(KPlainTextMarkupBuilder);
178 
179  d->m_text.append("--------------------\n");
180 }
181 
182 void KPlainTextMarkupBuilder::insertImage(const QString &src, qreal width, qreal height)
183 {
184  Q_D(KPlainTextMarkupBuilder);
185  Q_UNUSED(width)
186  Q_UNUSED(height)
187 
188  if (!d->m_urls.contains(src)) {
189  d->m_urls.append(src);
190  }
191  d->m_text.append(QString("[%1]").arg(d->m_urls.indexOf(src) + 1));
192 }
193 
194 
195 void KPlainTextMarkupBuilder::beginList(QTextListFormat::Style style)
196 {
197  Q_D(KPlainTextMarkupBuilder);
198  d->currentListItemStyles.append(style);
199  d->currentListItemNumbers.append(0);
200 }
201 
202 void KPlainTextMarkupBuilder::endList()
203 {
204  Q_D(KPlainTextMarkupBuilder);
205  if (!d->currentListItemNumbers.isEmpty()) {
206  d->currentListItemStyles.removeLast();
207  d->currentListItemNumbers.removeLast();
208  }
209 }
210 void KPlainTextMarkupBuilder::beginListItem()
211 {
212  Q_D(KPlainTextMarkupBuilder);
213  for (int i = 0; i < d->currentListItemNumbers.size(); i++) {
214  d->m_text.append(" ");
215  }
216 
217  int itemNumber = d->currentListItemNumbers.last();
218  QString letterString;
219 
220  switch (d->currentListItemStyles.last()) {
221  case QTextListFormat::ListDisc:
222  d->m_text.append(" * ");
223  break;
224  case QTextListFormat::ListCircle:
225  d->m_text.append(" o ");
226  break;
227  case QTextListFormat::ListSquare:
228  d->m_text.append(" - ");
229  break;
230  case QTextListFormat::ListDecimal:
231  d->m_text.append(QString(" %1. ").arg(itemNumber + 1));
232  break;
233  case QTextListFormat::ListLowerAlpha:
234  d->m_text.append(QString(" %1. ").arg(d->getLetterString(itemNumber)));
235  break;
236  case QTextListFormat::ListUpperAlpha:
237  d->m_text.append(QString(" %1. ").arg(d->getLetterString(itemNumber).toUpper()));
238  break;
239  default:
240  break;
241  }
242 }
243 
244 void KPlainTextMarkupBuilder::endListItem()
245 {
246  Q_D(KPlainTextMarkupBuilder);
247  d->currentListItemNumbers.last() = d->currentListItemNumbers.last() + 1;
248 }
249 
250 
251 void KPlainTextMarkupBuilder::beginSuperscript()
252 {
253  Q_D(KPlainTextMarkupBuilder);
254  d->m_text.append("^{");
255 }
256 
257 void KPlainTextMarkupBuilder::endSuperscript()
258 {
259  Q_D(KPlainTextMarkupBuilder);
260  d->m_text.append("}");
261 }
262 
263 void KPlainTextMarkupBuilder::beginSubscript()
264 {
265  Q_D(KPlainTextMarkupBuilder);
266  d->m_text.append("_{");
267 }
268 
269 void KPlainTextMarkupBuilder::endSubscript()
270 {
271  Q_D(KPlainTextMarkupBuilder);
272  d->m_text.append("}");
273 }
274 
275 void KPlainTextMarkupBuilder::appendLiteralText(const QString &text)
276 {
277  Q_D(KPlainTextMarkupBuilder);
278  d->m_text.append(text);
279 }
280 
281 QString& KPlainTextMarkupBuilder::getResult()
282 {
283  Q_D(KPlainTextMarkupBuilder);
284  QString &ret = d->m_text;
285  ret.append(d->getReferences());
286  return ret;
287 }
288 
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Tue Dec 4 2012 14:35:27 by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kpimtextedit/richtextbuilders

Skip menu "kpimtextedit/richtextbuilders"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.9.4 API Reference

Skip menu "kdepimlibs-4.9.4 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal