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 QFILESYSTEMMODEL_H
5#define QFILESYSTEMMODEL_H
6
7#include <QtGui/qtguiglobal.h>
8#include <QtCore/qabstractitemmodel.h>
9#include <QtCore/qpair.h>
10#include <QtCore/qdir.h>
11#include <QtGui/qicon.h>
12#include <QtCore/qdiriterator.h>
13
14QT_REQUIRE_CONFIG(filesystemmodel);
15
16QT_BEGIN_NAMESPACE
17
18class ExtendedInformation;
19class QFileSystemModelPrivate;
20class QAbstractFileIconProvider;
21
22class Q_GUI_EXPORT QFileSystemModel : public QAbstractItemModel
23{
24 Q_OBJECT
25 Q_PROPERTY(bool resolveSymlinks READ resolveSymlinks WRITE setResolveSymlinks)
26 Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly)
27 Q_PROPERTY(bool nameFilterDisables READ nameFilterDisables WRITE setNameFilterDisables)
28 Q_PROPERTY(Options options READ options WRITE setOptions)
29
30Q_SIGNALS:
31 void rootPathChanged(const QString &newPath);
32 void fileRenamed(const QString &path, const QString &oldName, const QString &newName);
33 void directoryLoaded(const QString &path);
34
35public:
36 enum Roles {
37 FileIconRole = Qt::DecorationRole,
38 FilePathRole = Qt::UserRole + 1,
39 FileNameRole = Qt::UserRole + 2,
40 FilePermissions = Qt::UserRole + 3
41 };
42
43 enum Option
44 {
45 DontWatchForChanges = 0x00000001,
46 DontResolveSymlinks = 0x00000002,
47 DontUseCustomDirectoryIcons = 0x00000004
48 };
49 Q_ENUM(Option)
50 Q_DECLARE_FLAGS(Options, Option)
51
52 explicit QFileSystemModel(QObject *parent = nullptr);
53 ~QFileSystemModel();
54
55 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
56 QModelIndex index(const QString &path, int column = 0) const;
57 QModelIndex parent(const QModelIndex &child) const override;
58 using QObject::parent;
59 QModelIndex sibling(int row, int column, const QModelIndex &idx) const override;
60 bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
61 bool canFetchMore(const QModelIndex &parent) const override;
62 void fetchMore(const QModelIndex &parent) override;
63
64 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
65 int columnCount(const QModelIndex &parent = QModelIndex()) const override;
66
67 QVariant myComputer(int role = Qt::DisplayRole) const;
68 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
69 bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
70
71 QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
72
73 Qt::ItemFlags flags(const QModelIndex &index) const override;
74
75 void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
76
77 QStringList mimeTypes() const override;
78 QMimeData *mimeData(const QModelIndexList &indexes) const override;
79 bool dropMimeData(const QMimeData *data, Qt::DropAction action,
80 int row, int column, const QModelIndex &parent) override;
81 Qt::DropActions supportedDropActions() const override;
82 QHash<int, QByteArray> roleNames() const override;
83
84 // QFileSystemModel specific API
85 QModelIndex setRootPath(const QString &path);
86 QString rootPath() const;
87 QDir rootDirectory() const;
88
89 void setIconProvider(QAbstractFileIconProvider *provider);
90 QAbstractFileIconProvider *iconProvider() const;
91
92 void setFilter(QDir::Filters filters);
93 QDir::Filters filter() const;
94
95 void setResolveSymlinks(bool enable);
96 bool resolveSymlinks() const;
97
98 void setReadOnly(bool enable);
99 bool isReadOnly() const;
100
101 void setNameFilterDisables(bool enable);
102 bool nameFilterDisables() const;
103
104 void setNameFilters(const QStringList &filters);
105 QStringList nameFilters() const;
106
107 void setOption(Option option, bool on = true);
108 bool testOption(Option option) const;
109 void setOptions(Options options);
110 Options options() const;
111
112 QString filePath(const QModelIndex &index) const;
113 bool isDir(const QModelIndex &index) const;
114 qint64 size(const QModelIndex &index) const;
115 QString type(const QModelIndex &index) const;
116
117 QDateTime lastModified(const QModelIndex &index) const;
118 QDateTime lastModified(const QModelIndex &index, const QTimeZone &tz) const;
119
120 QModelIndex mkdir(const QModelIndex &parent, const QString &name);
121 bool rmdir(const QModelIndex &index);
122 inline QString fileName(const QModelIndex &index) const;
123 inline QIcon fileIcon(const QModelIndex &index) const;
124 QFile::Permissions permissions(const QModelIndex &index) const;
125 QFileInfo fileInfo(const QModelIndex &index) const;
126 bool remove(const QModelIndex &index);
127
128protected:
129 QFileSystemModel(QFileSystemModelPrivate &, QObject *parent = nullptr);
130 void timerEvent(QTimerEvent *event) override;
131 bool event(QEvent *event) override;
132
133private:
134 Q_DECLARE_PRIVATE(QFileSystemModel)
135 Q_DISABLE_COPY(QFileSystemModel)
136
137 Q_PRIVATE_SLOT(d_func(), void _q_directoryChanged(const QString &directory, const QStringList &list))
138 Q_PRIVATE_SLOT(d_func(), void _q_performDelayedSort())
139 Q_PRIVATE_SLOT(d_func(),
140 void _q_fileSystemChanged(const QString &path,
141 const QList<QPair<QString, QFileInfo>> &))
142 Q_PRIVATE_SLOT(d_func(), void _q_resolvedName(const QString &fileName, const QString &resolvedName))
143
144 friend class QFileDialogPrivate;
145};
146
147inline QString QFileSystemModel::fileName(const QModelIndex &aindex) const
148{ return aindex.data(arole: Qt::DisplayRole).toString(); }
149inline QIcon QFileSystemModel::fileIcon(const QModelIndex &aindex) const
150{ return qvariant_cast<QIcon>(v: aindex.data(arole: Qt::DecorationRole)); }
151
152Q_DECLARE_OPERATORS_FOR_FLAGS(QFileSystemModel::Options)
153
154QT_END_NAMESPACE
155
156#endif // QFILESYSTEMMODEL_H
157

source code of qtbase/src/gui/itemmodels/qfilesystemmodel.h