From e635568985ef8a09302edc02ff4f74eca4a65843 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Wed, 29 Jul 2020 20:16:48 +0200 Subject: QFileSystemModel: use the QFileInfo from the model as much as possible MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the file system already stores a QFileInfo, then it will have the information cached, which avoids costly roundtrips to the file system. Task-number: QTBUG-41373 Change-Id: I830a39fe0e20ebf07abde4438a87f7572f608d66 Reviewed-by: Richard Moe Gustavsen Reviewed-by: MÃ¥rten Nordheim --- src/widgets/dialogs/qfilesystemmodel.cpp | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/widgets/dialogs/qfilesystemmodel.cpp b/src/widgets/dialogs/qfilesystemmodel.cpp index 96d3f0e011..f088712987 100644 --- a/src/widgets/dialogs/qfilesystemmodel.cpp +++ b/src/widgets/dialogs/qfilesystemmodel.cpp @@ -1393,8 +1393,14 @@ QString QFileSystemModel::filePath(const QModelIndex &index) const #endif && d->resolvedSymLinks.contains(fullPath) && dirNode->isDir()) { - QFileInfo resolvedInfo(fullPath); - resolvedInfo = resolvedInfo.canonicalFilePath(); + QFileInfo fullPathInfo(dirNode->fileInfo()); + if (!dirNode->hasInformation()) + fullPathInfo = QFileInfo(fullPath); + QString canonicalPath = fullPathInfo.canonicalFilePath(); + auto *canonicalNode = d->node(fullPathInfo.canonicalFilePath(), false); + QFileInfo resolvedInfo = canonicalNode->fileInfo(); + if (!canonicalNode->hasInformation()) + resolvedInfo = QFileInfo(canonicalPath); if (resolvedInfo.exists()) return resolvedInfo.filePath(); } @@ -1485,12 +1491,9 @@ QModelIndex QFileSystemModel::setRootPath(const QString &newPath) #else QString longNewPath = newPath; #endif - QDir newPathDir(longNewPath); //we remove .. and . from the given path if exist - if (!newPath.isEmpty()) { + if (!newPath.isEmpty()) longNewPath = QDir::cleanPath(longNewPath); - newPathDir.setPath(longNewPath); - } d->setRootPath = true; @@ -1501,8 +1504,15 @@ QModelIndex QFileSystemModel::setRootPath(const QString &newPath) if (d->rootDir.path() == longNewPath) return d->index(rootPath()); + auto node = d->node(longNewPath); + QFileInfo newPathInfo; + if (node && node->hasInformation()) + newPathInfo = node->fileInfo(); + else + newPathInfo = QFileInfo(longNewPath); + bool showDrives = (longNewPath.isEmpty() || longNewPath == QFileSystemModelPrivate::myComputer()); - if (!showDrives && !newPathDir.exists()) + if (!showDrives && !newPathInfo.exists()) return d->index(rootPath()); //We remove the watcher on the previous path @@ -1518,13 +1528,13 @@ QModelIndex QFileSystemModel::setRootPath(const QString &newPath) } // We have a new valid root path - d->rootDir = newPathDir; + d->rootDir = QDir(longNewPath); QModelIndex newRootIndex; if (showDrives) { // otherwise dir will become '.' d->rootDir.setPath(QLatin1String("")); } else { - newRootIndex = d->index(newPathDir.path()); + newRootIndex = d->index(d->rootDir.path()); } fetchMore(newRootIndex); emit rootPathChanged(longNewPath); -- cgit v1.2.3