Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qtextdocumentwriter.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#include <QtCore/qfile.h>
6#include <QtCore/qbytearray.h>
7#include <QtCore/qfileinfo.h>
8#include <QtCore/qtextstream.h>
9#include <QtCore/qdebug.h>
10#include "qtextdocument.h"
12
14#include "qtextodfwriter_p.h"
15#if QT_CONFIG(textmarkdownwriter)
17#endif
18
19#include <algorithm>
20
22
35
72
82
93
105
110{
111 if (d->deleteDevice)
112 delete d->device;
113 delete d;
114}
115
131
138{
139 return d->format;
140}
141
157{
158 if (d->device && d->deleteDevice)
159 delete d->device;
160
161 d->device = device;
162 d->deleteDevice = false;
163}
164
170{
171 return d->device;
172}
173
183{
185 d->deleteDevice = true;
186}
187
196{
197 QFile *file = qobject_cast<QFile *>(d->device);
198 return file ? file->fileName() : QString();
199}
200
206{
207 if (!d->device)
208 return false;
209
210 QByteArray suffix;
211 if (d->format.isEmpty()) {
212 // if there's no format, see if device is a file, and if so, find
213 // the file suffix
214 if (QFile *file = qobject_cast<QFile *>(d->device))
215 suffix = QFileInfo(file->fileName()).suffix().toLower().toLatin1();
216 }
217
218 QByteArray format = !d->format.isEmpty() ? d->format.toLower() : suffix;
219
220#ifndef QT_NO_TEXTODFWRITER
221 if (format == "odf" || format == "opendocumentformat" || format == "odt") {
222 QTextOdfWriter writer(*document, d->device);
223 return writer.writeAll();
224 }
225#endif // QT_NO_TEXTODFWRITER
226
227#if QT_CONFIG(textmarkdownwriter)
228 if (format == "md" || format == "mkd" || format == "markdown") {
229 if (!d->device->isWritable() && !d->device->open(QIODevice::WriteOnly)) {
230 qWarning("QTextDocumentWriter::write: the device can not be opened for writing");
231 return false;
232 }
233 QTextStream s(d->device);
234 QTextMarkdownWriter writer(s, QTextDocument::MarkdownDialectGitHub);
235 return writer.writeAll(document);
236 }
237#endif // textmarkdownwriter
238
239#ifndef QT_NO_TEXTHTMLPARSER
240 if (format == "html" || format == "htm") {
241 if (!d->device->isWritable() && ! d->device->open(QIODevice::WriteOnly)) {
242 qWarning("QTextDocumentWriter::write: the device cannot be opened for writing");
243 return false;
244 }
245 d->device->write(document->toHtml().toUtf8());
246 d->device->close();
247 return true;
248 }
249#endif
250 if (format == "txt" || format == "plaintext") {
251 if (!d->device->isWritable() && ! d->device->open(QIODevice::WriteOnly)) {
252 qWarning("QTextDocumentWriter::write: the device cannot be opened for writing");
253 return false;
254 }
255 d->device->write(document->toPlainText().toUtf8());
256 d->device->close();
257 return true;
258 }
259
260 return false;
261}
262
268{
269 if (fragment.d == nullptr)
270 return false; // invalid fragment.
271 QTextDocument *doc = fragment.d->doc;
272 if (doc)
273 return write(doc);
274 return false;
275}
276
293{
294 QList<QByteArray> answer;
295 answer << "plaintext";
296
297#ifndef QT_NO_TEXTHTMLPARSER
298 answer << "HTML";
299#endif
300#ifndef QT_NO_TEXTODFWRITER
301 answer << "ODF";
302#endif // QT_NO_TEXTODFWRITER
303#if QT_CONFIG(textmarkdownwriter)
304 answer << "markdown";
305#endif
306
307 std::sort(answer.begin(), answer.end());
308 return answer;
309}
310
IOBluetoothDevice * device
\inmodule QtCore
Definition qbytearray.h:57
bool isEmpty() const noexcept
Returns true if the byte array has size 0; otherwise returns false.
Definition qbytearray.h:107
QByteArray toLower() const &
Definition qbytearray.h:254
QString suffix() const
Returns the suffix (extension) of the file.
\inmodule QtCore
Definition qfile.h:93
QString fileName() const override
Returns the name set by setFileName() or to the QFile constructors.
Definition qfile.cpp:277
\inmodule QtCore \reentrant
Definition qiodevice.h:34
virtual bool open(QIODeviceBase::OpenMode mode)
Opens the device and sets its OpenMode to mode.
qint64 write(const char *data, qint64 len)
Writes at most maxSize bytes of data from data to the device.
bool isWritable() const
Returns true if data can be written to the device; otherwise returns false.
virtual void close()
First emits aboutToClose(), then closes the device and sets its OpenMode to NotOpen.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QByteArray toLatin1() const &
Definition qstring.h:630
QString toLower() const &
Definition qstring.h:435
QTextDocumentWriterPrivate(QTextDocumentWriter *qq)
The QTextDocumentWriter class provides a format-independent interface for writing a QTextDocument to ...
void setFormat(const QByteArray &format)
Sets the format used to write documents to the format specified.
QString fileName() const
If the currently assigned device is a QFile, or if setFileName() has been called, this function retur...
QByteArray format() const
Returns the format used for writing documents.
bool write(const QTextDocument *document)
Writes the given document to the assigned device or file and returns true if successful; otherwise re...
void setDevice(QIODevice *device)
Sets the writer's device to the device specified.
static QList< QByteArray > supportedDocumentFormats()
Returns the list of document formats supported by QTextDocumentWriter.
void setFileName(const QString &fileName)
Sets the name of the file to be written to fileName.
QTextDocumentWriter()
Constructs an empty QTextDocumentWriter object.
~QTextDocumentWriter()
Destroys the QTextDocumentWriter object.
QIODevice * device() const
Returns the device currently assigned, or \nullptr if no device has been assigned.
\reentrant \inmodule QtGui
\inmodule QtCore
#define this
Definition dialogs.cpp:9
Combined button and popup list for selecting options.
#define qWarning
Definition qlogging.h:166
GLint GLsizei GLsizei GLenum format
GLdouble s
[6]
Definition qopenglext.h:235
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
QFile file
[0]
QObject::connect nullptr