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 editor->setCursorPosition(0);
357 setStatus(QQuickTextDocument::Status::Loaded, {});
358 qCDebug(lcTextDoc) << editor << "loaded" << filePath
359 << "as" << editor->textFormat() << "detected" << detectedFormat
360#if QT_CONFIG(mimetype)
361 << "(file type" << mimeType << ')'
362#endif
363 ;
364 doc->setModified(false);
365 return;
366 }
367 setStatus(QQuickTextDocument::Status::ReadError,
368 QQuickTextDocument::tr("Failed to read: %1").arg(file.errorString()));
369 } else {
370 setStatus(QQuickTextDocument::Status::ReadError,
371 QQuickTextDocument::tr("%1 does not exist").arg(filePath));
372 }
373}
374
375void QQuickTextDocumentPrivate::writeTo(const QUrl &fileUrl)
376{
377 auto *doc = editor->document();
378 if (!doc)
379 return;
380
381 const QString filePath = QQmlFile::urlToLocalFileOrQrc(fileUrl);
382 const bool sameUrl = fileUrl == url;
383 if (!sameUrl) {
384#if QT_CONFIG(mimetype)
385 const auto type = QMimeDatabase().mimeTypeForUrl(fileUrl);
386 if (type.inherits("text/html"_L1))
387 detectedFormat = Qt::RichText;
388 else if (type.inherits("text/markdown"_L1))
389 detectedFormat = Qt::MarkdownText;
390 else
391 detectedFormat = Qt::PlainText;
392#else
393 if (filePath.endsWith(".html"_L1, Qt::CaseInsensitive) ||
394 filePath.endsWith(".htm"_L1, Qt::CaseInsensitive))
395 detectedFormat = Qt::RichText;
396 else if (filePath.endsWith(".md"_L1, Qt::CaseInsensitive) ||
397 filePath.endsWith(".markdown"_L1, Qt::CaseInsensitive))
398 detectedFormat = Qt::MarkdownText;
399 else
400 detectedFormat = Qt::PlainText;
401#endif
402 }
403 QFile file(filePath);
404 if (!file.open(QFile::WriteOnly | QFile::Truncate |
405 (detectedFormat == Qt::RichText ? QFile::NotOpen : QFile::Text))) {
406 setStatus(QQuickTextDocument::Status::WriteError,
407 QQuickTextDocument::tr("Cannot save: %1").arg(file.errorString()));
408 return;
409 }
410 setStatus(QQuickTextDocument::Status::Saving, {});
411 QByteArray raw;
412
413 switch (detectedFormat) {
414#if QT_CONFIG(textmarkdownwriter)
415 case Qt::MarkdownText:
416 raw = doc->toMarkdown().toUtf8();
417 break;
418#endif
419#if QT_CONFIG(texthtmlparser)
420 case Qt::RichText:
421 if (sameUrl && encoding) {
422 QStringEncoder enc(*encoding);
423 raw = enc.encode(doc->toHtml());
424 } else {
425 // default to UTF-8 unless the user is saving the same file as previously loaded
426 raw = doc->toHtml().toUtf8();
427 }
428 break;
429#endif
430 default:
431 raw = doc->toPlainText().toUtf8();
432 break;
433 }
434
435 file.write(raw);
436 file.close();
437 setStatus(QQuickTextDocument::Status::Saved, {});
438 doc->setModified(false);
439}
440
442{
443 return editor->document();
444}
445
446void QQuickTextDocumentPrivate::setDocument(QTextDocument *doc)
447{
448 Q_Q(QQuickTextDocument);
449 QTextDocument *oldDoc = editor->document();
450 if (doc == oldDoc)
451 return;
452
453 if (oldDoc)
454 oldDoc->disconnect(q);
455 if (doc) {
456 q->connect(doc, &QTextDocument::modificationChanged,
457 q, &QQuickTextDocument::modifiedChanged);
458 }
459 editor->setDocument(doc);
460 emit q->textDocumentChanged();
461}
462
463/*!
464 Returns a pointer to the QTextDocument object.
465*/
466QTextDocument *QQuickTextDocument::textDocument() const
467{
468 Q_D(const QQuickTextDocument);
469 return d->document();
470}
471
472/*!
473 \brief Sets the given \a document.
474 \since 6.7
475
476 The caller retains ownership of the document.
477*/
478void QQuickTextDocument::setTextDocument(QTextDocument *document)
479{
480 d_func()->setDocument(document);
481}
482
483/*!
484 \fn void QQuickTextDocument::textDocumentChanged()
485 \since 6.7
486
487 This signal is emitted when the underlying QTextDocument is
488 replaced with a different instance.
489
490 \sa setTextDocument()
491*/
492
493/*!
494 \preliminary
495 \fn void QQuickTextDocument::sourceChanged()
496*/
497
498/*!
499 \preliminary
500 \fn void QQuickTextDocument::modifiedChanged()
501*/
502
503/*!
504 \preliminary
505 \fn void QQuickTextDocument::statusChanged()
506*/
507
508/*!
509 \preliminary
510 \fn void QQuickTextDocument::errorStringChanged()
511*/
512
513/*!
514 \fn void QQuickTextDocument::save()
515 \since 6.7
516 \preliminary
517
518 Saves the contents to the same file and format specified by \l source.
519
520 \note You can save to a local file, or on Android to a \c content URL.
521
522 \sa source, saveAs()
523*/
524
525/*!
526 \qmlmethod void QtQuick::TextDocument::save()
527 \brief Saves the contents to the same file and format specified by \l source.
528 \since 6.7
529 \preliminary
530
531 \note You can save to a local file, or on Android to a \c content URL.
532
533 \sa source, saveAs()
534*/
535void QQuickTextDocument::save()
536{
537 Q_D(QQuickTextDocument);
538 d->writeTo(d->url);
539}
540
541/*!
542 \fn void QQuickTextDocument::saveAs(const QUrl &url)
543 \brief Saves the contents to the file and format specified by \a url.
544 \since 6.7
545 \preliminary
546
547 The file extension in \a url specifies the file format
548 (as determined by QMimeDatabase::mimeTypeForUrl()).
549
550 \note You can save to a local file, or on Android to a \c content URL.
551
552 \sa source, save()
553*/
554
555/*!
556 \qmlmethod void QtQuick::TextDocument::saveAs(url url)
557 \brief Saves the contents to the file and format specified by \a url.
558 \since 6.7
559 \preliminary
560
561 The file extension in \a url specifies the file format
562 (as determined by QMimeDatabase::mimeTypeForUrl()).
563
564 \note You can save to a local file, or on Android to a \c content URL.
565
566 \sa source, save()
567*/
568void QQuickTextDocument::saveAs(const QUrl &url)
569{
570 Q_D(QQuickTextDocument);
571 bool canWrite = url.isLocalFile();
572#ifdef Q_OS_ANDROID
573 canWrite = canWrite || url.scheme() == "content"_L1;
574#endif
575 if (!canWrite) {
576 d->setStatus(QQuickTextDocument::Status::NonLocalFileError,
577 QQuickTextDocument::tr("Can only save to local files"));
578 return;
579 }
580 d->writeTo(url);
581
582 if (url == d->url)
583 return;
584
585 d->url = url;
586 emit sourceChanged();
587}
588
589QQuickTextImageHandler::QQuickTextImageHandler(QObject *parent)
590 : QObject(parent)
591{
592}
593
595 QTextDocument *doc, int, const QTextFormat &format)
596{
597 if (format.isImageFormat()) {
598 QTextImageFormat imageFormat = format.toImageFormat();
599 int width = qRound(qBound(qreal(INT_MIN), imageFormat.width(), qreal(INT_MAX)));
600 const bool hasWidth = imageFormat.hasProperty(QTextFormat::ImageWidth) && width > 0;
601 const int height = qRound(qBound(qreal(INT_MIN), imageFormat.height(), qreal(INT_MAX)));
602 const bool hasHeight = imageFormat.hasProperty(QTextFormat::ImageHeight) && height > 0;
603 const auto maxWidth = imageFormat.maximumWidth();
604 const bool hasMaxWidth = imageFormat.hasProperty(QTextFormat::ImageMaxWidth) && maxWidth.type() != QTextLength::VariableLength;
605
606 int effectiveMaxWidth = INT_MAX;
607 if (hasMaxWidth) {
608 if (maxWidth.type() == QTextLength::PercentageLength) {
609 effectiveMaxWidth = (doc->pageSize().width() - 2 * doc->documentMargin()) * maxWidth.value(100) / 100;
610 } else {
611 effectiveMaxWidth = maxWidth.rawValue();
612 }
613
614 width = qMin(effectiveMaxWidth, width);
615 }
616
617 QSizeF size(width, height);
618 if (!hasWidth || !hasHeight) {
619 QVariant res = doc->resource(QTextDocument::ImageResource, QUrl(imageFormat.name()));
620 QImage image = res.value<QImage>();
621 if (image.isNull()) {
622 // autotests expect us to reserve a 16x16 space for a "broken image" icon,
623 // even though we don't actually display one
624 if (!hasWidth)
625 size.setWidth(16);
626 if (!hasHeight)
627 size.setHeight(16);
628 return size;
629 }
630 QSize imgSize = image.size();
631 if (imgSize.width() > effectiveMaxWidth) {
632 // image is bigger than effectiveMaxWidth, scale it down
633 imgSize.setHeight(effectiveMaxWidth * imgSize.height() / (qreal) imgSize.width());
634 imgSize.setWidth(effectiveMaxWidth);
635 }
636
637 if (!hasWidth) {
638 if (!hasHeight)
639 size.setWidth(imgSize.width());
640 else
641 size.setWidth(qMin(effectiveMaxWidth, qRound(height * (imgSize.width() / (qreal) imgSize.height()))));
642 }
643 if (!hasHeight) {
644 if (!hasWidth)
645 size.setHeight(imgSize.height());
646 else
647 size.setHeight(qRound(width * (imgSize.height() / (qreal) imgSize.width())));
648 }
649 }
650 return size;
651 }
652 return QSizeF();
653}
654
655QT_END_NAMESPACE
656
657#include "moc_qquicktextdocument.cpp"
658#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...