diff options
author | Aaron Kennedy <aaron.kennedy@nokia.com> | 2010-04-15 06:41:15 (GMT) |
---|---|---|
committer | Aaron Kennedy <aaron.kennedy@nokia.com> | 2010-04-15 06:41:15 (GMT) |
commit | d8a49ccfb8308aec11cd24e823c297ef157cc4c6 (patch) | |
tree | 18fc19e36264839be851553a892480ce575da11e /src/declarative | |
parent | 30a3e78a12b72ab6f67992ec2f06cd4f21a01a6c (diff) | |
parent | 62e52a19f1c7ac81a36f394e43f916c3c8933e8f (diff) | |
download | Qt-d8a49ccfb8308aec11cd24e823c297ef157cc4c6.zip Qt-d8a49ccfb8308aec11cd24e823c297ef157cc4c6.tar.gz Qt-d8a49ccfb8308aec11cd24e823c297ef157cc4c6.tar.bz2 |
Merge branch '4.7' of scm.dev.nokia.troll.no:qt/qt-qml into 4.7
Diffstat (limited to 'src/declarative')
-rw-r--r-- | src/declarative/graphicsitems/qdeclarativetext.cpp | 102 | ||||
-rw-r--r-- | src/declarative/graphicsitems/qdeclarativetext_p.h | 7 | ||||
-rw-r--r-- | src/declarative/graphicsitems/qdeclarativetext_p_p.h | 5 |
3 files changed, 104 insertions, 10 deletions
diff --git a/src/declarative/graphicsitems/qdeclarativetext.cpp b/src/declarative/graphicsitems/qdeclarativetext.cpp index 3a35dd5..3b39fa4 100644 --- a/src/declarative/graphicsitems/qdeclarativetext.cpp +++ b/src/declarative/graphicsitems/qdeclarativetext.cpp @@ -43,6 +43,7 @@ #include "private/qdeclarativetext_p_p.h" #include <qdeclarativestyledtext_p.h> #include <qdeclarativeinfo.h> +#include <qdeclarativepixmapcache_p.h> #include <QTextLayout> #include <QTextLine> @@ -55,6 +56,50 @@ QT_BEGIN_NAMESPACE +class QTextDocumentWithImageResources : public QTextDocument { + Q_OBJECT + +public: + QTextDocumentWithImageResources(QDeclarativeText *parent) : + QTextDocument(parent), + outstanding(0) + { + } + + int resourcesLoading() const { return outstanding; } + +protected: + QVariant loadResource(int type, const QUrl &name) + { + QUrl url = qmlContext(parent())->resolvedUrl(name); + + if (type == QTextDocument::ImageResource) { + QPixmap pm; + QDeclarativePixmapReply::Status status = QDeclarativePixmapCache::get(url, &pm, 0, true, 0, 0); + if (status == QDeclarativePixmapReply::Ready) + return pm; + if (status != QDeclarativePixmapReply::Error) { + QDeclarativePixmapReply *reply = QDeclarativePixmapCache::request(qmlEngine(parent()), url); + connect(reply, SIGNAL(finished()), this, SLOT(requestFinished())); + outstanding++; + static_cast<QDeclarativeText*>(parent())->reloadWithResources(); + } + } + + return QTextDocument::loadResource(type,url); // The *resolved* URL + } + +private slots: + void requestFinished() + { + outstanding--; + static_cast<QDeclarativeText*>(parent())->reloadWithResources(); + } + +private: + int outstanding; +}; + /*! \qmlclass Text QDeclarativeText \since 4.7 @@ -77,6 +122,9 @@ QT_BEGIN_NAMESPACE The \c elide property can alternatively be used to fit a single line of plain text to a set width. + Note that the \l{Supported HTML Subset} is limited, and that if IMG tags + load remote images, the text reloads (see resourcesLoading). + Text provides read-only text. For editable text, see \l TextEdit. */ @@ -259,11 +307,10 @@ void QDeclarativeText::setText(const QString &n) d->richText = d->format == RichText || (d->format == AutoText && Qt::mightBeRichText(n)); if (d->richText) { - if (!d->doc) { - d->doc = new QTextDocument(this); - d->doc->setDocumentMargin(0); + if (isComponentComplete()) { + d->ensureDoc(); + d->doc->setHtml(n); } - d->doc->setHtml(n); } d->text = n; @@ -550,11 +597,10 @@ void QDeclarativeText::setTextFormat(TextFormat format) d->updateLayout(); d->markImgDirty(); } else if (!wasRich && d->richText) { - if (!d->doc) { - d->doc = new QTextDocument(this); - d->doc->setDocumentMargin(0); + if (isComponentComplete()) { + d->ensureDoc(); + d->doc->setHtml(d->text); } - d->doc->setHtml(d->text); d->updateLayout(); d->markImgDirty(); } @@ -898,6 +944,39 @@ void QDeclarativeTextPrivate::checkImgCache() imgDirty = false; } +void QDeclarativeTextPrivate::ensureDoc() +{ + if (!doc) { + Q_Q(QDeclarativeText); + doc = new QTextDocumentWithImageResources(q); + doc->setDocumentMargin(0); + } +} + +void QDeclarativeText::reloadWithResources() +{ + Q_D(QDeclarativeText); + if (!d->richText) + return; + if (resourcesLoading()!=0) + return; + emit resourcesLoadingChanged(); + d->doc->setHtml(d->text); + d->updateLayout(); + d->markImgDirty(); +} + +/*! + \qmlproperty int Text::resourcesLoading + This property is the number of resources (images) that are being loaded asynchronously. +*/ +int QDeclarativeText::resourcesLoading() const +{ + Q_D(const QDeclarativeText); + return d->doc ? d->doc->resourcesLoading() : 0; +} + + void QDeclarativeText::paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *) { Q_D(QDeclarativeText); @@ -1011,6 +1090,10 @@ void QDeclarativeText::componentComplete() Q_D(QDeclarativeText); QDeclarativeItem::componentComplete(); if (d->dirty) { + if (d->richText) { + d->ensureDoc(); + d->doc->setHtml(d->text); + } d->updateLayout(); d->dirty = false; } @@ -1061,4 +1144,7 @@ void QDeclarativeText::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) if (!event->isAccepted()) QDeclarativeItem::mouseReleaseEvent(event); } + QT_END_NAMESPACE + +#include "qdeclarativetext.moc" diff --git a/src/declarative/graphicsitems/qdeclarativetext_p.h b/src/declarative/graphicsitems/qdeclarativetext_p.h index 871c833..fcad898 100644 --- a/src/declarative/graphicsitems/qdeclarativetext_p.h +++ b/src/declarative/graphicsitems/qdeclarativetext_p.h @@ -72,6 +72,7 @@ class Q_DECLARATIVE_EXPORT QDeclarativeText : public QDeclarativeItem Q_PROPERTY(bool wrap READ wrap WRITE setWrap NOTIFY wrapModeChanged) Q_PROPERTY(TextFormat textFormat READ textFormat WRITE setTextFormat NOTIFY textFormatChanged) Q_PROPERTY(TextElideMode elide READ elideMode WRITE setElideMode NOTIFY elideModeChanged) //### elideMode? + Q_PROPERTY(int resourcesLoading READ resourcesLoading NOTIFY resourcesLoadingChanged) public: QDeclarativeText(QDeclarativeItem *parent=0); @@ -138,6 +139,8 @@ public: virtual void componentComplete(); + int resourcesLoading() const; + Q_SIGNALS: void textChanged(const QString &text); void linkActivated(const QString &link); @@ -150,6 +153,7 @@ Q_SIGNALS: void wrapModeChanged(); void textFormatChanged(TextFormat textFormat); void elideModeChanged(TextElideMode mode); + void resourcesLoadingChanged(); protected: void mousePressEvent(QGraphicsSceneMouseEvent *event); @@ -160,6 +164,9 @@ protected: private: Q_DISABLE_COPY(QDeclarativeText) Q_DECLARE_PRIVATE_D(QGraphicsItem::d_ptr.data(), QDeclarativeText) + + friend class QTextDocumentWithImageResources; + void reloadWithResources(); }; QT_END_NAMESPACE diff --git a/src/declarative/graphicsitems/qdeclarativetext_p_p.h b/src/declarative/graphicsitems/qdeclarativetext_p_p.h index cc5a9f2..332f99e 100644 --- a/src/declarative/graphicsitems/qdeclarativetext_p_p.h +++ b/src/declarative/graphicsitems/qdeclarativetext_p_p.h @@ -63,7 +63,7 @@ QT_BEGIN_NAMESPACE class QTextLayout; -class QTextDocument; +class QTextDocumentWithImageResources; class QDeclarativeTextPrivate : public QDeclarativeItemPrivate { @@ -84,6 +84,7 @@ public: ~QDeclarativeTextPrivate(); + void ensureDoc(); void updateSize(); void updateLayout(); void markImgDirty() { @@ -118,7 +119,7 @@ public: bool richText:1; bool singleline:1; bool cache:1; - QTextDocument *doc; + QTextDocumentWithImageResources *doc; QTextLayout layout; QSize cachedLayoutSize; QDeclarativeText::TextFormat format; |