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
15QT_REQUIRE_CONFIG(filedialog);
16
17QT_BEGIN_NAMESPACE
18
19class QModelIndex;
20class QItemSelection;
21struct QFileDialogArgs;
22class QFileDialogPrivate;
23class QAbstractFileIconProvider;
24class QAbstractItemDelegate;
25class QAbstractProxyModel;
26
27class 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
37public:
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 setSidebarUrls(const QList<QUrl> &urls);
107 QList<QUrl> sidebarUrls() 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
144Q_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
157public:
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 static void saveFileContent(const QByteArray &fileContent, const QString &fileNameHint = QString());
218
219protected:
220 QFileDialog(const QFileDialogArgs &args);
221 void done(int result) override;
222 void accept() override;
223 void changeEvent(QEvent *e) override;
224
225private:
226 Q_DECLARE_PRIVATE(QFileDialog)
227 Q_DISABLE_COPY(QFileDialog)
228
229 Q_PRIVATE_SLOT(d_func(), void _q_pathChanged(const QString &))
230
231 Q_PRIVATE_SLOT(d_func(), void _q_navigateBackward())
232 Q_PRIVATE_SLOT(d_func(), void _q_navigateForward())
233 Q_PRIVATE_SLOT(d_func(), void _q_navigateToParent())
234 Q_PRIVATE_SLOT(d_func(), void _q_createDirectory())
235 Q_PRIVATE_SLOT(d_func(), void _q_showListView())
236 Q_PRIVATE_SLOT(d_func(), void _q_showDetailsView())
237 Q_PRIVATE_SLOT(d_func(), void _q_showContextMenu(const QPoint &))
238 Q_PRIVATE_SLOT(d_func(), void _q_renameCurrent())
239 Q_PRIVATE_SLOT(d_func(), void _q_deleteCurrent())
240 Q_PRIVATE_SLOT(d_func(), void _q_showHidden())
241 Q_PRIVATE_SLOT(d_func(), void _q_updateOkButton())
242 Q_PRIVATE_SLOT(d_func(), void _q_currentChanged(const QModelIndex &index))
243 Q_PRIVATE_SLOT(d_func(), void _q_enterDirectory(const QModelIndex &index))
244 Q_PRIVATE_SLOT(d_func(), void _q_emitUrlSelected(const QUrl &))
245 Q_PRIVATE_SLOT(d_func(), void _q_emitUrlsSelected(const QList<QUrl> &))
246 Q_PRIVATE_SLOT(d_func(), void _q_nativeCurrentChanged(const QUrl &))
247 Q_PRIVATE_SLOT(d_func(), void _q_nativeEnterDirectory(const QUrl&))
248 Q_PRIVATE_SLOT(d_func(), void _q_goToDirectory(const QString &path))
249 Q_PRIVATE_SLOT(d_func(), void _q_useNameFilter(int index))
250 Q_PRIVATE_SLOT(d_func(), void _q_selectionChanged())
251 Q_PRIVATE_SLOT(d_func(), void _q_goToUrl(const QUrl &url))
252 Q_PRIVATE_SLOT(d_func(), void _q_goHome())
253 Q_PRIVATE_SLOT(d_func(), void _q_showHeader(QAction *))
254 Q_PRIVATE_SLOT(d_func(), void _q_autoCompleteFileName(const QString &text))
255 Q_PRIVATE_SLOT(d_func(), void _q_rowsInserted(const QModelIndex & parent))
256 Q_PRIVATE_SLOT(d_func(), void _q_fileRenamed(const QString &path,
257 const QString &oldName,
258 const QString &newName))
259 friend class QPlatformDialogHelper;
260};
261
262inline void QFileDialog::setDirectory(const QDir &adirectory)
263{ setDirectory(adirectory.absolutePath()); }
264
265Q_DECLARE_OPERATORS_FOR_FLAGS(QFileDialog::Options)
266
267QT_END_NAMESPACE
268
269#endif // QFILEDIALOG_H
270

source code of qtbase/src/widgets/dialogs/qfiledialog.h