If you having many namenspace use;
QXmlStreamWriter
you declare all namenspace at construct class
( qt 4.5 version code )
#include <QXmlStreamWriter>
This class was introduced in Qt 4.3.
and write faster as dom
textNS
(QLatin1String("urn:oasis:names:tc:opendocument:xmlns:text:1.0")),
styleNS
(QLatin1String("urn:oasis:names:tc:opendocument:xmlns:style:1.0")),
foNS
(QLatin1String("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0")),
tableNS
(QLatin1String("urn:oasis:names:tc:opendocument:xmlns:table:1.0")),
drawNS
(QLatin1String("urn:oasis:names:tc:opendocument:xmlns:drawing:1.0")),
svgNS
(QLatin1String("urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0")),
textNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:text:1.0")),
styleNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:style:1.0")),
foNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0")),
tableNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:table:1.0")),
drawNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:drawing:1.0")),
xlinkNS (QLatin1String("http://www.w3.org/1999/xlink")),
svgNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0")),
To copy to clipboard, switch view to plain text mode
After you only writer.writeStartElement(drawNS, QString::fromLatin1("frame"));
this sample draw a image to openoffice format
void QTextOdfWriter
::writeInlineCharacter(QXmlStreamWriter
&writer,
const QTextFragment &fragment
) const {
writer.
writeStartElement(drawNS,
QString::fromLatin1("frame"));
if (m_strategy == 0) {
// don't do anything.
}
else if (fragment.charFormat().isImageFormat()) {
writer.
writeAttribute(drawNS,
QString::fromLatin1("name"), imageFormat.
name());
// vvv Copy pasted mostly from Qt =================
if (name.
startsWith(QLatin1String(":/"))) // auto-detect resources QUrl url
= QUrl::fromEncoded(name.
toUtf8());
image = qvariant_cast<QImage>(data);
} else if (data.
type() == QVariant::ByteArray) { image.loadFromData(data.toByteArray());
}
if (image.isNull()) {
if (QTextImageHandler::externalLoader)
image = QTextImageHandler::externalLoader(name, context);
if (image.isNull()) { // try direct loading
name = imageFormat.name(); // remove qrc:/ prefix again
image.load(name);
}
}
// ^^^ Copy pasted mostly from Qt =================
if (! image.isNull()) {
imageWriter.write(image);
QString filename
= m_strategy
->createUniqueImageName
();
m_strategy
->addFile
(filename,
QString::fromLatin1("image/png"), imageBytes.
data());
// get the width/height from the format.
qreal width
= (imageFormat.
hasProperty(QTextFormat::ImageWidth)) ? imageFormat.
width() : image.
width();
writer.
writeAttribute(svgNS,
QString::fromLatin1("width"), pixelToPoint
(width
));
qreal height
= (imageFormat.
hasProperty(QTextFormat::ImageHeight)) ? imageFormat.
height() : image.
height();
writer.
writeAttribute(svgNS,
QString::fromLatin1("height"), pixelToPoint
(height
));
writer.
writeStartElement(drawNS,
QString::fromLatin1("image"));
writer.
writeAttribute(xlinkNS,
QString::fromLatin1("href"), filename
);
writer.writeEndElement(); // image
}
}
writer.writeEndElement(); // frame
}
void QTextOdfWriter::writeInlineCharacter(QXmlStreamWriter &writer, const QTextFragment &fragment) const
{
writer.writeStartElement(drawNS, QString::fromLatin1("frame"));
if (m_strategy == 0) {
// don't do anything.
}
else if (fragment.charFormat().isImageFormat()) {
QTextImageFormat imageFormat = fragment.charFormat().toImageFormat();
writer.writeAttribute(drawNS, QString::fromLatin1("name"), imageFormat.name());
// vvv Copy pasted mostly from Qt =================
QImage image;
QString name = imageFormat.name();
if (name.startsWith(QLatin1String(":/"))) // auto-detect resources
name.prepend(QLatin1String("qrc"));
QUrl url = QUrl::fromEncoded(name.toUtf8());
const QVariant data = m_document->resource(QTextDocument::ImageResource, url);
if (data.type() == QVariant::Image) {
image = qvariant_cast<QImage>(data);
} else if (data.type() == QVariant::ByteArray) {
image.loadFromData(data.toByteArray());
}
if (image.isNull()) {
QString context;
if (QTextImageHandler::externalLoader)
image = QTextImageHandler::externalLoader(name, context);
if (image.isNull()) { // try direct loading
name = imageFormat.name(); // remove qrc:/ prefix again
image.load(name);
}
}
// ^^^ Copy pasted mostly from Qt =================
if (! image.isNull()) {
QBuffer imageBytes;
QImageWriter imageWriter(&imageBytes, "png");
imageWriter.write(image);
QString filename = m_strategy->createUniqueImageName();
m_strategy->addFile(filename, QString::fromLatin1("image/png"), imageBytes.data());
// get the width/height from the format.
qreal width = (imageFormat.hasProperty(QTextFormat::ImageWidth)) ? imageFormat.width() : image.width();
writer.writeAttribute(svgNS, QString::fromLatin1("width"), pixelToPoint(width));
qreal height = (imageFormat.hasProperty(QTextFormat::ImageHeight)) ? imageFormat.height() : image.height();
writer.writeAttribute(svgNS, QString::fromLatin1("height"), pixelToPoint(height));
writer.writeStartElement(drawNS, QString::fromLatin1("image"));
writer.writeAttribute(xlinkNS, QString::fromLatin1("href"), filename);
writer.writeEndElement(); // image
}
}
writer.writeEndElement(); // frame
}
To copy to clipboard, switch view to plain text mode
have a look on:
http://fop-miniscribus.googlecode.co...todfwriter.cpp
it begin at bottom....
Bookmarks