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