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