Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickfolderlistmodel.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5//![code]
9#include <QtCore/qloggingcategory.h>
10#include <qqmlcontext.h>
11#include <qqmlfile.h>
12
14
15Q_STATIC_LOGGING_CATEGORY(lcFolderListModel, "qt.labs.folderlistmodel")
16
17class QQuickFolderListModelPrivate
18{
19 Q_DECLARE_PUBLIC(QQuickFolderListModel)
20
21public:
22 QQuickFolderListModelPrivate(QQuickFolderListModel *q) : q_ptr(q) { }
23
24 QQuickFolderListModel *q_ptr;
25 QUrl currentDir;
26 QUrl rootDir;
27 FileInfoThread fileInfoThread;
28 QList<FileProperty> data;
29 QHash<int, QByteArray> roleNames;
30 QQuickFolderListModel::SortField sortField = QQuickFolderListModel::Name;
31 QStringList nameFilters = { QLatin1String("*") };
32 QQuickFolderListModel::Status status = QQuickFolderListModel::Null;
33 bool sortReversed = false;
34 bool showFiles = true;
35 bool showDirs = true;
36 bool showDirsFirst = false;
37 bool showDotAndDotDot = false;
38 bool showOnlyReadable = false;
39 bool showHidden = false;
40 bool caseSensitive = true;
41 bool sortCaseSensitive = true;
42 bool resettingModel = false;
43
44 ~QQuickFolderListModelPrivate() {}
45 void init();
46 void updateSorting();
47
48 void finishModelReset();
49
50 // private slots
51 void _q_directoryChanged(const QString &directory, const QList<FileProperty> &list);
52 void _q_directoryUpdated(const QString &directory, const QList<FileProperty> &list, int fromIndex, int toIndex);
53 void _q_sortFinished(const QList<FileProperty> &list);
54 void _q_statusChanged(QQuickFolderListModel::Status s);
55
56 static QString resolvePath(const QUrl &path);
57};
58
59
60void QQuickFolderListModelPrivate::init()
61{
62 Q_Q(QQuickFolderListModel);
63 qRegisterMetaType<QList<FileProperty> >("QList<FileProperty>");
64 qRegisterMetaType<QQuickFolderListModel::Status>("QQuickFolderListModel::Status");
65 q->connect(&fileInfoThread, SIGNAL(directoryChanged(QString,QList<FileProperty>)),
66 q, SLOT(_q_directoryChanged(QString,QList<FileProperty>)));
67 q->connect(&fileInfoThread, SIGNAL(directoryUpdated(QString,QList<FileProperty>,int,int)),
68 q, SLOT(_q_directoryUpdated(QString,QList<FileProperty>,int,int)));
69 q->connect(&fileInfoThread, SIGNAL(sortFinished(QList<FileProperty>)),
70 q, SLOT(_q_sortFinished(QList<FileProperty>)));
71 q->connect(&fileInfoThread, SIGNAL(statusChanged(QQuickFolderListModel::Status)),
72 q, SLOT(_q_statusChanged(QQuickFolderListModel::Status)));
73 q->connect(q, SIGNAL(rowCountChanged()), q, SIGNAL(countChanged()));
74}
75
76
77void QQuickFolderListModelPrivate::updateSorting()
78{
79 Q_Q(QQuickFolderListModel);
80
81 QDir::SortFlags flags;
82
83 switch (sortField) {
84 case QQuickFolderListModel::Unsorted:
85 flags |= QDir::Unsorted;
86 break;
87 case QQuickFolderListModel::Name:
88 flags |= QDir::Name;
89 break;
90 case QQuickFolderListModel::Time:
91 flags |= QDir::Time;
92 break;
93 case QQuickFolderListModel::Size:
94 flags |= QDir::Size;
95 break;
96 case QQuickFolderListModel::Type:
97 flags |= QDir::Type;
98 break;
99 }
100
101 emit q->layoutAboutToBeChanged();
102
103 if (sortReversed)
104 flags |= QDir::Reversed;
105 if (!sortCaseSensitive)
106 flags |= QDir::IgnoreCase;
107
108 fileInfoThread.setSortFlags(flags);
109}
110
111void QQuickFolderListModelPrivate::finishModelReset()
112{
113 Q_Q(QQuickFolderListModel);
114 const bool wasDataEmpty = data.isEmpty();
115 data.clear();
116 qCDebug(lcFolderListModel) << "about to emit endResetModel";
117 q->endResetModel();
118 if (!wasDataEmpty)
119 emit q->rowCountChanged();
120 if (status != QQuickFolderListModel::Null) {
121 status = QQuickFolderListModel::Null;
122 emit q->statusChanged();
123 }
124 resettingModel = false;
125}
126
127void QQuickFolderListModelPrivate::_q_directoryChanged(const QString &directory, const QList<FileProperty> &list)
128{
129 qCDebug(lcFolderListModel) << "_q_directoryChanged called with directory" << directory;
130 Q_Q(QQuickFolderListModel);
131 Q_UNUSED(directory);
132 if (!resettingModel) {
133 resettingModel = true;
134 q->beginResetModel();
135 }
136
137 data = list;
138 q->endResetModel();
139 qCDebug(lcFolderListModel) << "- endResetModel called";
140 emit q->rowCountChanged();
141 emit q->folderChanged();
142 resettingModel = false;
143}
144
145
146void QQuickFolderListModelPrivate::_q_directoryUpdated(const QString &directory, const QList<FileProperty> &list, int fromIndex, int toIndex)
147{
148 Q_Q(QQuickFolderListModel);
149 Q_UNUSED(directory);
150
151 QModelIndex parent;
152 if (data.size() == list.size()) {
153 QModelIndex modelIndexFrom = q->createIndex(fromIndex, 0);
154 QModelIndex modelIndexTo = q->createIndex(toIndex, 0);
155 data = list;
156 emit q->dataChanged(modelIndexFrom, modelIndexTo);
157 } else {
158 // File(s) inserted or removed. Since I do not know how many
159 // or where, I need to update the whole list from the first item.
160 // This is a little pessimistic, but optimizing it would require
161 // more information in the signal from FileInfoThread.
162 if (data.size() > 0) {
163 q->beginRemoveRows(parent, 0, data.size() - 1);
164 q->endRemoveRows();
165 }
166 data = list;
167 if (list.size() > 0) {
168 if (toIndex > list.size() - 1)
169 toIndex = list.size() - 1;
170 q->beginInsertRows(parent, 0, data.size() - 1);
171 q->endInsertRows();
172 }
173 emit q->rowCountChanged();
174 }
175}
176
177void QQuickFolderListModelPrivate::_q_sortFinished(const QList<FileProperty> &list)
178{
179 Q_Q(QQuickFolderListModel);
180 qCDebug(lcFolderListModel) << "_q_sortFinished called with" << list.size() << "files";
181
182 QModelIndex parent;
183 if (data.size() > 0) {
184 qCDebug(lcFolderListModel) << "- removing all existing rows...";
185 q->beginRemoveRows(parent, 0, data.size()-1);
186 data.clear();
187 q->endRemoveRows();
188 qCDebug(lcFolderListModel) << "- ...removed all existing rows";
189 }
190
191 qCDebug(lcFolderListModel) << "- inserting sorted rows...";
192 q->beginInsertRows(parent, 0, list.size()-1);
193 data = list;
194 q->endInsertRows();
195 qCDebug(lcFolderListModel) << "- ... inserted sorted rows";
196}
197
198void QQuickFolderListModelPrivate::_q_statusChanged(QQuickFolderListModel::Status s)
199{
200 Q_Q(QQuickFolderListModel);
201
202 if (status != s) {
203 status = s;
204 emit q->statusChanged();
205 }
206}
207
208QString QQuickFolderListModelPrivate::resolvePath(const QUrl &path)
209{
210 QString localPath = QQmlFile::urlToLocalFileOrQrc(path);
211 QUrl localUrl = QUrl(localPath);
212 QString fullPath = localUrl.path();
213 if (localUrl.scheme().size())
214 fullPath = localUrl.scheme() + QLatin1Char(':') + fullPath;
215 return QDir::cleanPath(fullPath);
216}
217
218/*!
219 \qmlmodule Qt.labs.folderlistmodel 2.\QtMinorVersion
220 \title Qt Labs FolderListModel QML Types
221 \ingroup qmlmodules
222 \brief The FolderListModel provides a model of the contents of a file system folder.
223
224 To use this module, import the module with the following line:
225
226 \qml
227 import Qt.labs.folderlistmodel
228 \endqml
229*/
230
231/*!
232 \qmltype FolderListModel
233 \inqmlmodule Qt.labs.folderlistmodel
234//! \nativetype QQuickFolderListModel
235 \ingroup qtquick-models
236 \brief The FolderListModel provides a model of the contents of a file system folder.
237
238 FolderListModel provides access to information about the contents of a folder
239 in the local file system, exposing a list of files to views and other data components.
240
241 \note This type is made available by importing the \c Qt.labs.folderlistmodel module.
242 \e{Elements in the Qt.labs module are not guaranteed to remain compatible
243 in future versions.}
244
245 \note Some features in FolderListModel depend on \l QFileSystemWatcher. If \l QFileSystemWatcher
246 is disabled, the folder set using \c setFolder is not watched for changes, which results in
247 signals typically emitted on directory changes (like directoryUpdated or directoryChanged) not
248 being emitted without manually calling \c setFolder again. For more information, see
249 \l{Qt Configure Options}.
250
251 \qml
252 import Qt.labs.folderlistmodel
253 \endqml
254
255 The \l folder property specifies the folder to access. Information about the
256 files and directories in the folder is supplied via the model's interface.
257 Components access names and paths via the following roles:
258
259 \list
260 \li \c fileName (\c string)
261 \li \c filePath (\c string)
262 \li \c fileUrl (\c url) (since Qt 5.15)
263 \li \c fileBaseName (\c string)
264 \li \c fileSuffix (\c string)
265 \li \c fileSize (\c qlonglong)
266 \li \c fileModified (\c date)
267 \li \c fileAccessed (\c date)
268 \li \c fileIsDir (\c bool)
269 \endlist
270
271 Additionally a file entry can be differentiated from a folder entry via the
272 isFolder() method.
273
274 \section1 Filtering
275
276 Various properties can be set to filter the number of files and directories
277 exposed by the model.
278
279 The \l nameFilters property can be set to contain a list of wildcard filters
280 that are applied to names of files and directories, causing only those that
281 match the filters to be exposed.
282
283 Directories can be included or excluded using the \l showDirs property,
284 navigation directories can also be excluded by setting the \l showDotAndDotDot
285 property to false, hidden files can be included or excluded using the
286 \l showHidden property.
287
288 It is sometimes useful to limit the files and directories exposed to those
289 that the user can access. The \l showOnlyReadable property can be set to
290 enable this feature.
291
292 \section1 Example Usage
293
294 The following example shows a FolderListModel being used to provide a list
295 of QML files in a \l ListView:
296
297 \qml
298 import QtQuick
299 import Qt.labs.folderlistmodel
300
301 ListView {
302 width: 200; height: 400
303
304 FolderListModel {
305 id: folderModel
306 nameFilters: ["*.qml"]
307 }
308
309 Component {
310 id: fileDelegate
311 required property string fileName
312 Text { text: fileName }
313 }
314
315 model: folderModel
316 delegate: fileDelegate
317 }
318 \endqml
319
320 \section1 Path Separators
321
322 Qt uses "/" as a universal directory separator in the same way that "/" is
323 used as a path separator in URLs. If you always use "/" as a directory
324 separator, Qt will translate your paths to conform to the underlying
325 operating system.
326
327 \sa {QML Data Models}
328*/
329
330QQuickFolderListModel::QQuickFolderListModel(QObject *parent)
331 : QAbstractListModel(parent), d_ptr(new QQuickFolderListModelPrivate(this))
332{
333 Q_D(QQuickFolderListModel);
334 d->roleNames[FileNameRole] = "fileName";
335 d->roleNames[FilePathRole] = "filePath";
336 d->roleNames[FileBaseNameRole] = "fileBaseName";
337 d->roleNames[FileSuffixRole] = "fileSuffix";
338 d->roleNames[FileSizeRole] = "fileSize";
339 d->roleNames[FileLastModifiedRole] = "fileModified";
340 d->roleNames[FileLastReadRole] = "fileAccessed";
341 d->roleNames[FileIsDirRole] = "fileIsDir";
342 d->roleNames[FileUrlRole] = "fileUrl";
343 d->init();
344}
345
346QQuickFolderListModel::~QQuickFolderListModel()
347{
348}
349
350QVariant QQuickFolderListModel::data(const QModelIndex &index, int role) const
351{
352 Q_D(const QQuickFolderListModel);
353 QVariant rv;
354
355 const int row = index.row();
356 if (row < 0 || row >= d->data.size())
357 return rv;
358
359 switch (role)
360 {
361 case FileNameRole:
362 rv = d->data.at(row).fileName();
363 break;
364 case FilePathRole:
365 rv = d->data.at(row).filePath();
366 break;
367 case FileBaseNameRole:
368 rv = d->data.at(row).baseName();
369 break;
370 case FileSuffixRole:
371 rv = d->data.at(row).suffix();
372 break;
373 case FileSizeRole:
374 rv = d->data.at(row).size();
375 break;
376 case FileLastModifiedRole:
377 rv = d->data.at(row).lastModified();
378 break;
379 case FileLastReadRole:
380 rv = d->data.at(row).lastRead();
381 break;
382 case FileIsDirRole:
383 rv = d->data.at(row).isDir();
384 break;
385 case FileUrlRole:
386 rv = QUrl::fromLocalFile(d->data.at(row).filePath());
387 break;
388 default:
389 break;
390 }
391 return rv;
392}
393
394QHash<int, QByteArray> QQuickFolderListModel::roleNames() const
395{
396 Q_D(const QQuickFolderListModel);
397 return d->roleNames;
398}
399
400/*!
401 \qmlproperty int FolderListModel::count
402 \readonly
403
404 Returns the number of items in the current folder that match the
405 filter criteria.
406*/
407int QQuickFolderListModel::rowCount(const QModelIndex &parent) const
408{
409 Q_D(const QQuickFolderListModel);
410 Q_UNUSED(parent);
411 return d->data.size();
412}
413
414QModelIndex QQuickFolderListModel::index(int row, int , const QModelIndex &) const
415{
416 return createIndex(row, 0);
417}
418
419/*!
420 \qmlproperty url FolderListModel::folder
421
422 The \a folder property holds a URL for the folder that the model
423 currently provides.
424
425 The value must be a \c file: or \c qrc: URL, or a relative URL.
426
427 The default value is the application's working directory at the time
428 when the FolderListModel is first initialized.
429*/
430QUrl QQuickFolderListModel::folder() const
431{
432 Q_D(const QQuickFolderListModel);
433 return d->currentDir;
434}
435
436void QQuickFolderListModel::setFolder(const QUrl &folder)
437{
438 Q_D(QQuickFolderListModel);
439
440 if (folder == d->currentDir)
441 return;
442
443 // It's possible for the folder to be set twice in quick succession,
444 // in which case we could still be waiting for FileInfoThread to finish
445 // getting the list of files from the previously set folder. We should
446 // at least ensure that the begin/end model reset routine is followed
447 // in the correct order, and not e.g. call beginResetModel twice.
448 if (d->resettingModel)
449 d->finishModelReset();
450
451 d->resettingModel = true;
452
453 QString resolvedPath = QQuickFolderListModelPrivate::resolvePath(folder);
454
455 qCDebug(lcFolderListModel) << "about to emit beginResetModel since our folder was set to" << folder;
456 beginResetModel();
457
458 //Remove the old path for the file system watcher
459 if (!d->currentDir.isEmpty())
460 d->fileInfoThread.removePath(d->currentDir.path());
461
462 d->currentDir = folder;
463
464 QFileInfo info(resolvedPath);
465 if (!info.exists() || !info.isDir()) {
466 d->finishModelReset();
467 return;
468 }
469
470 d->fileInfoThread.setPath(resolvedPath);
471}
472
473
474/*!
475 \qmlproperty url FolderListModel::rootFolder
476
477 When this property is set, the given folder will
478 be treated as the root in the file system, so that
479 you can only traverse subfolders within it.
480*/
481QUrl QQuickFolderListModel::rootFolder() const
482{
483 Q_D(const QQuickFolderListModel);
484 return d->rootDir;
485}
486
487void QQuickFolderListModel::setRootFolder(const QUrl &path)
488{
489 Q_D(QQuickFolderListModel);
490
491 if (path.isEmpty())
492 return;
493
494 QString resolvedPath = QQuickFolderListModelPrivate::resolvePath(path);
495
496 QFileInfo info(resolvedPath);
497 if (!info.exists() || !info.isDir())
498 return;
499 if (path != d->rootDir) {
500 d->fileInfoThread.setRootPath(resolvedPath);
501 d->rootDir = path;
502 emit rootFolderChanged();
503 }
504}
505
506
507/*!
508 \qmlproperty url FolderListModel::parentFolder
509 \readonly
510
511 Returns the URL of the parent of the current \l folder.
512*/
513QUrl QQuickFolderListModel::parentFolder() const
514{
515 Q_D(const QQuickFolderListModel);
516
517 QString localFile = d->currentDir.toLocalFile();
518 if (!localFile.isEmpty()) {
519 QDir dir(localFile);
520 if (dir.isRoot() || !dir.cdUp())
521 return QUrl();
522 localFile = dir.path();
523 } else {
524 const QString path = d->currentDir.path();
525 const int pos = path.lastIndexOf(QLatin1Char('/'));
526 if (pos <= 0)
527 return QUrl();
528 localFile = path.left(pos);
529 }
530 return QUrl::fromLocalFile(localFile);
531}
532
533/*!
534 \qmlproperty list<string> FolderListModel::nameFilters
535
536 The \a nameFilters property contains a list of file name filters.
537 The filters may include the ? and * wildcards.
538
539 The example below filters on PNG and JPEG files:
540
541 \qml
542 FolderListModel {
543 nameFilters: [ "*.png", "*.jpg" ]
544 }
545 \endqml
546
547 \note Directories are not excluded by filters.
548*/
549QStringList QQuickFolderListModel::nameFilters() const
550{
551 Q_D(const QQuickFolderListModel);
552 return d->nameFilters;
553}
554
555void QQuickFolderListModel::setNameFilters(const QStringList &filters)
556{
557 Q_D(QQuickFolderListModel);
558 if (d->nameFilters == filters)
559 return;
560 d->fileInfoThread.setNameFilters(filters);
561 d->nameFilters = filters;
562 emit nameFilterChanged();
563}
564
565void QQuickFolderListModel::classBegin()
566{
567}
568
569void QQuickFolderListModel::componentComplete()
570{
571 Q_D(QQuickFolderListModel);
572 QString localPath = QQmlFile::urlToLocalFileOrQrc(d->currentDir);
573 if (localPath.isEmpty() || !QDir(localPath).exists())
574 setFolder(QUrl::fromLocalFile(QDir::currentPath()));
575 d->fileInfoThread.start(QThread::LowPriority);
576}
577
578/*!
579 \qmlproperty enumeration FolderListModel::sortField
580
581 The \a sortField property contains the field to use for sorting.
582 \c sortField may be one of:
583
584 \value FolderListModel.Unsorted no sorting is applied
585 \value FolderListModel.Name sort by filename (default)
586 \value FolderListModel.Time sort by time modified
587 \value FolderListModel.Size sort by file size
588 \value FolderListModel.Type sort by file type/extension
589
590 \sa sortReversed
591*/
592QQuickFolderListModel::SortField QQuickFolderListModel::sortField() const
593{
594 Q_D(const QQuickFolderListModel);
595 return d->sortField;
596}
597
598void QQuickFolderListModel::setSortField(SortField field)
599{
600 Q_D(QQuickFolderListModel);
601
602 if (field != d->sortField) {
603 d->sortField = field;
604 d->updateSorting();
605 emit sortFieldChanged();
606 }
607}
608
609int QQuickFolderListModel::roleFromString(const QString &roleName) const
610{
611 Q_D(const QQuickFolderListModel);
612 return d->roleNames.key(roleName.toLatin1(), -1);
613}
614
615/*!
616 \qmlproperty bool FolderListModel::sortReversed
617
618 If set to true, reverses the sort order. The default is false.
619
620 \sa sortField
621*/
622bool QQuickFolderListModel::sortReversed() const
623{
624 Q_D(const QQuickFolderListModel);
625 return d->sortReversed;
626}
627
628void QQuickFolderListModel::setSortReversed(bool rev)
629{
630 Q_D(QQuickFolderListModel);
631
632 if (rev != d->sortReversed) {
633 d->sortReversed = rev;
634 d->updateSorting();
635 emit sortReversedChanged();
636 }
637}
638
639/*!
640 \qmlmethod bool FolderListModel::isFolder(int index)
641
642 Returns true if the entry \a index is a folder; otherwise
643 returns false.
644*/
645bool QQuickFolderListModel::isFolder(int index) const
646{
647 if (index != -1) {
648 QModelIndex idx = createIndex(index, 0);
649 if (idx.isValid()) {
650 QVariant var = data(idx, FileIsDirRole);
651 if (var.isValid())
652 return var.toBool();
653 }
654 }
655 return false;
656}
657
658/*!
659 \qmlproperty bool FolderListModel::showFiles
660 \since 5.2
661
662 If true, files are included in the model; otherwise only directories
663 are included.
664
665 By default, this property is true.
666
667 \sa showDirs
668*/
669bool QQuickFolderListModel::showFiles() const
670{
671 Q_D(const QQuickFolderListModel);
672 return d->showFiles;
673}
674
675void QQuickFolderListModel::setShowFiles(bool on)
676{
677 Q_D(QQuickFolderListModel);
678
679 if (on != d->showFiles) {
680 d->fileInfoThread.setShowFiles(on);
681 d->showFiles = on;
682 emit showFilesChanged();
683 }
684}
685
686/*!
687 \qmlproperty bool FolderListModel::showDirs
688
689 If true, directories are included in the model; otherwise only files
690 are included.
691
692 By default, this property is true.
693
694 Note that the nameFilters are not applied to directories.
695
696 \sa showDotAndDotDot
697*/
698bool QQuickFolderListModel::showDirs() const
699{
700 Q_D(const QQuickFolderListModel);
701 return d->showDirs;
702}
703
704void QQuickFolderListModel::setShowDirs(bool on)
705{
706 Q_D(QQuickFolderListModel);
707
708 if (on != d->showDirs) {
709 d->fileInfoThread.setShowDirs(on);
710 d->showDirs = on;
711 emit showDirsChanged();
712 }
713}
714
715/*!
716 \qmlproperty bool FolderListModel::showDirsFirst
717
718 If true, if directories are included in the model they will
719 always be shown first, then the files.
720
721 By default, this property is false.
722
723*/
724bool QQuickFolderListModel::showDirsFirst() const
725{
726 Q_D(const QQuickFolderListModel);
727 return d->showDirsFirst;
728}
729
730void QQuickFolderListModel::setShowDirsFirst(bool on)
731{
732 Q_D(QQuickFolderListModel);
733
734 if (on != d->showDirsFirst) {
735 d->fileInfoThread.setShowDirsFirst(on);
736 d->showDirsFirst = on;
737 emit showDirsFirstChanged();
738 }
739}
740
741
742/*!
743 \qmlproperty bool FolderListModel::showDotAndDotDot
744
745 If true, the "." and ".." directories are included in the model; otherwise
746 they are excluded.
747
748 By default, this property is false.
749
750 \sa showDirs
751*/
752bool QQuickFolderListModel::showDotAndDotDot() const
753{
754 Q_D(const QQuickFolderListModel);
755 return d->showDotAndDotDot;
756}
757
758void QQuickFolderListModel::setShowDotAndDotDot(bool on)
759{
760 Q_D(QQuickFolderListModel);
761
762 if (on != d->showDotAndDotDot) {
763 d->fileInfoThread.setShowDotAndDotDot(on);
764 d->showDotAndDotDot = on;
765 emit showDotAndDotDotChanged();
766 }
767}
768
769
770/*!
771 \qmlproperty bool FolderListModel::showHidden
772 \since 5.2
773
774 If true, hidden files and directories are included in the model; otherwise
775 they are excluded.
776
777 By default, this property is false.
778*/
779bool QQuickFolderListModel::showHidden() const
780{
781 Q_D(const QQuickFolderListModel);
782 return d->showHidden;
783}
784
785void QQuickFolderListModel::setShowHidden(bool on)
786{
787 Q_D(QQuickFolderListModel);
788
789 if (on != d->showHidden) {
790 d->fileInfoThread.setShowHidden(on);
791 d->showHidden = on;
792 emit showHiddenChanged();
793 }
794}
795
796/*!
797 \qmlproperty bool FolderListModel::showOnlyReadable
798
799 If true, only readable files and directories are shown; otherwise all files
800 and directories are shown.
801
802 By default, this property is false.
803
804 \sa showDirs
805*/
806bool QQuickFolderListModel::showOnlyReadable() const
807{
808 Q_D(const QQuickFolderListModel);
809 return d->showOnlyReadable;
810}
811
812void QQuickFolderListModel::setShowOnlyReadable(bool on)
813{
814 Q_D(QQuickFolderListModel);
815
816 if (on != d->showOnlyReadable) {
817 d->fileInfoThread.setShowOnlyReadable(on);
818 d->showOnlyReadable = on;
819 emit showOnlyReadableChanged();
820 }
821}
822
823/*!
824 * \qmlproperty bool FolderListModel::caseSensitive
825 * \since 5.7
826 *
827 * Use case sensitive pattern matching.
828 *
829 * By default, this property is true.
830 *
831 */
832bool QQuickFolderListModel::caseSensitive() const
833{
834 Q_D(const QQuickFolderListModel);
835 return d->caseSensitive;
836}
837
838void QQuickFolderListModel::setCaseSensitive(bool on)
839{
840 Q_D(QQuickFolderListModel);
841
842 if (on != d->caseSensitive) {
843 d->fileInfoThread.setCaseSensitive(on);
844 d->caseSensitive = on;
845 emit caseSensitiveChanged();
846 }
847}
848
849/*!
850 \qmlproperty enumeration FolderListModel::status
851 \since 5.11
852 \readonly
853
854 This property holds the status of folder reading. It can be one of:
855
856 \value FolderListModel.Null no \a folder has been set
857 \value FolderListModel.Ready the folder has been loaded
858 \value FolderListModel.Loading the folder is currently being loaded
859
860 Use this status to provide an update or respond to the status change in some way.
861 For example, you could:
862
863 \list
864 \li Trigger a state change:
865 \qml
866 State { name: 'loaded'; when: folderModel.status == FolderListModel.Ready }
867 \endqml
868
869 \li Implement an \c onStatusChanged signal handler:
870 \qml
871 FolderListModel {
872 id: folderModel
873 onStatusChanged: if (folderModel.status == FolderListModel.Ready) console.log('Loaded')
874 }
875 \endqml
876
877 \li Bind to the status value:
878 \qml
879 Text { text: folderModel.status == FolderListModel.Ready ? 'Loaded' : 'Not loaded' }
880 \endqml
881 \endlist
882*/
883QQuickFolderListModel::Status QQuickFolderListModel::status() const
884{
885 Q_D(const QQuickFolderListModel);
886 return d->status;
887}
888
889/*!
890 \qmlproperty bool FolderListModel::sortCaseSensitive
891 \since 5.12
892
893 If set to \c true, the sort is case sensitive. This property is \c true by default.
894*/
895
896bool QQuickFolderListModel::sortCaseSensitive() const
897{
898 Q_D(const QQuickFolderListModel);
899 return d->sortCaseSensitive;
900}
901
902void QQuickFolderListModel::setSortCaseSensitive(bool on)
903{
904 Q_D(QQuickFolderListModel);
905
906 if (on != d->sortCaseSensitive) {
907 d->sortCaseSensitive = on;
908 d->updateSorting();
909 emit sortCaseSensitiveChanged();
910 }
911}
912
913/*!
914 \qmlmethod var FolderListModel::get(int index, string property)
915
916 Returns the folder \a property for the given \a index. The following properties
917 are available:
918
919 \list
920 \li \c fileName (\c string)
921 \li \c filePath (\c string)
922 \li \c fileUrl (\c url) (since Qt 5.15)
923 \li \c fileBaseName (\c string)
924 \li \c fileSuffix (\c string)
925 \li \c fileSize (\c qlonglong)
926 \li \c fileModified (\c date)
927 \li \c fileAccessed (\c date)
928 \li \c fileIsDir (\c bool)
929 \endlist
930*/
931QVariant QQuickFolderListModel::get(int idx, const QString &property) const
932{
933 int role = roleFromString(property);
934 if (role >= 0 && idx >= 0)
935 return data(index(idx), role);
936 else
937 return QVariant();
938}
939
940/*!
941 \qmlmethod int FolderListModel::indexOf(url file)
942 \since 5.6
943
944 Returns the index of the given \a file URL if the model contains it,
945 or -1 if not.
946*/
947int QQuickFolderListModel::indexOf(const QUrl &file) const
948{
949 Q_D(const QQuickFolderListModel);
950 FileProperty toFind(QFileInfo(file.toLocalFile()));
951 return d->data.indexOf(toFind);
952}
953
954//![code]
955QT_END_NAMESPACE
956
957#include "moc_qquickfolderlistmodel_p.cpp"