| 1 | // Copyright (C) 2018 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 | |
| 4 | #ifndef QQUICKTEXTDOCUMENT_P_H |
| 5 | #define QQUICKTEXTDOCUMENT_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include "qquicktextdocument.h" |
| 19 | |
| 20 | #include <QtGui/qabstracttextdocumentlayout.h> |
| 21 | #include <QtGui/qtextdocument.h> |
| 22 | #include <QtGui/qtextdocumentfragment.h> |
| 23 | #include <QtGui/qtextformat.h> |
| 24 | #include <QtCore/qrect.h> |
| 25 | #include <QtCore/private/qobject_p_p.h> |
| 26 | |
| 27 | #if QT_CONFIG(mimetype) |
| 28 | #include <QtCore/qmimedatabase.h> |
| 29 | #endif |
| 30 | |
| 31 | QT_BEGIN_NAMESPACE |
| 32 | |
| 33 | class QQuickPixmap; |
| 34 | class QQuickTextEdit; |
| 35 | |
| 36 | /*! \internal |
| 37 | QTextImageHandler would attempt to resolve relative paths, and load the |
| 38 | image itself if the document returns an invalid image from loadResource(). |
| 39 | We replace it with this version instead, because Qt Quick's text resources |
| 40 | are resolved against the Text item's context, and because we override |
| 41 | intrinsicSize(). drawObject() is empty because we don't need to use this |
| 42 | handler to paint images: they get put into scene graph nodes instead. |
| 43 | */ |
| 44 | class QQuickTextImageHandler : public QObject, public QTextObjectInterface |
| 45 | { |
| 46 | Q_OBJECT |
| 47 | Q_INTERFACES(QTextObjectInterface) |
| 48 | public: |
| 49 | QQuickTextImageHandler(QObject *parent = nullptr); |
| 50 | ~QQuickTextImageHandler() override = default; |
| 51 | QSizeF intrinsicSize(QTextDocument *doc, int posInDocument, const QTextFormat &format) override; |
| 52 | void drawObject(QPainter *, const QRectF &, QTextDocument *, int, const QTextFormat &) override { } |
| 53 | }; |
| 54 | |
| 55 | class QQuickTextDocumentPrivate : public QObjectPrivate |
| 56 | { |
| 57 | Q_DECLARE_PUBLIC(QQuickTextDocument) |
| 58 | public: |
| 59 | static QQuickTextDocumentPrivate *get(QQuickTextDocument *doc) { return doc->d_func(); } |
| 60 | static const QQuickTextDocumentPrivate *get(const QQuickTextDocument *doc) { return doc->d_func(); } |
| 61 | |
| 62 | void load(); |
| 63 | void writeTo(const QUrl &fileUrl); |
| 64 | QTextDocument *document() const; |
| 65 | void setDocument(QTextDocument *doc); |
| 66 | void setStatus(QQuickTextDocument::Status s, const QString &err); |
| 67 | |
| 68 | // so far the QQuickItem given to the QQuickTextDocument ctor is always a QQuickTextEdit |
| 69 | QQuickTextEdit *editor = nullptr; |
| 70 | QUrl url; |
| 71 | QString errorString; |
| 72 | Qt::TextFormat detectedFormat = Qt::AutoText; // url's extension, independent of TextEdit.textFormat |
| 73 | std::optional<QStringConverter::Encoding> encoding; // only relevant for HTML (Qt::RichText) |
| 74 | QQuickTextDocument::Status status = QQuickTextDocument::Status::Null; |
| 75 | }; |
| 76 | |
| 77 | namespace QtPrivate { |
| 78 | class ProtectedLayoutAccessor: public QAbstractTextDocumentLayout |
| 79 | { |
| 80 | public: |
| 81 | inline QTextCharFormat formatAccessor(int pos) |
| 82 | { |
| 83 | return format(pos); |
| 84 | } |
| 85 | }; |
| 86 | } // namespace QtPrivate |
| 87 | |
| 88 | QT_END_NAMESPACE |
| 89 | |
| 90 | #endif // QQUICKTEXTDOCUMENT_P_H |
| 91 | |