Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquicktextdocument.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
3// Qt-Security score:significant reason:default
4
7
9
10#include <QtQml/qqmlcontext.h>
11#include <QtQml/qqmlfile.h>
12#include <QtQml/qqmlinfo.h>
13#include <QtQuick/private/qquickpixmap_p.h>
14
15#include <QtCore/qfile.h>
16#include <QtCore/qpointer.h>
17
18QT_BEGIN_NAMESPACE
19
20Q_STATIC_LOGGING_CATEGORY(lcTextDoc, "qt.quick.textdocument")
21
22using namespace Qt::StringLiterals;
23
24/*!
25 \qmltype TextDocument
26 \nativetype QQuickTextDocument
27 \inqmlmodule QtQuick
28 \brief A wrapper around TextEdit's backing QTextDocument.
29 \preliminary
30
31 To load text into the document, set the \l source property. If the user then
32 modifies the text and wants to save the same document, call \l save() to save
33 it to the same source again (only if \l {QUrl::isLocalFile()}{it's a local file}).
34 Or call \l saveAs() to save it to a different file.
35
36 This class cannot be instantiated in QML, but is available from \l TextEdit::textDocument.
37
38 \note All loading and saving is done synchronously for now.
39 This may block the UI if the \l source is a slow network drive.
40 This may be improved in future versions of Qt.
41
42 \note This API is considered tech preview and may change in future versions of Qt.
43*/
44
45/*!
46 \class QQuickTextDocument
47 \since 5.1
48 \brief The QQuickTextDocument class provides access to the QTextDocument of QQuickTextEdit.
49 \inmodule QtQuick
50
51 This class provides access to the QTextDocument of QQuickTextEdit elements.
52 This is provided to allow usage of the \l{Rich Text Processing} functionalities of Qt,
53 including document modifications. It can also be used to output content,
54 for example with \l{QTextDocumentWriter}, or provide additional formatting,
55 for example with \l{QSyntaxHighlighter}.
56*/
57
58/*!
59 Constructs a QQuickTextDocument object with
60 \a parent as the parent object.
61*/
62QQuickTextDocument::QQuickTextDocument(QQuickItem *parent)
63 : QObject(*(new QQuickTextDocumentPrivate), parent)
64{
65 Q_D(QQuickTextDocument);
66 Q_ASSERT(parent);
67 d->editor = qobject_cast<QQuickTextEdit *>(parent);
68 Q_ASSERT(d->editor);
69 connect(textDocument(), &QTextDocument::modificationChanged,
70 this, &QQuickTextDocument::modifiedChanged);
71}
72
73/*!
74 \property QQuickTextDocument::status
75 \brief the status of document loading or saving
76 \since 6.7
77 \preliminary
78
79 This property holds the status of document loading or saving. It can be one of:
80
81 \value Null No file has been loaded
82 \value Loading Reading from \l source has begun
83 \value Loaded Reading has successfully finished
84 \value Saving File writing has begun after save() or saveAs()
85 \value Saved Writing has successfully finished
86 \value ReadError An error occurred while reading from \l source
87 \value WriteError An error occurred in save() or saveAs()
88 \value NonLocalFileError saveAs() was called with a URL pointing
89 to a remote resource rather than a local file
90
91 \sa errorString, source, save(), saveAs()
92*/
93
94/*!
95 \qmlproperty enumeration QtQuick::TextDocument::status
96 \readonly
97 \since 6.7
98 \preliminary
99
100 This property holds the status of document loading or saving. It can be one of:
101
102 \value TextDocument.Null No file has been loaded
103 \value TextDocument.Loading Reading from \l source has begun
104 \value TextDocument.Loaded Reading has successfully finished
105 \value TextDocument.Saving File writing has begun after save() or saveAs()
106 \value TextDocument.Saved Writing has successfully finished
107 \value TextDocument.ReadError An error occurred while reading from \l source
108 \value TextDocument.WriteError An error occurred in save() or saveAs()
109 \value TextDocument.NonLocalFileError saveAs() was called with a URL pointing
110 to a remote resource rather than a local file
111
112 Use this status to provide an update or respond to the status change in some way.
113 For example, you could:
114
115 \list
116 \li Trigger a state change:
117 \qml
118 State {
119 name: 'loaded'
120 when: textEdit.textDocument.status == textEdit.textDocument.Loaded
121 }
122 \endqml
123
124 \li Implement an \c onStatusChanged signal handler:
125 \qml
126 TextEdit {
127 onStatusChanged: {
128 if (textDocument.status === textDocument.Loaded)
129 console.log('Loaded')
130 }
131 }
132 \endqml
133
134 \li Bind to the status value:
135
136 \snippet qml/textEditStatusSwitch.qml 0
137
138 \endlist
139
140 \sa errorString, source, save(), saveAs()
141*/
142QQuickTextDocument::Status QQuickTextDocument::status() const
143{
144 Q_D(const QQuickTextDocument);
145 return d->status;
146}
147
148/*!
149 \property QQuickTextDocument::errorString
150 \brief a human-readable string describing the error that occurred during loading or saving, if any
151 \since 6.7
152 \preliminary
153
154 By default this string is empty.
155
156 \sa status, source, save(), saveAs()
157*/
158
159/*!
160 \qmlproperty string QtQuick::TextDocument::errorString
161 \readonly
162 \since 6.7
163 \preliminary
164
165 This property holds a human-readable string describing the error that
166 occurred during loading or saving, if any; otherwise, an empty string.
167
168 \sa status, source, save(), saveAs()
169*/
170QString QQuickTextDocument::errorString() const
171{
172 Q_D(const QQuickTextDocument);
173 return d->errorString;
174}
175
176void QQuickTextDocumentPrivate::setStatus(QQuickTextDocument::Status s, const QString &err)
177{
178 Q_Q(QQuickTextDocument);
179 if (status == s)
180 return;
181
182 status = s;
183 emit q->statusChanged();
184
185 if (errorString == err)
186 return;
187 errorString = err;
188 emit q->errorStringChanged();
189 if (!err.isEmpty())
190 qmlWarning(q) << err;
191}
192
193/*!
194 \property QQuickTextDocument::source
195 \brief the URL from which to load document contents
196 \since 6.7
197 \preliminary
198
199 QQuickTextDocument can handle any text format supported by Qt, loaded from
200 any URL scheme supported by Qt.
201
202 The \c source property cannot be changed while the document's \l modified
203 state is \c true. If the user has modified the document contents, you
204 should prompt the user whether to \l save(), or else discard changes by
205 setting \l modified to \c false before setting the \c source property to a
206 different URL.
207
208 \sa QTextDocumentWriter::supportedDocumentFormats()
209*/
210
211/*!
212 \qmlproperty url QtQuick::TextDocument::source
213 \since 6.7
214 \preliminary
215
216 QQuickTextDocument can handle any text format supported by Qt, loaded from
217 any URL scheme supported by Qt.
218
219 The URL may be absolute, or relative to the URL of the component.
220
221 The \c source property cannot be changed while the document's \l modified
222 state is \c true. If the user has modified the document contents, you
223 should prompt the user whether to \l save(), or else discard changes by
224 setting \c {modified = false} before setting the \l source property to a
225 different URL.
226
227 \sa QTextDocumentWriter::supportedDocumentFormats()
228*/
229QUrl QQuickTextDocument::source() const
230{
231 Q_D(const QQuickTextDocument);
232 return d->url;
233}
234
235void QQuickTextDocument::setSource(const QUrl &url)
236{
237 Q_D(QQuickTextDocument);
238
239 if (url == d->url)
240 return;
241
242 if (isModified()) {
243 qmlWarning(this) << "Existing document modified: you should save(),"
244 " or set modified=false before setting a different source";
245 return;
246 }
247
248 d->url = url;
249 emit sourceChanged();
250 d->load();
251}
252
253/*!
254 \property QQuickTextDocument::modified
255 \brief whether the document has been modified by the user
256 \since 6.7
257 \preliminary
258
259 This property holds whether the document has been modified by the user
260 since the last time it was loaded or saved. By default, this property is
261 \c false.
262
263 As with \l QTextDocument::modified, you can set the modified property:
264 for example, set it to \c false to allow setting the \l source property
265 to a different URL (thus discarding the user's changes).
266
267 \sa QTextDocument::modified
268*/
269
270/*!
271 \qmlproperty bool QtQuick::TextDocument::modified
272 \since 6.7
273 \preliminary
274
275 This property holds whether the document has been modified by the user
276 since the last time it was loaded or saved. By default, this property is
277 \c false.
278
279 As with \l QTextDocument::modified, you can set the modified property:
280 for example, set it to \c false to allow setting the \l source property
281 to a different URL (thus discarding the user's changes).
282
283 \sa QTextDocument::modified
284*/
285bool QQuickTextDocument::isModified() const
286{
287 const auto *doc = textDocument();
288 return doc && doc->isModified();
289}
290
291void QQuickTextDocument::setModified(bool modified)
292{
293 if (auto *doc = textDocument())
294 doc->setModified(modified);
295}
296
298{
299 auto *doc = editor->document();
300 if (!doc) {
301 setStatus(QQuickTextDocument::Status::ReadError,
302 QQuickTextDocument::tr("Null document object: cannot load"));
303 return;
304 }
305 const QQmlContext *context = qmlContext(editor);
306 const QUrl &resolvedUrl = context ? context->resolvedUrl(url) : url;
307 const QString filePath = QQmlFile::urlToLocalFileOrQrc(resolvedUrl);
308 QFile file(filePath);
309 if (file.exists()) {
310#if QT_CONFIG(mimetype)
311 QMimeType mimeType = QMimeDatabase().mimeTypeForFile(filePath);
312 const bool isHtml = mimeType.inherits("text/html"_L1);
313 const bool isMarkdown = mimeType.inherits("text/markdown"_L1)
314 || mimeType.inherits("text/x-web-markdown"_L1); //Tika database
315#else
316 const bool isHtml = filePath.endsWith(".html"_L1, Qt::CaseInsensitive) ||
317 filePath.endsWith(".htm"_L1, Qt::CaseInsensitive);
318 const bool isMarkdown = filePath.endsWith(".md"_L1, Qt::CaseInsensitive) ||
319 filePath.endsWith(".markdown"_L1, Qt::CaseInsensitive);
320#endif
321 if (isHtml)
322 detectedFormat = Qt::RichText;
323 else if (isMarkdown)
324 detectedFormat = Qt::MarkdownText;
325 else
326 detectedFormat = Qt::PlainText;
327 if (file.open(QFile::ReadOnly | QFile::Text)) {
328 setStatus(QQuickTextDocument::Status::Loading, {});
329 QByteArray data = file.readAll();
330 doc->setBaseUrl(resolvedUrl.adjusted(QUrl::RemoveFilename));
331#if QT_CONFIG(textmarkdownreader) || QT_CONFIG(texthtmlparser)
332 const bool plainText = editor->textFormat() == QQuickTextEdit::PlainText;
333#endif
334#if QT_CONFIG(textmarkdownreader)
335 if (!plainText && isMarkdown) {
336 doc->setMarkdown(QString::fromUtf8(data));
337 } else
338#endif
339#if QT_CONFIG(texthtmlparser)
340 if (!plainText && isHtml) {
341 // If a user loads an HTML file, remember the encoding.
342 // If the user then calls save() later, the same encoding will be used.
343 encoding = QStringConverter::encodingForHtml(data);
344 if (encoding) {
345 QStringDecoder decoder(*encoding);
346 doc->setHtml(decoder(data));
347 } else {
348 // fall back to utf8
349 doc->setHtml(QString::fromUtf8(data));
350 }
351 } else
352#endif
353 {
354 doc->setPlainText(QString::fromUtf8(data));
355 }
356 setStatus(QQuickTextDocument::Status::Loaded, {});
357 qCDebug(lcTextDoc) << editor << "loaded" << filePath
358 << "as" << editor->textFormat() << "detected" << detectedFormat
359#if QT_CONFIG(mimetype)
360 << "(file type" << mimeType << ')'
361#endif
362 ;
363 doc->setModified(false);
364 return;
365 }
366 setStatus(QQuickTextDocument::Status::ReadError,
367 QQuickTextDocument::tr("Failed to read: %1").arg(file.errorString()));
368 } else {
369 setStatus(QQuickTextDocument::Status::ReadError,
370 QQuickTextDocument::tr("%1 does not exist").arg(filePath));
371 }
372}
373
374void QQuickTextDocumentPrivate::writeTo(const QUrl &fileUrl)
375{
376 auto *doc = editor->document();
377 if (!doc)
378 return;
379
380 const QString filePath = fileUrl.toLocalFile();
381 const bool sameUrl = fileUrl == url;
382 if (!sameUrl) {
383#if QT_CONFIG(mimetype)
384 const auto type = QMimeDatabase().mimeTypeForUrl(fileUrl);
385 if (type.inherits("text/html"_L1))
386 detectedFormat = Qt::RichText;
387 else if (type.inherits("text/markdown"_L1))
388 detectedFormat = Qt::MarkdownText;
389 else
390 detectedFormat = Qt::PlainText;
391#else
392 if (filePath.endsWith(".html"_L1, Qt::CaseInsensitive) ||
393 filePath.endsWith(".htm"_L1, Qt::CaseInsensitive))
394 detectedFormat = Qt::RichText;
395 else if (filePath.endsWith(".md"_L1, Qt::CaseInsensitive) ||
396 filePath.endsWith(".markdown"_L1, Qt::CaseInsensitive))
397 detectedFormat = Qt::MarkdownText;
398 else
399 detectedFormat = Qt::PlainText;
400#endif
401 }
402 QFile file(filePath);
403 if (!file.open(QFile::WriteOnly | QFile::Truncate |
404 (detectedFormat == Qt::RichText ? QFile::NotOpen : QFile::Text))) {
405 setStatus(QQuickTextDocument::Status::WriteError,
406 QQuickTextDocument::tr("Cannot save: %1").arg(file.errorString()));
407 return;
408 }
409 setStatus(QQuickTextDocument::Status::Saving, {});
410 QByteArray raw;
411
412 switch (detectedFormat) {
413#if QT_CONFIG(textmarkdownwriter)
414 case Qt::MarkdownText:
415 raw = doc->toMarkdown().toUtf8();
416 break;
417#endif
418#if QT_CONFIG(texthtmlparser)
419 case Qt::RichText:
420 if (sameUrl && encoding) {
421 QStringEncoder enc(*encoding);
422 raw = enc.encode(doc->toHtml());
423 } else {
424 // default to UTF-8 unless the user is saving the same file as previously loaded
425 raw = doc->toHtml().toUtf8();
426 }
427 break;
428#endif
429 default:
430 raw = doc->toPlainText().toUtf8();
431 break;
432 }
433
434 file.write(raw);
435 file.close();
436 setStatus(QQuickTextDocument::Status::Saved, {});
437 doc->setModified(false);
438}
439
441{
442 return editor->document();
443}
444
445void QQuickTextDocumentPrivate::setDocument(QTextDocument *doc)
446{
447 Q_Q(QQuickTextDocument);
448 QTextDocument *oldDoc = editor->document();
449 if (doc == oldDoc)
450 return;
451
452 if (oldDoc)
453 oldDoc->disconnect(q);
454 if (doc) {
455 q->connect(doc, &QTextDocument::modificationChanged,
456 q, &QQuickTextDocument::modifiedChanged);
457 }
458 editor->setDocument(doc);
459 emit q->textDocumentChanged();
460}
461
462/*!
463 Returns a pointer to the QTextDocument object.
464*/
465QTextDocument *QQuickTextDocument::textDocument() const
466{
467 Q_D(const QQuickTextDocument);
468 return d->document();
469}
470
471/*!
472 \brief Sets the given \a document.
473 \since 6.7
474
475 The caller retains ownership of the document.
476*/
477void QQuickTextDocument::setTextDocument(QTextDocument *document)
478{
479 d_func()->setDocument(document);
480}
481
482/*!
483 \fn void QQuickTextDocument::textDocumentChanged()
484 \since 6.7
485
486 This signal is emitted when the underlying QTextDocument is
487 replaced with a different instance.
488
489 \sa setTextDocument()
490*/
491
492/*!
493 \preliminary
494 \fn void QQuickTextDocument::sourceChanged()
495*/
496
497/*!
498 \preliminary
499 \fn void QQuickTextDocument::modifiedChanged()
500*/
501
502/*!
503 \preliminary
504 \fn void QQuickTextDocument::statusChanged()
505*/
506
507/*!
508 \preliminary
509 \fn void QQuickTextDocument::errorStringChanged()
510*/
511
512/*!
513 \fn void QQuickTextDocument::save()
514 \since 6.7
515 \preliminary
516
517 Saves the contents to the same file and format specified by \l source.
518
519 \note You can save only to a \l {QUrl::isLocalFile()}{file on a mounted filesystem}.
520
521 \sa source, saveAs()
522*/
523
524/*!
525 \qmlmethod void QtQuick::TextDocument::save()
526 \brief Saves the contents to the same file and format specified by \l source.
527 \since 6.7
528 \preliminary
529
530 \note You can save only to a \l {QUrl::isLocalFile()}{file on a mounted filesystem}.
531
532 \sa source, saveAs()
533*/
534void QQuickTextDocument::save()
535{
536 Q_D(QQuickTextDocument);
537 d->writeTo(d->url);
538}
539
540/*!
541 \fn void QQuickTextDocument::saveAs(const QUrl &url)
542 \brief Saves the contents to the file and format specified by \a url.
543 \since 6.7
544 \preliminary
545
546 The file extension in \a url specifies the file format
547 (as determined by QMimeDatabase::mimeTypeForUrl()).
548
549 \note You can save only to a \l {QUrl::isLocalFile()}{file on a mounted filesystem}.
550
551 \sa source, save()
552*/
553
554/*!
555 \qmlmethod void QtQuick::TextDocument::saveAs(url url)
556 \brief Saves the contents to the file and format specified by \a url.
557 \since 6.7
558 \preliminary
559
560 The file extension in \a url specifies the file format
561 (as determined by QMimeDatabase::mimeTypeForUrl()).
562
563 \note You can save only to a \l {QUrl::isLocalFile()}{file on a mounted filesystem}.
564
565 \sa source, save()
566*/
567void QQuickTextDocument::saveAs(const QUrl &url)
568{
569 Q_D(QQuickTextDocument);
570 if (!url.isLocalFile()) {
571 d->setStatus(QQuickTextDocument::Status::NonLocalFileError,
572 QQuickTextDocument::tr("Can only save to local files"));
573 return;
574 }
575 d->writeTo(url);
576
577 if (url == d->url)
578 return;
579
580 d->url = url;
581 emit sourceChanged();
582}
583
584QQuickTextImageHandler::QQuickTextImageHandler(QObject *parent)
585 : QObject(parent)
586{
587}
588
590 QTextDocument *doc, int, const QTextFormat &format)
591{
592 if (format.isImageFormat()) {
593 QTextImageFormat imageFormat = format.toImageFormat();
594 int width = qRound(imageFormat.width());
595 const bool hasWidth = imageFormat.hasProperty(QTextFormat::ImageWidth) && width > 0;
596 const int height = qRound(imageFormat.height());
597 const bool hasHeight = imageFormat.hasProperty(QTextFormat::ImageHeight) && height > 0;
598 const auto maxWidth = imageFormat.maximumWidth();
599 const bool hasMaxWidth = imageFormat.hasProperty(QTextFormat::ImageMaxWidth) && maxWidth.type() != QTextLength::VariableLength;
600
601 int effectiveMaxWidth = INT_MAX;
602 if (hasMaxWidth) {
603 if (maxWidth.type() == QTextLength::PercentageLength) {
604 effectiveMaxWidth = (doc->pageSize().width() - 2 * doc->documentMargin()) * maxWidth.value(100) / 100;
605 } else {
606 effectiveMaxWidth = maxWidth.rawValue();
607 }
608
609 width = qMin(effectiveMaxWidth, width);
610 }
611
612 QSizeF size(width, height);
613 if (!hasWidth || !hasHeight) {
614 QVariant res = doc->resource(QTextDocument::ImageResource, QUrl(imageFormat.name()));
615 QImage image = res.value<QImage>();
616 if (image.isNull()) {
617 // autotests expect us to reserve a 16x16 space for a "broken image" icon,
618 // even though we don't actually display one
619 if (!hasWidth)
620 size.setWidth(16);
621 if (!hasHeight)
622 size.setHeight(16);
623 return size;
624 }
625 QSize imgSize = image.size();
626 if (imgSize.width() > effectiveMaxWidth) {
627 // image is bigger than effectiveMaxWidth, scale it down
628 imgSize.setHeight(effectiveMaxWidth * imgSize.height() / (qreal) imgSize.width());
629 imgSize.setWidth(effectiveMaxWidth);
630 }
631
632 if (!hasWidth) {
633 if (!hasHeight)
634 size.setWidth(imgSize.width());
635 else
636 size.setWidth(qMin(effectiveMaxWidth, qRound(height * (imgSize.width() / (qreal) imgSize.height()))));
637 }
638 if (!hasHeight) {
639 if (!hasWidth)
640 size.setHeight(imgSize.height());
641 else
642 size.setHeight(qRound(width * (imgSize.height() / (qreal) imgSize.width())));
643 }
644 }
645 return size;
646 }
647 return QSizeF();
648}
649
650QT_END_NAMESPACE
651
652#include "moc_qquicktextdocument.cpp"
653#include "moc_qquicktextdocument_p.cpp"
QTextDocument * document() const
void setStatus(QQuickTextDocument::Status s, const QString &err)
void setDocument(QTextDocument *doc)
void writeTo(const QUrl &fileUrl)
QSizeF intrinsicSize(QTextDocument *doc, int posInDocument, const QTextFormat &format) override
The intrinsicSize() function returns the size of the text object represented by format in the given d...