From edc455c69b7a938f7a7ce98b5b0ffd1398f239c6 Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Sat, 26 Jan 2019 11:50:33 +0100 Subject: QtWidgets: mark QDialog/QFileDialog functions as deprecated Mark some long obsolete functions as deprecated so the can be removed with Qt6: - QDialog::setOrientation()/orientation() - QDialog::setExtension()/extension()/showExtension() - QFileDialog::setNameFilterDetailsVisible()/isNameFilterDetailsVisible() - QFileDialog::setResolveSymlinks()/resolveSymlinks() Change-Id: Ibbd5b4192ea8ab483d6b2a8dbf9879f29f9ee86d Reviewed-by: Friedemann Kleint Reviewed-by: Richard Moe Gustavsen --- src/widgets/dialogs/qfiledialog.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/widgets/dialogs/qfiledialog.cpp') diff --git a/src/widgets/dialogs/qfiledialog.cpp b/src/widgets/dialogs/qfiledialog.cpp index edaa7854ba..f42cf89708 100644 --- a/src/widgets/dialogs/qfiledialog.cpp +++ b/src/widgets/dialogs/qfiledialog.cpp @@ -1359,6 +1359,7 @@ void QFileDialog::setNameFilter(const QString &filter) } +#if QT_DEPRECATED_SINCE(5, 13) /*! \property QFileDialog::nameFilterDetailsVisible \obsolete @@ -1380,6 +1381,7 @@ bool QFileDialog::isNameFilterDetailsVisible() const { return !testOption(HideNameFilterDetails); } +#endif /* @@ -1879,6 +1881,7 @@ bool QFileDialog::isReadOnly() const return testOption(ReadOnly); } +#if QT_DEPRECATED_SINCE(5, 13) /*! \property QFileDialog::resolveSymlinks \obsolete @@ -1899,6 +1902,7 @@ bool QFileDialog::resolveSymlinks() const { return !testOption(DontResolveSymlinks); } +#endif /*! \property QFileDialog::confirmOverwrite -- cgit v1.2.3 From 928cab5ff1d931d00074d8930c41537109814371 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Johan=20S=C3=B8rvig?= Date: Fri, 18 Jan 2019 15:02:32 +0100 Subject: wasm: add public API for local file access The web sandbox restricts access to the local file system, which means Qt needs new public API to provide such access to applications. This adds a new static, asynchornous getOpenFileContent() function to QFileDialog, which will show a file dialog, read the file contents once a file has been selected, and finally call the user-provided callback with the selected file name and file data. auto fileReady = [](const QString &fileName, const QByteArray &fileContents) { // Process file } QFileDialog::getOpenFileContent("*.*", fileReady); This API can be expanded in at least two ways in the future: allow reading multiple files, and allow file streaming by returning a QOIDevice. Change-Id: I011b92fbcf21f0696604e66b7f9eb265c1a07aaa Reviewed-by: Lorn Potter --- src/widgets/dialogs/qfiledialog.cpp | 82 +++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) (limited to 'src/widgets/dialogs/qfiledialog.cpp') diff --git a/src/widgets/dialogs/qfiledialog.cpp b/src/widgets/dialogs/qfiledialog.cpp index f42cf89708..3e756da505 100644 --- a/src/widgets/dialogs/qfiledialog.cpp +++ b/src/widgets/dialogs/qfiledialog.cpp @@ -73,6 +73,9 @@ #elif defined(Q_OS_WIN) # include #endif +#if defined(Q_OS_WASM) +#include +#endif QT_BEGIN_NAMESPACE @@ -2347,6 +2350,85 @@ QList QFileDialog::getOpenFileUrls(QWidget *parent, return QList(); } +/*! + This is a convenience static function that will return the content of a file + selected by the user. + + This function is used to access local files on Qt for WebAssembly, where the web + sandbox places restrictions on how such access may happen. Its implementation will + make the browser display a native file dialog, where the user makes the file selection. + + It can also be used on other platforms, where it will fall back to using QFileDialog. + + The function is asynchronous and returns immediately. The \a fileOpenCompleted + callback will be called when a file has been selected and its contents has been + read into memory. + + \snippet code/src_gui_dialogs_qfiledialog.cpp 14 + \since 5.13 +*/ +void QFileDialog::getOpenFileContent(const QString &nameFilter, const std::function &fileOpenCompleted) +{ +#ifdef Q_OS_WASM + auto openFileImpl = std::make_shared>(); + QString fileName; + QByteArray fileContent; + *openFileImpl = [=]() mutable { + auto fileDialogClosed = [&](bool fileSelected) { + if (!fileSelected) { + fileOpenCompleted(fileName, fileContent); + openFileImpl.reset(); + } + }; + auto acceptFile = [&](uint64_t size, const std::string name) -> char * { + const uint64_t twoGB = 1ULL << 31; // QByteArray limit + if (size > twoGB) + return nullptr; + + fileName = QString::fromStdString(name); + fileContent.resize(size); + return fileContent.data(); + }; + auto fileContentReady = [&]() mutable { + fileOpenCompleted(fileName, fileContent); + openFileImpl.reset(); + }; + + auto qtFilterStringToWebAcceptString = [](const QString &qtString) { + // The Qt and Web name filter string formats are similar, but + // not identical. + return qtString.toStdString(); // ### TODO + }; + + QWasmLocalFileAccess::openFile(qtFilterStringToWebAcceptString(nameFilter), fileDialogClosed, acceptFile, fileContentReady); + }; + + (*openFileImpl)(); +#else + QFileDialog *dialog = new QFileDialog(); + dialog->selectNameFilter(nameFilter); + + auto fileSelected = [=](const QString &fileName) { + QByteArray fileContent; + if (!fileName.isNull()) { + QFile selectedFile(fileName); + selectedFile.open(QIODevice::ReadOnly); + fileContent = selectedFile.readAll(); + } + fileOpenCompleted(fileName, fileContent); + }; + + auto dialogClosed = [=](int code) { + Q_UNUSED(code); + delete dialog; + }; + + connect(dialog, &QFileDialog::fileSelected, fileSelected); + connect(dialog, &QFileDialog::finished, dialogClosed); + dialog->show(); +#endif +} + /*! This is a convenience static function that will return a file name selected by the user. The file does not have to exist. -- cgit v1.2.3 From afc7b26313e0d6c43bdc62b021751f30e6fa27a5 Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Sun, 10 Feb 2019 19:37:03 +0100 Subject: QtWidgets: mark QFileDialog functions as deprecated Mark some long obsolete functions as deprecated so the can be removed with Qt6: - QFileDialog::setConfirmOverwrite()/confirmOverwrite() - QFileDialog::setReadOnly()/isReadOnly() Change-Id: I3cc1df76c8e40e95b8e9893ae06ef488fad26fb6 Reviewed-by: Friedemann Kleint --- src/widgets/dialogs/qfiledialog.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/widgets/dialogs/qfiledialog.cpp') diff --git a/src/widgets/dialogs/qfiledialog.cpp b/src/widgets/dialogs/qfiledialog.cpp index 3e756da505..d8a4ad5f24 100644 --- a/src/widgets/dialogs/qfiledialog.cpp +++ b/src/widgets/dialogs/qfiledialog.cpp @@ -1864,6 +1864,7 @@ QFileDialog::AcceptMode QFileDialog::acceptMode() const return static_cast(d->options->acceptMode()); } +#if QT_DEPRECATED_SINCE(5, 13) /*! \property QFileDialog::readOnly \obsolete @@ -1884,7 +1885,6 @@ bool QFileDialog::isReadOnly() const return testOption(ReadOnly); } -#if QT_DEPRECATED_SINCE(5, 13) /*! \property QFileDialog::resolveSymlinks \obsolete @@ -1905,7 +1905,6 @@ bool QFileDialog::resolveSymlinks() const { return !testOption(DontResolveSymlinks); } -#endif /*! \property QFileDialog::confirmOverwrite @@ -1925,6 +1924,7 @@ bool QFileDialog::confirmOverwrite() const { return !testOption(DontConfirmOverwrite); } +#endif /*! \property QFileDialog::defaultSuffix -- cgit v1.2.3