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_P_H
41#define QFILEDIALOG_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists purely as an
48// implementation detail. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <QtWidgets/private/qtwidgetsglobal_p.h>
55
56#include "qfiledialog.h"
57#include "private/qdialog_p.h"
58#include "qplatformdefs.h"
59
60#include "qfilesystemmodel_p.h"
61#include <qlistview.h>
62#include <qtreeview.h>
63#include <qcombobox.h>
64#include <qtoolbutton.h>
65#include <qlabel.h>
66#include <qevent.h>
67#include <qlineedit.h>
68#include <qurl.h>
69#include <qstackedwidget.h>
70#include <qdialogbuttonbox.h>
71#include <qabstractproxymodel.h>
72#if QT_CONFIG(completer)
73#include <qcompleter.h>
74#endif
75#include <qpointer.h>
76#include <qdebug.h>
77#include "qsidebar_p.h"
78#if QT_CONFIG(fscompleter)
79#include "qfscompleter_p.h"
80#endif
81
82#if defined (Q_OS_UNIX)
83#include <unistd.h>
84#endif
85
86QT_REQUIRE_CONFIG(filedialog);
87
88QT_BEGIN_NAMESPACE
89
90class QFileDialogListView;
91class QFileDialogTreeView;
92class QFileDialogLineEdit;
93class QGridLayout;
94class QCompleter;
95class QHBoxLayout;
96class Ui_QFileDialog;
97class QPlatformDialogHelper;
98
99struct QFileDialogArgs
100{
101 QFileDialogArgs(const QUrl &url = {});
102
103 QWidget *parent = nullptr;
104 QString caption;
105 QUrl directory;
106 QString selection;
107 QString filter;
108 QFileDialog::FileMode mode = QFileDialog::AnyFile;
109 QFileDialog::Options options = {};
110};
111
112#define UrlRole (Qt::UserRole + 1)
113
114class Q_WIDGETS_EXPORT QFileDialogPrivate : public QDialogPrivate
115{
116 Q_DECLARE_PUBLIC(QFileDialog)
117
118public:
119 using PersistentModelIndexList = QVector<QPersistentModelIndex>;
120
121 struct HistoryItem
122 {
123 QString path;
124 PersistentModelIndexList selection;
125 };
126
127 QFileDialogPrivate();
128
129 QPlatformFileDialogHelper *platformFileDialogHelper() const
130 { return static_cast<QPlatformFileDialogHelper *>(platformHelper()); }
131
132 void createToolButtons();
133 void createMenuActions();
134 void createWidgets();
135
136 void init(const QFileDialogArgs &args);
137 bool itemViewKeyboardEvent(QKeyEvent *event);
138 QString getEnvironmentVariable(const QString &string);
139 QStringList typedFiles() const;
140 QList<QUrl> userSelectedFiles() const;
141 QStringList addDefaultSuffixToFiles(const QStringList &filesToFix) const;
142 QList<QUrl> addDefaultSuffixToUrls(const QList<QUrl> &urlsToFix) const;
143 bool removeDirectory(const QString &path);
144 void setLabelTextControl(QFileDialog::DialogLabel label, const QString &text);
145 inline void updateLookInLabel();
146 inline void updateFileNameLabel();
147 inline void updateFileTypeLabel();
148 void updateOkButtonText(bool saveAsOnFolder = false);
149 void updateCancelButtonText();
150
151 inline QModelIndex mapToSource(const QModelIndex &index) const;
152 inline QModelIndex mapFromSource(const QModelIndex &index) const;
153 inline QModelIndex rootIndex() const;
154 inline void setRootIndex(const QModelIndex &index) const;
155 inline QModelIndex select(const QModelIndex &index) const;
156 inline QString rootPath() const;
157
158 QLineEdit *lineEdit() const;
159
160 static int maxNameLength(const QString &path);
161
162 QString basename(const QString &path) const
163 {
164 int separator = QDir::toNativeSeparators(pathName: path).lastIndexOf(c: QDir::separator());
165 if (separator != -1)
166 return path.mid(position: separator + 1);
167 return path;
168 }
169
170 QDir::Filters filterForMode(QDir::Filters filters) const
171 {
172 filters |= QDir::Drives | QDir::AllDirs | QDir::Dirs | QDir::Files;
173 if (q_func()->testOption(option: QFileDialog::ShowDirsOnly))
174 filters &= ~QDir::Files;
175 return filters;
176 }
177
178 QAbstractItemView *currentView() const;
179
180 static inline QString toInternal(const QString &path)
181 {
182#if defined(Q_OS_WIN)
183 QString n(path);
184 n.replace(QLatin1Char('\\'), QLatin1Char('/'));
185 return n;
186#else // the compile should optimize away this
187 return path;
188#endif
189 }
190
191#if QT_CONFIG(settings)
192 void saveSettings();
193 bool restoreFromSettings();
194#endif
195
196 bool restoreWidgetState(QStringList &history, int splitterPosition);
197 static void setLastVisitedDirectory(const QUrl &dir);
198 void retranslateWindowTitle();
199 void retranslateStrings();
200 void emitFilesSelected(const QStringList &files);
201 void saveHistorySelection();
202
203 void _q_goHome();
204 void _q_pathChanged(const QString &);
205 void navigate(HistoryItem &);
206 void _q_navigateBackward();
207 void _q_navigateForward();
208 void _q_navigateToParent();
209 void _q_createDirectory();
210 void _q_showListView();
211 void _q_showDetailsView();
212 void _q_showContextMenu(const QPoint &position);
213 void _q_renameCurrent();
214 void _q_deleteCurrent();
215 void _q_showHidden();
216 void _q_showHeader(QAction *);
217 void _q_updateOkButton();
218 void _q_currentChanged(const QModelIndex &index);
219 void _q_enterDirectory(const QModelIndex &index);
220 void _q_emitUrlSelected(const QUrl &file);
221 void _q_emitUrlsSelected(const QList<QUrl> &files);
222 void _q_nativeCurrentChanged(const QUrl &file);
223 void _q_nativeEnterDirectory(const QUrl &directory);
224 void _q_goToDirectory(const QString &);
225 void _q_useNameFilter(int index);
226 void _q_selectionChanged();
227 void _q_goToUrl(const QUrl &url);
228 void _q_autoCompleteFileName(const QString &);
229 void _q_rowsInserted(const QModelIndex & parent);
230 void _q_fileRenamed(const QString &path, const QString &oldName, const QString &newName);
231
232 // layout
233#if QT_CONFIG(proxymodel)
234 QAbstractProxyModel *proxyModel;
235#endif
236
237 // data
238 QStringList watching;
239 QFileSystemModel *model;
240
241#if QT_CONFIG(fscompleter)
242 QFSCompleter *completer;
243#endif //QT_CONFIG(fscompleter)
244
245 QString setWindowTitle;
246
247 QList<HistoryItem> currentHistory;
248 int currentHistoryLocation;
249
250 QAction *renameAction;
251 QAction *deleteAction;
252 QAction *showHiddenAction;
253 QAction *newFolderAction;
254
255 bool useDefaultCaption;
256
257 // setVisible_sys returns true if it ends up showing a native
258 // dialog. Returning false means that a non-native dialog must be
259 // used instead.
260 bool canBeNativeDialog() const override;
261 inline bool usingWidgets() const;
262
263 inline void setDirectory_sys(const QUrl &directory);
264 inline QUrl directory_sys() const;
265 inline void selectFile_sys(const QUrl &filename);
266 inline QList<QUrl> selectedFiles_sys() const;
267 inline void setFilter_sys();
268 inline void selectMimeTypeFilter_sys(const QString &filter);
269 inline QString selectedMimeTypeFilter_sys() const;
270 inline void selectNameFilter_sys(const QString &filter);
271 inline QString selectedNameFilter_sys() const;
272 //////////////////////////////////////////////
273
274 QScopedPointer<Ui_QFileDialog> qFileDialogUi;
275
276 QString acceptLabel;
277
278 QPointer<QObject> receiverToDisconnectOnClose;
279 QByteArray memberToDisconnectOnClose;
280 QByteArray signalToDisconnectOnClose;
281
282 QSharedPointer<QFileDialogOptions> options;
283
284 // Memory of what was read from QSettings in restoreState() in case widgets are not used
285 QByteArray splitterState;
286 QByteArray headerData;
287 QList<QUrl> sidebarUrls;
288
289 ~QFileDialogPrivate();
290
291private:
292 virtual void initHelper(QPlatformDialogHelper *) override;
293 virtual void helperPrepareShow(QPlatformDialogHelper *) override;
294 virtual void helperDone(QDialog::DialogCode, QPlatformDialogHelper *) override;
295
296 Q_DISABLE_COPY_MOVE(QFileDialogPrivate)
297};
298
299class QFileDialogLineEdit : public QLineEdit
300{
301public:
302 QFileDialogLineEdit(QWidget *parent = nullptr) : QLineEdit(parent), d_ptr(nullptr){}
303 void setFileDialogPrivate(QFileDialogPrivate *d_pointer) {d_ptr = d_pointer; }
304 void keyPressEvent(QKeyEvent *e) override;
305 bool hideOnEsc;
306private:
307 QFileDialogPrivate *d_ptr;
308};
309
310class QFileDialogComboBox : public QComboBox
311{
312public:
313 QFileDialogComboBox(QWidget *parent = nullptr) : QComboBox(parent), urlModel(nullptr) {}
314 void setFileDialogPrivate(QFileDialogPrivate *d_pointer);
315 void showPopup() override;
316 void setHistory(const QStringList &paths);
317 QStringList history() const { return m_history; }
318 void paintEvent(QPaintEvent *) override;
319
320private:
321 QUrlModel *urlModel;
322 QFileDialogPrivate *d_ptr;
323 QStringList m_history;
324};
325
326class QFileDialogListView : public QListView
327{
328public:
329 QFileDialogListView(QWidget *parent = nullptr);
330 void setFileDialogPrivate(QFileDialogPrivate *d_pointer);
331 QSize sizeHint() const override;
332protected:
333 void keyPressEvent(QKeyEvent *e) override;
334private:
335 QFileDialogPrivate *d_ptr;
336};
337
338class QFileDialogTreeView : public QTreeView
339{
340public:
341 QFileDialogTreeView(QWidget *parent);
342 void setFileDialogPrivate(QFileDialogPrivate *d_pointer);
343 QSize sizeHint() const override;
344
345protected:
346 void keyPressEvent(QKeyEvent *e) override;
347private:
348 QFileDialogPrivate *d_ptr;
349};
350
351QModelIndex QFileDialogPrivate::mapToSource(const QModelIndex &index) const {
352#if QT_CONFIG(proxymodel)
353 return proxyModel ? proxyModel->mapToSource(proxyIndex: index) : index;
354#else
355 return index;
356#endif
357}
358QModelIndex QFileDialogPrivate::mapFromSource(const QModelIndex &index) const {
359#if QT_CONFIG(proxymodel)
360 return proxyModel ? proxyModel->mapFromSource(sourceIndex: index) : index;
361#else
362 return index;
363#endif
364}
365
366QString QFileDialogPrivate::rootPath() const
367{
368 return (model ? model->rootPath() : QStringLiteral("/"));
369}
370
371void QFileDialogPrivate::setDirectory_sys(const QUrl &directory)
372{
373 QPlatformFileDialogHelper *helper = platformFileDialogHelper();
374
375 if (!helper)
376 return;
377
378 if (helper->isSupportedUrl(url: directory))
379 helper->setDirectory(directory);
380}
381
382QUrl QFileDialogPrivate::directory_sys() const
383{
384 if (QPlatformFileDialogHelper *helper = platformFileDialogHelper())
385 return helper->directory();
386 return QUrl();
387}
388
389void QFileDialogPrivate::selectFile_sys(const QUrl &filename)
390{
391 QPlatformFileDialogHelper *helper = platformFileDialogHelper();
392
393 if (!helper)
394 return;
395
396 if (helper->isSupportedUrl(url: filename))
397 helper->selectFile(filename);
398}
399
400QList<QUrl> QFileDialogPrivate::selectedFiles_sys() const
401{
402 if (QPlatformFileDialogHelper *helper = platformFileDialogHelper())
403 return helper->selectedFiles();
404 return QList<QUrl>();
405}
406
407void QFileDialogPrivate::setFilter_sys()
408{
409 if (QPlatformFileDialogHelper *helper = platformFileDialogHelper())
410 helper->setFilter();
411}
412
413void QFileDialogPrivate::selectMimeTypeFilter_sys(const QString &filter)
414{
415 if (QPlatformFileDialogHelper *helper = platformFileDialogHelper())
416 helper->selectMimeTypeFilter(filter);
417}
418
419QString QFileDialogPrivate::selectedMimeTypeFilter_sys() const
420{
421 if (QPlatformFileDialogHelper *helper = platformFileDialogHelper())
422 return helper->selectedMimeTypeFilter();
423
424 return QString();
425}
426
427void QFileDialogPrivate::selectNameFilter_sys(const QString &filter)
428{
429 if (QPlatformFileDialogHelper *helper = platformFileDialogHelper())
430 helper->selectNameFilter(filter);
431}
432
433QString QFileDialogPrivate::selectedNameFilter_sys() const
434{
435 if (QPlatformFileDialogHelper *helper = platformFileDialogHelper())
436 return helper->selectedNameFilter();
437 return QString();
438}
439
440QT_END_NAMESPACE
441
442#endif // QFILEDIALOG_P_H
443

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