libodsstream
Library for mass spectrometry
Loading...
Searching...
No Matches
odsdocwriter.cpp
Go to the documentation of this file.
1/*
2 libodsstream is a library to read and write ODS documents as streams
3 Copyright (C) 2013 Olivier Langella <Olivier.Langella@moulon.inra.fr>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18*/
19
20#include "odsdocwriter.h"
21#include "odsexception.h"
25
26#include <quazipfile.h>
27#include <quacrc32.h>
28#include <QDebug>
29#include <QFileInfo>
30
31OdsDocWriter::OdsDocWriter(const QString &filename)
32{
33 _p_device_delete = true;
34 QFile *p_file = new QFile(filename);
35 try
36 {
37 openDevice(p_file);
38 }
39 catch(const OdsException &ods_error)
40 {
41 throw OdsException(
42 QObject::tr("error opening ODS file. check file permission for %1")
43 .arg(QFileInfo(filename).absoluteFilePath()));
44 }
45}
46
48{
49 openDevice(device);
50}
51
52void
53OdsDocWriter::openDevice(QIODevice *device)
54{
55 //_p_device_delete = false;
56 _p_device = device;
57 _p_quaZip = new QuaZip(device);
58 bool open_ok = _p_quaZip->open(QuaZip::mdCreate);
59
60 if(open_ok == false)
61 {
62 throw OdsException(QObject::tr("error opening ODS file."));
63 }
64 // QuaZipFile::open(): file open mode 2 incompatible with ZIP open mode 0
65 // QuaZip::close(): ZIP is not open
66
67
68 //_p_quaZip->setComment("application/vnd.oasis.opendocument.spreadsheet");
69 QByteArray *messageIn = new QByteArray();
70 *messageIn = "application/vnd.oasis.opendocument.spreadsheet";
71
72 QuaZipFile outFile(_p_quaZip);
73 QuaCrc32 crc32;
74 quint32 crc(crc32.calculate(*messageIn));
75 QuaZipNewInfo info("mimetype");
76 info.uncompressedSize = messageIn->length();
77 outFile.open(QIODevice::WriteOnly, info, NULL, crc, 0, 0, true);
78 outFile.write(*messageIn);
79 outFile.close();
80
81 delete(messageIn);
82
83 ManifestXml manifest(_p_quaZip);
84 MetaXml metaxml(_p_quaZip);
86}
87
88
90{
91 // this->close();
92 if(_p_content != nullptr)
93 {
94 this->close();
95 }
96}
97
98/**
99 * ends the document creation. It is required to obtain a correct output
100 *
101 */
102void
104{
105 qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
106 if(_p_content != nullptr)
107 {
108 _p_content->close();
109 delete _p_content;
110 }
111 StylesXml stylesXml(_p_quaZip);
113 _p_quaZip->close();
114 delete _p_quaZip;
115
116 if(_p_device != nullptr)
117 {
118 //_p_device->flush();
119 _p_device->close();
120 }
121 _p_content = nullptr;
122 _p_quaZip = nullptr;
123 // zipOutputStream = null;
124
125 if(_p_device != nullptr)
126 {
128 {
129 qDebug() << "_p_device_delete";
130 delete _p_device;
131 }
132 }
133}
134
135
136/**
137 * adds a new datasheet to the document
138 *
139 * @param sheetName
140 * @throws XMLStreamException
141 */
142void
143OdsDocWriter::writeSheet(const QString &sheetName)
144{
145 _p_content->writeSheet(sheetName);
146}
147
148/**
149 * adds a new line to the document
150 *
151 * @throws XMLStreamException
152 */
153void
158
159
160void
165/**
166 * adds a new text cell to the document
167 *
168 * @param text
169 * @throws XMLStreamException
170 */
171void
172OdsDocWriter::writeCell(const QString &text)
173{
176 // if (text == null) {
177 // this.writeEmptyCell();
178 // return;
179 // }
180 // this.content.writeCell(text);
181}
182
183void
184OdsDocWriter::writeCell(const char *text)
185{
186 QString textQt(text);
189 // if (text == null) {
190 // this.writeEmptyCell();
191 // return;
192 // }
193 // this.content.writeCell(text);
194}
195
196/**
197 * writes an empty cell
198 *
199 * @throws XMLStreamException
200 */
201void
203{
206 // this.content.writeCell("");
207}
208
209/**
210 * adds a new cell containing an integer
211 *
212 * @param value
213 * @throws XMLStreamException
214 */
215void
216OdsDocWriter::writeCell(std::size_t value)
217{
220 // this.content.writeCell(value);
221}
222
223
224/**
225 * adds a new cell containing an integer
226 *
227 * @param value
228 * @throws XMLStreamException
229 */
230void
232{
235 // this.content.writeCell(value);
236}
237
238/**
239 * adds a new cell containing a float
240 *
241 * @param value
242 * @throws XMLStreamException
243 */
244void
246{
249 // this.content.writeCell(value);
250}
251
252/**
253 * adds a new cell containing a double
254 *
255 * @param value
256 * @throws XMLStreamException
257 */
258void
260{
263 // this.content.writeCell(value);
264}
265
266
267/**
268 * adds a new cell containing a percentage
269 *
270 * @param value
271 * @throws XMLStreamException
272 */
273void
279
280/**
281 * adds a new cell containing a boolean
282 *
283 * @param value
284 * @throws XMLStreamException
285 */
286void
288{
291 // this.content.writeCell(value);
292}
293
294/**
295 * adds a new cell containing a date
296 *
297 * @param date
298 * @throws XMLStreamException
299 * @throws DatatypeConfigurationException
300 */
301void
302OdsDocWriter::writeCell(const QDate &date)
303{
306 // if (date == null) {
307 // this.writeEmptyCell();
308 // return;
309 // }
310 // this.content.writeCell(date);
311}
312
313/**
314 * adds a new cell containing a timestamp
315 *
316 * @param date
317 * @throws XMLStreamException
318 * @throws DatatypeConfigurationException
319 */
320void
321OdsDocWriter::writeCell(const QDateTime &date)
322{
325 // if (date == null) {
326 // this.writeEmptyCell();
327 // return;
328 // }
329 // this.content.writeCell(new Date(date.getTime()));
330}
331
332/**
333 * NOT WORKING, adds a new cell containing a duration
334 *
335 * @param duration
336 * @throws XMLStreamException
337 * @throws DatatypeConfigurationException
338 */
339// void OdsDocWriter::writeCell(Duration duration) {
340// if (duration == null) {
341// this.writeEmptyCell();
342// return;
343// }
344// this.content.writeCell(duration);
345// }
346
347/**
348 * adds a new cell containing a text inside an href link
349 *
350 * @param theUri
351 * @param text
352 * @throws XMLStreamException
353 */
354void
355OdsDocWriter::writeCell(const QUrl &theUri, const QString &text)
356{
357 _p_content->writeCell(theUri, text, _next_annotation);
359}
360
361
362/**
363 * get a table cell style reference with a style definition
364 *
365 * @param style OdsTableCellStyle
366 * @return OdsTableCellStyleRef pointer on a style reference
367 */
374
375void
380
381
382void
383OdsDocWriter::setCellAnnotation(const QString &annotation)
384{
385 _next_annotation = annotation;
386}
387
388void
390{
391 _p_content->addColorScale(ods_color_scale);
392}
393
394
395QString
400
401void
void writeLine()
void writeCell(double value, const QString &annotation)
void writeCellPercentage(double value, const QString &annotation)
OdsTableCellStyleRef getTableCellStyleRef(const OdsTableCellStyle &style)
void writeEmptyCell(const QString &annotation)
void addColorScale(const OdsColorScale &ods_color_scale)
void setTableCellStyleRef(OdsTableCellStyleRef style_ref)
void writeSheet(const QString &sheetName)
QString getCellCoordinate()
SettingsXml _settings_xml
void clearAnnotation()
void writeSheet(const QString &sheetName) override
QuaZip * _p_quaZip
QIODevice * _p_device
bool _p_device_delete
QString getOdsCellCoordinate() override
get the last written cell coordinate in ODS coordinate format get the coordinate of the last written ...
void writeEmptyCell() override
void writeCellPercentage(double value) override
void close() override
void setCellAnnotation(const QString &annotation) override
set annotation to write in the next cell
ContentXml * _p_content
void openDevice(QIODevice *device)
QString _next_annotation
OdsTableCellStyleRef getTableCellStyleRef(const OdsTableCellStyle &style) override
void writeCell(const char *) override
write a text cell
void addColorScale(const OdsColorScale &ods_color_scale) override
apply solor scale conditional format on a cell range
void setTableCellStyleRef(OdsTableCellStyleRef style_ref) override
void setCurrentOdsTableSettings(const OdsTableSettings &settings) override
set ODS table settings of the current sheet (table)
virtual ~OdsDocWriter()
void writeLine() override
OdsDocWriter(const QString &filename)
void setCurrentOdsTableSettings(const OdsTableSettings &settings)
void write(QuaZip *p_quazip)