summaryrefslogtreecommitdiffstats
path: root/src/webengine/ui_delegates_manager.cpp
diff options
context:
space:
mode:
authorPierre Rossi <pierre.rossi@digia.com>2013-12-16 20:48:08 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-01-15 21:35:35 +0100
commitdb2c0781218b7dfa03f7bc39b1e2115dbf4a0fb0 (patch)
tree9f7a959fd2dd1744603ac6481787262ca652ba7a /src/webengine/ui_delegates_manager.cpp
parent6f5850dd4f6c4cc253c48f41f59130aee5415591 (diff)
Qt Quick File Picker
Change-Id: I6195c49f1647c78b16d9d47770ab37ba998a61a5 Reviewed-by: Jocelyn Turcotte <jocelyn.turcotte@digia.com>
Diffstat (limited to 'src/webengine/ui_delegates_manager.cpp')
-rw-r--r--src/webengine/ui_delegates_manager.cpp93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/webengine/ui_delegates_manager.cpp b/src/webengine/ui_delegates_manager.cpp
index 6ee3ea36..13e4a9fc 100644
--- a/src/webengine/ui_delegates_manager.cpp
+++ b/src/webengine/ui_delegates_manager.cpp
@@ -304,3 +304,96 @@ void UIDelegatesManager::showDialog(QSharedPointer<JavaScriptDialogController> d
QMetaObject::invokeMethod(dialog, "open");
}
+
+namespace {
+class FilePickerController : public QObject {
+ Q_OBJECT
+public:
+ FilePickerController(WebContentsAdapterClient::FileChooserMode, const QExplicitlySharedDataPointer<WebContentsAdapter> &, QObject * = 0);
+
+public Q_SLOTS:
+ void accepted(const QVariant &files);
+ void rejected();
+
+private:
+ QExplicitlySharedDataPointer<WebContentsAdapter> m_adapter;
+ WebContentsAdapterClient::FileChooserMode m_mode;
+
+};
+
+
+FilePickerController::FilePickerController(WebContentsAdapterClient::FileChooserMode mode, const QExplicitlySharedDataPointer<WebContentsAdapter> &adapter, QObject *parent)
+ : QObject(parent)
+ , m_adapter(adapter)
+ , m_mode(mode)
+{
+}
+
+void FilePickerController::accepted(const QVariant &files)
+{
+ QStringList stringList;
+ // Qt Quick's file dialog returns a list of QUrls, this will hence shape our API there.
+ Q_FOREACH (const QUrl &url, files.value<QList<QUrl> >())
+ stringList.append(url.toLocalFile());
+
+ m_adapter->filesSelectedInChooser(stringList, m_mode);
+}
+
+void FilePickerController::rejected()
+{
+ m_adapter->filesSelectedInChooser(QStringList(), m_mode);
+}
+
+} // namespace
+
+
+void UIDelegatesManager::showFilePicker(WebContentsAdapterClient::FileChooserMode mode, const QString &defaultFileName, const QStringList &acceptedMimeTypes, const QExplicitlySharedDataPointer<WebContentsAdapter> &adapter)
+{
+ Q_UNUSED(defaultFileName);
+ Q_UNUSED(acceptedMimeTypes);
+
+ if (!ensureComponentLoaded(FilePicker))
+ return;
+ QQmlContext *context(creationContextForComponent(filePickerComponent.data()));
+ QObject *filePicker = filePickerComponent->beginCreate(context);
+ if (QQuickItem* item = qobject_cast<QQuickItem*>(filePicker))
+ item->setParentItem(m_view);
+ filePicker->setParent(m_view);
+ filePickerComponent->completeCreate();
+
+ // Fine-tune some properties depending on the mode.
+ switch (mode) {
+ case WebContentsAdapterClient::Open:
+ break;
+ case WebContentsAdapterClient::Save:
+ filePicker->setProperty("selectExisting", false);
+ break;
+ case WebContentsAdapterClient::OpenMultiple:
+ filePicker->setProperty("selectMultiple", true);
+ break;
+ case WebContentsAdapterClient::UploadFolder:
+ filePicker->setProperty("selectFolder", true);
+ break;
+ default:
+ Q_UNREACHABLE();
+ }
+
+ FilePickerController *controller = new FilePickerController(mode, adapter, filePicker);
+ QQmlProperty filesPickedSignal(filePicker, QStringLiteral("onFilesSelected"));
+ CHECK_QML_SIGNAL_PROPERTY(filesPickedSignal, filePickerComponent->url());
+ QQmlProperty rejectSignal(filePicker, QStringLiteral("onRejected"));
+ CHECK_QML_SIGNAL_PROPERTY(rejectSignal, filePickerComponent->url());
+ static int acceptedIndex = controller->metaObject()->indexOfSlot("accepted(QVariant)");
+ QObject::connect(filePicker, filesPickedSignal.method(), controller, controller->metaObject()->method(acceptedIndex));
+ static int rejectedIndex = controller->metaObject()->indexOfSlot("rejected()");
+ QObject::connect(filePicker, rejectSignal.method(), controller, controller->metaObject()->method(rejectedIndex));
+
+ // delete when done.
+ static int deleteLaterIndex = filePicker->metaObject()->indexOfSlot("deleteLater()");
+ QObject::connect(filePicker, filesPickedSignal.method(), filePicker, filePicker->metaObject()->method(deleteLaterIndex));
+ QObject::connect(filePicker, rejectSignal.method(), filePicker, filePicker->metaObject()->method(deleteLaterIndex));
+
+ QMetaObject::invokeMethod(filePicker, "open");
+}
+
+#include "ui_delegates_manager.moc"