| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the QtGui module of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include <qglobal.h> | 
|---|
| 43 |  | 
|---|
| 44 | #ifndef QT_NO_TEXTODFWRITER | 
|---|
| 45 |  | 
|---|
| 46 | #include "qtextodfwriter_p.h" | 
|---|
| 47 |  | 
|---|
| 48 | #include <QImageWriter> | 
|---|
| 49 | #include <QTextListFormat> | 
|---|
| 50 | #include <QTextList> | 
|---|
| 51 | #include <QBuffer> | 
|---|
| 52 | #include <QUrl> | 
|---|
| 53 |  | 
|---|
| 54 | #include "qtextdocument_p.h" | 
|---|
| 55 | #include "qtexttable.h" | 
|---|
| 56 | #include "qtextcursor.h" | 
|---|
| 57 | #include "qtextimagehandler_p.h" | 
|---|
| 58 | #include "qzipwriter_p.h" | 
|---|
| 59 |  | 
|---|
| 60 | #include <QDebug> | 
|---|
| 61 |  | 
|---|
| 62 | QT_BEGIN_NAMESPACE | 
|---|
| 63 |  | 
|---|
| 64 | /// Convert pixels to postscript point units | 
|---|
| 65 | static QString pixelToPoint(qreal pixels) | 
|---|
| 66 | { | 
|---|
| 67 | // we hardcode 96 DPI, we do the same in the ODF importer to have a perfect roundtrip. | 
|---|
| 68 | return QString::number(pixels * 72 / 96) + QString::fromLatin1("pt"); | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | // strategies | 
|---|
| 72 | class QOutputStrategy { | 
|---|
| 73 | public: | 
|---|
| 74 | QOutputStrategy() : contentStream(0), counter(1) { } | 
|---|
| 75 | virtual ~QOutputStrategy() {} | 
|---|
| 76 | virtual void addFile(const QString &fileName, const QString &mimeType, const QByteArray &bytes) = 0; | 
|---|
| 77 |  | 
|---|
| 78 | QString createUniqueImageName() | 
|---|
| 79 | { | 
|---|
| 80 | return QString::fromLatin1("Pictures/Picture%1").arg(counter++); | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | QIODevice *contentStream; | 
|---|
| 84 | int counter; | 
|---|
| 85 | }; | 
|---|
| 86 |  | 
|---|
| 87 | class QXmlStreamStrategy : public QOutputStrategy { | 
|---|
| 88 | public: | 
|---|
| 89 | QXmlStreamStrategy(QIODevice *device) | 
|---|
| 90 | { | 
|---|
| 91 | contentStream = device; | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | virtual ~QXmlStreamStrategy() | 
|---|
| 95 | { | 
|---|
| 96 | if (contentStream) | 
|---|
| 97 | contentStream->close(); | 
|---|
| 98 | } | 
|---|
| 99 | virtual void addFile(const QString &, const QString &, const QByteArray &) | 
|---|
| 100 | { | 
|---|
| 101 | // we ignore this... | 
|---|
| 102 | } | 
|---|
| 103 | }; | 
|---|
| 104 |  | 
|---|
| 105 | class QZipStreamStrategy : public QOutputStrategy { | 
|---|
| 106 | public: | 
|---|
| 107 | QZipStreamStrategy(QIODevice *device) | 
|---|
| 108 | : zip(device), | 
|---|
| 109 | manifestWriter(&manifest) | 
|---|
| 110 | { | 
|---|
| 111 | QByteArray mime("application/vnd.oasis.opendocument.text"); | 
|---|
| 112 | zip.setCompressionPolicy(QZipWriter::NeverCompress); | 
|---|
| 113 | zip.addFile(QString::fromLatin1("mimetype"), mime); // for mime-magick | 
|---|
| 114 | zip.setCompressionPolicy(QZipWriter::AutoCompress); | 
|---|
| 115 | contentStream = &content; | 
|---|
| 116 | content.open(QIODevice::WriteOnly); | 
|---|
| 117 | manifest.open(QIODevice::WriteOnly); | 
|---|
| 118 |  | 
|---|
| 119 | manifestNS = QString::fromLatin1("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"); | 
|---|
| 120 | // prettyfy | 
|---|
| 121 | manifestWriter.setAutoFormatting(true); | 
|---|
| 122 | manifestWriter.setAutoFormattingIndent(1); | 
|---|
| 123 |  | 
|---|
| 124 | manifestWriter.writeNamespace(manifestNS, QString::fromLatin1("manifest")); | 
|---|
| 125 | manifestWriter.writeStartDocument(); | 
|---|
| 126 | manifestWriter.writeStartElement(manifestNS, QString::fromLatin1("manifest")); | 
|---|
| 127 | addFile(QString::fromLatin1("/"), QString::fromLatin1("application/vnd.oasis.opendocument.text")); | 
|---|
| 128 | addFile(QString::fromLatin1("content.xml"), QString::fromLatin1("text/xml")); | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | ~QZipStreamStrategy() | 
|---|
| 132 | { | 
|---|
| 133 | manifestWriter.writeEndDocument(); | 
|---|
| 134 | manifest.close(); | 
|---|
| 135 | zip.addFile(QString::fromLatin1("META-INF/manifest.xml"), &manifest); | 
|---|
| 136 | content.close(); | 
|---|
| 137 | zip.addFile(QString::fromLatin1("content.xml"), &content); | 
|---|
| 138 | zip.close(); | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | virtual void addFile(const QString &fileName, const QString &mimeType, const QByteArray &bytes) | 
|---|
| 142 | { | 
|---|
| 143 | zip.addFile(fileName, bytes); | 
|---|
| 144 | addFile(fileName, mimeType); | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | private: | 
|---|
| 148 | void addFile(const QString &fileName, const QString &mimeType) | 
|---|
| 149 | { | 
|---|
| 150 | manifestWriter.writeEmptyElement(manifestNS, QString::fromLatin1("file-entry")); | 
|---|
| 151 | manifestWriter.writeAttribute(manifestNS, QString::fromLatin1("media-type"), mimeType); | 
|---|
| 152 | manifestWriter.writeAttribute(manifestNS, QString::fromLatin1("full-path"), fileName); | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | QBuffer content; | 
|---|
| 156 | QBuffer manifest; | 
|---|
| 157 | QZipWriter zip; | 
|---|
| 158 | QXmlStreamWriter manifestWriter; | 
|---|
| 159 | QString manifestNS; | 
|---|
| 160 | }; | 
|---|
| 161 |  | 
|---|
| 162 | static QString bulletChar(QTextListFormat::Style style) | 
|---|
| 163 | { | 
|---|
| 164 | switch(style) { | 
|---|
| 165 | case QTextListFormat::ListDisc: | 
|---|
| 166 | return QChar(0x25cf); // bullet character | 
|---|
| 167 | case QTextListFormat::ListCircle: | 
|---|
| 168 | return QChar(0x25cb); // white circle | 
|---|
| 169 | case QTextListFormat::ListSquare: | 
|---|
| 170 | return QChar(0x25a1); // white square | 
|---|
| 171 | case QTextListFormat::ListDecimal: | 
|---|
| 172 | return QString::fromLatin1("1"); | 
|---|
| 173 | case QTextListFormat::ListLowerAlpha: | 
|---|
| 174 | return QString::fromLatin1("a"); | 
|---|
| 175 | case QTextListFormat::ListUpperAlpha: | 
|---|
| 176 | return QString::fromLatin1("A"); | 
|---|
| 177 | default: | 
|---|
| 178 | case QTextListFormat::ListStyleUndefined: | 
|---|
| 179 | return QString(); | 
|---|
| 180 | } | 
|---|
| 181 | } | 
|---|
| 182 |  | 
|---|
| 183 | void QTextOdfWriter::writeFrame(QXmlStreamWriter &writer, const QTextFrame *frame) | 
|---|
| 184 | { | 
|---|
| 185 | Q_ASSERT(frame); | 
|---|
| 186 | const QTextTable *table = qobject_cast<const QTextTable*> (frame); | 
|---|
| 187 |  | 
|---|
| 188 | if (table) { // Start a table. | 
|---|
| 189 | writer.writeStartElement(tableNS, QString::fromLatin1("table")); | 
|---|
| 190 | writer.writeEmptyElement(tableNS, QString::fromLatin1("table-column")); | 
|---|
| 191 | writer.writeAttribute(tableNS, QString::fromLatin1("number-columns-repeated"), QString::number(table->columns())); | 
|---|
| 192 | } else if (frame->document() && frame->document()->rootFrame() != frame) { // start a section | 
|---|
| 193 | writer.writeStartElement(textNS, QString::fromLatin1("section")); | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | QTextFrame::iterator iterator = frame->begin(); | 
|---|
| 197 | QTextFrame *child = 0; | 
|---|
| 198 |  | 
|---|
| 199 | int tableRow = -1; | 
|---|
| 200 | while (! iterator.atEnd()) { | 
|---|
| 201 | if (iterator.currentFrame() && child != iterator.currentFrame()) | 
|---|
| 202 | writeFrame(writer, iterator.currentFrame()); | 
|---|
| 203 | else { // no frame, its a block | 
|---|
| 204 | QTextBlock block = iterator.currentBlock(); | 
|---|
| 205 | if (table) { | 
|---|
| 206 | QTextTableCell cell = table->cellAt(block.position()); | 
|---|
| 207 | if (tableRow < cell.row()) { | 
|---|
| 208 | if (tableRow >= 0) | 
|---|
| 209 | writer.writeEndElement(); // close table row | 
|---|
| 210 | tableRow = cell.row(); | 
|---|
| 211 | writer.writeStartElement(tableNS, QString::fromLatin1("table-row")); | 
|---|
| 212 | } | 
|---|
| 213 | writer.writeStartElement(tableNS, QString::fromLatin1("table-cell")); | 
|---|
| 214 | if (cell.columnSpan() > 1) | 
|---|
| 215 | writer.writeAttribute(tableNS, QString::fromLatin1("number-columns-spanned"), QString::number(cell.columnSpan())); | 
|---|
| 216 | if (cell.rowSpan() > 1) | 
|---|
| 217 | writer.writeAttribute(tableNS, QString::fromLatin1("number-rows-spanned"), QString::number(cell.rowSpan())); | 
|---|
| 218 | if (cell.format().isTableCellFormat()) { | 
|---|
| 219 | writer.writeAttribute(tableNS, QString::fromLatin1("style-name"), QString::fromLatin1("T%1").arg(cell.tableCellFormatIndex())); | 
|---|
| 220 | } | 
|---|
| 221 | } | 
|---|
| 222 | writeBlock(writer, block); | 
|---|
| 223 | if (table) | 
|---|
| 224 | writer.writeEndElement(); // table-cell | 
|---|
| 225 | } | 
|---|
| 226 | child = iterator.currentFrame(); | 
|---|
| 227 | ++iterator; | 
|---|
| 228 | } | 
|---|
| 229 | if (tableRow >= 0) | 
|---|
| 230 | writer.writeEndElement(); // close table-row | 
|---|
| 231 |  | 
|---|
| 232 | if (table || (frame->document() && frame->document()->rootFrame() != frame)) | 
|---|
| 233 | writer.writeEndElement();  // close table or section element | 
|---|
| 234 | } | 
|---|
| 235 |  | 
|---|
| 236 | void QTextOdfWriter::writeBlock(QXmlStreamWriter &writer, const QTextBlock &block) | 
|---|
| 237 | { | 
|---|
| 238 | if (block.textList()) { // its a list-item | 
|---|
| 239 | const int listLevel = block.textList()->format().indent(); | 
|---|
| 240 | if (m_listStack.isEmpty() || m_listStack.top() != block.textList()) { | 
|---|
| 241 | // not the same list we were in. | 
|---|
| 242 | while (m_listStack.count() >= listLevel && !m_listStack.isEmpty() && m_listStack.top() != block.textList() ) { // we need to close tags | 
|---|
| 243 | m_listStack.pop(); | 
|---|
| 244 | writer.writeEndElement(); // list | 
|---|
| 245 | if (m_listStack.count()) | 
|---|
| 246 | writer.writeEndElement(); // list-item | 
|---|
| 247 | } | 
|---|
| 248 | while (m_listStack.count() < listLevel) { | 
|---|
| 249 | if (m_listStack.count()) | 
|---|
| 250 | writer.writeStartElement(textNS, QString::fromLatin1("list-item")); | 
|---|
| 251 | writer.writeStartElement(textNS, QString::fromLatin1("list")); | 
|---|
| 252 | if (m_listStack.count() == listLevel - 1) { | 
|---|
| 253 | m_listStack.push(block.textList()); | 
|---|
| 254 | writer.writeAttribute(textNS, QString::fromLatin1("style-name"), QString::fromLatin1("L%1") | 
|---|
| 255 | .arg(block.textList()->formatIndex())); | 
|---|
| 256 | } | 
|---|
| 257 | else { | 
|---|
| 258 | m_listStack.push(0); | 
|---|
| 259 | } | 
|---|
| 260 | } | 
|---|
| 261 | } | 
|---|
| 262 | writer.writeStartElement(textNS, QString::fromLatin1("list-item")); | 
|---|
| 263 | } | 
|---|
| 264 | else { | 
|---|
| 265 | while (! m_listStack.isEmpty()) { | 
|---|
| 266 | m_listStack.pop(); | 
|---|
| 267 | writer.writeEndElement(); // list | 
|---|
| 268 | if (m_listStack.count()) | 
|---|
| 269 | writer.writeEndElement(); // list-item | 
|---|
| 270 | } | 
|---|
| 271 | } | 
|---|
| 272 |  | 
|---|
| 273 | if (block.length() == 1) { // only a linefeed | 
|---|
| 274 | writer.writeEmptyElement(textNS, QString::fromLatin1("p")); | 
|---|
| 275 | writer.writeAttribute(textNS, QString::fromLatin1("style-name"), QString::fromLatin1("p%1") | 
|---|
| 276 | .arg(block.blockFormatIndex())); | 
|---|
| 277 | if (block.textList()) | 
|---|
| 278 | writer.writeEndElement(); // numbered-paragraph | 
|---|
| 279 | return; | 
|---|
| 280 | } | 
|---|
| 281 | writer.writeStartElement(textNS, QString::fromLatin1("p")); | 
|---|
| 282 | writer.writeAttribute(textNS, QString::fromLatin1("style-name"), QString::fromLatin1("p%1") | 
|---|
| 283 | .arg(block.blockFormatIndex())); | 
|---|
| 284 | for (QTextBlock::Iterator frag= block.begin(); !frag.atEnd(); frag++) { | 
|---|
| 285 | writer.writeCharacters(QString()); // Trick to make sure that the span gets no linefeed in front of it. | 
|---|
| 286 | writer.writeStartElement(textNS, QString::fromLatin1("span")); | 
|---|
| 287 |  | 
|---|
| 288 | QString fragmentText = frag.fragment().text(); | 
|---|
| 289 | if (fragmentText.length() == 1 && fragmentText[0] == 0xFFFC) { // its an inline character. | 
|---|
| 290 | writeInlineCharacter(writer, frag.fragment()); | 
|---|
| 291 | writer.writeEndElement(); // span | 
|---|
| 292 | continue; | 
|---|
| 293 | } | 
|---|
| 294 |  | 
|---|
| 295 | writer.writeAttribute(textNS, QString::fromLatin1("style-name"), QString::fromLatin1("c%1") | 
|---|
| 296 | .arg(frag.fragment().charFormatIndex())); | 
|---|
| 297 | bool escapeNextSpace = true; | 
|---|
| 298 | int precedingSpaces = 0, precedingTabs = 0; | 
|---|
| 299 | int exportedIndex = 0; | 
|---|
| 300 | for (int i=0; i <= fragmentText.count(); ++i) { | 
|---|
| 301 | bool isTab = false, isSpace = false; | 
|---|
| 302 | if (i < fragmentText.count()) { | 
|---|
| 303 | QChar character = fragmentText[i]; | 
|---|
| 304 | isTab = character.unicode() == '\t'; | 
|---|
| 305 | isSpace = character.unicode() == ' '; | 
|---|
| 306 | if (character.unicode() == 0x2028) { // soft-return | 
|---|
| 307 | writer.writeCharacters(fragmentText.mid(exportedIndex, i)); | 
|---|
| 308 | writer.writeEmptyElement(textNS, QString::fromLatin1("line-break")); | 
|---|
| 309 | exportedIndex = i+1; | 
|---|
| 310 | continue; | 
|---|
| 311 | } | 
|---|
| 312 | if (isSpace) { | 
|---|
| 313 | ++precedingSpaces; | 
|---|
| 314 | escapeNextSpace = true; | 
|---|
| 315 | } | 
|---|
| 316 | else if (isTab) { | 
|---|
| 317 | precedingTabs++; | 
|---|
| 318 | } | 
|---|
| 319 | } | 
|---|
| 320 | // find more than one space. -> <text:s text:c="2" /> | 
|---|
| 321 | if (!isSpace && escapeNextSpace && precedingSpaces > 1) { | 
|---|
| 322 | const bool startParag = exportedIndex == 0 && i == precedingSpaces; | 
|---|
| 323 | if (!startParag) | 
|---|
| 324 | writer.writeCharacters(fragmentText.mid(exportedIndex, i - precedingSpaces + 1 - exportedIndex)); | 
|---|
| 325 | writer.writeEmptyElement(textNS, QString::fromLatin1("s")); | 
|---|
| 326 | const int count = precedingSpaces - (startParag?0:1); | 
|---|
| 327 | if (count > 1) | 
|---|
| 328 | writer.writeAttribute(textNS, QString::fromLatin1("c"), QString::number(count)); | 
|---|
| 329 | precedingSpaces = 0; | 
|---|
| 330 | exportedIndex = i; | 
|---|
| 331 | } | 
|---|
| 332 | // find tabs.   ->  <text:tab text:tab-ref="3" />  or <text:tab/> | 
|---|
| 333 | if (!isTab && precedingTabs) { | 
|---|
| 334 | writer.writeCharacters(fragmentText.mid(exportedIndex, i - precedingTabs - exportedIndex)); | 
|---|
| 335 | writer.writeEmptyElement(textNS, QString::fromLatin1("tab")); | 
|---|
| 336 | if (precedingTabs > 1) | 
|---|
| 337 | writer.writeAttribute(textNS, QString::fromLatin1("tab-ref"), QString::number(precedingTabs)); | 
|---|
| 338 | precedingTabs = 0; | 
|---|
| 339 | exportedIndex = i; | 
|---|
| 340 | } | 
|---|
| 341 | if (!isSpace && !isTab) | 
|---|
| 342 | precedingSpaces = 0; | 
|---|
| 343 | } | 
|---|
| 344 |  | 
|---|
| 345 | writer.writeCharacters(fragmentText.mid(exportedIndex)); | 
|---|
| 346 | writer.writeEndElement(); // span | 
|---|
| 347 | } | 
|---|
| 348 | writer.writeCharacters(QString()); // Trick to make sure that the span gets no linefeed behind it. | 
|---|
| 349 | writer.writeEndElement(); // p | 
|---|
| 350 | if (block.textList()) | 
|---|
| 351 | writer.writeEndElement(); // list-item | 
|---|
| 352 | } | 
|---|
| 353 |  | 
|---|
| 354 | void QTextOdfWriter::writeInlineCharacter(QXmlStreamWriter &writer, const QTextFragment &fragment) const | 
|---|
| 355 | { | 
|---|
| 356 | writer.writeStartElement(drawNS, QString::fromLatin1("frame")); | 
|---|
| 357 | if (m_strategy == 0) { | 
|---|
| 358 | // don't do anything. | 
|---|
| 359 | } | 
|---|
| 360 | else if (fragment.charFormat().isImageFormat()) { | 
|---|
| 361 | QTextImageFormat imageFormat = fragment.charFormat().toImageFormat(); | 
|---|
| 362 | writer.writeAttribute(drawNS, QString::fromLatin1("name"), imageFormat.name()); | 
|---|
| 363 |  | 
|---|
| 364 | // vvv  Copy pasted mostly from Qt ================= | 
|---|
| 365 | QImage image; | 
|---|
| 366 | QString name = imageFormat.name(); | 
|---|
| 367 | if (name.startsWith(QLatin1String(":/"))) // auto-detect resources | 
|---|
| 368 | name.prepend(QLatin1String("qrc")); | 
|---|
| 369 | QUrl url = QUrl::fromEncoded(name.toUtf8()); | 
|---|
| 370 | const QVariant data = m_document->resource(QTextDocument::ImageResource, url); | 
|---|
| 371 | if (data.type() == QVariant::Image) { | 
|---|
| 372 | image = qvariant_cast<QImage>(data); | 
|---|
| 373 | } else if (data.type() == QVariant::ByteArray) { | 
|---|
| 374 | image.loadFromData(data.toByteArray()); | 
|---|
| 375 | } | 
|---|
| 376 |  | 
|---|
| 377 | if (image.isNull()) { | 
|---|
| 378 | QString context; | 
|---|
| 379 | if (QTextImageHandler::externalLoader) | 
|---|
| 380 | image = QTextImageHandler::externalLoader(name, context); | 
|---|
| 381 |  | 
|---|
| 382 | if (image.isNull()) { // try direct loading | 
|---|
| 383 | name = imageFormat.name(); // remove qrc:/ prefix again | 
|---|
| 384 | image.load(name); | 
|---|
| 385 | } | 
|---|
| 386 | } | 
|---|
| 387 |  | 
|---|
| 388 | // ^^^ Copy pasted mostly from Qt ================= | 
|---|
| 389 | if (! image.isNull()) { | 
|---|
| 390 | QBuffer imageBytes; | 
|---|
| 391 | QImageWriter imageWriter(&imageBytes, "png"); | 
|---|
| 392 | imageWriter.write(image); | 
|---|
| 393 | QString filename = m_strategy->createUniqueImageName(); | 
|---|
| 394 | m_strategy->addFile(filename, QString::fromLatin1("image/png"), imageBytes.data()); | 
|---|
| 395 |  | 
|---|
| 396 | // get the width/height from the format. | 
|---|
| 397 | qreal width = (imageFormat.hasProperty(QTextFormat::ImageWidth)) ? imageFormat.width() : image.width(); | 
|---|
| 398 | writer.writeAttribute(svgNS, QString::fromLatin1("width"), pixelToPoint(width)); | 
|---|
| 399 | qreal height = (imageFormat.hasProperty(QTextFormat::ImageHeight)) ? imageFormat.height() : image.height(); | 
|---|
| 400 | writer.writeAttribute(svgNS, QString::fromLatin1("height"), pixelToPoint(height)); | 
|---|
| 401 |  | 
|---|
| 402 | writer.writeStartElement(drawNS, QString::fromLatin1("image")); | 
|---|
| 403 | writer.writeAttribute(xlinkNS, QString::fromLatin1("href"), filename); | 
|---|
| 404 | writer.writeEndElement(); // image | 
|---|
| 405 | } | 
|---|
| 406 | } | 
|---|
| 407 |  | 
|---|
| 408 | writer.writeEndElement(); // frame | 
|---|
| 409 | } | 
|---|
| 410 |  | 
|---|
| 411 | void QTextOdfWriter::writeFormats(QXmlStreamWriter &writer, QSet<int> formats) const | 
|---|
| 412 | { | 
|---|
| 413 | writer.writeStartElement(officeNS, QString::fromLatin1("automatic-styles")); | 
|---|
| 414 | QVector<QTextFormat> allStyles = m_document->allFormats(); | 
|---|
| 415 | QSetIterator<int> formatId(formats); | 
|---|
| 416 | while(formatId.hasNext()) { | 
|---|
| 417 | int formatIndex = formatId.next(); | 
|---|
| 418 | QTextFormat textFormat = allStyles.at(formatIndex); | 
|---|
| 419 | switch (textFormat.type()) { | 
|---|
| 420 | case QTextFormat::CharFormat: | 
|---|
| 421 | if (textFormat.isTableCellFormat()) | 
|---|
| 422 | writeTableCellFormat(writer, textFormat.toTableCellFormat(), formatIndex); | 
|---|
| 423 | else | 
|---|
| 424 | writeCharacterFormat(writer, textFormat.toCharFormat(), formatIndex); | 
|---|
| 425 | break; | 
|---|
| 426 | case QTextFormat::BlockFormat: | 
|---|
| 427 | writeBlockFormat(writer, textFormat.toBlockFormat(), formatIndex); | 
|---|
| 428 | break; | 
|---|
| 429 | case QTextFormat::ListFormat: | 
|---|
| 430 | writeListFormat(writer, textFormat.toListFormat(), formatIndex); | 
|---|
| 431 | break; | 
|---|
| 432 | case QTextFormat::FrameFormat: | 
|---|
| 433 | writeFrameFormat(writer, textFormat.toFrameFormat(), formatIndex); | 
|---|
| 434 | break; | 
|---|
| 435 | case QTextFormat::TableFormat: | 
|---|
| 436 | ;break; | 
|---|
| 437 | } | 
|---|
| 438 | } | 
|---|
| 439 |  | 
|---|
| 440 | writer.writeEndElement(); // automatic-styles | 
|---|
| 441 | } | 
|---|
| 442 |  | 
|---|
| 443 | void QTextOdfWriter::writeBlockFormat(QXmlStreamWriter &writer, QTextBlockFormat format, int formatIndex) const | 
|---|
| 444 | { | 
|---|
| 445 | writer.writeStartElement(styleNS, QString::fromLatin1("style")); | 
|---|
| 446 | writer.writeAttribute(styleNS, QString::fromLatin1("name"), QString::fromLatin1("p%1").arg(formatIndex)); | 
|---|
| 447 | writer.writeAttribute(styleNS, QString::fromLatin1("family"), QString::fromLatin1("paragraph")); | 
|---|
| 448 | writer.writeStartElement(styleNS, QString::fromLatin1("paragraph-properties")); | 
|---|
| 449 |  | 
|---|
| 450 | if (format.hasProperty(QTextFormat::BlockAlignment)) { | 
|---|
| 451 | QString value; | 
|---|
| 452 | if (format.alignment() == Qt::AlignLeading) | 
|---|
| 453 | value = QString::fromLatin1("start"); | 
|---|
| 454 | else if (format.alignment() == Qt::AlignTrailing) | 
|---|
| 455 | value = QString::fromLatin1("end"); | 
|---|
| 456 | else if (format.alignment() == (Qt::AlignLeft | Qt::AlignAbsolute)) | 
|---|
| 457 | value = QString::fromLatin1("left"); | 
|---|
| 458 | else if (format.alignment() == (Qt::AlignRight | Qt::AlignAbsolute)) | 
|---|
| 459 | value = QString::fromLatin1("right"); | 
|---|
| 460 | else if (format.alignment() == Qt::AlignHCenter) | 
|---|
| 461 | value = QString::fromLatin1("center"); | 
|---|
| 462 | else if (format.alignment() == Qt::AlignJustify) | 
|---|
| 463 | value = QString::fromLatin1("justify"); | 
|---|
| 464 | else | 
|---|
| 465 | qWarning() << "QTextOdfWriter: unsupported paragraph alignment; " << format.alignment(); | 
|---|
| 466 | if (! value.isNull()) | 
|---|
| 467 | writer.writeAttribute(foNS, QString::fromLatin1("text-align"), value); | 
|---|
| 468 | } | 
|---|
| 469 |  | 
|---|
| 470 | if (format.hasProperty(QTextFormat::BlockTopMargin)) | 
|---|
| 471 | writer.writeAttribute(foNS, QString::fromLatin1("margin-top"), pixelToPoint(qMax(qreal(0.), format.topMargin())) ); | 
|---|
| 472 | if (format.hasProperty(QTextFormat::BlockBottomMargin)) | 
|---|
| 473 | writer.writeAttribute(foNS, QString::fromLatin1("margin-bottom"), pixelToPoint(qMax(qreal(0.), format.bottomMargin())) ); | 
|---|
| 474 | if (format.hasProperty(QTextFormat::BlockLeftMargin) || format.hasProperty(QTextFormat::BlockIndent)) | 
|---|
| 475 | writer.writeAttribute(foNS, QString::fromLatin1("margin-left"), pixelToPoint(qMax(qreal(0.), | 
|---|
| 476 | format.leftMargin() + format.indent()))); | 
|---|
| 477 | if (format.hasProperty(QTextFormat::BlockRightMargin)) | 
|---|
| 478 | writer.writeAttribute(foNS, QString::fromLatin1("margin-right"), pixelToPoint(qMax(qreal(0.), format.rightMargin())) ); | 
|---|
| 479 | if (format.hasProperty(QTextFormat::TextIndent)) | 
|---|
| 480 | writer.writeAttribute(foNS, QString::fromLatin1("text-indent"), QString::number(format.textIndent())); | 
|---|
| 481 | if (format.hasProperty(QTextFormat::PageBreakPolicy)) { | 
|---|
| 482 | if (format.pageBreakPolicy() & QTextFormat::PageBreak_AlwaysBefore) | 
|---|
| 483 | writer.writeAttribute(foNS, QString::fromLatin1("break-before"), QString::fromLatin1("page")); | 
|---|
| 484 | if (format.pageBreakPolicy() & QTextFormat::PageBreak_AlwaysAfter) | 
|---|
| 485 | writer.writeAttribute(foNS, QString::fromLatin1("break-after"), QString::fromLatin1("page")); | 
|---|
| 486 | } | 
|---|
| 487 | if (format.hasProperty(QTextFormat::BlockNonBreakableLines)) | 
|---|
| 488 | writer.writeAttribute(foNS, QString::fromLatin1("keep-together"), | 
|---|
| 489 | format.nonBreakableLines() ? QString::fromLatin1("true") : QString::fromLatin1("false")); | 
|---|
| 490 | if (format.hasProperty(QTextFormat::TabPositions)) { | 
|---|
| 491 | QList<QTextOption::Tab> tabs = format.tabPositions(); | 
|---|
| 492 | writer.writeStartElement(styleNS, QString::fromLatin1("style-tab-stops")); | 
|---|
| 493 | QList<QTextOption::Tab>::Iterator iterator = tabs.begin(); | 
|---|
| 494 | while(iterator != tabs.end()) { | 
|---|
| 495 | writer.writeEmptyElement(styleNS, QString::fromLatin1("style-tab-stop")); | 
|---|
| 496 | writer.writeAttribute(styleNS, QString::fromLatin1("position"), pixelToPoint(iterator->position) ); | 
|---|
| 497 | QString type; | 
|---|
| 498 | switch(iterator->type) { | 
|---|
| 499 | case QTextOption::DelimiterTab: type = QString::fromLatin1("char"); break; | 
|---|
| 500 | case QTextOption::LeftTab: type = QString::fromLatin1("left"); break; | 
|---|
| 501 | case QTextOption::RightTab: type = QString::fromLatin1("right"); break; | 
|---|
| 502 | case QTextOption::CenterTab: type = QString::fromLatin1("center"); break; | 
|---|
| 503 | } | 
|---|
| 504 | writer.writeAttribute(styleNS, QString::fromLatin1("type"), type); | 
|---|
| 505 | if (iterator->delimiter != 0) | 
|---|
| 506 | writer.writeAttribute(styleNS, QString::fromLatin1("char"), iterator->delimiter); | 
|---|
| 507 | ++iterator; | 
|---|
| 508 | } | 
|---|
| 509 |  | 
|---|
| 510 | writer.writeEndElement(); // style-tab-stops | 
|---|
| 511 | } | 
|---|
| 512 |  | 
|---|
| 513 | writer.writeEndElement(); // paragraph-properties | 
|---|
| 514 | writer.writeEndElement(); // style | 
|---|
| 515 | } | 
|---|
| 516 |  | 
|---|
| 517 | void QTextOdfWriter::writeCharacterFormat(QXmlStreamWriter &writer, QTextCharFormat format, int formatIndex) const | 
|---|
| 518 | { | 
|---|
| 519 | writer.writeStartElement(styleNS, QString::fromLatin1("style")); | 
|---|
| 520 | writer.writeAttribute(styleNS, QString::fromLatin1("name"), QString::fromLatin1("c%1").arg(formatIndex)); | 
|---|
| 521 | writer.writeAttribute(styleNS, QString::fromLatin1("family"), QString::fromLatin1("text")); | 
|---|
| 522 | writer.writeEmptyElement(styleNS, QString::fromLatin1("text-properties")); | 
|---|
| 523 | if (format.fontItalic()) | 
|---|
| 524 | writer.writeAttribute(foNS, QString::fromLatin1("font-style"), QString::fromLatin1("italic")); | 
|---|
| 525 | if (format.hasProperty(QTextFormat::FontWeight) && format.fontWeight() != QFont::Normal) { | 
|---|
| 526 | QString value; | 
|---|
| 527 | if (format.fontWeight() == QFont::Bold) | 
|---|
| 528 | value = QString::fromLatin1("bold"); | 
|---|
| 529 | else | 
|---|
| 530 | value = QString::number(format.fontWeight() * 10); | 
|---|
| 531 | writer.writeAttribute(foNS, QString::fromLatin1("font-weight"), value); | 
|---|
| 532 | } | 
|---|
| 533 | if (format.hasProperty(QTextFormat::FontFamily)) | 
|---|
| 534 | writer.writeAttribute(foNS, QString::fromLatin1("font-family"), format.fontFamily()); | 
|---|
| 535 | else | 
|---|
| 536 | writer.writeAttribute(foNS, QString::fromLatin1("font-family"), QString::fromLatin1("Sans")); // Qt default | 
|---|
| 537 | if (format.hasProperty(QTextFormat::FontPointSize)) | 
|---|
| 538 | writer.writeAttribute(foNS, QString::fromLatin1("font-size"), QString::fromLatin1("%1pt").arg(format.fontPointSize())); | 
|---|
| 539 | if (format.hasProperty(QTextFormat::FontCapitalization)) { | 
|---|
| 540 | switch(format.fontCapitalization()) { | 
|---|
| 541 | case QFont::MixedCase: | 
|---|
| 542 | writer.writeAttribute(foNS, QString::fromLatin1("text-transform"), QString::fromLatin1("none")); break; | 
|---|
| 543 | case QFont::AllUppercase: | 
|---|
| 544 | writer.writeAttribute(foNS, QString::fromLatin1("text-transform"), QString::fromLatin1("uppercase")); break; | 
|---|
| 545 | case QFont::AllLowercase: | 
|---|
| 546 | writer.writeAttribute(foNS, QString::fromLatin1("text-transform"), QString::fromLatin1("lowercase")); break; | 
|---|
| 547 | case QFont::Capitalize: | 
|---|
| 548 | writer.writeAttribute(foNS, QString::fromLatin1("text-transform"), QString::fromLatin1("capitalize")); break; | 
|---|
| 549 | case QFont::SmallCaps: | 
|---|
| 550 | writer.writeAttribute(foNS, QString::fromLatin1("font-variant"), QString::fromLatin1("small-caps")); break; | 
|---|
| 551 | } | 
|---|
| 552 | } | 
|---|
| 553 | if (format.hasProperty(QTextFormat::FontLetterSpacing)) | 
|---|
| 554 | writer.writeAttribute(foNS, QString::fromLatin1("letter-spacing"), pixelToPoint(format.fontLetterSpacing()) ); | 
|---|
| 555 | if (format.hasProperty(QTextFormat::FontWordSpacing)) | 
|---|
| 556 | writer.writeAttribute(foNS, QString::fromLatin1("letter-spacing"), pixelToPoint(format.fontWordSpacing()) ); | 
|---|
| 557 | if (format.hasProperty(QTextFormat::FontUnderline)) | 
|---|
| 558 | writer.writeAttribute(styleNS, QString::fromLatin1("text-underline-type"), | 
|---|
| 559 | format.fontUnderline() ? QString::fromLatin1("single") : QString::fromLatin1("none")); | 
|---|
| 560 | if (format.hasProperty(QTextFormat::FontOverline)) { | 
|---|
| 561 | //   bool   fontOverline () const  TODO | 
|---|
| 562 | } | 
|---|
| 563 | if (format.hasProperty(QTextFormat::FontStrikeOut)) | 
|---|
| 564 | writer.writeAttribute(styleNS,QString::fromLatin1( "text-line-through-type"), | 
|---|
| 565 | format.fontStrikeOut() ? QString::fromLatin1("single") : QString::fromLatin1("none")); | 
|---|
| 566 | if (format.hasProperty(QTextFormat::TextUnderlineColor)) | 
|---|
| 567 | writer.writeAttribute(styleNS, QString::fromLatin1("text-underline-color"), format.underlineColor().name()); | 
|---|
| 568 | if (format.hasProperty(QTextFormat::FontFixedPitch)) { | 
|---|
| 569 | //   bool   fontFixedPitch () const  TODO | 
|---|
| 570 | } | 
|---|
| 571 | if (format.hasProperty(QTextFormat::TextUnderlineStyle)) { | 
|---|
| 572 | QString value; | 
|---|
| 573 | switch (format.underlineStyle()) { | 
|---|
| 574 | case QTextCharFormat::NoUnderline: value = QString::fromLatin1("none"); break; | 
|---|
| 575 | case QTextCharFormat::SingleUnderline: value = QString::fromLatin1("solid"); break; | 
|---|
| 576 | case QTextCharFormat::DashUnderline: value = QString::fromLatin1("dash"); break; | 
|---|
| 577 | case QTextCharFormat::DotLine: value = QString::fromLatin1("dotted"); break; | 
|---|
| 578 | case QTextCharFormat::DashDotLine: value = QString::fromLatin1("dash-dot"); break; | 
|---|
| 579 | case QTextCharFormat::DashDotDotLine: value = QString::fromLatin1("dot-dot-dash"); break; | 
|---|
| 580 | case QTextCharFormat::WaveUnderline: value = QString::fromLatin1("wave"); break; | 
|---|
| 581 | case QTextCharFormat::SpellCheckUnderline: value = QString::fromLatin1("none"); break; | 
|---|
| 582 | } | 
|---|
| 583 | writer.writeAttribute(styleNS, QString::fromLatin1("text-underline-style"), value); | 
|---|
| 584 | } | 
|---|
| 585 | if (format.hasProperty(QTextFormat::TextVerticalAlignment)) { | 
|---|
| 586 | QString value; | 
|---|
| 587 | switch (format.verticalAlignment()) { | 
|---|
| 588 | case QTextCharFormat::AlignMiddle: | 
|---|
| 589 | case QTextCharFormat::AlignNormal: value = QString::fromLatin1("0%"); break; | 
|---|
| 590 | case QTextCharFormat::AlignSuperScript: value = QString::fromLatin1("super"); break; | 
|---|
| 591 | case QTextCharFormat::AlignSubScript: value = QString::fromLatin1("sub"); break; | 
|---|
| 592 | case QTextCharFormat::AlignTop: value = QString::fromLatin1("100%"); break; | 
|---|
| 593 | case QTextCharFormat::AlignBottom : value = QString::fromLatin1("-100%"); break; | 
|---|
| 594 | } | 
|---|
| 595 | writer.writeAttribute(styleNS, QString::fromLatin1("text-position"), value); | 
|---|
| 596 | } | 
|---|
| 597 | if (format.hasProperty(QTextFormat::TextOutline)) | 
|---|
| 598 | writer.writeAttribute(styleNS, QString::fromLatin1("text-outline"), QString::fromLatin1("true")); | 
|---|
| 599 | if (format.hasProperty(QTextFormat::TextToolTip)) { | 
|---|
| 600 | //   QString   toolTip () const  TODO | 
|---|
| 601 | } | 
|---|
| 602 | if (format.hasProperty(QTextFormat::IsAnchor)) { | 
|---|
| 603 | //   bool   isAnchor () const  TODO | 
|---|
| 604 | } | 
|---|
| 605 | if (format.hasProperty(QTextFormat::AnchorHref)) { | 
|---|
| 606 | //   QString   anchorHref () const  TODO | 
|---|
| 607 | } | 
|---|
| 608 | if (format.hasProperty(QTextFormat::AnchorName)) { | 
|---|
| 609 | //   QString   anchorName () const  TODO | 
|---|
| 610 | } | 
|---|
| 611 | if (format.hasProperty(QTextFormat::ForegroundBrush)) { | 
|---|
| 612 | QBrush brush = format.foreground(); | 
|---|
| 613 | // TODO | 
|---|
| 614 | writer.writeAttribute(foNS, QString::fromLatin1("color"), brush.color().name()); | 
|---|
| 615 | } | 
|---|
| 616 |  | 
|---|
| 617 | writer.writeEndElement(); // style | 
|---|
| 618 | } | 
|---|
| 619 |  | 
|---|
| 620 | void QTextOdfWriter::writeListFormat(QXmlStreamWriter &writer, QTextListFormat format, int formatIndex) const | 
|---|
| 621 | { | 
|---|
| 622 | writer.writeStartElement(textNS, QString::fromLatin1("list-style")); | 
|---|
| 623 | writer.writeAttribute(styleNS, QString::fromLatin1("name"), QString::fromLatin1("L%1").arg(formatIndex)); | 
|---|
| 624 |  | 
|---|
| 625 | QTextListFormat::Style style = format.style(); | 
|---|
| 626 | if (style == QTextListFormat::ListDecimal || style == QTextListFormat::ListLowerAlpha | 
|---|
| 627 | || style == QTextListFormat::ListUpperAlpha) { | 
|---|
| 628 | writer.writeStartElement(textNS, QString::fromLatin1("list-level-style-number")); | 
|---|
| 629 | writer.writeAttribute(styleNS, QString::fromLatin1("num-format"), bulletChar(style)); | 
|---|
| 630 | writer.writeAttribute(styleNS, QString::fromLatin1("num-suffix"), QString::fromLatin1(".")); | 
|---|
| 631 | } else { | 
|---|
| 632 | writer.writeStartElement(textNS, QString::fromLatin1("list-level-style-bullet")); | 
|---|
| 633 | writer.writeAttribute(textNS, QString::fromLatin1("bullet-char"), bulletChar(style)); | 
|---|
| 634 | } | 
|---|
| 635 |  | 
|---|
| 636 | writer.writeAttribute(textNS, QString::fromLatin1("level"), QString::number(format.indent())); | 
|---|
| 637 | writer.writeEmptyElement(styleNS, QString::fromLatin1("list-level-properties")); | 
|---|
| 638 | writer.writeAttribute(foNS, QString::fromLatin1("text-align"), QString::fromLatin1("start")); | 
|---|
| 639 | QString spacing = QString::fromLatin1("%1mm").arg(format.indent() * 8); | 
|---|
| 640 | writer.writeAttribute(textNS, QString::fromLatin1("space-before"), spacing); | 
|---|
| 641 | //writer.writeAttribute(textNS, QString::fromLatin1("min-label-width"), spacing); | 
|---|
| 642 |  | 
|---|
| 643 | writer.writeEndElement(); // list-level-style-* | 
|---|
| 644 | writer.writeEndElement(); // list-style | 
|---|
| 645 | } | 
|---|
| 646 |  | 
|---|
| 647 | void QTextOdfWriter::writeFrameFormat(QXmlStreamWriter &writer, QTextFrameFormat format, int formatIndex) const | 
|---|
| 648 | { | 
|---|
| 649 | writer.writeStartElement(styleNS, QString::fromLatin1("style")); | 
|---|
| 650 | writer.writeAttribute(styleNS, QString::fromLatin1("name"), QString::fromLatin1("s%1").arg(formatIndex)); | 
|---|
| 651 | writer.writeAttribute(styleNS, QString::fromLatin1("family"), QString::fromLatin1("section")); | 
|---|
| 652 | writer.writeEmptyElement(styleNS, QString::fromLatin1("section-properties")); | 
|---|
| 653 | if (format.hasProperty(QTextFormat::BlockTopMargin)) | 
|---|
| 654 | writer.writeAttribute(foNS, QString::fromLatin1("margin-top"), pixelToPoint(qMax(qreal(0.), format.topMargin())) ); | 
|---|
| 655 | if (format.hasProperty(QTextFormat::BlockBottomMargin)) | 
|---|
| 656 | writer.writeAttribute(foNS, QString::fromLatin1("margin-bottom"), pixelToPoint(qMax(qreal(0.), format.bottomMargin())) ); | 
|---|
| 657 | if (format.hasProperty(QTextFormat::BlockLeftMargin)) | 
|---|
| 658 | writer.writeAttribute(foNS, QString::fromLatin1("margin-left"), pixelToPoint(qMax(qreal(0.), format.leftMargin())) ); | 
|---|
| 659 | if (format.hasProperty(QTextFormat::BlockRightMargin)) | 
|---|
| 660 | writer.writeAttribute(foNS, QString::fromLatin1("margin-right"), pixelToPoint(qMax(qreal(0.), format.rightMargin())) ); | 
|---|
| 661 |  | 
|---|
| 662 | writer.writeEndElement(); // style | 
|---|
| 663 |  | 
|---|
| 664 | // TODO consider putting the following properties in a qt-namespace. | 
|---|
| 665 | // Position   position () const | 
|---|
| 666 | // qreal   border () const | 
|---|
| 667 | // QBrush   borderBrush () const | 
|---|
| 668 | // BorderStyle   borderStyle () const | 
|---|
| 669 | // qreal   padding () const | 
|---|
| 670 | // QTextLength   width () const | 
|---|
| 671 | // QTextLength   height () const | 
|---|
| 672 | // PageBreakFlags   pageBreakPolicy () const | 
|---|
| 673 | } | 
|---|
| 674 |  | 
|---|
| 675 | void QTextOdfWriter::writeTableCellFormat(QXmlStreamWriter &writer, QTextTableCellFormat format, int formatIndex) const | 
|---|
| 676 | { | 
|---|
| 677 | writer.writeStartElement(styleNS, QString::fromLatin1("style")); | 
|---|
| 678 | writer.writeAttribute(styleNS, QString::fromLatin1("name"), QString::fromLatin1("T%1").arg(formatIndex)); | 
|---|
| 679 | writer.writeAttribute(styleNS, QString::fromLatin1("family"), QString::fromLatin1("table")); | 
|---|
| 680 | writer.writeEmptyElement(styleNS, QString::fromLatin1("table-properties")); | 
|---|
| 681 |  | 
|---|
| 682 |  | 
|---|
| 683 | qreal padding = format.topPadding(); | 
|---|
| 684 | if (padding > 0 && padding == format.bottomPadding() | 
|---|
| 685 | && padding == format.leftPadding() && padding == format.rightPadding()) { | 
|---|
| 686 | writer.writeAttribute(foNS, QString::fromLatin1("padding"), pixelToPoint(padding)); | 
|---|
| 687 | } | 
|---|
| 688 | else { | 
|---|
| 689 | if (padding > 0) | 
|---|
| 690 | writer.writeAttribute(foNS, QString::fromLatin1("padding-top"), pixelToPoint(padding)); | 
|---|
| 691 | if (format.bottomPadding() > 0) | 
|---|
| 692 | writer.writeAttribute(foNS, QString::fromLatin1("padding-top"), pixelToPoint(format.bottomPadding())); | 
|---|
| 693 | if (format.leftPadding() > 0) | 
|---|
| 694 | writer.writeAttribute(foNS, QString::fromLatin1("padding-top"), pixelToPoint(format.leftPadding())); | 
|---|
| 695 | if (format.rightPadding() > 0) | 
|---|
| 696 | writer.writeAttribute(foNS, QString::fromLatin1("padding-top"), pixelToPoint(format.rightPadding())); | 
|---|
| 697 | } | 
|---|
| 698 |  | 
|---|
| 699 | if (format.hasProperty(QTextFormat::TextVerticalAlignment)) { | 
|---|
| 700 | QString pos; | 
|---|
| 701 | switch (format.verticalAlignment()) { | 
|---|
| 702 | case QTextCharFormat::AlignMiddle: | 
|---|
| 703 | pos = QString::fromLatin1("middle"); break; | 
|---|
| 704 | case QTextCharFormat::AlignTop: | 
|---|
| 705 | pos = QString::fromLatin1("top"); break; | 
|---|
| 706 | case QTextCharFormat::AlignBottom: | 
|---|
| 707 | pos = QString::fromLatin1("bottom"); break; | 
|---|
| 708 | default: | 
|---|
| 709 | pos = QString::fromLatin1("automatic"); break; | 
|---|
| 710 | } | 
|---|
| 711 | writer.writeAttribute(styleNS, QString::fromLatin1("vertical-align"), pos); | 
|---|
| 712 | } | 
|---|
| 713 |  | 
|---|
| 714 | // TODO | 
|---|
| 715 | // ODF just search for style-table-cell-properties-attlist) | 
|---|
| 716 | // QTextFormat::BackgroundImageUrl | 
|---|
| 717 | // format.background | 
|---|
| 718 | // QTextFormat::FrameBorder | 
|---|
| 719 |  | 
|---|
| 720 | writer.writeEndElement(); // style | 
|---|
| 721 | } | 
|---|
| 722 |  | 
|---|
| 723 | /////////////////////// | 
|---|
| 724 |  | 
|---|
| 725 | QTextOdfWriter::QTextOdfWriter(const QTextDocument &document, QIODevice *device) | 
|---|
| 726 | : officeNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:office:1.0")), | 
|---|
| 727 | textNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:text:1.0")), | 
|---|
| 728 | styleNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:style:1.0")), | 
|---|
| 729 | foNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0")), | 
|---|
| 730 | tableNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:table:1.0")), | 
|---|
| 731 | drawNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:drawing:1.0")), | 
|---|
| 732 | xlinkNS (QLatin1String("http://www.w3.org/1999/xlink")), | 
|---|
| 733 | svgNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0")), | 
|---|
| 734 | m_document(&document), | 
|---|
| 735 | m_device(device), | 
|---|
| 736 | m_strategy(0), | 
|---|
| 737 | m_codec(0), | 
|---|
| 738 | m_createArchive(true) | 
|---|
| 739 | { | 
|---|
| 740 | } | 
|---|
| 741 |  | 
|---|
| 742 | bool QTextOdfWriter::writeAll() | 
|---|
| 743 | { | 
|---|
| 744 | if (m_createArchive) | 
|---|
| 745 | m_strategy = new QZipStreamStrategy(m_device); | 
|---|
| 746 | else | 
|---|
| 747 | m_strategy = new QXmlStreamStrategy(m_device); | 
|---|
| 748 |  | 
|---|
| 749 | if (!m_device->isWritable() && ! m_device->open(QIODevice::WriteOnly)) { | 
|---|
| 750 | qWarning() << "QTextOdfWriter::writeAll: the device can not be opened for writing"; | 
|---|
| 751 | return false; | 
|---|
| 752 | } | 
|---|
| 753 | QXmlStreamWriter writer(m_strategy->contentStream); | 
|---|
| 754 | #ifndef QT_NO_TEXTCODEC | 
|---|
| 755 | if (m_codec) | 
|---|
| 756 | writer.setCodec(m_codec); | 
|---|
| 757 | #endif | 
|---|
| 758 | // prettyfy | 
|---|
| 759 | writer.setAutoFormatting(true); | 
|---|
| 760 | writer.setAutoFormattingIndent(2); | 
|---|
| 761 |  | 
|---|
| 762 | writer.writeNamespace(officeNS, QString::fromLatin1("office")); | 
|---|
| 763 | writer.writeNamespace(textNS, QString::fromLatin1("text")); | 
|---|
| 764 | writer.writeNamespace(styleNS, QString::fromLatin1("style")); | 
|---|
| 765 | writer.writeNamespace(foNS, QString::fromLatin1("fo")); | 
|---|
| 766 | writer.writeNamespace(tableNS, QString::fromLatin1("table")); | 
|---|
| 767 | writer.writeNamespace(drawNS, QString::fromLatin1("draw")); | 
|---|
| 768 | writer.writeNamespace(xlinkNS, QString::fromLatin1("xlink")); | 
|---|
| 769 | writer.writeNamespace(svgNS, QString::fromLatin1("svg")); | 
|---|
| 770 | writer.writeStartDocument(); | 
|---|
| 771 | writer.writeStartElement(officeNS, QString::fromLatin1("document-content")); | 
|---|
| 772 |  | 
|---|
| 773 | // add fragments. (for character formats) | 
|---|
| 774 | QTextDocumentPrivate::FragmentIterator fragIt = m_document->docHandle()->begin(); | 
|---|
| 775 | QSet<int> formats; | 
|---|
| 776 | while (fragIt != m_document->docHandle()->end()) { | 
|---|
| 777 | const QTextFragmentData * const frag = fragIt.value(); | 
|---|
| 778 | formats << frag->format; | 
|---|
| 779 | ++fragIt; | 
|---|
| 780 | } | 
|---|
| 781 |  | 
|---|
| 782 | // add blocks (for blockFormats) | 
|---|
| 783 | QTextDocumentPrivate::BlockMap &blocks = m_document->docHandle()->blockMap(); | 
|---|
| 784 | QTextDocumentPrivate::BlockMap::Iterator blockIt = blocks.begin(); | 
|---|
| 785 | while (blockIt != blocks.end()) { | 
|---|
| 786 | const QTextBlockData * const block = blockIt.value(); | 
|---|
| 787 | formats << block->format; | 
|---|
| 788 | ++blockIt; | 
|---|
| 789 | } | 
|---|
| 790 |  | 
|---|
| 791 | // add objects for lists, frames and tables | 
|---|
| 792 | QVector<QTextFormat> allFormats = m_document->allFormats(); | 
|---|
| 793 | QList<int> copy = formats.toList(); | 
|---|
| 794 | for (QList<int>::Iterator iter = copy.begin(); iter != copy.end(); ++iter) { | 
|---|
| 795 | QTextObject *object = m_document->objectForFormat(allFormats[*iter]); | 
|---|
| 796 | if (object) | 
|---|
| 797 | formats << object->formatIndex(); | 
|---|
| 798 | } | 
|---|
| 799 |  | 
|---|
| 800 | writeFormats(writer, formats); | 
|---|
| 801 |  | 
|---|
| 802 | writer.writeStartElement(officeNS, QString::fromLatin1("body")); | 
|---|
| 803 | writer.writeStartElement(officeNS, QString::fromLatin1("text")); | 
|---|
| 804 | QTextFrame *rootFrame = m_document->rootFrame(); | 
|---|
| 805 | writeFrame(writer, rootFrame); | 
|---|
| 806 | writer.writeEndElement(); // text | 
|---|
| 807 | writer.writeEndElement(); // body | 
|---|
| 808 | writer.writeEndElement(); // document-content | 
|---|
| 809 | writer.writeEndDocument(); | 
|---|
| 810 | delete m_strategy; | 
|---|
| 811 | m_strategy = 0; | 
|---|
| 812 |  | 
|---|
| 813 | return true; | 
|---|
| 814 | } | 
|---|
| 815 |  | 
|---|
| 816 | QT_END_NAMESPACE | 
|---|
| 817 |  | 
|---|
| 818 | #endif // QT_NO_TEXTODFWRITER | 
|---|