Mercurial > clickerconvert
comparison src/xlsx/xlsxcontenttypes.cpp @ 1:93d3106bb9a4
Add qt xlsx library
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Tue, 22 Mar 2016 10:38:08 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:49cd5cc0b072 | 1:93d3106bb9a4 |
---|---|
1 /**************************************************************************** | |
2 ** Copyright (c) 2013-2014 Debao Zhang <hello@debao.me> | |
3 ** All right reserved. | |
4 ** | |
5 ** Permission is hereby granted, free of charge, to any person obtaining | |
6 ** a copy of this software and associated documentation files (the | |
7 ** "Software"), to deal in the Software without restriction, including | |
8 ** without limitation the rights to use, copy, modify, merge, publish, | |
9 ** distribute, sublicense, and/or sell copies of the Software, and to | |
10 ** permit persons to whom the Software is furnished to do so, subject to | |
11 ** the following conditions: | |
12 ** | |
13 ** The above copyright notice and this permission notice shall be | |
14 ** included in all copies or substantial portions of the Software. | |
15 ** | |
16 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
17 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
18 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
19 ** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
20 ** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
21 ** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
22 ** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
23 ** | |
24 ****************************************************************************/ | |
25 #include "xlsxcontenttypes_p.h" | |
26 #include <QXmlStreamWriter> | |
27 #include <QXmlStreamReader> | |
28 #include <QFile> | |
29 #include <QMapIterator> | |
30 #include <QBuffer> | |
31 #include <QDebug> | |
32 | |
33 namespace QXlsx { | |
34 | |
35 ContentTypes::ContentTypes(CreateFlag flag) | |
36 :AbstractOOXmlFile(flag) | |
37 { | |
38 m_package_prefix = QStringLiteral("application/vnd.openxmlformats-package."); | |
39 m_document_prefix = QStringLiteral("application/vnd.openxmlformats-officedocument."); | |
40 | |
41 m_defaults.insert(QStringLiteral("rels"), m_package_prefix + QStringLiteral("relationships+xml")); | |
42 m_defaults.insert(QStringLiteral("xml"), QStringLiteral("application/xml")); | |
43 } | |
44 | |
45 void ContentTypes::addDefault(const QString &key, const QString &value) | |
46 { | |
47 m_defaults.insert(key, value); | |
48 } | |
49 | |
50 void ContentTypes::addOverride(const QString &key, const QString &value) | |
51 { | |
52 m_overrides.insert(key, value); | |
53 } | |
54 | |
55 void ContentTypes::addDocPropApp() | |
56 { | |
57 addOverride(QStringLiteral("/docProps/app.xml"), m_document_prefix + QStringLiteral("extended-properties+xml")); | |
58 } | |
59 | |
60 void ContentTypes::addDocPropCore() | |
61 { | |
62 addOverride(QStringLiteral("/docProps/core.xml"), m_package_prefix + QStringLiteral("core-properties+xml")); | |
63 } | |
64 | |
65 void ContentTypes::addStyles() | |
66 { | |
67 addOverride(QStringLiteral("/xl/styles.xml"), m_document_prefix + QStringLiteral("spreadsheetml.styles+xml")); | |
68 } | |
69 | |
70 void ContentTypes::addTheme() | |
71 { | |
72 addOverride(QStringLiteral("/xl/theme/theme1.xml"), m_document_prefix + QStringLiteral("theme+xml")); | |
73 } | |
74 | |
75 void ContentTypes::addWorkbook() | |
76 { | |
77 addOverride(QStringLiteral("/xl/workbook.xml"), m_document_prefix + QStringLiteral("spreadsheetml.sheet.main+xml")); | |
78 } | |
79 | |
80 void ContentTypes::addWorksheetName(const QString &name) | |
81 { | |
82 addOverride(QStringLiteral("/xl/worksheets/%1.xml").arg(name), m_document_prefix + QStringLiteral("spreadsheetml.worksheet+xml")); | |
83 } | |
84 | |
85 void ContentTypes::addChartsheetName(const QString &name) | |
86 { | |
87 addOverride(QStringLiteral("/xl/chartsheets/%1.xml").arg(name), m_document_prefix + QStringLiteral("spreadsheetml.chartsheet+xml")); | |
88 } | |
89 | |
90 void ContentTypes::addDrawingName(const QString &name) | |
91 { | |
92 addOverride(QStringLiteral("/xl/drawings/%1.xml").arg(name), m_document_prefix + QStringLiteral("drawing+xml")); | |
93 } | |
94 | |
95 void ContentTypes::addChartName(const QString &name) | |
96 { | |
97 addOverride(QStringLiteral("/xl/charts/%1.xml").arg(name), m_document_prefix + QStringLiteral("drawingml.chart+xml")); | |
98 } | |
99 | |
100 void ContentTypes::addCommentName(const QString &name) | |
101 { | |
102 addOverride(QStringLiteral("/xl/%1.xml").arg(name), m_document_prefix + QStringLiteral("spreadsheetml.comments+xml")); | |
103 } | |
104 | |
105 void ContentTypes::addTableName(const QString &name) | |
106 { | |
107 addOverride(QStringLiteral("/xl/tables/%1.xml").arg(name), m_document_prefix + QStringLiteral("spreadsheetml.table+xml")); | |
108 } | |
109 | |
110 void ContentTypes::addExternalLinkName(const QString &name) | |
111 { | |
112 addOverride(QStringLiteral("/xl/externalLinks/%1.xml").arg(name), m_document_prefix + QStringLiteral("spreadsheetml.externalLink+xml")); | |
113 } | |
114 | |
115 void ContentTypes::addSharedString() | |
116 { | |
117 addOverride(QStringLiteral("/xl/sharedStrings.xml"), m_document_prefix + QStringLiteral("spreadsheetml.sharedStrings+xml")); | |
118 } | |
119 | |
120 void ContentTypes::addVmlName() | |
121 { | |
122 addOverride(QStringLiteral("vml"), m_document_prefix + QStringLiteral("vmlDrawing")); | |
123 } | |
124 | |
125 void ContentTypes::addCalcChain() | |
126 { | |
127 addOverride(QStringLiteral("/xl/calcChain.xml"), m_document_prefix + QStringLiteral("spreadsheetml.calcChain+xml")); | |
128 } | |
129 | |
130 void ContentTypes::addVbaProject() | |
131 { | |
132 //:TODO | |
133 addOverride(QStringLiteral("bin"), QStringLiteral("application/vnd.ms-office.vbaProject")); | |
134 } | |
135 | |
136 void ContentTypes::clearOverrides() | |
137 { | |
138 m_overrides.clear(); | |
139 } | |
140 | |
141 void ContentTypes::saveToXmlFile(QIODevice *device) const | |
142 { | |
143 QXmlStreamWriter writer(device); | |
144 | |
145 writer.writeStartDocument(QStringLiteral("1.0"), true); | |
146 writer.writeStartElement(QStringLiteral("Types")); | |
147 writer.writeAttribute(QStringLiteral("xmlns"), QStringLiteral("http://schemas.openxmlformats.org/package/2006/content-types")); | |
148 | |
149 { | |
150 QMapIterator<QString, QString> it(m_defaults); | |
151 while (it.hasNext()) { | |
152 it.next(); | |
153 writer.writeStartElement(QStringLiteral("Default")); | |
154 writer.writeAttribute(QStringLiteral("Extension"), it.key()); | |
155 writer.writeAttribute(QStringLiteral("ContentType"), it.value()); | |
156 writer.writeEndElement();//Default | |
157 } | |
158 } | |
159 | |
160 { | |
161 QMapIterator<QString, QString> it(m_overrides); | |
162 while (it.hasNext()) { | |
163 it.next(); | |
164 writer.writeStartElement(QStringLiteral("Override")); | |
165 writer.writeAttribute(QStringLiteral("PartName"), it.key()); | |
166 writer.writeAttribute(QStringLiteral("ContentType"), it.value()); | |
167 writer.writeEndElement(); //Override | |
168 } | |
169 } | |
170 | |
171 writer.writeEndElement();//Types | |
172 writer.writeEndDocument(); | |
173 | |
174 } | |
175 | |
176 bool ContentTypes::loadFromXmlFile(QIODevice *device) | |
177 { | |
178 m_defaults.clear(); | |
179 m_overrides.clear(); | |
180 | |
181 QXmlStreamReader reader(device); | |
182 while (!reader.atEnd()) { | |
183 QXmlStreamReader::TokenType token = reader.readNext(); | |
184 if (token == QXmlStreamReader::StartElement) { | |
185 if (reader.name() == QLatin1String("Default")) { | |
186 QXmlStreamAttributes attrs = reader.attributes(); | |
187 QString extension = attrs.value(QLatin1String("Extension")).toString(); | |
188 QString type = attrs.value(QLatin1String("ContentType")).toString(); | |
189 m_defaults.insert(extension, type); | |
190 } else if (reader.name() == QLatin1String("Override")) { | |
191 QXmlStreamAttributes attrs = reader.attributes(); | |
192 QString partName = attrs.value(QLatin1String("PartName")).toString(); | |
193 QString type = attrs.value(QLatin1String("ContentType")).toString(); | |
194 m_overrides.insert(partName, type); | |
195 } | |
196 } | |
197 | |
198 if (reader.hasError()) { | |
199 qDebug()<<reader.errorString(); | |
200 } | |
201 } | |
202 return true; | |
203 } | |
204 | |
205 } //namespace QXlsx |