Use QFileInfo to represent paths

Wed, 19 Apr 2023 22:42:43 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Wed, 19 Apr 2023 22:42:43 +0300
changeset 380
16f6717a218b
parent 379
8d88adffb779
child 381
80bea7a6e84f

Use QFileInfo to represent paths

src/documentmanager.cpp file | annotate | diff | comparison | revisions
src/libraries.cpp file | annotate | diff | comparison | revisions
src/libraries.h file | annotate | diff | comparison | revisions
--- a/src/documentmanager.cpp	Wed Apr 12 01:53:42 2023 +0300
+++ b/src/documentmanager.cpp	Wed Apr 19 22:42:43 2023 +0300
@@ -341,7 +341,7 @@
 	}
 }
 
-static QString findFile(
+static QFileInfo findFile(
 	QString referenceName,
 	const QString& modelPath,
 	const LibrariesModel& libraries)
@@ -349,8 +349,8 @@
 	// Try to find the file in the same place as the model itself
 	referenceName.replace("\\", "/");
 	const QDir dir = QFileInfo{modelPath}.dir();
-	QString referencedFilePath = dir.filePath(referenceName);
-	if (not QFileInfo{referencedFilePath}.exists())
+	QFileInfo referencedFilePath = {dir.filePath(referenceName)};
+	if (not referencedFilePath.exists())
 	{
 		// Look for it in the libraries
 		referencedFilePath = libraries.findFile(referenceName);
@@ -373,14 +373,14 @@
 struct Dependency
 {
 	QString name;
-	QString path;
+	QFileInfo path;
 	bool operator<(const Dependency& other) const
 	{
 		if (this->name != other.name) {
 			return this->name < other.name;
 		}
 		else {
-			return this->path < other.path;
+			return this->path.absoluteFilePath() < other.path.absoluteFilePath();
 		}
 	}
 };
@@ -393,8 +393,9 @@
 	const std::set<QString> refNames = referenceNames(modelInfo->model.get());
 	if (modelInfo != nullptr) {
 		for (const QString& name : refNames) {
-			const QString path = findFile(name, modelInfo->path, *libraries);
-			if (not path.isEmpty()) {
+			const QFileInfo path = findFile(name, modelInfo->path, *libraries);
+			if (path.exists())
+			{
 				result.insert(Dependency{.name = name, .path = path});
 			}
 		}
@@ -419,17 +420,17 @@
 			if (idp != nullptr) {
 				info->dependencies[dep.name] = *idp;
 			}
-			else if (not info->dependencies.contains(dep.name) and not missing.contains(dep.path)) {
+			else if (not info->dependencies.contains(dep.name) and not missing.contains(dep.path.absoluteFilePath())) {
 				QString loadErrorString;
 				QTextStream localErrorStream{&loadErrorString};
 				const std::optional<ModelId> modelIdOpt = documents->openModel(
-					dep.path,
+					dep.path.absoluteFilePath(),
 					localErrorStream,
 					OpenType::AutomaticallyOpened);
 				if (not modelIdOpt.has_value()) {
 					const QString& errorMessage = QObject::tr("could not load '%1': %2")
-					.arg(dep.path, loadErrorString);
-					missing[dep.path] = errorMessage;
+					.arg(dep.path.absoluteFilePath(), loadErrorString);
+					missing[dep.path.absoluteFilePath()] = errorMessage;
 				}
 				else {
 					info->dependencies[dep.name] = modelIdOpt.value();
--- a/src/libraries.cpp	Wed Apr 12 01:53:42 2023 +0300
+++ b/src/libraries.cpp	Wed Apr 19 22:42:43 2023 +0300
@@ -34,9 +34,9 @@
  * @param fileName File to search for
  * @return Full path to the file, or empty string if not found.
  */
-QString LibrariesModel::findFile(QString fileName) const
+QFileInfo LibrariesModel::findFile(QString fileName) const
 {
-	QString path;
+	QFileInfo result;
 	fileName.replace("\\", "/");
 	bool found = false;
 	for (const Library& library : this->libraries)
@@ -48,7 +48,7 @@
 			QFileInfo fileInfo{directory.absoluteFilePath(fileName)};
 			if (fileInfo.exists() && fileInfo.isFile())
 			{
-				path = fileInfo.absoluteFilePath();
+				result = fileInfo;
 				found = true;
 				break;
 			}
@@ -58,7 +58,7 @@
 			break;
 		}
 	}
-	return path;
+	return result;
 }
 
 /**
--- a/src/libraries.h	Wed Apr 12 01:53:42 2023 +0300
+++ b/src/libraries.h	Wed Apr 19 22:42:43 2023 +0300
@@ -53,7 +53,7 @@
 	LibrariesModel(QObject* parent = nullptr);
 	auto begin() const { return this->libraries.begin(); }
 	auto end() const { return this->libraries.end(); }
-	QString findFile(QString fileName) const;
+	QFileInfo findFile(QString fileName) const;
 	void addLibrary(const Library& library);
 	void removeLibrary(const index_t libraryIndex);
 	const Library& library(index_t libraryIndex) const;

mercurial