diff options
author | David Faure <faure@kde.org> | 2012-02-11 02:13:22 +0100 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-02-17 05:26:47 +0100 |
commit | 1209fccaf7c78f46f7d30cf729ed34e12e295e62 (patch) | |
tree | 9f6b52b604ad4d7c7f265c884d0ee7babb3e4520 /src/corelib/kernel/qmimedata.cpp | |
parent | cb8445f0323b0eefbb04f1d8adad81a00b53abd8 (diff) |
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 <lars.knoll@nokia.com>
Diffstat (limited to 'src/corelib/kernel/qmimedata.cpp')
-rw-r--r-- | src/corelib/kernel/qmimedata.cpp | 28 |
1 files changed, 27 insertions, 1 deletions
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<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(); } /*! |