From: Andrew den Exter Date: Tue, 20 Dec 2011 07:21:55 +0000 (+1000) Subject: Delay initializing the canPaste property until it's requested. X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=3db5afa71443533eee6825a5fb0da29498f96b94;p=konrad%2Fqtdeclarative.git Delay initializing the canPaste property until it's requested. With xcb querying data from the clipboard can take as long as 50 ms. Deferring the check until it is requested speeds up construction time when it's not used and only checking the available formats when it is requested should also help. Change-Id: I796fc83d0457d9ac1490cfb9f510c6342eb8a872 Reviewed-by: Martin Jones --- diff --git a/src/quick/items/qquicktextcontrol.cpp b/src/quick/items/qquicktextcontrol.cpp index 90483e5..13b3381 100644 --- a/src/quick/items/qquicktextcontrol.cpp +++ b/src/quick/items/qquicktextcontrol.cpp @@ -1743,12 +1743,12 @@ bool QQuickTextControl::canInsertFromMimeData(const QMimeData *source) const { Q_D(const QQuickTextControl); if (d->acceptRichText) - return (source->hasText() && !source->text().isEmpty()) + return source->hasText() || source->hasHtml() || source->hasFormat(QLatin1String("application/x-qrichtext")) || source->hasFormat(QLatin1String("application/x-qt-richtext")); else - return source->hasText() && !source->text().isEmpty(); + return source->hasText(); } void QQuickTextControl::insertFromMimeData(const QMimeData *source) diff --git a/src/quick/items/qquicktextedit.cpp b/src/quick/items/qquicktextedit.cpp index 107da16..c25f305 100644 --- a/src/quick/items/qquicktextedit.cpp +++ b/src/quick/items/qquicktextedit.cpp @@ -1596,6 +1596,10 @@ QSGNode *QQuickTextEdit::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData * bool QQuickTextEdit::canPaste() const { Q_D(const QQuickTextEdit); + if (!d->canPasteValid) { + const_cast(d)->canPaste = d->control->canPaste(); + const_cast(d)->canPasteValid = true; + } return d->canPaste; } @@ -1656,7 +1660,6 @@ void QQuickTextEditPrivate::init() #ifndef QT_NO_CLIPBOARD QObject::connect(q, SIGNAL(readOnlyChanged(bool)), q, SLOT(q_canPasteChanged())); QObject::connect(QGuiApplication::clipboard(), SIGNAL(dataChanged()), q, SLOT(q_canPasteChanged())); - canPaste = control->canPaste(); #endif document->setDefaultFont(font); @@ -1971,7 +1974,9 @@ void QQuickTextEdit::q_canPasteChanged() Q_D(QQuickTextEdit); bool old = d->canPaste; d->canPaste = d->control->canPaste(); - if (old!=d->canPaste) + bool changed = old!=d->canPaste || !d->canPasteValid; + d->canPasteValid = true; + if (changed) emit canPasteChanged(); } diff --git a/src/quick/items/qquicktextedit_p_p.h b/src/quick/items/qquicktextedit_p_p.h index 9886aba..4ac3dcb 100644 --- a/src/quick/items/qquicktextedit_p_p.h +++ b/src/quick/items/qquicktextedit_p_p.h @@ -73,7 +73,7 @@ public: : color("black"), hAlign(QQuickTextEdit::AlignLeft), vAlign(QQuickTextEdit::AlignTop), documentDirty(true), dirty(false), richText(false), cursorVisible(false), focusOnPress(true), persistentSelection(true), requireImplicitWidth(false), selectByMouse(false), canPaste(false), - hAlignImplicit(true), rightToLeftText(false), useImageFallback(false), + canPasteValid(false), hAlignImplicit(true), rightToLeftText(false), useImageFallback(false), textMargin(0.0), lastSelectionStart(0), lastSelectionEnd(0), cursorComponent(0), cursor(0), format(QQuickTextEdit::PlainText), document(0), wrapMode(QQuickTextEdit::NoWrap), mouseSelectionMode(QQuickTextEdit::SelectCharacters), @@ -110,6 +110,7 @@ public: bool requireImplicitWidth:1; bool selectByMouse:1; bool canPaste:1; + bool canPasteValid:1; bool hAlignImplicit:1; bool rightToLeftText:1; bool useImageFallback:1; diff --git a/src/quick/items/qquicktextinput.cpp b/src/quick/items/qquicktextinput.cpp index b3387f5..b58e57e 100644 --- a/src/quick/items/qquicktextinput.cpp +++ b/src/quick/items/qquicktextinput.cpp @@ -1793,6 +1793,11 @@ void QQuickTextInput::setMouseSelectionMode(SelectionMode mode) bool QQuickTextInput::canPaste() const { Q_D(const QQuickTextInput); + if (!d->canPasteValid) { + if (const QMimeData *mimeData = QGuiApplication::clipboard()->mimeData()) + const_cast(d)->canPaste = !d->m_readOnly && mimeData->hasText(); + const_cast(d)->canPasteValid = true; + } return d->canPaste; } @@ -2043,7 +2048,6 @@ void QQuickTextInputPrivate::init() q, SLOT(q_canPasteChanged())); q->connect(QGuiApplication::clipboard(), SIGNAL(dataChanged()), q, SLOT(q_canPasteChanged())); - canPaste = !m_readOnly && QGuiApplication::clipboard()->text().length() != 0; #endif // QT_NO_CLIPBOARD m_textLayout.beginLayout(); m_textLayout.createLine(); @@ -2146,10 +2150,17 @@ void QQuickTextInput::q_canPasteChanged() Q_D(QQuickTextInput); bool old = d->canPaste; #ifndef QT_NO_CLIPBOARD - d->canPaste = !d->m_readOnly && QGuiApplication::clipboard()->text().length() != 0; + if (const QMimeData *mimeData = QGuiApplication::clipboard()->mimeData()) + d->canPaste = !d->m_readOnly && mimeData->hasText(); + else + d->canPaste = false; #endif - if (d->canPaste != old) + + bool changed = d->canPaste != old || !d->canPasteValid; + d->canPasteValid = true; + if (changed) emit canPasteChanged(); + } // ### these should come from QStyleHints diff --git a/src/quick/items/qquicktextinput_p_p.h b/src/quick/items/qquicktextinput_p_p.h index 804adf4..fafb181 100644 --- a/src/quick/items/qquicktextinput_p_p.h +++ b/src/quick/items/qquicktextinput_p_p.h @@ -112,6 +112,7 @@ public: , autoScroll(true) , selectByMouse(false) , canPaste(false) + , canPasteValid(false) , hAlignImplicit(true) , selectPressed(false) , textLayoutDirty(true) @@ -235,6 +236,7 @@ public: bool autoScroll:1; bool selectByMouse:1; bool canPaste:1; + bool canPasteValid:1; bool hAlignImplicit:1; bool selectPressed:1; bool textLayoutDirty:1;