aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/items/qquicktextdocument_p.h
blob: 3e5236a55c9cfcc8a475b92099a135e5effdb829 (plain)
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
// Copyright (C) 2018 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

#ifndef QQUICKTEXTDOCUMENT_P_H
#define QQUICKTEXTDOCUMENT_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include "qquicktextdocument.h"

#include <QtGui/qabstracttextdocumentlayout.h>
#include <QtGui/qtextdocument.h>
#include <QtGui/qtextdocumentfragment.h>
#include <QtGui/qtextformat.h>
#include <QtCore/qrect.h>
#include <QtCore/private/qobject_p_p.h>

#if QT_CONFIG(mimetype)
#include <QtCore/qmimedatabase.h>
#endif

QT_BEGIN_NAMESPACE

class QQuickPixmap;
class QQuickTextEdit;

/*! \internal
    QTextImageHandler would attempt to resolve relative paths, and load the
    image itself if the document returns an invalid image from loadResource().
    We replace it with this version instead, because Qt Quick's text resources
    are resolved against the Text item's context, and because we override
    intrinsicSize(). drawObject() is empty because we don't need to use this
    handler to paint images: they get put into scene graph nodes instead.
*/
class QQuickTextImageHandler : public QObject, public QTextObjectInterface
{
    Q_OBJECT
    Q_INTERFACES(QTextObjectInterface)
public:
    QQuickTextImageHandler(QObject *parent = nullptr);
    ~QQuickTextImageHandler() override = default;
    QSizeF intrinsicSize(QTextDocument *doc, int posInDocument, const QTextFormat &format) override;
    void drawObject(QPainter *, const QRectF &, QTextDocument *, int, const QTextFormat &) override { }
};

class QQuickTextDocumentPrivate : public QObjectPrivate
{
    Q_DECLARE_PUBLIC(QQuickTextDocument)
public:
    static QQuickTextDocumentPrivate *get(QQuickTextDocument *doc) { return doc->d_func(); }
    static const QQuickTextDocumentPrivate *get(const QQuickTextDocument *doc) { return doc->d_func(); }

    void load();
    void writeTo(const QUrl &fileUrl);
    QTextDocument *document() const;
    void setDocument(QTextDocument *doc);
    void setStatus(QQuickTextDocument::Status s, const QString &err);

    // so far the QQuickItem given to the QQuickTextDocument ctor is always a QQuickTextEdit
    QQuickTextEdit *editor = nullptr;
    QUrl url;
    QString errorString;
    Qt::TextFormat detectedFormat = Qt::AutoText; // url's extension, independent of TextEdit.textFormat
    std::optional<QStringConverter::Encoding> encoding; // only relevant for HTML (Qt::RichText)
    QQuickTextDocument::Status status = QQuickTextDocument::Status::Null;
};

namespace QtPrivate {
class ProtectedLayoutAccessor: public QAbstractTextDocumentLayout
{
public:
    inline QTextCharFormat formatAccessor(int pos)
    {
        return format(pos);
    }
};
} // namespace QtPrivate

QT_END_NAMESPACE

#endif // QQUICKTEXTDOCUMENT_P_H