Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qfiledialog_p.h
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
4
5#ifndef QFILEDIALOG_P_H
6#define QFILEDIALOG_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtWidgets/private/qtwidgetsglobal_p.h>
20
21#include "qfiledialog.h"
22#include "private/qdialog_p.h"
23#include "qplatformdefs.h"
24
25#include <QtGui/private/qfilesystemmodel_p.h>
26#include <qlistview.h>
27#include <qtreeview.h>
28#include <qcombobox.h>
29#include <qtoolbutton.h>
30#include <qlabel.h>
31#include <qevent.h>
32#include <qlineedit.h>
33#include <qurl.h>
34#include <qstackedwidget.h>
35#include <qdialogbuttonbox.h>
36#include <qabstractproxymodel.h>
37#include <qfileiconprovider.h>
38#if QT_CONFIG(completer)
39#include <qcompleter.h>
40#endif
41#include <qpointer.h>
42#include "qsidebar_p.h"
43#if QT_CONFIG(fscompleter)
44#include "qfscompleter_p.h"
45#endif
46
47#if defined (Q_OS_UNIX)
48#include <unistd.h>
49#endif
50
51#include <memory>
52
54
55QT_BEGIN_NAMESPACE
56
57class QFileDialogListView;
60class QGridLayout;
61class QCompleter;
62class QHBoxLayout;
63class Ui_QFileDialog;
64class QPlatformDialogHelper;
65
78
79class Q_WIDGETS_EXPORT QFileDialogPrivate : public QDialogPrivate
80{
81 Q_DECLARE_PUBLIC(QFileDialog)
82
83public:
84 using PersistentModelIndexList = QList<QPersistentModelIndex>;
85
86 struct HistoryItem
87 {
88 QString path;
89 PersistentModelIndexList selection;
90 };
91
92 QFileDialogPrivate();
93
94 QPlatformFileDialogHelper *platformFileDialogHelper() const
95 { return static_cast<QPlatformFileDialogHelper *>(platformHelper()); }
96
97 void createToolButtons();
98 void createMenuActions();
99 void createWidgets();
100
101 void init(const QFileDialogArgs &args);
102 bool itemViewKeyboardEvent(QKeyEvent *event);
103 QString getEnvironmentVariable(const QString &string);
104 QStringList typedFiles() const;
105 QList<QUrl> userSelectedFiles() const;
106 QStringList addDefaultSuffixToFiles(const QStringList &filesToFix) const;
107 QList<QUrl> addDefaultSuffixToUrls(const QList<QUrl> &urlsToFix) const;
108 bool removeDirectory(const QString &path);
109 void setLabelTextControl(QFileDialog::DialogLabel label, const QString &text);
110 inline void updateLookInLabel();
111 inline void updateFileNameLabel();
112 inline void updateFileTypeLabel();
113 void updateOkButtonText(bool saveAsOnFolder = false);
114 void updateCancelButtonText();
115
116 inline QModelIndex mapToSource(const QModelIndex &index) const;
117 inline QModelIndex mapFromSource(const QModelIndex &index) const;
118 inline QModelIndex rootIndex() const;
119 inline void setRootIndex(const QModelIndex &index) const;
120 inline QModelIndex select(const QModelIndex &index) const;
121 inline QString rootPath() const;
122
123 QLineEdit *lineEdit() const;
124
125 static long maxNameLength(const QString &path);
126
127 QString basename(const QString &path) const
128 {
129 const qsizetype separator = QDir::toNativeSeparators(path).lastIndexOf(QDir::separator());
130 if (separator != -1)
131 return path.mid(separator + 1);
132 return path;
133 }
134
135 QDir::Filters filterForMode(QDir::Filters filters) const
136 {
137 filters |= QDir::Drives | QDir::AllDirs | QDir::Dirs | QDir::Files;
138 if (q_func()->testOption(QFileDialog::ShowDirsOnly))
139 filters &= ~QDir::Files;
140 return filters;
141 }
142
143 QAbstractItemView *currentView() const;
144
145 static inline QString toInternal(const QString &path)
146 {
147#if defined(Q_OS_WIN)
148 QString n(path);
149 n.replace(u'\\', u'/');
150 return n;
151#else // the compile should optimize away this
152 return path;
153#endif
154 }
155
156#if QT_CONFIG(settings)
157 void saveSettings();
158 bool restoreFromSettings();
159#endif
160
161 bool restoreWidgetState(QStringList &history, int splitterPosition);
162 static void setLastVisitedDirectory(const QUrl &dir);
163 void retranslateWindowTitle();
164 void retranslateStrings();
165 void emitFilesSelected(const QStringList &files);
166 void saveHistorySelection();
167
168 void goHome();
169 void pathChanged(const QString &);
170 void navigate(HistoryItem &);
171 void navigateBackward();
172 void navigateForward();
173 void navigateToParent();
174 void createDirectory();
175 void showListView();
176 void showDetailsView();
177 void showContextMenu(const QPoint &position);
178 void renameCurrent();
179 void deleteCurrent();
180 void showHidden();
181 void showHeader(QAction *);
182 void updateOkButton();
183 void currentChanged(const QModelIndex &index);
184 void enterDirectory(const QModelIndex &index);
185 void emitUrlSelected(const QUrl &file);
186 void emitUrlsSelected(const QList<QUrl> &files);
187 void nativeCurrentChanged(const QUrl &file);
188 void nativeEnterDirectory(const QUrl &directory);
189 void goToDirectory(const QString &);
190 void useNameFilter(int index);
191 void selectionChanged();
192 void goToUrl(const QUrl &url);
193 void autoCompleteFileName(const QString &);
194 void rowsInserted(const QModelIndex & parent);
195 void fileRenamed(const QString &path, const QString &oldName, const QString &newName);
196
197 // layout
198#if QT_CONFIG(proxymodel)
199 QAbstractProxyModel *proxyModel;
200#endif
201
202 // data
203 QStringList watching;
204 QFileSystemModel *model;
205
206#if QT_CONFIG(fscompleter)
207 QFSCompleter *completer;
208#endif //QT_CONFIG(fscompleter)
209
210 QString setWindowTitle;
211
212 QList<HistoryItem> currentHistory;
213 int currentHistoryLocation;
214
215 QAction *renameAction;
216 QAction *deleteAction;
217 QAction *showHiddenAction;
218 QAction *newFolderAction;
219
220 bool useDefaultCaption;
221
222 // setVisible_sys returns true if it ends up showing a native
223 // dialog. Returning false means that a non-native dialog must be
224 // used instead.
225 bool canBeNativeDialog() const override;
226 void setVisible(bool visible) override;
227 inline bool usingWidgets() const;
228
229 inline void setDirectory_sys(const QUrl &directory);
230 inline QUrl directory_sys() const;
231 inline void selectFile_sys(const QUrl &filename);
232 inline QList<QUrl> selectedFiles_sys() const;
233 inline void setFilter_sys();
234 inline void selectMimeTypeFilter_sys(const QString &filter);
235 inline QString selectedMimeTypeFilter_sys() const;
236 inline void selectNameFilter_sys(const QString &filter);
237 inline QString selectedNameFilter_sys() const;
238 //////////////////////////////////////////////
239
240 std::unique_ptr<Ui_QFileDialog> qFileDialogUi;
241
242 QString acceptLabel;
243
244 QPointer<QObject> receiverToDisconnectOnClose;
245 QByteArray memberToDisconnectOnClose;
246 QByteArray signalToDisconnectOnClose;
247
248 QSharedPointer<QFileDialogOptions> options;
249
250 // Memory of what was read from QSettings in restoreState() in case widgets are not used
251 QByteArray splitterState;
252 QByteArray headerData;
253 QList<QUrl> sidebarUrls;
254 QFileIconProvider defaultIconProvider;
255
256 ~QFileDialogPrivate();
257
258private:
259 virtual void initHelper(QPlatformDialogHelper *) override;
260 virtual void helperPrepareShow(QPlatformDialogHelper *) override;
261 virtual void helperDone(QDialog::DialogCode, QPlatformDialogHelper *) override;
262
263 void itemNotFound(const QString &fileName, QFileDialog::FileMode mode);
264 bool itemAlreadyExists(const QString &fileName);
265 Q_DISABLE_COPY_MOVE(QFileDialogPrivate)
266};
267
269{
270public:
271 QFileDialogLineEdit(QWidget *parent = nullptr) : QLineEdit(parent) {}
272 void keyPressEvent(QKeyEvent *e) override;
274};
275
277{
278public:
279 QFileDialogComboBox(QWidget *parent = nullptr) :
280 QComboBox(parent), urlModel(nullptr), d_ptr(nullptr) {}
281 void setFileDialogPrivate(QFileDialogPrivate *d_pointer);
282 void showPopup() override;
283 void setHistory(const QStringList &paths);
284 QStringList history() const { return m_history; }
285 void paintEvent(QPaintEvent *) override;
286
287private:
288 QUrlModel *urlModel;
289 QFileDialogPrivate *d_ptr;
290 QStringList m_history;
291};
292
294{
295public:
296 QFileDialogListView(QWidget *parent = nullptr) : QListView(parent), d_ptr(nullptr) {}
297 void setFileDialogPrivate(QFileDialogPrivate *d_pointer);
298 QSize sizeHint() const override;
299protected:
300 void keyPressEvent(QKeyEvent *e) override;
301private:
302 QFileDialogPrivate *d_ptr;
303};
304
306{
307public:
308 QFileDialogTreeView(QWidget *parent) : QTreeView(parent), d_ptr(nullptr) {}
309 void setFileDialogPrivate(QFileDialogPrivate *d_pointer);
310 QSize sizeHint() const override;
311
312protected:
313 void keyPressEvent(QKeyEvent *e) override;
314private:
315 QFileDialogPrivate *d_ptr;
316};
317
318QModelIndex QFileDialogPrivate::mapToSource(const QModelIndex &index) const {
319#if QT_CONFIG(proxymodel)
320 return proxyModel ? proxyModel->mapToSource(index) : index;
321#else
322 return index;
323#endif
324}
325QModelIndex QFileDialogPrivate::mapFromSource(const QModelIndex &index) const {
326#if QT_CONFIG(proxymodel)
327 return proxyModel ? proxyModel->mapFromSource(index) : index;
328#else
329 return index;
330#endif
331}
332
333QString QFileDialogPrivate::rootPath() const
334{
335 return (model ? model->rootPath() : QStringLiteral("/"));
336}
337
338void QFileDialogPrivate::setDirectory_sys(const QUrl &directory)
339{
340 QPlatformFileDialogHelper *helper = platformFileDialogHelper();
341
342 if (!helper)
343 return;
344
345 if (helper->isSupportedUrl(directory))
346 helper->setDirectory(directory);
347}
348
349QUrl QFileDialogPrivate::directory_sys() const
350{
351 if (QPlatformFileDialogHelper *helper = platformFileDialogHelper())
352 return helper->directory();
353 return QUrl();
354}
355
356void QFileDialogPrivate::selectFile_sys(const QUrl &filename)
357{
358 QPlatformFileDialogHelper *helper = platformFileDialogHelper();
359
360 if (!helper)
361 return;
362
363 if (helper->isSupportedUrl(filename))
364 helper->selectFile(filename);
365}
366
367QList<QUrl> QFileDialogPrivate::selectedFiles_sys() const
368{
369 if (QPlatformFileDialogHelper *helper = platformFileDialogHelper())
370 return helper->selectedFiles();
371 return QList<QUrl>();
372}
373
374void QFileDialogPrivate::setFilter_sys()
375{
376 if (QPlatformFileDialogHelper *helper = platformFileDialogHelper())
377 helper->setFilter();
378}
379
380void QFileDialogPrivate::selectMimeTypeFilter_sys(const QString &filter)
381{
382 if (QPlatformFileDialogHelper *helper = platformFileDialogHelper())
383 helper->selectMimeTypeFilter(filter);
384}
385
386QString QFileDialogPrivate::selectedMimeTypeFilter_sys() const
387{
388 if (QPlatformFileDialogHelper *helper = platformFileDialogHelper())
389 return helper->selectedMimeTypeFilter();
390
391 return QString();
392}
393
394void QFileDialogPrivate::selectNameFilter_sys(const QString &filter)
395{
396 if (QPlatformFileDialogHelper *helper = platformFileDialogHelper())
397 helper->selectNameFilter(filter);
398}
399
400QString QFileDialogPrivate::selectedNameFilter_sys() const
401{
402 if (QPlatformFileDialogHelper *helper = platformFileDialogHelper())
403 return helper->selectedNameFilter();
404 return QString();
405}
406
407QT_END_NAMESPACE
408
409#endif // QFILEDIALOG_P_H
QFileDialogComboBox(QWidget *parent=nullptr)
QStringList history() const
void setHistory(const QStringList &paths)
void setFileDialogPrivate(QFileDialogPrivate *d_pointer)
void showPopup() override
Displays the list of items in the combobox.
void paintEvent(QPaintEvent *) override
\reimp
\inmodule QtWidgets
void keyPressEvent(QKeyEvent *e) override
FIXME: this is a hack to avoid propagating key press events to the dialog and from there to the "Ok" ...
QFileDialogLineEdit(QWidget *parent=nullptr)
void setFileDialogPrivate(QFileDialogPrivate *d_pointer)
QSize sizeHint() const override
QFileDialogListView(QWidget *parent=nullptr)
void keyPressEvent(QKeyEvent *e) override
void keyPressEvent(QKeyEvent *e) override
QSize sizeHint() const override
QFileDialogTreeView(QWidget *parent)
void setFileDialogPrivate(QFileDialogPrivate *d_pointer)
Provides a dialog that allows users to select files or directories.
Definition qfiledialog.h:29
Definition qlist.h:82
\inmodule QtCore
QT_REQUIRE_CONFIG(animation)
static QUrl _qt_get_directory(const QUrl &url, const QFileInfo &local)
static void _qt_init_lastVisited()
QStringList qt_strip_filters(const QStringList &filters)
QStringList qt_make_filter_list(const QString &filter)
static QString fileFromPath(const QString &rootPath, QString path)
static bool isCaseSensitiveFileSystem(const QString &path)
QT_REQUIRE_CONFIG(filedialog)
QFileDialogArgs(const QUrl &url={})