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

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