src/documentmanager.cpp

changeset 1217
314e12e23c3a
parent 1215
77a0270352a3
child 1222
34def2630300
--- a/src/documentmanager.cpp	Thu Jan 04 19:40:52 2018 +0200
+++ b/src/documentmanager.cpp	Thu Jan 04 19:44:26 2018 +0200
@@ -26,21 +26,21 @@
 #include "documentloader.h"
 #include "glRenderer.h"
 
-ConfigOption (QStringList RecentFiles)
-ConfigOption (bool TryDownloadMissingFiles = false)
+ConfigOption(QStringList RecentFiles)
+ConfigOption(bool TryDownloadMissingFiles = false)
 
 enum
 {
 	MaxRecentFiles = 10
 };
 
-DocumentManager::DocumentManager (QObject* parent) :
-	QObject (parent),
-	HierarchyElement (parent),
-	m_loadingMainFile (false),
-	m_isLoadingLogoedStuds (false),
-	m_logoedStud (nullptr),
-	m_logoedStud2 (nullptr) {}
+DocumentManager::DocumentManager(QObject* parent) :
+	QObject(parent),
+	HierarchyElement(parent),
+	m_loadingMainFile(false),
+	m_isLoadingLogoedStuds(false),
+	m_logoedStud(nullptr),
+	m_logoedStud2(nullptr) {}
 
 DocumentManager::~DocumentManager()
 {
@@ -58,27 +58,27 @@
 	m_documents.clear();
 }
 
-LDDocument* DocumentManager::getDocumentByName (QString filename)
+LDDocument* DocumentManager::getDocumentByName(QString filename)
 {
-	LDDocument* doc = findDocumentByName (filename);
+	LDDocument* doc = findDocumentByName(filename);
 
 	if (doc == nullptr)
 	{
 		bool tmp = m_loadingMainFile;
 		m_loadingMainFile = false;
-		doc = openDocument (filename, true, true);
+		doc = openDocument(filename, true, true);
 		m_loadingMainFile = tmp;
 	}
 
 	return doc;
 }
 
-void DocumentManager::openMainModel (QString path)
+void DocumentManager::openMainModel(QString path)
 {
 	// If there's already a file with the same name, this file must replace it.
 	LDDocument* documentToReplace = nullptr;
 	LDDocument* file = nullptr;
-	QString shortName = LDDocument::shortenName (path);
+	QString shortName = LDDocument::shortenName(path);
 
 	for (LDDocument* doc : m_documents)
 	{
@@ -105,15 +105,15 @@
 	}
 
 	bool aborted;
-	file = openDocument (path, false, false, file, &aborted);
+	file = openDocument(path, false, false, file, &aborted);
 
 	if (file == nullptr)
 	{
 		if (not aborted)
 		{
 			// Tell the user loading failed.
-			setlocale (LC_ALL, "C");
-			Critical (format (tr ("Failed to open %1: %2"), path, strerror (errno)));
+			setlocale(LC_ALL, "C");
+			Critical(format(tr("Failed to open %1: %2"), path, strerror(errno)));
 		}
 
 		m_loadingMainFile = false;
@@ -122,9 +122,9 @@
 
 	file->openForEditing();
 	m_window->closeInitialDocument();
-	m_window->changeDocument (file);
+	m_window->changeDocument(file);
 	m_window->doFullRefresh();
-	addRecentFile (path);
+	addRecentFile(path);
 	m_loadingMainFile = false;
 
 	// If there were problems loading subfile references, try see if we can find these
@@ -133,20 +133,20 @@
 
 	for (LDObject* obj : file->objects())
 	{
-		if (obj->type() != OBJ_Error or static_cast<LDError*> (obj)->fileReferenced().isEmpty())
+		if (obj->type() != OBJ_Error or static_cast<LDError*>(obj)->fileReferenced().isEmpty())
 			continue;
 
-		unknowns << static_cast<LDError*> (obj)->fileReferenced();
+		unknowns << static_cast<LDError*>(obj)->fileReferenced();
 	}
 
 	if (config.tryDownloadMissingFiles() and not unknowns.isEmpty())
 	{
-		PartDownloader dl (m_window);
-		dl.setSourceType (PartDownloader::PartsTracker);
-		dl.setPrimaryFile (file);
+		PartDownloader dl(m_window);
+		dl.setSourceType(PartDownloader::PartsTracker);
+		dl.setPrimaryFile(file);
 
 		for (QString const& unknown : unknowns)
-			dl.downloadFromPartsTracker (unknown);
+			dl.downloadFromPartsTracker(unknown);
 
 		dl.exec();
 		dl.checkIfFinished();
@@ -154,23 +154,23 @@
 	}
 }
 
-LDDocument* DocumentManager::findDocumentByName (QString name)
+LDDocument* DocumentManager::findDocumentByName(QString name)
 {
 	for (LDDocument* document : m_documents)
 	{
-		if (isOneOf (name, document->name(), document->defaultName()))
+		if (isOneOf(name, document->name(), document->defaultName()))
 			return document;
 	}
 
 	return nullptr;
 }
 
-QString Dirname (QString path)
+QString Dirname(QString path)
 {
-	int lastpos = path.lastIndexOf (DIRSLASH);
+	int lastpos = path.lastIndexOf(DIRSLASH);
 
 	if (lastpos > 0)
-		return path.left (lastpos);
+		return path.left(lastpos);
 
 #ifndef _WIN32
 	if (path[0] == DIRSLASH_CHAR)
@@ -180,34 +180,34 @@
 	return "";
 }
 
-QString Basename (QString path)
+QString Basename(QString path)
 {
-	int lastpos = path.lastIndexOf (DIRSLASH);
+	int lastpos = path.lastIndexOf(DIRSLASH);
 
 	if (lastpos != -1)
-		return path.mid (lastpos + 1);
+		return path.mid(lastpos + 1);
 
 	return path;
 }
 
-QString DocumentManager::findDocumentPath (QString relativePath, bool subdirs)
+QString DocumentManager::findDocumentPath(QString relativePath, bool subdirs)
 {
 	// LDraw models use backslashes as path separators. Replace those into forward slashes for Qt.
-	relativePath.replace ("\\", "/");
+	relativePath.replace("\\", "/");
 
 	// Try find it relative to other currently open documents. We want a file in the immediate vicinity of a current
 	// part model to override stock LDraw stuff.
-	QString relativeTopDir = Basename (Dirname (relativePath));
+	QString relativeTopDir = Basename(Dirname(relativePath));
 
 	for (LDDocument* document : m_documents)
 	{
-		QString partpath = format ("%1/%2", Dirname (document->fullPath()), relativePath);
-		QFileInfo fileinfo (partpath);
+		QString partpath = format("%1/%2", Dirname(document->fullPath()), relativePath);
+		QFileInfo fileinfo(partpath);
 
 		if (fileinfo.exists())
 		{
 			// Ensure we don't mix subfiles and 48-primitives with non-subfiles and non-48
-			QString partTopDir = Basename (Dirname (partpath));
+			QString partTopDir = Basename(Dirname(partpath));
 
 			for (QString subdir : g_specialSubdirectories)
 			{
@@ -221,13 +221,13 @@
 		continue;
 	}
 
-	if (QFileInfo::exists (relativePath))
+	if (QFileInfo::exists(relativePath))
 		return relativePath;
 
 	// Try with just the LDraw path first
-	QString fullPath = format ("%1" DIRSLASH "%2", config.lDrawPath(), relativePath);
+	QString fullPath = format("%1" DIRSLASH "%2", config.lDrawPath(), relativePath);
 
-	if (QFileInfo::exists (fullPath))
+	if (QFileInfo::exists(fullPath))
 		return fullPath;
 
 	if (subdirs)
@@ -237,11 +237,11 @@
 		QStringList dirs = { config.lDrawPath(), config.downloadFilePath() };
 		for (const QString& topdir : dirs)
 		{
-			for (const QString& subdir : QStringList ({ "parts", "p" }))
+			for (const QString& subdir : QStringList({ "parts", "p" }))
 			{
-				fullPath = format ("%1" DIRSLASH "%2" DIRSLASH "%3", topdir, subdir, relativePath);
+				fullPath = format("%1" DIRSLASH "%2" DIRSLASH "%3", topdir, subdir, relativePath);
 
-				if (QFile::exists (fullPath))
+				if (QFile::exists(fullPath))
 					return fullPath;
 			}
 		}
@@ -251,10 +251,10 @@
 	return "";
 }
 
-QFile* DocumentManager::openLDrawFile (QString relpath, bool subdirs, QString* pathpointer)
+QFile* DocumentManager::openLDrawFile(QString relpath, bool subdirs, QString* pathpointer)
 {
-	print ("Opening %1...\n", relpath);
-	QString path = findDocumentPath (relpath, subdirs);
+	print("Opening %1...\n", relpath);
+	QString path = findDocumentPath(relpath, subdirs);
 
 	if (pathpointer)
 		*pathpointer = path;
@@ -262,24 +262,24 @@
 	if (path.isEmpty())
 		return nullptr;
 
-	QFile* fp = new QFile (path);
+	QFile* fp = new QFile(path);
 
-	if (fp->open (QIODevice::ReadOnly))
+	if (fp->open(QIODevice::ReadOnly))
 		return fp;
 
 	fp->deleteLater();
 	return nullptr;
 }
 
-LDObjectList DocumentManager::loadFileContents (QFile* fp, int* numWarnings, bool* ok)
+LDObjectList DocumentManager::loadFileContents(QFile* fp, int* numWarnings, bool* ok)
 {
 	LDObjectList objs;
 
 	if (numWarnings)
 		*numWarnings = 0;
 
-	DocumentLoader* loader = new DocumentLoader (m_loadingMainFile);
-	loader->read (fp);
+	DocumentLoader* loader = new DocumentLoader(m_loadingMainFile);
+	loader->read(fp);
 	loader->start();
 
 	// After start() returns, if the loader isn't done yet, it's delaying
@@ -298,7 +298,7 @@
 	return objs;
 }
 
-LDDocument* DocumentManager::openDocument (QString path, bool search, bool implicit, LDDocument* fileToOverride, bool* aborted)
+LDDocument* DocumentManager::openDocument(QString path, bool search, bool implicit, LDDocument* fileToOverride, bool* aborted)
 {
 	// Convert the file name to lowercase when searching because some parts contain subfile
 	// subfile references with uppercase file names. I'll assume here that the library will always
@@ -308,14 +308,14 @@
 
 	if (search)
 	{
-		fp = openLDrawFile (path.toLower(), true, &fullpath);
+		fp = openLDrawFile(path.toLower(), true, &fullpath);
 	}
 	else
 	{
-		fp = new QFile (path);
+		fp = new QFile(path);
 		fullpath = path;
 
-		if (not fp->open (QIODevice::ReadOnly))
+		if (not fp->open(QIODevice::ReadOnly))
 		{
 			delete fp;
 			return nullptr;
@@ -325,16 +325,16 @@
 	if (not fp)
 		return nullptr;
 
-	LDDocument* load = (fileToOverride ? fileToOverride : m_window->newDocument (implicit));
-	load->setFullPath (fullpath);
-	load->setName (LDDocument::shortenName (load->fullPath()));
+	LDDocument* load = (fileToOverride ? fileToOverride : m_window->newDocument(implicit));
+	load->setFullPath(fullpath);
+	load->setName(LDDocument::shortenName(load->fullPath()));
 
 	// Loading the file shouldn't count as actual edits to the document.
-	load->history()->setIgnoring (true);
+	load->history()->setIgnoring(true);
 
 	int numWarnings;
 	bool ok;
-	LDObjectList objs = loadFileContents (fp, &numWarnings, &ok);
+	LDObjectList objs = loadFileContents(fp, &numWarnings, &ok);
 	fp->close();
 	fp->deleteLater();
 
@@ -347,23 +347,23 @@
 		return nullptr;
 	}
 
-	load->addObjects (objs);
+	load->addObjects(objs);
 
 	if (m_loadingMainFile)
 	{
-		m_window->changeDocument (load);
-		m_window->renderer()->setDocument (load);
-		print (tr ("File %1 parsed successfully (%2 errors)."), path, numWarnings);
+		m_window->changeDocument(load);
+		m_window->renderer()->setDocument(load);
+		print(tr("File %1 parsed successfully(%2 errors)."), path, numWarnings);
 	}
 
-	load->history()->setIgnoring (false);
+	load->history()->setIgnoring(false);
 	return load;
 }
 
-void DocumentManager::addRecentFile (QString path)
+void DocumentManager::addRecentFile(QString path)
 {
 	QStringList recentFiles = config.recentFiles();
-	int idx = recentFiles.indexOf (path);
+	int idx = recentFiles.indexOf(path);
 
 	// If this file already is in the list, pop it out.
 	if (idx != -1)
@@ -371,16 +371,16 @@
 		if (idx == recentFiles.size() - 1)
 			return; // first recent file - abort and do nothing
 
-		recentFiles.removeAt (idx);
+		recentFiles.removeAt(idx);
 	}
 
 	// If there's too many recent files, drop one out.
-	while (recentFiles.size() > (MaxRecentFiles - 1))
-		recentFiles.removeAt (0);
+	while (recentFiles.size() >(MaxRecentFiles - 1))
+		recentFiles.removeAt(0);
 
 	// Add the file
 	recentFiles << path;
-	config.setRecentFiles (recentFiles);
+	config.setRecentFiles(recentFiles);
 	m_window->syncSettings();
 	m_window->updateRecentFilesMenu();
 }
@@ -398,19 +398,19 @@
 
 void DocumentManager::loadLogoedStuds()
 {
-	if (m_isLoadingLogoedStuds or (m_logoedStud and m_logoedStud2))
+	if (m_isLoadingLogoedStuds or(m_logoedStud and m_logoedStud2))
 		return;
 
 	m_isLoadingLogoedStuds = true;
-	m_logoedStud = openDocument ("stud-logo.dat", true, true);
-	m_logoedStud2 = openDocument ("stud2-logo.dat", true, true);
+	m_logoedStud = openDocument("stud-logo.dat", true, true);
+	m_logoedStud2 = openDocument("stud2-logo.dat", true, true);
 	m_isLoadingLogoedStuds = false;
 
 	if (m_logoedStud and m_logoedStud2)
-		print (tr ("Logoed studs loaded.\n"));
+		print(tr("Logoed studs loaded.\n"));
 }
 
-bool DocumentManager::preInline (LDDocument* doc, LDObjectList& objs, bool deep, bool renderinline)
+bool DocumentManager::preInline(LDDocument* doc, LDObjectList& objs, bool deep, bool renderinline)
 {
 	// Possibly substitute with logoed studs:
 	// stud.dat -> stud-logo.dat
@@ -422,12 +422,12 @@
 
 		if (doc->name() == "stud.dat" and m_logoedStud)
 		{
-			objs = m_logoedStud->inlineContents (deep, renderinline);
+			objs = m_logoedStud->inlineContents(deep, renderinline);
 			return true;
 		}
 		else if (doc->name() == "stud2.dat" and m_logoedStud2)
 		{
-			objs = m_logoedStud2->inlineContents (deep, renderinline);
+			objs = m_logoedStud2->inlineContents(deep, renderinline);
 			return true;
 		}
 	}
@@ -436,7 +436,7 @@
 
 LDDocument* DocumentManager::createNew()
 {
-	LDDocument* document = new LDDocument (this);
-	m_documents.insert (document);
+	LDDocument* document = new LDDocument(this);
+	m_documents.insert(document);
 	return document;
 }

mercurial