qtextdocumentwriter.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/gui/text/qtextdocumentwriter.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2016 The Qt Company Ltd.-
4** Contact: https://www.qt.io/licensing/-
5**-
6** This file is part of the QtGui module of the Qt Toolkit.-
7**-
8** $QT_BEGIN_LICENSE:LGPL$-
9** Commercial License Usage-
10** Licensees holding valid commercial Qt licenses may use this file in-
11** accordance with the commercial license agreement provided with the-
12** Software or, alternatively, in accordance with the terms contained in-
13** a written agreement between you and The Qt Company. For licensing terms-
14** and conditions see https://www.qt.io/terms-conditions. For further-
15** information use the contact form at https://www.qt.io/contact-us.-
16**-
17** GNU Lesser General Public License Usage-
18** Alternatively, this file may be used under the terms of the GNU Lesser-
19** General Public License version 3 as published by the Free Software-
20** Foundation and appearing in the file LICENSE.LGPL3 included in the-
21** packaging of this file. Please review the following information to-
22** ensure the GNU Lesser General Public License version 3 requirements-
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.-
24**-
25** GNU General Public License Usage-
26** Alternatively, this file may be used under the terms of the GNU-
27** General Public License version 2.0 or (at your option) the GNU General-
28** Public license version 3 or any later version approved by the KDE Free-
29** Qt Foundation. The licenses are as published by the Free Software-
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3-
31** included in the packaging of this file. Please review the following-
32** information to ensure the GNU General Public License requirements will-
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and-
34** https://www.gnu.org/licenses/gpl-3.0.html.-
35**-
36** $QT_END_LICENSE$-
37**-
38****************************************************************************/-
39#include "qtextdocumentwriter.h"-
40-
41#include <QtCore/qfile.h>-
42#include <QtCore/qbytearray.h>-
43#include <QtCore/qfileinfo.h>-
44#include <QtCore/qtextcodec.h>-
45#include <QtCore/qtextstream.h>-
46#include <QtCore/qdebug.h>-
47#include "qtextdocument.h"-
48#include "qtextdocumentfragment.h"-
49-
50#include "qtextdocumentfragment_p.h"-
51#include "qtextodfwriter_p.h"-
52-
53#include <algorithm>-
54-
55QT_BEGIN_NAMESPACE-
56-
57class QTextDocumentWriterPrivate-
58{-
59public:-
60 QTextDocumentWriterPrivate(QTextDocumentWriter* qq);-
61-
62 // device-
63 QByteArray format;-
64 QIODevice *device;-
65 bool deleteDevice;-
66#ifndef QT_NO_TEXTCODEC-
67 QTextCodec *codec;-
68#endif-
69-
70 QTextDocumentWriter *q;-
71};-
72-
73/*!-
74 \since 4.5-
75 \class QTextDocumentWriter-
76-
77 \brief The QTextDocumentWriter class provides a format-independent interface for writing a QTextDocument to files or other devices.-
78 \inmodule QtGui-
79-
80 \ingroup richtext-processing-
81 \ingroup io-
82-
83 To write a document, construct a QTextDocumentWriter object with either a-
84 file name or a device object, and specify the document format to be-
85 written. You can construct a writer and set the format using setFormat()-
86 later.-
87-
88 Call write() to write the document to the device. If the document is-
89 successfully written, this function returns \c true. However, if an error-
90 occurs when writing the document, it will return false.-
91-
92 Call supportedDocumentFormats() for a list of formats that-
93 QTextDocumentWriter can write.-
94-
95 Since the capabilities of the supported output formats vary considerably,-
96 the writer simply outputs the appropriate subset of objects for each format.-
97 This typically includes the formatted text and images contained in a-
98 document.-
99*/-
100-
101/*!-
102 \internal-
103*/-
104QTextDocumentWriterPrivate::QTextDocumentWriterPrivate(QTextDocumentWriter *qq)-
105 : device(0),-
106 deleteDevice(false),-
107#ifndef QT_NO_TEXTCODEC-
108 codec(QTextCodec::codecForName("utf-8")),-
109#endif-
110 q(qq)-
111{-
112}
never executed: end of block
0
113-
114/*!-
115 Constructs an empty QTextDocumentWriter object. Before writing, you must-
116 call setFormat() to set a document format, then setDevice() or-
117 setFileName().-
118*/-
119QTextDocumentWriter::QTextDocumentWriter()-
120 : d(new QTextDocumentWriterPrivate(this))-
121{-
122}
never executed: end of block
0
123-
124/*!-
125 Constructs a QTextDocumentWriter object to write to the given \a device-
126 in the document format specified by \a format.-
127*/-
128QTextDocumentWriter::QTextDocumentWriter(QIODevice *device, const QByteArray &format)-
129 : d(new QTextDocumentWriterPrivate(this))-
130{-
131 d->device = device;-
132 d->format = format;-
133}
never executed: end of block
0
134-
135/*!-
136 Constructs an QTextDocumentWriter object that will write to a file with-
137 the name \a fileName, using the document format specified by \a format.-
138 If \a format is not provided, QTextDocumentWriter will detect the document-
139 format by inspecting the extension of \a fileName.-
140*/-
141QTextDocumentWriter::QTextDocumentWriter(const QString &fileName, const QByteArray &format)-
142 : d(new QTextDocumentWriterPrivate(this))-
143{-
144 QFile *file = new QFile(fileName);-
145 d->device = file;-
146 d->deleteDevice = true;-
147 d->format = format;-
148}
never executed: end of block
0
149-
150/*!-
151 Destroys the QTextDocumentWriter object.-
152*/-
153QTextDocumentWriter::~QTextDocumentWriter()-
154{-
155 if (d->deleteDevice)
d->deleteDeviceDescription
TRUEnever evaluated
FALSEnever evaluated
0
156 delete d->device;
never executed: delete d->device;
0
157 delete d;-
158}
never executed: end of block
0
159-
160/*!-
161 Sets the format used to write documents to the \a format specified.-
162 \a format is a case insensitive text string. For example:-
163-
164 \snippet code/src_gui_text_qtextdocumentwriter.cpp 0-
165-
166 You can call supportedDocumentFormats() for the full list of formats-
167 QTextDocumentWriter supports.-
168-
169 \sa format()-
170*/-
171void QTextDocumentWriter::setFormat (const QByteArray &format)-
172{-
173 d->format = format;-
174}
never executed: end of block
0
175-
176/*!-
177 Returns the format used for writing documents.-
178-
179 \sa setFormat()-
180*/-
181QByteArray QTextDocumentWriter::format () const-
182{-
183 return d->format;
never executed: return d->format;
0
184}-
185-
186/*!-
187 Sets the writer's device to the \a device specified. If a device has-
188 already been set, the old device is removed but otherwise left-
189 unchanged.-
190-
191 If the device is not already open, QTextDocumentWriter will attempt to-
192 open the device in \l QIODevice::WriteOnly mode by calling open().-
193-
194 \note This will not work for certain devices, such as QProcess,-
195 QTcpSocket and QUdpSocket, where some configuration is required before-
196 the device can be opened.-
197-
198 \sa device(), setFileName()-
199*/-
200void QTextDocumentWriter::setDevice (QIODevice *device)-
201{-
202 if (d->device && d->deleteDevice)
d->deviceDescription
TRUEnever evaluated
FALSEnever evaluated
d->deleteDeviceDescription
TRUEnever evaluated
FALSEnever evaluated
0
203 delete d->device;
never executed: delete d->device;
0
204-
205 d->device = device;-
206 d->deleteDevice = false;-
207}
never executed: end of block
0
208-
209/*!-
210 Returns the device currently assigned, or 0 if no device has been-
211 assigned.-
212*/-
213QIODevice *QTextDocumentWriter::device () const-
214{-
215 return d->device;
never executed: return d->device;
0
216}-
217-
218/*!-
219 Sets the name of the file to be written to \a fileName. Internally,-
220 QTextDocumentWriter will create a QFile and open it in \l-
221 QIODevice::WriteOnly mode, and use this file when writing the document.-
222-
223 \sa fileName(), setDevice()-
224*/-
225void QTextDocumentWriter::setFileName (const QString &fileName)-
226{-
227 setDevice(new QFile(fileName));-
228 d->deleteDevice = true;-
229}
never executed: end of block
0
230-
231/*!-
232 If the currently assigned device is a QFile, or if setFileName()-
233 has been called, this function returns the name of the file-
234 to be written to. In all other cases, it returns an empty string.-
235-
236 \sa setFileName(), setDevice()-
237*/-
238QString QTextDocumentWriter::fileName () const-
239{-
240 QFile *file = qobject_cast<QFile *>(d->device);-
241 return file ? file->fileName() : QString();
never executed: return file ? file->fileName() : QString();
0
242}-
243-
244/*!-
245 Writes the given \a document to the assigned device or file and-
246 returns \c true if successful; otherwise returns \c false.-
247*/-
248bool QTextDocumentWriter::write(const QTextDocument *document)-
249{-
250 QByteArray suffix;-
251-
252 if (d->device && d->format.isEmpty()) {
d->deviceDescription
TRUEnever evaluated
FALSEnever evaluated
d->format.isEmpty()Description
TRUEnever evaluated
FALSEnever evaluated
0
253 // if there's no format, see if device is a file, and if so, find-
254 // the file suffix-
255 if (QFile *file = qobject_cast<QFile *>(d->device))
QFile *file = ... *>(d->device)Description
TRUEnever evaluated
FALSEnever evaluated
0
256 suffix = QFileInfo(file->fileName()).suffix().toLower().toLatin1();
never executed: suffix = QFileInfo(file->fileName()).suffix().toLower().toLatin1();
0
257 }
never executed: end of block
0
258-
259 QByteArray format = !d->format.isEmpty() ? d->format.toLower() : suffix;
!d->format.isEmpty()Description
TRUEnever evaluated
FALSEnever evaluated
0
260-
261#ifndef QT_NO_TEXTODFWRITER-
262 if (format == "odf" || format == "opendocumentformat" || format == "odt") {
format == "odf"Description
TRUEnever evaluated
FALSEnever evaluated
format == "opendocumentformat"Description
TRUEnever evaluated
FALSEnever evaluated
format == "odt"Description
TRUEnever evaluated
FALSEnever evaluated
0
263 QTextOdfWriter writer(*document, d->device);-
264#ifndef QT_NO_TEXTCODEC-
265 writer.setCodec(d->codec);-
266#endif-
267 return writer.writeAll();
never executed: return writer.writeAll();
0
268 }-
269#endif // QT_NO_TEXTODFWRITER-
270-
271#ifndef QT_NO_TEXTHTMLPARSER-
272 if (format == "html" || format == "htm") {
format == "html"Description
TRUEnever evaluated
FALSEnever evaluated
format == "htm"Description
TRUEnever evaluated
FALSEnever evaluated
0
273 if (!d->device->isWritable() && ! d->device->open(QIODevice::WriteOnly)) {
!d->device->isWritable()Description
TRUEnever evaluated
FALSEnever evaluated
! d->device->o...ce::WriteOnly)Description
TRUEnever evaluated
FALSEnever evaluated
0
274 qWarning("QTextDocumentWriter::write: the device can not be opened for writing");-
275 return false;
never executed: return false;
0
276 }-
277 QTextStream ts(d->device);-
278#ifndef QT_NO_TEXTCODEC-
279 ts.setCodec(d->codec);-
280 ts << document->toHtml(d->codec->name());-
281#endif-
282 d->device->close();-
283 return true;
never executed: return true;
0
284 }-
285#endif-
286 if (format == "txt" || format == "plaintext") {
format == "txt"Description
TRUEnever evaluated
FALSEnever evaluated
format == "plaintext"Description
TRUEnever evaluated
FALSEnever evaluated
0
287 if (!d->device->isWritable() && ! d->device->open(QIODevice::WriteOnly)) {
!d->device->isWritable()Description
TRUEnever evaluated
FALSEnever evaluated
! d->device->o...ce::WriteOnly)Description
TRUEnever evaluated
FALSEnever evaluated
0
288 qWarning("QTextDocumentWriter::write: the device can not be opened for writing");-
289 return false;
never executed: return false;
0
290 }-
291 QTextStream ts(d->device);-
292#ifndef QT_NO_TEXTCODEC-
293 ts.setCodec(d->codec);-
294#endif-
295 ts << document->toPlainText();-
296 d->device->close();-
297 return true;
never executed: return true;
0
298 }-
299-
300 return false;
never executed: return false;
0
301}-
302-
303/*!-
304 Writes the document fragment specified by \a fragment to the assigned device-
305 or file and returns \c true if successful; otherwise returns \c false.-
306*/-
307bool QTextDocumentWriter::write(const QTextDocumentFragment &fragment)-
308{-
309 if (fragment.d == 0)
fragment.d == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
310 return false; // invalid fragment.
never executed: return false;
0
311 QTextDocument *doc = fragment.d->doc;-
312 if (doc)
docDescription
TRUEnever evaluated
FALSEnever evaluated
0
313 return write(doc);
never executed: return write(doc);
0
314 return false;
never executed: return false;
0
315}-
316-
317/*!-
318 Sets the codec for this stream to \a codec. The codec is used for-
319 encoding any data that is written. By default, QTextDocumentWriter-
320 uses UTF-8.-
321*/-
322-
323#ifndef QT_NO_TEXTCODEC-
324void QTextDocumentWriter::setCodec(QTextCodec *codec)-
325{-
326 if (codec == 0)
codec == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
327 codec = QTextCodec::codecForName("UTF-8");
never executed: codec = QTextCodec::codecForName("UTF-8");
0
328 Q_ASSERT(codec);-
329 d->codec = codec;-
330}
never executed: end of block
0
331#endif-
332-
333/*!-
334 Returns the codec that is currently assigned to the writer.-
335*/-
336#ifndef QT_NO_TEXTCODEC-
337QTextCodec *QTextDocumentWriter::codec() const-
338{-
339 return d->codec;
never executed: return d->codec;
0
340}-
341#endif-
342-
343/*!-
344 Returns the list of document formats supported by QTextDocumentWriter.-
345-
346 By default, Qt can write the following formats:-
347-
348 \table-
349 \header \li Format \li Description-
350 \row \li plaintext \li Plain text-
351 \row \li HTML \li HyperText Markup Language-
352 \row \li ODF \li OpenDocument Format-
353 \endtable-
354-
355 \sa setFormat()-
356*/-
357QList<QByteArray> QTextDocumentWriter::supportedDocumentFormats()-
358{-
359 QList<QByteArray> answer;-
360 answer << "plaintext";-
361-
362#ifndef QT_NO_TEXTHTMLPARSER-
363 answer << "HTML";-
364#endif-
365#ifndef QT_NO_TEXTODFWRITER-
366 answer << "ODF";-
367#endif // QT_NO_TEXTODFWRITER-
368-
369 std::sort(answer.begin(), answer.end());-
370 return answer;
never executed: return answer;
0
371}-
372-
373QT_END_NAMESPACE-
Source codeSwitch to Preprocessed file

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