QMimeData: export URLs as text too
authorDavid Faure <faure@kde.org>
Sat, 11 Feb 2012 01:13:22 +0000 (02:13 +0100)
committerQt by Nokia <qt-info@nokia.com>
Fri, 17 Feb 2012 04:26:47 +0000 (05:26 +0100)
This allows to drop or paste them into lineedits and text widgets
(including such widgets in non-Qt applications)

Implementation note: this is done on-demand rather than in setUrls
so that it's still possible to setText explicitely; the new code
is only a fallback for when no text/plain data is available.

Change-Id: Ie90c43a30bfa64a6047b627e7351d20bf5ec8e03
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
src/corelib/kernel/qmimedata.cpp
tests/auto/corelib/kernel/qmimedata/tst_qmimedata.cpp

index 25e2b35..cfe985d 100644 (file)
@@ -105,6 +105,28 @@ QVariant QMimeDataPrivate::retrieveTypedData(const QString &format, QVariant::Ty
     Q_Q(const QMimeData);
 
     QVariant data = q->retrieveData(format, type);
+
+    // Text data requested: fallback to URL data if available
+    if (format == QLatin1String("text/plain") && !data.isValid()) {
+        data = retrieveTypedData(QLatin1String("text/uri-list"), QVariant::List);
+        if (data.type() == QVariant::Url) {
+            data = QVariant(data.toUrl().toDisplayString());
+        } else if (data.type() == QVariant::List) {
+            QString text;
+            int numUrls = 0;
+            const QList<QVariant> list = data.toList();
+            for (int i = 0; i < list.size(); ++i) {
+                if (list.at(i).type() == QVariant::Url) {
+                    text.append(list.at(i).toUrl().toDisplayString() + QLatin1Char('\n'));
+                    ++numUrls;
+                }
+            }
+            if (numUrls == 1)
+                text.chop(1); // no final '\n' if there's only one URL
+            data = QVariant(text);
+        }
+    }
+
     if (data.type() == type || !data.isValid())
         return data;
 
@@ -326,6 +348,10 @@ QList<QUrl> QMimeData::urls() const
 
     URLs correspond to the MIME type \c text/uri-list.
 
+    Since Qt 5.0, setUrls also exports the urls as plain text, if setText
+    was not called before, to make it possible to drop them into any lineedit
+    and text editor.
+
     \sa hasUrls(), setData()
 */
 void QMimeData::setUrls(const QList<QUrl> &urls)
@@ -385,7 +411,7 @@ void QMimeData::setText(const QString &text)
 */
 bool QMimeData::hasText() const
 {
-    return hasFormat(QLatin1String("text/plain"));
+    return hasFormat(QLatin1String("text/plain")) || hasUrls();
 }
 
 /*!
index 09f1760..f9a6bae 100644 (file)
@@ -317,19 +317,22 @@ void tst_QMimeData::setUrls() const
     longUrlList += QUrl("http://www.google.com");
 
     // verify initial state
-    QVERIFY(mimeData.hasUrls() == false);
+    QCOMPARE(mimeData.hasUrls(), false);
 
     // set a few, verify
     mimeData.setUrls(shortUrlList);
     QCOMPARE(mimeData.urls(), shortUrlList);
+    QCOMPARE(mimeData.text(), QString("http://qt.nokia.com"));
 
     // change them, verify
     mimeData.setUrls(longUrlList);
     QCOMPARE(mimeData.urls(), longUrlList);
+    QCOMPARE(mimeData.text(), QString("http://qt.nokia.com\nhttp://www.google.com\n"));
 
     // clear, verify
     mimeData.clear();
-    QVERIFY(mimeData.hasUrls() == false);
+    QCOMPARE(mimeData.hasUrls(), false);
+    QCOMPARE(mimeData.hasText(), false);
 }
 
 QTEST_MAIN(tst_QMimeData)