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
qohosplatformfiledialoghelper.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
6#include <QtCore/qeventloop.h>
7#include <QtCore/qfileinfo.h>
8#include <qohosjsenv_p.h>
9#include <QtGui/qguiapplication.h>
10#include <QtGui/qwindow.h>
11#include <algorithm>
12#include <qohoswindowmanager.h>
13
15
16namespace {
17
19mapQFileDialogOptionsToOhosDocumentSelectMode(const QFileDialogOptions &options)
20{
21 using FileMode = QFileDialogOptions::FileMode;
22 using DocumentSelectMode = QOhosWindowManager::DocumentSelectMode;
23
24 switch (options.fileMode()) {
25 case FileMode::Directory:
26 case FileMode::DirectoryOnly:
27 return DocumentSelectMode::FOLDER;
28 case FileMode::ExistingFile:
29 case FileMode::ExistingFiles:
30 return DocumentSelectMode::FILE;
31 case FileMode::AnyFile:
32 return DocumentSelectMode::MIXED;
33 }
34 return DocumentSelectMode::MIXED;
35}
36
38mapQFileDialogOptionsToOhosResultMultiplicity(const QFileDialogOptions &options)
39{
40 return options.fileMode() == QFileDialogOptions::FileMode::ExistingFile
41 ? QOhosWindowManager::ResultMultiplicity::SINGLE
42 : QOhosWindowManager::ResultMultiplicity::MULTIPLE;
43}
44
46{
47public:
50
52 void setDirectory(const QUrl &directory) override;
53 QUrl directory() const override;
54 void selectFile(const QUrl &filename) override;
55 QList<QUrl> selectedFiles() const override;
57 void selectNameFilter(const QString &filter) override;
58 QString selectedNameFilter() const override;
59 void hide() override;
60 void exec() override;
61 bool show(Qt::WindowFlags windowFlags, Qt::WindowModality windowModality, QWindow *parent) override;
62
63private:
64 void setDialogResult(bool accepted, QStringList files, QOhosOptional<int> optSelectedFilterIndex);
65 static QStringList convertQtNameFiltersToOhosStandard(const QStringList &);
66
67 QEventLoop m_eventLoop;
68 bool m_shown;
69
70 QUrl m_directory;
71 QUrl m_selectFileName;
72 QString m_selectedNameFilter;
73
74 QList<QUrl> m_selectedFiles;
75};
76
82
84{
85 auto _dbg = make_QCScopedDebug("QOhosPlatformFileDialogHelperImpl::defaultNameFilterDisables");
86 return false;
87}
88
90{
91 auto _dbg = make_QCScopedDebug("QOhosPlatformFileDialogHelperImpl::setDirectory");
92 m_directory = directory;
93}
94
96{
97 auto _dbg = make_QCScopedDebug("QOhosPlatformFileDialogHelperImpl::directory");
98 return m_directory;
99}
100
102{
103 auto _dbg = make_QCScopedDebug("QOhosPlatformFileDialogHelperImpl::selectFile");
104 m_selectFileName = filename;
105}
106
108{
109 auto _dbg = make_QCScopedDebug("QOhosPlatformFileDialogHelperImpl::setFilter");
110}
111
113{
114 m_selectedNameFilter = filter;
115 auto _dbg = make_QCScopedDebug("QOhosPlatformFileDialogHelperImpl::selectNameFilter");
116}
117
119{
120 auto _dbg = make_QCScopedDebug("QOhosPlatformFileDialogHelperImpl::selectedNameFilter");
121 return m_selectedNameFilter;
122}
123
125{
126 auto _dbg = make_QCScopedDebug("QOhosPlatformFileDialogHelperImpl::selectedFiles");
127 return m_selectedFiles;
128}
129
131{
132 m_shown = false;
133 // Programmatically closing the DocumentViewPicker is not supported by the
134 // HarmonyOS platform API. The picker can only be dismissed by the user.
135 // See: https://developer.huawei.com/consumer/en/doc/harmonyos-references/js-apis-file-picker#documentviewpicker
136 m_eventLoop.exit();
137 auto _dbg = make_QCScopedDebug("QOhosPlatformFileDialogHelperImpl::hide()");
138}
139
140void QOhosPlatformFileDialogHelperImpl::setDialogResult(
141 bool accepted, QStringList files, QOhosOptional<int> optSelectedFilterIndex)
142{
143 auto _dbg = make_QCScopedDebug("QOhosPlatformFileDialogHelperImpl::dialogResult()");
144
145 hide();
146 m_selectedFiles.clear();
147 if (accepted) {
148 if (optSelectedFilterIndex.has_value()) {
149 auto nameFilters = options()->nameFilters();
150 auto selectedFilterIndex = optSelectedFilterIndex.value();
151 if (selectedFilterIndex >= 0 && selectedFilterIndex < nameFilters.count())
152 selectNameFilter(nameFilters.at(selectedFilterIndex));
153 }
154 std::copy(files.constBegin(), files.constEnd(), std::back_inserter(m_selectedFiles));
155 emit accept();
156 } else {
157 emit reject();
158 }
159
160}
161
163{
164 auto _dbg = make_QCScopedDebug("QOhosPlatformFileDialogHelperImpl::exec()");
165
166 if (!m_shown)
167 show(Qt::Dialog, Qt::ApplicationModal, 0);
168
169 // Run event loop only if the call was successful
170 if (m_shown)
171 m_eventLoop.exec();
172}
173
174QStringList QOhosPlatformFileDialogHelperImpl::convertQtNameFiltersToOhosStandard(const QStringList &qtNameFilters)
175{
176 const auto qtAnyFilesFilter = QString::fromUtf8("*");
177 const auto ohosAnyFilesFilter = QString::fromUtf8(".*");
178 QStringList ohosNameFilters;
179 for (const auto &qtFilter : qtNameFilters) {
180 QStringList qtCleanFilters = QPlatformFileDialogHelper::cleanFilterList(qtFilter);
181 QStringList ohosExtensionList;
182 for (auto &qtCleanFilter : qtCleanFilters) {
183 QString extensionOnly = qtCleanFilter.replace(QLatin1String("*."), QLatin1String("."));
184 ohosExtensionList.append(extensionOnly);
185 }
186 QString ohosExtensionsSeparatedByComma = ohosExtensionList.join(QString::fromUtf8(","));
187 if (ohosExtensionsSeparatedByComma == qtAnyFilesFilter)
188 ohosExtensionsSeparatedByComma = ohosAnyFilesFilter;
189 auto ohosNameFilter = qtFilter + QLatin1String("|") + ohosExtensionsSeparatedByComma;
190 ohosNameFilters.append(ohosNameFilter);
191 }
192 return ohosNameFilters;
193}
194
196 Qt::WindowFlags windowFlags, Qt::WindowModality windowModality, QWindow *parent)
197{
198 Q_UNUSED(windowFlags)
199 Q_UNUSED(windowModality)
200
201 auto contextWindowRef = QtOhos::QObjectThreadSafeRef(
202 parent != nullptr ? parent : QGuiApplication::focusWindow());
203
204 // TODO: don't assume that "this" is always alive
205 QSharedPointer<QFileDialogOptions> opt = options();
206 auto ohosNameFilters = convertQtNameFiltersToOhosStandard(opt->nameFilters());
207 if (opt->acceptMode() == QFileDialogOptions::AcceptOpen)
209 contextWindowRef,
210 ohosNameFilters,
211 !m_selectFileName.isEmpty()
212 ? m_selectFileName.toLocalFile()
213 : !m_directory.isEmpty()
214 ? m_directory.toLocalFile()
215 : QString(),
216 mapQFileDialogOptionsToOhosDocumentSelectMode(*opt),
217 mapQFileDialogOptionsToOhosResultMultiplicity(*opt),
218 [this](QOhosOptional<QOhosWindowManager::OpenResult> optOpenResult) {
219 auto filesPaths = optOpenResult.has_value()
220 ? optOpenResult.value().selectedUrls
221 : QStringList();
222 setDialogResult(!filesPaths.isEmpty(), filesPaths, {});
223 });
224 else
226 contextWindowRef,
227 !m_selectFileName.isEmpty()
228 ? QStringList(QFileInfo(m_selectFileName.toLocalFile()).fileName())
229 : QStringList(),
230 !m_directory.isEmpty()
231 ? m_directory.toLocalFile()
232 : !m_selectFileName.isEmpty()
233 ? QFileInfo(m_selectFileName.toLocalFile()).absoluteDir().path()
234 : QString(),
235 ohosNameFilters,
236 [this](QOhosOptional<QOhosWindowManager::SaveResult> optSaveResult) {
237 auto filesPaths = optSaveResult.has_value()
238 ? optSaveResult.value().savedUrls
239 : QStringList();
240 auto optSelectedFilterIndex = qTransform(
241 optSaveResult,
242 [](const auto &saveResult) {
243 return saveResult.selectedFileSuffixChoiceIndex;
244 });
245 setDialogResult(!filesPaths.isEmpty(), filesPaths, optSelectedFilterIndex);
246 });
247 m_shown = true;
248
249 return true;
250}
251
252}
253
258
259QT_END_NAMESPACE
bool show(Qt::WindowFlags windowFlags, Qt::WindowModality windowModality, QWindow *parent) override
void showFileDialogSave(QtOhos::QObjectThreadSafeRef contextWindowRef, QStringList newFileNames, QString defaultFilePath, QStringList fileSuffixChoices, QOhosConsumer< QOhosOptional< SaveResult > > resultCallback)
void showFileDialogOpen(QtOhos::QObjectThreadSafeRef contextWindowRef, QStringList filters, QString defaultPath, DocumentSelectMode documentSelectMode, ResultMultiplicity resultMultiplicity, QOhosConsumer< QOhosOptional< OpenResult > > resultCallback)
QtOhos::enums::ohos::file::picker::DocumentSelectMode DocumentSelectMode
Combined button and popup list for selecting options.
QOhosWindowManager::ResultMultiplicity mapQFileDialogOptionsToOhosResultMultiplicity(const QFileDialogOptions &options)
QOhosWindowManager::DocumentSelectMode mapQFileDialogOptionsToOhosDocumentSelectMode(const QFileDialogOptions &options)
QPlatformFileDialogHelper * makeQOhosPlatformFileDialogHelper()