Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qwasmfiledialoghelper.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6
7#include <QtCore/QDebug>
8#include <QtCore/QUrl>
9#include <QtGui/private/qwasmlocalfileaccess_p.h>
10#include <QtCore/private/qwasmlocalfileengine_p.h>
11
13
14QWasmFileDialogHelper::QWasmFileDialogHelper()
15 : m_eventLoop(nullptr)
16{
17
18}
19
20QWasmFileDialogHelper::~QWasmFileDialogHelper()
21{
22
23}
24
25bool QWasmFileDialogHelper::defaultNameFilterDisables() const
26{
27 return false;
28}
29
30void QWasmFileDialogHelper::setDirectory(const QUrl &directory)
31{
32 Q_UNUSED(directory)
33}
34
35QUrl QWasmFileDialogHelper::directory() const
36{
37 return QUrl();
38}
39
40void QWasmFileDialogHelper::selectFile(const QUrl &file)
41{
42 m_selectedFiles.clear();
43 m_selectedFiles.append(file);
44}
45
46QList<QUrl> QWasmFileDialogHelper::selectedFiles() const
47{
48 return m_selectedFiles;
49}
50
51void QWasmFileDialogHelper::setFilter()
52{
53
54}
55
56void QWasmFileDialogHelper::selectNameFilter(const QString &filter)
57{
58 Q_UNUSED(filter);
59 // TODO
60}
61
62QString QWasmFileDialogHelper::selectedNameFilter() const
63{
64 return QString();
65}
66
67void QWasmFileDialogHelper::exec()
68{
69 QEventLoop eventLoop;
70 m_eventLoop = &eventLoop;
71 eventLoop.exec();
72 m_eventLoop = nullptr;
73}
74
75bool QWasmFileDialogHelper::show(Qt::WindowFlags flags, Qt::WindowModality modality, QWindow *parent)
76{
77 Q_UNUSED(flags)
78 Q_UNUSED(modality)
79 Q_UNUSED(parent)
80 showFileDialog();
81 return true;
82}
83
84void QWasmFileDialogHelper::hide()
85{
86
87}
88
89void QWasmFileDialogHelper::showFileDialog()
90{
91 if (options()->acceptMode() == QFileDialogOptions::AcceptOpen) {
92 // Use name filters from options
93 QString nameFilter = options()->nameFilters().join(";;");
94 if (nameFilter.isEmpty())
95 nameFilter = "*";
96
97 QWasmLocalFileAccess::showOpenFileDialog(nameFilter.toStdString(), [this](bool accepted, std::vector<qstdweb::File> files) {
98 onOpenDialogClosed(accepted, files);
99 });
100 } else if (options()->acceptMode() == QFileDialogOptions::AcceptSave) {
101 QString suggestion = m_selectedFiles.isEmpty() ? QString() : QUrl(m_selectedFiles.first()).fileName();
102 m_selectedFiles.clear();
103
104 QWasmLocalFileAccess::showSaveFileDialog(suggestion.toStdString(), [this](bool accepted, qstdweb::FileSystemFileHandle file){
105 onSaveDialogClosed(accepted, file);
106 });
107 }
108}
109
110void QWasmFileDialogHelper::onOpenDialogClosed(bool accepted, std::vector<qstdweb::File> files)
111{
112 m_selectedFiles.clear();
113
114 if (!accepted) {
115 emit reject();
116
117 if (m_eventLoop)
118 m_eventLoop->quit();
119 return;
120 }
121
122 // Track opened files
123 for (const auto &file : files) {
124 QString wasmFileName = QWasmFileEngineHandler::addFile(file);
125 QUrl fileUrl(wasmFileName);
126 m_selectedFiles.append(fileUrl);
127 }
128
129 // Emit signals
130 if (m_selectedFiles.size() > 0) {
131 emit fileSelected(m_selectedFiles.first());
132 emit filesSelected(m_selectedFiles);
133 }
134 emit accept();
135
136 // exit exec() if in exec()
137 if (m_eventLoop)
138 m_eventLoop->quit();
139}
140
141void QWasmFileDialogHelper::onSaveDialogClosed(bool accepted, qstdweb::FileSystemFileHandle file)
142{
143 if (!accepted) {
144 emit reject();
145
146 if (m_eventLoop)
147 m_eventLoop->quit();
148 return;
149 }
150
151 // Track save file
152 QString wasmFileName = QWasmFileEngineHandler::addFile(file);
153 QUrl fileUrl(wasmFileName);
154 m_selectedFiles.append(fileUrl);
155
156 // Emit signals
157 emit fileSelected(m_selectedFiles.first());
158 emit accept();
159
160 if (m_eventLoop)
161 m_eventLoop->quit();
162}
163
164QT_END_NAMESPACE
Combined button and popup list for selecting options.