| 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 QFileDialogPrivate; |
| 59 | class QAbstractFileIconProvider; |
| 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 | Q_PROPERTY(Options options READ options WRITE setOptions) |
| 71 | Q_PROPERTY(QStringList supportedSchemes READ supportedSchemes WRITE setSupportedSchemes) |
| 72 | |
| 73 | public: |
| 74 | enum ViewMode { Detail, List }; |
| 75 | Q_ENUM(ViewMode) |
| 76 | enum FileMode { AnyFile, ExistingFile, Directory, ExistingFiles }; |
| 77 | Q_ENUM(FileMode) |
| 78 | enum AcceptMode { AcceptOpen, AcceptSave }; |
| 79 | Q_ENUM(AcceptMode) |
| 80 | enum DialogLabel { LookIn, FileName, FileType, Accept, Reject }; |
| 81 | |
| 82 | enum Option |
| 83 | { |
| 84 | ShowDirsOnly = 0x00000001, |
| 85 | DontResolveSymlinks = 0x00000002, |
| 86 | DontConfirmOverwrite = 0x00000004, |
| 87 | DontUseNativeDialog = 0x00000008, |
| 88 | ReadOnly = 0x00000010, |
| 89 | HideNameFilterDetails = 0x00000020, |
| 90 | DontUseCustomDirectoryIcons = 0x00000040 |
| 91 | }; |
| 92 | Q_ENUM(Option) |
| 93 | Q_DECLARE_FLAGS(Options, Option) |
| 94 | Q_FLAG(Options) |
| 95 | |
| 96 | QFileDialog(QWidget *parent, Qt::WindowFlags f); |
| 97 | explicit QFileDialog(QWidget *parent = nullptr, |
| 98 | const QString &caption = QString(), |
| 99 | const QString &directory = QString(), |
| 100 | const QString &filter = QString()); |
| 101 | ~QFileDialog(); |
| 102 | |
| 103 | void setDirectory(const QString &directory); |
| 104 | inline void setDirectory(const QDir &directory); |
| 105 | QDir directory() const; |
| 106 | |
| 107 | void setDirectoryUrl(const QUrl &directory); |
| 108 | QUrl directoryUrl() const; |
| 109 | |
| 110 | void selectFile(const QString &filename); |
| 111 | QStringList selectedFiles() const; |
| 112 | |
| 113 | void selectUrl(const QUrl &url); |
| 114 | QList<QUrl> selectedUrls() const; |
| 115 | |
| 116 | void setNameFilter(const QString &filter); |
| 117 | void setNameFilters(const QStringList &filters); |
| 118 | QStringList nameFilters() const; |
| 119 | void selectNameFilter(const QString &filter); |
| 120 | QString selectedMimeTypeFilter() const; |
| 121 | QString selectedNameFilter() const; |
| 122 | |
| 123 | #if QT_CONFIG(mimetype) |
| 124 | void setMimeTypeFilters(const QStringList &filters); |
| 125 | QStringList mimeTypeFilters() const; |
| 126 | void selectMimeTypeFilter(const QString &filter); |
| 127 | #endif |
| 128 | |
| 129 | QDir::Filters filter() const; |
| 130 | void setFilter(QDir::Filters filters); |
| 131 | |
| 132 | void setViewMode(ViewMode mode); |
| 133 | ViewMode viewMode() const; |
| 134 | |
| 135 | void setFileMode(FileMode mode); |
| 136 | FileMode fileMode() const; |
| 137 | |
| 138 | void setAcceptMode(AcceptMode mode); |
| 139 | AcceptMode acceptMode() const; |
| 140 | |
| 141 | void (const QList<QUrl> &urls); |
| 142 | QList<QUrl> () const; |
| 143 | |
| 144 | QByteArray saveState() const; |
| 145 | bool restoreState(const QByteArray &state); |
| 146 | |
| 147 | void setDefaultSuffix(const QString &suffix); |
| 148 | QString defaultSuffix() const; |
| 149 | |
| 150 | void setHistory(const QStringList &paths); |
| 151 | QStringList history() const; |
| 152 | |
| 153 | void setItemDelegate(QAbstractItemDelegate *delegate); |
| 154 | QAbstractItemDelegate *itemDelegate() const; |
| 155 | |
| 156 | void setIconProvider(QAbstractFileIconProvider *provider); |
| 157 | QAbstractFileIconProvider *iconProvider() const; |
| 158 | |
| 159 | void setLabelText(DialogLabel label, const QString &text); |
| 160 | QString labelText(DialogLabel label) const; |
| 161 | |
| 162 | void setSupportedSchemes(const QStringList &schemes); |
| 163 | QStringList supportedSchemes() const; |
| 164 | |
| 165 | #if QT_CONFIG(proxymodel) |
| 166 | void setProxyModel(QAbstractProxyModel *model); |
| 167 | QAbstractProxyModel *proxyModel() const; |
| 168 | #endif |
| 169 | |
| 170 | void setOption(Option option, bool on = true); |
| 171 | bool testOption(Option option) const; |
| 172 | void setOptions(Options options); |
| 173 | Options options() const; |
| 174 | |
| 175 | using QDialog::open; |
| 176 | void open(QObject *receiver, const char *member); |
| 177 | void setVisible(bool visible) override; |
| 178 | |
| 179 | Q_SIGNALS: |
| 180 | void fileSelected(const QString &file); |
| 181 | void filesSelected(const QStringList &files); |
| 182 | void currentChanged(const QString &path); |
| 183 | void directoryEntered(const QString &directory); |
| 184 | |
| 185 | void urlSelected(const QUrl &url); |
| 186 | void urlsSelected(const QList<QUrl> &urls); |
| 187 | void currentUrlChanged(const QUrl &url); |
| 188 | void directoryUrlEntered(const QUrl &directory); |
| 189 | |
| 190 | void filterSelected(const QString &filter); |
| 191 | |
| 192 | public: |
| 193 | |
| 194 | static QString getOpenFileName(QWidget *parent = nullptr, |
| 195 | const QString &caption = QString(), |
| 196 | const QString &dir = QString(), |
| 197 | const QString &filter = QString(), |
| 198 | QString *selectedFilter = nullptr, |
| 199 | Options options = Options()); |
| 200 | |
| 201 | static QUrl getOpenFileUrl(QWidget *parent = nullptr, |
| 202 | const QString &caption = QString(), |
| 203 | const QUrl &dir = QUrl(), |
| 204 | const QString &filter = QString(), |
| 205 | QString *selectedFilter = nullptr, |
| 206 | Options options = Options(), |
| 207 | const QStringList &supportedSchemes = QStringList()); |
| 208 | |
| 209 | static QString getSaveFileName(QWidget *parent = nullptr, |
| 210 | const QString &caption = QString(), |
| 211 | const QString &dir = QString(), |
| 212 | const QString &filter = QString(), |
| 213 | QString *selectedFilter = nullptr, |
| 214 | Options options = Options()); |
| 215 | |
| 216 | static QUrl getSaveFileUrl(QWidget *parent = nullptr, |
| 217 | const QString &caption = QString(), |
| 218 | const QUrl &dir = QUrl(), |
| 219 | const QString &filter = QString(), |
| 220 | QString *selectedFilter = nullptr, |
| 221 | Options options = Options(), |
| 222 | const QStringList &supportedSchemes = QStringList()); |
| 223 | |
| 224 | static QString getExistingDirectory(QWidget *parent = nullptr, |
| 225 | const QString &caption = QString(), |
| 226 | const QString &dir = QString(), |
| 227 | Options options = ShowDirsOnly); |
| 228 | |
| 229 | static QUrl getExistingDirectoryUrl(QWidget *parent = nullptr, |
| 230 | const QString &caption = QString(), |
| 231 | const QUrl &dir = QUrl(), |
| 232 | Options options = ShowDirsOnly, |
| 233 | const QStringList &supportedSchemes = QStringList()); |
| 234 | |
| 235 | static QStringList getOpenFileNames(QWidget *parent = nullptr, |
| 236 | const QString &caption = QString(), |
| 237 | const QString &dir = QString(), |
| 238 | const QString &filter = QString(), |
| 239 | QString *selectedFilter = nullptr, |
| 240 | Options options = Options()); |
| 241 | |
| 242 | static QList<QUrl> getOpenFileUrls(QWidget *parent = nullptr, |
| 243 | const QString &caption = QString(), |
| 244 | const QUrl &dir = QUrl(), |
| 245 | const QString &filter = QString(), |
| 246 | QString *selectedFilter = nullptr, |
| 247 | Options options = Options(), |
| 248 | const QStringList &supportedSchemes = QStringList()); |
| 249 | |
| 250 | static void getOpenFileContent(const QString &nameFilter, |
| 251 | const std::function<void(const QString &, const QByteArray &)> &fileContentsReady); |
| 252 | static void saveFileContent(const QByteArray &fileContent, const QString &fileNameHint = QString()); |
| 253 | |
| 254 | protected: |
| 255 | QFileDialog(const QFileDialogArgs &args); |
| 256 | void done(int result) override; |
| 257 | void accept() override; |
| 258 | void changeEvent(QEvent *e) override; |
| 259 | |
| 260 | private: |
| 261 | Q_DECLARE_PRIVATE(QFileDialog) |
| 262 | Q_DISABLE_COPY(QFileDialog) |
| 263 | |
| 264 | Q_PRIVATE_SLOT(d_func(), void _q_pathChanged(const QString &)) |
| 265 | |
| 266 | Q_PRIVATE_SLOT(d_func(), void _q_navigateBackward()) |
| 267 | Q_PRIVATE_SLOT(d_func(), void _q_navigateForward()) |
| 268 | Q_PRIVATE_SLOT(d_func(), void _q_navigateToParent()) |
| 269 | Q_PRIVATE_SLOT(d_func(), void _q_createDirectory()) |
| 270 | Q_PRIVATE_SLOT(d_func(), void _q_showListView()) |
| 271 | Q_PRIVATE_SLOT(d_func(), void _q_showDetailsView()) |
| 272 | Q_PRIVATE_SLOT(d_func(), void _q_showContextMenu(const QPoint &)) |
| 273 | Q_PRIVATE_SLOT(d_func(), void _q_renameCurrent()) |
| 274 | Q_PRIVATE_SLOT(d_func(), void _q_deleteCurrent()) |
| 275 | Q_PRIVATE_SLOT(d_func(), void _q_showHidden()) |
| 276 | Q_PRIVATE_SLOT(d_func(), void _q_updateOkButton()) |
| 277 | Q_PRIVATE_SLOT(d_func(), void _q_currentChanged(const QModelIndex &index)) |
| 278 | Q_PRIVATE_SLOT(d_func(), void _q_enterDirectory(const QModelIndex &index)) |
| 279 | Q_PRIVATE_SLOT(d_func(), void _q_emitUrlSelected(const QUrl &)) |
| 280 | Q_PRIVATE_SLOT(d_func(), void _q_emitUrlsSelected(const QList<QUrl> &)) |
| 281 | Q_PRIVATE_SLOT(d_func(), void _q_nativeCurrentChanged(const QUrl &)) |
| 282 | Q_PRIVATE_SLOT(d_func(), void _q_nativeEnterDirectory(const QUrl&)) |
| 283 | Q_PRIVATE_SLOT(d_func(), void _q_goToDirectory(const QString &path)) |
| 284 | Q_PRIVATE_SLOT(d_func(), void _q_useNameFilter(int index)) |
| 285 | Q_PRIVATE_SLOT(d_func(), void _q_selectionChanged()) |
| 286 | Q_PRIVATE_SLOT(d_func(), void _q_goToUrl(const QUrl &url)) |
| 287 | Q_PRIVATE_SLOT(d_func(), void _q_goHome()) |
| 288 | Q_PRIVATE_SLOT(d_func(), void _q_showHeader(QAction *)) |
| 289 | Q_PRIVATE_SLOT(d_func(), void _q_autoCompleteFileName(const QString &text)) |
| 290 | Q_PRIVATE_SLOT(d_func(), void _q_rowsInserted(const QModelIndex & parent)) |
| 291 | Q_PRIVATE_SLOT(d_func(), void _q_fileRenamed(const QString &path, |
| 292 | const QString &oldName, |
| 293 | const QString &newName)) |
| 294 | friend class QPlatformDialogHelper; |
| 295 | }; |
| 296 | |
| 297 | inline void QFileDialog::setDirectory(const QDir &adirectory) |
| 298 | { setDirectory(adirectory.absolutePath()); } |
| 299 | |
| 300 | Q_DECLARE_OPERATORS_FOR_FLAGS(QFileDialog::Options) |
| 301 | |
| 302 | QT_END_NAMESPACE |
| 303 | |
| 304 | #endif // QFILEDIALOG_H |
| 305 | |