QVariant QQuickTextDocumentWithImageResources::loadResource(int type, const QUrl &name)
{
QDeclarativeContext *context = qmlContext(parent());
- QUrl url = context->resolvedUrl(name);
+ QUrl url = m_baseUrl.resolved(name);
if (type == QTextDocument::ImageResource) {
QDeclarativePixmap *p = loadPixmap(context, url);
QSizeF size(width, height);
if (!hasWidth || !hasHeight) {
QDeclarativeContext *context = qmlContext(parent());
- QUrl url = context->resolvedUrl(QUrl(imageFormat.name()));
+ QUrl url = m_baseUrl.resolved(QUrl(imageFormat.name()));
QDeclarativePixmap *p = loadPixmap(context, url);
if (!p->isReady()) {
QImage QQuickTextDocumentWithImageResources::image(const QTextImageFormat &format)
{
QDeclarativeContext *context = qmlContext(parent());
- QUrl url = context->resolvedUrl(QUrl(format.name()));
+ QUrl url = m_baseUrl.resolved(QUrl(format.name()));
QDeclarativePixmap *p = loadPixmap(context, url);
return p->image();
}
+void QQuickTextDocumentWithImageResources::setBaseUrl(const QUrl &url, bool clear)
+{
+ m_baseUrl = url;
+ if (clear) {
+ clearResources();
+ markContentsDirty(0, characterCount());
+ }
+}
+
QDeclarativePixmap *QQuickTextDocumentWithImageResources::loadPixmap(
QDeclarativeContext *context, const QUrl &url)
{
Q_Q(QQuickText);
doc = new QQuickTextDocumentWithImageResources(q);
doc->setDocumentMargin(0);
+ doc->setBaseUrl(q->baseUrl());
FAST_CONNECT(doc, SIGNAL(imagesLoaded()), q, SLOT(q_imagesLoaded()));
}
}
emit elideModeChanged(d->elideMode);
}
+/*!
+ \qmlproperty url QtQuick2::Text::baseUrl
+
+ This property specifies a base URL which is used to resolve relative URLs
+ within the text.
+
+ By default is the url of the Text element.
+*/
+
+QUrl QQuickText::baseUrl() const
+{
+ Q_D(const QQuickText);
+ if (d->baseUrl.isEmpty()) {
+ if (QDeclarativeContext *context = qmlContext(this))
+ const_cast<QQuickTextPrivate *>(d)->baseUrl = context->baseUrl();
+ }
+ return d->baseUrl;
+}
+
+void QQuickText::setBaseUrl(const QUrl &url)
+{
+ Q_D(QQuickText);
+ if (baseUrl() != url) {
+ d->baseUrl = url;
+
+ if (d->doc)
+ d->doc->setBaseUrl(url);
+ emit baseUrlChanged();
+ }
+}
+
+void QQuickText::resetBaseUrl()
+{
+ if (QDeclarativeContext *context = qmlContext(this))
+ setBaseUrl(context->baseUrl());
+ else
+ setBaseUrl(QUrl());
+}
+
/*! \internal */
QRectF QQuickText::boundingRect() const
{
Q_PROPERTY(qreal paintedHeight READ paintedHeight NOTIFY paintedSizeChanged)
Q_PROPERTY(qreal lineHeight READ lineHeight WRITE setLineHeight NOTIFY lineHeightChanged)
Q_PROPERTY(LineHeightMode lineHeightMode READ lineHeightMode WRITE setLineHeightMode NOTIFY lineHeightModeChanged)
+ Q_PROPERTY(QUrl baseUrl READ baseUrl WRITE setBaseUrl RESET resetBaseUrl NOTIFY baseUrlChanged)
public:
QQuickText(QQuickItem *parent=0);
LineHeightMode lineHeightMode() const;
void setLineHeightMode(LineHeightMode);
+ QUrl baseUrl() const;
+ void setBaseUrl(const QUrl &url);
+ void resetBaseUrl();
+
virtual void componentComplete();
int resourcesLoading() const; // mainly for testing
void lineHeightModeChanged(LineHeightMode mode);
void effectiveHorizontalAlignmentChanged();
void lineLaidOut(QQuickTextLine *line);
+ void baseUrlChanged();
protected:
void mousePressEvent(QMouseEvent *event);
bool isLineLaidOutConnected();
QString text;
+ QUrl baseUrl;
QFont font;
QFont sourceFont;
QColor color;
QImage image(const QTextImageFormat &format);
+ void setBaseUrl(const QUrl &url, bool clear = true);
+
Q_SIGNALS:
void imagesLoaded();
private:
QHash<QUrl, QDeclarativePixmap *> m_resources;
+ QUrl m_baseUrl;
int outstanding;
static QSet<QUrl> errors;
}
/*!
+ \qmlproperty url QtQuick2::TextEdit::baseUrl
+
+ This property specifies a base URL which is used to resolve relative URLs
+ within the text.
+
+ By default is the url of the TextEdit element.
+*/
+
+QUrl QQuickTextEdit::baseUrl() const
+{
+ Q_D(const QQuickTextEdit);
+ if (d->baseUrl.isEmpty()) {
+ if (QDeclarativeContext *context = qmlContext(this))
+ const_cast<QQuickTextEditPrivate *>(d)->baseUrl = context->baseUrl();
+ }
+ return d->baseUrl;
+}
+
+void QQuickTextEdit::setBaseUrl(const QUrl &url)
+{
+ Q_D(QQuickTextEdit);
+ if (baseUrl() != url) {
+ d->baseUrl = url;
+
+ d->document->setBaseUrl(url, d->richText);
+ emit baseUrlChanged();
+ }
+}
+
+void QQuickTextEdit::resetBaseUrl()
+{
+ if (QDeclarativeContext *context = qmlContext(this))
+ setBaseUrl(context->baseUrl());
+ else
+ setBaseUrl(QUrl());
+}
+
+/*!
\qmlmethod rectangle QtQuick2::TextEdit::positionToRectangle(position)
Returns the rectangle at the given \a position in the text. The x, y,
Q_D(QQuickTextEdit);
QQuickImplicitSizeItem::componentComplete();
+ d->document->setBaseUrl(baseUrl(), d->richText);
if (d->richText)
d->useImageFallback = qmlEnableImageCache();
Q_PROPERTY(bool canUndo READ canUndo NOTIFY canUndoChanged)
Q_PROPERTY(bool canRedo READ canRedo NOTIFY canRedoChanged)
Q_PROPERTY(bool inputMethodComposing READ isInputMethodComposing NOTIFY inputMethodComposingChanged)
+ Q_PROPERTY(QUrl baseUrl READ baseUrl WRITE setBaseUrl RESET resetBaseUrl NOTIFY baseUrlChanged)
public:
QQuickTextEdit(QQuickItem *parent=0);
void canRedoChanged();
void inputMethodComposingChanged();
void effectiveHorizontalAlignmentChanged();
+ void baseUrlChanged();
public Q_SLOTS:
void selectAll();
qreal getImplicitWidth() const;
QString text;
+ QUrl baseUrl;
QFont font;
QFont sourceFont;
QColor color;
--- /dev/null
+import QtQuick 2.0
+
+Text {
+ textFormat: Text.RichText
+ text: "<img src='exists.png'>"
+ baseUrl: "http/"
+}
--- /dev/null
+import QtQuick 2.0
+
+Text {
+ textFormat: Text.RichText
+ text: "<img src='exists.png'>"
+ baseUrl: "http://127.0.0.1:14453/text.html"
+}
void alignments_data();
void alignments();
+ void baseUrl();
void embeddedImages_data();
void embeddedImages();
}
}
+void tst_qquicktext::baseUrl()
+{
+ QUrl localUrl("file:///tests/text.qml");
+ QUrl remoteUrl("http://qt.nokia.com/test.qml");
+
+ QDeclarativeComponent textComponent(&engine);
+ textComponent.setData("import QtQuick 2.0\n Text {}", localUrl);
+ QQuickText *textObject = qobject_cast<QQuickText *>(textComponent.create());
+
+ QCOMPARE(textObject->baseUrl(), localUrl);
+
+ QSignalSpy spy(textObject, SIGNAL(baseUrlChanged()));
+
+ textObject->setBaseUrl(localUrl);
+ QCOMPARE(textObject->baseUrl(), localUrl);
+ QCOMPARE(spy.count(), 0);
+
+ textObject->setBaseUrl(remoteUrl);
+ QCOMPARE(textObject->baseUrl(), remoteUrl);
+ QCOMPARE(spy.count(), 1);
+
+ textObject->resetBaseUrl();
+ QCOMPARE(textObject->baseUrl(), localUrl);
+ QCOMPARE(spy.count(), 2);
+}
+
void tst_qquicktext::embeddedImages_data()
{
QTest::addColumn<QUrl>("qmlfile");
QTest::newRow("local") << testFileUrl("embeddedImagesLocal.qml") << "";
QTest::newRow("local-error") << testFileUrl("embeddedImagesLocalError.qml")
<< testFileUrl("embeddedImagesLocalError.qml").toString()+":3:1: QML Text: Cannot open: " + testFileUrl("http/notexists.png").toString();
+ QTest::newRow("local") << testFileUrl("embeddedImagesLocalRelative.qml") << "";
QTest::newRow("remote") << testFileUrl("embeddedImagesRemote.qml") << "";
QTest::newRow("remote-error") << testFileUrl("embeddedImagesRemoteError.qml")
<< testFileUrl("embeddedImagesRemoteError.qml").toString()+":3:1: QML Text: Error downloading http://127.0.0.1:14453/notexists.png - server replied: Not found";
+ QTest::newRow("remote") << testFileUrl("embeddedImagesRemoteRelative.qml") << "";
}
void tst_qquicktext::embeddedImages()
--- /dev/null
+import QtQuick 2.0
+
+TextEdit {
+ textFormat: TextEdit.RichText
+ text: "<img src='exists.png'>"
+ baseUrl: "http/"
+}
--- /dev/null
+import QtQuick 2.0
+
+TextEdit {
+ textFormat: TextEdit.RichText
+ text: "<img src='exists.png'>"
+ baseUrl: "http://127.0.0.1:42332/text.html"
+}
void undo_keypressevents_data();
void undo_keypressevents();
+ void baseUrl();
void embeddedImages();
void embeddedImages_data();
QVERIFY(textEdit->text().isEmpty());
}
+void tst_qquicktextedit::baseUrl()
+{
+ QUrl localUrl("file:///tests/text.qml");
+ QUrl remoteUrl("http://qt.nokia.com/test.qml");
+
+ QDeclarativeComponent textComponent(&engine);
+ textComponent.setData("import QtQuick 2.0\n TextEdit {}", localUrl);
+ QQuickTextEdit *textObject = qobject_cast<QQuickTextEdit *>(textComponent.create());
+
+ QCOMPARE(textObject->baseUrl(), localUrl);
+
+ QSignalSpy spy(textObject, SIGNAL(baseUrlChanged()));
+
+ textObject->setBaseUrl(localUrl);
+ QCOMPARE(textObject->baseUrl(), localUrl);
+ QCOMPARE(spy.count(), 0);
+
+ textObject->setBaseUrl(remoteUrl);
+ QCOMPARE(textObject->baseUrl(), remoteUrl);
+ QCOMPARE(spy.count(), 1);
+
+ textObject->resetBaseUrl();
+ QCOMPARE(textObject->baseUrl(), localUrl);
+ QCOMPARE(spy.count(), 2);
+}
+
void tst_qquicktextedit::embeddedImages_data()
{
QTest::addColumn<QUrl>("qmlfile");
QTest::newRow("local") << testFileUrl("embeddedImagesLocal.qml") << "";
QTest::newRow("local-error") << testFileUrl("embeddedImagesLocalError.qml")
<< testFileUrl("embeddedImagesLocalError.qml").toString()+":3:1: QML TextEdit: Cannot open: " + testFileUrl("http/notexists.png").toString();
+ QTest::newRow("local") << testFileUrl("embeddedImagesLocalRelative.qml") << "";
QTest::newRow("remote") << testFileUrl("embeddedImagesRemote.qml") << "";
QTest::newRow("remote-error") << testFileUrl("embeddedImagesRemoteError.qml")
<< testFileUrl("embeddedImagesRemoteError.qml").toString()+":3:1: QML TextEdit: Error downloading http://127.0.0.1:42332/notexists.png - server replied: Not found";
+ QTest::newRow("remote") << testFileUrl("embeddedImagesRemoteRelative.qml") << "";
}
void tst_qquicktextedit::embeddedImages()