summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMorten Johan Sørvig <morten.sorvig@qt.io>2019-05-02 14:01:43 +0200
committerJani Heikkinen <jani.heikkinen@qt.io>2019-05-09 04:28:41 +0000
commitecc092fa5519e77a47ef560f0137b4cd5f417c5e (patch)
treea3ef9b3cf11804311ec8fb90faa44f306bf44348
parent0e97dedfcaa036b988d41566a37157472dfc2af6 (diff)
Support folderlistmodel for the no-thread configv5.13.0-rc3v5.13.0-rc2v5.13.0-rc1v5.13.0-beta4v5.13.05.13.0
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 <lorn.potter@gmail.com> Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>
-rw-r--r--src/imports/folderlistmodel/fileinfothread.cpp58
-rw-r--r--src/imports/folderlistmodel/fileinfothread_p.h3
-rw-r--r--src/imports/imports.pro2
3 files changed, 50 insertions, 13 deletions
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 <qdiriterator.h>
+#include <qpointer.h>
+#include <qtimer.h>
#include <QDebug>
@@ -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<FileInfoThread> 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)
{
diff --git a/src/imports/folderlistmodel/fileinfothread_p.h b/src/imports/folderlistmodel/fileinfothread_p.h
index 438dea6faa..923cb29e03 100644
--- a/src/imports/folderlistmodel/fileinfothread_p.h
+++ b/src/imports/folderlistmodel/fileinfothread_p.h
@@ -99,6 +99,8 @@ public Q_SLOTS:
protected:
void run() override;
+ void runOnce();
+ void initiateScan();
void getFileInfos(const QString &path);
void findChangeRange(const QList<FileProperty> &list, int &fromIndex, int &toIndex);
@@ -106,6 +108,7 @@ private:
QMutex mutex;
QWaitCondition condition;
volatile bool abort;
+ bool scanPending;
#if QT_CONFIG(filesystemwatcher)
QFileSystemWatcher *watcher;
diff --git a/src/imports/imports.pro b/src/imports/imports.pro
index 24e93fec1c..54e52b1103 100644
--- a/src/imports/imports.pro
+++ b/src/imports/imports.pro
@@ -6,7 +6,7 @@ SUBDIRS += \
models \
labsmodels
-qtConfig(thread): SUBDIRS += folderlistmodel
+SUBDIRS += folderlistmodel
qtHaveModule(sql): SUBDIRS += localstorage
qtConfig(settings): SUBDIRS += settings
qtConfig(statemachine): SUBDIRS += statemachine