diff options
Diffstat (limited to 'src/gui/text')
-rw-r--r-- | src/gui/text/qtextdocument.cpp | 80 | ||||
-rw-r--r-- | src/gui/text/qtextdocument.h | 8 | ||||
-rw-r--r-- | src/gui/text/qtextdocument_p.h | 1 |
3 files changed, 65 insertions, 24 deletions
diff --git a/src/gui/text/qtextdocument.cpp b/src/gui/text/qtextdocument.cpp index 30e0f32547..180b09be33 100644 --- a/src/gui/text/qtextdocument.cpp +++ b/src/gui/text/qtextdocument.cpp @@ -551,6 +551,39 @@ void QTextDocument::setDefaultTextOption(const QTextOption &option) } /*! + \property QTextDocument::baseUrl + \since 5.3 + \brief the base URL used to resolve relative resource URLs within the document. + + Resource URLs are resolved to be within the same directory as the target of the base + URL meaning any portion of the path after the last '/' will be ignored. + + \table + \header \li Base URL \li Relative URL \li Resolved URL + \row \li file:///path/to/content \li images/logo.png \li file:///path/to/images/logo.png + \row \li file:///path/to/content/ \li images/logo.png \li file:///path/to/content/images/logo.png + \row \li file:///path/to/content/index.html \li images/logo.png \li file:///path/to/content/images/logo.png + \row \li file:///path/to/content/images/ \li ../images/logo.png \li file:///path/to/content/images/logo.png + \endtable +*/ +QUrl QTextDocument::baseUrl() const +{ + Q_D(const QTextDocument); + return d->baseUrl; +} + +void QTextDocument::setBaseUrl(const QUrl &url) +{ + Q_D(QTextDocument); + if (d->baseUrl != url) { + d->baseUrl = url; + if (d->lout) + d->lout->documentChanged(0, 0, d->length()); + emit baseUrlChanged(url); + } +} + +/*! \since 4.8 The default cursor movement style is used by all QTextCursor objects @@ -1849,11 +1882,12 @@ void QTextDocument::print(QPagedPaintDevice *printer) const QVariant QTextDocument::resource(int type, const QUrl &name) const { Q_D(const QTextDocument); - QVariant r = d->resources.value(name); + const QUrl url = d->baseUrl.resolved(name); + QVariant r = d->resources.value(url); if (!r.isValid()) { - r = d->cachedResources.value(name); + r = d->cachedResources.value(url); if (!r.isValid()) - r = const_cast<QTextDocument *>(this)->loadResource(type, name); + r = const_cast<QTextDocument *>(this)->loadResource(type, url); } return r; } @@ -1924,27 +1958,29 @@ QVariant QTextDocument::loadResource(int type, const QUrl &name) } // if resource was not loaded try to load it here - if (!qobject_cast<QTextDocument *>(p) && r.isNull() && name.isRelative()) { - QUrl currentURL = d->url; + if (!qobject_cast<QTextDocument *>(p) && r.isNull()) { QUrl resourceUrl = name; - // For the second case QUrl can merge "#someanchor" with "foo.html" - // correctly to "foo.html#someanchor" - if (!(currentURL.isRelative() - || (currentURL.scheme() == QLatin1String("file") - && !QFileInfo(currentURL.toLocalFile()).isAbsolute())) - || (name.hasFragment() && name.path().isEmpty())) { - resourceUrl = currentURL.resolved(name); - } else { - // this is our last resort when current url and new url are both relative - // we try to resolve against the current working directory in the local - // file system. - QFileInfo fi(currentURL.toLocalFile()); - if (fi.exists()) { - resourceUrl = - QUrl::fromLocalFile(fi.absolutePath() + QDir::separator()).resolved(name); - } else if (currentURL.isEmpty()) { - resourceUrl.setScheme(QLatin1String("file")); + if (name.isRelative()) { + QUrl currentURL = d->url; + // For the second case QUrl can merge "#someanchor" with "foo.html" + // correctly to "foo.html#someanchor" + if (!(currentURL.isRelative() + || (currentURL.scheme() == QLatin1String("file") + && !QFileInfo(currentURL.toLocalFile()).isAbsolute())) + || (name.hasFragment() && name.path().isEmpty())) { + resourceUrl = currentURL.resolved(name); + } else { + // this is our last resort when current url and new url are both relative + // we try to resolve against the current working directory in the local + // file system. + QFileInfo fi(currentURL.toLocalFile()); + if (fi.exists()) { + resourceUrl = + QUrl::fromLocalFile(fi.absolutePath() + QDir::separator()).resolved(name); + } else if (currentURL.isEmpty()) { + resourceUrl.setScheme(QLatin1String("file")); + } } } diff --git a/src/gui/text/qtextdocument.h b/src/gui/text/qtextdocument.h index a85e9c86c9..24e93b7e63 100644 --- a/src/gui/text/qtextdocument.h +++ b/src/gui/text/qtextdocument.h @@ -47,6 +47,7 @@ #include <QtCore/qrect.h> #include <QtCore/qvariant.h> #include <QtGui/qfont.h> +#include <QtCore/qurl.h> QT_BEGIN_NAMESPACE @@ -63,7 +64,6 @@ class QTextFormat; class QTextFrame; class QTextBlock; class QTextCodec; -class QUrl; class QVariant; class QRectF; class QTextOption; @@ -114,6 +114,7 @@ class Q_GUI_EXPORT QTextDocument : public QObject Q_PROPERTY(int maximumBlockCount READ maximumBlockCount WRITE setMaximumBlockCount) Q_PROPERTY(qreal documentMargin READ documentMargin WRITE setDocumentMargin) QDOC_PROPERTY(QTextOption defaultTextOption READ defaultTextOption WRITE setDefaultTextOption) + Q_PROPERTY(QUrl baseUrl READ baseUrl WRITE setBaseUrl NOTIFY baseUrlChanged) public: explicit QTextDocument(QObject *parent = 0); @@ -256,6 +257,9 @@ public: QTextOption defaultTextOption() const; void setDefaultTextOption(const QTextOption &option); + QUrl baseUrl() const; + void setBaseUrl(const QUrl &url); + Qt::CursorMoveStyle defaultCursorMoveStyle() const; void setDefaultCursorMoveStyle(Qt::CursorMoveStyle style); @@ -268,7 +272,7 @@ Q_SIGNALS: void modificationChanged(bool m); void cursorPositionChanged(const QTextCursor &cursor); void blockCountChanged(int newBlockCount); - + void baseUrlChanged(const QUrl &url); void documentLayoutChanged(); public Q_SLOTS: diff --git a/src/gui/text/qtextdocument_p.h b/src/gui/text/qtextdocument_p.h index 1394e88465..b3358161c5 100644 --- a/src/gui/text/qtextdocument_p.h +++ b/src/gui/text/qtextdocument_p.h @@ -355,6 +355,7 @@ public: QString url; qreal indentWidth; qreal documentMargin; + QUrl baseUrl; void mergeCachedResources(const QTextDocumentPrivate *priv); |