Qt 4.8
qtextdocumentwriter.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include "qtextdocumentwriter.h"
42 
43 #include <QtCore/qfile.h>
44 #include <QtCore/qbytearray.h>
45 #include <QtCore/qfileinfo.h>
46 #include <QtCore/qtextcodec.h>
47 #include <QtCore/qtextstream.h>
48 #include <QtCore/qdebug.h>
49 #include "qtextdocument.h"
50 #include "qtextdocumentfragment.h"
51 
53 #include "qtextodfwriter_p.h"
54 
56 
58 {
59 public:
61 
62  // device
66 #ifndef QT_NO_TEXTCODEC
68 #endif
69 
71 };
72 
107  : device(0),
108  deleteDevice(false),
109 #ifndef QT_NO_TEXTCODEC
110  codec(QTextCodec::codecForName("utf-8")),
111 #endif
112  q(qq)
113 {
114 }
115 
122  : d(new QTextDocumentWriterPrivate(this))
123 {
124 }
125 
131  : d(new QTextDocumentWriterPrivate(this))
132 {
133  d->device = device;
134  d->format = format;
135 }
136 
144  : d(new QTextDocumentWriterPrivate(this))
145 {
146  QFile *file = new QFile(fileName);
147  d->device = file;
148  d->deleteDevice = true;
149  d->format = format;
150 }
151 
156 {
157  if (d->deleteDevice)
158  delete d->device;
159  delete d;
160 }
161 
174 {
175  d->format = format;
176 }
177 
184 {
185  return d->format;
186 }
187 
203 {
204  if (d->device && d->deleteDevice)
205  delete d->device;
206 
207  d->device = device;
208  d->deleteDevice = false;
209 }
210 
216 {
217  return d->device;
218 }
219 
227 {
228  setDevice(new QFile(fileName));
229  d->deleteDevice = true;
230 }
231 
240 {
241  QFile *file = qobject_cast<QFile *>(d->device);
242  return file ? file->fileName() : QString();
243 }
244 
250 {
252 
253  if (d->device && d->format.isEmpty()) {
254  // if there's no format, see if device is a file, and if so, find
255  // the file suffix
256  if (QFile *file = qobject_cast<QFile *>(d->device))
257  suffix = QFileInfo(file->fileName()).suffix().toLower().toLatin1();
258  }
259 
261 
262 #ifndef QT_NO_TEXTODFWRITER
263  if (format == "odf" || format == "opendocumentformat" || format == "odt") {
264  QTextOdfWriter writer(*document, d->device);
265 #ifndef QT_NO_TEXTCODEC
266  writer.setCodec(d->codec);
267 #endif
268  return writer.writeAll();
269  }
270 #endif // QT_NO_TEXTODFWRITER
271 
272 #ifndef QT_NO_TEXTHTMLPARSER
273  if (format == "html" || format == "htm") {
274  if (!d->device->isWritable() && ! d->device->open(QIODevice::WriteOnly)) {
275  qWarning() << "QTextDocumentWriter::write: the device can not be opened for writing";
276  return false;
277  }
278  QTextStream ts(d->device);
279 #ifndef QT_NO_TEXTCODEC
280  ts.setCodec(d->codec);
281  ts << document->toHtml(d->codec->name());
282 #endif
283  d->device->close();
284  return true;
285  }
286 #endif
287  if (format == "txt" || format == "plaintext") {
288  if (!d->device->isWritable() && ! d->device->open(QIODevice::WriteOnly)) {
289  qWarning() << "QTextDocumentWriter::write: the device can not be opened for writing";
290  return false;
291  }
292  QTextStream ts(d->device);
293 #ifndef QT_NO_TEXTCODEC
294  ts.setCodec(d->codec);
295 #endif
296  ts << document->toPlainText();
297  d->device->close();
298  return true;
299  }
300 
301  return false;
302 }
303 
309 {
310  if (fragment.d == 0)
311  return false; // invalid fragment.
312  QTextDocument *doc = fragment.d->doc;
313  if (doc)
314  return write(doc);
315  return false;
316 }
317 
324 #ifndef QT_NO_TEXTCODEC
326 {
327  if (codec == 0)
328  codec = QTextCodec::codecForName("UTF-8");
329  Q_ASSERT(codec);
330  d->codec = codec;
331 }
332 #endif
333 
337 #ifndef QT_NO_TEXTCODEC
339 {
340  return d->codec;
341 }
342 #endif
343 
359 {
360  QList<QByteArray> answer;
361  answer << "plaintext";
362 
363 #ifndef QT_NO_TEXTHTMLPARSER
364  answer << "HTML";
365 #endif
366 #ifndef QT_NO_TEXTODFWRITER
367  answer << "ODF";
368 #endif // QT_NO_TEXTODFWRITER
369 
370  qSort(answer);
371  return answer;
372 }
373 
T qobject_cast(QObject *object)
Definition: qobject.h:375
double d
Definition: qnumeric_p.h:62
QString fileName() const
Returns the name set by setFileName() or to the QFile constructors.
Definition: qfile.cpp:470
QString fileName() const
If the currently assigned device is a QFile, or if setFileName() has been called, this function retur...
QTextCodec * codec() const
Returns the codec that is currently assigned to the writer.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
bool isWritable() const
Returns true if data can be written to the device; otherwise returns false.
Definition: qiodevice.cpp:558
virtual void close()
First emits aboutToClose(), then closes the device and sets its OpenMode to NotOpen.
Definition: qiodevice.cpp:590
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
void setCodec(QTextCodec *codec)
Sets the codec for this stream to codec.
QTextDocumentFragmentPrivate * d
bool write(const QTextDocument *document)
Writes the given document to the assigned device or file and returns true if successful; otherwise re...
QByteArray toLower() const
Returns a lowercase copy of the byte array.
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QString toPlainText() const
Returns the plain text contained in the document.
void setCodec(QTextCodec *codec)
Sets the codec for this stream to codec.
QIODevice * device() const
Returns the device currently assigned, or 0 if no device has been assigned.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
Q_CORE_EXPORT void qWarning(const char *,...)
void setFormat(const QByteArray &format)
Sets the format used to write documents to the format specified.
void qSort(RandomAccessIterator start, RandomAccessIterator end)
Definition: qalgorithms.h:177
QTextDocumentWriterPrivate * d
virtual QByteArray name() const =0
QTextCodec subclasses must reimplement this function.
QByteArray format() const
Returns the format used for writing documents.
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
The QTextStream class provides a convenient interface for reading and writing text.
Definition: qtextstream.h:73
QTextDocumentWriter()
Constructs an empty QTextDocumentWriter object.
void setCodec(QTextCodec *codec)
void setDevice(QIODevice *device)
Sets the writer&#39;s device to the device specified.
#define QT_NO_TEXTCODEC
QByteArray suffix
static QTextCodec * codecForName(const QByteArray &name)
Searches all installed QTextCodec objects and returns the one which best matches name; the match is c...
virtual bool open(OpenMode mode)
Opens the device and sets its OpenMode to mode.
Definition: qiodevice.cpp:570
The QTextDocument class holds formatted text that can be viewed and edited using a QTextEdit...
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
The QTextDocumentWriter class provides a format-independent interface for writing a QTextDocument to ...
QTextDocumentWriterPrivate(QTextDocumentWriter *qq)
static QList< QByteArray > supportedDocumentFormats()
Returns the list of document formats supported by QTextDocumentWriter.
The QTextCodec class provides conversions between text encodings.
Definition: qtextcodec.h:62
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
The QIODevice class is the base interface class of all I/O devices in Qt.
Definition: qiodevice.h:66
void setFileName(const QString &fileName)
Sets the name of the file to be written to fileName.
The QTextDocumentFragment class represents a piece of formatted text from a QTextDocument.
QString toHtml(const QByteArray &encoding=QByteArray()) const
Returns a string containing an HTML representation of the document.
~QTextDocumentWriter()
Destroys the QTextDocumentWriter object.