further improved file behavior

Fri, 20 Dec 2013 18:04:23 +0200

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Fri, 20 Dec 2013 18:04:23 +0200
changeset 579
4e140de264d2
parent 578
a86ae85a277c
child 580
341580df1e61

further improved file behavior

src/document.cc file | annotate | diff | comparison | revisions
src/gui.cc file | annotate | diff | comparison | revisions
src/types.cc file | annotate | diff | comparison | revisions
src/types.h file | annotate | diff | comparison | revisions
--- a/src/document.cc	Fri Dec 20 17:06:06 2013 +0200
+++ b/src/document.cc	Fri Dec 20 18:04:23 2013 +0200
@@ -221,10 +221,13 @@
 	relpath.replace ("\\", "/");
 #endif // WIN32
 
-	if (getCurrentDocument())
-	{	// First, try find the file in the current model's file path. We want a file
-		// in the immediate vicinity of the current model to override stock LDraw stuff.
-		str partpath = fmt ("%1" DIRSLASH "%2", dirname (getCurrentDocument()->getName()), relpath);
+	// Try find it relative to other currently open documents. We want a file
+	// in the immediate vicinity of a current model to override stock LDraw stuff.
+	for (LDDocument* doc : g_loadedFiles)
+	{	if (doc->getFullPath().isEmpty())
+			continue;
+
+		str partpath = fmt ("%1/%2", dirname (doc->getFullPath()), relpath);
 
 		if (f->open (partpath, File::Read))
 			return f;
@@ -423,8 +426,10 @@
 		return null;
 
 	LDDocument* load = new LDDocument;
-	load->setFullPath (path);
-	load->setName (LDDocument::shortenName (path));
+	load->setFullPath (f->getPath());
+	load->setName (LDDocument::shortenName (load->getFullPath()));
+	dlog ("name: %1 (%2)", load->getName(), load->getFullPath());
+	g_loadedFiles << load;
 
 	// Don't take the file loading as actual edits to the file
 	load->getHistory()->setIgnoring (true);
@@ -432,18 +437,15 @@
 	int numWarnings;
 	bool ok;
 	QList<LDObject*> objs = loadFileContents (f, &numWarnings, &ok);
+	delete f;
 
 	if (!ok)
-	{	delete f;
+	{	g_loadedFiles.removeOne (load);
 		delete load;
 		return null;
 	}
 
-	for (LDObject* obj : objs)
-		load->addObject (obj);
-
-	delete f;
-	g_loadedFiles << load;
+	load->addObjects (objs);
 
 	if (g_loadingMainFile)
 	{	LDDocument::setCurrent (load);
@@ -891,7 +893,7 @@
 			if (fileInfo)
 				ref->setFileInfo (fileInfo);
 			else
-				ref->replace (new LDError (ref->raw(), "Could not open referred file"));
+				ref->replace (new LDError (ref->raw(), fmt ("Could not open %1", ref->getFileInfo()->getName())));
 		}
 
 		// Reparse gibberish files. It could be that they are invalid because
--- a/src/gui.cc	Fri Dec 20 17:06:06 2013 +0200
+++ b/src/gui.cc	Fri Dec 20 18:04:23 2013 +0200
@@ -327,7 +327,7 @@
 			case LDObject::Subfile:
 			{	LDSubfile* ref = static_cast<LDSubfile*> (obj);
 
-				descr = fmt ("%1 %2, (", ref->getFileInfo()->getName(), ref->getPosition().stringRep (true));
+				descr = fmt ("%1 %2, (", ref->getFileInfo()->getDisplayName(), ref->getPosition().stringRep (true));
 
 				for (int i = 0; i < 9; ++i)
 					descr += fmt ("%1%2", ref->getTransform()[i], (i != 8) ? " " : "");
@@ -354,7 +354,7 @@
 
 		QListWidgetItem* item = new QListWidgetItem (descr);
 		item->setIcon (getIcon (obj->getTypeName()));
-		
+
 		// Use italic font if hidden
 		if (obj->isHidden())
 		{	QFont font = item->font();
--- a/src/types.cc	Fri Dec 20 17:06:06 2013 +0200
+++ b/src/types.cc	Fri Dec 20 18:04:23 2013 +0200
@@ -346,6 +346,7 @@
 
 File::File (str path, OpenType rtype)
 {	m_file = null;
+	m_path = path;
 	open (path, rtype);
 }
 
@@ -393,6 +394,7 @@
 
 	if (result)
 	{	m_textstream = new QTextStream (m_file);
+		m_path = path;
 		return true;
 	}
 
--- a/src/types.h	Fri Dec 20 17:06:06 2013 +0200
+++ b/src/types.h	Fri Dec 20 18:04:23 2013 +0200
@@ -287,10 +287,13 @@
 		bool         operator!() const;
 		operator bool() const;
 
+		inline str getPath() const { return m_path; }
+
 	private:
-		QFile*       m_file;
-		QTextStream* m_textstream;
-		iterator     m_endIterator;
+		QFile*			m_file;
+		QTextStream*	m_textstream;
+		iterator			m_endIterator;
+		str				m_path;
 };
 
 // =============================================================================

mercurial