summaryrefslogtreecommitdiffstats
path: root/src/gui/text/qtextdocument.cpp
diff options
context:
space:
mode:
authorAlexander Volkov <avolkov@astralinux.ru>2018-09-25 18:09:28 +0300
committerAlexander Volkov <avolkov@astralinux.ru>2021-01-12 15:33:10 +0300
commit376e3bd8ecf40881685714f6f19e12d68e92127e (patch)
tree8a137b8cea51115b405202ef6c0cffdf6fe78192 /src/gui/text/qtextdocument.cpp
parent9e09677c1dd4da92735736a0fbcb703416bf4d6d (diff)
Introduce QUrlResourceProvider to load resources for HTML
QTextDocument and the text editor classes suggest to override their loadResource() methods to provide data associated with a text document. This approach has the following drawbacks: - it requires subclassing - there is no way to set a global resource provider - QLabel is missing virtual loadResource() method and it can't be added without breaking ABI QUrlResourceProvider is designed to solve these issues. One should create a derived class that implements QUrlResourceProvider::resource(). The objects of the derived class then can be set for any text document. The default resource provider can be set with QUrlResourceProvider::setDefaultProvider(). This change also adds QLabel::setResourceProvider(), which doesn't break ABI. [ChangeLog][QtGui][Text] Introduced QUrlResourceProvider that allows to load resources for HTML. It is intended to replace the use of QTextDocument::loadResource(). Change-Id: Iaf19b229f522a73508f20715257450fe58f68daf Reviewed-by: Konstantin Ritt <ritt.ks@gmail.com> Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'src/gui/text/qtextdocument.cpp')
-rw-r--r--src/gui/text/qtextdocument.cpp35
1 files changed, 34 insertions, 1 deletions
diff --git a/src/gui/text/qtextdocument.cpp b/src/gui/text/qtextdocument.cpp
index 0fae775bae..2e9293eb99 100644
--- a/src/gui/text/qtextdocument.cpp
+++ b/src/gui/text/qtextdocument.cpp
@@ -46,6 +46,7 @@
#include "qtextdocumentfragment_p.h"
#include "qtexttable.h"
#include "qtextlist.h"
+#include "qurlresourceprovider.h"
#include <qdebug.h>
#if QT_CONFIG(regularexpression)
#include <qregularexpression.h>
@@ -354,6 +355,7 @@ QTextDocument *QTextDocument::clone(QObject *parent) const
priv->setDefaultFont(d->defaultFont());
priv->resources = d->resources;
priv->cachedResources.clear();
+ priv->resourceProvider = d->resourceProvider;
#ifndef QT_NO_CSSPARSER
priv->defaultStyleSheet = d->defaultStyleSheet;
priv->parsedDefaultStyleSheet = d->parsedDefaultStyleSheet;
@@ -2087,8 +2089,15 @@ QVariant QTextDocument::resource(int type, const QUrl &name) const
QVariant r = d->resources.value(url);
if (!r.isValid()) {
r = d->cachedResources.value(url);
- if (!r.isValid())
+ if (!r.isValid()) {
r = const_cast<QTextDocument *>(this)->loadResource(type, url);
+ if (!r.isValid()) {
+ if (d->resourceProvider)
+ r = d->resourceProvider->resource(url);
+ else if (auto defaultProvider = QUrlResourceProvider::defaultProvider())
+ r = defaultProvider->resource(url);
+ }
+ }
}
return r;
}
@@ -2119,6 +2128,30 @@ void QTextDocument::addResource(int type, const QUrl &name, const QVariant &reso
}
/*!
+ \since 6.1
+
+ Returns the resource provider for this text document.
+*/
+QUrlResourceProvider *QTextDocument::resourceProvider() const
+{
+ Q_D(const QTextDocument);
+ return d->resourceProvider;
+}
+
+/*!
+ \since 6.1
+
+ Sets the \a provider of resources for the text document.
+
+ \note The text document \e{does not} take ownership of the \a provider.
+*/
+void QTextDocument::setResourceProvider(QUrlResourceProvider *provider)
+{
+ Q_D(QTextDocument);
+ d->resourceProvider = provider;
+}
+
+/*!
Loads data of the specified \a type from the resource with the
given \a name.