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>
13#include <qohoswindowmanager.h>
22 using FileMode = QFileDialogOptions::FileMode;
25 switch (options.fileMode()) {
26 case FileMode::Directory:
27 case FileMode::DirectoryOnly:
29 case FileMode::ExistingFile:
30 case FileMode::ExistingFiles:
31 return DocumentSelectMode
::FILE;
32 case FileMode::AnyFile:
33 return DocumentSelectMode
::MIXED;
35 return DocumentSelectMode
::MIXED;
41 return options.fileMode() == QFileDialogOptions::FileMode::ExistingFile
42 ? QOhosWindowManager::ResultMultiplicity::SINGLE
43 : QOhosWindowManager::ResultMultiplicity::MULTIPLE;
62 bool show(Qt::WindowFlags windowFlags, Qt::WindowModality windowModality, QWindow *parent)
override;
65 void setDialogResult(
bool accepted, QStringList files,
std::optional<
int> optSelectedFilterIndex);
66 static QStringList convertQtNameFiltersToOhosStandard(
const QStringList &);
68 QEventLoop m_eventLoop;
72 QUrl m_selectFileName;
73 QString m_selectedNameFilter;
75 QList<QUrl> m_selectedFiles;
86 auto _dbg = make_QCScopedDebug(
"QOhosPlatformFileDialogHelperImpl::defaultNameFilterDisables");
92 auto _dbg = make_QCScopedDebug(
"QOhosPlatformFileDialogHelperImpl::setDirectory");
93 m_directory = directory;
98 auto _dbg = make_QCScopedDebug(
"QOhosPlatformFileDialogHelperImpl::directory");
104 auto _dbg = make_QCScopedDebug(
"QOhosPlatformFileDialogHelperImpl::selectFile");
105 m_selectFileName = filename;
110 auto _dbg = make_QCScopedDebug(
"QOhosPlatformFileDialogHelperImpl::setFilter");
115 m_selectedNameFilter = filter;
116 auto _dbg = make_QCScopedDebug(
"QOhosPlatformFileDialogHelperImpl::selectNameFilter");
121 auto _dbg = make_QCScopedDebug(
"QOhosPlatformFileDialogHelperImpl::selectedNameFilter");
122 return m_selectedNameFilter;
127 auto _dbg = make_QCScopedDebug(
"QOhosPlatformFileDialogHelperImpl::selectedFiles");
128 return m_selectedFiles;
138 auto _dbg = make_QCScopedDebug(
"QOhosPlatformFileDialogHelperImpl::hide()");
142 bool accepted, QStringList files,
std::optional<
int> optSelectedFilterIndex)
144 auto _dbg = make_QCScopedDebug(
"QOhosPlatformFileDialogHelperImpl::dialogResult()");
147 m_selectedFiles.clear();
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));
155 std::copy(files.constBegin(), files.constEnd(),
std::back_inserter(m_selectedFiles));
165 auto _dbg = make_QCScopedDebug(
"QOhosPlatformFileDialogHelperImpl::exec()");
168 show(Qt::Dialog, Qt::ApplicationModal, 0);
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);
187 QString ohosExtensionsSeparatedByComma = ohosExtensionList.join(QString::fromUtf8(
","));
188 if (ohosExtensionsSeparatedByComma == qtAnyFilesFilter)
189 ohosExtensionsSeparatedByComma = ohosAnyFilesFilter;
190 auto ohosNameFilter = qtFilter + QLatin1String(
"|") + ohosExtensionsSeparatedByComma;
191 ohosNameFilters.append(ohosNameFilter);
193 return ohosNameFilters;
197 Qt::WindowFlags windowFlags, Qt::WindowModality windowModality, QWindow *parent)
199 Q_UNUSED(windowFlags)
200 Q_UNUSED(windowModality)
202 auto contextWindowRef = QtOhos::QObjectThreadSafeRef(
203 parent !=
nullptr ? parent : QGuiApplication::focusWindow());
206 QSharedPointer<QFileDialogOptions> opt = options();
207 auto ohosNameFilters = convertQtNameFiltersToOhosStandard(opt->nameFilters());
208 if (opt->acceptMode() == QFileDialogOptions::AcceptOpen)
212 !m_selectFileName.isEmpty()
213 ? m_selectFileName.toLocalFile()
214 : !m_directory.isEmpty()
215 ? m_directory.toLocalFile()
217 mapQFileDialogOptionsToOhosDocumentSelectMode(*opt),
218 mapQFileDialogOptionsToOhosResultMultiplicity(*opt),
220 auto filesPaths = optOpenResult.has_value()
221 ? optOpenResult.value().selectedUrls
223 setDialogResult(!filesPaths.isEmpty(), filesPaths, {});
228 !m_selectFileName.isEmpty()
229 ? QStringList(QFileInfo(m_selectFileName.toLocalFile()).fileName())
231 !m_directory.isEmpty()
232 ? m_directory.toLocalFile()
233 : !m_selectFileName.isEmpty()
234 ? QFileInfo(m_selectFileName.toLocalFile()).absoluteDir().path()
238 auto filesPaths = optSaveResult.has_value()
239 ? optSaveResult.value().savedUrls
241 auto optSelectedFilterIndex = qTransform(
243 [](
const auto &saveResult) {
244 return saveResult.selectedFileSuffixChoiceIndex;
246 setDialogResult(!filesPaths.isEmpty(), filesPaths, optSelectedFilterIndex);
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)