diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index ab8bb47d53..451187da17 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -242,7 +242,7 @@ qt_internal_add_module(Gui text/qtextobject.cpp text/qtextobject.h text/qtextobject_p.h text/qtextoption.cpp text/qtextoption.h text/qtexttable.cpp text/qtexttable.h text/qtexttable_p.h - text/qurlresourceprovider.cpp text/qurlresourceprovider.h + text/qtextdocumentresourceprovider.cpp text/qtextdocumentresourceprovider.h util/qabstractlayoutstyleinfo.cpp util/qabstractlayoutstyleinfo_p.h util/qastchandler.cpp util/qastchandler_p.h util/qdesktopservices.cpp util/qdesktopservices.h diff --git a/src/gui/text/qtextdocument.cpp b/src/gui/text/qtextdocument.cpp index 2e9293eb99..9234714f64 100644 --- a/src/gui/text/qtextdocument.cpp +++ b/src/gui/text/qtextdocument.cpp @@ -46,7 +46,7 @@ #include "qtextdocumentfragment_p.h" #include "qtexttable.h" #include "qtextlist.h" -#include "qurlresourceprovider.h" +#include "qtextdocumentresourceprovider.h" #include #if QT_CONFIG(regularexpression) #include @@ -2094,7 +2094,7 @@ QVariant QTextDocument::resource(int type, const QUrl &name) const if (!r.isValid()) { if (d->resourceProvider) r = d->resourceProvider->resource(url); - else if (auto defaultProvider = QUrlResourceProvider::defaultProvider()) + else if (auto defaultProvider = QTextDocumentResourceProvider::defaultProvider()) r = defaultProvider->resource(url); } } @@ -2132,7 +2132,7 @@ void QTextDocument::addResource(int type, const QUrl &name, const QVariant &reso Returns the resource provider for this text document. */ -QUrlResourceProvider *QTextDocument::resourceProvider() const +QTextDocumentResourceProvider *QTextDocument::resourceProvider() const { Q_D(const QTextDocument); return d->resourceProvider; @@ -2145,7 +2145,7 @@ QUrlResourceProvider *QTextDocument::resourceProvider() const \note The text document \e{does not} take ownership of the \a provider. */ -void QTextDocument::setResourceProvider(QUrlResourceProvider *provider) +void QTextDocument::setResourceProvider(QTextDocumentResourceProvider *provider) { Q_D(QTextDocument); d->resourceProvider = provider; @@ -2167,6 +2167,8 @@ void QTextDocument::setResourceProvider(QUrlResourceProvider *provider) loadResource method such as QTextEdit, QTextBrowser or a QTextDocument itself then the default implementation tries to retrieve the data from the parent. + + \sa QTextDocumentResourceProvider */ QVariant QTextDocument::loadResource(int type, const QUrl &name) { diff --git a/src/gui/text/qtextdocument.h b/src/gui/text/qtextdocument.h index ea57b697a8..286b1ed299 100644 --- a/src/gui/text/qtextdocument.h +++ b/src/gui/text/qtextdocument.h @@ -68,7 +68,7 @@ class QVariant; class QRectF; class QTextOption; class QTextCursor; -class QUrlResourceProvider; +class QTextDocumentResourceProvider; namespace Qt @@ -240,8 +240,8 @@ public: QVariant resource(int type, const QUrl &name) const; void addResource(int type, const QUrl &name, const QVariant &resource); - QUrlResourceProvider *resourceProvider() const; - void setResourceProvider(QUrlResourceProvider *provider); + QTextDocumentResourceProvider *resourceProvider() const; + void setResourceProvider(QTextDocumentResourceProvider *provider); QList allFormats() const; diff --git a/src/gui/text/qtextdocument_p.cpp b/src/gui/text/qtextdocument_p.cpp index 1f2e8d2da2..eb62168a31 100644 --- a/src/gui/text/qtextdocument_p.cpp +++ b/src/gui/text/qtextdocument_p.cpp @@ -52,7 +52,7 @@ #include "qtextdocumentlayout_p.h" #include "qtexttable.h" #include "qtextengine_p.h" -#include "qurlresourceprovider.h" +#include "qtextdocumentresourceprovider.h" #include diff --git a/src/gui/text/qtextdocument_p.h b/src/gui/text/qtextdocument_p.h index d04113b320..f68cd56d88 100644 --- a/src/gui/text/qtextdocument_p.h +++ b/src/gui/text/qtextdocument_p.h @@ -366,7 +366,7 @@ private: QMap objects; QMap resources; QMap cachedResources; - QUrlResourceProvider *resourceProvider; + QTextDocumentResourceProvider *resourceProvider; QString defaultStyleSheet; int lastBlockCount; diff --git a/src/gui/text/qurlresourceprovider.cpp b/src/gui/text/qtextdocumentresourceprovider.cpp similarity index 71% rename from src/gui/text/qurlresourceprovider.cpp rename to src/gui/text/qtextdocumentresourceprovider.cpp index 6a50504471..1a03e1bb44 100644 --- a/src/gui/text/qurlresourceprovider.cpp +++ b/src/gui/text/qtextdocumentresourceprovider.cpp @@ -37,50 +37,62 @@ ** ****************************************************************************/ -#include "qurlresourceprovider.h" +#include "qtextdocumentresourceprovider.h" #include QT_BEGIN_NAMESPACE /*! - \class QUrlResourceProvider + \class QTextDocumentResourceProvider \inmodule QtGui \since 6.1 - \brief The QUrlResourceProvider is the base class of resource providers for QTextDocument. + \brief The QTextDocumentResourceProvider is the base class of resource providers for QTextDocument. + + Override resource() in a subclass, and set a subclass instance on a text document via + QTextDocument::setResourceProvider, or on a label via QLabel::setResourceProvider. This + allows customizing how resources are loaded in rich text documents without having to subclass + QTextDocument or QLabel, respectively. + \note An implementation should be thread-safe if it can be accessed from different threads, - e.g. when the default resource provider lives in the main thread and a QTexDocument lives + e.g. when the default resource provider lives in the main thread and a QTextDocument lives outside the main thread. */ -static QAtomicPointer qt_provider; +static QAtomicPointer qt_provider; /*! Destroys the resource provider. */ -QUrlResourceProvider::~QUrlResourceProvider() +QTextDocumentResourceProvider::~QTextDocumentResourceProvider() { qt_provider.testAndSetRelease(this, nullptr); } /*! - \fn virtual QVariant QUrlResourceProvider::resource(const QUrl &url) = 0; + \fn virtual QVariant QTextDocumentResourceProvider::resource(const QUrl &url) = 0; Returns data specified by the \a url. + + \sa QTextDocument::loadResource */ /*! Returns the default resource provider. + + \sa QTextDocument::loadResource */ -QUrlResourceProvider *QUrlResourceProvider::defaultProvider() +QTextDocumentResourceProvider *QTextDocumentResourceProvider::defaultProvider() { return qt_provider.loadAcquire(); } /*! Set the default resource provider to \a provider. + + \sa QTextDocument::loadResource */ -void QUrlResourceProvider::setDefaultProvider(QUrlResourceProvider *provider) +void QTextDocumentResourceProvider::setDefaultProvider(QTextDocumentResourceProvider *provider) { qt_provider.storeRelease(provider); } diff --git a/src/gui/text/qurlresourceprovider.h b/src/gui/text/qtextdocumentresourceprovider.h similarity index 82% rename from src/gui/text/qurlresourceprovider.h rename to src/gui/text/qtextdocumentresourceprovider.h index 088a878d8c..5fd8699e7c 100644 --- a/src/gui/text/qurlresourceprovider.h +++ b/src/gui/text/qtextdocumentresourceprovider.h @@ -37,8 +37,8 @@ ** ****************************************************************************/ -#ifndef QURLRESOURCEPROVIDER_H -#define QURLRESOURCEPROVIDER_H +#ifndef QTEXTDOCUMENTRESOURCEPROVIDER_H +#define QTEXTDOCUMENTRESOURCEPROVIDER_H #include #include @@ -46,18 +46,18 @@ QT_BEGIN_NAMESPACE -class Q_GUI_EXPORT QUrlResourceProvider +class Q_GUI_EXPORT QTextDocumentResourceProvider { - Q_DISABLE_COPY(QUrlResourceProvider) + Q_DISABLE_COPY(QTextDocumentResourceProvider) public: - QUrlResourceProvider() = default; - virtual ~QUrlResourceProvider(); + QTextDocumentResourceProvider() = default; + virtual ~QTextDocumentResourceProvider(); virtual QVariant resource(const QUrl &url) = 0; - static QUrlResourceProvider *defaultProvider(); - static void setDefaultProvider(QUrlResourceProvider *provider); + static QTextDocumentResourceProvider *defaultProvider(); + static void setDefaultProvider(QTextDocumentResourceProvider *provider); }; QT_END_NAMESPACE -#endif // QURLRESOURCEPROVIDER_H +#endif // QTEXTDOCUMENTRESOURCEPROVIDER_H diff --git a/src/widgets/widgets/qlabel.cpp b/src/widgets/widgets/qlabel.cpp index 189a7488d3..d2f176c67d 100644 --- a/src/widgets/widgets/qlabel.cpp +++ b/src/widgets/widgets/qlabel.cpp @@ -1429,7 +1429,7 @@ void QLabel::setTextFormat(Qt::TextFormat format) Returns the resource provider for rich text of this label. */ -QUrlResourceProvider *QLabel::resourceProvider() const +QTextDocumentResourceProvider *QLabel::resourceProvider() const { Q_D(const QLabel); return d->control ? d->control->document()->resourceProvider() : d->resourceProvider; @@ -1442,7 +1442,7 @@ QUrlResourceProvider *QLabel::resourceProvider() const \note The label \e{does not} take ownership of the \a provider. */ -void QLabel::setResourceProvider(QUrlResourceProvider *provider) +void QLabel::setResourceProvider(QTextDocumentResourceProvider *provider) { Q_D(QLabel); d->resourceProvider = provider; diff --git a/src/widgets/widgets/qlabel.h b/src/widgets/widgets/qlabel.h index 69aea6d57a..1b6e946476 100644 --- a/src/widgets/widgets/qlabel.h +++ b/src/widgets/widgets/qlabel.h @@ -49,7 +49,7 @@ QT_REQUIRE_CONFIG(label); QT_BEGIN_NAMESPACE -class QUrlResourceProvider; +class QTextDocumentResourceProvider; class QLabelPrivate; class Q_WIDGETS_EXPORT QLabel : public QFrame @@ -93,8 +93,8 @@ public: Qt::TextFormat textFormat() const; void setTextFormat(Qt::TextFormat); - QUrlResourceProvider *resourceProvider() const; - void setResourceProvider(QUrlResourceProvider *provider); + QTextDocumentResourceProvider *resourceProvider() const; + void setResourceProvider(QTextDocumentResourceProvider *provider); Qt::Alignment alignment() const; void setAlignment(Qt::Alignment); diff --git a/src/widgets/widgets/qlabel_p.h b/src/widgets/widgets/qlabel_p.h index 80243239d2..635042a8cd 100644 --- a/src/widgets/widgets/qlabel_p.h +++ b/src/widgets/widgets/qlabel_p.h @@ -154,7 +154,7 @@ public: #endif uint openExternalLinks : 1; // <-- space for more bit field values here - QUrlResourceProvider *resourceProvider; + QTextDocumentResourceProvider *resourceProvider; friend class QMessageBoxPrivate; }; diff --git a/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp b/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp index 4e0b7f46df..0e2765922c 100644 --- a/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp +++ b/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp @@ -47,7 +47,7 @@ #include #include #include -#include +#include #include "common.h" // #define DEBUG_WRITE_OUTPUT @@ -3596,7 +3596,7 @@ void tst_QTextDocument::clearUndoRedoStacks() QVERIFY(!doc.isUndoAvailable()); } -class UrlResourceProvider : public QUrlResourceProvider +class UrlResourceProvider : public QTextDocumentResourceProvider { public: QVariant resource(const QUrl &url) override diff --git a/tests/auto/widgets/widgets/qlabel/tst_qlabel.cpp b/tests/auto/widgets/widgets/qlabel/tst_qlabel.cpp index 5eed823a34..e4e9d12fb4 100644 --- a/tests/auto/widgets/widgets/qlabel/tst_qlabel.cpp +++ b/tests/auto/widgets/widgets/qlabel/tst_qlabel.cpp @@ -40,7 +40,7 @@ #include #include #include -#include +#include #include class Widget : public QWidget @@ -599,7 +599,7 @@ void tst_QLabel::taskQTBUG_48157_dprMovie() QCOMPARE(label.sizeHint(), movie.currentPixmap().size() / movie.currentPixmap().devicePixelRatio()); } -class UrlResourceProvider : public QUrlResourceProvider +class UrlResourceProvider : public QTextDocumentResourceProvider { public: QVariant resource(const QUrl &url) override