File indexing completed on 2024-11-24 05:00:57

0001 /*  This file is part of the KDE libraries
0002     SPDX-FileCopyrightText: 2013 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include <QApplication>
0008 #include <QCommandLineParser>
0009 #include <QDebug>
0010 #include <QFileDialog>
0011 #include <QMetaEnum>
0012 
0013 int main(int argc, char **argv)
0014 {
0015     QApplication app(argc, argv);
0016     app.setApplicationName(QStringLiteral("QFileDialogTest"));
0017     QCommandLineParser parser;
0018     parser.addHelpOption();
0019     parser.addOption(QCommandLineOption(QStringList(QStringLiteral("staticFunction")),
0020                                         QStringLiteral("Test one of the static convenience function: 'getOpenFileUrl', 'getExistingDirectory'"),
0021                                         QStringLiteral("function name")));
0022     parser.addOption(QCommandLineOption(QStringList(QStringLiteral("acceptMode")),
0023                                         QStringLiteral("File dialog acceptMode: 'open' or 'save'"),
0024                                         QStringLiteral("type"),
0025                                         QStringLiteral("open")));
0026     parser.addOption(QCommandLineOption(QStringList(QStringLiteral("confirmOverwrite")),
0027                                         QStringLiteral("Test overwrite option: 'on' or 'off'"),
0028                                         QStringLiteral("option"),
0029                                         QStringLiteral("on")));
0030     parser.addOption(QCommandLineOption(QStringList(QStringLiteral("nativeDialog")),
0031                                         QStringLiteral("Use the platform native dialog: 'on' or 'off'"),
0032                                         QStringLiteral("option"),
0033                                         QStringLiteral("on")));
0034     parser.addOption(QCommandLineOption(QStringList(QStringLiteral("fileMode")),
0035                                         QStringLiteral("File dialog fileMode: 'AnyFile' or 'ExistingFile' or 'Directory' or 'ExistingFiles'"),
0036                                         QStringLiteral("type")));
0037     parser.addOption(QCommandLineOption(QStringList(QStringLiteral("nameFilter")),
0038                                         QStringLiteral("Dialog nameFilter, e. g. 'cppfiles (*.cpp *.h *.hpp)', can be specified multiple times"),
0039                                         QStringLiteral("nameFilter"),
0040                                         QStringLiteral("Everything (*)")));
0041     parser.addOption(QCommandLineOption(QStringList(QStringLiteral("mimeTypeFilter")),
0042                                         QStringLiteral("Dialog mimeTypeFilter, e. g. 'application/json', can be specified multiple times"),
0043                                         QStringLiteral("mimeTypeFilter")));
0044     parser.addOption(QCommandLineOption(QStringList(QStringLiteral("selectNameFilter")),
0045                                         QStringLiteral("Initially selected nameFilter"),
0046                                         QStringLiteral("selectNameFilter")));
0047     parser.addOption(QCommandLineOption(QStringList(QStringLiteral("selectMimeTypeFilter")),
0048                                         QStringLiteral("Initially selected mimeTypeFilter"),
0049                                         QStringLiteral("selectMimeTypeFilter")));
0050     parser.addOption(QCommandLineOption(QStringList(QStringLiteral("selectFile")), QStringLiteral("Initially selected file"), QStringLiteral("filename")));
0051     parser.addOption(
0052         QCommandLineOption(QStringList(QStringLiteral("selectDirectory")), QStringLiteral("Initially selected directory"), QStringLiteral("dirname")));
0053     parser.addOption(
0054         QCommandLineOption(QStringList(QStringLiteral("modal")), QStringLiteral("Test modal dialog"), QStringLiteral("modality"), QStringLiteral("on")));
0055     parser.addOption(QCommandLineOption(QStringList(QStringLiteral("options")), QStringLiteral("See QFileDialog::Options"), QStringLiteral("option")));
0056     parser.process(app);
0057 
0058     const QString staticFunction = parser.value(QStringLiteral("staticFunction"));
0059     if (staticFunction == QLatin1String("getExistingDirectory")) {
0060         QString dir = QFileDialog::getExistingDirectory(nullptr, QStringLiteral("getExistingDirectory test"), QStringLiteral("/tmp"));
0061         qDebug() << dir;
0062         return 0;
0063     } else if (staticFunction == QLatin1String("getOpenFileUrl")) {
0064         qDebug() << QFileDialog::getOpenFileUrl(nullptr, QStringLiteral("getOpenFileUrl test"), QUrl::fromLocalFile(QDir::homePath()));
0065         return 0;
0066     }
0067 
0068     QFileDialog dialog;
0069     dialog.setAcceptMode(parser.value(QStringLiteral("acceptMode")) == QStringLiteral("open") ? QFileDialog::AcceptOpen : QFileDialog::AcceptSave);
0070 
0071     QString fileModeValue = parser.value(QStringLiteral("fileMode"));
0072     if (fileModeValue == QLatin1String("AnyFile")) {
0073         dialog.setFileMode(QFileDialog::AnyFile);
0074     } else if (fileModeValue == QLatin1String("ExistingFile")) {
0075         dialog.setFileMode(QFileDialog::ExistingFile);
0076     } else if (fileModeValue == QLatin1String("ExistingFiles")) {
0077         dialog.setFileMode(QFileDialog::ExistingFiles);
0078     } else if (fileModeValue == QLatin1String("Directory")) {
0079         dialog.setFileMode(QFileDialog::Directory);
0080     } else if (!fileModeValue.isEmpty()) {
0081         qDebug() << "Not implemented or not valid:" << fileModeValue;
0082         exit(0);
0083     }
0084 
0085     QStringList nameFilterList = parser.values(QStringLiteral("nameFilter"));
0086     if (nameFilterList.size() == 1) {
0087         dialog.setNameFilter(nameFilterList.first());
0088     } else {
0089         dialog.setNameFilters(nameFilterList);
0090     }
0091 
0092     if (parser.isSet(QStringLiteral("options"))) {
0093         auto optStrings = parser.values(QStringLiteral("options"));
0094         QFileDialog::Options options = {};
0095         const auto mo = QFileDialog::staticMetaObject;
0096         const auto enumerator = mo.indexOfEnumerator("Options");
0097         for (const auto &optString : std::as_const(optStrings)) {
0098             options |= QFileDialog::Option(mo.enumerator(enumerator).keyToValue(optString.toLatin1().constData()));
0099         }
0100         dialog.setOptions(options);
0101     }
0102 
0103     const auto mimeFilterList = parser.values(QStringLiteral("mimeTypeFilter"));
0104     if (!mimeFilterList.isEmpty()) {
0105         dialog.setMimeTypeFilters(mimeFilterList);
0106     }
0107 
0108     QString selectNameFilter = parser.value(QStringLiteral("selectNameFilter"));
0109     QString selectMimeTypeFilter = parser.value(QStringLiteral("selectMimeTypeFilter"));
0110     if (!selectNameFilter.isEmpty()) {
0111         dialog.selectNameFilter(selectNameFilter);
0112     } else if (!selectMimeTypeFilter.isEmpty()) {
0113         dialog.selectMimeTypeFilter(selectMimeTypeFilter);
0114     }
0115 
0116     if (parser.value(QStringLiteral("confirmOverwrite")) == QStringLiteral("off")) {
0117         dialog.setOption(QFileDialog::DontConfirmOverwrite, true);
0118     }
0119 
0120     if (parser.value(QStringLiteral("nativeDialog")) == QStringLiteral("off")) {
0121         dialog.setOption(QFileDialog::DontUseNativeDialog, true);
0122     }
0123 
0124     dialog.setDirectoryUrl(QUrl::fromUserInput(parser.value(QStringLiteral("selectDirectory")), {}, QUrl::AssumeLocalFile));
0125     dialog.selectFile(parser.value(QStringLiteral("selectFile")));
0126 
0127     int ret;
0128     if (parser.value(QStringLiteral("modal")) == QStringLiteral("off")) {
0129         dialog.show();
0130         ret = app.exec();
0131     } else {
0132         ret = dialog.exec();
0133     }
0134 
0135     if (dialog.result() == QDialog::Accepted) {
0136         qDebug() << "selected files" << dialog.selectedFiles();
0137         qDebug() << "selected urls" << dialog.selectedUrls();
0138         qDebug() << "selected mime type filter" << dialog.selectedMimeTypeFilter();
0139     }
0140 
0141     qDebug() << "mime type filter(s):" << dialog.mimeTypeFilters();
0142 
0143     return ret;
0144 }