From: Andrew den Exter Date: Mon, 5 Dec 2011 01:36:55 +0000 (+1000) Subject: Add support for pixmap cache images to TextEdit. X-Git-Tag: upstream/5.2.1~3120 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d052d2f569324f9a5416ee69ad60d4d4ded84ad3;p=platform%2Fupstream%2Fqtdeclarative.git Add support for pixmap cache images to TextEdit. Re-use the QQuickTextDocumentWithImageResources class from Text to allow image resources to be loaded from the pixmap cache. Change-Id: I13170eefe664447b77f2980fb2fe7b4e2f8eb81c Reviewed-by: Yann Bodson --- diff --git a/src/quick/items/qquicktext.cpp b/src/quick/items/qquicktext.cpp index d015127..274ea93 100644 --- a/src/quick/items/qquicktext.cpp +++ b/src/quick/items/qquicktext.cpp @@ -67,29 +67,6 @@ QT_BEGIN_NAMESPACE extern Q_GUI_EXPORT bool qt_applefontsmoothing_enabled; -class QQuickTextDocumentWithImageResources : public QTextDocument { - Q_OBJECT - -public: - QQuickTextDocumentWithImageResources(QQuickText *parent); - virtual ~QQuickTextDocumentWithImageResources(); - - void setText(const QString &); - int resourcesLoading() const { return outstanding; } - -protected: - QVariant loadResource(int type, const QUrl &name); - -private slots: - void requestFinished(); - -private: - QHash m_resources; - - int outstanding; - static QSet errors; -}; - DEFINE_BOOL_CONFIG_OPTION(qmlDisableDistanceField, QML_DISABLE_DISTANCEFIELD) DEFINE_BOOL_CONFIG_OPTION(enableImageCache, QML_ENABLE_TEXT_IMAGE_CACHE); @@ -123,7 +100,7 @@ void QQuickTextPrivate::init() q->setFlag(QQuickItem::ItemHasContents); } -QQuickTextDocumentWithImageResources::QQuickTextDocumentWithImageResources(QQuickText *parent) +QQuickTextDocumentWithImageResources::QQuickTextDocumentWithImageResources(QQuickItem *parent) : QTextDocument(parent), outstanding(0) { setUndoRedoEnabled(false); @@ -171,25 +148,32 @@ void QQuickTextDocumentWithImageResources::requestFinished() { outstanding--; if (outstanding == 0) { - QQuickText *textItem = static_cast(parent()); - QString text = textItem->text(); -#ifndef QT_NO_TEXTHTMLPARSER - setHtml(text); -#else - setPlainText(text); -#endif - QQuickTextPrivate *d = QQuickTextPrivate::get(textItem); - d->updateLayout(); + markContentsDirty(0, characterCount()); + + if (QQuickText *item = qobject_cast(parent())) + QQuickTextPrivate::get(item)->updateLayout(); } } +void QQuickTextDocumentWithImageResources::clear() +{ + clearResources(); + + QTextDocument::clear(); +} + +void QQuickTextDocumentWithImageResources::clearResources() +{ + foreach (QDeclarativePixmap *pixmap, m_resources) + pixmap->clear(this); + qDeleteAll(m_resources); + m_resources.clear(); + outstanding = 0; +} + void QQuickTextDocumentWithImageResources::setText(const QString &text) { - if (!m_resources.isEmpty()) { - qDeleteAll(m_resources); - m_resources.clear(); - outstanding = 0; - } + clearResources(); #ifndef QT_NO_TEXTHTMLPARSER setHtml(text); @@ -1946,5 +1930,3 @@ void QQuickText::mouseReleaseEvent(QMouseEvent *event) } QT_END_NAMESPACE - -#include "qquicktext.moc" diff --git a/src/quick/items/qquicktext_p_p.h b/src/quick/items/qquicktext_p_p.h index 2035f47..756acbf 100644 --- a/src/quick/items/qquicktext_p_p.h +++ b/src/quick/items/qquicktext_p_p.h @@ -54,7 +54,7 @@ // We mean it. // -#include "qquickitem.h" +#include "qquicktext_p.h" #include "qquickimplicitsizeitem_p_p.h" #include @@ -164,6 +164,34 @@ public: #endif }; +class QDeclarativePixmap; +class QQuickTextDocumentWithImageResources : public QTextDocument { + Q_OBJECT + +public: + QQuickTextDocumentWithImageResources(QQuickItem *parent); + virtual ~QQuickTextDocumentWithImageResources(); + + void setText(const QString &); + int resourcesLoading() const { return outstanding; } + + void clearResources(); + + void clear(); + +protected: + QVariant loadResource(int type, const QUrl &name); + +private slots: + void requestFinished(); + +private: + QHash m_resources; + + int outstanding; + static QSet errors; +}; + QT_END_NAMESPACE #endif // QQUICKTEXT_P_P_H diff --git a/src/quick/items/qquicktextedit.cpp b/src/quick/items/qquicktextedit.cpp index c43a5be..254a133 100644 --- a/src/quick/items/qquicktextedit.cpp +++ b/src/quick/items/qquicktextedit.cpp @@ -41,6 +41,7 @@ #include "qquicktextedit_p.h" #include "qquicktextedit_p_p.h" +#include "qquicktext_p_p.h" #include "qquickevents_p_p.h" #include "qquickcanvas.h" #include "qquicktextnode_p.h" @@ -253,6 +254,7 @@ void QQuickTextEdit::setText(const QString &text) if (QQuickTextEdit::text() == text) return; + d->document->clearResources(); d->richText = d->format == RichText || (d->format == AutoText && Qt::mightBeRichText(text)); if (d->richText) { #ifndef QT_NO_TEXTHTMLPARSER @@ -1626,7 +1628,9 @@ void QQuickTextEditPrivate::init() q->setFlag(QQuickItem::ItemAcceptsInputMethod); q->setFlag(QQuickItem::ItemHasContents); - control = new QTextControl(q); + document = new QQuickTextDocumentWithImageResources(q); + + control = new QTextControl(document, q); control->setIgnoreUnusedNavigationEvents(true); control->setTextInteractionFlags(Qt::LinksAccessibleByMouse | Qt::TextSelectableByKeyboard | Qt::TextEditable); control->setDragEnabled(false); @@ -1664,7 +1668,6 @@ void QQuickTextEditPrivate::init() canPaste = control->canPaste(); #endif - document = control->document(); document->setDefaultFont(font); document->setDocumentMargin(textMargin); document->setUndoRedoEnabled(false); // flush undo buffer. diff --git a/src/quick/items/qquicktextedit_p_p.h b/src/quick/items/qquicktextedit_p_p.h index 7bcbe2a..583b13b 100644 --- a/src/quick/items/qquicktextedit_p_p.h +++ b/src/quick/items/qquicktextedit_p_p.h @@ -62,7 +62,7 @@ QT_BEGIN_NAMESPACE class QTextLayout; -class QTextDocument; +class QQuickTextDocumentWithImageResources; class QTextControl; class QQuickTextEditPrivate : public QQuickImplicitSizeItemPrivate { @@ -121,7 +121,7 @@ public: QDeclarativeComponent* cursorComponent; QQuickItem* cursor; QQuickTextEdit::TextFormat format; - QTextDocument *document; + QQuickTextDocumentWithImageResources *document; QTextControl *control; QQuickTextEdit::WrapMode wrapMode; QQuickTextEdit::SelectionMode mouseSelectionMode;