Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/itemviews/qdirmodel.cpp |
Switch to Source code | Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | - | |||||||||||||
2 | - | |||||||||||||
3 | - | |||||||||||||
4 | - | |||||||||||||
5 | - | |||||||||||||
6 | class QDirModelPrivate : public QAbstractItemModelPrivate | - | ||||||||||||
7 | { | - | ||||||||||||
8 | inline QDirModel* q_func() { return static_cast<QDirModel *>(q_ptr); } inline const QDirModel* q_func() const { return static_cast<const QDirModel *>(q_ptr); } friend class QDirModel; | - | ||||||||||||
9 | - | |||||||||||||
10 | public: | - | ||||||||||||
11 | struct QDirNode | - | ||||||||||||
12 | { | - | ||||||||||||
13 | QDirNode() : parent(0), populated(false), stat(false) {} | - | ||||||||||||
14 | QDirNode *parent; | - | ||||||||||||
15 | QFileInfo info; | - | ||||||||||||
16 | QIcon icon; | - | ||||||||||||
17 | mutable QVector<QDirNode> children; | - | ||||||||||||
18 | mutable bool populated; | - | ||||||||||||
19 | mutable bool stat; | - | ||||||||||||
20 | }; | - | ||||||||||||
21 | - | |||||||||||||
22 | QDirModelPrivate() | - | ||||||||||||
23 | : resolveSymlinks(true), | - | ||||||||||||
24 | readOnly(true), | - | ||||||||||||
25 | lazyChildCount(false), | - | ||||||||||||
26 | allowAppendChild(true), | - | ||||||||||||
27 | iconProvider(&defaultProvider), | - | ||||||||||||
28 | shouldStat(true) | - | ||||||||||||
29 | { } | - | ||||||||||||
30 | - | |||||||||||||
31 | void init(); | - | ||||||||||||
32 | QDirNode *node(int row, QDirNode *parent) const; | - | ||||||||||||
33 | QVector<QDirNode> children(QDirNode *parent, bool stat) const; | - | ||||||||||||
34 | - | |||||||||||||
35 | void _q_refresh(); | - | ||||||||||||
36 | - | |||||||||||||
37 | void savePersistentIndexes(); | - | ||||||||||||
38 | void restorePersistentIndexes(); | - | ||||||||||||
39 | - | |||||||||||||
40 | QFileInfoList entryInfoList(const QString &path) const; | - | ||||||||||||
41 | QStringList entryList(const QString &path) const; | - | ||||||||||||
42 | - | |||||||||||||
43 | QString name(const QModelIndex &index) const; | - | ||||||||||||
44 | QString size(const QModelIndex &index) const; | - | ||||||||||||
45 | QString type(const QModelIndex &index) const; | - | ||||||||||||
46 | QString time(const QModelIndex &index) const; | - | ||||||||||||
47 | - | |||||||||||||
48 | void appendChild(QDirModelPrivate::QDirNode *parent, const QString &path) const; | - | ||||||||||||
49 | static QFileInfo resolvedInfo(QFileInfo info); | - | ||||||||||||
50 | - | |||||||||||||
51 | inline QDirNode *node(const QModelIndex &index) const; | - | ||||||||||||
52 | inline void populate(QDirNode *parent) const; | - | ||||||||||||
53 | inline void clear(QDirNode *parent) const; | - | ||||||||||||
54 | - | |||||||||||||
55 | void invalidate(); | - | ||||||||||||
56 | - | |||||||||||||
57 | mutable QDirNode root; | - | ||||||||||||
58 | bool resolveSymlinks; | - | ||||||||||||
59 | bool readOnly; | - | ||||||||||||
60 | bool lazyChildCount; | - | ||||||||||||
61 | bool allowAppendChild; | - | ||||||||||||
62 | - | |||||||||||||
63 | QDir::Filters filters; | - | ||||||||||||
64 | QDir::SortFlags sort; | - | ||||||||||||
65 | QStringList nameFilters; | - | ||||||||||||
66 | - | |||||||||||||
67 | QFileIconProvider *iconProvider; | - | ||||||||||||
68 | QFileIconProvider defaultProvider; | - | ||||||||||||
69 | - | |||||||||||||
70 | struct SavedPersistent { | - | ||||||||||||
71 | QString path; | - | ||||||||||||
72 | int column; | - | ||||||||||||
73 | QPersistentModelIndexData *data; | - | ||||||||||||
74 | QPersistentModelIndex index; | - | ||||||||||||
75 | }; | - | ||||||||||||
76 | QListQVector<SavedPersistent> savedPersistent; | - | ||||||||||||
77 | QPersistentModelIndex toBeRefreshed; | - | ||||||||||||
78 | - | |||||||||||||
79 | bool shouldStat; | - | ||||||||||||
80 | }; | - | ||||||||||||
81 | template<> class QTypeInfo<QDirModelPrivate::SavedPersistent > { public: enum { isComplex = (((Q_MOVABLE_TYPE) & Q_PRIMITIVE_TYPE) == 0), isStatic = (((Q_MOVABLE_TYPE) & (Q_MOVABLE_TYPE | Q_PRIMITIVE_TYPE)) == 0), isRelocatable = !isStatic || ((Q_MOVABLE_TYPE) & Q_RELOCATABLE_TYPE), isLarge = (sizeof(QDirModelPrivate::SavedPersistent)>sizeof(void*)), isPointer = false, isIntegral = QtPrivate::is_integral< QDirModelPrivate::SavedPersistent >::value, isDummy = (((Q_MOVABLE_TYPE) & Q_DUMMY_TYPE) != 0), sizeOf = sizeof(QDirModelPrivate::SavedPersistent) }; static inline const char *name() { return "QDirModelPrivate::SavedPersistent"; } }; | - | ||||||||||||
82 | - | |||||||||||||
83 | void qt_setDirModelShouldNotStat(QDirModelPrivate *modelPrivate) | - | ||||||||||||
84 | { | - | ||||||||||||
85 | modelPrivate->shouldStat = false; | - | ||||||||||||
86 | } | - | ||||||||||||
87 | - | |||||||||||||
88 | QDirModelPrivate::QDirNode *QDirModelPrivate::node(const QModelIndex &index) const | - | ||||||||||||
89 | { | - | ||||||||||||
90 | QDirModelPrivate::QDirNode *n = | - | ||||||||||||
91 | static_cast<QDirModelPrivate::QDirNode*>(index.internalPointer()); | - | ||||||||||||
92 | ((!(n)) ? qt_assert("n",__FILE__,146155) : qt_noop()); | - | ||||||||||||
93 | return n; | - | ||||||||||||
94 | } | - | ||||||||||||
95 | - | |||||||||||||
96 | void QDirModelPrivate::populate(QDirNode *parent) const | - | ||||||||||||
97 | { | - | ||||||||||||
98 | ((!(parent)) ? qt_assert("parent",__FILE__,152161) : qt_noop()); | - | ||||||||||||
99 | parent->children = children(parent, parent->stat); | - | ||||||||||||
100 | parent->populated = true; | - | ||||||||||||
101 | } | - | ||||||||||||
102 | - | |||||||||||||
103 | void QDirModelPrivate::clear(QDirNode *parent) const | - | ||||||||||||
104 | { | - | ||||||||||||
105 | ((!(parent)) ? qt_assert("parent",__FILE__,159168) : qt_noop()); | - | ||||||||||||
106 | parent->children.clear(); | - | ||||||||||||
107 | parent->populated = false; | - | ||||||||||||
108 | } | - | ||||||||||||
109 | - | |||||||||||||
110 | void QDirModelPrivate::invalidate() | - | ||||||||||||
111 | { | - | ||||||||||||
112 | QStackstd::stack<const QDirNode*, std::vector<const QDirNode*> > nodes; | - | ||||||||||||
113 | nodes.push(&root); | - | ||||||||||||
114 | while (!nodes.empty()
| 0 | ||||||||||||
115 | const QDirNode *current = nodes.top(); | - | ||||||||||||
116 | nodes.pop(); | - | ||||||||||||
117 | current->stat = false; | - | ||||||||||||
118 | const QVector<QDirNode> children&children = current->children; | - | ||||||||||||
119 | for (int i = 0; i <const auto &child : children.count(); ++i) | - | ||||||||||||
120 | nodes.push(&children.at(i));&child); never executed: nodes.push(&child); | 0 | ||||||||||||
121 | } never executed: end of block | 0 | ||||||||||||
122 | } never executed: end of block | 0 | ||||||||||||
123 | QDirModel::QDirModel(const QStringList &nameFilters, | - | ||||||||||||
124 | QDir::Filters filters, | - | ||||||||||||
125 | QDir::SortFlags sort, | - | ||||||||||||
126 | QObject *parent) | - | ||||||||||||
127 | : QAbstractItemModel(*new QDirModelPrivate, parent) | - | ||||||||||||
128 | { | - | ||||||||||||
129 | QDirModelPrivate * const d = d_func(); | - | ||||||||||||
130 | - | |||||||||||||
131 | d->nameFilters = nameFilters.isEmpty() ? QStringList(QLatin1String("*")) : nameFilters; | - | ||||||||||||
132 | d->filters = filters; | - | ||||||||||||
133 | d->sort = sort; | - | ||||||||||||
134 | d->root.parent = 0; | - | ||||||||||||
135 | d->root.info = QFileInfo(); | - | ||||||||||||
136 | d->clear(&d->root); | - | ||||||||||||
137 | } | - | ||||||||||||
138 | - | |||||||||||||
139 | - | |||||||||||||
140 | - | |||||||||||||
141 | - | |||||||||||||
142 | - | |||||||||||||
143 | QDirModel::QDirModel(QObject *parent) | - | ||||||||||||
144 | : QAbstractItemModel(*new QDirModelPrivate, parent) | - | ||||||||||||
145 | { | - | ||||||||||||
146 | QDirModelPrivate * const d = d_func(); | - | ||||||||||||
147 | d->init(); | - | ||||||||||||
148 | } | - | ||||||||||||
149 | - | |||||||||||||
150 | - | |||||||||||||
151 | - | |||||||||||||
152 | - | |||||||||||||
153 | QDirModel::QDirModel(QDirModelPrivate &dd, QObject *parent) | - | ||||||||||||
154 | : QAbstractItemModel(dd, parent) | - | ||||||||||||
155 | { | - | ||||||||||||
156 | QDirModelPrivate * const d = d_func(); | - | ||||||||||||
157 | d->init(); | - | ||||||||||||
158 | } | - | ||||||||||||
159 | - | |||||||||||||
160 | - | |||||||||||||
161 | - | |||||||||||||
162 | - | |||||||||||||
163 | - | |||||||||||||
164 | QDirModel::~QDirModel() | - | ||||||||||||
165 | { | - | ||||||||||||
166 | - | |||||||||||||
167 | } | - | ||||||||||||
168 | - | |||||||||||||
169 | - | |||||||||||||
170 | - | |||||||||||||
171 | - | |||||||||||||
172 | - | |||||||||||||
173 | - | |||||||||||||
174 | - | |||||||||||||
175 | QModelIndex QDirModel::index(int row, int column, const QModelIndex &parent) const | - | ||||||||||||
176 | { | - | ||||||||||||
177 | const QDirModelPrivate * const d = d_func(); | - | ||||||||||||
178 | - | |||||||||||||
179 | if (column < 0 || column >= columnCount(parent) || row < 0 || parent.column() > 0) | - | ||||||||||||
180 | return QModelIndex(); | - | ||||||||||||
181 | - | |||||||||||||
182 | QDirModelPrivate::QDirNode *p = (d->indexValid(parent) ? d->node(parent) : &d->root); | - | ||||||||||||
183 | ((!(p)) ? qt_assert("p",__FILE__,278288) : qt_noop()); | - | ||||||||||||
184 | if (!p->populated) | - | ||||||||||||
185 | d->populate(p); | - | ||||||||||||
186 | if (row >= p->children.count()) | - | ||||||||||||
187 | return QModelIndex(); | - | ||||||||||||
188 | - | |||||||||||||
189 | QDirModelPrivate::QDirNode *n = d->node(row, d->indexValid(parent) ? p : 0); | - | ||||||||||||
190 | ((!(n)) ? qt_assert("n",__FILE__,285295) : qt_noop()); | - | ||||||||||||
191 | - | |||||||||||||
192 | return createIndex(row, column, n); | - | ||||||||||||
193 | } | - | ||||||||||||
194 | - | |||||||||||||
195 | - | |||||||||||||
196 | - | |||||||||||||
197 | - | |||||||||||||
198 | - | |||||||||||||
199 | QModelIndex QDirModel::parent(const QModelIndex &child) const | - | ||||||||||||
200 | { | - | ||||||||||||
201 | const QDirModelPrivate * const d = d_func(); | - | ||||||||||||
202 | - | |||||||||||||
203 | if (!d->indexValid(child)) | - | ||||||||||||
204 | return QModelIndex(); | - | ||||||||||||
205 | QDirModelPrivate::QDirNode *node = d->node(child); | - | ||||||||||||
206 | QDirModelPrivate::QDirNode *par = (node ? node->parent : 0); | - | ||||||||||||
207 | if (par == 0) | - | ||||||||||||
208 | return QModelIndex(); | - | ||||||||||||
209 | - | |||||||||||||
210 | - | |||||||||||||
211 | const QVector<QDirModelPrivate::QDirNode> children = | - | ||||||||||||
212 | par->parent ? par->parent->children : d->root.children; | - | ||||||||||||
213 | ((!(children.count() > 0)) ? qt_assert("children.count() > 0",__FILE__,308318) : qt_noop()); | - | ||||||||||||
214 | int row = (par - &(children.at(0))); | - | ||||||||||||
215 | ((!(row >= 0)) ? qt_assert("row >= 0",__FILE__,310320) : qt_noop()); | - | ||||||||||||
216 | - | |||||||||||||
217 | return createIndex(row, 0, par); | - | ||||||||||||
218 | } | - | ||||||||||||
219 | - | |||||||||||||
220 | - | |||||||||||||
221 | - | |||||||||||||
222 | - | |||||||||||||
223 | - | |||||||||||||
224 | - | |||||||||||||
225 | int QDirModel::rowCount(const QModelIndex &parent) const | - | ||||||||||||
226 | { | - | ||||||||||||
227 | const QDirModelPrivate * const d = d_func(); | - | ||||||||||||
228 | if (parent.column() > 0) | - | ||||||||||||
229 | return 0; | - | ||||||||||||
230 | - | |||||||||||||
231 | if (!parent.isValid()) { | - | ||||||||||||
232 | if (!d->root.populated) | - | ||||||||||||
233 | d->populate(&d->root); | - | ||||||||||||
234 | return d->root.children.count(); | - | ||||||||||||
235 | } | - | ||||||||||||
236 | if (parent.model() != this) | - | ||||||||||||
237 | return 0; | - | ||||||||||||
238 | QDirModelPrivate::QDirNode *p = d->node(parent); | - | ||||||||||||
239 | if (p->info.isDir() && !p->populated) | - | ||||||||||||
240 | d->populate(p); | - | ||||||||||||
241 | return p->children.count(); | - | ||||||||||||
242 | } | - | ||||||||||||
243 | - | |||||||||||||
244 | - | |||||||||||||
245 | - | |||||||||||||
246 | - | |||||||||||||
247 | - | |||||||||||||
248 | - | |||||||||||||
249 | int QDirModel::columnCount(const QModelIndex &parent) const | - | ||||||||||||
250 | { | - | ||||||||||||
251 | if (parent.column() > 0) | - | ||||||||||||
252 | return 0; | - | ||||||||||||
253 | return 4; | - | ||||||||||||
254 | } | - | ||||||||||||
255 | - | |||||||||||||
256 | - | |||||||||||||
257 | - | |||||||||||||
258 | - | |||||||||||||
259 | QVariant QDirModel::data(const QModelIndex &index, int role) const | - | ||||||||||||
260 | { | - | ||||||||||||
261 | const QDirModelPrivate * const d = d_func(); | - | ||||||||||||
262 | if (!d->indexValid(index)) | - | ||||||||||||
263 | return QVariant(); | - | ||||||||||||
264 | - | |||||||||||||
265 | if (role == Qt::DisplayRole || role == Qt::EditRole) { | - | ||||||||||||
266 | switch (index.column()) { | - | ||||||||||||
267 | case 0: return d->name(index); | - | ||||||||||||
268 | case 1: return d->size(index); | - | ||||||||||||
269 | case 2: return d->type(index); | - | ||||||||||||
270 | case 3: return d->time(index); | - | ||||||||||||
271 | default: | - | ||||||||||||
272 | QMessageLogger(__FILE__, 367377, __PRETTY_FUNCTION__).warning("data: invalid display value column %d", index.column()); | - | ||||||||||||
273 | return QVariant(); | - | ||||||||||||
274 | } | - | ||||||||||||
275 | } | - | ||||||||||||
276 | - | |||||||||||||
277 | if (index.column() == 0) { | - | ||||||||||||
278 | if (role == FileIconRole) | - | ||||||||||||
279 | return fileIcon(index); | - | ||||||||||||
280 | if (role == FilePathRole) | - | ||||||||||||
281 | return filePath(index); | - | ||||||||||||
282 | if (role == FileNameRole) | - | ||||||||||||
283 | return fileName(index); | - | ||||||||||||
284 | } | - | ||||||||||||
285 | - | |||||||||||||
286 | if (index.column() == 1 && Qt::TextAlignmentRole == role) { | - | ||||||||||||
287 | return Qt::AlignRight; | - | ||||||||||||
288 | } | - | ||||||||||||
289 | return QVariant(); | - | ||||||||||||
290 | } | - | ||||||||||||
291 | bool QDirModel::setData(const QModelIndex &index, const QVariant &value, int role) | - | ||||||||||||
292 | { | - | ||||||||||||
293 | QDirModelPrivate * const d = d_func(); | - | ||||||||||||
294 | if (!d->indexValid(index) || index.column() != 0 | - | ||||||||||||
295 | || (flags(index) & Qt::ItemIsEditable) == 0 || role != Qt::EditRole) | - | ||||||||||||
296 | return false; | - | ||||||||||||
297 | - | |||||||||||||
298 | QDirModelPrivate::QDirNode *node = d->node(index); | - | ||||||||||||
299 | QDir dir = node->info.dir(); | - | ||||||||||||
300 | QString name = value.toString(); | - | ||||||||||||
301 | if (dir.rename(node->info.fileName(), name)) { | - | ||||||||||||
302 | node->info = QFileInfo(dir, name); | - | ||||||||||||
303 | QModelIndex sibling = index.sibling(index.row(), 3); | - | ||||||||||||
304 | dataChanged(index, sibling); | - | ||||||||||||
305 | - | |||||||||||||
306 | d->toBeRefreshed = index.parent(); | - | ||||||||||||
307 | QMetaObject::invokeMethod(this, "_q_refresh", Qt::QueuedConnection); | - | ||||||||||||
308 | - | |||||||||||||
309 | return true; | - | ||||||||||||
310 | } | - | ||||||||||||
311 | - | |||||||||||||
312 | return false; | - | ||||||||||||
313 | } | - | ||||||||||||
314 | - | |||||||||||||
315 | - | |||||||||||||
316 | - | |||||||||||||
317 | - | |||||||||||||
318 | - | |||||||||||||
319 | - | |||||||||||||
320 | QVariant QDirModel::headerData(int section, Qt::Orientation orientation, int role) const | - | ||||||||||||
321 | { | - | ||||||||||||
322 | if (orientation == Qt::Horizontal) { | - | ||||||||||||
323 | if (role != Qt::DisplayRole) | - | ||||||||||||
324 | return QVariant(); | - | ||||||||||||
325 | switch (section) { | - | ||||||||||||
326 | case 0: return tr("Name"); | - | ||||||||||||
327 | case 1: return tr("Size"); | - | ||||||||||||
328 | case 2: return | - | ||||||||||||
329 | - | |||||||||||||
330 | - | |||||||||||||
331 | - | |||||||||||||
332 | tr("Type", "All other platforms"); | - | ||||||||||||
333 | - | |||||||||||||
334 | - | |||||||||||||
335 | - | |||||||||||||
336 | - | |||||||||||||
337 | - | |||||||||||||
338 | case 3: return tr("Date Modified"); | - | ||||||||||||
339 | default: return QVariant(); | - | ||||||||||||
340 | } | - | ||||||||||||
341 | } | - | ||||||||||||
342 | return QAbstractItemModel::headerData(section, orientation, role); | - | ||||||||||||
343 | } | - | ||||||||||||
344 | - | |||||||||||||
345 | - | |||||||||||||
346 | - | |||||||||||||
347 | - | |||||||||||||
348 | - | |||||||||||||
349 | - | |||||||||||||
350 | bool QDirModel::hasChildren(const QModelIndex &parent) const | - | ||||||||||||
351 | { | - | ||||||||||||
352 | const QDirModelPrivate * const d = d_func(); | - | ||||||||||||
353 | if (parent.column() > 0) | - | ||||||||||||
354 | return false; | - | ||||||||||||
355 | - | |||||||||||||
356 | if (!parent.isValid()) | - | ||||||||||||
357 | return true; | - | ||||||||||||
358 | QDirModelPrivate::QDirNode *p = d->node(parent); | - | ||||||||||||
359 | ((!(p)) ? qt_assert("p",__FILE__,463473) : qt_noop()); | - | ||||||||||||
360 | - | |||||||||||||
361 | if (d->lazyChildCount) | - | ||||||||||||
362 | return p->info.isDir(); | - | ||||||||||||
363 | return p->info.isDir() && rowCount(parent) > 0; | - | ||||||||||||
364 | } | - | ||||||||||||
365 | - | |||||||||||||
366 | - | |||||||||||||
367 | - | |||||||||||||
368 | - | |||||||||||||
369 | - | |||||||||||||
370 | - | |||||||||||||
371 | Qt::ItemFlags QDirModel::flags(const QModelIndex &index) const | - | ||||||||||||
372 | { | - | ||||||||||||
373 | const QDirModelPrivate * const d = d_func(); | - | ||||||||||||
374 | Qt::ItemFlags flags = QAbstractItemModel::flags(index); | - | ||||||||||||
375 | if (!d->indexValid(index)) | - | ||||||||||||
376 | return flags; | - | ||||||||||||
377 | flags |= Qt::ItemIsDragEnabled; | - | ||||||||||||
378 | if (d->readOnly) | - | ||||||||||||
379 | return flags; | - | ||||||||||||
380 | QDirModelPrivate::QDirNode *node = d->node(index); | - | ||||||||||||
381 | if ((index.column() == 0) && node->info.isWritable()) { | - | ||||||||||||
382 | flags |= Qt::ItemIsEditable; | - | ||||||||||||
383 | if (fileInfo(index).isDir()) | - | ||||||||||||
384 | flags |= Qt::ItemIsDropEnabled; | - | ||||||||||||
385 | } | - | ||||||||||||
386 | return flags; | - | ||||||||||||
387 | } | - | ||||||||||||
388 | - | |||||||||||||
389 | - | |||||||||||||
390 | - | |||||||||||||
391 | - | |||||||||||||
392 | - | |||||||||||||
393 | - | |||||||||||||
394 | void QDirModel::sort(int column, Qt::SortOrder order) | - | ||||||||||||
395 | { | - | ||||||||||||
396 | QDir::SortFlags sort = QDir::DirsFirst | QDir::IgnoreCase; | - | ||||||||||||
397 | if (order == Qt::DescendingOrder) | - | ||||||||||||
398 | sort |= QDir::Reversed; | - | ||||||||||||
399 | - | |||||||||||||
400 | switch (column) { | - | ||||||||||||
401 | case 0: | - | ||||||||||||
402 | sort |= QDir::Name; | - | ||||||||||||
403 | break; | - | ||||||||||||
404 | case 1: | - | ||||||||||||
405 | sort |= QDir::Size; | - | ||||||||||||
406 | break; | - | ||||||||||||
407 | case 2: | - | ||||||||||||
408 | sort |= QDir::Type; | - | ||||||||||||
409 | break; | - | ||||||||||||
410 | case 3: | - | ||||||||||||
411 | sort |= QDir::Time; | - | ||||||||||||
412 | break; | - | ||||||||||||
413 | default: | - | ||||||||||||
414 | break; | - | ||||||||||||
415 | } | - | ||||||||||||
416 | - | |||||||||||||
417 | setSorting(sort); | - | ||||||||||||
418 | } | - | ||||||||||||
419 | - | |||||||||||||
420 | - | |||||||||||||
421 | - | |||||||||||||
422 | - | |||||||||||||
423 | - | |||||||||||||
424 | - | |||||||||||||
425 | QStringList QDirModel::mimeTypes() const | - | ||||||||||||
426 | { | - | ||||||||||||
427 | return QStringList(QLatin1String("text/uri-list")); | - | ||||||||||||
428 | } | - | ||||||||||||
429 | QMimeData *QDirModel::mimeData(const QModelIndexList &indexes) const | - | ||||||||||||
430 | { | - | ||||||||||||
431 | QList<QUrl> urls; | - | ||||||||||||
432 | QList<QModelIndex>::const_iterator it = indexes.begin(); | - | ||||||||||||
433 | for (; it != indexes.end(); ++it) | - | ||||||||||||
434 | if ((*it).column() == 0) | - | ||||||||||||
435 | urls << QUrl::fromLocalFile(filePath(*it)); | - | ||||||||||||
436 | QMimeData *data = new QMimeData(); | - | ||||||||||||
437 | data->setUrls(urls); | - | ||||||||||||
438 | return data; | - | ||||||||||||
439 | } | - | ||||||||||||
440 | bool QDirModel::dropMimeData(const QMimeData *data, Qt::DropAction action, | - | ||||||||||||
441 | int , int , const QModelIndex &parent) | - | ||||||||||||
442 | { | - | ||||||||||||
443 | QDirModelPrivate * const d = d_func(); | - | ||||||||||||
444 | if (!d->indexValid(parent) || isReadOnly()) | - | ||||||||||||
445 | return false; | - | ||||||||||||
446 | - | |||||||||||||
447 | bool success = true; | - | ||||||||||||
448 | QString to = filePath(parent) + QDir::separator(); | - | ||||||||||||
449 | QModelIndex _parent = parent; | - | ||||||||||||
450 | - | |||||||||||||
451 | QList<QUrl> urls = data->urls(); | - | ||||||||||||
452 | QList<QUrl>::const_iterator it = urls.constBegin(); | - | ||||||||||||
453 | - | |||||||||||||
454 | switch (action) { | - | ||||||||||||
455 | case Qt::CopyAction: | - | ||||||||||||
456 | for (; it != urls.constEnd(); ++it) { | - | ||||||||||||
457 | QString path = (*it).toLocalFile(); | - | ||||||||||||
458 | success = QFile::copy(path, to + QFileInfo(path).fileName()) && success; | - | ||||||||||||
459 | } | - | ||||||||||||
460 | break; | - | ||||||||||||
461 | case Qt::LinkAction: | - | ||||||||||||
462 | for (; it != urls.constEnd(); ++it) { | - | ||||||||||||
463 | QString path = (*it).toLocalFile(); | - | ||||||||||||
464 | success = QFile::link(path, to + QFileInfo(path).fileName()) && success; | - | ||||||||||||
465 | } | - | ||||||||||||
466 | break; | - | ||||||||||||
467 | case Qt::MoveAction: | - | ||||||||||||
468 | for (; it != urls.constEnd(); ++it) { | - | ||||||||||||
469 | QString path = (*it).toLocalFile(); | - | ||||||||||||
470 | if (QFile::copy(path, to + QFileInfo(path).fileName()) | - | ||||||||||||
471 | && QFile::remove(path)) { | - | ||||||||||||
472 | QModelIndex idx=index(QFileInfo(path).path()); | - | ||||||||||||
473 | if (idx.isValid()) { | - | ||||||||||||
474 | refresh(idx); | - | ||||||||||||
475 | - | |||||||||||||
476 | _parent = index(to); | - | ||||||||||||
477 | } | - | ||||||||||||
478 | } else { | - | ||||||||||||
479 | success = false; | - | ||||||||||||
480 | } | - | ||||||||||||
481 | } | - | ||||||||||||
482 | break; | - | ||||||||||||
483 | default: | - | ||||||||||||
484 | return false; | - | ||||||||||||
485 | } | - | ||||||||||||
486 | - | |||||||||||||
487 | if (success) | - | ||||||||||||
488 | refresh(_parent); | - | ||||||||||||
489 | - | |||||||||||||
490 | return success; | - | ||||||||||||
491 | } | - | ||||||||||||
492 | - | |||||||||||||
493 | - | |||||||||||||
494 | - | |||||||||||||
495 | - | |||||||||||||
496 | - | |||||||||||||
497 | - | |||||||||||||
498 | - | |||||||||||||
499 | Qt::DropActions QDirModel::supportedDropActions() const | - | ||||||||||||
500 | { | - | ||||||||||||
501 | return Qt::CopyAction | Qt::MoveAction; | - | ||||||||||||
502 | } | - | ||||||||||||
503 | - | |||||||||||||
504 | - | |||||||||||||
505 | - | |||||||||||||
506 | - | |||||||||||||
507 | - | |||||||||||||
508 | - | |||||||||||||
509 | void QDirModel::setIconProvider(QFileIconProvider *provider) | - | ||||||||||||
510 | { | - | ||||||||||||
511 | QDirModelPrivate * const d = d_func(); | - | ||||||||||||
512 | d->iconProvider = provider; | - | ||||||||||||
513 | } | - | ||||||||||||
514 | - | |||||||||||||
515 | - | |||||||||||||
516 | - | |||||||||||||
517 | - | |||||||||||||
518 | - | |||||||||||||
519 | QFileIconProvider *QDirModel::iconProvider() const | - | ||||||||||||
520 | { | - | ||||||||||||
521 | const QDirModelPrivate * const d = d_func(); | - | ||||||||||||
522 | return d->iconProvider; | - | ||||||||||||
523 | } | - | ||||||||||||
524 | - | |||||||||||||
525 | - | |||||||||||||
526 | - | |||||||||||||
527 | - | |||||||||||||
528 | - | |||||||||||||
529 | void QDirModel::setNameFilters(const QStringList &filters) | - | ||||||||||||
530 | { | - | ||||||||||||
531 | QDirModelPrivate * const d = d_func(); | - | ||||||||||||
532 | d->nameFilters = filters; | - | ||||||||||||
533 | layoutAboutToBeChanged(); | - | ||||||||||||
534 | if (d->shouldStat) | - | ||||||||||||
535 | refresh(QModelIndex()); | - | ||||||||||||
536 | else | - | ||||||||||||
537 | d->invalidate(); | - | ||||||||||||
538 | layoutChanged(); | - | ||||||||||||
539 | } | - | ||||||||||||
540 | - | |||||||||||||
541 | - | |||||||||||||
542 | - | |||||||||||||
543 | - | |||||||||||||
544 | - | |||||||||||||
545 | QStringList QDirModel::nameFilters() const | - | ||||||||||||
546 | { | - | ||||||||||||
547 | const QDirModelPrivate * const d = d_func(); | - | ||||||||||||
548 | return d->nameFilters; | - | ||||||||||||
549 | } | - | ||||||||||||
550 | void QDirModel::setFilter(QDir::Filters filters) | - | ||||||||||||
551 | { | - | ||||||||||||
552 | QDirModelPrivate * const d = d_func(); | - | ||||||||||||
553 | d->filters = filters; | - | ||||||||||||
554 | layoutAboutToBeChanged(); | - | ||||||||||||
555 | if (d->shouldStat) | - | ||||||||||||
556 | refresh(QModelIndex()); | - | ||||||||||||
557 | else | - | ||||||||||||
558 | d->invalidate(); | - | ||||||||||||
559 | layoutChanged(); | - | ||||||||||||
560 | } | - | ||||||||||||
561 | - | |||||||||||||
562 | - | |||||||||||||
563 | - | |||||||||||||
564 | - | |||||||||||||
565 | - | |||||||||||||
566 | - | |||||||||||||
567 | - | |||||||||||||
568 | QDir::Filters QDirModel::filter() const | - | ||||||||||||
569 | { | - | ||||||||||||
570 | const QDirModelPrivate * const d = d_func(); | - | ||||||||||||
571 | return d->filters; | - | ||||||||||||
572 | } | - | ||||||||||||
573 | - | |||||||||||||
574 | - | |||||||||||||
575 | - | |||||||||||||
576 | - | |||||||||||||
577 | - | |||||||||||||
578 | - | |||||||||||||
579 | - | |||||||||||||
580 | void QDirModel::setSorting(QDir::SortFlags sort) | - | ||||||||||||
581 | { | - | ||||||||||||
582 | QDirModelPrivate * const d = d_func(); | - | ||||||||||||
583 | d->sort = sort; | - | ||||||||||||
584 | layoutAboutToBeChanged(); | - | ||||||||||||
585 | if (d->shouldStat) | - | ||||||||||||
586 | refresh(QModelIndex()); | - | ||||||||||||
587 | else | - | ||||||||||||
588 | d->invalidate(); | - | ||||||||||||
589 | layoutChanged(); | - | ||||||||||||
590 | } | - | ||||||||||||
591 | - | |||||||||||||
592 | - | |||||||||||||
593 | - | |||||||||||||
594 | - | |||||||||||||
595 | - | |||||||||||||
596 | - | |||||||||||||
597 | - | |||||||||||||
598 | QDir::SortFlags QDirModel::sorting() const | - | ||||||||||||
599 | { | - | ||||||||||||
600 | const QDirModelPrivate * const d = d_func(); | - | ||||||||||||
601 | return d->sort; | - | ||||||||||||
602 | } | - | ||||||||||||
603 | void QDirModel::setResolveSymlinks(bool enable) | - | ||||||||||||
604 | { | - | ||||||||||||
605 | QDirModelPrivate * const d = d_func(); | - | ||||||||||||
606 | d->resolveSymlinks = enable; | - | ||||||||||||
607 | } | - | ||||||||||||
608 | - | |||||||||||||
609 | bool QDirModel::resolveSymlinks() const | - | ||||||||||||
610 | { | - | ||||||||||||
611 | const QDirModelPrivate * const d = d_func(); | - | ||||||||||||
612 | return d->resolveSymlinks; | - | ||||||||||||
613 | } | - | ||||||||||||
614 | void QDirModel::setReadOnly(bool enable) | - | ||||||||||||
615 | { | - | ||||||||||||
616 | QDirModelPrivate * const d = d_func(); | - | ||||||||||||
617 | d->readOnly = enable; | - | ||||||||||||
618 | } | - | ||||||||||||
619 | - | |||||||||||||
620 | bool QDirModel::isReadOnly() const | - | ||||||||||||
621 | { | - | ||||||||||||
622 | const QDirModelPrivate * const d = d_func(); | - | ||||||||||||
623 | return d->readOnly; | - | ||||||||||||
624 | } | - | ||||||||||||
625 | void QDirModel::setLazyChildCount(bool enable) | - | ||||||||||||
626 | { | - | ||||||||||||
627 | QDirModelPrivate * const d = d_func(); | - | ||||||||||||
628 | d->lazyChildCount = enable; | - | ||||||||||||
629 | } | - | ||||||||||||
630 | - | |||||||||||||
631 | bool QDirModel::lazyChildCount() const | - | ||||||||||||
632 | { | - | ||||||||||||
633 | const QDirModelPrivate * const d = d_func(); | - | ||||||||||||
634 | return d->lazyChildCount; | - | ||||||||||||
635 | } | - | ||||||||||||
636 | void QDirModel::refresh(const QModelIndex &parent) | - | ||||||||||||
637 | { | - | ||||||||||||
638 | QDirModelPrivate * const d = d_func(); | - | ||||||||||||
639 | - | |||||||||||||
640 | QDirModelPrivate::QDirNode *n = d->indexValid(parent) ? d->node(parent) : &(d->root); | - | ||||||||||||
641 | - | |||||||||||||
642 | int rows = n->children.count(); | - | ||||||||||||
643 | if (rows == 0) { | - | ||||||||||||
644 | layoutAboutToBeChanged(); | - | ||||||||||||
645 | n->stat = true; | - | ||||||||||||
646 | n->populated = false; | - | ||||||||||||
647 | layoutChanged(); | - | ||||||||||||
648 | return; | - | ||||||||||||
649 | } | - | ||||||||||||
650 | - | |||||||||||||
651 | layoutAboutToBeChanged(); | - | ||||||||||||
652 | d->savePersistentIndexes(); | - | ||||||||||||
653 | d->rowsAboutToBeRemoved(parent, 0, rows - 1); | - | ||||||||||||
654 | n->stat = true; | - | ||||||||||||
655 | d->clear(n); | - | ||||||||||||
656 | d->rowsRemoved(parent, 0, rows - 1); | - | ||||||||||||
657 | d->restorePersistentIndexes(); | - | ||||||||||||
658 | layoutChanged(); | - | ||||||||||||
659 | } | - | ||||||||||||
660 | - | |||||||||||||
661 | - | |||||||||||||
662 | - | |||||||||||||
663 | - | |||||||||||||
664 | - | |||||||||||||
665 | - | |||||||||||||
666 | - | |||||||||||||
667 | QModelIndex QDirModel::index(const QString &path, int column) const | - | ||||||||||||
668 | { | - | ||||||||||||
669 | const QDirModelPrivate * const d = d_func(); | - | ||||||||||||
670 | - | |||||||||||||
671 | if (path.isEmpty()
| 0 | ||||||||||||
672 | return never executed: QModelIndex();return QModelIndex(); never executed: return QModelIndex(); | 0 | ||||||||||||
673 | - | |||||||||||||
674 | QString absolutePath = QDir(path).absolutePath(); | - | ||||||||||||
675 | QStringList pathElements = absolutePath.split(QLatin1Char('/'), QString::SkipEmptyParts); | - | ||||||||||||
676 | if ((pathElements.isEmpty()
| 0 | ||||||||||||
677 | - | |||||||||||||
678 | && path != QLatin1String("/")
| 0 | ||||||||||||
679 | - | |||||||||||||
680 | ) | - | ||||||||||||
681 | return never executed: QModelIndex();return QModelIndex(); never executed: return QModelIndex(); | 0 | ||||||||||||
682 | - | |||||||||||||
683 | QModelIndex idx; | - | ||||||||||||
684 | if (!d->root.populated
| 0 | ||||||||||||
685 | d->populate(&d->root); never executed: d->populate(&d->root); | 0 | ||||||||||||
686 | pathElements.prepend(QLatin1String("/")); | - | ||||||||||||
687 | - | |||||||||||||
688 | - | |||||||||||||
689 | for (int i = 0; i < pathElements.count()
| 0 | ||||||||||||
690 | ((!(!pathElements.at(i).isEmpty())) ? qt_assert("!pathElements.at(i).isEmpty()",__FILE__,904914) : qt_noop()); | - | ||||||||||||
691 | QString element = pathElements.at(i); | - | ||||||||||||
692 | QDirModelPrivate::QDirNode *parent = (idx.isValid()
| 0 | ||||||||||||
693 | - | |||||||||||||
694 | ((!(parent)) ? qt_assert("parent",__FILE__,908918) : qt_noop()); | - | ||||||||||||
695 | if (!parent->populated
| 0 | ||||||||||||
696 | d->populate(parent); never executed: d->populate(parent); | 0 | ||||||||||||
697 | - | |||||||||||||
698 | - | |||||||||||||
699 | int row = -1; | - | ||||||||||||
700 | for (int j = parent->children.count() - 1; j >= 0
| 0 | ||||||||||||
701 | const QFileInfo& fi = parent->children.at(j).info; | - | ||||||||||||
702 | QString childFileName; | - | ||||||||||||
703 | childFileName = idx.isValid()
| 0 | ||||||||||||
704 | - | |||||||||||||
705 | - | |||||||||||||
706 | - | |||||||||||||
707 | if (childFileName == element
| 0 | ||||||||||||
708 | if (i == pathElements.count() - 1
| 0 | ||||||||||||
709 | parent->children[j].stat = true; never executed: parent->children[j].stat = true; | 0 | ||||||||||||
710 | row = j; | - | ||||||||||||
711 | break; never executed: break; | 0 | ||||||||||||
712 | } | - | ||||||||||||
713 | } never executed: end of block | 0 | ||||||||||||
714 | - | |||||||||||||
715 | - | |||||||||||||
716 | if (row == -1
| 0 | ||||||||||||
717 | - | |||||||||||||
718 | - | |||||||||||||
719 | - | |||||||||||||
720 | - | |||||||||||||
721 | - | |||||||||||||
722 | - | |||||||||||||
723 | - | |||||||||||||
724 | QString newPath = parent->info.absoluteFilePath() + QLatin1Char('/') + element; | - | ||||||||||||
725 | - | |||||||||||||
726 | if (!d->allowAppendChild
| 0 | ||||||||||||
727 | return never executed: QModelIndex();return QModelIndex(); never executed: return QModelIndex(); | 0 | ||||||||||||
728 | d->appendChild(parent, newPath); | - | ||||||||||||
729 | row = parent->children.count() - 1; | - | ||||||||||||
730 | if (i == pathElements.count() - 1
| 0 | ||||||||||||
731 | parent->children[row].stat = true; never executed: parent->children[row].stat = true; | 0 | ||||||||||||
732 | const_cast<QDirModel*>(this)->layoutChanged(); | - | ||||||||||||
733 | } never executed: end of block | 0 | ||||||||||||
734 | - | |||||||||||||
735 | ((!(row >= 0)) ? qt_assert("row >= 0",__FILE__,949959) : qt_noop()); | - | ||||||||||||
736 | idx = createIndex(row, 0, static_cast<void*>(&parent->children[row])); | - | ||||||||||||
737 | ((!(idx.isValid())) ? qt_assert("idx.isValid()",__FILE__,951961) : qt_noop()); | - | ||||||||||||
738 | } never executed: end of block | 0 | ||||||||||||
739 | - | |||||||||||||
740 | if (column != 0
| 0 | ||||||||||||
741 | return never executed: idx.sibling(idx.row(), column);return idx.sibling(idx.row(), column); never executed: return idx.sibling(idx.row(), column); | 0 | ||||||||||||
742 | return never executed: idx;return idx; never executed: return idx; | 0 | ||||||||||||
743 | } | - | ||||||||||||
744 | - | |||||||||||||
745 | - | |||||||||||||
746 | - | |||||||||||||
747 | - | |||||||||||||
748 | - | |||||||||||||
749 | - | |||||||||||||
750 | bool QDirModel::isDir(const QModelIndex &index) const | - | ||||||||||||
751 | { | - | ||||||||||||
752 | const QDirModelPrivate * const d = d_func(); | - | ||||||||||||
753 | ((!(d->indexValid(index))) ? qt_assert("d->indexValid(index)",__FILE__,967977) : qt_noop()); | - | ||||||||||||
754 | QDirModelPrivate::QDirNode *node = d->node(index); | - | ||||||||||||
755 | return node->info.isDir(); | - | ||||||||||||
756 | } | - | ||||||||||||
757 | - | |||||||||||||
758 | - | |||||||||||||
759 | - | |||||||||||||
760 | - | |||||||||||||
761 | - | |||||||||||||
762 | QModelIndex QDirModel::mkdir(const QModelIndex &parent, const QString &name) | - | ||||||||||||
763 | { | - | ||||||||||||
764 | QDirModelPrivate * const d = d_func(); | - | ||||||||||||
765 | if (!d->indexValid(parent) || isReadOnly()) | - | ||||||||||||
766 | return QModelIndex(); | - | ||||||||||||
767 | - | |||||||||||||
768 | QDirModelPrivate::QDirNode *p = d->node(parent); | - | ||||||||||||
769 | QString path = p->info.absoluteFilePath(); | - | ||||||||||||
770 | - | |||||||||||||
771 | - | |||||||||||||
772 | - | |||||||||||||
773 | QDir newDir(name); | - | ||||||||||||
774 | QDir dir(path); | - | ||||||||||||
775 | if (newDir.isRelative()) | - | ||||||||||||
776 | newDir = QDir(path + QLatin1Char('/') + name); | - | ||||||||||||
777 | QString childName = newDir.dirName(); | - | ||||||||||||
778 | newDir.cdUp(); | - | ||||||||||||
779 | - | |||||||||||||
780 | if (newDir.absolutePath() != dir.absolutePath() || !dir.mkdir(name)) | - | ||||||||||||
781 | return QModelIndex(); | - | ||||||||||||
782 | - | |||||||||||||
783 | refresh(parent); | - | ||||||||||||
784 | - | |||||||||||||
785 | QStringList entryList = d->entryList(path); | - | ||||||||||||
786 | int r = entryList.indexOf(childName); | - | ||||||||||||
787 | QModelIndex i = index(r, 0, parent); | - | ||||||||||||
788 | - | |||||||||||||
789 | return i; | - | ||||||||||||
790 | } | - | ||||||||||||
791 | bool QDirModel::rmdir(const QModelIndex &index) | - | ||||||||||||
792 | { | - | ||||||||||||
793 | QDirModelPrivate * const d = d_func(); | - | ||||||||||||
794 | if (!d->indexValid(index)
| 0 | ||||||||||||
795 | return never executed: false;return false; never executed: return false; | 0 | ||||||||||||
796 | - | |||||||||||||
797 | QDirModelPrivate::QDirNode *n = d_func()->node(index); | - | ||||||||||||
798 | if (!(__builtin_expect(!!(!
| 0 | ||||||||||||
799 | QMessageLogger(__FILE__, 10261036, __PRETTY_FUNCTION__).warning("rmdir: the node is not a directory"); | - | ||||||||||||
800 | return never executed: false;return false; never executed: return false; | 0 | ||||||||||||
801 | } | - | ||||||||||||
802 | - | |||||||||||||
803 | QModelIndex par = parent(index); | - | ||||||||||||
804 | QDirModelPrivate::QDirNode *p = d_func()->node(par); | - | ||||||||||||
805 | QDir dir = p->info.dir(); | - | ||||||||||||
806 | QString path = n->info.absoluteFilePath(); | - | ||||||||||||
807 | if (!dir.rmdir(path)
| 0 | ||||||||||||
808 | return never executed: false;return false; never executed: return false; | 0 | ||||||||||||
809 | - | |||||||||||||
810 | refresh(par); | - | ||||||||||||
811 | - | |||||||||||||
812 | return never executed: true;return true; never executed: return true; | 0 | ||||||||||||
813 | } | - | ||||||||||||
814 | bool QDirModel::remove(const QModelIndex &index) | - | ||||||||||||
815 | { | - | ||||||||||||
816 | QDirModelPrivate * const d = d_func(); | - | ||||||||||||
817 | if (!d->indexValid(index) || isReadOnly()) | - | ||||||||||||
818 | return false; | - | ||||||||||||
819 | - | |||||||||||||
820 | QDirModelPrivate::QDirNode *n = d_func()->node(index); | - | ||||||||||||
821 | if (n->info.isDir()) | - | ||||||||||||
822 | return false; | - | ||||||||||||
823 | - | |||||||||||||
824 | QModelIndex par = parent(index); | - | ||||||||||||
825 | QDirModelPrivate::QDirNode *p = d_func()->node(par); | - | ||||||||||||
826 | QDir dir = p->info.dir(); | - | ||||||||||||
827 | QString path = n->info.absoluteFilePath(); | - | ||||||||||||
828 | if (!dir.remove(path)) | - | ||||||||||||
829 | return false; | - | ||||||||||||
830 | - | |||||||||||||
831 | refresh(par); | - | ||||||||||||
832 | - | |||||||||||||
833 | return true; | - | ||||||||||||
834 | } | - | ||||||||||||
835 | - | |||||||||||||
836 | - | |||||||||||||
837 | - | |||||||||||||
838 | - | |||||||||||||
839 | - | |||||||||||||
840 | - | |||||||||||||
841 | - | |||||||||||||
842 | QString QDirModel::filePath(const QModelIndex &index) const | - | ||||||||||||
843 | { | - | ||||||||||||
844 | const QDirModelPrivate * const d = d_func(); | - | ||||||||||||
845 | if (d->indexValid(index)) { | - | ||||||||||||
846 | QFileInfo fi = fileInfo(index); | - | ||||||||||||
847 | if (d->resolveSymlinks && fi.isSymLink()) | - | ||||||||||||
848 | fi = d->resolvedInfo(fi); | - | ||||||||||||
849 | return QDir::cleanPath(fi.absoluteFilePath()); | - | ||||||||||||
850 | } | - | ||||||||||||
851 | return QString(); | - | ||||||||||||
852 | } | - | ||||||||||||
853 | - | |||||||||||||
854 | - | |||||||||||||
855 | - | |||||||||||||
856 | - | |||||||||||||
857 | - | |||||||||||||
858 | - | |||||||||||||
859 | - | |||||||||||||
860 | QString QDirModel::fileName(const QModelIndex &index) const | - | ||||||||||||
861 | { | - | ||||||||||||
862 | const QDirModelPrivate * const d = d_func(); | - | ||||||||||||
863 | if (!d->indexValid(index)) | - | ||||||||||||
864 | return QString(); | - | ||||||||||||
865 | QFileInfo info = fileInfo(index); | - | ||||||||||||
866 | if (info.isRoot()) | - | ||||||||||||
867 | return info.absoluteFilePath(); | - | ||||||||||||
868 | if (d->resolveSymlinks && info.isSymLink()) | - | ||||||||||||
869 | info = d->resolvedInfo(info); | - | ||||||||||||
870 | return info.fileName(); | - | ||||||||||||
871 | } | - | ||||||||||||
872 | - | |||||||||||||
873 | - | |||||||||||||
874 | - | |||||||||||||
875 | - | |||||||||||||
876 | - | |||||||||||||
877 | - | |||||||||||||
878 | QIcon QDirModel::fileIcon(const QModelIndex &index) const | - | ||||||||||||
879 | { | - | ||||||||||||
880 | const QDirModelPrivate * const d = d_func(); | - | ||||||||||||
881 | if (!d->indexValid(index)) | - | ||||||||||||
882 | return d->iconProvider->icon(QFileIconProvider::Computer); | - | ||||||||||||
883 | QDirModelPrivate::QDirNode *node = d->node(index); | - | ||||||||||||
884 | if (node->icon.isNull()) | - | ||||||||||||
885 | node->icon = d->iconProvider->icon(node->info); | - | ||||||||||||
886 | return node->icon; | - | ||||||||||||
887 | } | - | ||||||||||||
888 | QFileInfo QDirModel::fileInfo(const QModelIndex &index) const | - | ||||||||||||
889 | { | - | ||||||||||||
890 | const QDirModelPrivate * const d = d_func(); | - | ||||||||||||
891 | ((!(d->indexValid(index))) ? qt_assert("d->indexValid(index)",__FILE__,11421152) : qt_noop()); | - | ||||||||||||
892 | - | |||||||||||||
893 | QDirModelPrivate::QDirNode *node = d->node(index); | - | ||||||||||||
894 | return node->info; | - | ||||||||||||
895 | } | - | ||||||||||||
896 | - | |||||||||||||
897 | - | |||||||||||||
898 | - | |||||||||||||
899 | - | |||||||||||||
900 | - | |||||||||||||
901 | void QDirModelPrivate::init() | - | ||||||||||||
902 | { | - | ||||||||||||
903 | filters = QDir::AllEntries | QDir::NoDotAndDotDot; | - | ||||||||||||
904 | sort = QDir::Name; | - | ||||||||||||
905 | nameFilters << QLatin1String("*"); | - | ||||||||||||
906 | root.parent = 0; | - | ||||||||||||
907 | root.info = QFileInfo(); | - | ||||||||||||
908 | clear(&root); | - | ||||||||||||
909 | roleNames.insertMulti(QDirModel::FileIconRole, ([]() -> QByteArray { enum { Size = sizeof("fileIcon") - 1 }; static const QStaticByteArrayData<Size> qbytearray_literal = { { { { -1 } }, Size, 0, 0, sizeof(QByteArrayData) }, "fileIcon" }; QByteArrayDataPtr holder = { qbytearray_literal.data_ptr() }; const QByteArray ba(holder); return ba; }())); | - | ||||||||||||
910 | roleNames.insert(QDirModel::FilePathRole, ([]() -> QByteArray { enum { Size = sizeof("filePath") - 1 }; static const QStaticByteArrayData<Size> qbytearray_literal = { { { { -1 } }, Size, 0, 0, sizeof(QByteArrayData) }, "filePath" }; QByteArrayDataPtr holder = { qbytearray_literal.data_ptr() }; const QByteArray ba(holder); return ba; }())); | - | ||||||||||||
911 | roleNames.insert(QDirModel::FileNameRole, ([]() -> QByteArray { enum { Size = sizeof("fileName") - 1 }; static const QStaticByteArrayData<Size> qbytearray_literal = { { { { -1 } }, Size, 0, 0, sizeof(QByteArrayData) }, "fileName" }; QByteArrayDataPtr holder = { qbytearray_literal.data_ptr() }; const QByteArray ba(holder); return ba; }())); | - | ||||||||||||
912 | } | - | ||||||||||||
913 | - | |||||||||||||
914 | QDirModelPrivate::QDirNode *QDirModelPrivate::node(int row, QDirNode *parent) const | - | ||||||||||||
915 | { | - | ||||||||||||
916 | if (row < 0
| 0 | ||||||||||||
917 | return never executed: 0;return 0; never executed: return 0; | 0 | ||||||||||||
918 | - | |||||||||||||
919 | bool isDir = !parent
| 0 | ||||||||||||
920 | QDirNode *p = (parent
| 0 | ||||||||||||
921 | if (isDir
| 0 | ||||||||||||
922 | populate(p); never executed: populate(p); | 0 | ||||||||||||
923 | - | |||||||||||||
924 | if (__builtin_expect(!!(
| 0 | ||||||||||||
925 | QMessageLogger(__FILE__, 11761186, __PRETTY_FUNCTION__).warning("node: the row does not exist"); | - | ||||||||||||
926 | return never executed: 0;return 0; never executed: return 0; | 0 | ||||||||||||
927 | } | - | ||||||||||||
928 | - | |||||||||||||
929 | return never executed: const_cast<QDirNode*>(&p->children.at(row));return const_cast<QDirNode*>(&p->children.at(row)); never executed: return const_cast<QDirNode*>(&p->children.at(row)); | 0 | ||||||||||||
930 | } | - | ||||||||||||
931 | - | |||||||||||||
932 | QVector<QDirModelPrivate::QDirNode> QDirModelPrivate::children(QDirNode *parent, bool stat) const | - | ||||||||||||
933 | { | - | ||||||||||||
934 | ((!(parent)) ? qt_assert("parent",__FILE__,11851195) : qt_noop()); | - | ||||||||||||
935 | QFileInfoList infoList; | - | ||||||||||||
936 | if (parent == &root) { | - | ||||||||||||
937 | parent = 0; | - | ||||||||||||
938 | infoList = QDir::drives(); | - | ||||||||||||
939 | } else if (parent->info.isDir()) { | - | ||||||||||||
940 | - | |||||||||||||
941 | if (parent->info.isSymLink() && resolveSymlinks) { | - | ||||||||||||
942 | QString link = parent->info.symLinkTarget(); | - | ||||||||||||
943 | if (link.size() > 1 && link.at(link.size() - 1) == QDir::separator()) | - | ||||||||||||
944 | link.chop(1); | - | ||||||||||||
945 | if (stat) | - | ||||||||||||
946 | infoList = entryInfoList(link); | - | ||||||||||||
947 | else | - | ||||||||||||
948 | infoList = QDir(link).entryInfoList(nameFilters, QDir::AllEntries | QDir::System); | - | ||||||||||||
949 | } else { | - | ||||||||||||
950 | if (stat) | - | ||||||||||||
951 | infoList = entryInfoList(parent->info.absoluteFilePath()); | - | ||||||||||||
952 | else | - | ||||||||||||
953 | infoList = QDir(parent->info.absoluteFilePath()).entryInfoList(nameFilters, QDir::AllEntries | QDir::System); | - | ||||||||||||
954 | } | - | ||||||||||||
955 | } | - | ||||||||||||
956 | - | |||||||||||||
957 | QVector<QDirNode> nodes(infoList.count()); | - | ||||||||||||
958 | for (int i = 0; i < infoList.count(); ++i) { | - | ||||||||||||
959 | QDirNode &node = nodes[i]; | - | ||||||||||||
960 | node.parent = parent; | - | ||||||||||||
961 | node.info = infoList.at(i); | - | ||||||||||||
962 | node.populated = false; | - | ||||||||||||
963 | node.stat = shouldStat; | - | ||||||||||||
964 | } | - | ||||||||||||
965 | - | |||||||||||||
966 | return nodes; | - | ||||||||||||
967 | } | - | ||||||||||||
968 | - | |||||||||||||
969 | void QDirModelPrivate::_q_refresh() | - | ||||||||||||
970 | { | - | ||||||||||||
971 | QDirModel * const q = q_func(); | - | ||||||||||||
972 | q->refresh(toBeRefreshed); | - | ||||||||||||
973 | toBeRefreshed = QModelIndex(); | - | ||||||||||||
974 | } | - | ||||||||||||
975 | - | |||||||||||||
976 | void QDirModelPrivate::savePersistentIndexes() | - | ||||||||||||
977 | { | - | ||||||||||||
978 | QDirModel * const q = q_func(); | - | ||||||||||||
979 | savedPersistent.clear(); | - | ||||||||||||
980 | savedPersistent.reserve(persistent.indexes.size()); | - | ||||||||||||
981 | for (QForeachContainer<typename QtPrivate::remove_reference<decltype(persistent.indexes)>::type> _container_((persistent.indexes)); _container_.control && _container_.i != _container_.e; ++_container_.i, _container_.control ^= 1) for (QPersistentModelIndexData *data = *_container_.i; _container_.control; _container_.control = 0) { | - | ||||||||||||
982 | SavedPersistent saved;QModelIndex index = data->index; | - | ||||||||||||
983 | SavedPersistent saved .path= { | - | ||||||||||||
984 | q->filePath(index); | - | ||||||||||||
saved.column =), | ||||||||||||||
985 | index.column(); | - | ||||||||||||
saved.data =(), | ||||||||||||||
986 | data; | - | ||||||||||||
saved.index =, | ||||||||||||||
987 | index;, | - | ||||||||||||
988 | }; | - | ||||||||||||
989 | savedPersistent.appendpush_back(std::move(saved);)); | - | ||||||||||||
990 | } never executed: end of block | 0 | ||||||||||||
991 | } never executed: end of block | 0 | ||||||||||||
992 | - | |||||||||||||
993 | void QDirModelPrivate::restorePersistentIndexes() | - | ||||||||||||
994 | { | - | ||||||||||||
995 | QDirModel * const q = q_func(); | - | ||||||||||||
996 | bool allow = allowAppendChild; | - | ||||||||||||
997 | allowAppendChild = false; | - | ||||||||||||
998 | for (int i = 0; i <const SavedPersistent &sp : qAsConst(savedPersistent.count(); ++i))) { | - | ||||||||||||
999 | QPersistentModelIndexData *data = savedPersistentsp.at(i).data; | - | ||||||||||||
QString path = savedPersistent.at(i).path; | ||||||||||||||
int column = savedPersistent.at(i).column; | ||||||||||||||
1000 | QModelIndex idx = q->index(sp.path, sp.column); | - | ||||||||||||
1001 | if (idx != data->index
| 0 | ||||||||||||
1002 | - | |||||||||||||
1003 | persistent.indexes.remove(data->index); | - | ||||||||||||
1004 | data->index = idx; | - | ||||||||||||
1005 | data->model = q; | - | ||||||||||||
1006 | if (idx.isValid()
| 0 | ||||||||||||
1007 | persistent.indexes.insert(idx, data); never executed: persistent.indexes.insert(idx, data); | 0 | ||||||||||||
1008 | } never executed: end of block | 0 | ||||||||||||
1009 | } never executed: end of block | 0 | ||||||||||||
1010 | savedPersistent.clear(); | - | ||||||||||||
1011 | allowAppendChild = allow; | - | ||||||||||||
1012 | } never executed: end of block | 0 | ||||||||||||
1013 | - | |||||||||||||
1014 | QFileInfoList QDirModelPrivate::entryInfoList(const QString &path) const | - | ||||||||||||
1015 | { | - | ||||||||||||
1016 | const QDir dir(path); | - | ||||||||||||
1017 | return dir.entryInfoList(nameFilters, filters, sort); | - | ||||||||||||
1018 | } | - | ||||||||||||
1019 | - | |||||||||||||
1020 | QStringList QDirModelPrivate::entryList(const QString &path) const | - | ||||||||||||
1021 | { | - | ||||||||||||
1022 | const QDir dir(path); | - | ||||||||||||
1023 | return dir.entryList(nameFilters, filters, sort); | - | ||||||||||||
1024 | } | - | ||||||||||||
1025 | - | |||||||||||||
1026 | QString QDirModelPrivate::name(const QModelIndex &index) const | - | ||||||||||||
1027 | { | - | ||||||||||||
1028 | const QDirNode *n = node(index); | - | ||||||||||||
1029 | const QFileInfo info = n->info; | - | ||||||||||||
1030 | if (info.isRoot()) { | - | ||||||||||||
1031 | QString name = info.absoluteFilePath(); | - | ||||||||||||
1032 | - | |||||||||||||
1033 | - | |||||||||||||
1034 | - | |||||||||||||
1035 | - | |||||||||||||
1036 | - | |||||||||||||
1037 | - | |||||||||||||
1038 | return name; | - | ||||||||||||
1039 | } | - | ||||||||||||
1040 | return info.fileName(); | - | ||||||||||||
1041 | } | - | ||||||||||||
1042 | - | |||||||||||||
1043 | QString QDirModelPrivate::size(const QModelIndex &index) const | - | ||||||||||||
1044 | { | - | ||||||||||||
1045 | const QDirNode *n = node(index); | - | ||||||||||||
1046 | if (n->info.isDir()) { | - | ||||||||||||
1047 | - | |||||||||||||
1048 | - | |||||||||||||
1049 | - | |||||||||||||
1050 | return QLatin1String(""); | - | ||||||||||||
1051 | - | |||||||||||||
1052 | - | |||||||||||||
1053 | - | |||||||||||||
1054 | - | |||||||||||||
1055 | - | |||||||||||||
1056 | } | - | ||||||||||||
1057 | - | |||||||||||||
1058 | - | |||||||||||||
1059 | - | |||||||||||||
1060 | const quint64 kb = 1024; | - | ||||||||||||
1061 | const quint64 mb = 1024 * kb; | - | ||||||||||||
1062 | const quint64 gb = 1024 * mb; | - | ||||||||||||
1063 | const quint64 tb = 1024 * gb; | - | ||||||||||||
1064 | quint64 bytes = n->info.size(); | - | ||||||||||||
1065 | if (bytes >= tb) | - | ||||||||||||
1066 | return QFileSystemModel::tr("%1 TB").arg(QLocale().toString(qreal(bytes) / tb, 'f', 3)); | - | ||||||||||||
1067 | if (bytes >= gb) | - | ||||||||||||
1068 | return QFileSystemModel::tr("%1 GB").arg(QLocale().toString(qreal(bytes) / gb, 'f', 2)); | - | ||||||||||||
1069 | if (bytes >= mb) | - | ||||||||||||
1070 | return QFileSystemModel::tr("%1 MB").arg(QLocale().toString(qreal(bytes) / mb, 'f', 1)); | - | ||||||||||||
1071 | if (bytes >= kb) | - | ||||||||||||
1072 | return QFileSystemModel::tr("%1 KB").arg(QLocale().toString(bytes / kb)); | - | ||||||||||||
1073 | return QFileSystemModel::tr("%1 byte(s)").arg(QLocale().toString(bytes)); | - | ||||||||||||
1074 | } | - | ||||||||||||
1075 | - | |||||||||||||
1076 | QString QDirModelPrivate::type(const QModelIndex &index) const | - | ||||||||||||
1077 | { | - | ||||||||||||
1078 | return iconProvider->type(node(index)->info); | - | ||||||||||||
1079 | } | - | ||||||||||||
1080 | - | |||||||||||||
1081 | QString QDirModelPrivate::time(const QModelIndex &index) const | - | ||||||||||||
1082 | { | - | ||||||||||||
1083 | - | |||||||||||||
1084 | return node(index)->info.lastModified().toString(Qt::LocalDate); | - | ||||||||||||
1085 | - | |||||||||||||
1086 | - | |||||||||||||
1087 | - | |||||||||||||
1088 | - | |||||||||||||
1089 | } | - | ||||||||||||
1090 | - | |||||||||||||
1091 | void QDirModelPrivate::appendChild(QDirModelPrivate::QDirNode *parent, const QString &path) const | - | ||||||||||||
1092 | { | - | ||||||||||||
1093 | QDirModelPrivate::QDirNode node; | - | ||||||||||||
1094 | node.populated = false; | - | ||||||||||||
1095 | node.stat = shouldStat; | - | ||||||||||||
1096 | node.parent = (parent == &root ? 0 : parent); | - | ||||||||||||
1097 | node.info = QFileInfo(path); | - | ||||||||||||
1098 | node.info.setCaching(true); | - | ||||||||||||
1099 | - | |||||||||||||
1100 | - | |||||||||||||
1101 | - | |||||||||||||
1102 | QDirModelPrivate *that = const_cast<QDirModelPrivate *>(this); | - | ||||||||||||
1103 | that->savePersistentIndexes(); | - | ||||||||||||
1104 | parent->children.append(node); | - | ||||||||||||
1105 | for (int i = 0; i < parent->children.count(); ++i) { | - | ||||||||||||
1106 | QDirNode *childNode = &parent->children[i]; | - | ||||||||||||
1107 | for (int j = 0; j < childNode->children.count(); ++j) | - | ||||||||||||
1108 | childNode->children[j].parent = childNode; | - | ||||||||||||
1109 | } | - | ||||||||||||
1110 | that->restorePersistentIndexes(); | - | ||||||||||||
1111 | } | - | ||||||||||||
1112 | - | |||||||||||||
1113 | QFileInfo QDirModelPrivate::resolvedInfo(QFileInfo info) | - | ||||||||||||
1114 | { | - | ||||||||||||
1115 | - | |||||||||||||
1116 | - | |||||||||||||
1117 | - | |||||||||||||
1118 | - | |||||||||||||
1119 | QStringList paths; | - | ||||||||||||
1120 | do { | - | ||||||||||||
1121 | QFileInfo link(info.symLinkTarget()); | - | ||||||||||||
1122 | if (link.isRelative()) | - | ||||||||||||
1123 | info.setFile(info.absolutePath(), link.filePath()); | - | ||||||||||||
1124 | else | - | ||||||||||||
1125 | info = link; | - | ||||||||||||
1126 | if (paths.contains(info.absoluteFilePath())) | - | ||||||||||||
1127 | return QFileInfo(); | - | ||||||||||||
1128 | paths.append(info.absoluteFilePath()); | - | ||||||||||||
1129 | } while (info.isSymLink()); | - | ||||||||||||
1130 | return info; | - | ||||||||||||
1131 | - | |||||||||||||
1132 | } | - | ||||||||||||
1133 | - | |||||||||||||
1134 | - | |||||||||||||
1135 | - | |||||||||||||
Switch to Source code | Preprocessed file |