| 1 | // Copyright (C) 2016 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 | |
| 4 | #ifndef QFILEDIALOG_H |
| 5 | #define QFILEDIALOG_H |
| 6 | |
| 7 | #include <QtWidgets/qtwidgetsglobal.h> |
| 8 | #include <QtCore/qdir.h> |
| 9 | #include <QtCore/qstring.h> |
| 10 | #include <QtCore/qurl.h> |
| 11 | #include <QtWidgets/qdialog.h> |
| 12 | |
| 13 | #include <functional> |
| 14 | |
| 15 | QT_REQUIRE_CONFIG(filedialog); |
| 16 | |
| 17 | QT_BEGIN_NAMESPACE |
| 18 | |
| 19 | class QModelIndex; |
| 20 | class QItemSelection; |
| 21 | struct QFileDialogArgs; |
| 22 | class QFileDialogPrivate; |
| 23 | class QAbstractFileIconProvider; |
| 24 | class QAbstractItemDelegate; |
| 25 | class QAbstractProxyModel; |
| 26 | |
| 27 | class Q_WIDGETS_EXPORT QFileDialog : public QDialog |
| 28 | { |
| 29 | Q_OBJECT |
| 30 | Q_PROPERTY(ViewMode viewMode READ viewMode WRITE setViewMode) |
| 31 | Q_PROPERTY(FileMode fileMode READ fileMode WRITE setFileMode) |
| 32 | Q_PROPERTY(AcceptMode acceptMode READ acceptMode WRITE setAcceptMode) |
| 33 | Q_PROPERTY(QString defaultSuffix READ defaultSuffix WRITE setDefaultSuffix) |
| 34 | Q_PROPERTY(Options options READ options WRITE setOptions) |
| 35 | Q_PROPERTY(QStringList supportedSchemes READ supportedSchemes WRITE setSupportedSchemes) |
| 36 | |
| 37 | public: |
| 38 | enum ViewMode { Detail, List }; |
| 39 | Q_ENUM(ViewMode) |
| 40 | enum FileMode { AnyFile, ExistingFile, Directory, ExistingFiles }; |
| 41 | Q_ENUM(FileMode) |
| 42 | enum AcceptMode { AcceptOpen, AcceptSave }; |
| 43 | Q_ENUM(AcceptMode) |
| 44 | enum DialogLabel { LookIn, FileName, FileType, Accept, Reject }; |
| 45 | |
| 46 | // keep this in sync with QFileDialogOption::FileDialogOptions |
| 47 | enum Option |
| 48 | { |
| 49 | ShowDirsOnly = 0x00000001, |
| 50 | DontResolveSymlinks = 0x00000002, |
| 51 | DontConfirmOverwrite = 0x00000004, |
| 52 | DontUseNativeDialog = 0x00000008, |
| 53 | ReadOnly = 0x00000010, |
| 54 | HideNameFilterDetails = 0x00000020, |
| 55 | DontUseCustomDirectoryIcons = 0x00000040 |
| 56 | }; |
| 57 | Q_ENUM(Option) |
| 58 | Q_DECLARE_FLAGS(Options, Option) |
| 59 | Q_FLAG(Options) |
| 60 | |
| 61 | QFileDialog(QWidget *parent, Qt::WindowFlags f); |
| 62 | explicit QFileDialog(QWidget *parent = nullptr, |
| 63 | const QString &caption = QString(), |
| 64 | const QString &directory = QString(), |
| 65 | const QString &filter = QString()); |
| 66 | ~QFileDialog(); |
| 67 | |
| 68 | void setDirectory(const QString &directory); |
| 69 | inline void setDirectory(const QDir &directory); |
| 70 | QDir directory() const; |
| 71 | |
| 72 | void setDirectoryUrl(const QUrl &directory); |
| 73 | QUrl directoryUrl() const; |
| 74 | |
| 75 | void selectFile(const QString &filename); |
| 76 | QStringList selectedFiles() const; |
| 77 | |
| 78 | void selectUrl(const QUrl &url); |
| 79 | QList<QUrl> selectedUrls() const; |
| 80 | |
| 81 | void setNameFilter(const QString &filter); |
| 82 | void setNameFilters(const QStringList &filters); |
| 83 | QStringList nameFilters() const; |
| 84 | void selectNameFilter(const QString &filter); |
| 85 | QString selectedMimeTypeFilter() const; |
| 86 | QString selectedNameFilter() const; |
| 87 | |
| 88 | #if QT_CONFIG(mimetype) |
| 89 | void setMimeTypeFilters(const QStringList &filters); |
| 90 | QStringList mimeTypeFilters() const; |
| 91 | void selectMimeTypeFilter(const QString &filter); |
| 92 | #endif |
| 93 | |
| 94 | QDir::Filters filter() const; |
| 95 | void setFilter(QDir::Filters filters); |
| 96 | |
| 97 | void setViewMode(ViewMode mode); |
| 98 | ViewMode viewMode() const; |
| 99 | |
| 100 | void setFileMode(FileMode mode); |
| 101 | FileMode fileMode() const; |
| 102 | |
| 103 | void setAcceptMode(AcceptMode mode); |
| 104 | AcceptMode acceptMode() const; |
| 105 | |
| 106 | void (const QList<QUrl> &urls); |
| 107 | QList<QUrl> () const; |
| 108 | |
| 109 | QByteArray saveState() const; |
| 110 | bool restoreState(const QByteArray &state); |
| 111 | |
| 112 | void setDefaultSuffix(const QString &suffix); |
| 113 | QString defaultSuffix() const; |
| 114 | |
| 115 | void setHistory(const QStringList &paths); |
| 116 | QStringList history() const; |
| 117 | |
| 118 | void setItemDelegate(QAbstractItemDelegate *delegate); |
| 119 | QAbstractItemDelegate *itemDelegate() const; |
| 120 | |
| 121 | void setIconProvider(QAbstractFileIconProvider *provider); |
| 122 | QAbstractFileIconProvider *iconProvider() const; |
| 123 | |
| 124 | void setLabelText(DialogLabel label, const QString &text); |
| 125 | QString labelText(DialogLabel label) const; |
| 126 | |
| 127 | void setSupportedSchemes(const QStringList &schemes); |
| 128 | QStringList supportedSchemes() const; |
| 129 | |
| 130 | #if QT_CONFIG(proxymodel) |
| 131 | void setProxyModel(QAbstractProxyModel *model); |
| 132 | QAbstractProxyModel *proxyModel() const; |
| 133 | #endif |
| 134 | |
| 135 | void setOption(Option option, bool on = true); |
| 136 | bool testOption(Option option) const; |
| 137 | void setOptions(Options options); |
| 138 | Options options() const; |
| 139 | |
| 140 | using QDialog::open; |
| 141 | void open(QObject *receiver, const char *member); |
| 142 | void setVisible(bool visible) override; |
| 143 | |
| 144 | Q_SIGNALS: |
| 145 | void fileSelected(const QString &file); |
| 146 | void filesSelected(const QStringList &files); |
| 147 | void currentChanged(const QString &path); |
| 148 | void directoryEntered(const QString &directory); |
| 149 | |
| 150 | void urlSelected(const QUrl &url); |
| 151 | void urlsSelected(const QList<QUrl> &urls); |
| 152 | void currentUrlChanged(const QUrl &url); |
| 153 | void directoryUrlEntered(const QUrl &directory); |
| 154 | |
| 155 | void filterSelected(const QString &filter); |
| 156 | |
| 157 | public: |
| 158 | |
| 159 | static QString getOpenFileName(QWidget *parent = nullptr, |
| 160 | const QString &caption = QString(), |
| 161 | const QString &dir = QString(), |
| 162 | const QString &filter = QString(), |
| 163 | QString *selectedFilter = nullptr, |
| 164 | Options options = Options()); |
| 165 | |
| 166 | static QUrl getOpenFileUrl(QWidget *parent = nullptr, |
| 167 | const QString &caption = QString(), |
| 168 | const QUrl &dir = QUrl(), |
| 169 | const QString &filter = QString(), |
| 170 | QString *selectedFilter = nullptr, |
| 171 | Options options = Options(), |
| 172 | const QStringList &supportedSchemes = QStringList()); |
| 173 | |
| 174 | static QString getSaveFileName(QWidget *parent = nullptr, |
| 175 | const QString &caption = QString(), |
| 176 | const QString &dir = QString(), |
| 177 | const QString &filter = QString(), |
| 178 | QString *selectedFilter = nullptr, |
| 179 | Options options = Options()); |
| 180 | |
| 181 | static QUrl getSaveFileUrl(QWidget *parent = nullptr, |
| 182 | const QString &caption = QString(), |
| 183 | const QUrl &dir = QUrl(), |
| 184 | const QString &filter = QString(), |
| 185 | QString *selectedFilter = nullptr, |
| 186 | Options options = Options(), |
| 187 | const QStringList &supportedSchemes = QStringList()); |
| 188 | |
| 189 | static QString getExistingDirectory(QWidget *parent = nullptr, |
| 190 | const QString &caption = QString(), |
| 191 | const QString &dir = QString(), |
| 192 | Options options = ShowDirsOnly); |
| 193 | |
| 194 | static QUrl getExistingDirectoryUrl(QWidget *parent = nullptr, |
| 195 | const QString &caption = QString(), |
| 196 | const QUrl &dir = QUrl(), |
| 197 | Options options = ShowDirsOnly, |
| 198 | const QStringList &supportedSchemes = QStringList()); |
| 199 | |
| 200 | static QStringList getOpenFileNames(QWidget *parent = nullptr, |
| 201 | const QString &caption = QString(), |
| 202 | const QString &dir = QString(), |
| 203 | const QString &filter = QString(), |
| 204 | QString *selectedFilter = nullptr, |
| 205 | Options options = Options()); |
| 206 | |
| 207 | static QList<QUrl> getOpenFileUrls(QWidget *parent = nullptr, |
| 208 | const QString &caption = QString(), |
| 209 | const QUrl &dir = QUrl(), |
| 210 | const QString &filter = QString(), |
| 211 | QString *selectedFilter = nullptr, |
| 212 | Options options = Options(), |
| 213 | const QStringList &supportedSchemes = QStringList()); |
| 214 | |
| 215 | static void getOpenFileContent(const QString &nameFilter, |
| 216 | const std::function<void(const QString &, const QByteArray &)> &fileContentsReady, |
| 217 | QWidget *parent= nullptr); |
| 218 | |
| 219 | static void saveFileContent(const QByteArray &fileContent, |
| 220 | const QString &fileNameHint, |
| 221 | QWidget *parent = nullptr); |
| 222 | |
| 223 | #if QT_WIDGETS_REMOVED_SINCE(6, 7) |
| 224 | static void getOpenFileContent(const QString &nameFilter, |
| 225 | const std::function<void(const QString &, const QByteArray &)> &fileContentsReady); |
| 226 | static void saveFileContent(const QByteArray &fileContent, |
| 227 | const QString &fileNameHint = QString()); |
| 228 | #endif |
| 229 | |
| 230 | |
| 231 | protected: |
| 232 | QFileDialog(const QFileDialogArgs &args); |
| 233 | void done(int result) override; |
| 234 | void accept() override; |
| 235 | void changeEvent(QEvent *e) override; |
| 236 | |
| 237 | private: |
| 238 | Q_DECLARE_PRIVATE(QFileDialog) |
| 239 | Q_DISABLE_COPY(QFileDialog) |
| 240 | |
| 241 | friend class QPlatformDialogHelper; |
| 242 | }; |
| 243 | |
| 244 | inline void QFileDialog::setDirectory(const QDir &adirectory) |
| 245 | { setDirectory(adirectory.absolutePath()); } |
| 246 | |
| 247 | Q_DECLARE_OPERATORS_FOR_FLAGS(QFileDialog::Options) |
| 248 | |
| 249 | QT_END_NAMESPACE |
| 250 | |
| 251 | #endif // QFILEDIALOG_H |
| 252 | |