1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qquicktextdocument.h"
#include "qquicktextdocument_p.h"
#include "qquicktextedit_p.h"
#include "qquicktextedit_p_p.h"
#include "qquicktext_p_p.h"
#include <QtQml/qqmlinfo.h>
#include <QtQml/qqmlcontext.h>
#include <QtQuick/private/qquickpixmap_p.h>
#include <QtCore/qmimedatabase.h>
#include <QtCore/qpointer.h>
QT_BEGIN_NAMESPACE
using namespace Qt::StringLiterals;
/*!
\qmltype TextDocument
\instantiates QQuickTextDocument
\inqmlmodule QtQuick
\brief A wrapper around TextEdit's backing QTextDocument.
To load text into the document, set the \l source property. If the user then
modifies the text and wants to save the same document, call \l save() to save
it to the same source again (only if \l {QUrl::isLocalFile()}{it's a local file}).
Or call \l saveAs() to save it to a different file.
This class cannot be instantiated in QML, but is available from \l TextEdit::textDocument.
\note All loading and saving is done synchronously for now.
This may block the UI if the \l source is a slow network drive.
This may be improved in future versions of Qt.
\note This API is considered tech preview and may change in future versions of Qt.
*/
/*!
\class QQuickTextDocument
\since 5.1
\brief The QQuickTextDocument class provides access to the QTextDocument of QQuickTextEdit.
\inmodule QtQuick
This class provides access to the QTextDocument of QQuickTextEdit elements.
This is provided to allow usage of the \l{Rich Text Processing} functionalities of Qt,
including document modifications. It can also be used to output content,
for example with \l{QTextDocumentWriter}), or provide additional formatting,
for example with \l{QSyntaxHighlighter}.
*/
/*!
Constructs a QQuickTextDocument object with
\a parent as the parent object.
*/
QQuickTextDocument::QQuickTextDocument(QQuickItem *parent)
: QObject(*(new QQuickTextDocumentPrivate), parent)
{
Q_D(QQuickTextDocument);
Q_ASSERT(parent);
d->editor = qobject_cast<QQuickTextEdit *>(parent);
Q_ASSERT(d->editor);
connect(textDocument(), &QTextDocument::modificationChanged,
this, &QQuickTextDocument::modifiedChanged);
}
/*!
\qmlproperty url QtQuick::TextDocument::source
\since 6.7
QQuickTextDocument can handle any text format supported by Qt, loaded from
any URL scheme supported by Qt.
The URL may be absolute, or relative to the URL of the component.
The \c source property cannot be changed while the document's \l modified
state is \c true. If the user has modified the document contents, you
should prompt the user whether to \l save(), or else discard changes by
setting \c {modified = false} before setting the \l source property to a
different URL.
\sa QTextDocumentWriter::supportedDocumentFormats()
*/
QUrl QQuickTextDocument::source() const
{
Q_D(const QQuickTextDocument);
return d->url;
}
void QQuickTextDocument::setSource(const QUrl &url)
{
Q_D(QQuickTextDocument);
if (url == d->url)
return;
if (isModified()) {
qmlWarning(this) << "Existing document modified: you should save(),"
"or call TextEdit.clear() before setting a different source";
return;
}
d->url = url;
emit sourceChanged();
d->load();
}
/*!
\qmlproperty bool QtQuick::TextDocument::modified
\since 6.7
This property holds whether the document has been modified by the user
since the last time it was loaded or saved. By default, this property is
\c false.
As with \l QTextDocument::modified, you can set the modified property:
for example, set it to \c false to allow setting the \c source property
to a different URL (thus discarding the user's changes).
\sa QTextDocument::modified
*/
bool QQuickTextDocument::isModified() const
{
const auto *doc = textDocument();
return doc && doc->isModified();
}
void QQuickTextDocument::setModified(bool modified)
{
if (auto *doc = textDocument())
doc->setModified(modified);
}
void QQuickTextDocumentPrivate::load()
{
Q_Q(QQuickTextDocument);
const QQmlContext *context = qmlContext(editor);
const QUrl &resolvedUrl = context ? context->resolvedUrl(url) : url;
const QString fileName = QQmlFile::urlToLocalFileOrQrc(resolvedUrl);
if (QFile::exists(fileName)) {
mimeType = QMimeDatabase().mimeTypeForFile(fileName);
QFile file(fileName);
if (file.open(QFile::ReadOnly)) {
QByteArray data = file.readAll();
if (auto *doc = editor->document()) {
doc->setBaseUrl(resolvedUrl.adjusted(QUrl::RemoveFilename));
if (mimeType.inherits("text/markdown"_L1)) {
doc->setMarkdown(QString::fromUtf8(data));
} else if (mimeType.inherits("text/html"_L1)) {
// If a user loads an HTML file, remember the encoding.
// If the user then calls save() later, the same encoding will be used.
encoding = QStringConverter::encodingForHtml(data);
if (encoding) {
QStringDecoder decoder(*encoding);
doc->setHtml(decoder(data));
} else {
// fall back to utf8
doc->setHtml(QString::fromUtf8(data));
}
} else {
doc->setPlainText(QString::fromUtf8(data));
}
doc->setModified(false);
}
} else {
emit q->error(QQuickTextDocument::tr("Cannot load: %1").arg(file.errorString()));
}
}
}
void QQuickTextDocumentPrivate::writeTo(const QUrl &fileUrl)
{
Q_Q(QQuickTextDocument);
auto *doc = editor->document();
if (!doc)
return;
const QString filePath = fileUrl.toLocalFile();
const bool sameUrl = fileUrl == url;
const auto type = (sameUrl ? mimeType : QMimeDatabase().mimeTypeForUrl(fileUrl));
const bool isHtml = type.inherits("text/html"_L1);
QFile file(filePath);
if (!file.open(QFile::WriteOnly | QFile::Truncate | (isHtml ? QFile::NotOpen : QFile::Text))) {
emit q->error(QQuickTextDocument::tr("Cannot save: %1").arg(file.errorString()));
return;
}
QByteArray raw;
if (type.inherits("text/markdown"_L1)) {
raw = doc->toMarkdown().toUtf8();
} else if (isHtml) {
if (sameUrl && encoding) {
QStringEncoder enc(*encoding);
raw = enc.encode(doc->toHtml());
} else {
// default to UTF-8 unless the user is saving the same file as previously loaded
raw = doc->toHtml().toUtf8();
}
} else {
raw = doc->toPlainText().toUtf8();
}
file.write(raw);
file.close();
doc->setModified(false);
}
QTextDocument *QQuickTextDocumentPrivate::document() const
{
return editor->document();
}
void QQuickTextDocumentPrivate::setDocument(QTextDocument *doc)
{
Q_Q(QQuickTextDocument);
QTextDocument *oldDoc = editor->document();
if (doc == oldDoc)
return;
if (oldDoc)
oldDoc->disconnect(q);
if (doc) {
q->connect(doc, &QTextDocument::modificationChanged,
q, &QQuickTextDocument::modifiedChanged);
}
editor->setDocument(doc);
emit q->textDocumentChanged();
}
/*!
Returns a pointer to the QTextDocument object.
*/
QTextDocument *QQuickTextDocument::textDocument() const
{
Q_D(const QQuickTextDocument);
return d->document();
}
/*!
\brief Sets the given \a document.
\since 6.7
The caller retains ownership of the document.
*/
void QQuickTextDocument::setTextDocument(QTextDocument *document)
{
d_func()->setDocument(document);
}
/*!
\fn void QQuickTextDocument::textDocumentChanged()
\since 6.7
This signal is emitted when the underlying QTextDocument is
replaced with a different instance.
\sa setTextDocument()
*/
/*!
\qmlmethod void QtQuick::TextDocument::save()
\brief Saves the contents to the same file and format specified by \l source.
\since 6.7
\note You can save only to a \l {QUrl::isLocalFile()}{file on a mounted filesystem}.
\sa source, saveAs()
*/
void QQuickTextDocument::save()
{
Q_D(QQuickTextDocument);
d->writeTo(d->url);
}
/*!
\qmlmethod void QtQuick::TextDocument::saveAs(url url)
\brief Saves the contents to the file and format specified by \a url.
\since 6.7
The file extension in \a url specifies the file format
(as determined by QMimeDatabase::mimeTypeForUrl()).
\note You can save only to a \l {QUrl::isLocalFile()}{file on a mounted filesystem}.
\sa source, saveAs()
*/
void QQuickTextDocument::saveAs(const QUrl &url)
{
Q_D(QQuickTextDocument);
if (!url.isLocalFile()) {
emit error(tr("Can only save to local files"));
return;
}
d->writeTo(url);
if (url == d->url)
return;
d->url = url;
emit sourceChanged();
}
QQuickTextImageHandler::QQuickTextImageHandler(QObject *parent)
: QObject(parent)
{
}
QSizeF QQuickTextImageHandler::intrinsicSize(
QTextDocument *doc, int, const QTextFormat &format)
{
if (format.isImageFormat()) {
QTextImageFormat imageFormat = format.toImageFormat();
const int width = qRound(imageFormat.width());
const bool hasWidth = imageFormat.hasProperty(QTextFormat::ImageWidth) && width > 0;
const int height = qRound(imageFormat.height());
const bool hasHeight = imageFormat.hasProperty(QTextFormat::ImageHeight) && height > 0;
QSizeF size(width, height);
if (!hasWidth || !hasHeight) {
QVariant res = doc->resource(QTextDocument::ImageResource, QUrl(imageFormat.name()));
QImage image = res.value<QImage>();
if (image.isNull()) {
// autotests expect us to reserve a 16x16 space for a "broken image" icon,
// even though we don't actually display one
if (!hasWidth)
size.setWidth(16);
if (!hasHeight)
size.setHeight(16);
return size;
}
QSize imgSize = image.size();
if (!hasWidth) {
if (!hasHeight)
size.setWidth(imgSize.width());
else
size.setWidth(qRound(height * (imgSize.width() / (qreal) imgSize.height())));
}
if (!hasHeight) {
if (!hasWidth)
size.setHeight(imgSize.height());
else
size.setHeight(qRound(width * (imgSize.height() / (qreal) imgSize.width())));
}
}
return size;
}
return QSizeF();
}
/*!
\qmlsignal QtQuick::TextDocument::error(string message)
This signal is emitted when an error \a message (translated string) should
be presented to the user, for example with a MessageDialog.
*/
QT_END_NAMESPACE
#include "moc_qquicktextdocument.cpp"
#include "moc_qquicktextdocument_p.cpp"
|