diff options
author | Aleix Pol <aleixpol@kde.org> | 2015-05-23 14:10:36 +0200 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@theqtcompany.com> | 2015-05-23 13:52:36 +0000 |
commit | dad48f0e9cca10528fcaab7ec7d08f678e81b1a9 (patch) | |
tree | b2673ae09e2a5d1399e4bdbe266885fec3f35bcc | |
parent | 1ebee6e995541554aa9250ccf3326d2579c237c6 (diff) |
Makes it possible to extract all data from a QML DropArea
There's many types that aren't being covered by DropArea API, such as
images. getDataAsString is not acceptable, since fromUtf8 chokes on
reading non-utf8.
This patch introduces getDataAsArrayBuffer method that won't try to convert
the data into a QString and simply pass a QV4::ArrayBuffer, that internally
keeps a QByteArray.
Change-Id: I65411b3d365ee6c5e13effd9167156771ee08a05
Reviewed-by: Simon Hausmann <simon.hausmann@theqtcompany.com>
-rw-r--r-- | src/qml/jsruntime/qv4engine.cpp | 2 | ||||
-rw-r--r-- | src/quick/items/qquickdroparea.cpp | 20 | ||||
-rw-r--r-- | src/quick/items/qquickdroparea_p.h | 1 | ||||
-rw-r--r-- | tests/auto/quick/qquickdroparea/tst_qquickdroparea.cpp | 29 |
4 files changed, 52 insertions, 0 deletions
diff --git a/src/qml/jsruntime/qv4engine.cpp b/src/qml/jsruntime/qv4engine.cpp index 5e450859ec..ba6f5a3b79 100644 --- a/src/qml/jsruntime/qv4engine.cpp +++ b/src/qml/jsruntime/qv4engine.cpp @@ -1266,6 +1266,8 @@ static QVariant toVariant(QV4::ExecutionEngine *e, const QV4::Value &value, int return ld->d()->locale; if (QV4::DateObject *d = value.asDateObject()) return d->toQDateTime(); + if (QV4::ArrayBuffer *d = value.as<ArrayBuffer>()) + return d->asByteArray(); // NOTE: since we convert QTime to JS Date, round trip will change the variant type (to QDateTime)! QV4::ScopedObject o(scope, value); diff --git a/src/quick/items/qquickdroparea.cpp b/src/quick/items/qquickdroparea.cpp index 217b455d43..e46d546a16 100644 --- a/src/quick/items/qquickdroparea.cpp +++ b/src/quick/items/qquickdroparea.cpp @@ -36,6 +36,7 @@ #include "qquickitem_p.h" #include "qquickwindow.h" +#include <private/qv4arraybuffer_p.h> #include <private/qqmlengine_p.h> #ifndef QT_NO_DRAGANDDROP @@ -496,6 +497,14 @@ void QQuickDropArea::dropEvent(QDropEvent *event) Returns the data for the given \a format converted to a string. \a format should be one contained in the \l formats property. */ +/*! + \qmlmethod string QtQuick::DragEvent::getDataAsArrayBuffer(string format) + \since 5.5 + + Returns the data for the given \a format into an ArrayBuffer, which can + easily be translated into a QByteArray. \a format should be one contained in the \l formats property. +*/ + QObject *QQuickDropEvent::source() { if (const QQuickDragMimeData *dragMime = qobject_cast<const QQuickDragMimeData *>(event->mimeData())) @@ -566,6 +575,17 @@ void QQuickDropEvent::getDataAsString(QQmlV4Function *args) } } +void QQuickDropEvent::getDataAsArrayBuffer(QQmlV4Function *args) +{ + if (args->length() != 0) { + QV4::ExecutionEngine *v4 = args->v4engine(); + QV4::Scope scope(v4); + QV4::ScopedValue v(scope, (*args)[0]); + const QString format = v->toQString(); + args->setReturnValue(v4->newArrayBuffer(event->mimeData()->data(format))->asReturnedValue()); + } +} + void QQuickDropEvent::acceptProposedAction(QQmlV4Function *) { event->acceptProposedAction(); diff --git a/src/quick/items/qquickdroparea_p.h b/src/quick/items/qquickdroparea_p.h index e35ec40757..3223f4203b 100644 --- a/src/quick/items/qquickdroparea_p.h +++ b/src/quick/items/qquickdroparea_p.h @@ -96,6 +96,7 @@ public: QStringList formats() const; Q_INVOKABLE void getDataAsString(QQmlV4Function *); + Q_INVOKABLE void getDataAsArrayBuffer(QQmlV4Function *); Q_INVOKABLE void acceptProposedAction(QQmlV4Function *); Q_INVOKABLE void accept(QQmlV4Function *); diff --git a/tests/auto/quick/qquickdroparea/tst_qquickdroparea.cpp b/tests/auto/quick/qquickdroparea/tst_qquickdroparea.cpp index ece439d01a..67ee1a3714 100644 --- a/tests/auto/quick/qquickdroparea/tst_qquickdroparea.cpp +++ b/tests/auto/quick/qquickdroparea/tst_qquickdroparea.cpp @@ -78,6 +78,7 @@ private slots: // void drop_external(); void competingDrags(); void simultaneousDrags(); + void dropStuff(); private: QQmlEngine engine; @@ -1157,6 +1158,34 @@ void tst_QQuickDropArea::simultaneousDrags() QWindowSystemInterface::handleDrop(&alternateWindow, &data, QPoint(50, 50), Qt::CopyAction); } +void tst_QQuickDropArea::dropStuff() +{ + QQuickWindow window; + QQmlComponent component(&engine); + component.setData( + "import QtQuick 2.3\n" + "DropArea {\n" + "width: 100; height: 100\n" + "property var array\n" + "onDropped: { array = drop.getDataAsArrayBuffer('text/x-red'); }\n" + "}", QUrl()); + + QScopedPointer<QObject> object(component.create()); + QQuickItem *dropArea = qobject_cast<QQuickItem *>(object.data()); + QVERIFY(dropArea); + dropArea->setParentItem(window.contentItem()); + + QMimeData data; + data.setData("text/x-red", "red"); + + QCOMPARE(evaluate<QVariant>(dropArea, "array"), QVariant()); + + QWindowSystemInterface::handleDrag(&window, &data, QPoint(50, 50), Qt::CopyAction); + QWindowSystemInterface::handleDrop(&window, &data, QPoint(50, 50), Qt::CopyAction); + QCOMPARE(evaluate<int>(dropArea, "array.byteLength"), 3); + QCOMPARE(evaluate<QByteArray>(dropArea, "array"), QByteArray("red")); +} + QTEST_MAIN(tst_QQuickDropArea) #include "tst_qquickdroparea.moc" |