qdirmodel.cpp

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

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9