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 <QtWidgets/qtwidgetsglobal.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 
50 QT_REQUIRE_CONFIG(filesystemmodel);
51 
52 QT_BEGIN_NAMESPACE
53 
54 class ExtendedInformation;
55 class QFileSystemModelPrivate;
56 class QFileIconProvider;
57 
58 class Q_WIDGETS_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 
66 Q_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 
71 public:
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 
119     // QFileSystemModel specific API
120     QModelIndex setRootPath(const QString &path);
121     QString rootPath() const;
122     QDir rootDirectory() const;
123 
124     void setIconProvider(QFileIconProvider *provider);
125     QFileIconProvider *iconProvider() const;
126 
127     void setFilter(QDir::Filters filters);
128     QDir::Filters filter() const;
129 
130     void setResolveSymlinks(bool enable);
131     bool resolveSymlinks() const;
132 
133     void setReadOnly(bool enable);
134     bool isReadOnly() const;
135 
136     void setNameFilterDisables(bool enable);
137     bool nameFilterDisables() const;
138 
139     void setNameFilters(const QStringList &filters);
140     QStringList nameFilters() const;
141 
142     void setOption(Option option, bool on = true);
143     bool testOption(Option option) const;
144     void setOptions(Options options);
145     Options options() const;
146 
147     QString filePath(const QModelIndex &index) const;
148     bool isDir(const QModelIndex &index) const;
149     qint64 size(const QModelIndex &index) const;
150     QString type(const QModelIndex &index) const;
151     QDateTime lastModified(const QModelIndex &index) const;
152 
153     QModelIndex mkdir(const QModelIndex &parent, const QString &name);
154     bool rmdir(const QModelIndex &index);
155     inline QString fileName(const QModelIndex &index) const;
156     inline QIcon fileIcon(const QModelIndex &index) const;
157     QFile::Permissions permissions(const QModelIndex &index) const;
158     QFileInfo fileInfo(const QModelIndex &index) const;
159     bool remove(const QModelIndex &index);
160 
161 protected:
162     QFileSystemModel(QFileSystemModelPrivate &, QObject *parent = nullptr);
163     void timerEvent(QTimerEvent *event) override;
164     bool event(QEvent *event) override;
165 
166 private:
167     Q_DECLARE_PRIVATE(QFileSystemModel)
168     Q_DISABLE_COPY(QFileSystemModel)
169 
170     Q_PRIVATE_SLOT(d_func(), void _q_directoryChanged(const QString &directory, const QStringList &list))
171     Q_PRIVATE_SLOT(d_func(), void _q_performDelayedSort())
172     Q_PRIVATE_SLOT(d_func(), void _q_fileSystemChanged(const QString &path, const QVector<QPair<QString, QFileInfo> > &))
173     Q_PRIVATE_SLOT(d_func(), void _q_resolvedName(const QString &fileName, const QString &resolvedName))
174 
175     friend class QFileDialogPrivate;
176 };
177 
fileName(const QModelIndex & aindex)178 inline QString QFileSystemModel::fileName(const QModelIndex &aindex) const
179 { return aindex.data(Qt::DisplayRole).toString(); }
fileIcon(const QModelIndex & aindex)180 inline QIcon QFileSystemModel::fileIcon(const QModelIndex &aindex) const
181 { return qvariant_cast<QIcon>(aindex.data(Qt::DecorationRole)); }
182 
183 Q_DECLARE_OPERATORS_FOR_FLAGS(QFileSystemModel::Options)
184 
185 QT_END_NAMESPACE
186 
187 #endif // QFILESYSTEMMODEL_H
188