1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtWidgets module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QFILEDIALOG_H
41 #define QFILEDIALOG_H
42 
43 #include <QtWidgets/qtwidgetsglobal.h>
44 #include <QtCore/qdir.h>
45 #include <QtCore/qstring.h>
46 #include <QtCore/qurl.h>
47 #include <QtWidgets/qdialog.h>
48 
49 #include <functional>
50 
51 QT_REQUIRE_CONFIG(filedialog);
52 
53 QT_BEGIN_NAMESPACE
54 
55 class QModelIndex;
56 class QItemSelection;
57 struct QFileDialogArgs;
58 class QFileIconProvider;
59 class QFileDialogPrivate;
60 class QAbstractItemDelegate;
61 class QAbstractProxyModel;
62 
63 class Q_WIDGETS_EXPORT QFileDialog : public QDialog
64 {
65     Q_OBJECT
66     Q_PROPERTY(ViewMode viewMode READ viewMode WRITE setViewMode)
67     Q_PROPERTY(FileMode fileMode READ fileMode WRITE setFileMode)
68     Q_PROPERTY(AcceptMode acceptMode READ acceptMode WRITE setAcceptMode)
69     Q_PROPERTY(QString defaultSuffix READ defaultSuffix WRITE setDefaultSuffix)
70 #if QT_DEPRECATED_SINCE(5, 13)
71     Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly DESIGNABLE false)
72     Q_PROPERTY(bool confirmOverwrite READ confirmOverwrite WRITE setConfirmOverwrite DESIGNABLE false)
73     Q_PROPERTY(bool resolveSymlinks READ resolveSymlinks WRITE setResolveSymlinks DESIGNABLE false)
74     Q_PROPERTY(bool nameFilterDetailsVisible READ isNameFilterDetailsVisible
75                WRITE setNameFilterDetailsVisible DESIGNABLE false)
76 #endif
77     Q_PROPERTY(Options options READ options WRITE setOptions)
78     Q_PROPERTY(QStringList supportedSchemes READ supportedSchemes WRITE setSupportedSchemes)
79 
80 public:
81     enum ViewMode { Detail, List };
82     Q_ENUM(ViewMode)
83     enum FileMode { AnyFile, ExistingFile, Directory, ExistingFiles,
84                     DirectoryOnly Q_DECL_ENUMERATOR_DEPRECATED_X("Use setOption(ShowDirsOnly, true) instead")};
85     Q_ENUM(FileMode)
86     enum AcceptMode { AcceptOpen, AcceptSave };
87     Q_ENUM(AcceptMode)
88     enum DialogLabel { LookIn, FileName, FileType, Accept, Reject };
89 
90     enum Option
91     {
92         ShowDirsOnly                = 0x00000001,
93         DontResolveSymlinks         = 0x00000002,
94         DontConfirmOverwrite        = 0x00000004,
95 #if QT_DEPRECATED_SINCE(5, 14)
96         DontUseSheet Q_DECL_ENUMERATOR_DEPRECATED = 0x00000008,
97 #endif
98         DontUseNativeDialog         = 0x00000010,
99         ReadOnly                    = 0x00000020,
100         HideNameFilterDetails       = 0x00000040,
101         DontUseCustomDirectoryIcons = 0x00000080
102     };
103     Q_ENUM(Option)
104     Q_DECLARE_FLAGS(Options, Option)
105     Q_FLAG(Options)
106 
107     QFileDialog(QWidget *parent, Qt::WindowFlags f);
108     explicit QFileDialog(QWidget *parent = nullptr,
109                          const QString &caption = QString(),
110                          const QString &directory = QString(),
111                          const QString &filter = QString());
112     ~QFileDialog();
113 
114     void setDirectory(const QString &directory);
115     inline void setDirectory(const QDir &directory);
116     QDir directory() const;
117 
118     void setDirectoryUrl(const QUrl &directory);
119     QUrl directoryUrl() const;
120 
121     void selectFile(const QString &filename);
122     QStringList selectedFiles() const;
123 
124     void selectUrl(const QUrl &url);
125     QList<QUrl> selectedUrls() const;
126 
127 #if QT_DEPRECATED_SINCE(5, 13)
128     QT_DEPRECATED_X("Use setOption(HideNameFilterDetails, !enabled) instead")
129     void setNameFilterDetailsVisible(bool enabled);
130     QT_DEPRECATED_X("Use !testOption(HideNameFilterDetails) instead")
131     bool isNameFilterDetailsVisible() const;
132 #endif
133 
134     void setNameFilter(const QString &filter);
135     void setNameFilters(const QStringList &filters);
136     QStringList nameFilters() const;
137     void selectNameFilter(const QString &filter);
138     QString selectedMimeTypeFilter() const;
139     QString selectedNameFilter() const;
140 
141 #if QT_CONFIG(mimetype)
142     void setMimeTypeFilters(const QStringList &filters);
143     QStringList mimeTypeFilters() const;
144     void selectMimeTypeFilter(const QString &filter);
145 #endif
146 
147     QDir::Filters filter() const;
148     void setFilter(QDir::Filters filters);
149 
150     void setViewMode(ViewMode mode);
151     ViewMode viewMode() const;
152 
153     void setFileMode(FileMode mode);
154     FileMode fileMode() const;
155 
156     void setAcceptMode(AcceptMode mode);
157     AcceptMode acceptMode() const;
158 
159 #if QT_DEPRECATED_SINCE(5, 13)
160     void setReadOnly(bool enabled);
161     bool isReadOnly() const;
162 
163     QT_DEPRECATED_X("Use setOption(DontResolveSymlinks, !enabled) instead")
164     void setResolveSymlinks(bool enabled);
165     QT_DEPRECATED_X("Use !testOption(DontResolveSymlinks) instead")
166     bool resolveSymlinks() const;
167 #endif
168 
169     void setSidebarUrls(const QList<QUrl> &urls);
170     QList<QUrl> sidebarUrls() const;
171 
172     QByteArray saveState() const;
173     bool restoreState(const QByteArray &state);
174 
175 #if QT_DEPRECATED_SINCE(5, 13)
176     QT_DEPRECATED_X("Use setOption(DontConfirmOverwrite, !enabled) instead")
177     void setConfirmOverwrite(bool enabled);
178     QT_DEPRECATED_X("Use !testOption(DontConfirmOverwrite) instead")
179     bool confirmOverwrite() const;
180 #endif
181 
182     void setDefaultSuffix(const QString &suffix);
183     QString defaultSuffix() const;
184 
185     void setHistory(const QStringList &paths);
186     QStringList history() const;
187 
188     void setItemDelegate(QAbstractItemDelegate *delegate);
189     QAbstractItemDelegate *itemDelegate() const;
190 
191     void setIconProvider(QFileIconProvider *provider);
192     QFileIconProvider *iconProvider() const;
193 
194     void setLabelText(DialogLabel label, const QString &text);
195     QString labelText(DialogLabel label) const;
196 
197     void setSupportedSchemes(const QStringList &schemes);
198     QStringList supportedSchemes() const;
199 
200 #if QT_CONFIG(proxymodel)
201     void setProxyModel(QAbstractProxyModel *model);
202     QAbstractProxyModel *proxyModel() const;
203 #endif
204 
205     void setOption(Option option, bool on = true);
206     bool testOption(Option option) const;
207     void setOptions(Options options);
208     Options options() const;
209 
210     using QDialog::open;
211     void open(QObject *receiver, const char *member);
212     void setVisible(bool visible) override;
213 
214 Q_SIGNALS:
215     void fileSelected(const QString &file);
216     void filesSelected(const QStringList &files);
217     void currentChanged(const QString &path);
218     void directoryEntered(const QString &directory);
219 
220     void urlSelected(const QUrl &url);
221     void urlsSelected(const QList<QUrl> &urls);
222     void currentUrlChanged(const QUrl &url);
223     void directoryUrlEntered(const QUrl &directory);
224 
225     void filterSelected(const QString &filter);
226 
227 public:
228 
229     static QString getOpenFileName(QWidget *parent = nullptr,
230                                    const QString &caption = QString(),
231                                    const QString &dir = QString(),
232                                    const QString &filter = QString(),
233                                    QString *selectedFilter = nullptr,
234                                    Options options = Options());
235 
236     static QUrl getOpenFileUrl(QWidget *parent = nullptr,
237                                const QString &caption = QString(),
238                                const QUrl &dir = QUrl(),
239                                const QString &filter = QString(),
240                                QString *selectedFilter = nullptr,
241                                Options options = Options(),
242                                const QStringList &supportedSchemes = QStringList());
243 
244     static QString getSaveFileName(QWidget *parent = nullptr,
245                                    const QString &caption = QString(),
246                                    const QString &dir = QString(),
247                                    const QString &filter = QString(),
248                                    QString *selectedFilter = nullptr,
249                                    Options options = Options());
250 
251     static QUrl getSaveFileUrl(QWidget *parent = nullptr,
252                                const QString &caption = QString(),
253                                const QUrl &dir = QUrl(),
254                                const QString &filter = QString(),
255                                QString *selectedFilter = nullptr,
256                                Options options = Options(),
257                                const QStringList &supportedSchemes = QStringList());
258 
259     static QString getExistingDirectory(QWidget *parent = nullptr,
260                                         const QString &caption = QString(),
261                                         const QString &dir = QString(),
262                                         Options options = ShowDirsOnly);
263 
264     static QUrl getExistingDirectoryUrl(QWidget *parent = nullptr,
265                                         const QString &caption = QString(),
266                                         const QUrl &dir = QUrl(),
267                                         Options options = ShowDirsOnly,
268                                         const QStringList &supportedSchemes = QStringList());
269 
270     static QStringList getOpenFileNames(QWidget *parent = nullptr,
271                                         const QString &caption = QString(),
272                                         const QString &dir = QString(),
273                                         const QString &filter = QString(),
274                                         QString *selectedFilter = nullptr,
275                                         Options options = Options());
276 
277     static QList<QUrl> getOpenFileUrls(QWidget *parent = nullptr,
278                                        const QString &caption = QString(),
279                                        const QUrl &dir = QUrl(),
280                                        const QString &filter = QString(),
281                                        QString *selectedFilter = nullptr,
282                                        Options options = Options(),
283                                        const QStringList &supportedSchemes = QStringList());
284 
285     static void getOpenFileContent(const QString &nameFilter,
286                                    const std::function<void(const QString &, const QByteArray &)> &fileContentsReady);
287     static void saveFileContent(const QByteArray &fileContent, const QString &fileNameHint = QString());
288 
289 protected:
290     QFileDialog(const QFileDialogArgs &args);
291     void done(int result) override;
292     void accept() override;
293     void changeEvent(QEvent *e) override;
294 
295 private:
296     Q_DECLARE_PRIVATE(QFileDialog)
297     Q_DISABLE_COPY(QFileDialog)
298 
299     Q_PRIVATE_SLOT(d_func(), void _q_pathChanged(const QString &))
300 
301     Q_PRIVATE_SLOT(d_func(), void _q_navigateBackward())
302     Q_PRIVATE_SLOT(d_func(), void _q_navigateForward())
303     Q_PRIVATE_SLOT(d_func(), void _q_navigateToParent())
304     Q_PRIVATE_SLOT(d_func(), void _q_createDirectory())
305     Q_PRIVATE_SLOT(d_func(), void _q_showListView())
306     Q_PRIVATE_SLOT(d_func(), void _q_showDetailsView())
307     Q_PRIVATE_SLOT(d_func(), void _q_showContextMenu(const QPoint &))
308     Q_PRIVATE_SLOT(d_func(), void _q_renameCurrent())
309     Q_PRIVATE_SLOT(d_func(), void _q_deleteCurrent())
310     Q_PRIVATE_SLOT(d_func(), void _q_showHidden())
311     Q_PRIVATE_SLOT(d_func(), void _q_updateOkButton())
312     Q_PRIVATE_SLOT(d_func(), void _q_currentChanged(const QModelIndex &index))
313     Q_PRIVATE_SLOT(d_func(), void _q_enterDirectory(const QModelIndex &index))
314     Q_PRIVATE_SLOT(d_func(), void _q_emitUrlSelected(const QUrl &))
315     Q_PRIVATE_SLOT(d_func(), void _q_emitUrlsSelected(const QList<QUrl> &))
316     Q_PRIVATE_SLOT(d_func(), void _q_nativeCurrentChanged(const QUrl &))
317     Q_PRIVATE_SLOT(d_func(), void _q_nativeEnterDirectory(const QUrl&))
318     Q_PRIVATE_SLOT(d_func(), void _q_goToDirectory(const QString &path))
319     Q_PRIVATE_SLOT(d_func(), void _q_useNameFilter(int index))
320     Q_PRIVATE_SLOT(d_func(), void _q_selectionChanged())
321     Q_PRIVATE_SLOT(d_func(), void _q_goToUrl(const QUrl &url))
322     Q_PRIVATE_SLOT(d_func(), void _q_goHome())
323     Q_PRIVATE_SLOT(d_func(), void _q_showHeader(QAction *))
324     Q_PRIVATE_SLOT(d_func(), void _q_autoCompleteFileName(const QString &text))
325     Q_PRIVATE_SLOT(d_func(), void _q_rowsInserted(const QModelIndex & parent))
326     Q_PRIVATE_SLOT(d_func(), void _q_fileRenamed(const QString &path,
327                                                  const QString &oldName,
328                                                  const QString &newName))
329     friend class QPlatformDialogHelper;
330 };
331 
setDirectory(const QDir & adirectory)332 inline void QFileDialog::setDirectory(const QDir &adirectory)
333 { setDirectory(adirectory.absolutePath()); }
334 
335 Q_DECLARE_OPERATORS_FOR_FLAGS(QFileDialog::Options)
336 
337 QT_END_NAMESPACE
338 
339 #endif // QFILEDIALOG_H
340