diff options
author | Qt Forward Merge Bot <qt_forward_merge_bot@qt-project.org> | 2019-02-20 01:01:00 +0100 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2019-02-20 09:51:12 +0100 |
commit | 035f934d7a798e97bf0213a5d42a3d511132f03d (patch) | |
tree | 89aa6efdc86864ce479cddca6b9c4ba523c2754a /src/widgets/dialogs/qfiledialog.cpp | |
parent | f4cc23cffbe3005f0a522cac938695e87ecd6407 (diff) | |
parent | da4ab444ffac37514435364d4d3f0ad59d4f9bc3 (diff) |
Merge remote-tracking branch 'origin/5.13' into dev
Conflicts:
tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp
Added tests/auto/testlib/selftests/expected_crashes_5.txt to work
round the output of the crashes test (which exercises UB, see
QTBUG-73903) being truncated on one test platform.
Change-Id: I9cd3f2639b4e50c3c4513e14629a40bdca8f8273
Diffstat (limited to 'src/widgets/dialogs/qfiledialog.cpp')
-rw-r--r-- | src/widgets/dialogs/qfiledialog.cpp | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/widgets/dialogs/qfiledialog.cpp b/src/widgets/dialogs/qfiledialog.cpp index edaa7854ba..d8a4ad5f24 100644 --- a/src/widgets/dialogs/qfiledialog.cpp +++ b/src/widgets/dialogs/qfiledialog.cpp @@ -73,6 +73,9 @@ #elif defined(Q_OS_WIN) # include <QtCore/qt_windows.h> #endif +#if defined(Q_OS_WASM) +#include <private/qwasmlocalfileaccess_p.h> +#endif QT_BEGIN_NAMESPACE @@ -1359,6 +1362,7 @@ void QFileDialog::setNameFilter(const QString &filter) } +#if QT_DEPRECATED_SINCE(5, 13) /*! \property QFileDialog::nameFilterDetailsVisible \obsolete @@ -1380,6 +1384,7 @@ bool QFileDialog::isNameFilterDetailsVisible() const { return !testOption(HideNameFilterDetails); } +#endif /* @@ -1859,6 +1864,7 @@ QFileDialog::AcceptMode QFileDialog::acceptMode() const return static_cast<AcceptMode>(d->options->acceptMode()); } +#if QT_DEPRECATED_SINCE(5, 13) /*! \property QFileDialog::readOnly \obsolete @@ -1918,6 +1924,7 @@ bool QFileDialog::confirmOverwrite() const { return !testOption(DontConfirmOverwrite); } +#endif /*! \property QFileDialog::defaultSuffix @@ -2344,6 +2351,85 @@ QList<QUrl> QFileDialog::getOpenFileUrls(QWidget *parent, } /*! + 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<void(const QString &, const QByteArray &)> &fileOpenCompleted) +{ +#ifdef Q_OS_WASM + auto openFileImpl = std::make_shared<std::function<void(void)>>(); + 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. |