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

Provided by KDAB

Privacy Policy
Learn Advanced QML with KDAB
Find out more

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