qtextodfwriter.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/gui/text/qtextodfwriter.cpp
Switch to Source codePreprocessed file
LineSourceCount
1-
2-
3-
4-
5-
6-
7-
8-
9-
10-
11-
12static QString pixelToPoint(qreal pixels)-
13{-
14-
15 return QString::number(pixels * 72 / 96) + QString::fromLatin1("pt");-
16}-
17-
18-
19class QOutputStrategy {-
20public:-
21 QOutputStrategy() : contentStream(0), counter(1) { }-
22 virtual ~QOutputStrategy() {}-
23 virtual void addFile(const QString &fileName, const QString &mimeType, const QByteArray &bytes) = 0;-
24-
25 QString createUniqueImageName()-
26 {-
27 return QString::fromLatin1("Pictures/Picture%1").arg(counter++);-
28 }-
29-
30 QIODevice *contentStream;-
31 int counter;-
32};-
33-
34class QXmlStreamStrategy : public QOutputStrategy {-
35public:-
36 QXmlStreamStrategy(QIODevice *device)-
37 {-
38 contentStream = device;-
39 }-
40-
41 virtual ~QXmlStreamStrategy()-
42 {-
43 if (contentStream)-
44 contentStream->close();-
45 }-
46 virtual void addFile(const QString &, const QString &, const QByteArray &) override-
47 {-
48-
49 }-
50};-
51-
52class QZipStreamStrategy : public QOutputStrategy {-
53public:-
54 QZipStreamStrategy(QIODevice *device)-
55 : zip(device),-
56 manifestWriter(&manifest)-
57 {-
58 QByteArray mime("application/vnd.oasis.opendocument.text");-
59 zip.setCompressionPolicy(QZipWriter::NeverCompress);-
60 zip.addFile(QString::fromLatin1("mimetype"), mime);-
61 zip.setCompressionPolicy(QZipWriter::AutoCompress);-
62 contentStream = &content;-
63 content.open(QIODevice::WriteOnly);-
64 manifest.open(QIODevice::WriteOnly);-
65-
66 manifestNS = QString::fromLatin1("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0");-
67-
68 manifestWriter.setAutoFormatting(true);-
69 manifestWriter.setAutoFormattingIndent(1);-
70-
71 manifestWriter.writeNamespace(manifestNS, QString::fromLatin1("manifest"));-
72 manifestWriter.writeStartDocument();-
73 manifestWriter.writeStartElement(manifestNS, QString::fromLatin1("manifest"));-
74 manifestWriter.writeAttribute(manifestNS, QString::fromLatin1("version"), QString::fromLatin1("1.2"));-
75 addFile(QString::fromLatin1("/"), QString::fromLatin1("application/vnd.oasis.opendocument.text"));-
76 addFile(QString::fromLatin1("content.xml"), QString::fromLatin1("text/xml"));-
77 }-
78-
79 ~QZipStreamStrategy()-
80 {-
81 manifestWriter.writeEndDocument();-
82 manifest.close();-
83 zip.addFile(QString::fromLatin1("META-INF/manifest.xml"), &manifest);-
84 content.close();-
85 zip.addFile(QString::fromLatin1("content.xml"), &content);-
86 zip.close();-
87 }-
88-
89 virtual void addFile(const QString &fileName, const QString &mimeType, const QByteArray &bytes) override-
90 {-
91 zip.addFile(fileName, bytes);-
92 addFile(fileName, mimeType);-
93 }-
94-
95private:-
96 void addFile(const QString &fileName, const QString &mimeType)-
97 {-
98 manifestWriter.writeEmptyElement(manifestNS, QString::fromLatin1("file-entry"));-
99 manifestWriter.writeAttribute(manifestNS, QString::fromLatin1("media-type"), mimeType);-
100 manifestWriter.writeAttribute(manifestNS, QString::fromLatin1("full-path"), fileName);-
101 }-
102-
103 QBuffer content;-
104 QBuffer manifest;-
105 QZipWriter zip;-
106 QXmlStreamWriter manifestWriter;-
107 QString manifestNS;-
108};-
109-
110static QString bulletChar(QTextListFormat::Style style)-
111{-
112 switch(style) {-
113 case QTextListFormat::ListDisc:-
114 return QChar(0x25cf);-
115 case QTextListFormat::ListCircle:-
116 return QChar(0x25cb);-
117 case QTextListFormat::ListSquare:-
118 return QChar(0x25a1);-
119 case QTextListFormat::ListDecimal:-
120 return QString::fromLatin1("1");-
121 case QTextListFormat::ListLowerAlpha:-
122 return QString::fromLatin1("a");-
123 case QTextListFormat::ListUpperAlpha:-
124 return QString::fromLatin1("A");-
125 case QTextListFormat::ListLowerRoman:-
126 return QString::fromLatin1("i");-
127 case QTextListFormat::ListUpperRoman:-
128 return QString::fromLatin1("I");-
129 default:-
130 case QTextListFormat::ListStyleUndefined:-
131 return QString();-
132 }-
133}-
134-
135void QTextOdfWriter::writeFrame(QXmlStreamWriter &writer, const QTextFrame *frame)-
136{-
137 ((!(frame)) ? qt_assert("frame",__FILE__,182188) : qt_noop());-
138 const QTextTable *table = qobject_cast<const QTextTable*> (frame);-
139-
140 if (table) {-
141 writer.writeStartElement(tableNS, QString::fromLatin1("table"));-
142 writer.writeEmptyElement(tableNS, QString::fromLatin1("table-column"));-
143 writer.writeAttribute(tableNS, QString::fromLatin1("number-columns-repeated"), QString::number(table->columns()));-
144 } else if (frame->document() && frame->document()->rootFrame() != frame) {-
145 writer.writeStartElement(textNS, QString::fromLatin1("section"));-
146 }-
147-
148 QTextFrame::iterator iterator = frame->begin();-
149 QTextFrame *child = 0;-
150-
151 int tableRow = -1;-
152 while (! iterator.atEnd()) {-
153 if (iterator.currentFrame() && child != iterator.currentFrame())-
154 writeFrame(writer, iterator.currentFrame());-
155 else {-
156 QTextBlock block = iterator.currentBlock();-
157 if (table) {-
158 QTextTableCell cell = table->cellAt(block.position());-
159 if (tableRow < cell.row()) {-
160 if (tableRow >= 0)-
161 writer.writeEndElement();-
162 tableRow = cell.row();-
163 writer.writeStartElement(tableNS, QString::fromLatin1("table-row"));-
164 }-
165 writer.writeStartElement(tableNS, QString::fromLatin1("table-cell"));-
166 if (cell.columnSpan() > 1)-
167 writer.writeAttribute(tableNS, QString::fromLatin1("number-columns-spanned"), QString::number(cell.columnSpan()));-
168 if (cell.rowSpan() > 1)-
169 writer.writeAttribute(tableNS, QString::fromLatin1("number-rows-spanned"), QString::number(cell.rowSpan()));-
170 if (cell.format().isTableCellFormat()) {-
171 writer.writeAttribute(tableNS, QString::fromLatin1("style-name"), QString::fromLatin1("T%1").arg(cell.tableCellFormatIndex()));-
172 }-
173 }-
174 writeBlock(writer, block);-
175 if (table)-
176 writer.writeEndElement();-
177 }-
178 child = iterator.currentFrame();-
179 ++iterator;-
180 }-
181 if (tableRow >= 0)-
182 writer.writeEndElement();-
183-
184 if (table || (frame->document() && frame->document()->rootFrame() != frame))-
185 writer.writeEndElement();-
186}-
187-
188void QTextOdfWriter::writeBlock(QXmlStreamWriter &writer, const QTextBlock &block)-
189{-
190 if (block.textList()) {-
191 const int listLevel = block.textList()->format().indent();-
192 if (m_listStack.isEmpty() || m_listStack.top() != block.textList()) {-
193-
194 while (m_listStack.count() >= listLevel && !m_listStack.isEmpty() && m_listStack.top() != block.textList() ) {-
195 m_listStack.pop();-
196 writer.writeEndElement();-
197 if (m_listStack.count())-
198 writer.writeEndElement();-
199 }-
200 while (m_listStack.count() < listLevel) {-
201 if (m_listStack.count())-
202 writer.writeStartElement(textNS, QString::fromLatin1("list-item"));-
203 writer.writeStartElement(textNS, QString::fromLatin1("list"));-
204 if (m_listStack.count() == listLevel - 1) {-
205 m_listStack.push(block.textList());-
206 writer.writeAttribute(textNS, QString::fromLatin1("style-name"), QString::fromLatin1("L%1")-
207 .arg(block.textList()->formatIndex()));-
208 }-
209 else {-
210 m_listStack.push(0);-
211 }-
212 }-
213 }-
214 writer.writeStartElement(textNS, QString::fromLatin1("list-item"));-
215 }-
216 else {-
217 while (! m_listStack.isEmpty()) {-
218 m_listStack.pop();-
219 writer.writeEndElement();-
220 if (m_listStack.count())-
221 writer.writeEndElement();-
222 }-
223 }-
224-
225 if (block.length() == 1) {-
226 writer.writeEmptyElement(textNS, QString::fromLatin1("p"));-
227 writer.writeAttribute(textNS, QString::fromLatin1("style-name"), QString::fromLatin1("p%1")-
228 .arg(block.blockFormatIndex()));-
229 if (block.textList())-
230 writer.writeEndElement();-
231 return;-
232 }-
233 writer.writeStartElement(textNS, QString::fromLatin1("p"));-
234 writer.writeAttribute(textNS, QString::fromLatin1("style-name"), QString::fromLatin1("p%1")-
235 .arg(block.blockFormatIndex()));-
236 for (QTextBlock::Iterator frag = block.begin(); !frag.atEnd(); ++frag) {-
237 bool isHyperlink = frag.fragment().charFormat().hasProperty(QTextFormat::AnchorHref);-
238 if (isHyperlink) {-
239 QString value = frag.fragment().charFormat().property(QTextFormat::AnchorHref).toString();-
240 writer.writeStartElement(textNS, QString::fromLatin1("a"));-
241 writer.writeAttribute(xlinkNS, QString::fromLatin1("href"), value);-
242 }-
243 writer.writeCharacters(QString());-
244 writer.writeStartElement(textNS, QString::fromLatin1("span"));-
245-
246 QString fragmentText = frag.fragment().text();-
247 if (fragmentText.length() == 1 && fragmentText[0] == 0xFFFC) {-
248 writeInlineCharacter(writer, frag.fragment());-
249 writer.writeEndElement();-
250 continue;-
251 }-
252-
253 writer.writeAttribute(textNS, QString::fromLatin1("style-name"), QString::fromLatin1("c%1")-
254 .arg(frag.fragment().charFormatIndex()));-
255 bool escapeNextSpace = true;-
256 int precedingSpaces = 0;-
257 int exportedIndex = 0;-
258 for (int i=0; i <= fragmentText.count(); ++i) {-
259 QChar character = fragmentText[i];-
260 bool isSpace = character.unicode() == ' ';-
261-
262-
263 if (!isSpace && escapeNextSpace && precedingSpaces > 1) {-
264 const bool startParag = exportedIndex == 0 && i == precedingSpaces;-
265 if (!startParag)-
266 writer.writeCharacters(fragmentText.mid(exportedIndex, i - precedingSpaces + 1 - exportedIndex));-
267 writer.writeEmptyElement(textNS, QString::fromLatin1("s"));-
268 const int count = precedingSpaces - (startParag?0:1);-
269 if (count > 1)-
270 writer.writeAttribute(textNS, QString::fromLatin1("c"), QString::number(count));-
271 precedingSpaces = 0;-
272 exportedIndex = i;-
273 }-
274-
275 if (i < fragmentText.count()) {-
276 if (character.unicode() == 0x2028) {-
277-
278 writer.writeCharacters(fragmentText.mid(exportedIndex, i - exportedIndex));-
279 writer.writeEmptyElement(textNS, QString::fromLatin1("line-break"));-
280 exportedIndex = i+1;-
281 continue;-
282 } else if (character.unicode() == '\t') {-
283-
284 writer.writeCharacters(fragmentText.mid(exportedIndex, i - exportedIndex));-
285 writer.writeEmptyElement(textNS, QString::fromLatin1("tab"));-
286 exportedIndex = i+1;-
287 precedingSpaces = 0;-
288 } else if (isSpace) {-
289 ++precedingSpaces;-
290 escapeNextSpace = true;-
291 } else if (!isSpace) {-
292 precedingSpaces = 0;-
293 }-
294 }-
295 }-
296-
297 writer.writeCharacters(fragmentText.mid(exportedIndex));-
298 writer.writeEndElement();-
299 writer.writeCharacters(QString());-
300 if (isHyperlink)-
301 writer.writeEndElement();-
302 }-
303 writer.writeCharacters(QString());-
304 writer.writeEndElement();-
305 if (block.textList())-
306 writer.writeEndElement();-
307}-
308-
309void QTextOdfWriter::writeInlineCharacter(QXmlStreamWriter &writer, const QTextFragment &fragment) const-
310{-
311 writer.writeStartElement(drawNS, QString::fromLatin1("frame"));-
312 if (m_strategy == 0) {-
313-
314 }-
315 else if (fragment.charFormat().isImageFormat()) {-
316 QTextImageFormat imageFormat = fragment.charFormat().toImageFormat();-
317 writer.writeAttribute(drawNS, QString::fromLatin1("name"), imageFormat.name());-
318-
319-
320 QImage image;-
321 QString name = imageFormat.name();-
322 if (name.startsWith(QLatin1String(":/")))-
323 name.prepend(QLatin1String("qrc"));-
324 QUrl url = QUrl(name);-
325 const QVariant data = m_document->resource(QTextDocument::ImageResource, url);-
326 if (data.type() == QVariant::Image) {-
327 image = qvariant_cast<QImage>(data);-
328 } else if (data.type() == QVariant::ByteArray) {-
329 image.loadFromData(data.toByteArray());-
330 }-
331-
332 if (image.isNull()) {-
333 QString context;-
334 if (image.isNull()) {-
335 name = imageFormat.name();-
336 image.load(name);-
337 }-
338 }-
339-
340-
341 if (! image.isNull()) {-
342 QBuffer imageBytes;-
343 QImageWriter imageWriter(&imageBytes, "png");-
344 imageWriter.write(image);-
345 QString filename = m_strategy->createUniqueImageName();-
346 m_strategy->addFile(filename, QString::fromLatin1("image/png"), imageBytes.data());-
347-
348-
349 qreal width = (imageFormat.hasProperty(QTextFormat::ImageWidth)) ? imageFormat.width() : image.width();-
350 writer.writeAttribute(svgNS, QString::fromLatin1("width"), pixelToPoint(width));-
351 qreal height = (imageFormat.hasProperty(QTextFormat::ImageHeight)) ? imageFormat.height() : image.height();-
352 writer.writeAttribute(svgNS, QString::fromLatin1("height"), pixelToPoint(height));-
353-
354 writer.writeStartElement(drawNS, QString::fromLatin1("image"));-
355 writer.writeAttribute(xlinkNS, QString::fromLatin1("href"), filename);-
356 writer.writeEndElement();-
357 }-
358 }-
359-
360 writer.writeEndElement();-
361}-
362-
363void QTextOdfWriter::writeFormats(QXmlStreamWriter &writer, const QSet<int> &formats) const-
364{-
365 writer.writeStartElement(officeNS, QString::fromLatin1("automatic-styles"));-
366 QVector<QTextFormat> allStyles = m_document->allFormats();-
367 QSetIterator<int> formatId(formats);-
368 while(formatId.hasNext()) {-
369 int formatIndex = formatId.next();-
370 QTextFormat textFormat = allStyles.at(formatIndex);-
371 switch (textFormat.type()) {-
372 case QTextFormat::CharFormat:-
373 if (textFormat.isTableCellFormat())-
374 writeTableCellFormat(writer, textFormat.toTableCellFormat(), formatIndex);-
375 else-
376 writeCharacterFormat(writer, textFormat.toCharFormat(), formatIndex);-
377 break;-
378 case QTextFormat::BlockFormat:-
379 writeBlockFormat(writer, textFormat.toBlockFormat(), formatIndex);-
380 break;-
381 case QTextFormat::ListFormat:-
382 writeListFormat(writer, textFormat.toListFormat(), formatIndex);-
383 break;-
384 case QTextFormat::FrameFormat:-
385 writeFrameFormat(writer, textFormat.toFrameFormat(), formatIndex);-
386 break;-
387 case QTextFormat::TableFormat:-
388 ;break;-
389 }-
390 }-
391-
392 writer.writeEndElement();-
393}-
394-
395void QTextOdfWriter::writeBlockFormat(QXmlStreamWriter &writer, QTextBlockFormat format, int formatIndex) const-
396{-
397 writer.writeStartElement(styleNS, QString::fromLatin1("style"));-
398 writer.writeAttribute(styleNS, QString::fromLatin1("name"), QString::fromLatin1("p%1").arg(formatIndex));-
399 writer.writeAttribute(styleNS, QString::fromLatin1("family"), QString::fromLatin1("paragraph"));-
400 writer.writeStartElement(styleNS, QString::fromLatin1("paragraph-properties"));-
401-
402 if (format.hasProperty(QTextFormat::BlockAlignment)) {-
403 const Qt::Alignment alignment = format.alignment() & Qt::AlignHorizontal_Mask;-
404 QString value;-
405 if (alignment == Qt::AlignLeading)-
406 value = QString::fromLatin1("start");-
407 else if (alignment == Qt::AlignTrailing)-
408 value = QString::fromLatin1("end");-
409 else if (alignment == (Qt::AlignLeft | Qt::AlignAbsolute))-
410 value = QString::fromLatin1("left");-
411 else if (alignment == (Qt::AlignRight | Qt::AlignAbsolute))-
412 value = QString::fromLatin1("right");-
413 else if (alignment == Qt::AlignHCenter)-
414 value = QString::fromLatin1("center");-
415 else if (alignment == Qt::AlignJustify)-
416 value = QString::fromLatin1("justify");-
417 else-
418 QMessageLogger(__FILE__, 463469, __PRETTY_FUNCTION__).warning() << "QTextOdfWriter: unsupported paragraph alignment; " << format.alignment();-
419 if (! value.isNull())-
420 writer.writeAttribute(foNS, QString::fromLatin1("text-align"), value);-
421 }-
422-
423 if (format.hasProperty(QTextFormat::BlockTopMargin))-
424 writer.writeAttribute(foNS, QString::fromLatin1("margin-top"), pixelToPoint(qMax(qreal(0.), format.topMargin())) );-
425 if (format.hasProperty(QTextFormat::BlockBottomMargin))-
426 writer.writeAttribute(foNS, QString::fromLatin1("margin-bottom"), pixelToPoint(qMax(qreal(0.), format.bottomMargin())) );-
427 if (format.hasProperty(QTextFormat::BlockLeftMargin) || format.hasProperty(QTextFormat::BlockIndent))-
428 writer.writeAttribute(foNS, QString::fromLatin1("margin-left"), pixelToPoint(qMax(qreal(0.),-
429 format.leftMargin() + format.indent())));-
430 if (format.hasProperty(QTextFormat::BlockRightMargin))-
431 writer.writeAttribute(foNS, QString::fromLatin1("margin-right"), pixelToPoint(qMax(qreal(0.), format.rightMargin())) );-
432 if (format.hasProperty(QTextFormat::TextIndent))-
433 writer.writeAttribute(foNS, QString::fromLatin1("text-indent"), pixelToPoint(format.textIndent()));-
434 if (format.hasProperty(QTextFormat::PageBreakPolicy)) {-
435 if (format.pageBreakPolicy() & QTextFormat::PageBreak_AlwaysBefore)-
436 writer.writeAttribute(foNS, QString::fromLatin1("break-before"), QString::fromLatin1("page"));-
437 if (format.pageBreakPolicy() & QTextFormat::PageBreak_AlwaysAfter)-
438 writer.writeAttribute(foNS, QString::fromLatin1("break-after"), QString::fromLatin1("page"));-
439 }-
440 if (format.hasProperty(QTextFormat::BackgroundBrush)) {-
441 QBrush brush = format.background();-
442 writer.writeAttribute(foNS, QString::fromLatin1("background-color"), brush.color().name());-
443 }-
444 if (format.hasProperty(QTextFormat::BlockNonBreakableLines))-
445 writer.writeAttribute(foNS, QString::fromLatin1("keep-together"),-
446 format.nonBreakableLines() ? QString::fromLatin1("true") : QString::fromLatin1("false"));-
447 if (format.hasProperty(QTextFormat::TabPositions)) {-
448 QList<QTextOption::Tab> tabs = format.tabPositions();-
449 writer.writeStartElement(styleNS, QString::fromLatin1("tab-stops"));-
450 QList<QTextOption::Tab>::Iterator iterator = tabs.begin();-
451 while(iterator != tabs.end()) {-
452 writer.writeEmptyElement(styleNS, QString::fromLatin1("tab-stop"));-
453 writer.writeAttribute(styleNS, QString::fromLatin1("position"), pixelToPoint(iterator->position) );-
454 QString type;-
455 switch(iterator->type) {-
456 case QTextOption::DelimiterTab: type = QString::fromLatin1("char"); break;-
457 case QTextOption::LeftTab: type = QString::fromLatin1("left"); break;-
458 case QTextOption::RightTab: type = QString::fromLatin1("right"); break;-
459 case QTextOption::CenterTab: type = QString::fromLatin1("center"); break;-
460 }-
461 writer.writeAttribute(styleNS, QString::fromLatin1("type"), type);-
462 if (iterator->delimiter != 0)-
463 writer.writeAttribute(styleNS, QString::fromLatin1("char"), iterator->delimiter);-
464 ++iterator;-
465 }-
466-
467 writer.writeEndElement();-
468 }-
469-
470 writer.writeEndElement();-
471 writer.writeEndElement();-
472}-
473-
474void QTextOdfWriter::writeCharacterFormat(QXmlStreamWriter &writer, QTextCharFormat format, int formatIndex) const-
475{-
476 writer.writeStartElement(styleNS, QString::fromLatin1("style"));-
477 writer.writeAttribute(styleNS, QString::fromLatin1("name"), QString::fromLatin1("c%1").arg(formatIndex));-
478 writer.writeAttribute(styleNS, QString::fromLatin1("family"), QString::fromLatin1("text"));-
479 writer.writeEmptyElement(styleNS, QString::fromLatin1("text-properties"));-
480 if (format.fontItalic())-
481 writer.writeAttribute(foNS, QString::fromLatin1("font-style"), QString::fromLatin1("italic"));-
482 if (format.hasProperty(QTextFormat::FontWeight) && format.fontWeight() != QFont::Normal) {-
483 QString value;-
484 if (format.fontWeight() == QFont::Bold)-
485 value = QString::fromLatin1("bold");-
486 else-
487 value = QString::number(format.fontWeight() * 10);-
488 writer.writeAttribute(foNS, QString::fromLatin1("font-weight"), value);-
489 }-
490 if (format.hasProperty(QTextFormat::FontFamily))-
491 writer.writeAttribute(foNS, QString::fromLatin1("font-family"), format.fontFamily());-
492 else-
493 writer.writeAttribute(foNS, QString::fromLatin1("font-family"), QString::fromLatin1("Sans"));-
494 if (format.hasProperty(QTextFormat::FontPointSize))-
495 writer.writeAttribute(foNS, QString::fromLatin1("font-size"), QString::fromLatin1("%1pt").arg(format.fontPointSize()));-
496 if (format.hasProperty(QTextFormat::FontCapitalization)) {-
497 switch(format.fontCapitalization()) {-
498 case QFont::MixedCase:-
499 writer.writeAttribute(foNS, QString::fromLatin1("text-transform"), QString::fromLatin1("none")); break;-
500 case QFont::AllUppercase:-
501 writer.writeAttribute(foNS, QString::fromLatin1("text-transform"), QString::fromLatin1("uppercase")); break;-
502 case QFont::AllLowercase:-
503 writer.writeAttribute(foNS, QString::fromLatin1("text-transform"), QString::fromLatin1("lowercase")); break;-
504 case QFont::Capitalize:-
505 writer.writeAttribute(foNS, QString::fromLatin1("text-transform"), QString::fromLatin1("capitalize")); break;-
506 case QFont::SmallCaps:-
507 writer.writeAttribute(foNS, QString::fromLatin1("font-variant"), QString::fromLatin1("small-caps")); break;-
508 }-
509 }-
510 if (format.hasProperty(QTextFormat::FontLetterSpacing))-
511 writer.writeAttribute(foNS, QString::fromLatin1("letter-spacing"), pixelToPoint(format.fontLetterSpacing()));-
512 if (format.hasProperty(QTextFormat::FontWordSpacing) && format.fontWordSpacing() != 0)-
513 writer.writeAttribute(foNS, QString::fromLatin1("word-spacing"), pixelToPoint(format.fontWordSpacing()));-
514 if (format.hasProperty(QTextFormat::FontUnderline))-
515 writer.writeAttribute(styleNS, QString::fromLatin1("text-underline-type"),-
516 format.fontUnderline() ? QString::fromLatin1("single") : QString::fromLatin1("none"));-
517 if (format.hasProperty(QTextFormat::FontOverline)) {-
518-
519 }-
520 if (format.hasProperty(QTextFormat::FontStrikeOut))-
521 writer.writeAttribute(styleNS,QString::fromLatin1( "text-line-through-type"),-
522 format.fontStrikeOut() ? QString::fromLatin1("single") : QString::fromLatin1("none"));-
523 if (format.hasProperty(QTextFormat::TextUnderlineColor))-
524 writer.writeAttribute(styleNS, QString::fromLatin1("text-underline-color"), format.underlineColor().name());-
525 if (format.hasProperty(QTextFormat::FontFixedPitch)) {-
526-
527 }-
528 if (format.hasProperty(QTextFormat::TextUnderlineStyle)) {-
529 QString value;-
530 switch (format.underlineStyle()) {-
531 case QTextCharFormat::NoUnderline: value = QString::fromLatin1("none"); break;-
532 case QTextCharFormat::SingleUnderline: value = QString::fromLatin1("solid"); break;-
533 case QTextCharFormat::DashUnderline: value = QString::fromLatin1("dash"); break;-
534 case QTextCharFormat::DotLine: value = QString::fromLatin1("dotted"); break;-
535 case QTextCharFormat::DashDotLine: value = QString::fromLatin1("dash-dot"); break;-
536 case QTextCharFormat::DashDotDotLine: value = QString::fromLatin1("dot-dot-dash"); break;-
537 case QTextCharFormat::WaveUnderline: value = QString::fromLatin1("wave"); break;-
538 case QTextCharFormat::SpellCheckUnderline: value = QString::fromLatin1("none"); break;-
539 }-
540 writer.writeAttribute(styleNS, QString::fromLatin1("text-underline-style"), value);-
541 }-
542 if (format.hasProperty(QTextFormat::TextVerticalAlignment)) {-
543 QString value;-
544 switch (format.verticalAlignment()) {-
545 case QTextCharFormat::AlignMiddle:-
546 case QTextCharFormat::AlignNormal: value = QString::fromLatin1("0%"); break;-
547 case QTextCharFormat::AlignSuperScript: value = QString::fromLatin1("super"); break;-
548 case QTextCharFormat::AlignSubScript: value = QString::fromLatin1("sub"); break;-
549 case QTextCharFormat::AlignTop: value = QString::fromLatin1("100%"); break;-
550 case QTextCharFormat::AlignBottom : value = QString::fromLatin1("-100%"); break;-
551 case QTextCharFormat::AlignBaseline: break;-
552 }-
553 writer.writeAttribute(styleNS, QString::fromLatin1("text-position"), value);-
554 }-
555 if (format.hasProperty(QTextFormat::TextOutline))-
556 writer.writeAttribute(styleNS, QString::fromLatin1("text-outline"), QString::fromLatin1("true"));-
557 if (format.hasProperty(QTextFormat::TextToolTip)) {-
558-
559 }-
560 if (format.hasProperty(QTextFormat::IsAnchor)) {-
561-
562 }-
563 if (format.hasProperty(QTextFormat::AnchorHref)) {-
564-
565 }-
566 if (format.hasProperty(QTextFormat::AnchorName)) {-
567-
568 }-
569 if (format.hasProperty(QTextFormat::ForegroundBrush)) {-
570 QBrush brush = format.foreground();-
571 writer.writeAttribute(foNS, QString::fromLatin1("color"), brush.color().name());-
572 }-
573 if (format.hasProperty(QTextFormat::BackgroundBrush)) {-
574 QBrush brush = format.background();-
575 writer.writeAttribute(foNS, QString::fromLatin1("background-color"), brush.color().name());-
576 }-
577-
578 writer.writeEndElement();-
579}-
580-
581void QTextOdfWriter::writeListFormat(QXmlStreamWriter &writer, QTextListFormat format, int formatIndex) const-
582{-
583 writer.writeStartElement(textNS, QString::fromLatin1("list-style"));-
584 writer.writeAttribute(styleNS, QString::fromLatin1("name"), QString::fromLatin1("L%1").arg(formatIndex));-
585-
586 QTextListFormat::Style style = format.style();-
587 if (style == QTextListFormat::ListDecimal || style == QTextListFormat::ListLowerAlpha-
588 || style == QTextListFormat::ListUpperAlpha-
589 || style == QTextListFormat::ListLowerRoman-
590 || style == QTextListFormat::ListUpperRoman) {-
591 writer.writeStartElement(textNS, QString::fromLatin1("list-level-style-number"));-
592 writer.writeAttribute(styleNS, QString::fromLatin1("num-format"), bulletChar(style));-
593-
594 if (format.hasProperty(QTextFormat::ListNumberSuffix))-
595 writer.writeAttribute(styleNS, QString::fromLatin1("num-suffix"), format.numberSuffix());-
596 else-
597 writer.writeAttribute(styleNS, QString::fromLatin1("num-suffix"), QString::fromLatin1("."));-
598-
599 if (format.hasProperty(QTextFormat::ListNumberPrefix))-
600 writer.writeAttribute(styleNS, QString::fromLatin1("num-prefix"), format.numberPrefix());-
601-
602 } else {-
603 writer.writeStartElement(textNS, QString::fromLatin1("list-level-style-bullet"));-
604 writer.writeAttribute(textNS, QString::fromLatin1("bullet-char"), bulletChar(style));-
605 }-
606-
607 writer.writeAttribute(textNS, QString::fromLatin1("level"), QString::number(format.indent()));-
608 writer.writeEmptyElement(styleNS, QString::fromLatin1("list-level-properties"));-
609 writer.writeAttribute(foNS, QString::fromLatin1("text-align"), QString::fromLatin1("start"));-
610 QString spacing = QString::fromLatin1("%1mm").arg(format.indent() * 8);-
611 writer.writeAttribute(textNS, QString::fromLatin1("space-before"), spacing);-
612-
613-
614 writer.writeEndElement();-
615 writer.writeEndElement();-
616}-
617-
618void QTextOdfWriter::writeFrameFormat(QXmlStreamWriter &writer, QTextFrameFormat format, int formatIndex) const-
619{-
620 writer.writeStartElement(styleNS, QString::fromLatin1("style"));-
621 writer.writeAttribute(styleNS, QString::fromLatin1("name"), QString::fromLatin1("s%1").arg(formatIndex));-
622 writer.writeAttribute(styleNS, QString::fromLatin1("family"), QString::fromLatin1("section"));-
623 writer.writeEmptyElement(styleNS, QString::fromLatin1("section-properties"));-
624 if (format.hasProperty(QTextFormat::FrameTopMargin))-
625 writer.writeAttribute(foNS, QString::fromLatin1("margin-top"), pixelToPoint(qMax(qreal(0.), format.topMargin())) );-
626 if (format.hasProperty(QTextFormat::FrameBottomMargin))-
627 writer.writeAttribute(foNS, QString::fromLatin1("margin-bottom"), pixelToPoint(qMax(qreal(0.), format.bottomMargin())) );-
628 if (format.hasProperty(QTextFormat::FrameLeftMargin))-
629 writer.writeAttribute(foNS, QString::fromLatin1("margin-left"), pixelToPoint(qMax(qreal(0.), format.leftMargin())) );-
630 if (format.hasProperty(QTextFormat::FrameRightMargin))-
631 writer.writeAttribute(foNS, QString::fromLatin1("margin-right"), pixelToPoint(qMax(qreal(0.), format.rightMargin())) );-
632-
633 writer.writeEndElement();-
634}-
635-
636void QTextOdfWriter::writeTableCellFormat(QXmlStreamWriter &writer, QTextTableCellFormat format, int formatIndex) const-
637{-
638 writer.writeStartElement(styleNS, QString::fromLatin1("style"));-
639 writer.writeAttribute(styleNS, QString::fromLatin1("name"), QString::fromLatin1("T%1").arg(formatIndex));-
640 writer.writeAttribute(styleNS, QString::fromLatin1("family"), QString::fromLatin1("table"));-
641 writer.writeEmptyElement(styleNS, QString::fromLatin1("table-properties"));-
642-
643-
644 qreal padding = format.topPadding();-
645 if (padding > 0 && padding == format.bottomPadding()-
646 && padding == format.leftPadding() && padding == format.rightPadding()) {-
647 writer.writeAttribute(foNS, QString::fromLatin1("padding"), pixelToPoint(padding));-
648 }-
649 else {-
650 if (padding > 0)-
651 writer.writeAttribute(foNS, QString::fromLatin1("padding-top"), pixelToPoint(padding));-
652 if (format.bottomPadding() > 0)-
653 writer.writeAttribute(foNS, QString::fromLatin1("padding-bottom"), pixelToPoint(format.bottomPadding()));-
654 if (format.leftPadding() > 0)-
655 writer.writeAttribute(foNS, QString::fromLatin1("padding-left"), pixelToPoint(format.leftPadding()));-
656 if (format.rightPadding() > 0)-
657 writer.writeAttribute(foNS, QString::fromLatin1("padding-right"), pixelToPoint(format.rightPadding()));-
658 }-
659-
660 if (format.hasProperty(QTextFormat::TextVerticalAlignment)) {-
661 QString pos;-
662 switch (format.verticalAlignment()) {-
663 case QTextCharFormat::AlignMiddle:-
664 pos = QString::fromLatin1("middle"); break;-
665 case QTextCharFormat::AlignTop:-
666 pos = QString::fromLatin1("top"); break;-
667 case QTextCharFormat::AlignBottom:-
668 pos = QString::fromLatin1("bottom"); break;-
669 default:-
670 pos = QString::fromLatin1("automatic"); break;-
671 }-
672 writer.writeAttribute(styleNS, QString::fromLatin1("vertical-align"), pos);-
673 }-
674-
675-
676-
677-
678-
679-
680-
681 writer.writeEndElement();-
682}-
683-
684-
685-
686QTextOdfWriter::QTextOdfWriter(const QTextDocument &document, QIODevice *device)-
687 : officeNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:office:1.0")),-
688 textNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:text:1.0")),-
689 styleNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:style:1.0")),-
690 foNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0")),-
691 tableNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:table:1.0")),-
692 drawNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:drawing:1.0")),-
693 xlinkNS (QLatin1String("http://www.w3.org/1999/xlink")),-
694 svgNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0")),-
695 m_document(&document),-
696 m_device(device),-
697 m_strategy(0),-
698 m_codec(0),-
699 m_createArchive(true)-
700{-
701}-
702-
703bool QTextOdfWriter::writeAll()-
704{-
705 if (m_createArchive
m_createArchiveDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
706 m_strategy = new QZipStreamStrategy(m_device);
never executed: m_strategy = new QZipStreamStrategy(m_device);
0
707 else-
708 m_strategy = new QXmlStreamStrategy(m_device);
never executed: m_strategy = new QXmlStreamStrategy(m_device);
0
709-
710 if (!m_device->isWritable()
!m_device->isWritable()Description
TRUEnever evaluated
FALSEnever evaluated
&& ! m_device->open(QIODevice::WriteOnly)
! m_device->op...ce::WriteOnly)Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
711 QMessageLogger(__FILE__, 766772, __PRETTY_FUNCTION__).warning() << ("QTextOdfWriter::writeAll: the device can not be opened for writing";);-
712 return
never executed: return false;
false;
never executed: return false;
0
713 }-
714 QXmlStreamWriter writer(m_strategy->contentStream);-
715-
716 if (m_codec
m_codecDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
717 writer.setCodec(m_codec);
never executed: writer.setCodec(m_codec);
0
718-
719-
720 writer.setAutoFormatting(true);-
721 writer.setAutoFormattingIndent(2);-
722-
723 writer.writeNamespace(officeNS, QString::fromLatin1("office"));-
724 writer.writeNamespace(textNS, QString::fromLatin1("text"));-
725 writer.writeNamespace(styleNS, QString::fromLatin1("style"));-
726 writer.writeNamespace(foNS, QString::fromLatin1("fo"));-
727 writer.writeNamespace(tableNS, QString::fromLatin1("table"));-
728 writer.writeNamespace(drawNS, QString::fromLatin1("draw"));-
729 writer.writeNamespace(xlinkNS, QString::fromLatin1("xlink"));-
730 writer.writeNamespace(svgNS, QString::fromLatin1("svg"));-
731 writer.writeStartDocument();-
732 writer.writeStartElement(officeNS, QString::fromLatin1("document-content"));-
733 writer.writeAttribute(officeNS, QString::fromLatin1("version"), QString::fromLatin1("1.2"));-
734-
735-
736 QTextDocumentPrivate::FragmentIterator fragIt = m_document->docHandle()->begin();-
737 QSet<int> formats;-
738 while (fragIt != m_document->docHandle()->end()
fragIt != m_do...andle()->end()Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
739 const QTextFragmentData * const frag = fragIt.value();-
740 formats << frag->format;-
741 ++fragIt;-
742 }
never executed: end of block
0
743-
744-
745 QTextDocumentPrivate::BlockMap &blocks = m_document->docHandle()->blockMap();-
746 QTextDocumentPrivate::BlockMap::Iterator blockIt = blocks.begin();-
747 while (blockIt != blocks.end()
blockIt != blocks.end()Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
748 const QTextBlockData * const block = blockIt.value();-
749 formats << block->format;-
750 ++blockIt;-
751 }
never executed: end of block
0
752-
753-
754 const QVector<QTextFormat> allFormats = m_document->allFormats();-
755 const QList<int> copy = formats.toList();-
756 for (QList<int>::Iterator iter = copy.begin(); iter !=auto index : copy.end(); ++iter) {-
757 QTextObject *object = m_document->objectForFormat(allFormats[*iter[index]);-
758 if (object
objectDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
759 formats << object->formatIndex();
never executed: formats << object->formatIndex();
0
760 }
never executed: end of block
0
761-
762 writeFormats(writer, formats);-
763-
764 writer.writeStartElement(officeNS, QString::fromLatin1("body"));-
765 writer.writeStartElement(officeNS, QString::fromLatin1("text"));-
766 QTextFrame *rootFrame = m_document->rootFrame();-
767 writeFrame(writer, rootFrame);-
768 writer.writeEndElement();-
769 writer.writeEndElement();-
770 writer.writeEndElement();-
771 writer.writeEndDocument();-
772 delete m_strategy;-
773 m_strategy = 0;-
774-
775 return
never executed: return true;
true;
never executed: return true;
0
776}-
777-
778-
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9