Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qfilesystemmodel_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 QFILESYSTEMMODEL_P_H
5#define QFILESYSTEMMODEL_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 <QtGui/private/qtguiglobal_p.h>
19#include "qfilesystemmodel.h"
20
21#include <private/qabstractitemmodel_p.h>
22#include <qabstractitemmodel.h>
23#include "qfileinfogatherer_p.h"
24#include <qpair.h>
25#include <qdir.h>
26#include <qicon.h>
27#include <qfileinfo.h>
28#include <qtimer.h>
29#include <qhash.h>
30
31#include <vector>
32
33QT_REQUIRE_CONFIG(filesystemmodel);
34
36
37class ExtendedInformation;
40
41#if defined(Q_OS_WIN)
43{
44public:
49};
50
52
53inline size_t qHash(const QFileSystemModelNodePathKey &key, size_t seed = 0)
54{
55 return qHash(key.toCaseFolded(), seed);
56}
57#else // Q_OS_WIN
59#endif
60
62{
63 Q_DECLARE_PUBLIC(QFileSystemModel)
64
65public:
66 enum {
71 NumColumns = 4
72 };
73
75 {
76 public:
77 Q_DISABLE_COPY_MOVE(QFileSystemNode)
78
79 explicit QFileSystemNode(const QString &filename = QString(), QFileSystemNode *p = nullptr)
80 : fileName(filename), parent(p) {}
82 qDeleteAll(children);
83 delete info;
84 }
85
87#if defined(Q_OS_WIN)
88 QString volumeName;
89#endif
90
91 inline qint64 size() const { if (info && !info->isDir()) return info->size(); return 0; }
92 inline QString type() const { if (info) return info->displayType; return QLatin1StringView(""); }
93 inline QDateTime lastModified(const QTimeZone &tz) const { return info ? info->lastModified(tz) : QDateTime(); }
94 inline QFile::Permissions permissions() const { if (info) return info->permissions(); return { }; }
95 inline bool isReadable() const { return ((permissions() & QFile::ReadUser) != 0); }
96 inline bool isWritable() const { return ((permissions() & QFile::WriteUser) != 0); }
97 inline bool isExecutable() const { return ((permissions() & QFile::ExeUser) != 0); }
98 inline bool isDir() const {
99 if (info)
100 return info->isDir();
101 if (children.size() > 0)
102 return true;
103 return false;
104 }
105 inline QFileInfo fileInfo() const { if (info) return info->fileInfo(); return QFileInfo(); }
106 inline bool isFile() const { if (info) return info->isFile(); return true; }
107 inline bool isSystem() const { if (info) return info->isSystem(); return true; }
108 inline bool isHidden() const { if (info) return info->isHidden(); return false; }
109 inline bool isSymLink(bool ignoreNtfsSymLinks = false) const { return info && info->isSymLink(ignoreNtfsSymLinks); }
110 inline bool caseSensitive() const { if (info) return info->isCaseSensitive(); return false; }
111 inline QIcon icon() const { if (info) return info->icon; return QIcon(); }
112
113 inline bool operator <(const QFileSystemNode &node) const {
114 if (caseSensitive() || node.caseSensitive())
115 return fileName < node.fileName;
117 }
118 inline bool operator >(const QString &name) const {
119 if (caseSensitive())
120 return fileName > name;
122 }
123 inline bool operator <(const QString &name) const {
124 if (caseSensitive())
125 return fileName < name;
127 }
128 inline bool operator !=(const QExtendedInformation &fileInfo) const {
129 return !operator==(fileInfo);
130 }
131 bool operator ==(const QString &name) const {
132 if (caseSensitive())
133 return fileName == name;
135 }
136 bool operator ==(const QExtendedInformation &fileInfo) const {
137 return info && (*info == fileInfo);
138 }
139
140 inline bool hasInformation() const { return info != nullptr; }
141
142 void populate(const QExtendedInformation &fileInfo) {
143 if (!info)
144 info = new QExtendedInformation(fileInfo.fileInfo());
145 (*info) = fileInfo;
146 }
147
148 // children shouldn't normally be accessed directly, use node()
149 inline int visibleLocation(const QString &childName) {
150 return visibleChildren.indexOf(childName);
151 }
152 void updateIcon(QAbstractFileIconProvider *iconProvider, const QString &path) {
153 if (!iconProvider)
154 return;
155
156 if (info)
157 info->icon = iconProvider->icon(QFileInfo(path));
158
159 for (QFileSystemNode *child : std::as_const(children)) {
160 //On windows the root (My computer) has no path so we don't want to add a / for nothing (e.g. /C:/)
161 if (!path.isEmpty()) {
162 if (path.endsWith(u'/'))
163 child->updateIcon(iconProvider, path + child->fileName);
164 else
165 child->updateIcon(iconProvider, path + u'/' + child->fileName);
166 } else
167 child->updateIcon(iconProvider, child->fileName);
168 }
169 }
170
172 if (!iconProvider)
173 return;
174
175 if (info)
176 info->displayType = iconProvider->type(QFileInfo(path));
177 for (QFileSystemNode *child : std::as_const(children)) {
178 //On windows the root (My computer) has no path so we don't want to add a / for nothing (e.g. /C:/)
179 if (!path.isEmpty()) {
180 if (path.endsWith(u'/'))
181 child->retranslateStrings(iconProvider, path + child->fileName);
182 else
183 child->retranslateStrings(iconProvider, path + u'/' + child->fileName);
184 } else
185 child->retranslateStrings(iconProvider, child->fileName);
186 }
187 }
188
189 QHash<QFileSystemModelNodePathKey, QFileSystemNode *> children;
190 QList<QString> visibleChildren;
193 int dirtyChildrenIndex = -1;
194 bool populatedChildren = false;
195 bool isVisible = false;
196 };
197
200 void init();
201 /*
202 \internal
203
204 Return true if index which is owned by node is hidden by the filter.
205 */
206 inline bool isHiddenByFilter(QFileSystemNode *indexNode, const QModelIndex &index) const
207 {
208 return (indexNode != &root && !index.isValid());
209 }
210 QFileSystemNode *node(const QModelIndex &index) const;
211 QFileSystemNode *node(const QString &path, bool fetch = true) const;
212 inline QModelIndex index(const QString &path, int column = 0) { return index(node(path), column); }
213 QModelIndex index(const QFileSystemNode *node, int column = 0) const;
214 bool filtersAcceptsNode(const QFileSystemNode *node) const;
215 bool passNameFilters(const QFileSystemNode *node) const;
216 void removeNode(QFileSystemNode *parentNode, const QString &name);
217 QFileSystemNode* addNode(QFileSystemNode *parentNode, const QString &fileName, const QFileInfo &info);
218 void addVisibleFiles(QFileSystemNode *parentNode, const QStringList &newFiles);
219 void removeVisibleFile(QFileSystemNode *parentNode, int visibleLocation);
220 void sortChildren(int column, const QModelIndex &parent);
221
222 inline int translateVisibleLocation(QFileSystemNode *parent, int row) const {
223 if (sortOrder != Qt::AscendingOrder) {
224 if (parent->dirtyChildrenIndex == -1)
225 return parent->visibleChildren.size() - row - 1;
226
227 if (row < parent->dirtyChildrenIndex)
228 return parent->dirtyChildrenIndex - row - 1;
229 }
230
231 return row;
232 }
233
234 inline static QString myComputer() {
235 // ### TODO We should query the system to find out what the string should be
236 // XP == "My Computer",
237 // Vista == "Computer",
238 // OS X == "Computer" (sometime user generated) "Benjamin's PowerBook G4"
239#ifdef Q_OS_WIN
240 return QFileSystemModel::tr("My Computer");
241#else
242 return QFileSystemModel::tr("Computer");
243#endif
244 }
245
246 inline void delayedSort() {
247 if (!delayedSortTimer.isActive())
248 delayedSortTimer.start(0);
249 }
250
251 QIcon icon(const QModelIndex &index) const;
252 QString name(const QModelIndex &index) const;
253 QString displayName(const QModelIndex &index) const;
254 QString filePath(const QModelIndex &index) const;
255 QString size(const QModelIndex &index) const;
256 static QString size(qint64 bytes);
257 QString type(const QModelIndex &index) const;
258 QString time(const QModelIndex &index) const;
259
260 void directoryChanged(const QString &directory, const QStringList &list);
261 void performDelayedSort();
262 void fileSystemChanged(const QString &path, const QList<std::pair<QString, QFileInfo>> &);
263 void resolvedName(const QString &fileName, const QString &resolvedName);
264
266#if QT_CONFIG(filesystemwatcher)
267# ifdef Q_OS_WIN
268 QStringList unwatchPathsAt(const QModelIndex &);
269 void watchPaths(const QStringList &paths) { fileInfoGatherer->watchPaths(paths); }
270# endif // Q_OS_WIN
271 std::unique_ptr<QFileInfoGatherer> fileInfoGatherer;
272#endif // filesystemwatcher
274 QHash<const QFileSystemNode*, bool> bypassFilters;
275#if QT_CONFIG(regularexpression)
276 QStringList nameFilters;
277 std::vector<QRegularExpression> nameFiltersRegexps;
278 void rebuildNameFilterRegexps();
279#endif
280 QHash<QString, QString> resolvedSymLinks;
281
283
289 QList<Fetching> toFetch;
290
292
294 int sortColumn = 0;
296 bool forceSort = true;
297 bool readOnly = true;
298 bool setRootPath = false;
299 bool nameFilterDisables = true; // false on windows, true on mac and unix
300 // This flag is an optimization for QFileDialog. It enables a sort which is
301 // not recursive, meaning we sort only what we see.
302 bool disableRecursiveSort = false;
303};
305
307
308#endif
virtual QIcon icon(IconType) const
Returns an icon set for the given type, using the current icon theme.
virtual QString type(const QFileInfo &) const
Returns the type of the file described by info.
\inmodule QtCore
Definition qbasictimer.h:18
\inmodule QtCore\reentrant
Definition qdatetime.h:283
\inmodule QtCore
Definition qdir.h:20
@ AllEntries
Definition qdir.h:26
@ AllDirs
Definition qdir.h:40
@ NoDotAndDotDot
Definition qdir.h:44
QFileInfo fileInfo() const
\inmodule QtWidgets
void populate(const QExtendedInformation &fileInfo)
void updateIcon(QAbstractFileIconProvider *iconProvider, const QString &path)
QDateTime lastModified(const QTimeZone &tz) const
QHash< QFileSystemModelNodePathKey, QFileSystemNode * > children
bool isSymLink(bool ignoreNtfsSymLinks=false) const
int visibleLocation(const QString &childName)
void retranslateStrings(QAbstractFileIconProvider *iconProvider, const QString &path)
QHash< const QFileSystemNode *, bool > bypassFilters
QHash< QString, QString > resolvedSymLinks
QModelIndex index(const QString &path, int column=0)
bool isHiddenByFilter(QFileSystemNode *indexNode, const QModelIndex &index) const
int translateVisibleLocation(QFileSystemNode *parent, int row) const
The QFileSystemModel class provides a data model for the local filesystem.
The QIcon class provides scalable icons in different modes and states.
Definition qicon.h:20
Definition qlist.h:75
qsizetype size() const noexcept
Definition qlist.h:397
\inmodule QtCore
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
int compare(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
Definition qstring.cpp:6664
\inmodule QtCore
Definition qtimezone.h:26
\inmodule QtCore
Definition qtimer.h:20
qDeleteAll(list.begin(), list.end())
Combined button and popup list for selecting options.
SortOrder
Definition qnamespace.h:121
@ AscendingOrder
Definition qnamespace.h:122
@ CaseInsensitive
static QString displayName(CGDirectDisplayID displayID)
constexpr bool operator!=(const timespec &t1, const timespec &t2)
QString QFileSystemModelNodePathKey
size_t qHash(const QFileSystemWatcherPathKey &key, size_t seed=0)
GLuint64 key
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint index
[2]
GLenum type
GLsizei const GLuint * paths
GLuint name
GLenum GLenum GLsizei void GLsizei void * column
GLbyte GLbyte tz
GLsizei const GLchar *const * path
GLenum GLenum GLsizei void * row
GLfloat GLfloat p
[1]
bool operator>(const QPoint &a, const QPoint &b)
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
Definition qrandom.cpp:1220
static bool operator<(const QSettingsIniKey &k1, const QSettingsIniKey &k2)
#define QT_REQUIRE_CONFIG(feature)
static QT_BEGIN_NAMESPACE void init(QTextBoundaryFinder::BoundaryType type, QStringView str, QCharAttributes *attributes)
static int compare(quint64 a, quint64 b)
@ Q_RELOCATABLE_TYPE
Definition qtypeinfo.h:158
#define Q_DECLARE_TYPEINFO(TYPE, FLAGS)
Definition qtypeinfo.h:180
long long qint64
Definition qtypes.h:60
#define explicit
QList< int > list
[14]
QSharedPointer< T > other(t)
[5]
const QStringList filters({"Image files (*.png *.xpm *.jpg)", "Text files (*.txt)", "Any files (*)" })
[6]
edit isVisible()
QLayoutItem * child
[0]
QHostInfo info
[0]