Qt 6.x
The Qt SDK
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
6
7#include "qquicktextedit_p.h"
9#include "qquicktext_p_p.h"
10
11#include <QtQml/qqmlinfo.h>
12#include <QtQml/qqmlcontext.h>
13#include <QtQuick/private/qquickpixmapcache_p.h>
14
16
36{
37public:
39};
40
47{
50 Q_ASSERT(qobject_cast<QQuickTextEdit*>(parent));
51 d->document = QPointer<QTextDocument>(qobject_cast<QQuickTextEdit*>(parent)->d_func()->document);
52}
53
58{
59 Q_D(const QQuickTextDocument);
60 return d->document.data();
61}
62
64: QTextDocument(parent), outstanding(0)
65{
66 setUndoRedoEnabled(false);
68 connect(this, &QTextDocument::baseUrlChanged, [this]() {
71 });
72}
73
75{
76 if (!m_resources.isEmpty())
77 qDeleteAll(m_resources);
78}
79
81{
87 resource = p->image();
88 }
89
90 return resource;
91}
92
93void QQuickTextDocumentWithImageResources::requestFinished()
94{
95 outstanding--;
96 if (outstanding == 0) {
99 }
100}
101
103 QTextDocument *, int, const QTextFormat &format)
104{
105 if (format.isImageFormat()) {
106 QTextImageFormat imageFormat = format.toImageFormat();
107
108 const int width = qRound(imageFormat.width());
109 const bool hasWidth = imageFormat.hasProperty(QTextFormat::ImageWidth) && width > 0;
110 const int height = qRound(imageFormat.height());
111 const bool hasHeight = imageFormat.hasProperty(QTextFormat::ImageHeight) && height > 0;
112
114 if (!hasWidth || !hasHeight) {
116 QImage image = res.value<QImage>();
117 if (image.isNull()) {
118 if (!hasWidth)
119 size.setWidth(16);
120 if (!hasHeight)
121 size.setHeight(16);
122 return size;
123 }
124 QSize imgSize = image.size();
125
126 if (!hasWidth) {
127 if (!hasHeight)
128 size.setWidth(imgSize.width());
129 else
130 size.setWidth(qRound(height * (imgSize.width() / (qreal) imgSize.height())));
131 }
132 if (!hasHeight) {
133 if (!hasWidth)
134 size.setHeight(imgSize.height());
135 else
136 size.setHeight(qRound(width * (imgSize.height() / (qreal) imgSize.width())));
137 }
138 }
139 return size;
140 }
141 return QSizeF();
142}
143
145 QPainter *, const QRectF &, QTextDocument *, int, const QTextFormat &)
146{
147}
148
150{
152 return res.value<QImage>();
153}
154
156 QQmlContext *context, const QUrl &url)
157{
158
160
161 if (iter == m_resources.end()) {
162 QQuickPixmap *p = new QQuickPixmap(context->engine(), url);
163 iter = m_resources.insert(url, p);
164
165 if (p->isLoading()) {
166 p->connectFinished(this, SLOT(requestFinished()));
167 outstanding++;
168 }
169 }
170
171 QQuickPixmap *p = *iter;
172 if (p->isError()) {
173 if (!errors.contains(url)) {
174 errors.insert(url);
175 qmlWarning(parent()) << p->error();
176 }
177 }
178 return p;
179}
180
182{
183 for (QQuickPixmap *pixmap : std::as_const(m_resources))
184 pixmap->clear(this);
185 qDeleteAll(m_resources);
186 m_resources.clear();
187 outstanding = 0;
188}
189
190QSet<QUrl> QQuickTextDocumentWithImageResources::errors;
191
193
194#include "moc_qquicktextdocument.cpp"
195#include "moc_qquicktextdocument_p.cpp"
void registerHandler(int objectType, QObject *component)
Registers the given component as a handler for items of the given objectType.
\inmodule QtCore
Definition qhash.h:1093
iterator find(const Key &key)
Returns an iterator pointing to the item with the key in the hash.
Definition qhash.h:1258
iterator end() noexcept
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last ...
Definition qhash.h:1206
void clear() noexcept(std::is_nothrow_destructible< Node >::value)
Removes all items from the hash and frees up all memory used by it.
Definition qhash.h:949
bool isEmpty() const noexcept
Returns true if the hash contains no items; otherwise returns false.
Definition qhash.h:926
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition qhash.h:1283
\inmodule QtGui
Definition qimage.h:37
\inmodule QtCore
Definition qobject.h:90
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:311
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2823
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
\inmodule QtCore
Definition qpointer.h:18
The QQmlContext class defines a context within a QML engine.
Definition qqmlcontext.h:25
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:64
QPointer< QTextDocument > document
QVariant loadResource(int type, const QUrl &name) override
Loads data of the specified type from the resource with the given name.
void drawObject(QPainter *p, const QRectF &rect, QTextDocument *doc, int posInDocument, const QTextFormat &format) override
Draws this text object using the specified painter.
QQuickTextDocumentWithImageResources(QQuickItem *parent)
QImage image(const QTextImageFormat &format) const
QQuickPixmap * loadPixmap(QQmlContext *context, const QUrl &name)
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...
The QQuickTextDocument class provides access to the QTextDocument of QQuickTextEdit....
QQuickTextDocument(QQuickItem *parent)
Constructs a QQuickTextDocument object with parent as the parent object.
QTextDocument * textDocument() const
Returns a pointer to the QTextDocument object.
\inmodule QtCore\reentrant
Definition qrect.h:483
Definition qset.h:18
bool contains(const T &value) const
Definition qset.h:71
iterator insert(const T &value)
Definition qset.h:155
\inmodule QtCore
Definition qsize.h:207
\inmodule QtCore
Definition qsize.h:25
constexpr int height() const noexcept
Returns the height.
Definition qsize.h:132
constexpr int width() const noexcept
Returns the width.
Definition qsize.h:129
\reentrant \inmodule QtGui
QAbstractTextDocumentLayout * documentLayout() const
Returns the document layout for this document.
QSizeF size
the actual size of the document. This is equivalent to documentLayout()->documentSize();
void baseUrlChanged(const QUrl &url)
QVariant resource(int type, const QUrl &name) const
Returns data of the specified type from the resource with the given name.
void setUndoRedoEnabled(bool enable)
QUrl baseUrl
the base URL used to resolve relative resource URLs within the document.
virtual Q_INVOKABLE QVariant loadResource(int type, const QUrl &name)
Loads data of the specified type from the resource with the given name.
void markContentsDirty(int from, int length)
Marks the contents specified by the given position and length as "dirty", informing the document that...
int characterCount() const
\reentrant
Definition qtextformat.h:90
QTextImageFormat toImageFormat() const
Returns this format as an image format.
bool hasProperty(int propertyId) const
Returns true if the text format has a property with the given propertyId; otherwise returns false.
qreal width() const
Returns the width of the rectangle occupied by the image.
qreal height() const
Returns the height of the rectangle occupied by the image.
QString name() const
Returns the name of the image.
\inmodule QtCore
Definition qurl.h:94
QUrl resolved(const QUrl &relative) const
Returns the result of the merge of this URL with relative.
Definition qurl.cpp:2722
\inmodule QtCore
Definition qvariant.h:64
bool isNull() const
Returns true if this is a null variant, false otherwise.
qDeleteAll(list.begin(), list.end())
Combined button and popup list for selecting options.
Definition image.cpp:4
static void * context
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter * iter
int qRound(qfloat16 d) noexcept
Definition qfloat16.h:281
#define SLOT(a)
Definition qobjectdefs.h:51
GLint GLsizei GLsizei height
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLint GLsizei width
GLenum type
GLuint name
GLint GLsizei GLsizei GLenum format
GLuint res
GLfloat GLfloat p
[1]
QQmlContext * qmlContext(const QObject *obj)
Definition qqml.cpp:71
Q_QML_EXPORT QQmlInfo qmlWarning(const QObject *me)
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define emit
double qreal
Definition qtypes.h:92
QUrl url("example.com")
[constructor-url-reference]
widget render & pixmap
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent