From 1209fccaf7c78f46f7d30cf729ed34e12e295e62 Mon Sep 17 00:00:00 2001 From: David Faure Date: Sat, 11 Feb 2012 02:13:22 +0100 Subject: QMimeData: export URLs as text too 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 --- src/corelib/kernel/qmimedata.cpp | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'src/corelib/kernel/qmimedata.cpp') diff --git a/src/corelib/kernel/qmimedata.cpp b/src/corelib/kernel/qmimedata.cpp index 25e2b3532b..cfe985da26 100644 --- a/src/corelib/kernel/qmimedata.cpp +++ b/src/corelib/kernel/qmimedata.cpp @@ -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 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 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 &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(); } /*! -- cgit v1.2.3