1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtWidgets module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QFILESYSTEMMODEL_H
41#define QFILESYSTEMMODEL_H
42
43#include <QtGui/qtguiglobal.h>
44#include <QtCore/qabstractitemmodel.h>
45#include <QtCore/qpair.h>
46#include <QtCore/qdir.h>
47#include <QtGui/qicon.h>
48#include <QtCore/qdiriterator.h>
49
50QT_REQUIRE_CONFIG(filesystemmodel);
51
52QT_BEGIN_NAMESPACE
53
54class ExtendedInformation;
55class QFileSystemModelPrivate;
56class QAbstractFileIconProvider;
57
58class Q_GUI_EXPORT QFileSystemModel : public QAbstractItemModel
59{
60 Q_OBJECT
61 Q_PROPERTY(bool resolveSymlinks READ resolveSymlinks WRITE setResolveSymlinks)
62 Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly)
63 Q_PROPERTY(bool nameFilterDisables READ nameFilterDisables WRITE setNameFilterDisables)
64 Q_PROPERTY(Options options READ options WRITE setOptions)
65
66Q_SIGNALS:
67 void rootPathChanged(const QString &newPath);
68 void fileRenamed(const QString &path, const QString &oldName, const QString &newName);
69 void directoryLoaded(const QString &path);
70
71public:
72 enum Roles {
73 FileIconRole = Qt::DecorationRole,
74 FilePathRole = Qt::UserRole + 1,
75 FileNameRole = Qt::UserRole + 2,
76 FilePermissions = Qt::UserRole + 3
77 };
78
79 enum Option
80 {
81 DontWatchForChanges = 0x00000001,
82 DontResolveSymlinks = 0x00000002,
83 DontUseCustomDirectoryIcons = 0x00000004
84 };
85 Q_ENUM(Option)
86 Q_DECLARE_FLAGS(Options, Option)
87
88 explicit QFileSystemModel(QObject *parent = nullptr);
89 ~QFileSystemModel();
90
91 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
92 QModelIndex index(const QString &path, int column = 0) const;
93 QModelIndex parent(const QModelIndex &child) const override;
94 using QObject::parent;
95 QModelIndex sibling(int row, int column, const QModelIndex &idx) const override;
96 bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
97 bool canFetchMore(const QModelIndex &parent) const override;
98 void fetchMore(const QModelIndex &parent) override;
99
100 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
101 int columnCount(const QModelIndex &parent = QModelIndex()) const override;
102
103 QVariant myComputer(int role = Qt::DisplayRole) const;
104 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
105 bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
106
107 QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
108
109 Qt::ItemFlags flags(const QModelIndex &index) const override;
110
111 void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
112
113 QStringList mimeTypes() const override;
114 QMimeData *mimeData(const QModelIndexList &indexes) const override;
115 bool dropMimeData(const QMimeData *data, Qt::DropAction action,
116 int row, int column, const QModelIndex &parent) override;
117 Qt::DropActions supportedDropActions() const override;
118 QHash<int, QByteArray> roleNames() const override;
119
120 // QFileSystemModel specific API
121 QModelIndex setRootPath(const QString &path);
122 QString rootPath() const;
123 QDir rootDirectory() const;
124
125 void setIconProvider(QAbstractFileIconProvider *provider);
126 QAbstractFileIconProvider *iconProvider() const;
127
128 void setFilter(QDir::Filters filters);
129 QDir::Filters filter() const;
130
131 void setResolveSymlinks(bool enable);
132 bool resolveSymlinks() const;
133
134 void setReadOnly(bool enable);
135 bool isReadOnly() const;
136
137 void setNameFilterDisables(bool enable);
138 bool nameFilterDisables() const;
139
140 void setNameFilters(const QStringList &filters);
141 QStringList nameFilters() const;
142
143 void setOption(Option option, bool on = true);
144 bool testOption(Option option) const;
145 void setOptions(Options options);
146 Options options() const;
147
148 QString filePath(const QModelIndex &index) const;
149 bool isDir(const QModelIndex &index) const;
150 qint64 size(const QModelIndex &index) const;
151 QString type(const QModelIndex &index) const;
152 QDateTime lastModified(const QModelIndex &index) const;
153
154 QModelIndex mkdir(const QModelIndex &parent, const QString &name);
155 bool rmdir(const QModelIndex &index);
156 inline QString fileName(const QModelIndex &index) const;
157 inline QIcon fileIcon(const QModelIndex &index) const;
158 QFile::Permissions permissions(const QModelIndex &index) const;
159 QFileInfo fileInfo(const QModelIndex &index) const;
160 bool remove(const QModelIndex &index);
161
162protected:
163 QFileSystemModel(QFileSystemModelPrivate &, QObject *parent = nullptr);
164 void timerEvent(QTimerEvent *event) override;
165 bool event(QEvent *event) override;
166
167private:
168 Q_DECLARE_PRIVATE(QFileSystemModel)
169 Q_DISABLE_COPY(QFileSystemModel)
170
171 Q_PRIVATE_SLOT(d_func(), void _q_directoryChanged(const QString &directory, const QStringList &list))
172 Q_PRIVATE_SLOT(d_func(), void _q_performDelayedSort())
173 Q_PRIVATE_SLOT(d_func(),
174 void _q_fileSystemChanged(const QString &path,
175 const QList<QPair<QString, QFileInfo>> &))
176 Q_PRIVATE_SLOT(d_func(), void _q_resolvedName(const QString &fileName, const QString &resolvedName))
177
178 friend class QFileDialogPrivate;
179};
180
181inline QString QFileSystemModel::fileName(const QModelIndex &aindex) const
182{ return aindex.data(Qt::DisplayRole).toString(); }
183inline QIcon QFileSystemModel::fileIcon(const QModelIndex &aindex) const
184{ return qvariant_cast<QIcon>(aindex.data(Qt::DecorationRole)); }
185
186Q_DECLARE_OPERATORS_FOR_FLAGS(QFileSystemModel::Options)
187
188QT_END_NAMESPACE
189
190#endif // QFILESYSTEMMODEL_H
191