Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/itemviews/qdirmodel.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||||||||
2 | ** | - | ||||||||||||||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||||||||
5 | ** | - | ||||||||||||||||||||||||
6 | ** This file is part of the QtWidgets module of the Qt Toolkit. | - | ||||||||||||||||||||||||
7 | ** | - | ||||||||||||||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||||||||
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 http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||||||||
15 | ** information use the contact form at http://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 2.1 or version 3 as published by the Free | - | ||||||||||||||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||||||||
25 | ** | - | ||||||||||||||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||||||||
29 | ** | - | ||||||||||||||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||||||||
31 | ** | - | ||||||||||||||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||||||||||||||
33 | - | |||||||||||||||||||||||||
34 | #include "qdirmodel.h" | - | ||||||||||||||||||||||||
35 | - | |||||||||||||||||||||||||
36 | #ifndef QT_NO_DIRMODEL | - | ||||||||||||||||||||||||
37 | #include <qstack.h> | - | ||||||||||||||||||||||||
38 | #include <qfile.h> | - | ||||||||||||||||||||||||
39 | #include <qfilesystemmodel.h> | - | ||||||||||||||||||||||||
40 | #include <qurl.h> | - | ||||||||||||||||||||||||
41 | #include <qmimedata.h> | - | ||||||||||||||||||||||||
42 | #include <qpair.h> | - | ||||||||||||||||||||||||
43 | #include <qvector.h> | - | ||||||||||||||||||||||||
44 | #include <qobject.h> | - | ||||||||||||||||||||||||
45 | #include <qdatetime.h> | - | ||||||||||||||||||||||||
46 | #include <qlocale.h> | - | ||||||||||||||||||||||||
47 | #include <qstyle.h> | - | ||||||||||||||||||||||||
48 | #include <qapplication.h> | - | ||||||||||||||||||||||||
49 | #include <private/qabstractitemmodel_p.h> | - | ||||||||||||||||||||||||
50 | #include <qdebug.h> | - | ||||||||||||||||||||||||
51 | - | |||||||||||||||||||||||||
52 | /*! | - | ||||||||||||||||||||||||
53 | \enum QDirModel::Roles | - | ||||||||||||||||||||||||
54 | \value FileIconRole | - | ||||||||||||||||||||||||
55 | \value FilePathRole | - | ||||||||||||||||||||||||
56 | \value FileNameRole | - | ||||||||||||||||||||||||
57 | */ | - | ||||||||||||||||||||||||
58 | - | |||||||||||||||||||||||||
59 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||||||||
60 | - | |||||||||||||||||||||||||
61 | class QDirModelPrivate : public QAbstractItemModelPrivate | - | ||||||||||||||||||||||||
62 | { | - | ||||||||||||||||||||||||
63 | Q_DECLARE_PUBLIC(QDirModel) | - | ||||||||||||||||||||||||
64 | - | |||||||||||||||||||||||||
65 | public: | - | ||||||||||||||||||||||||
66 | struct QDirNode | - | ||||||||||||||||||||||||
67 | { | - | ||||||||||||||||||||||||
68 | QDirNode() : parent(0), populated(false), stat(false) {} never executed: end of block | 0 | ||||||||||||||||||||||||
69 | QDirNode *parent; | - | ||||||||||||||||||||||||
70 | QFileInfo info; | - | ||||||||||||||||||||||||
71 | QIcon icon; // cache the icon | - | ||||||||||||||||||||||||
72 | mutable QVector<QDirNode> children; | - | ||||||||||||||||||||||||
73 | mutable bool populated; // have we read the children | - | ||||||||||||||||||||||||
74 | mutable bool stat; | - | ||||||||||||||||||||||||
75 | }; | - | ||||||||||||||||||||||||
76 | - | |||||||||||||||||||||||||
77 | QDirModelPrivate() | - | ||||||||||||||||||||||||
78 | : resolveSymlinks(true), | - | ||||||||||||||||||||||||
79 | readOnly(true), | - | ||||||||||||||||||||||||
80 | lazyChildCount(false), | - | ||||||||||||||||||||||||
81 | allowAppendChild(true), | - | ||||||||||||||||||||||||
82 | iconProvider(&defaultProvider), | - | ||||||||||||||||||||||||
83 | shouldStat(true) // ### This is set to false by QFileDialog | - | ||||||||||||||||||||||||
84 | { } never executed: end of block | 0 | ||||||||||||||||||||||||
85 | - | |||||||||||||||||||||||||
86 | void init(); | - | ||||||||||||||||||||||||
87 | QDirNode *node(int row, QDirNode *parent) const; | - | ||||||||||||||||||||||||
88 | QVector<QDirNode> children(QDirNode *parent, bool stat) const; | - | ||||||||||||||||||||||||
89 | - | |||||||||||||||||||||||||
90 | void _q_refresh(); | - | ||||||||||||||||||||||||
91 | - | |||||||||||||||||||||||||
92 | void savePersistentIndexes(); | - | ||||||||||||||||||||||||
93 | void restorePersistentIndexes(); | - | ||||||||||||||||||||||||
94 | - | |||||||||||||||||||||||||
95 | QFileInfoList entryInfoList(const QString &path) const; | - | ||||||||||||||||||||||||
96 | QStringList entryList(const QString &path) const; | - | ||||||||||||||||||||||||
97 | - | |||||||||||||||||||||||||
98 | QString name(const QModelIndex &index) const; | - | ||||||||||||||||||||||||
99 | QString size(const QModelIndex &index) const; | - | ||||||||||||||||||||||||
100 | QString type(const QModelIndex &index) const; | - | ||||||||||||||||||||||||
101 | QString time(const QModelIndex &index) const; | - | ||||||||||||||||||||||||
102 | - | |||||||||||||||||||||||||
103 | void appendChild(QDirModelPrivate::QDirNode *parent, const QString &path) const; | - | ||||||||||||||||||||||||
104 | static QFileInfo resolvedInfo(QFileInfo info); | - | ||||||||||||||||||||||||
105 | - | |||||||||||||||||||||||||
106 | inline QDirNode *node(const QModelIndex &index) const; | - | ||||||||||||||||||||||||
107 | inline void populate(QDirNode *parent) const; | - | ||||||||||||||||||||||||
108 | inline void clear(QDirNode *parent) const; | - | ||||||||||||||||||||||||
109 | - | |||||||||||||||||||||||||
110 | void invalidate(); | - | ||||||||||||||||||||||||
111 | - | |||||||||||||||||||||||||
112 | mutable QDirNode root; | - | ||||||||||||||||||||||||
113 | bool resolveSymlinks; | - | ||||||||||||||||||||||||
114 | bool readOnly; | - | ||||||||||||||||||||||||
115 | bool lazyChildCount; | - | ||||||||||||||||||||||||
116 | bool allowAppendChild; | - | ||||||||||||||||||||||||
117 | - | |||||||||||||||||||||||||
118 | QDir::Filters filters; | - | ||||||||||||||||||||||||
119 | QDir::SortFlags sort; | - | ||||||||||||||||||||||||
120 | QStringList nameFilters; | - | ||||||||||||||||||||||||
121 | - | |||||||||||||||||||||||||
122 | QFileIconProvider *iconProvider; | - | ||||||||||||||||||||||||
123 | QFileIconProvider defaultProvider; | - | ||||||||||||||||||||||||
124 | - | |||||||||||||||||||||||||
125 | struct SavedPersistent { | - | ||||||||||||||||||||||||
126 | QString path; | - | ||||||||||||||||||||||||
127 | int column; | - | ||||||||||||||||||||||||
128 | QPersistentModelIndexData *data; | - | ||||||||||||||||||||||||
129 | QPersistentModelIndex index; | - | ||||||||||||||||||||||||
130 | }; | - | ||||||||||||||||||||||||
131 | QList<SavedPersistent> savedPersistent; | - | ||||||||||||||||||||||||
132 | QPersistentModelIndex toBeRefreshed; | - | ||||||||||||||||||||||||
133 | - | |||||||||||||||||||||||||
134 | bool shouldStat; // use the "carefull not to stat directories" mode | - | ||||||||||||||||||||||||
135 | }; | - | ||||||||||||||||||||||||
136 | - | |||||||||||||||||||||||||
137 | void qt_setDirModelShouldNotStat(QDirModelPrivate *modelPrivate) | - | ||||||||||||||||||||||||
138 | { | - | ||||||||||||||||||||||||
139 | modelPrivate->shouldStat = false; | - | ||||||||||||||||||||||||
140 | } never executed: end of block | 0 | ||||||||||||||||||||||||
141 | - | |||||||||||||||||||||||||
142 | QDirModelPrivate::QDirNode *QDirModelPrivate::node(const QModelIndex &index) const | - | ||||||||||||||||||||||||
143 | { | - | ||||||||||||||||||||||||
144 | QDirModelPrivate::QDirNode *n = | - | ||||||||||||||||||||||||
145 | static_cast<QDirModelPrivate::QDirNode*>(index.internalPointer()); | - | ||||||||||||||||||||||||
146 | Q_ASSERT(n); | - | ||||||||||||||||||||||||
147 | return n; never executed: return n; | 0 | ||||||||||||||||||||||||
148 | } | - | ||||||||||||||||||||||||
149 | - | |||||||||||||||||||||||||
150 | void QDirModelPrivate::populate(QDirNode *parent) const | - | ||||||||||||||||||||||||
151 | { | - | ||||||||||||||||||||||||
152 | Q_ASSERT(parent); | - | ||||||||||||||||||||||||
153 | parent->children = children(parent, parent->stat); | - | ||||||||||||||||||||||||
154 | parent->populated = true; | - | ||||||||||||||||||||||||
155 | } never executed: end of block | 0 | ||||||||||||||||||||||||
156 | - | |||||||||||||||||||||||||
157 | void QDirModelPrivate::clear(QDirNode *parent) const | - | ||||||||||||||||||||||||
158 | { | - | ||||||||||||||||||||||||
159 | Q_ASSERT(parent); | - | ||||||||||||||||||||||||
160 | parent->children.clear(); | - | ||||||||||||||||||||||||
161 | parent->populated = false; | - | ||||||||||||||||||||||||
162 | } never executed: end of block | 0 | ||||||||||||||||||||||||
163 | - | |||||||||||||||||||||||||
164 | void QDirModelPrivate::invalidate() | - | ||||||||||||||||||||||||
165 | { | - | ||||||||||||||||||||||||
166 | QStack<const QDirNode*> nodes; | - | ||||||||||||||||||||||||
167 | nodes.push(&root); | - | ||||||||||||||||||||||||
168 | while (!nodes.empty()) {
| 0 | ||||||||||||||||||||||||
169 | const QDirNode *current = nodes.pop(); | - | ||||||||||||||||||||||||
170 | current->stat = false; | - | ||||||||||||||||||||||||
171 | const QVector<QDirNode> children = current->children; | - | ||||||||||||||||||||||||
172 | for (int i = 0; i < children.count(); ++i)
| 0 | ||||||||||||||||||||||||
173 | nodes.push(&children.at(i)); never executed: nodes.push(&children.at(i)); | 0 | ||||||||||||||||||||||||
174 | } never executed: end of block | 0 | ||||||||||||||||||||||||
175 | } never executed: end of block | 0 | ||||||||||||||||||||||||
176 | - | |||||||||||||||||||||||||
177 | /*! | - | ||||||||||||||||||||||||
178 | \class QDirModel | - | ||||||||||||||||||||||||
179 | \obsolete | - | ||||||||||||||||||||||||
180 | \brief The QDirModel class provides a data model for the local filesystem. | - | ||||||||||||||||||||||||
181 | - | |||||||||||||||||||||||||
182 | \ingroup model-view | - | ||||||||||||||||||||||||
183 | \inmodule QtWidgets | - | ||||||||||||||||||||||||
184 | - | |||||||||||||||||||||||||
185 | The usage of QDirModel is not recommended anymore. The | - | ||||||||||||||||||||||||
186 | QFileSystemModel class is a more performant alternative. | - | ||||||||||||||||||||||||
187 | - | |||||||||||||||||||||||||
188 | This class provides access to the local filesystem, providing functions | - | ||||||||||||||||||||||||
189 | for renaming and removing files and directories, and for creating new | - | ||||||||||||||||||||||||
190 | directories. In the simplest case, it can be used with a suitable display | - | ||||||||||||||||||||||||
191 | widget as part of a browser or filer. | - | ||||||||||||||||||||||||
192 | - | |||||||||||||||||||||||||
193 | QDirModel keeps a cache with file information. The cache needs to be | - | ||||||||||||||||||||||||
194 | updated with refresh(). | - | ||||||||||||||||||||||||
195 | - | |||||||||||||||||||||||||
196 | QDirModel can be accessed using the standard interface provided by | - | ||||||||||||||||||||||||
197 | QAbstractItemModel, but it also provides some convenience functions | - | ||||||||||||||||||||||||
198 | that are specific to a directory model. The fileInfo() and isDir() | - | ||||||||||||||||||||||||
199 | functions provide information about the underlying files and directories | - | ||||||||||||||||||||||||
200 | related to items in the model. | - | ||||||||||||||||||||||||
201 | - | |||||||||||||||||||||||||
202 | Directories can be created and removed using mkdir(), rmdir(), and the | - | ||||||||||||||||||||||||
203 | model will be automatically updated to take the changes into account. | - | ||||||||||||||||||||||||
204 | - | |||||||||||||||||||||||||
205 | \note QDirModel requires an instance of \l QApplication. | - | ||||||||||||||||||||||||
206 | - | |||||||||||||||||||||||||
207 | \sa nameFilters(), setFilter(), filter(), QListView, QTreeView, QFileSystemModel, | - | ||||||||||||||||||||||||
208 | {Dir View Example}, {Model Classes} | - | ||||||||||||||||||||||||
209 | */ | - | ||||||||||||||||||||||||
210 | - | |||||||||||||||||||||||||
211 | /*! | - | ||||||||||||||||||||||||
212 | Constructs a new directory model with the given \a parent. | - | ||||||||||||||||||||||||
213 | Only those files matching the \a nameFilters and the | - | ||||||||||||||||||||||||
214 | \a filters are included in the model. The sort order is given by the | - | ||||||||||||||||||||||||
215 | \a sort flags. | - | ||||||||||||||||||||||||
216 | */ | - | ||||||||||||||||||||||||
217 | - | |||||||||||||||||||||||||
218 | QDirModel::QDirModel(const QStringList &nameFilters, | - | ||||||||||||||||||||||||
219 | QDir::Filters filters, | - | ||||||||||||||||||||||||
220 | QDir::SortFlags sort, | - | ||||||||||||||||||||||||
221 | QObject *parent) | - | ||||||||||||||||||||||||
222 | : QAbstractItemModel(*new QDirModelPrivate, parent) | - | ||||||||||||||||||||||||
223 | { | - | ||||||||||||||||||||||||
224 | Q_D(QDirModel); | - | ||||||||||||||||||||||||
225 | // we always start with QDir::drives() | - | ||||||||||||||||||||||||
226 | d->nameFilters = nameFilters.isEmpty() ? QStringList(QLatin1String("*")) : nameFilters;
| 0 | ||||||||||||||||||||||||
227 | d->filters = filters; | - | ||||||||||||||||||||||||
228 | d->sort = sort; | - | ||||||||||||||||||||||||
229 | d->root.parent = 0; | - | ||||||||||||||||||||||||
230 | d->root.info = QFileInfo(); | - | ||||||||||||||||||||||||
231 | d->clear(&d->root); | - | ||||||||||||||||||||||||
232 | } never executed: end of block | 0 | ||||||||||||||||||||||||
233 | - | |||||||||||||||||||||||||
234 | /*! | - | ||||||||||||||||||||||||
235 | Constructs a directory model with the given \a parent. | - | ||||||||||||||||||||||||
236 | */ | - | ||||||||||||||||||||||||
237 | - | |||||||||||||||||||||||||
238 | QDirModel::QDirModel(QObject *parent) | - | ||||||||||||||||||||||||
239 | : QAbstractItemModel(*new QDirModelPrivate, parent) | - | ||||||||||||||||||||||||
240 | { | - | ||||||||||||||||||||||||
241 | Q_D(QDirModel); | - | ||||||||||||||||||||||||
242 | d->init(); | - | ||||||||||||||||||||||||
243 | } never executed: end of block | 0 | ||||||||||||||||||||||||
244 | - | |||||||||||||||||||||||||
245 | /*! | - | ||||||||||||||||||||||||
246 | \internal | - | ||||||||||||||||||||||||
247 | */ | - | ||||||||||||||||||||||||
248 | QDirModel::QDirModel(QDirModelPrivate &dd, QObject *parent) | - | ||||||||||||||||||||||||
249 | : QAbstractItemModel(dd, parent) | - | ||||||||||||||||||||||||
250 | { | - | ||||||||||||||||||||||||
251 | Q_D(QDirModel); | - | ||||||||||||||||||||||||
252 | d->init(); | - | ||||||||||||||||||||||||
253 | } never executed: end of block | 0 | ||||||||||||||||||||||||
254 | - | |||||||||||||||||||||||||
255 | /*! | - | ||||||||||||||||||||||||
256 | Destroys this directory model. | - | ||||||||||||||||||||||||
257 | */ | - | ||||||||||||||||||||||||
258 | - | |||||||||||||||||||||||||
259 | QDirModel::~QDirModel() | - | ||||||||||||||||||||||||
260 | { | - | ||||||||||||||||||||||||
261 | - | |||||||||||||||||||||||||
262 | } | - | ||||||||||||||||||||||||
263 | - | |||||||||||||||||||||||||
264 | /*! | - | ||||||||||||||||||||||||
265 | Returns the model item index for the item in the \a parent with the | - | ||||||||||||||||||||||||
266 | given \a row and \a column. | - | ||||||||||||||||||||||||
267 | - | |||||||||||||||||||||||||
268 | */ | - | ||||||||||||||||||||||||
269 | - | |||||||||||||||||||||||||
270 | QModelIndex QDirModel::index(int row, int column, const QModelIndex &parent) const | - | ||||||||||||||||||||||||
271 | { | - | ||||||||||||||||||||||||
272 | Q_D(const QDirModel); | - | ||||||||||||||||||||||||
273 | // note that rowCount does lazy population | - | ||||||||||||||||||||||||
274 | if (column < 0 || column >= columnCount(parent) || row < 0 || parent.column() > 0)
| 0 | ||||||||||||||||||||||||
275 | return QModelIndex(); never executed: return QModelIndex(); | 0 | ||||||||||||||||||||||||
276 | // make sure the list of children is up to date | - | ||||||||||||||||||||||||
277 | QDirModelPrivate::QDirNode *p = (d->indexValid(parent) ? d->node(parent) : &d->root);
| 0 | ||||||||||||||||||||||||
278 | Q_ASSERT(p); | - | ||||||||||||||||||||||||
279 | if (!p->populated)
| 0 | ||||||||||||||||||||||||
280 | d->populate(p); // populate without stat'ing never executed: d->populate(p); | 0 | ||||||||||||||||||||||||
281 | if (row >= p->children.count())
| 0 | ||||||||||||||||||||||||
282 | return QModelIndex(); never executed: return QModelIndex(); | 0 | ||||||||||||||||||||||||
283 | // now get the internal pointer for the index | - | ||||||||||||||||||||||||
284 | QDirModelPrivate::QDirNode *n = d->node(row, d->indexValid(parent) ? p : 0); | - | ||||||||||||||||||||||||
285 | Q_ASSERT(n); | - | ||||||||||||||||||||||||
286 | - | |||||||||||||||||||||||||
287 | return createIndex(row, column, n); never executed: return createIndex(row, column, n); | 0 | ||||||||||||||||||||||||
288 | } | - | ||||||||||||||||||||||||
289 | - | |||||||||||||||||||||||||
290 | /*! | - | ||||||||||||||||||||||||
291 | Return the parent of the given \a child model item. | - | ||||||||||||||||||||||||
292 | */ | - | ||||||||||||||||||||||||
293 | - | |||||||||||||||||||||||||
294 | QModelIndex QDirModel::parent(const QModelIndex &child) const | - | ||||||||||||||||||||||||
295 | { | - | ||||||||||||||||||||||||
296 | Q_D(const QDirModel); | - | ||||||||||||||||||||||||
297 | - | |||||||||||||||||||||||||
298 | if (!d->indexValid(child))
| 0 | ||||||||||||||||||||||||
299 | return QModelIndex(); never executed: return QModelIndex(); | 0 | ||||||||||||||||||||||||
300 | QDirModelPrivate::QDirNode *node = d->node(child); | - | ||||||||||||||||||||||||
301 | QDirModelPrivate::QDirNode *par = (node ? node->parent : 0);
| 0 | ||||||||||||||||||||||||
302 | if (par == 0) // parent is the root node
| 0 | ||||||||||||||||||||||||
303 | return QModelIndex(); never executed: return QModelIndex(); | 0 | ||||||||||||||||||||||||
304 | - | |||||||||||||||||||||||||
305 | // get the parent's row | - | ||||||||||||||||||||||||
306 | const QVector<QDirModelPrivate::QDirNode> children = | - | ||||||||||||||||||||||||
307 | par->parent ? par->parent->children : d->root.children;
| 0 | ||||||||||||||||||||||||
308 | Q_ASSERT(children.count() > 0); | - | ||||||||||||||||||||||||
309 | int row = (par - &(children.at(0))); | - | ||||||||||||||||||||||||
310 | Q_ASSERT(row >= 0); | - | ||||||||||||||||||||||||
311 | - | |||||||||||||||||||||||||
312 | return createIndex(row, 0, par); never executed: return createIndex(row, 0, par); | 0 | ||||||||||||||||||||||||
313 | } | - | ||||||||||||||||||||||||
314 | - | |||||||||||||||||||||||||
315 | /*! | - | ||||||||||||||||||||||||
316 | Returns the number of rows in the \a parent model item. | - | ||||||||||||||||||||||||
317 | - | |||||||||||||||||||||||||
318 | */ | - | ||||||||||||||||||||||||
319 | - | |||||||||||||||||||||||||
320 | int QDirModel::rowCount(const QModelIndex &parent) const | - | ||||||||||||||||||||||||
321 | { | - | ||||||||||||||||||||||||
322 | Q_D(const QDirModel); | - | ||||||||||||||||||||||||
323 | if (parent.column() > 0)
| 0 | ||||||||||||||||||||||||
324 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
325 | - | |||||||||||||||||||||||||
326 | if (!parent.isValid()) {
| 0 | ||||||||||||||||||||||||
327 | if (!d->root.populated) // lazy population
| 0 | ||||||||||||||||||||||||
328 | d->populate(&d->root); never executed: d->populate(&d->root); | 0 | ||||||||||||||||||||||||
329 | return d->root.children.count(); never executed: return d->root.children.count(); | 0 | ||||||||||||||||||||||||
330 | } | - | ||||||||||||||||||||||||
331 | if (parent.model() != this)
| 0 | ||||||||||||||||||||||||
332 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
333 | QDirModelPrivate::QDirNode *p = d->node(parent); | - | ||||||||||||||||||||||||
334 | if (p->info.isDir() && !p->populated) // lazy population
| 0 | ||||||||||||||||||||||||
335 | d->populate(p); never executed: d->populate(p); | 0 | ||||||||||||||||||||||||
336 | return p->children.count(); never executed: return p->children.count(); | 0 | ||||||||||||||||||||||||
337 | } | - | ||||||||||||||||||||||||
338 | - | |||||||||||||||||||||||||
339 | /*! | - | ||||||||||||||||||||||||
340 | Returns the number of columns in the \a parent model item. | - | ||||||||||||||||||||||||
341 | - | |||||||||||||||||||||||||
342 | */ | - | ||||||||||||||||||||||||
343 | - | |||||||||||||||||||||||||
344 | int QDirModel::columnCount(const QModelIndex &parent) const | - | ||||||||||||||||||||||||
345 | { | - | ||||||||||||||||||||||||
346 | if (parent.column() > 0)
| 0 | ||||||||||||||||||||||||
347 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
348 | return 4; never executed: return 4; | 0 | ||||||||||||||||||||||||
349 | } | - | ||||||||||||||||||||||||
350 | - | |||||||||||||||||||||||||
351 | /*! | - | ||||||||||||||||||||||||
352 | Returns the data for the model item \a index with the given \a role. | - | ||||||||||||||||||||||||
353 | */ | - | ||||||||||||||||||||||||
354 | QVariant QDirModel::data(const QModelIndex &index, int role) const | - | ||||||||||||||||||||||||
355 | { | - | ||||||||||||||||||||||||
356 | Q_D(const QDirModel); | - | ||||||||||||||||||||||||
357 | if (!d->indexValid(index))
| 0 | ||||||||||||||||||||||||
358 | return QVariant(); never executed: return QVariant(); | 0 | ||||||||||||||||||||||||
359 | - | |||||||||||||||||||||||||
360 | if (role == Qt::DisplayRole || role == Qt::EditRole) {
| 0 | ||||||||||||||||||||||||
361 | switch (index.column()) { | - | ||||||||||||||||||||||||
362 | case 0: return d->name(index); never executed: return d->name(index); never executed: case 0: | 0 | ||||||||||||||||||||||||
363 | case 1: return d->size(index); never executed: return d->size(index); never executed: case 1: | 0 | ||||||||||||||||||||||||
364 | case 2: return d->type(index); never executed: return d->type(index); never executed: case 2: | 0 | ||||||||||||||||||||||||
365 | case 3: return d->time(index); never executed: return d->time(index); never executed: case 3: | 0 | ||||||||||||||||||||||||
366 | default: never executed: default: | 0 | ||||||||||||||||||||||||
367 | qWarning("data: invalid display value column %d", index.column()); | - | ||||||||||||||||||||||||
368 | return QVariant(); never executed: return QVariant(); | 0 | ||||||||||||||||||||||||
369 | } | - | ||||||||||||||||||||||||
370 | } | - | ||||||||||||||||||||||||
371 | - | |||||||||||||||||||||||||
372 | if (index.column() == 0) {
| 0 | ||||||||||||||||||||||||
373 | if (role == FileIconRole)
| 0 | ||||||||||||||||||||||||
374 | return fileIcon(index); never executed: return fileIcon(index); | 0 | ||||||||||||||||||||||||
375 | if (role == FilePathRole)
| 0 | ||||||||||||||||||||||||
376 | return filePath(index); never executed: return filePath(index); | 0 | ||||||||||||||||||||||||
377 | if (role == FileNameRole)
| 0 | ||||||||||||||||||||||||
378 | return fileName(index); never executed: return fileName(index); | 0 | ||||||||||||||||||||||||
379 | } never executed: end of block | 0 | ||||||||||||||||||||||||
380 | - | |||||||||||||||||||||||||
381 | if (index.column() == 1 && Qt::TextAlignmentRole == role) {
| 0 | ||||||||||||||||||||||||
382 | return Qt::AlignRight; never executed: return Qt::AlignRight; | 0 | ||||||||||||||||||||||||
383 | } | - | ||||||||||||||||||||||||
384 | return QVariant(); never executed: return QVariant(); | 0 | ||||||||||||||||||||||||
385 | } | - | ||||||||||||||||||||||||
386 | - | |||||||||||||||||||||||||
387 | /*! | - | ||||||||||||||||||||||||
388 | Sets the data for the model item \a index with the given \a role to | - | ||||||||||||||||||||||||
389 | the data referenced by the \a value. Returns \c true if successful; | - | ||||||||||||||||||||||||
390 | otherwise returns \c false. | - | ||||||||||||||||||||||||
391 | - | |||||||||||||||||||||||||
392 | \sa Qt::ItemDataRole | - | ||||||||||||||||||||||||
393 | */ | - | ||||||||||||||||||||||||
394 | - | |||||||||||||||||||||||||
395 | bool QDirModel::setData(const QModelIndex &index, const QVariant &value, int role) | - | ||||||||||||||||||||||||
396 | { | - | ||||||||||||||||||||||||
397 | Q_D(QDirModel); | - | ||||||||||||||||||||||||
398 | if (!d->indexValid(index) || index.column() != 0
| 0 | ||||||||||||||||||||||||
399 | || (flags(index) & Qt::ItemIsEditable) == 0 || role != Qt::EditRole)
| 0 | ||||||||||||||||||||||||
400 | return false; never executed: return false; | 0 | ||||||||||||||||||||||||
401 | - | |||||||||||||||||||||||||
402 | QDirModelPrivate::QDirNode *node = d->node(index); | - | ||||||||||||||||||||||||
403 | QDir dir = node->info.dir(); | - | ||||||||||||||||||||||||
404 | QString name = value.toString(); | - | ||||||||||||||||||||||||
405 | if (dir.rename(node->info.fileName(), name)) {
| 0 | ||||||||||||||||||||||||
406 | node->info = QFileInfo(dir, name); | - | ||||||||||||||||||||||||
407 | QModelIndex sibling = index.sibling(index.row(), 3); | - | ||||||||||||||||||||||||
408 | emit dataChanged(index, sibling); | - | ||||||||||||||||||||||||
409 | - | |||||||||||||||||||||||||
410 | d->toBeRefreshed = index.parent(); | - | ||||||||||||||||||||||||
411 | QMetaObject::invokeMethod(this, "_q_refresh", Qt::QueuedConnection); | - | ||||||||||||||||||||||||
412 | - | |||||||||||||||||||||||||
413 | return true; never executed: return true; | 0 | ||||||||||||||||||||||||
414 | } | - | ||||||||||||||||||||||||
415 | - | |||||||||||||||||||||||||
416 | return false; never executed: return false; | 0 | ||||||||||||||||||||||||
417 | } | - | ||||||||||||||||||||||||
418 | - | |||||||||||||||||||||||||
419 | /*! | - | ||||||||||||||||||||||||
420 | Returns the data stored under the given \a role for the specified \a section | - | ||||||||||||||||||||||||
421 | of the header with the given \a orientation. | - | ||||||||||||||||||||||||
422 | */ | - | ||||||||||||||||||||||||
423 | - | |||||||||||||||||||||||||
424 | QVariant QDirModel::headerData(int section, Qt::Orientation orientation, int role) const | - | ||||||||||||||||||||||||
425 | { | - | ||||||||||||||||||||||||
426 | if (orientation == Qt::Horizontal) {
| 0 | ||||||||||||||||||||||||
427 | if (role != Qt::DisplayRole)
| 0 | ||||||||||||||||||||||||
428 | return QVariant(); never executed: return QVariant(); | 0 | ||||||||||||||||||||||||
429 | switch (section) { | - | ||||||||||||||||||||||||
430 | case 0: return tr("Name"); never executed: return tr("Name"); never executed: case 0: | 0 | ||||||||||||||||||||||||
431 | case 1: return tr("Size"); never executed: return tr("Size"); never executed: case 1: | 0 | ||||||||||||||||||||||||
432 | case 2: return never executed: return tr("Type", "All other platforms"); never executed: case 2: | 0 | ||||||||||||||||||||||||
433 | #ifdef Q_OS_MAC never executed: return tr("Type", "All other platforms"); | 0 | ||||||||||||||||||||||||
434 | tr("Kind", "Match OS X Finder"); never executed: return tr("Type", "All other platforms"); | 0 | ||||||||||||||||||||||||
435 | #else never executed: return tr("Type", "All other platforms"); | 0 | ||||||||||||||||||||||||
436 | tr("Type", "All other platforms"); never executed: return tr("Type", "All other platforms"); | 0 | ||||||||||||||||||||||||
437 | #endif | - | ||||||||||||||||||||||||
438 | // Windows - Type | - | ||||||||||||||||||||||||
439 | // OS X - Kind | - | ||||||||||||||||||||||||
440 | // Konqueror - File Type | - | ||||||||||||||||||||||||
441 | // Nautilus - Type | - | ||||||||||||||||||||||||
442 | case 3: return tr("Date Modified"); never executed: return tr("Date Modified"); never executed: case 3: | 0 | ||||||||||||||||||||||||
443 | default: return QVariant(); never executed: return QVariant(); never executed: default: | 0 | ||||||||||||||||||||||||
444 | } | - | ||||||||||||||||||||||||
445 | } | - | ||||||||||||||||||||||||
446 | return QAbstractItemModel::headerData(section, orientation, role); never executed: return QAbstractItemModel::headerData(section, orientation, role); | 0 | ||||||||||||||||||||||||
447 | } | - | ||||||||||||||||||||||||
448 | - | |||||||||||||||||||||||||
449 | /*! | - | ||||||||||||||||||||||||
450 | Returns \c true if the \a parent model item has children; otherwise | - | ||||||||||||||||||||||||
451 | returns \c false. | - | ||||||||||||||||||||||||
452 | */ | - | ||||||||||||||||||||||||
453 | - | |||||||||||||||||||||||||
454 | bool QDirModel::hasChildren(const QModelIndex &parent) const | - | ||||||||||||||||||||||||
455 | { | - | ||||||||||||||||||||||||
456 | Q_D(const QDirModel); | - | ||||||||||||||||||||||||
457 | if (parent.column() > 0)
| 0 | ||||||||||||||||||||||||
458 | return false; never executed: return false; | 0 | ||||||||||||||||||||||||
459 | - | |||||||||||||||||||||||||
460 | if (!parent.isValid()) // the invalid index is the "My Computer" item
| 0 | ||||||||||||||||||||||||
461 | return true; // the drives never executed: return true; | 0 | ||||||||||||||||||||||||
462 | QDirModelPrivate::QDirNode *p = d->node(parent); | - | ||||||||||||||||||||||||
463 | Q_ASSERT(p); | - | ||||||||||||||||||||||||
464 | - | |||||||||||||||||||||||||
465 | if (d->lazyChildCount) // optimization that only checks for children if the node has been populated
| 0 | ||||||||||||||||||||||||
466 | return p->info.isDir(); never executed: return p->info.isDir(); | 0 | ||||||||||||||||||||||||
467 | return p->info.isDir() && rowCount(parent) > 0; never executed: return p->info.isDir() && rowCount(parent) > 0;
| 0 | ||||||||||||||||||||||||
468 | } | - | ||||||||||||||||||||||||
469 | - | |||||||||||||||||||||||||
470 | /*! | - | ||||||||||||||||||||||||
471 | Returns the item flags for the given \a index in the model. | - | ||||||||||||||||||||||||
472 | - | |||||||||||||||||||||||||
473 | \sa Qt::ItemFlags | - | ||||||||||||||||||||||||
474 | */ | - | ||||||||||||||||||||||||
475 | Qt::ItemFlags QDirModel::flags(const QModelIndex &index) const | - | ||||||||||||||||||||||||
476 | { | - | ||||||||||||||||||||||||
477 | Q_D(const QDirModel); | - | ||||||||||||||||||||||||
478 | Qt::ItemFlags flags = QAbstractItemModel::flags(index); | - | ||||||||||||||||||||||||
479 | if (!d->indexValid(index))
| 0 | ||||||||||||||||||||||||
480 | return flags; never executed: return flags; | 0 | ||||||||||||||||||||||||
481 | flags |= Qt::ItemIsDragEnabled; | - | ||||||||||||||||||||||||
482 | if (d->readOnly)
| 0 | ||||||||||||||||||||||||
483 | return flags; never executed: return flags; | 0 | ||||||||||||||||||||||||
484 | QDirModelPrivate::QDirNode *node = d->node(index); | - | ||||||||||||||||||||||||
485 | if ((index.column() == 0) && node->info.isWritable()) {
| 0 | ||||||||||||||||||||||||
486 | flags |= Qt::ItemIsEditable; | - | ||||||||||||||||||||||||
487 | if (fileInfo(index).isDir()) // is directory and is editable
| 0 | ||||||||||||||||||||||||
488 | flags |= Qt::ItemIsDropEnabled; never executed: flags |= Qt::ItemIsDropEnabled; | 0 | ||||||||||||||||||||||||
489 | } never executed: end of block | 0 | ||||||||||||||||||||||||
490 | return flags; never executed: return flags; | 0 | ||||||||||||||||||||||||
491 | } | - | ||||||||||||||||||||||||
492 | - | |||||||||||||||||||||||||
493 | /*! | - | ||||||||||||||||||||||||
494 | Sort the model items in the \a column using the \a order given. | - | ||||||||||||||||||||||||
495 | The order is a value defined in \l Qt::SortOrder. | - | ||||||||||||||||||||||||
496 | */ | - | ||||||||||||||||||||||||
497 | - | |||||||||||||||||||||||||
498 | void QDirModel::sort(int column, Qt::SortOrder order) | - | ||||||||||||||||||||||||
499 | { | - | ||||||||||||||||||||||||
500 | QDir::SortFlags sort = QDir::DirsFirst | QDir::IgnoreCase; | - | ||||||||||||||||||||||||
501 | if (order == Qt::DescendingOrder)
| 0 | ||||||||||||||||||||||||
502 | sort |= QDir::Reversed; never executed: sort |= QDir::Reversed; | 0 | ||||||||||||||||||||||||
503 | - | |||||||||||||||||||||||||
504 | switch (column) { | - | ||||||||||||||||||||||||
505 | case 0: never executed: case 0: | 0 | ||||||||||||||||||||||||
506 | sort |= QDir::Name; | - | ||||||||||||||||||||||||
507 | break; never executed: break; | 0 | ||||||||||||||||||||||||
508 | case 1: never executed: case 1: | 0 | ||||||||||||||||||||||||
509 | sort |= QDir::Size; | - | ||||||||||||||||||||||||
510 | break; never executed: break; | 0 | ||||||||||||||||||||||||
511 | case 2: never executed: case 2: | 0 | ||||||||||||||||||||||||
512 | sort |= QDir::Type; | - | ||||||||||||||||||||||||
513 | break; never executed: break; | 0 | ||||||||||||||||||||||||
514 | case 3: never executed: case 3: | 0 | ||||||||||||||||||||||||
515 | sort |= QDir::Time; | - | ||||||||||||||||||||||||
516 | break; never executed: break; | 0 | ||||||||||||||||||||||||
517 | default: never executed: default: | 0 | ||||||||||||||||||||||||
518 | break; never executed: break; | 0 | ||||||||||||||||||||||||
519 | } | - | ||||||||||||||||||||||||
520 | - | |||||||||||||||||||||||||
521 | setSorting(sort); | - | ||||||||||||||||||||||||
522 | } never executed: end of block | 0 | ||||||||||||||||||||||||
523 | - | |||||||||||||||||||||||||
524 | /*! | - | ||||||||||||||||||||||||
525 | Returns a list of MIME types that can be used to describe a list of items | - | ||||||||||||||||||||||||
526 | in the model. | - | ||||||||||||||||||||||||
527 | */ | - | ||||||||||||||||||||||||
528 | - | |||||||||||||||||||||||||
529 | QStringList QDirModel::mimeTypes() const | - | ||||||||||||||||||||||||
530 | { | - | ||||||||||||||||||||||||
531 | return QStringList(QLatin1String("text/uri-list")); never executed: return QStringList(QLatin1String("text/uri-list")); | 0 | ||||||||||||||||||||||||
532 | } | - | ||||||||||||||||||||||||
533 | - | |||||||||||||||||||||||||
534 | /*! | - | ||||||||||||||||||||||||
535 | Returns an object that contains a serialized description of the specified | - | ||||||||||||||||||||||||
536 | \a indexes. The format used to describe the items corresponding to the | - | ||||||||||||||||||||||||
537 | indexes is obtained from the mimeTypes() function. | - | ||||||||||||||||||||||||
538 | - | |||||||||||||||||||||||||
539 | If the list of indexes is empty, 0 is returned rather than a serialized | - | ||||||||||||||||||||||||
540 | empty list. | - | ||||||||||||||||||||||||
541 | */ | - | ||||||||||||||||||||||||
542 | - | |||||||||||||||||||||||||
543 | QMimeData *QDirModel::mimeData(const QModelIndexList &indexes) const | - | ||||||||||||||||||||||||
544 | { | - | ||||||||||||||||||||||||
545 | QList<QUrl> urls; | - | ||||||||||||||||||||||||
546 | QList<QModelIndex>::const_iterator it = indexes.begin(); | - | ||||||||||||||||||||||||
547 | for (; it != indexes.end(); ++it)
| 0 | ||||||||||||||||||||||||
548 | if ((*it).column() == 0)
| 0 | ||||||||||||||||||||||||
549 | urls << QUrl::fromLocalFile(filePath(*it)); never executed: urls << QUrl::fromLocalFile(filePath(*it)); | 0 | ||||||||||||||||||||||||
550 | QMimeData *data = new QMimeData(); | - | ||||||||||||||||||||||||
551 | data->setUrls(urls); | - | ||||||||||||||||||||||||
552 | return data; never executed: return data; | 0 | ||||||||||||||||||||||||
553 | } | - | ||||||||||||||||||||||||
554 | - | |||||||||||||||||||||||||
555 | /*! | - | ||||||||||||||||||||||||
556 | Handles the \a data supplied by a drag and drop operation that ended with | - | ||||||||||||||||||||||||
557 | the given \a action over the row in the model specified by the \a row and | - | ||||||||||||||||||||||||
558 | \a column and by the \a parent index. | - | ||||||||||||||||||||||||
559 | - | |||||||||||||||||||||||||
560 | Returns \c true if the drop was successful, and false otherwise. | - | ||||||||||||||||||||||||
561 | - | |||||||||||||||||||||||||
562 | \sa supportedDropActions() | - | ||||||||||||||||||||||||
563 | */ | - | ||||||||||||||||||||||||
564 | - | |||||||||||||||||||||||||
565 | bool QDirModel::dropMimeData(const QMimeData *data, Qt::DropAction action, | - | ||||||||||||||||||||||||
566 | int /* row */, int /* column */, const QModelIndex &parent) | - | ||||||||||||||||||||||||
567 | { | - | ||||||||||||||||||||||||
568 | Q_D(QDirModel); | - | ||||||||||||||||||||||||
569 | if (!d->indexValid(parent) || isReadOnly())
| 0 | ||||||||||||||||||||||||
570 | return false; never executed: return false; | 0 | ||||||||||||||||||||||||
571 | - | |||||||||||||||||||||||||
572 | bool success = true; | - | ||||||||||||||||||||||||
573 | QString to = filePath(parent) + QDir::separator(); | - | ||||||||||||||||||||||||
574 | QModelIndex _parent = parent; | - | ||||||||||||||||||||||||
575 | - | |||||||||||||||||||||||||
576 | QList<QUrl> urls = data->urls(); | - | ||||||||||||||||||||||||
577 | QList<QUrl>::const_iterator it = urls.constBegin(); | - | ||||||||||||||||||||||||
578 | - | |||||||||||||||||||||||||
579 | switch (action) { | - | ||||||||||||||||||||||||
580 | case Qt::CopyAction: never executed: case Qt::CopyAction: | 0 | ||||||||||||||||||||||||
581 | for (; it != urls.constEnd(); ++it) {
| 0 | ||||||||||||||||||||||||
582 | QString path = (*it).toLocalFile(); | - | ||||||||||||||||||||||||
583 | success = QFile::copy(path, to + QFileInfo(path).fileName()) && success;
| 0 | ||||||||||||||||||||||||
584 | } never executed: end of block | 0 | ||||||||||||||||||||||||
585 | break; never executed: break; | 0 | ||||||||||||||||||||||||
586 | case Qt::LinkAction: never executed: case Qt::LinkAction: | 0 | ||||||||||||||||||||||||
587 | for (; it != urls.constEnd(); ++it) {
| 0 | ||||||||||||||||||||||||
588 | QString path = (*it).toLocalFile(); | - | ||||||||||||||||||||||||
589 | success = QFile::link(path, to + QFileInfo(path).fileName()) && success;
| 0 | ||||||||||||||||||||||||
590 | } never executed: end of block | 0 | ||||||||||||||||||||||||
591 | break; never executed: break; | 0 | ||||||||||||||||||||||||
592 | case Qt::MoveAction: never executed: case Qt::MoveAction: | 0 | ||||||||||||||||||||||||
593 | for (; it != urls.constEnd(); ++it) {
| 0 | ||||||||||||||||||||||||
594 | QString path = (*it).toLocalFile(); | - | ||||||||||||||||||||||||
595 | if (QFile::copy(path, to + QFileInfo(path).fileName())
| 0 | ||||||||||||||||||||||||
596 | && QFile::remove(path)) {
| 0 | ||||||||||||||||||||||||
597 | QModelIndex idx=index(QFileInfo(path).path()); | - | ||||||||||||||||||||||||
598 | if (idx.isValid()) {
| 0 | ||||||||||||||||||||||||
599 | refresh(idx); | - | ||||||||||||||||||||||||
600 | //the previous call to refresh may invalidate the _parent. so recreate a new QModelIndex | - | ||||||||||||||||||||||||
601 | _parent = index(to); | - | ||||||||||||||||||||||||
602 | } never executed: end of block | 0 | ||||||||||||||||||||||||
603 | } else { never executed: end of block | 0 | ||||||||||||||||||||||||
604 | success = false; | - | ||||||||||||||||||||||||
605 | } never executed: end of block | 0 | ||||||||||||||||||||||||
606 | } | - | ||||||||||||||||||||||||
607 | break; never executed: break; | 0 | ||||||||||||||||||||||||
608 | default: never executed: default: | 0 | ||||||||||||||||||||||||
609 | return false; never executed: return false; | 0 | ||||||||||||||||||||||||
610 | } | - | ||||||||||||||||||||||||
611 | - | |||||||||||||||||||||||||
612 | if (success)
| 0 | ||||||||||||||||||||||||
613 | refresh(_parent); never executed: refresh(_parent); | 0 | ||||||||||||||||||||||||
614 | - | |||||||||||||||||||||||||
615 | return success; never executed: return success; | 0 | ||||||||||||||||||||||||
616 | } | - | ||||||||||||||||||||||||
617 | - | |||||||||||||||||||||||||
618 | /*! | - | ||||||||||||||||||||||||
619 | Returns the drop actions supported by this model. | - | ||||||||||||||||||||||||
620 | - | |||||||||||||||||||||||||
621 | \sa Qt::DropActions | - | ||||||||||||||||||||||||
622 | */ | - | ||||||||||||||||||||||||
623 | - | |||||||||||||||||||||||||
624 | Qt::DropActions QDirModel::supportedDropActions() const | - | ||||||||||||||||||||||||
625 | { | - | ||||||||||||||||||||||||
626 | return Qt::CopyAction | Qt::MoveAction; // FIXME: LinkAction is not supported yet never executed: return Qt::CopyAction | Qt::MoveAction; | 0 | ||||||||||||||||||||||||
627 | } | - | ||||||||||||||||||||||||
628 | - | |||||||||||||||||||||||||
629 | /*! | - | ||||||||||||||||||||||||
630 | Sets the \a provider of file icons for the directory model. | - | ||||||||||||||||||||||||
631 | - | |||||||||||||||||||||||||
632 | */ | - | ||||||||||||||||||||||||
633 | - | |||||||||||||||||||||||||
634 | void QDirModel::setIconProvider(QFileIconProvider *provider) | - | ||||||||||||||||||||||||
635 | { | - | ||||||||||||||||||||||||
636 | Q_D(QDirModel); | - | ||||||||||||||||||||||||
637 | d->iconProvider = provider; | - | ||||||||||||||||||||||||
638 | } never executed: end of block | 0 | ||||||||||||||||||||||||
639 | - | |||||||||||||||||||||||||
640 | /*! | - | ||||||||||||||||||||||||
641 | Returns the file icon provider for this directory model. | - | ||||||||||||||||||||||||
642 | */ | - | ||||||||||||||||||||||||
643 | - | |||||||||||||||||||||||||
644 | QFileIconProvider *QDirModel::iconProvider() const | - | ||||||||||||||||||||||||
645 | { | - | ||||||||||||||||||||||||
646 | Q_D(const QDirModel); | - | ||||||||||||||||||||||||
647 | return d->iconProvider; never executed: return d->iconProvider; | 0 | ||||||||||||||||||||||||
648 | } | - | ||||||||||||||||||||||||
649 | - | |||||||||||||||||||||||||
650 | /*! | - | ||||||||||||||||||||||||
651 | Sets the name \a filters for the directory model. | - | ||||||||||||||||||||||||
652 | */ | - | ||||||||||||||||||||||||
653 | - | |||||||||||||||||||||||||
654 | void QDirModel::setNameFilters(const QStringList &filters) | - | ||||||||||||||||||||||||
655 | { | - | ||||||||||||||||||||||||
656 | Q_D(QDirModel); | - | ||||||||||||||||||||||||
657 | d->nameFilters = filters; | - | ||||||||||||||||||||||||
658 | emit layoutAboutToBeChanged(); | - | ||||||||||||||||||||||||
659 | if (d->shouldStat)
| 0 | ||||||||||||||||||||||||
660 | refresh(QModelIndex()); never executed: refresh(QModelIndex()); | 0 | ||||||||||||||||||||||||
661 | else | - | ||||||||||||||||||||||||
662 | d->invalidate(); never executed: d->invalidate(); | 0 | ||||||||||||||||||||||||
663 | emit layoutChanged(); | - | ||||||||||||||||||||||||
664 | } never executed: end of block | 0 | ||||||||||||||||||||||||
665 | - | |||||||||||||||||||||||||
666 | /*! | - | ||||||||||||||||||||||||
667 | Returns a list of filters applied to the names in the model. | - | ||||||||||||||||||||||||
668 | */ | - | ||||||||||||||||||||||||
669 | - | |||||||||||||||||||||||||
670 | QStringList QDirModel::nameFilters() const | - | ||||||||||||||||||||||||
671 | { | - | ||||||||||||||||||||||||
672 | Q_D(const QDirModel); | - | ||||||||||||||||||||||||
673 | return d->nameFilters; never executed: return d->nameFilters; | 0 | ||||||||||||||||||||||||
674 | } | - | ||||||||||||||||||||||||
675 | - | |||||||||||||||||||||||||
676 | /*! | - | ||||||||||||||||||||||||
677 | Sets the directory model's filter to that specified by \a filters. | - | ||||||||||||||||||||||||
678 | - | |||||||||||||||||||||||||
679 | Note that the filter you set should always include the QDir::AllDirs enum value, | - | ||||||||||||||||||||||||
680 | otherwise QDirModel won't be able to read the directory structure. | - | ||||||||||||||||||||||||
681 | - | |||||||||||||||||||||||||
682 | \sa QDir::Filters | - | ||||||||||||||||||||||||
683 | */ | - | ||||||||||||||||||||||||
684 | - | |||||||||||||||||||||||||
685 | void QDirModel::setFilter(QDir::Filters filters) | - | ||||||||||||||||||||||||
686 | { | - | ||||||||||||||||||||||||
687 | Q_D(QDirModel); | - | ||||||||||||||||||||||||
688 | d->filters = filters; | - | ||||||||||||||||||||||||
689 | emit layoutAboutToBeChanged(); | - | ||||||||||||||||||||||||
690 | if (d->shouldStat)
| 0 | ||||||||||||||||||||||||
691 | refresh(QModelIndex()); never executed: refresh(QModelIndex()); | 0 | ||||||||||||||||||||||||
692 | else | - | ||||||||||||||||||||||||
693 | d->invalidate(); never executed: d->invalidate(); | 0 | ||||||||||||||||||||||||
694 | emit layoutChanged(); | - | ||||||||||||||||||||||||
695 | } never executed: end of block | 0 | ||||||||||||||||||||||||
696 | - | |||||||||||||||||||||||||
697 | /*! | - | ||||||||||||||||||||||||
698 | Returns the filter specification for the directory model. | - | ||||||||||||||||||||||||
699 | - | |||||||||||||||||||||||||
700 | \sa QDir::Filters | - | ||||||||||||||||||||||||
701 | */ | - | ||||||||||||||||||||||||
702 | - | |||||||||||||||||||||||||
703 | QDir::Filters QDirModel::filter() const | - | ||||||||||||||||||||||||
704 | { | - | ||||||||||||||||||||||||
705 | Q_D(const QDirModel); | - | ||||||||||||||||||||||||
706 | return d->filters; never executed: return d->filters; | 0 | ||||||||||||||||||||||||
707 | } | - | ||||||||||||||||||||||||
708 | - | |||||||||||||||||||||||||
709 | /*! | - | ||||||||||||||||||||||||
710 | Sets the directory model's sorting order to that specified by \a sort. | - | ||||||||||||||||||||||||
711 | - | |||||||||||||||||||||||||
712 | \sa QDir::SortFlags | - | ||||||||||||||||||||||||
713 | */ | - | ||||||||||||||||||||||||
714 | - | |||||||||||||||||||||||||
715 | void QDirModel::setSorting(QDir::SortFlags sort) | - | ||||||||||||||||||||||||
716 | { | - | ||||||||||||||||||||||||
717 | Q_D(QDirModel); | - | ||||||||||||||||||||||||
718 | d->sort = sort; | - | ||||||||||||||||||||||||
719 | emit layoutAboutToBeChanged(); | - | ||||||||||||||||||||||||
720 | if (d->shouldStat)
| 0 | ||||||||||||||||||||||||
721 | refresh(QModelIndex()); never executed: refresh(QModelIndex()); | 0 | ||||||||||||||||||||||||
722 | else | - | ||||||||||||||||||||||||
723 | d->invalidate(); never executed: d->invalidate(); | 0 | ||||||||||||||||||||||||
724 | emit layoutChanged(); | - | ||||||||||||||||||||||||
725 | } never executed: end of block | 0 | ||||||||||||||||||||||||
726 | - | |||||||||||||||||||||||||
727 | /*! | - | ||||||||||||||||||||||||
728 | Returns the sorting method used for the directory model. | - | ||||||||||||||||||||||||
729 | - | |||||||||||||||||||||||||
730 | \sa QDir::SortFlags | - | ||||||||||||||||||||||||
731 | */ | - | ||||||||||||||||||||||||
732 | - | |||||||||||||||||||||||||
733 | QDir::SortFlags QDirModel::sorting() const | - | ||||||||||||||||||||||||
734 | { | - | ||||||||||||||||||||||||
735 | Q_D(const QDirModel); | - | ||||||||||||||||||||||||
736 | return d->sort; never executed: return d->sort; | 0 | ||||||||||||||||||||||||
737 | } | - | ||||||||||||||||||||||||
738 | - | |||||||||||||||||||||||||
739 | /*! | - | ||||||||||||||||||||||||
740 | \property QDirModel::resolveSymlinks | - | ||||||||||||||||||||||||
741 | \brief Whether the directory model should resolve symbolic links | - | ||||||||||||||||||||||||
742 | - | |||||||||||||||||||||||||
743 | This is only relevant on operating systems that support symbolic | - | ||||||||||||||||||||||||
744 | links. | - | ||||||||||||||||||||||||
745 | */ | - | ||||||||||||||||||||||||
746 | void QDirModel::setResolveSymlinks(bool enable) | - | ||||||||||||||||||||||||
747 | { | - | ||||||||||||||||||||||||
748 | Q_D(QDirModel); | - | ||||||||||||||||||||||||
749 | d->resolveSymlinks = enable; | - | ||||||||||||||||||||||||
750 | } never executed: end of block | 0 | ||||||||||||||||||||||||
751 | - | |||||||||||||||||||||||||
752 | bool QDirModel::resolveSymlinks() const | - | ||||||||||||||||||||||||
753 | { | - | ||||||||||||||||||||||||
754 | Q_D(const QDirModel); | - | ||||||||||||||||||||||||
755 | return d->resolveSymlinks; never executed: return d->resolveSymlinks; | 0 | ||||||||||||||||||||||||
756 | } | - | ||||||||||||||||||||||||
757 | - | |||||||||||||||||||||||||
758 | /*! | - | ||||||||||||||||||||||||
759 | \property QDirModel::readOnly | - | ||||||||||||||||||||||||
760 | \brief Whether the directory model allows writing to the file system | - | ||||||||||||||||||||||||
761 | - | |||||||||||||||||||||||||
762 | If this property is set to false, the directory model will allow renaming, copying | - | ||||||||||||||||||||||||
763 | and deleting of files and directories. | - | ||||||||||||||||||||||||
764 | - | |||||||||||||||||||||||||
765 | This property is \c true by default | - | ||||||||||||||||||||||||
766 | */ | - | ||||||||||||||||||||||||
767 | - | |||||||||||||||||||||||||
768 | void QDirModel::setReadOnly(bool enable) | - | ||||||||||||||||||||||||
769 | { | - | ||||||||||||||||||||||||
770 | Q_D(QDirModel); | - | ||||||||||||||||||||||||
771 | d->readOnly = enable; | - | ||||||||||||||||||||||||
772 | } never executed: end of block | 0 | ||||||||||||||||||||||||
773 | - | |||||||||||||||||||||||||
774 | bool QDirModel::isReadOnly() const | - | ||||||||||||||||||||||||
775 | { | - | ||||||||||||||||||||||||
776 | Q_D(const QDirModel); | - | ||||||||||||||||||||||||
777 | return d->readOnly; never executed: return d->readOnly; | 0 | ||||||||||||||||||||||||
778 | } | - | ||||||||||||||||||||||||
779 | - | |||||||||||||||||||||||||
780 | /*! | - | ||||||||||||||||||||||||
781 | \property QDirModel::lazyChildCount | - | ||||||||||||||||||||||||
782 | \brief Whether the directory model optimizes the hasChildren function | - | ||||||||||||||||||||||||
783 | to only check if the item is a directory. | - | ||||||||||||||||||||||||
784 | - | |||||||||||||||||||||||||
785 | If this property is set to false, the directory model will make sure that a directory | - | ||||||||||||||||||||||||
786 | actually containes any files before reporting that it has children. | - | ||||||||||||||||||||||||
787 | Otherwise the directory model will report that an item has children if the item | - | ||||||||||||||||||||||||
788 | is a directory. | - | ||||||||||||||||||||||||
789 | - | |||||||||||||||||||||||||
790 | This property is \c false by default | - | ||||||||||||||||||||||||
791 | */ | - | ||||||||||||||||||||||||
792 | - | |||||||||||||||||||||||||
793 | void QDirModel::setLazyChildCount(bool enable) | - | ||||||||||||||||||||||||
794 | { | - | ||||||||||||||||||||||||
795 | Q_D(QDirModel); | - | ||||||||||||||||||||||||
796 | d->lazyChildCount = enable; | - | ||||||||||||||||||||||||
797 | } never executed: end of block | 0 | ||||||||||||||||||||||||
798 | - | |||||||||||||||||||||||||
799 | bool QDirModel::lazyChildCount() const | - | ||||||||||||||||||||||||
800 | { | - | ||||||||||||||||||||||||
801 | Q_D(const QDirModel); | - | ||||||||||||||||||||||||
802 | return d->lazyChildCount; never executed: return d->lazyChildCount; | 0 | ||||||||||||||||||||||||
803 | } | - | ||||||||||||||||||||||||
804 | - | |||||||||||||||||||||||||
805 | /*! | - | ||||||||||||||||||||||||
806 | QDirModel caches file information. This function updates the | - | ||||||||||||||||||||||||
807 | cache. The \a parent parameter is the directory from which the | - | ||||||||||||||||||||||||
808 | model is updated; the default value will update the model from | - | ||||||||||||||||||||||||
809 | root directory of the file system (the entire model). | - | ||||||||||||||||||||||||
810 | */ | - | ||||||||||||||||||||||||
811 | - | |||||||||||||||||||||||||
812 | void QDirModel::refresh(const QModelIndex &parent) | - | ||||||||||||||||||||||||
813 | { | - | ||||||||||||||||||||||||
814 | Q_D(QDirModel); | - | ||||||||||||||||||||||||
815 | - | |||||||||||||||||||||||||
816 | QDirModelPrivate::QDirNode *n = d->indexValid(parent) ? d->node(parent) : &(d->root);
| 0 | ||||||||||||||||||||||||
817 | - | |||||||||||||||||||||||||
818 | int rows = n->children.count(); | - | ||||||||||||||||||||||||
819 | if (rows == 0) {
| 0 | ||||||||||||||||||||||||
820 | emit layoutAboutToBeChanged(); | - | ||||||||||||||||||||||||
821 | n->stat = true; // make sure that next time we read all the info | - | ||||||||||||||||||||||||
822 | n->populated = false; | - | ||||||||||||||||||||||||
823 | emit layoutChanged(); | - | ||||||||||||||||||||||||
824 | return; never executed: return; | 0 | ||||||||||||||||||||||||
825 | } | - | ||||||||||||||||||||||||
826 | - | |||||||||||||||||||||||||
827 | emit layoutAboutToBeChanged(); | - | ||||||||||||||||||||||||
828 | d->savePersistentIndexes(); | - | ||||||||||||||||||||||||
829 | d->rowsAboutToBeRemoved(parent, 0, rows - 1); | - | ||||||||||||||||||||||||
830 | n->stat = true; // make sure that next time we read all the info | - | ||||||||||||||||||||||||
831 | d->clear(n); | - | ||||||||||||||||||||||||
832 | d->rowsRemoved(parent, 0, rows - 1); | - | ||||||||||||||||||||||||
833 | d->restorePersistentIndexes(); | - | ||||||||||||||||||||||||
834 | emit layoutChanged(); | - | ||||||||||||||||||||||||
835 | } never executed: end of block | 0 | ||||||||||||||||||||||||
836 | - | |||||||||||||||||||||||||
837 | /*! | - | ||||||||||||||||||||||||
838 | \overload | - | ||||||||||||||||||||||||
839 | - | |||||||||||||||||||||||||
840 | Returns the model item index for the given \a path. | - | ||||||||||||||||||||||||
841 | */ | - | ||||||||||||||||||||||||
842 | - | |||||||||||||||||||||||||
843 | QModelIndex QDirModel::index(const QString &path, int column) const | - | ||||||||||||||||||||||||
844 | { | - | ||||||||||||||||||||||||
845 | Q_D(const QDirModel); | - | ||||||||||||||||||||||||
846 | - | |||||||||||||||||||||||||
847 | if (path.isEmpty() || path == QCoreApplication::translate("QFileDialog", "My Computer"))
| 0 | ||||||||||||||||||||||||
848 | return QModelIndex(); never executed: return QModelIndex(); | 0 | ||||||||||||||||||||||||
849 | - | |||||||||||||||||||||||||
850 | QString absolutePath = QDir(path).absolutePath(); | - | ||||||||||||||||||||||||
851 | #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) | - | ||||||||||||||||||||||||
852 | absolutePath = absolutePath.toLower(); | - | ||||||||||||||||||||||||
853 | // On Windows, "filename......." and "filename" are equivalent | - | ||||||||||||||||||||||||
854 | if (absolutePath.endsWith(QLatin1Char('.'))) { | - | ||||||||||||||||||||||||
855 | int i; | - | ||||||||||||||||||||||||
856 | for (i = absolutePath.count() - 1; i >= 0; --i) { | - | ||||||||||||||||||||||||
857 | if (absolutePath.at(i) != QLatin1Char('.')) | - | ||||||||||||||||||||||||
858 | break; | - | ||||||||||||||||||||||||
859 | } | - | ||||||||||||||||||||||||
860 | absolutePath = absolutePath.left(i+1); | - | ||||||||||||||||||||||||
861 | } | - | ||||||||||||||||||||||||
862 | #endif | - | ||||||||||||||||||||||||
863 | - | |||||||||||||||||||||||||
864 | QStringList pathElements = absolutePath.split(QLatin1Char('/'), QString::SkipEmptyParts); | - | ||||||||||||||||||||||||
865 | if ((pathElements.isEmpty() || !QFileInfo::exists(path))
| 0 | ||||||||||||||||||||||||
866 | #if !defined(Q_OS_WIN) || defined(Q_OS_WINCE) | - | ||||||||||||||||||||||||
867 | && path != QLatin1String("/")
| 0 | ||||||||||||||||||||||||
868 | #endif | - | ||||||||||||||||||||||||
869 | ) | - | ||||||||||||||||||||||||
870 | return QModelIndex(); never executed: return QModelIndex(); | 0 | ||||||||||||||||||||||||
871 | - | |||||||||||||||||||||||||
872 | QModelIndex idx; // start with "My Computer" | - | ||||||||||||||||||||||||
873 | if (!d->root.populated) // make sure the root is populated
| 0 | ||||||||||||||||||||||||
874 | d->populate(&d->root); never executed: d->populate(&d->root); | 0 | ||||||||||||||||||||||||
875 | - | |||||||||||||||||||||||||
876 | #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) | - | ||||||||||||||||||||||||
877 | if (absolutePath.startsWith(QLatin1String("//"))) { // UNC path | - | ||||||||||||||||||||||||
878 | QString host = pathElements.first(); | - | ||||||||||||||||||||||||
879 | int r = 0; | - | ||||||||||||||||||||||||
880 | for (; r < d->root.children.count(); ++r) | - | ||||||||||||||||||||||||
881 | if (d->root.children.at(r).info.fileName() == host) | - | ||||||||||||||||||||||||
882 | break; | - | ||||||||||||||||||||||||
883 | bool childAppended = false; | - | ||||||||||||||||||||||||
884 | if (r >= d->root.children.count() && d->allowAppendChild) { | - | ||||||||||||||||||||||||
885 | d->appendChild(&d->root, QLatin1String("//") + host); | - | ||||||||||||||||||||||||
886 | childAppended = true; | - | ||||||||||||||||||||||||
887 | } | - | ||||||||||||||||||||||||
888 | idx = index(r, 0, QModelIndex()); | - | ||||||||||||||||||||||||
889 | pathElements.pop_front(); | - | ||||||||||||||||||||||||
890 | if (childAppended) | - | ||||||||||||||||||||||||
891 | emit const_cast<QDirModel*>(this)->layoutChanged(); | - | ||||||||||||||||||||||||
892 | } else | - | ||||||||||||||||||||||||
893 | #endif | - | ||||||||||||||||||||||||
894 | #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) | - | ||||||||||||||||||||||||
895 | if (pathElements.at(0).endsWith(QLatin1Char(':'))) { | - | ||||||||||||||||||||||||
896 | pathElements[0] += QLatin1Char('/'); | - | ||||||||||||||||||||||||
897 | } | - | ||||||||||||||||||||||||
898 | #else | - | ||||||||||||||||||||||||
899 | // add the "/" item, since it is a valid path element on unix | - | ||||||||||||||||||||||||
900 | pathElements.prepend(QLatin1String("/")); | - | ||||||||||||||||||||||||
901 | #endif | - | ||||||||||||||||||||||||
902 | - | |||||||||||||||||||||||||
903 | for (int i = 0; i < pathElements.count(); ++i) {
| 0 | ||||||||||||||||||||||||
904 | Q_ASSERT(!pathElements.at(i).isEmpty()); | - | ||||||||||||||||||||||||
905 | QString element = pathElements.at(i); | - | ||||||||||||||||||||||||
906 | QDirModelPrivate::QDirNode *parent = (idx.isValid() ? d->node(idx) : &d->root);
| 0 | ||||||||||||||||||||||||
907 | - | |||||||||||||||||||||||||
908 | Q_ASSERT(parent); | - | ||||||||||||||||||||||||
909 | if (!parent->populated)
| 0 | ||||||||||||||||||||||||
910 | d->populate(parent); never executed: d->populate(parent); | 0 | ||||||||||||||||||||||||
911 | - | |||||||||||||||||||||||||
912 | // search for the element in the child nodes first | - | ||||||||||||||||||||||||
913 | int row = -1; | - | ||||||||||||||||||||||||
914 | for (int j = parent->children.count() - 1; j >= 0; --j) {
| 0 | ||||||||||||||||||||||||
915 | const QFileInfo& fi = parent->children.at(j).info; | - | ||||||||||||||||||||||||
916 | QString childFileName; | - | ||||||||||||||||||||||||
917 | childFileName = idx.isValid() ? fi.fileName() : fi.absoluteFilePath();
| 0 | ||||||||||||||||||||||||
918 | #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) | - | ||||||||||||||||||||||||
919 | childFileName = childFileName.toLower(); | - | ||||||||||||||||||||||||
920 | #endif | - | ||||||||||||||||||||||||
921 | if (childFileName == element) {
| 0 | ||||||||||||||||||||||||
922 | if (i == pathElements.count() - 1)
| 0 | ||||||||||||||||||||||||
923 | parent->children[j].stat = true; never executed: parent->children[j].stat = true; | 0 | ||||||||||||||||||||||||
924 | row = j; | - | ||||||||||||||||||||||||
925 | break; never executed: break; | 0 | ||||||||||||||||||||||||
926 | } | - | ||||||||||||||||||||||||
927 | } never executed: end of block | 0 | ||||||||||||||||||||||||
928 | - | |||||||||||||||||||||||||
929 | // we couldn't find the path element, we create a new node since we _know_ that the path is valid | - | ||||||||||||||||||||||||
930 | if (row == -1) {
| 0 | ||||||||||||||||||||||||
931 | #if defined(Q_OS_WINCE) | - | ||||||||||||||||||||||||
932 | QString newPath; | - | ||||||||||||||||||||||||
933 | if (parent->info.isRoot()) | - | ||||||||||||||||||||||||
934 | newPath = parent->info.absoluteFilePath() + element; | - | ||||||||||||||||||||||||
935 | else | - | ||||||||||||||||||||||||
936 | newPath = parent->info.absoluteFilePath() + QLatin1Char('/') + element; | - | ||||||||||||||||||||||||
937 | #else | - | ||||||||||||||||||||||||
938 | QString newPath = parent->info.absoluteFilePath() + QLatin1Char('/') + element; | - | ||||||||||||||||||||||||
939 | #endif | - | ||||||||||||||||||||||||
940 | if (!d->allowAppendChild || !QFileInfo(newPath).isDir())
| 0 | ||||||||||||||||||||||||
941 | return QModelIndex(); never executed: return QModelIndex(); | 0 | ||||||||||||||||||||||||
942 | d->appendChild(parent, newPath); | - | ||||||||||||||||||||||||
943 | row = parent->children.count() - 1; | - | ||||||||||||||||||||||||
944 | if (i == pathElements.count() - 1) // always stat children of the last element
| 0 | ||||||||||||||||||||||||
945 | parent->children[row].stat = true; never executed: parent->children[row].stat = true; | 0 | ||||||||||||||||||||||||
946 | emit const_cast<QDirModel*>(this)->layoutChanged(); | - | ||||||||||||||||||||||||
947 | } never executed: end of block | 0 | ||||||||||||||||||||||||
948 | - | |||||||||||||||||||||||||
949 | Q_ASSERT(row >= 0); | - | ||||||||||||||||||||||||
950 | idx = createIndex(row, 0, static_cast<void*>(&parent->children[row])); | - | ||||||||||||||||||||||||
951 | Q_ASSERT(idx.isValid()); | - | ||||||||||||||||||||||||
952 | } never executed: end of block | 0 | ||||||||||||||||||||||||
953 | - | |||||||||||||||||||||||||
954 | if (column != 0)
| 0 | ||||||||||||||||||||||||
955 | return idx.sibling(idx.row(), column); never executed: return idx.sibling(idx.row(), column); | 0 | ||||||||||||||||||||||||
956 | return idx; never executed: return idx; | 0 | ||||||||||||||||||||||||
957 | } | - | ||||||||||||||||||||||||
958 | - | |||||||||||||||||||||||||
959 | /*! | - | ||||||||||||||||||||||||
960 | Returns \c true if the model item \a index represents a directory; | - | ||||||||||||||||||||||||
961 | otherwise returns \c false. | - | ||||||||||||||||||||||||
962 | */ | - | ||||||||||||||||||||||||
963 | - | |||||||||||||||||||||||||
964 | bool QDirModel::isDir(const QModelIndex &index) const | - | ||||||||||||||||||||||||
965 | { | - | ||||||||||||||||||||||||
966 | Q_D(const QDirModel); | - | ||||||||||||||||||||||||
967 | Q_ASSERT(d->indexValid(index)); | - | ||||||||||||||||||||||||
968 | QDirModelPrivate::QDirNode *node = d->node(index); | - | ||||||||||||||||||||||||
969 | return node->info.isDir(); never executed: return node->info.isDir(); | 0 | ||||||||||||||||||||||||
970 | } | - | ||||||||||||||||||||||||
971 | - | |||||||||||||||||||||||||
972 | /*! | - | ||||||||||||||||||||||||
973 | Create a directory with the \a name in the \a parent model item. | - | ||||||||||||||||||||||||
974 | */ | - | ||||||||||||||||||||||||
975 | - | |||||||||||||||||||||||||
976 | QModelIndex QDirModel::mkdir(const QModelIndex &parent, const QString &name) | - | ||||||||||||||||||||||||
977 | { | - | ||||||||||||||||||||||||
978 | Q_D(QDirModel); | - | ||||||||||||||||||||||||
979 | if (!d->indexValid(parent) || isReadOnly())
| 0 | ||||||||||||||||||||||||
980 | return QModelIndex(); never executed: return QModelIndex(); | 0 | ||||||||||||||||||||||||
981 | - | |||||||||||||||||||||||||
982 | QDirModelPrivate::QDirNode *p = d->node(parent); | - | ||||||||||||||||||||||||
983 | QString path = p->info.absoluteFilePath(); | - | ||||||||||||||||||||||||
984 | // For the indexOf() method to work, the new directory has to be a direct child of | - | ||||||||||||||||||||||||
985 | // the parent directory. | - | ||||||||||||||||||||||||
986 | - | |||||||||||||||||||||||||
987 | QDir newDir(name); | - | ||||||||||||||||||||||||
988 | QDir dir(path); | - | ||||||||||||||||||||||||
989 | if (newDir.isRelative())
| 0 | ||||||||||||||||||||||||
990 | newDir = QDir(path + QLatin1Char('/') + name); never executed: newDir = QDir(path + QLatin1Char('/') + name); | 0 | ||||||||||||||||||||||||
991 | QString childName = newDir.dirName(); // Get the singular name of the directory | - | ||||||||||||||||||||||||
992 | newDir.cdUp(); | - | ||||||||||||||||||||||||
993 | - | |||||||||||||||||||||||||
994 | if (newDir.absolutePath() != dir.absolutePath() || !dir.mkdir(name))
| 0 | ||||||||||||||||||||||||
995 | return QModelIndex(); // nothing happened never executed: return QModelIndex(); | 0 | ||||||||||||||||||||||||
996 | - | |||||||||||||||||||||||||
997 | refresh(parent); | - | ||||||||||||||||||||||||
998 | - | |||||||||||||||||||||||||
999 | QStringList entryList = d->entryList(path); | - | ||||||||||||||||||||||||
1000 | int r = entryList.indexOf(childName); | - | ||||||||||||||||||||||||
1001 | QModelIndex i = index(r, 0, parent); // return an invalid index | - | ||||||||||||||||||||||||
1002 | - | |||||||||||||||||||||||||
1003 | return i; never executed: return i; | 0 | ||||||||||||||||||||||||
1004 | } | - | ||||||||||||||||||||||||
1005 | - | |||||||||||||||||||||||||
1006 | /*! | - | ||||||||||||||||||||||||
1007 | Removes the directory corresponding to the model item \a index in the | - | ||||||||||||||||||||||||
1008 | directory model and \b{deletes the corresponding directory from the | - | ||||||||||||||||||||||||
1009 | file system}, returning true if successful. If the directory cannot be | - | ||||||||||||||||||||||||
1010 | removed, false is returned. | - | ||||||||||||||||||||||||
1011 | - | |||||||||||||||||||||||||
1012 | \warning This function deletes directories from the file system; it does | - | ||||||||||||||||||||||||
1013 | \b{not} move them to a location where they can be recovered. | - | ||||||||||||||||||||||||
1014 | - | |||||||||||||||||||||||||
1015 | \sa remove() | - | ||||||||||||||||||||||||
1016 | */ | - | ||||||||||||||||||||||||
1017 | - | |||||||||||||||||||||||||
1018 | bool QDirModel::rmdir(const QModelIndex &index) | - | ||||||||||||||||||||||||
1019 | { | - | ||||||||||||||||||||||||
1020 | Q_D(QDirModel); | - | ||||||||||||||||||||||||
1021 | if (!d->indexValid(index) || isReadOnly())
| 0 | ||||||||||||||||||||||||
1022 | return false; never executed: return false; | 0 | ||||||||||||||||||||||||
1023 | - | |||||||||||||||||||||||||
1024 | QDirModelPrivate::QDirNode *n = d_func()->node(index); | - | ||||||||||||||||||||||||
1025 | if (!n->info.isDir()) {
| 0 | ||||||||||||||||||||||||
1026 | qWarning("rmdir: the node is not a directory"); | - | ||||||||||||||||||||||||
1027 | return false; never executed: return false; | 0 | ||||||||||||||||||||||||
1028 | } | - | ||||||||||||||||||||||||
1029 | - | |||||||||||||||||||||||||
1030 | QModelIndex par = parent(index); | - | ||||||||||||||||||||||||
1031 | QDirModelPrivate::QDirNode *p = d_func()->node(par); | - | ||||||||||||||||||||||||
1032 | QDir dir = p->info.dir(); // parent dir | - | ||||||||||||||||||||||||
1033 | QString path = n->info.absoluteFilePath(); | - | ||||||||||||||||||||||||
1034 | if (!dir.rmdir(path))
| 0 | ||||||||||||||||||||||||
1035 | return false; never executed: return false; | 0 | ||||||||||||||||||||||||
1036 | - | |||||||||||||||||||||||||
1037 | refresh(par); | - | ||||||||||||||||||||||||
1038 | - | |||||||||||||||||||||||||
1039 | return true; never executed: return true; | 0 | ||||||||||||||||||||||||
1040 | } | - | ||||||||||||||||||||||||
1041 | - | |||||||||||||||||||||||||
1042 | /*! | - | ||||||||||||||||||||||||
1043 | Removes the model item \a index from the directory model and \b{deletes the | - | ||||||||||||||||||||||||
1044 | corresponding file from the file system}, returning true if successful. If the | - | ||||||||||||||||||||||||
1045 | item cannot be removed, false is returned. | - | ||||||||||||||||||||||||
1046 | - | |||||||||||||||||||||||||
1047 | \warning This function deletes files from the file system; it does \b{not} | - | ||||||||||||||||||||||||
1048 | move them to a location where they can be recovered. | - | ||||||||||||||||||||||||
1049 | - | |||||||||||||||||||||||||
1050 | \sa rmdir() | - | ||||||||||||||||||||||||
1051 | */ | - | ||||||||||||||||||||||||
1052 | - | |||||||||||||||||||||||||
1053 | bool QDirModel::remove(const QModelIndex &index) | - | ||||||||||||||||||||||||
1054 | { | - | ||||||||||||||||||||||||
1055 | Q_D(QDirModel); | - | ||||||||||||||||||||||||
1056 | if (!d->indexValid(index) || isReadOnly())
| 0 | ||||||||||||||||||||||||
1057 | return false; never executed: return false; | 0 | ||||||||||||||||||||||||
1058 | - | |||||||||||||||||||||||||
1059 | QDirModelPrivate::QDirNode *n = d_func()->node(index); | - | ||||||||||||||||||||||||
1060 | if (n->info.isDir())
| 0 | ||||||||||||||||||||||||
1061 | return false; never executed: return false; | 0 | ||||||||||||||||||||||||
1062 | - | |||||||||||||||||||||||||
1063 | QModelIndex par = parent(index); | - | ||||||||||||||||||||||||
1064 | QDirModelPrivate::QDirNode *p = d_func()->node(par); | - | ||||||||||||||||||||||||
1065 | QDir dir = p->info.dir(); // parent dir | - | ||||||||||||||||||||||||
1066 | QString path = n->info.absoluteFilePath(); | - | ||||||||||||||||||||||||
1067 | if (!dir.remove(path))
| 0 | ||||||||||||||||||||||||
1068 | return false; never executed: return false; | 0 | ||||||||||||||||||||||||
1069 | - | |||||||||||||||||||||||||
1070 | refresh(par); | - | ||||||||||||||||||||||||
1071 | - | |||||||||||||||||||||||||
1072 | return true; never executed: return true; | 0 | ||||||||||||||||||||||||
1073 | } | - | ||||||||||||||||||||||||
1074 | - | |||||||||||||||||||||||||
1075 | /*! | - | ||||||||||||||||||||||||
1076 | Returns the path of the item stored in the model under the | - | ||||||||||||||||||||||||
1077 | \a index given. | - | ||||||||||||||||||||||||
1078 | - | |||||||||||||||||||||||||
1079 | */ | - | ||||||||||||||||||||||||
1080 | - | |||||||||||||||||||||||||
1081 | QString QDirModel::filePath(const QModelIndex &index) const | - | ||||||||||||||||||||||||
1082 | { | - | ||||||||||||||||||||||||
1083 | Q_D(const QDirModel); | - | ||||||||||||||||||||||||
1084 | if (d->indexValid(index)) {
| 0 | ||||||||||||||||||||||||
1085 | QFileInfo fi = fileInfo(index); | - | ||||||||||||||||||||||||
1086 | if (d->resolveSymlinks && fi.isSymLink())
| 0 | ||||||||||||||||||||||||
1087 | fi = d->resolvedInfo(fi); never executed: fi = d->resolvedInfo(fi); | 0 | ||||||||||||||||||||||||
1088 | return QDir::cleanPath(fi.absoluteFilePath()); never executed: return QDir::cleanPath(fi.absoluteFilePath()); | 0 | ||||||||||||||||||||||||
1089 | } | - | ||||||||||||||||||||||||
1090 | return QString(); // root path never executed: return QString(); | 0 | ||||||||||||||||||||||||
1091 | } | - | ||||||||||||||||||||||||
1092 | - | |||||||||||||||||||||||||
1093 | /*! | - | ||||||||||||||||||||||||
1094 | Returns the name of the item stored in the model under the | - | ||||||||||||||||||||||||
1095 | \a index given. | - | ||||||||||||||||||||||||
1096 | - | |||||||||||||||||||||||||
1097 | */ | - | ||||||||||||||||||||||||
1098 | - | |||||||||||||||||||||||||
1099 | QString QDirModel::fileName(const QModelIndex &index) const | - | ||||||||||||||||||||||||
1100 | { | - | ||||||||||||||||||||||||
1101 | Q_D(const QDirModel); | - | ||||||||||||||||||||||||
1102 | if (!d->indexValid(index))
| 0 | ||||||||||||||||||||||||
1103 | return QString(); never executed: return QString(); | 0 | ||||||||||||||||||||||||
1104 | QFileInfo info = fileInfo(index); | - | ||||||||||||||||||||||||
1105 | if (info.isRoot())
| 0 | ||||||||||||||||||||||||
1106 | return info.absoluteFilePath(); never executed: return info.absoluteFilePath(); | 0 | ||||||||||||||||||||||||
1107 | if (d->resolveSymlinks && info.isSymLink())
| 0 | ||||||||||||||||||||||||
1108 | info = d->resolvedInfo(info); never executed: info = d->resolvedInfo(info); | 0 | ||||||||||||||||||||||||
1109 | return info.fileName(); never executed: return info.fileName(); | 0 | ||||||||||||||||||||||||
1110 | } | - | ||||||||||||||||||||||||
1111 | - | |||||||||||||||||||||||||
1112 | /*! | - | ||||||||||||||||||||||||
1113 | Returns the icons for the item stored in the model under the given | - | ||||||||||||||||||||||||
1114 | \a index. | - | ||||||||||||||||||||||||
1115 | */ | - | ||||||||||||||||||||||||
1116 | - | |||||||||||||||||||||||||
1117 | QIcon QDirModel::fileIcon(const QModelIndex &index) const | - | ||||||||||||||||||||||||
1118 | { | - | ||||||||||||||||||||||||
1119 | Q_D(const QDirModel); | - | ||||||||||||||||||||||||
1120 | if (!d->indexValid(index))
| 0 | ||||||||||||||||||||||||
1121 | return d->iconProvider->icon(QFileIconProvider::Computer); never executed: return d->iconProvider->icon(QFileIconProvider::Computer); | 0 | ||||||||||||||||||||||||
1122 | QDirModelPrivate::QDirNode *node = d->node(index); | - | ||||||||||||||||||||||||
1123 | if (node->icon.isNull())
| 0 | ||||||||||||||||||||||||
1124 | node->icon = d->iconProvider->icon(node->info); never executed: node->icon = d->iconProvider->icon(node->info); | 0 | ||||||||||||||||||||||||
1125 | return node->icon; never executed: return node->icon; | 0 | ||||||||||||||||||||||||
1126 | } | - | ||||||||||||||||||||||||
1127 | - | |||||||||||||||||||||||||
1128 | /*! | - | ||||||||||||||||||||||||
1129 | Returns the file information for the specified model \a index. | - | ||||||||||||||||||||||||
1130 | - | |||||||||||||||||||||||||
1131 | \b{Note:} If the model index represents a symbolic link in the | - | ||||||||||||||||||||||||
1132 | underlying filing system, the file information returned will contain | - | ||||||||||||||||||||||||
1133 | information about the symbolic link itself, regardless of whether | - | ||||||||||||||||||||||||
1134 | resolveSymlinks is enabled or not. | - | ||||||||||||||||||||||||
1135 | - | |||||||||||||||||||||||||
1136 | \sa QFileInfo::symLinkTarget() | - | ||||||||||||||||||||||||
1137 | */ | - | ||||||||||||||||||||||||
1138 | - | |||||||||||||||||||||||||
1139 | QFileInfo QDirModel::fileInfo(const QModelIndex &index) const | - | ||||||||||||||||||||||||
1140 | { | - | ||||||||||||||||||||||||
1141 | Q_D(const QDirModel); | - | ||||||||||||||||||||||||
1142 | Q_ASSERT(d->indexValid(index)); | - | ||||||||||||||||||||||||
1143 | - | |||||||||||||||||||||||||
1144 | QDirModelPrivate::QDirNode *node = d->node(index); | - | ||||||||||||||||||||||||
1145 | return node->info; never executed: return node->info; | 0 | ||||||||||||||||||||||||
1146 | } | - | ||||||||||||||||||||||||
1147 | - | |||||||||||||||||||||||||
1148 | /* | - | ||||||||||||||||||||||||
1149 | The root node is never seen outside the model. | - | ||||||||||||||||||||||||
1150 | */ | - | ||||||||||||||||||||||||
1151 | - | |||||||||||||||||||||||||
1152 | void QDirModelPrivate::init() | - | ||||||||||||||||||||||||
1153 | { | - | ||||||||||||||||||||||||
1154 | filters = QDir::AllEntries | QDir::NoDotAndDotDot; | - | ||||||||||||||||||||||||
1155 | sort = QDir::Name; | - | ||||||||||||||||||||||||
1156 | nameFilters << QLatin1String("*"); | - | ||||||||||||||||||||||||
1157 | root.parent = 0; | - | ||||||||||||||||||||||||
1158 | root.info = QFileInfo(); | - | ||||||||||||||||||||||||
1159 | clear(&root); | - | ||||||||||||||||||||||||
1160 | roleNames.insertMulti(QDirModel::FileIconRole, QByteArrayLiteral("fileIcon")); // == Qt::decoration never executed: return ba; | 0 | ||||||||||||||||||||||||
1161 | roleNames.insert(QDirModel::FilePathRole, QByteArrayLiteral("filePath")); never executed: return ba; | 0 | ||||||||||||||||||||||||
1162 | roleNames.insert(QDirModel::FileNameRole, QByteArrayLiteral("fileName")); never executed: return ba; | 0 | ||||||||||||||||||||||||
1163 | } never executed: end of block | 0 | ||||||||||||||||||||||||
1164 | - | |||||||||||||||||||||||||
1165 | QDirModelPrivate::QDirNode *QDirModelPrivate::node(int row, QDirNode *parent) const | - | ||||||||||||||||||||||||
1166 | { | - | ||||||||||||||||||||||||
1167 | if (row < 0)
| 0 | ||||||||||||||||||||||||
1168 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
1169 | - | |||||||||||||||||||||||||
1170 | bool isDir = !parent || parent->info.isDir();
| 0 | ||||||||||||||||||||||||
1171 | QDirNode *p = (parent ? parent : &root);
| 0 | ||||||||||||||||||||||||
1172 | if (isDir && !p->populated)
| 0 | ||||||||||||||||||||||||
1173 | populate(p); // will also resolve symlinks never executed: populate(p); | 0 | ||||||||||||||||||||||||
1174 | - | |||||||||||||||||||||||||
1175 | if (row >= p->children.count()) {
| 0 | ||||||||||||||||||||||||
1176 | qWarning("node: the row does not exist"); | - | ||||||||||||||||||||||||
1177 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
1178 | } | - | ||||||||||||||||||||||||
1179 | - | |||||||||||||||||||||||||
1180 | return const_cast<QDirNode*>(&p->children.at(row)); never executed: return const_cast<QDirNode*>(&p->children.at(row)); | 0 | ||||||||||||||||||||||||
1181 | } | - | ||||||||||||||||||||||||
1182 | - | |||||||||||||||||||||||||
1183 | QVector<QDirModelPrivate::QDirNode> QDirModelPrivate::children(QDirNode *parent, bool stat) const | - | ||||||||||||||||||||||||
1184 | { | - | ||||||||||||||||||||||||
1185 | Q_ASSERT(parent); | - | ||||||||||||||||||||||||
1186 | QFileInfoList infoList; | - | ||||||||||||||||||||||||
1187 | if (parent == &root) {
| 0 | ||||||||||||||||||||||||
1188 | parent = 0; | - | ||||||||||||||||||||||||
1189 | infoList = QDir::drives(); | - | ||||||||||||||||||||||||
1190 | } else if (parent->info.isDir()) { never executed: end of block
| 0 | ||||||||||||||||||||||||
1191 | //resolve directory links only if requested. | - | ||||||||||||||||||||||||
1192 | if (parent->info.isSymLink() && resolveSymlinks) {
| 0 | ||||||||||||||||||||||||
1193 | QString link = parent->info.symLinkTarget(); | - | ||||||||||||||||||||||||
1194 | if (link.size() > 1 && link.at(link.size() - 1) == QDir::separator())
| 0 | ||||||||||||||||||||||||
1195 | link.chop(1); never executed: link.chop(1); | 0 | ||||||||||||||||||||||||
1196 | if (stat)
| 0 | ||||||||||||||||||||||||
1197 | infoList = entryInfoList(link); never executed: infoList = entryInfoList(link); | 0 | ||||||||||||||||||||||||
1198 | else | - | ||||||||||||||||||||||||
1199 | infoList = QDir(link).entryInfoList(nameFilters, QDir::AllEntries | QDir::System); never executed: infoList = QDir(link).entryInfoList(nameFilters, QDir::AllEntries | QDir::System); | 0 | ||||||||||||||||||||||||
1200 | } else { | - | ||||||||||||||||||||||||
1201 | if (stat)
| 0 | ||||||||||||||||||||||||
1202 | infoList = entryInfoList(parent->info.absoluteFilePath()); never executed: infoList = entryInfoList(parent->info.absoluteFilePath()); | 0 | ||||||||||||||||||||||||
1203 | else | - | ||||||||||||||||||||||||
1204 | infoList = QDir(parent->info.absoluteFilePath()).entryInfoList(nameFilters, QDir::AllEntries | QDir::System); never executed: infoList = QDir(parent->info.absoluteFilePath()).entryInfoList(nameFilters, QDir::AllEntries | QDir::System); | 0 | ||||||||||||||||||||||||
1205 | } | - | ||||||||||||||||||||||||
1206 | } | - | ||||||||||||||||||||||||
1207 | - | |||||||||||||||||||||||||
1208 | QVector<QDirNode> nodes(infoList.count()); | - | ||||||||||||||||||||||||
1209 | for (int i = 0; i < infoList.count(); ++i) {
| 0 | ||||||||||||||||||||||||
1210 | QDirNode &node = nodes[i]; | - | ||||||||||||||||||||||||
1211 | node.parent = parent; | - | ||||||||||||||||||||||||
1212 | node.info = infoList.at(i); | - | ||||||||||||||||||||||||
1213 | node.populated = false; | - | ||||||||||||||||||||||||
1214 | node.stat = shouldStat; | - | ||||||||||||||||||||||||
1215 | } never executed: end of block | 0 | ||||||||||||||||||||||||
1216 | - | |||||||||||||||||||||||||
1217 | return nodes; never executed: return nodes; | 0 | ||||||||||||||||||||||||
1218 | } | - | ||||||||||||||||||||||||
1219 | - | |||||||||||||||||||||||||
1220 | void QDirModelPrivate::_q_refresh() | - | ||||||||||||||||||||||||
1221 | { | - | ||||||||||||||||||||||||
1222 | Q_Q(QDirModel); | - | ||||||||||||||||||||||||
1223 | q->refresh(toBeRefreshed); | - | ||||||||||||||||||||||||
1224 | toBeRefreshed = QModelIndex(); | - | ||||||||||||||||||||||||
1225 | } never executed: end of block | 0 | ||||||||||||||||||||||||
1226 | - | |||||||||||||||||||||||||
1227 | void QDirModelPrivate::savePersistentIndexes() | - | ||||||||||||||||||||||||
1228 | { | - | ||||||||||||||||||||||||
1229 | Q_Q(QDirModel); | - | ||||||||||||||||||||||||
1230 | savedPersistent.clear(); | - | ||||||||||||||||||||||||
1231 | foreach (QPersistentModelIndexData *data, persistent.indexes) { | - | ||||||||||||||||||||||||
1232 | SavedPersistent saved; | - | ||||||||||||||||||||||||
1233 | QModelIndex index = data->index; | - | ||||||||||||||||||||||||
1234 | saved.path = q->filePath(index); | - | ||||||||||||||||||||||||
1235 | saved.column = index.column(); | - | ||||||||||||||||||||||||
1236 | saved.data = data; | - | ||||||||||||||||||||||||
1237 | saved.index = index; | - | ||||||||||||||||||||||||
1238 | savedPersistent.append(saved); | - | ||||||||||||||||||||||||
1239 | } never executed: end of block | 0 | ||||||||||||||||||||||||
1240 | } never executed: end of block | 0 | ||||||||||||||||||||||||
1241 | - | |||||||||||||||||||||||||
1242 | void QDirModelPrivate::restorePersistentIndexes() | - | ||||||||||||||||||||||||
1243 | { | - | ||||||||||||||||||||||||
1244 | Q_Q(QDirModel); | - | ||||||||||||||||||||||||
1245 | bool allow = allowAppendChild; | - | ||||||||||||||||||||||||
1246 | allowAppendChild = false; | - | ||||||||||||||||||||||||
1247 | for (int i = 0; i < savedPersistent.count(); ++i) {
| 0 | ||||||||||||||||||||||||
1248 | QPersistentModelIndexData *data = savedPersistent.at(i).data; | - | ||||||||||||||||||||||||
1249 | QString path = savedPersistent.at(i).path; | - | ||||||||||||||||||||||||
1250 | int column = savedPersistent.at(i).column; | - | ||||||||||||||||||||||||
1251 | QModelIndex idx = q->index(path, column); | - | ||||||||||||||||||||||||
1252 | if (idx != data->index || data->model == 0) {
| 0 | ||||||||||||||||||||||||
1253 | //data->model may be equal to 0 if the model is getting destroyed | - | ||||||||||||||||||||||||
1254 | persistent.indexes.remove(data->index); | - | ||||||||||||||||||||||||
1255 | data->index = idx; | - | ||||||||||||||||||||||||
1256 | data->model = q; | - | ||||||||||||||||||||||||
1257 | if (idx.isValid())
| 0 | ||||||||||||||||||||||||
1258 | persistent.indexes.insert(idx, data); never executed: persistent.indexes.insert(idx, data); | 0 | ||||||||||||||||||||||||
1259 | } never executed: end of block | 0 | ||||||||||||||||||||||||
1260 | } never executed: end of block | 0 | ||||||||||||||||||||||||
1261 | savedPersistent.clear(); | - | ||||||||||||||||||||||||
1262 | allowAppendChild = allow; | - | ||||||||||||||||||||||||
1263 | } never executed: end of block | 0 | ||||||||||||||||||||||||
1264 | - | |||||||||||||||||||||||||
1265 | QFileInfoList QDirModelPrivate::entryInfoList(const QString &path) const | - | ||||||||||||||||||||||||
1266 | { | - | ||||||||||||||||||||||||
1267 | const QDir dir(path); | - | ||||||||||||||||||||||||
1268 | return dir.entryInfoList(nameFilters, filters, sort); never executed: return dir.entryInfoList(nameFilters, filters, sort); | 0 | ||||||||||||||||||||||||
1269 | } | - | ||||||||||||||||||||||||
1270 | - | |||||||||||||||||||||||||
1271 | QStringList QDirModelPrivate::entryList(const QString &path) const | - | ||||||||||||||||||||||||
1272 | { | - | ||||||||||||||||||||||||
1273 | const QDir dir(path); | - | ||||||||||||||||||||||||
1274 | return dir.entryList(nameFilters, filters, sort); never executed: return dir.entryList(nameFilters, filters, sort); | 0 | ||||||||||||||||||||||||
1275 | } | - | ||||||||||||||||||||||||
1276 | - | |||||||||||||||||||||||||
1277 | QString QDirModelPrivate::name(const QModelIndex &index) const | - | ||||||||||||||||||||||||
1278 | { | - | ||||||||||||||||||||||||
1279 | const QDirNode *n = node(index); | - | ||||||||||||||||||||||||
1280 | const QFileInfo info = n->info; | - | ||||||||||||||||||||||||
1281 | if (info.isRoot()) {
| 0 | ||||||||||||||||||||||||
1282 | QString name = info.absoluteFilePath(); | - | ||||||||||||||||||||||||
1283 | #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) | - | ||||||||||||||||||||||||
1284 | if (name.startsWith(QLatin1Char('/'))) // UNC host | - | ||||||||||||||||||||||||
1285 | return info.fileName(); | - | ||||||||||||||||||||||||
1286 | if (name.endsWith(QLatin1Char('/'))) | - | ||||||||||||||||||||||||
1287 | name.chop(1); | - | ||||||||||||||||||||||||
1288 | #endif | - | ||||||||||||||||||||||||
1289 | return name; never executed: return name; | 0 | ||||||||||||||||||||||||
1290 | } | - | ||||||||||||||||||||||||
1291 | return info.fileName(); never executed: return info.fileName(); | 0 | ||||||||||||||||||||||||
1292 | } | - | ||||||||||||||||||||||||
1293 | - | |||||||||||||||||||||||||
1294 | QString QDirModelPrivate::size(const QModelIndex &index) const | - | ||||||||||||||||||||||||
1295 | { | - | ||||||||||||||||||||||||
1296 | const QDirNode *n = node(index); | - | ||||||||||||||||||||||||
1297 | if (n->info.isDir()) {
| 0 | ||||||||||||||||||||||||
1298 | #ifdef Q_OS_MAC | - | ||||||||||||||||||||||||
1299 | return QLatin1String("--"); | - | ||||||||||||||||||||||||
1300 | #else | - | ||||||||||||||||||||||||
1301 | return QLatin1String(""); never executed: return QLatin1String(""); | 0 | ||||||||||||||||||||||||
1302 | #endif | - | ||||||||||||||||||||||||
1303 | // Windows - "" | - | ||||||||||||||||||||||||
1304 | // OS X - "--" | - | ||||||||||||||||||||||||
1305 | // Konqueror - "4 KB" | - | ||||||||||||||||||||||||
1306 | // Nautilus - "9 items" (the number of children) | - | ||||||||||||||||||||||||
1307 | } | - | ||||||||||||||||||||||||
1308 | - | |||||||||||||||||||||||||
1309 | // According to the Si standard KB is 1000 bytes, KiB is 1024 | - | ||||||||||||||||||||||||
1310 | // but on windows sizes are calulated by dividing by 1024 so we do what they do. | - | ||||||||||||||||||||||||
1311 | const quint64 kb = 1024; | - | ||||||||||||||||||||||||
1312 | const quint64 mb = 1024 * kb; | - | ||||||||||||||||||||||||
1313 | const quint64 gb = 1024 * mb; | - | ||||||||||||||||||||||||
1314 | const quint64 tb = 1024 * gb; | - | ||||||||||||||||||||||||
1315 | quint64 bytes = n->info.size(); | - | ||||||||||||||||||||||||
1316 | if (bytes >= tb)
| 0 | ||||||||||||||||||||||||
1317 | return QFileSystemModel::tr("%1 TB").arg(QLocale().toString(qreal(bytes) / tb, 'f', 3)); never executed: return QFileSystemModel::tr("%1 TB").arg(QLocale().toString(qreal(bytes) / tb, 'f', 3)); | 0 | ||||||||||||||||||||||||
1318 | if (bytes >= gb)
| 0 | ||||||||||||||||||||||||
1319 | return QFileSystemModel::tr("%1 GB").arg(QLocale().toString(qreal(bytes) / gb, 'f', 2)); never executed: return QFileSystemModel::tr("%1 GB").arg(QLocale().toString(qreal(bytes) / gb, 'f', 2)); | 0 | ||||||||||||||||||||||||
1320 | if (bytes >= mb)
| 0 | ||||||||||||||||||||||||
1321 | return QFileSystemModel::tr("%1 MB").arg(QLocale().toString(qreal(bytes) / mb, 'f', 1)); never executed: return QFileSystemModel::tr("%1 MB").arg(QLocale().toString(qreal(bytes) / mb, 'f', 1)); | 0 | ||||||||||||||||||||||||
1322 | if (bytes >= kb)
| 0 | ||||||||||||||||||||||||
1323 | return QFileSystemModel::tr("%1 KB").arg(QLocale().toString(bytes / kb)); never executed: return QFileSystemModel::tr("%1 KB").arg(QLocale().toString(bytes / kb)); | 0 | ||||||||||||||||||||||||
1324 | return QFileSystemModel::tr("%1 byte(s)").arg(QLocale().toString(bytes)); never executed: return QFileSystemModel::tr("%1 byte(s)").arg(QLocale().toString(bytes)); | 0 | ||||||||||||||||||||||||
1325 | } | - | ||||||||||||||||||||||||
1326 | - | |||||||||||||||||||||||||
1327 | QString QDirModelPrivate::type(const QModelIndex &index) const | - | ||||||||||||||||||||||||
1328 | { | - | ||||||||||||||||||||||||
1329 | return iconProvider->type(node(index)->info); never executed: return iconProvider->type(node(index)->info); | 0 | ||||||||||||||||||||||||
1330 | } | - | ||||||||||||||||||||||||
1331 | - | |||||||||||||||||||||||||
1332 | QString QDirModelPrivate::time(const QModelIndex &index) const | - | ||||||||||||||||||||||||
1333 | { | - | ||||||||||||||||||||||||
1334 | #ifndef QT_NO_DATESTRING | - | ||||||||||||||||||||||||
1335 | return node(index)->info.lastModified().toString(Qt::LocalDate); never executed: return node(index)->info.lastModified().toString(Qt::LocalDate); | 0 | ||||||||||||||||||||||||
1336 | #else | - | ||||||||||||||||||||||||
1337 | Q_UNUSED(index); | - | ||||||||||||||||||||||||
1338 | return QString(); | - | ||||||||||||||||||||||||
1339 | #endif | - | ||||||||||||||||||||||||
1340 | } | - | ||||||||||||||||||||||||
1341 | - | |||||||||||||||||||||||||
1342 | void QDirModelPrivate::appendChild(QDirModelPrivate::QDirNode *parent, const QString &path) const | - | ||||||||||||||||||||||||
1343 | { | - | ||||||||||||||||||||||||
1344 | QDirModelPrivate::QDirNode node; | - | ||||||||||||||||||||||||
1345 | node.populated = false; | - | ||||||||||||||||||||||||
1346 | node.stat = shouldStat; | - | ||||||||||||||||||||||||
1347 | node.parent = (parent == &root ? 0 : parent);
| 0 | ||||||||||||||||||||||||
1348 | node.info = QFileInfo(path); | - | ||||||||||||||||||||||||
1349 | node.info.setCaching(true); | - | ||||||||||||||||||||||||
1350 | - | |||||||||||||||||||||||||
1351 | // The following append(node) may reallocate the vector, thus | - | ||||||||||||||||||||||||
1352 | // we need to update the pointers to the childnodes parent. | - | ||||||||||||||||||||||||
1353 | QDirModelPrivate *that = const_cast<QDirModelPrivate *>(this); | - | ||||||||||||||||||||||||
1354 | that->savePersistentIndexes(); | - | ||||||||||||||||||||||||
1355 | parent->children.append(node); | - | ||||||||||||||||||||||||
1356 | for (int i = 0; i < parent->children.count(); ++i) {
| 0 | ||||||||||||||||||||||||
1357 | QDirNode *childNode = &parent->children[i]; | - | ||||||||||||||||||||||||
1358 | for (int j = 0; j < childNode->children.count(); ++j)
| 0 | ||||||||||||||||||||||||
1359 | childNode->children[j].parent = childNode; never executed: childNode->children[j].parent = childNode; | 0 | ||||||||||||||||||||||||
1360 | } never executed: end of block | 0 | ||||||||||||||||||||||||
1361 | that->restorePersistentIndexes(); | - | ||||||||||||||||||||||||
1362 | } never executed: end of block | 0 | ||||||||||||||||||||||||
1363 | - | |||||||||||||||||||||||||
1364 | QFileInfo QDirModelPrivate::resolvedInfo(QFileInfo info) | - | ||||||||||||||||||||||||
1365 | { | - | ||||||||||||||||||||||||
1366 | #ifdef Q_OS_WIN | - | ||||||||||||||||||||||||
1367 | // On windows, we cannot create a shortcut to a shortcut. | - | ||||||||||||||||||||||||
1368 | return QFileInfo(info.symLinkTarget()); | - | ||||||||||||||||||||||||
1369 | #else | - | ||||||||||||||||||||||||
1370 | QStringList paths; | - | ||||||||||||||||||||||||
1371 | do { | - | ||||||||||||||||||||||||
1372 | QFileInfo link(info.symLinkTarget()); | - | ||||||||||||||||||||||||
1373 | if (link.isRelative())
| 0 | ||||||||||||||||||||||||
1374 | info.setFile(info.absolutePath(), link.filePath()); never executed: info.setFile(info.absolutePath(), link.filePath()); | 0 | ||||||||||||||||||||||||
1375 | else | - | ||||||||||||||||||||||||
1376 | info = link; never executed: info = link; | 0 | ||||||||||||||||||||||||
1377 | if (paths.contains(info.absoluteFilePath()))
| 0 | ||||||||||||||||||||||||
1378 | return QFileInfo(); never executed: return QFileInfo(); | 0 | ||||||||||||||||||||||||
1379 | paths.append(info.absoluteFilePath()); | - | ||||||||||||||||||||||||
1380 | } while (info.isSymLink()); never executed: end of block
| 0 | ||||||||||||||||||||||||
1381 | return info; never executed: return info; | 0 | ||||||||||||||||||||||||
1382 | #endif | - | ||||||||||||||||||||||||
1383 | } | - | ||||||||||||||||||||||||
1384 | - | |||||||||||||||||||||||||
1385 | QT_END_NAMESPACE | - | ||||||||||||||||||||||||
1386 | - | |||||||||||||||||||||||||
1387 | #include "moc_qdirmodel.cpp" | - | ||||||||||||||||||||||||
1388 | - | |||||||||||||||||||||||||
1389 | #endif // QT_NO_DIRMODEL | - | ||||||||||||||||||||||||
Source code | Switch to Preprocessed file |