Qt 6.x
The Qt SDK
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
65{
66 QFileDialogArgs(const QUrl &url = {});
67
68 QWidget *parent = nullptr;
74 QFileDialog::Options options = {};
75};
76
77class Q_WIDGETS_EXPORT QFileDialogPrivate : public QDialogPrivate
78{
79 Q_DECLARE_PUBLIC(QFileDialog)
80
81public:
83
85 {
88 };
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 {
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 _q_goHome();
167 void _q_pathChanged(const QString &);
168 void navigate(HistoryItem &);
169 void _q_navigateBackward();
170 void _q_navigateForward();
171 void _q_navigateToParent();
172 void _q_createDirectory();
173 void _q_showListView();
174 void _q_showDetailsView();
175 void _q_showContextMenu(const QPoint &position);
176 void _q_renameCurrent();
177 void _q_deleteCurrent();
178 void _q_showHidden();
179 void _q_showHeader(QAction *);
180 void _q_updateOkButton();
181 void _q_currentChanged(const QModelIndex &index);
182 void _q_enterDirectory(const QModelIndex &index);
183 void _q_emitUrlSelected(const QUrl &file);
184 void _q_emitUrlsSelected(const QList<QUrl> &files);
185 void _q_nativeCurrentChanged(const QUrl &file);
186 void _q_nativeEnterDirectory(const QUrl &directory);
187 void _q_goToDirectory(const QString &);
188 void _q_useNameFilter(int index);
189 void _q_selectionChanged();
190 void _q_goToUrl(const QUrl &url);
191 void _q_autoCompleteFileName(const QString &);
192 void _q_rowsInserted(const QModelIndex & parent);
193 void _q_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
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 inline bool usingWidgets() const;
225
226 inline void setDirectory_sys(const QUrl &directory);
227 inline QUrl directory_sys() const;
228 inline void selectFile_sys(const QUrl &filename);
229 inline QList<QUrl> selectedFiles_sys() const;
230 inline void setFilter_sys();
231 inline void selectMimeTypeFilter_sys(const QString &filter);
232 inline QString selectedMimeTypeFilter_sys() const;
233 inline void selectNameFilter_sys(const QString &filter);
234 inline QString selectedNameFilter_sys() const;
236
238
240
244
246
247 // Memory of what was read from QSettings in restoreState() in case widgets are not used
252
254
255private:
256 virtual void initHelper(QPlatformDialogHelper *) override;
257 virtual void helperPrepareShow(QPlatformDialogHelper *) override;
258 virtual void helperDone(QDialog::DialogCode, QPlatformDialogHelper *) override;
259
260 void itemNotFound(const QString &fileName, QFileDialog::FileMode mode);
261 bool itemAlreadyExists(const QString &fileName);
262 Q_DISABLE_COPY_MOVE(QFileDialogPrivate)
263};
264
266{
267public:
269 void setFileDialogPrivate(QFileDialogPrivate *d_pointer) {d_ptr = d_pointer; }
270 void keyPressEvent(QKeyEvent *e) override;
272private:
273 QFileDialogPrivate *d_ptr;
274};
275
277{
278public:
280 QComboBox(parent), urlModel(nullptr), d_ptr(nullptr) {}
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:
298 QSize sizeHint() const override;
299protected:
300 void keyPressEvent(QKeyEvent *e) override;
301private:
302 QFileDialogPrivate *d_ptr;
303};
304
306{
307public:
310 QSize sizeHint() const override;
311
312protected:
313 void keyPressEvent(QKeyEvent *e) override;
314private:
315 QFileDialogPrivate *d_ptr;
316};
317
319#if QT_CONFIG(proxymodel)
320 return proxyModel ? proxyModel->mapToSource(index) : index;
321#else
322 return index;
323#endif
324}
326#if QT_CONFIG(proxymodel)
327 return proxyModel ? proxyModel->mapFromSource(index) : index;
328#else
329 return index;
330#endif
331}
332
334{
335 return (model ? model->rootPath() : QStringLiteral("/"));
336}
337
339{
341
342 if (!helper)
343 return;
344
345 if (helper->isSupportedUrl(directory))
346 helper->setDirectory(directory);
347}
348
350{
352 return helper->directory();
353 return QUrl();
354}
355
357{
359
360 if (!helper)
361 return;
362
363 if (helper->isSupportedUrl(filename))
364 helper->selectFile(filename);
365}
366
368{
370 return helper->selectedFiles();
371 return QList<QUrl>();
372}
373
375{
377 helper->setFilter();
378}
379
381{
383 helper->selectMimeTypeFilter(filter);
384}
385
387{
389 return helper->selectedMimeTypeFilter();
390
391 return QString();
392}
393
395{
397 helper->selectNameFilter(filter);
398}
399
401{
403 return helper->selectedNameFilter();
404 return QString();
405}
406
408
409#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 is a combined button and popup list.
Definition qcombobox.h:24
The QCompleter class provides completions based on an item model.
Definition qcompleter.h:24
virtual void initHelper(QPlatformDialogHelper *)
Definition qdialog_p.h:96
virtual void helperDone(QDialog::DialogCode, QPlatformDialogHelper *)
Definition qdialog_p.h:98
QPlatformDialogHelper * platformHelper() const
Definition qdialog.cpp:77
virtual bool canBeNativeDialog() const
Definition qdialog.cpp:99
virtual void helperPrepareShow(QPlatformDialogHelper *)
Definition qdialog_p.h:97
DialogCode
The value returned by a modal dialog.
Definition qdialog.h:30
\inmodule QtCore
Definition qdir.h:19
static QChar separator()
Returns the native directory separator: "/" under Unix and "\\" under Windows.
Definition qdir.h:206
static QString toNativeSeparators(const QString &pathName)
Definition qdir.cpp:929
@ Files
Definition qdir.h:22
@ Drives
Definition qdir.h:23
@ AllDirs
Definition qdir.h:39
@ Dirs
Definition qdir.h:21
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 allow 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; i.e.
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:423
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:311
The QPaintEvent class contains event parameters for paint events.
Definition qevent.h:485
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:23
\inmodule QtCore
Definition qpointer.h:18
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore
Definition qsize.h:25
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
qsizetype lastIndexOf(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
Definition qstring.h:279
The QTreeView class provides a default model/view implementation of a tree view.
Definition qtreeview.h:20
\inmodule QtCore
Definition qurl.h:94
void init(QWidget *desktopWidget, Qt::WindowFlags f)
Definition qwidget.cpp:936
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
QString text
double e
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)
ptrdiff_t qsizetype
Definition qtypes.h:70
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
PersistentModelIndexList selection
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent