Update the part's Name: field when saving

Sun, 19 May 2013 14:27:21 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Sun, 19 May 2013 14:27:21 +0300
changeset 227
e13cd69b16dd
parent 226
3dd9e63698cd
child 228
8cddb57489a7

Update the part's Name: field when saving

src/common.h file | annotate | diff | comparison | revisions
src/file.cpp file | annotate | diff | comparison | revisions
src/gui.cpp file | annotate | diff | comparison | revisions
src/gui_actions.cpp file | annotate | diff | comparison | revisions
--- a/src/common.h	Sun May 19 13:51:38 2013 +0300
+++ b/src/common.h	Sun May 19 14:27:21 2013 +0300
@@ -70,8 +70,10 @@
 
 #ifdef WIN32
 #define DIRSLASH "\\"
+#define DIRSLASH_CHAR '\\'
 #else // WIN32
 #define DIRSLASH "/"
+#define DIRSLASH_CHAR '/'
 #endif // WIN32
 
 #ifdef RELEASE
--- a/src/file.cpp	Sun May 19 13:51:38 2013 +0300
+++ b/src/file.cpp	Sun May 19 14:27:21 2013 +0300
@@ -102,8 +102,6 @@
 }
 
 // =============================================================================
-// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-// =============================================================================
 OpenFile* findLoadedFile (str zName) {
 	for (OpenFile* file : g_loadedFiles)
 		if (file->m_filename == zName)
@@ -113,28 +111,32 @@
 }
 
 // =============================================================================
-// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+str dirname (str path) {
+	long lastpos; // FIXME: why doesn't str::last () work here?
+	for (lastpos = path.len () - 1; lastpos >= 0; --lastpos)
+		if (path[(size_t) lastpos] == DIRSLASH_CHAR)
+			break;
+	
+	if (lastpos > 0)
+		return path.substr (0, lastpos);
+	
+	return "";
+}
+
 // =============================================================================
 FILE* openLDrawFile (str relpath, bool subdirs) {
+	printf ("%s: Try to open %s\n", __func__, relpath.c ());
 #ifndef WIN32
 	relpath.replace ("\\", "/");
 #endif // WIN32
 	
 	if (g_curfile != null) {
-		long lastpos;
-		for (lastpos = g_curfile->m_filename.len () - 1; lastpos >= 0; --lastpos)
-			if (g_curfile->m_filename[(size_t) lastpos] == '/')
-				break;
+		str partpath = fmt ("%s" DIRSLASH "%s", dirname (g_curfile->m_filename).c (), relpath.c ());
+		printf ("try %s\n", partpath.c ());
+		FILE* fp = fopen (partpath, "r");
 		
-		if (lastpos > 0) {
-			str dirname = g_curfile->m_filename.substr (0, lastpos);
-			str partpath = fmt ("%s" DIRSLASH "%s", dirname.c (), relpath.c ());
-			printf ("try %s\n", partpath.c ());
-			FILE* fp = fopen (partpath, "r");
-			
-			if (fp != null)
-				return fp;
-		}
+		if (fp != null)
+			return fp;
 	}
 	
 	printf ("try %s\n", relpath.chars ());
@@ -267,7 +269,7 @@
 			// If we don't have a file path yet, we have to ask the user for one.
 			if (m_filename.len () == 0) {
 				str path = QFileDialog::getSaveFileName (g_win, "Save As",
-					"", "LDraw files (*.dat *.ldr)");
+					g_curfile->m_filename, "LDraw files (*.dat *.ldr)");
 				
 				if (path.len () == 0)
 					return false;
@@ -420,21 +422,40 @@
 		return false;
 	}
 	
+	// If the second object in the list holds the file name, update that now.
+	// Only do this if the file is explicitly open. If it's saved into a directory
+	// called "s" or "48", prepend that into the name.
+	LDComment* fpathComment = null;
+	if (m_implicit == false && m_objs.size () >= 2 && object (1)->getType () == LDObject::Comment) {
+		fpathComment = static_cast<LDComment*> (object (1));
+		
+		if (fpathComment->text.substr (0, 6) == "Name: ") {
+			str newfname;
+			str dir = basename (dirname (path));
+			
+			if (dir == "s" || dir == "48")
+				newfname = fmt ("%s\\", dir.c ());
+			
+			newfname += basename (path);
+			fpathComment->text = fmt ("Name: %s", newfname.c ());
+			g_win->buildObjList ();
+		}
+	}
+	
 	// Write all entries now
 	for (LDObject* obj : m_objs) {
-		// LDraw requires lines to have DOS line endings
-		str zLine = fmt ("%s\r\n", obj->getContents ().chars ());
-		
-		fwrite (zLine.chars(), 1, ~zLine, fp);
+		// LDraw requires files to have DOS line endings
+		str line = fmt ("%s\r\n", obj->getContents ().chars ());
+		fwrite (line.chars(), 1, line.len (), fp);
 	}
 	
 	fclose (fp);
 	
 	// We have successfully saved, update the save position now.
 	savePos = History::pos ();
+	m_filename = path;
 	
 	g_win->updateTitle ();
-	
 	return true;
 }
 
--- a/src/gui.cpp	Sun May 19 13:51:38 2013 +0300
+++ b/src/gui.cpp	Sun May 19 14:27:21 2013 +0300
@@ -507,7 +507,10 @@
 	
 	// Append our current file if we have one
 	if (g_curfile) {
-		title += fmt (": %s", basename (g_curfile->m_filename.chars()));
+		if (g_curfile->m_filename.len () > 0)
+			title += fmt (": %s", basename (g_curfile->m_filename));
+		else
+			title += fmt (": <anonymous>");
 		
 		if (g_curfile->m_objs.size() > 0 &&
 			g_curfile->m_objs[0]->getType() == LDObject::Comment)
--- a/src/gui_actions.cpp	Sun May 19 13:51:38 2013 +0300
+++ b/src/gui_actions.cpp	Sun May 19 14:27:21 2013 +0300
@@ -70,7 +70,7 @@
 	
 	if (~path == 0 || saveAs) {
 		path = QFileDialog::getSaveFileName (g_win, "Save As",
-		"", "LDraw files (*.dat *.ldr)");
+			g_curfile->m_filename, "LDraw files (*.dat *.ldr)");
 		
 		if (~path == 0) {
 			// User didn't give a file name. This happens if the user cancelled

mercurial