From 6994400e716fdb266f91d34f76fc103120f6e2ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fauconnier?= Date: Tue, 9 Nov 2021 18:05:42 +0100 Subject: [PATCH] use progressdialog and QtConcurrent in order to create documentProxies structure (improving performances in a network-storage context) --- OpenBoard.pro | 1 + src/core/UBPersistenceManager.cpp | 84 ++++++++++++++++++++----------- src/core/UBPersistenceManager.h | 5 +- 3 files changed, 59 insertions(+), 31 deletions(-) diff --git a/OpenBoard.pro b/OpenBoard.pro index e0c661ac..8973cae4 100644 --- a/OpenBoard.pro +++ b/OpenBoard.pro @@ -40,6 +40,7 @@ QT += webkitwidgets QT += multimediawidgets QT += printsupport QT += core +QT += concurrent INCLUDEPATH += src diff --git a/src/core/UBPersistenceManager.cpp b/src/core/UBPersistenceManager.cpp index b2e26cca..37c22054 100644 --- a/src/core/UBPersistenceManager.cpp +++ b/src/core/UBPersistenceManager.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include "frameworks/UBPlatformUtils.h" #include "frameworks/UBFileSystemUtils.h" @@ -98,7 +99,6 @@ UBPersistenceManager::UBPersistenceManager(QObject *pParent) mDocumentTreeStructureModel = new UBDocumentTreeModel(this); createDocumentProxiesStructure(); - emit proxyListChanged(); } @@ -131,7 +131,27 @@ void UBPersistenceManager::createDocumentProxiesStructure(bool interactive) rootDir.mkpath(rootDir.path()); QFileInfoList contentList = rootDir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Time | QDir::Reversed); - createDocumentProxiesStructure(contentList, interactive); + + mProgress.setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint); + mProgress.setLabelText(QString("retrieving all your documents (found %1)").arg(contentList.size())); + mProgress.setCancelButton(nullptr); + + // Create a QFutureWatcher and connect signals and slots. + QFutureWatcher futureWatcher; + QObject::connect(&futureWatcher, &QFutureWatcher::finished, &mProgress, &QProgressDialog::reset); + QObject::connect(&futureWatcher, &QFutureWatcher::progressRangeChanged, &mProgress, &QProgressDialog::setRange); + QObject::connect(&futureWatcher, &QFutureWatcher::progressValueChanged, &mProgress, &QProgressDialog::setValue); + + // Start the computation. + futureWatcher.setFuture(QtConcurrent::map(contentList, [=] (QFileInfo& contentInfo) + { + createDocumentProxyStructure(contentInfo); + })); + + // Display the dialog and start the event loop. + mProgress.exec(); + + futureWatcher.waitForFinished(); if (QFileInfo(mFoldersXmlStorageName).exists()) { QDomDocument xmlDom; @@ -158,44 +178,48 @@ void UBPersistenceManager::createDocumentProxiesStructure(bool interactive) } } -void UBPersistenceManager::createDocumentProxiesStructure(const QFileInfoList &contentInfo, bool interactive) +void UBPersistenceManager::createDocumentProxiesStructure(const QFileInfoList &contentInfoList, bool interactive) { - foreach(QFileInfo path, contentInfo) + foreach(QFileInfo contentInfo, contentInfoList) { - QString fullPath = path.absoluteFilePath(); + createDocumentProxyStructure(contentInfo, interactive); + } +} - QDir dir(fullPath); +void UBPersistenceManager::createDocumentProxyStructure(const QFileInfo &contentInfo, bool interactive) +{ + QString fullPath = contentInfo.absoluteFilePath(); + QDir dir(fullPath); - if (dir.entryList(QDir::Files | QDir::NoDotAndDotDot).size() > 0) - { - QMap metadatas = UBMetadataDcSubsetAdaptor::load(fullPath); - QString docGroupName = metadatas.value(UBSettings::documentGroupName, QString()).toString(); - QString docName = metadatas.value(UBSettings::documentName, QString()).toString(); + if (dir.entryList(QDir::Files | QDir::NoDotAndDotDot).size() > 0) + { + QMap metadatas = UBMetadataDcSubsetAdaptor::load(fullPath); + QString docGroupName = metadatas.value(UBSettings::documentGroupName, QString()).toString(); + QString docName = metadatas.value(UBSettings::documentName, QString()).toString(); - if (docName.isEmpty()) { - qDebug() << "Group name and document name are empty in UBPersistenceManager::createDocumentProxiesStructure()"; - continue; - } + if (docName.isEmpty()) { + qDebug() << "Group name and document name are empty in UBPersistenceManager::createDocumentProxiesStructure()"; + return; + } - QModelIndex parentIndex = mDocumentTreeStructureModel->goTo(docGroupName); - if (!parentIndex.isValid()) { - return; - } + QModelIndex parentIndex = mDocumentTreeStructureModel->goTo(docGroupName); + if (!parentIndex.isValid()) { + return; + } - UBDocumentProxy* docProxy = new UBDocumentProxy(fullPath); // managed in UBDocumentTreeNode - foreach(QString key, metadatas.keys()) { - docProxy->setMetaData(key, metadatas.value(key)); - } + UBDocumentProxy* docProxy = new UBDocumentProxy(fullPath); // managed in UBDocumentTreeNode + foreach(QString key, metadatas.keys()) { + docProxy->setMetaData(key, metadatas.value(key)); + } - docProxy->setPageCount(sceneCount(docProxy)); + docProxy->setPageCount(sceneCount(docProxy)); - if (!interactive) - mDocumentTreeStructureModel->addDocument(docProxy, parentIndex); - else - processInteractiveReplacementDialog(docProxy); - } + if (!interactive) + mDocumentTreeStructureModel->addDocument(docProxy, parentIndex); + else + processInteractiveReplacementDialog(docProxy); } -} +}; QDialog::DialogCode UBPersistenceManager::processInteractiveReplacementDialog(UBDocumentProxy *pProxy) { diff --git a/src/core/UBPersistenceManager.h b/src/core/UBPersistenceManager.h index 8a1462ce..9d27dc80 100644 --- a/src/core/UBPersistenceManager.h +++ b/src/core/UBPersistenceManager.h @@ -131,7 +131,8 @@ class UBPersistenceManager : public QObject bool addDirectoryContentToDocument(const QString& documentRootFolder, UBDocumentProxy* pDocument); void createDocumentProxiesStructure(bool interactive = false); - void createDocumentProxiesStructure(const QFileInfoList &contentInfo, bool interactive = false); + void createDocumentProxiesStructure(const QFileInfoList &contentInfoList, bool interactive = false); + void createDocumentProxyStructure(const QFileInfo& contentInfo, bool interactive = false); QDialog::DialogCode processInteractiveReplacementDialog(UBDocumentProxy *pProxy); QStringList documentSubDirectories() @@ -187,6 +188,8 @@ private: bool mHasPurgedDocuments; QString mDocumentRepositoryPath; QString mFoldersXmlStorageName; + QProgressDialog mProgress; + QFutureWatcher futureWatcher; private slots: void documentRepositoryChanged(const QString& path);