Qt 6.x
The Qt SDK
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 (info)
154 info->icon = iconProvider->icon(QFileInfo(path));
155 for (QFileSystemNode *child : std::as_const(children)) {
156 //On windows the root (My computer) has no path so we don't want to add a / for nothing (e.g. /C:/)
157 if (!path.isEmpty()) {
158 if (path.endsWith(u'/'))
159 child->updateIcon(iconProvider, path + child->fileName);
160 else
161 child->updateIcon(iconProvider, path + u'/' + child->fileName);
162 } else
163 child->updateIcon(iconProvider, child->fileName);
164 }
165 }
166
168 if (info)
169 info->displayType = iconProvider->type(QFileInfo(path));
170 for (QFileSystemNode *child : std::as_const(children)) {
171 //On windows the root (My computer) has no path so we don't want to add a / for nothing (e.g. /C:/)
172 if (!path.isEmpty()) {
173 if (path.endsWith(u'/'))
174 child->retranslateStrings(iconProvider, path + child->fileName);
175 else
176 child->retranslateStrings(iconProvider, path + u'/' + child->fileName);
177 } else
178 child->retranslateStrings(iconProvider, child->fileName);
179 }
180 }
181
186 int dirtyChildrenIndex = -1;
187 bool populatedChildren = false;
188 bool isVisible = false;
189 };
190
193 void init();
194 /*
195 \internal
196
197 Return true if index which is owned by node is hidden by the filter.
198 */
199 inline bool isHiddenByFilter(QFileSystemNode *indexNode, const QModelIndex &index) const
200 {
201 return (indexNode != &root && !index.isValid());
202 }
203 QFileSystemNode *node(const QModelIndex &index) const;
204 QFileSystemNode *node(const QString &path, bool fetch = true) const;
205 inline QModelIndex index(const QString &path, int column = 0) { return index(node(path), column); }
206 QModelIndex index(const QFileSystemNode *node, int column = 0) const;
207 bool filtersAcceptsNode(const QFileSystemNode *node) const;
208 bool passNameFilters(const QFileSystemNode *node) const;
209 void removeNode(QFileSystemNode *parentNode, const QString &name);
210 QFileSystemNode* addNode(QFileSystemNode *parentNode, const QString &fileName, const QFileInfo &info);
211 void addVisibleFiles(QFileSystemNode *parentNode, const QStringList &newFiles);
212 void removeVisibleFile(QFileSystemNode *parentNode, int visibleLocation);
213 void sortChildren(int column, const QModelIndex &parent);
214
216 if (sortOrder != Qt::AscendingOrder) {
217 if (parent->dirtyChildrenIndex == -1)
218 return parent->visibleChildren.size() - row - 1;
219
220 if (row < parent->dirtyChildrenIndex)
221 return parent->dirtyChildrenIndex - row - 1;
222 }
223
224 return row;
225 }
226
227 inline static QString myComputer() {
228 // ### TODO We should query the system to find out what the string should be
229 // XP == "My Computer",
230 // Vista == "Computer",
231 // OS X == "Computer" (sometime user generated) "Benjamin's PowerBook G4"
232#ifdef Q_OS_WIN
233 return QFileSystemModel::tr("My Computer");
234#else
235 return QFileSystemModel::tr("Computer");
236#endif
237 }
238
239 inline void delayedSort() {
240 if (!delayedSortTimer.isActive())
241 delayedSortTimer.start(0);
242 }
243
244 QIcon icon(const QModelIndex &index) const;
245 QString name(const QModelIndex &index) const;
246 QString displayName(const QModelIndex &index) const;
247 QString filePath(const QModelIndex &index) const;
248 QString size(const QModelIndex &index) const;
249 static QString size(qint64 bytes);
250 QString type(const QModelIndex &index) const;
251 QString time(const QModelIndex &index) const;
252
253 void _q_directoryChanged(const QString &directory, const QStringList &list);
254 void _q_performDelayedSort();
255 void _q_fileSystemChanged(const QString &path, const QList<QPair<QString, QFileInfo>> &);
256 void _q_resolvedName(const QString &fileName, const QString &resolvedName);
257
259#if QT_CONFIG(filesystemwatcher)
260# ifdef Q_OS_WIN
261 QStringList unwatchPathsAt(const QModelIndex &);
262 void watchPaths(const QStringList &paths) { fileInfoGatherer.watchPaths(paths); }
263# endif // Q_OS_WIN
264 QFileInfoGatherer fileInfoGatherer;
265#endif // filesystemwatcher
268#if QT_CONFIG(regularexpression)
269 QStringList nameFilters;
270 std::vector<QRegularExpression> nameFiltersRegexps;
271 void rebuildNameFilterRegexps();
272#endif
274
276
277 struct Fetching {
281 };
283
285
287 int sortColumn = 0;
289 bool forceSort = true;
290 bool readOnly = true;
291 bool setRootPath = false;
292 bool nameFilterDisables = true; // false on windows, true on mac and unix
293 // This flag is an optimization for QFileDialog. It enables a sort which is
294 // not recursive, meaning we sort only what we see.
295 bool disableRecursiveSort = false;
296};
298
300
301#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:257
\inmodule QtCore
Definition qdir.h:19
@ AllEntries
Definition qdir.h:25
@ AllDirs
Definition qdir.h:39
@ NoDotAndDotDot
Definition qdir.h:43
QFileInfo fileInfo() const
\inmodule QtWidgets
\inmodule QtCore \reentrant
Definition qfileinfo.h:22
QDateTime lastModified() const
Returns the date and time when the file was last modified.
Definition qfileinfo.h:156
bool isSymLink() const
Returns true if this object points to a symbolic link, shortcut, or alias; otherwise returns false.
bool isFile() const
Returns true if this object points to a file or to a symbolic link to a file.
bool isDir() const
Returns true if this object points to a directory or to a symbolic link to a directory.
qint64 size() const
Returns the file size in bytes.
bool isHidden() const
Returns true if this is a ‘hidden’ file; otherwise returns false.
QFile::Permissions permissions() const
Returns the complete OR-ed together combination of QFile::Permissions for the file.
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.
\inmodule QtCore
Definition qhash.h:818
The QIcon class provides scalable icons in different modes and states.
Definition qicon.h:20
Definition qlist.h:74
\inmodule QtCore
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
int compare(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
Definition qstring.cpp:6498
\inmodule QtCore
Definition qtimezone.h:25
\inmodule QtCore
Definition qtimer.h:20
qDeleteAll(list.begin(), list.end())
Combined button and popup list for selecting options.
SortOrder
Definition qnamespace.h:120
@ AscendingOrder
Definition qnamespace.h:121
@ CaseInsensitive
static QString displayName(CGDirectDisplayID displayID)
std::pair< T1, T2 > QPair
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:1219
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:145
#define Q_DECLARE_TYPEINFO(TYPE, FLAGS)
Definition qtypeinfo.h:163
long long qint64
Definition qtypes.h:55
#define explicit
QList< int > list
[14]
QFileInfo info(fileName)
[8]
QSharedPointer< T > other(t)
[5]
const QStringList filters({"Image files (*.png *.xpm *.jpg)", "Text files (*.txt)", "Any files (*)" })
[6]
edit isVisible()
QLayoutItem * child
[0]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent