From ecc092fa5519e77a47ef560f0137b4cd5f417c5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Johan=20S=C3=B8rvig?= Date: Thu, 2 May 2019 14:01:43 +0200 Subject: Support folderlistmodel for the no-thread config Scan for files on the main thread instead of using a dedicated thread. Use a zero-timer to preserve the async behavior where initiating a scan is fast. This should work well for use cases like a handful of files stored as resources. Task-number: QTBUG-74537 Change-Id: I7bf1b6c7f9dfba69fe4fc2ea1b2dd2f256d66c2c Reviewed-by: Lorn Potter Reviewed-by: Eskil Abrahamsen Blomfeldt --- src/imports/folderlistmodel/fileinfothread.cpp | 58 ++++++++++++++++++++------ 1 file changed, 46 insertions(+), 12 deletions(-) (limited to 'src/imports/folderlistmodel/fileinfothread.cpp') diff --git a/src/imports/folderlistmodel/fileinfothread.cpp b/src/imports/folderlistmodel/fileinfothread.cpp index a006f659c9..a93edd3b1b 100644 --- a/src/imports/folderlistmodel/fileinfothread.cpp +++ b/src/imports/folderlistmodel/fileinfothread.cpp @@ -39,6 +39,8 @@ #include "fileinfothread_p.h" #include +#include +#include #include @@ -46,6 +48,7 @@ FileInfoThread::FileInfoThread(QObject *parent) : QThread(parent), abort(false), + scanPending(false), #if QT_CONFIG(filesystemwatcher) watcher(nullptr), #endif @@ -109,7 +112,7 @@ void FileInfoThread::setPath(const QString &path) #endif currentPath = path; needUpdate = true; - condition.wakeAll(); + initiateScan(); } void FileInfoThread::setRootPath(const QString &path) @@ -126,7 +129,7 @@ void FileInfoThread::dirChanged(const QString &directoryPath) Q_UNUSED(directoryPath); QMutexLocker locker(&mutex); folderUpdate = true; - condition.wakeAll(); + initiateScan(); } #endif @@ -136,7 +139,7 @@ void FileInfoThread::setSortFlags(QDir::SortFlags flags) sortFlags = flags; sortUpdate = true; needUpdate = true; - condition.wakeAll(); + initiateScan(); } void FileInfoThread::setNameFilters(const QStringList & filters) @@ -144,7 +147,7 @@ void FileInfoThread::setNameFilters(const QStringList & filters) QMutexLocker locker(&mutex); nameFilters = filters; folderUpdate = true; - condition.wakeAll(); + initiateScan(); } void FileInfoThread::setShowFiles(bool show) @@ -152,7 +155,7 @@ void FileInfoThread::setShowFiles(bool show) QMutexLocker locker(&mutex); showFiles = show; folderUpdate = true; - condition.wakeAll(); + initiateScan(); } void FileInfoThread::setShowDirs(bool showFolders) @@ -160,7 +163,7 @@ void FileInfoThread::setShowDirs(bool showFolders) QMutexLocker locker(&mutex); showDirs = showFolders; folderUpdate = true; - condition.wakeAll(); + initiateScan(); } void FileInfoThread::setShowDirsFirst(bool show) @@ -168,7 +171,7 @@ void FileInfoThread::setShowDirsFirst(bool show) QMutexLocker locker(&mutex); showDirsFirst = show; folderUpdate = true; - condition.wakeAll(); + initiateScan(); } void FileInfoThread::setShowDotAndDotDot(bool on) @@ -177,7 +180,7 @@ void FileInfoThread::setShowDotAndDotDot(bool on) showDotAndDotDot = on; folderUpdate = true; needUpdate = true; - condition.wakeAll(); + initiateScan(); } void FileInfoThread::setShowHidden(bool on) @@ -186,7 +189,7 @@ void FileInfoThread::setShowHidden(bool on) showHidden = on; folderUpdate = true; needUpdate = true; - condition.wakeAll(); + initiateScan(); } void FileInfoThread::setShowOnlyReadable(bool on) @@ -194,7 +197,7 @@ void FileInfoThread::setShowOnlyReadable(bool on) QMutexLocker locker(&mutex); showOnlyReadable = on; folderUpdate = true; - condition.wakeAll(); + initiateScan(); } void FileInfoThread::setCaseSensitive(bool on) @@ -202,7 +205,7 @@ void FileInfoThread::setCaseSensitive(bool on) QMutexLocker locker(&mutex); caseSensitive = on; folderUpdate = true; - condition.wakeAll(); + initiateScan(); } #if QT_CONFIG(filesystemwatcher) @@ -211,7 +214,7 @@ void FileInfoThread::updateFile(const QString &path) Q_UNUSED(path); QMutexLocker locker(&mutex); folderUpdate = true; - condition.wakeAll(); + initiateScan(); } #endif @@ -242,6 +245,37 @@ void FileInfoThread::run() } } +void FileInfoThread::runOnce() +{ + if (scanPending) + return; + scanPending = true; + QPointer guardedThis(this); + + auto getFileInfosAsync = [guardedThis](){ + if (!guardedThis) + return; + guardedThis->scanPending = false; + if (guardedThis->currentPath.isEmpty()) { + emit guardedThis->statusChanged(QQuickFolderListModel::Null); + return; + } + emit guardedThis->statusChanged(QQuickFolderListModel::Loading); + guardedThis->getFileInfos(guardedThis->currentPath); + emit guardedThis->statusChanged(QQuickFolderListModel::Ready); + }; + + QTimer::singleShot(0, getFileInfosAsync); +} + +void FileInfoThread::initiateScan() +{ +#if QT_CONFIG(thread) + condition.wakeAll(); +#else + runOnce(); +#endif +} void FileInfoThread::getFileInfos(const QString &path) { -- cgit v1.2.3