Delay initializing the canPaste property until it's requested.
authorAndrew den Exter <andrew.den-exter@nokia.com>
Tue, 20 Dec 2011 07:21:55 +0000 (17:21 +1000)
committerQt by Nokia <qt-info@nokia.com>
Wed, 21 Dec 2011 00:36:57 +0000 (01:36 +0100)
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 <martin.jones@nokia.com>

src/quick/items/qquicktextcontrol.cpp
src/quick/items/qquicktextedit.cpp
src/quick/items/qquicktextedit_p_p.h
src/quick/items/qquicktextinput.cpp
src/quick/items/qquicktextinput_p_p.h

index 90483e5..13b3381 100644 (file)
@@ -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)
index 107da16..c25f305 100644 (file)
@@ -1596,6 +1596,10 @@ QSGNode *QQuickTextEdit::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *
 bool QQuickTextEdit::canPaste() const
 {
     Q_D(const QQuickTextEdit);
+    if (!d->canPasteValid) {
+        const_cast<QQuickTextEditPrivate *>(d)->canPaste = d->control->canPaste();
+        const_cast<QQuickTextEditPrivate *>(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();
 }
 
index 9886aba..4ac3dcb 100644 (file)
@@ -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;
index b3387f5..b58e57e 100644 (file)
@@ -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<QQuickTextInputPrivate *>(d)->canPaste = !d->m_readOnly && mimeData->hasText();
+        const_cast<QQuickTextInputPrivate *>(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
index 804adf4..fafb181 100644 (file)
@@ -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;