Rename QUrlResourceProvider to QTextDocumentResourceProvider

While the class name is now a bit more than a mouthful, it's purpose
is very narrowly tied to QTextDocument, so don't use a very generic
name for it. That resources are provided based on a URL is to some
degree an implementation detail, and URLs are resource locators so
we don't need that in the class name.

Address code review comment for 6.1. Add documentation and links to
existing APIs with a similar purpose.

Task-number: QTBUG-90211
Task-number: QTBUG-92208
Pick-to: 6.1
Change-Id: I4f09057cc2f53a5595513c1c9422e6ccaad6ca13
Reviewed-by: Kai Koehne <kai.koehne@qt.io>
This commit is contained in:
Volker Hilsheimer 2021-03-25 10:20:44 +01:00
parent db1c238a66
commit c8f6f8a222
12 changed files with 52 additions and 38 deletions

View File

@ -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

View File

@ -46,7 +46,7 @@
#include "qtextdocumentfragment_p.h"
#include "qtexttable.h"
#include "qtextlist.h"
#include "qurlresourceprovider.h"
#include "qtextdocumentresourceprovider.h"
#include <qdebug.h>
#if QT_CONFIG(regularexpression)
#include <qregularexpression.h>
@ -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)
{

View File

@ -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<QTextFormat> allFormats() const;

View File

@ -52,7 +52,7 @@
#include "qtextdocumentlayout_p.h"
#include "qtexttable.h"
#include "qtextengine_p.h"
#include "qurlresourceprovider.h"
#include "qtextdocumentresourceprovider.h"
#include <stdlib.h>

View File

@ -366,7 +366,7 @@ private:
QMap<int, QTextObject *> objects;
QMap<QUrl, QVariant> resources;
QMap<QUrl, QVariant> cachedResources;
QUrlResourceProvider *resourceProvider;
QTextDocumentResourceProvider *resourceProvider;
QString defaultStyleSheet;
int lastBlockCount;

View File

@ -37,50 +37,62 @@
**
****************************************************************************/
#include "qurlresourceprovider.h"
#include "qtextdocumentresourceprovider.h"
#include <QtCore/qatomic.h>
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<QUrlResourceProvider> qt_provider;
static QAtomicPointer<QTextDocumentResourceProvider> 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);
}

View File

@ -37,8 +37,8 @@
**
****************************************************************************/
#ifndef QURLRESOURCEPROVIDER_H
#define QURLRESOURCEPROVIDER_H
#ifndef QTEXTDOCUMENTRESOURCEPROVIDER_H
#define QTEXTDOCUMENTRESOURCEPROVIDER_H
#include <QtGui/qtguiglobal.h>
#include <QtCore/qvariant.h>
@ -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

View File

@ -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;

View File

@ -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);

View File

@ -154,7 +154,7 @@ public:
#endif
uint openExternalLinks : 1;
// <-- space for more bit field values here
QUrlResourceProvider *resourceProvider;
QTextDocumentResourceProvider *resourceProvider;
friend class QMessageBoxPrivate;
};

View File

@ -47,7 +47,7 @@
#include <qimage.h>
#include <qtextlayout.h>
#include <QDomDocument>
#include <qurlresourceprovider.h>
#include <qtextdocumentresourceprovider.h>
#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

View File

@ -40,7 +40,7 @@
#include <qmessagebox.h>
#include <qfontmetrics.h>
#include <qmath.h>
#include <qurlresourceprovider.h>
#include <qtextdocumentresourceprovider.h>
#include <private/qlabel_p.h>
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