Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4#ifndef QFILEDIALOG_P_H
5#define QFILEDIALOG_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtWidgets/private/qtwidgetsglobal_p.h>
19
20#include "qfiledialog.h"
21#include "private/qdialog_p.h"
22#include "qplatformdefs.h"
23
24#include <QtGui/private/qfilesystemmodel_p.h>
25#include <qlistview.h>
26#include <qtreeview.h>
27#include <qcombobox.h>
28#include <qtoolbutton.h>
29#include <qlabel.h>
30#include <qevent.h>
31#include <qlineedit.h>
32#include <qurl.h>
33#include <qstackedwidget.h>
34#include <qdialogbuttonbox.h>
35#include <qabstractproxymodel.h>
36#include <qfileiconprovider.h>
37#if QT_CONFIG(completer)
38#include <qcompleter.h>
39#endif
40#include <qpointer.h>
41#include <qdebug.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
52
54
58class QGridLayout;
59class QCompleter;
60class QHBoxLayout;
61class Ui_QFileDialog;
63
76
77class Q_WIDGETS_EXPORT QFileDialogPrivate : public QDialogPrivate
78{
79 Q_DECLARE_PUBLIC(QFileDialog)
80
81public:
82 using PersistentModelIndexList = QList<QPersistentModelIndex>;
83
89
91
93 { return static_cast<QPlatformFileDialogHelper *>(platformHelper()); }
94
95 void createToolButtons();
96 void createMenuActions();
97 void createWidgets();
98
99 void init(const QFileDialogArgs &args);
100 bool itemViewKeyboardEvent(QKeyEvent *event);
101 QString getEnvironmentVariable(const QString &string);
102 QStringList typedFiles() const;
103 QList<QUrl> userSelectedFiles() const;
104 QStringList addDefaultSuffixToFiles(const QStringList &filesToFix) const;
105 QList<QUrl> addDefaultSuffixToUrls(const QList<QUrl> &urlsToFix) const;
106 bool removeDirectory(const QString &path);
107 void setLabelTextControl(QFileDialog::DialogLabel label, const QString &text);
108 inline void updateLookInLabel();
109 inline void updateFileNameLabel();
110 inline void updateFileTypeLabel();
111 void updateOkButtonText(bool saveAsOnFolder = false);
112 void updateCancelButtonText();
113
114 inline QModelIndex mapToSource(const QModelIndex &index) const;
115 inline QModelIndex mapFromSource(const QModelIndex &index) const;
116 inline QModelIndex rootIndex() const;
117 inline void setRootIndex(const QModelIndex &index) const;
118 inline QModelIndex select(const QModelIndex &index) const;
119 inline QString rootPath() const;
120
121 QLineEdit *lineEdit() const;
122
123 static long maxNameLength(const QString &path);
124
126 {
127 const qsizetype separator = QDir::toNativeSeparators(path).lastIndexOf(QDir::separator());
128 if (separator != -1)
129 return path.mid(separator + 1);
130 return path;
131 }
132
133 QDir::Filters filterForMode(QDir::Filters filters) const
134 {
136 if (q_func()->testOption(QFileDialog::ShowDirsOnly))
137 filters &= ~QDir::Files;
138 return filters;
139 }
140
141 QAbstractItemView *currentView() const;
142
143 static inline QString toInternal(const QString &path)
144 {
145#if defined(Q_OS_WIN)
146 QString n(path);
147 n.replace(u'\\', u'/');
148 return n;
149#else // the compile should optimize away this
150 return path;
151#endif
152 }
153
154#if QT_CONFIG(settings)
155 void saveSettings();
156 bool restoreFromSettings();
157#endif
158
159 bool restoreWidgetState(QStringList &history, int splitterPosition);
160 static void setLastVisitedDirectory(const QUrl &dir);
161 void retranslateWindowTitle();
162 void retranslateStrings();
163 void emitFilesSelected(const QStringList &files);
164 void saveHistorySelection();
165
166 void goHome();
167 void pathChanged(const QString &);
168 void navigate(HistoryItem &);
169 void navigateBackward();
170 void navigateForward();
171 void navigateToParent();
172 void createDirectory();
173 void showListView();
174 void showDetailsView();
175 void showContextMenu(const QPoint &position);
176 void renameCurrent();
177 void deleteCurrent();
178 void showHidden();
179 void showHeader(QAction *);
180 void updateOkButton();
181 void currentChanged(const QModelIndex &index);
182 void enterDirectory(const QModelIndex &index);
183 void emitUrlSelected(const QUrl &file);
184 void emitUrlsSelected(const QList<QUrl> &files);
185 void nativeCurrentChanged(const QUrl &file);
186 void nativeEnterDirectory(const QUrl &directory);
187 void goToDirectory(const QString &);
188 void useNameFilter(int index);
189 void selectionChanged();
190 void goToUrl(const QUrl &url);
191 void autoCompleteFileName(const QString &);
192 void rowsInserted(const QModelIndex & parent);
193 void fileRenamed(const QString &path, const QString &oldName, const QString &newName);
194
195 // layout
196#if QT_CONFIG(proxymodel)
197 QAbstractProxyModel *proxyModel;
198#endif
199
200 // data
203
204#if QT_CONFIG(fscompleter)
206#endif //QT_CONFIG(fscompleter)
207
209
210 QList<HistoryItem> currentHistory;
212
217
219
220 // setVisible_sys returns true if it ends up showing a native
221 // dialog. Returning false means that a non-native dialog must be
222 // used instead.
223 bool canBeNativeDialog() const override;
224 void setVisible(bool visible) override;
225 inline bool usingWidgets() const;
226
227 inline void setDirectory_sys(const QUrl &directory);
228 inline QUrl directory_sys() const;
229 inline void selectFile_sys(const QUrl &filename);
230 inline QList<QUrl> selectedFiles_sys() const;
231 inline void setFilter_sys();
232 inline void selectMimeTypeFilter_sys(const QString &filter);
233 inline QString selectedMimeTypeFilter_sys() const;
234 inline void selectNameFilter_sys(const QString &filter);
235 inline QString selectedNameFilter_sys() const;
237
238 QScopedPointer<Ui_QFileDialog> qFileDialogUi;
239
241
245
246 QSharedPointer<QFileDialogOptions> options;
247
248 // Memory of what was read from QSettings in restoreState() in case widgets are not used
251 QList<QUrl> sidebarUrls;
253
255
256private:
257 virtual void initHelper(QPlatformDialogHelper *) override;
258 virtual void helperPrepareShow(QPlatformDialogHelper *) override;
259 virtual void helperDone(QDialog::DialogCode, QPlatformDialogHelper *) override;
260
261 void itemNotFound(const QString &fileName, QFileDialog::FileMode mode);
262 bool itemAlreadyExists(const QString &fileName);
263 Q_DISABLE_COPY_MOVE(QFileDialogPrivate)
264};
265
267{
268public:
270 void setFileDialogPrivate(QFileDialogPrivate *d_pointer) {d_ptr = d_pointer; }
271 void keyPressEvent(QKeyEvent *e) override;
273private:
274 QFileDialogPrivate *d_ptr;
275};
276
278{
279public:
281 QComboBox(parent), urlModel(nullptr), d_ptr(nullptr) {}
283 void showPopup() override;
284 void setHistory(const QStringList &paths);
285 QStringList history() const { return m_history; }
286 void paintEvent(QPaintEvent *) override;
287
288private:
289 QUrlModel *urlModel;
290 QFileDialogPrivate *d_ptr;
291 QStringList m_history;
292};
293
295{
296public:
297 QFileDialogListView(QWidget *parent = nullptr) : QListView(parent), d_ptr(nullptr) {}
299 QSize sizeHint() const override;
300protected:
301 void keyPressEvent(QKeyEvent *e) override;
302private:
303 QFileDialogPrivate *d_ptr;
304};
305
307{
308public:
309 QFileDialogTreeView(QWidget *parent) : QTreeView(parent), d_ptr(nullptr) {}
311 QSize sizeHint() const override;
312
313protected:
314 void keyPressEvent(QKeyEvent *e) override;
315private:
316 QFileDialogPrivate *d_ptr;
317};
318
320#if QT_CONFIG(proxymodel)
321 return proxyModel ? proxyModel->mapToSource(index) : index;
322#else
323 return index;
324#endif
325}
327#if QT_CONFIG(proxymodel)
328 return proxyModel ? proxyModel->mapFromSource(index) : index;
329#else
330 return index;
331#endif
332}
333
335{
336 return (model ? model->rootPath() : QStringLiteral("/"));
337}
338
340{
342
343 if (!helper)
344 return;
345
346 if (helper->isSupportedUrl(directory))
347 helper->setDirectory(directory);
348}
349
351{
353 return helper->directory();
354 return QUrl();
355}
356
358{
360
361 if (!helper)
362 return;
363
364 if (helper->isSupportedUrl(filename))
365 helper->selectFile(filename);
366}
367
369{
371 return helper->selectedFiles();
372 return QList<QUrl>();
373}
374
376{
378 helper->setFilter();
379}
380
382{
384 helper->selectMimeTypeFilter(filter);
385}
386
388{
390 return helper->selectedMimeTypeFilter();
391
392 return QString();
393}
394
396{
398 helper->selectNameFilter(filter);
399}
400
402{
404 return helper->selectedNameFilter();
405 return QString();
406}
407
409
410#endif // QFILEDIALOG_P_H
The QAbstractItemView class provides the basic functionality for item view classes.
The QAbstractProxyModel class provides a base class for proxy item models that can do sorting,...
The QAction class provides an abstraction for user commands that can be added to different user inter...
Definition qaction.h:30
\inmodule QtCore
Definition qbytearray.h:57
The QComboBox widget combines a button with a dropdown list.
Definition qcombobox.h:24
The QCompleter class provides completions based on an item model.
Definition qcompleter.h:24
DialogCode
The value returned by a modal dialog.
Definition qdialog.h:30
\inmodule QtCore
Definition qdir.h:20
static QChar separator()
Returns the native directory separator: "/" under Unix and "\\" under Windows.
Definition qdir.h:209
static QString toNativeSeparators(const QString &pathName)
Definition qdir.cpp:929
@ Files
Definition qdir.h:23
@ Drives
Definition qdir.h:24
@ AllDirs
Definition qdir.h:40
@ Dirs
Definition qdir.h:22
QCompleter that can deal with QFileSystemModel.
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
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" ...
void setFileDialogPrivate(QFileDialogPrivate *d_pointer)
QFileDialogLineEdit(QWidget *parent=nullptr)
void setFileDialogPrivate(QFileDialogPrivate *d_pointer)
QSize sizeHint() const override
QFileDialogListView(QWidget *parent=nullptr)
void keyPressEvent(QKeyEvent *e) override
QModelIndex mapFromSource(const QModelIndex &index) const
QDir::Filters filterForMode(QDir::Filters filters) const
QString selectedMimeTypeFilter_sys() const
QPointer< QObject > receiverToDisconnectOnClose
QByteArray signalToDisconnectOnClose
void selectNameFilter_sys(const QString &filter)
QScopedPointer< Ui_QFileDialog > qFileDialogUi
QFileSystemModel * model
QAction * showHiddenAction
QList< QUrl > sidebarUrls
static QString toInternal(const QString &path)
QByteArray memberToDisconnectOnClose
QString basename(const QString &path) const
void selectMimeTypeFilter_sys(const QString &filter)
QList< QUrl > selectedFiles_sys() const
QUrl directory_sys() const
QFileIconProvider defaultIconProvider
QAction * newFolderAction
QList< HistoryItem > currentHistory
QString rootPath() const
QModelIndex mapToSource(const QModelIndex &index) const
QPlatformFileDialogHelper * platformFileDialogHelper() const
void setDirectory_sys(const QUrl &directory)
void selectFile_sys(const QUrl &filename)
QSharedPointer< QFileDialogOptions > options
QByteArray splitterState
QString selectedNameFilter_sys() const
void keyPressEvent(QKeyEvent *e) override
QSize sizeHint() const override
QFileDialogTreeView(QWidget *parent)
void setFileDialogPrivate(QFileDialogPrivate *d_pointer)
The QFileDialog class provides a dialog that allows users to select files or directories.
Definition qfiledialog.h:28
FileMode
This enum is used to indicate what the user may select in the file dialog; that is,...
Definition qfiledialog.h:40
DialogLabel
\value LookIn \value FileName \value FileType \value Accept \value Reject
Definition qfiledialog.h:44
\inmodule QtWidgets
The QFileSystemModel class provides a data model for the local filesystem.
QString rootPath() const
The currently set root path.
The QGridLayout class lays out widgets in a grid.
Definition qgridlayout.h:21
The QHBoxLayout class lines up widgets horizontally.
Definition qboxlayout.h:78
The QKeyEvent class describes a key event.
Definition qevent.h:424
The QLineEdit widget is a one-line text editor.
Definition qlineedit.h:28
The QListView class provides a list or icon view onto a model.
Definition qlistview.h:17
\inmodule QtCore
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
The QPaintEvent class contains event parameters for paint events.
Definition qevent.h:486
The QPlatformDialogHelper class allows for platform-specific customization of dialogs.
The QPlatformFileDialogHelper class allows for platform-specific customization of file dialogs.
virtual bool isSupportedUrl(const QUrl &url) const
virtual void setDirectory(const QUrl &directory)=0
virtual void selectFile(const QUrl &filename)=0
\inmodule QtCore\reentrant
Definition qpoint.h:25
\inmodule QtCore
Definition qsize.h:25
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
The QTreeView class provides a default model/view implementation of a tree view.
Definition qtreeview.h:20
\inmodule QtCore
Definition qurl.h:94
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
QString text
Combined button and popup list for selecting options.
GLenum mode
GLuint index
[2]
GLsizei const GLuint * paths
GLuint GLsizei const GLchar * label
[43]
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
GLfloat n
struct _cl_event * event
GLsizei const GLchar *const * path
static qreal position(const QQuickItem *item, QQuickAnchors::Anchor anchorLine)
#define QStringLiteral(str)
#define QT_REQUIRE_CONFIG(feature)
static QT_BEGIN_NAMESPACE void init(QTextBoundaryFinder::BoundaryType type, QStringView str, QCharAttributes *attributes)
ptrdiff_t qsizetype
Definition qtypes.h:165
QFile file
[0]
QUrl url("example.com")
[constructor-url-reference]
QObject::connect nullptr
QLineEdit * lineEdit
QString dir
[11]
QStringList files
[8]
const QStringList filters({"Image files (*.png *.xpm *.jpg)", "Text files (*.txt)", "Any files (*)" })
[6]
selection select(topLeft, bottomRight)
QCompleter * completer
[0]
QJSValueList args
QFileDialog::Options options
QFileDialogArgs(const QUrl &url={})
PersistentModelIndexList selection
bool createDirectory(const QString &directory, QString *errorMessage, bool dryRun)
Definition utils.cpp:64