Restructed recent files handling, the list shouldn't screw the hell up anymore.

Mon, 10 Jun 2013 16:17:24 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Mon, 10 Jun 2013 16:17:24 +0300
changeset 273
0a9141118630
parent 272
2074672a6554
child 274
d232fe4d88a6

Restructed recent files handling, the list shouldn't screw the hell up anymore.

changelog.txt file | annotate | diff | comparison | revisions
src/file.cpp file | annotate | diff | comparison | revisions
src/string.cpp file | annotate | diff | comparison | revisions
src/string.h file | annotate | diff | comparison | revisions
src/types.h file | annotate | diff | comparison | revisions
--- a/changelog.txt	Mon Jun 10 15:51:08 2013 +0300
+++ b/changelog.txt	Mon Jun 10 16:17:24 2013 +0300
@@ -5,11 +5,12 @@
 - Added a progress dialog for file loading to respond to desktops while loading files. With large files
 	the no-response policy could be a bad thing.
 - Added Export To File action, moved it + insert from to File menu
+- Added: Parts are now zoomed to fit properly
+- Added: ability to snap to pre-existing vertices while drawing.
 - Fixed: text editing did not trigger checks while setting LDraw path, removed the Configure
 	button from the LDraw path config dialog, it's no longer needed.
 - Fixed: Coordinates weren't drawn properly on a bright background (was always drawn in bright colors..).
-- Parts are now zoomed to fit properly
-- Added ability to snap to pre-existing vertices while drawing.
+- Fixed: Recent files should behave coherently now.
 - Replace coords: allow replacing all coords regardless of original value, plus relative moving (offset)
 - When drawing, drawn vertices now display coordinate labels.
 - Calculated coordinates are now rounded down (to prevent stuff like Z:160.000001)
--- a/src/file.cpp	Mon Jun 10 15:51:08 2013 +0300
+++ b/src/file.cpp	Mon Jun 10 16:17:24 2013 +0300
@@ -31,6 +31,7 @@
 #include "history.h"
 #include "dialogs.h"
 #include "gldraw.h"
+#include "string.h"
 
 cfg (str, io_ldpath, "");
 cfg (str, io_recentfiles, "");
@@ -437,34 +438,28 @@
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
 void addRecentFile (str path) {
-	long pos = io_recentfiles.value.first (path);
+	vector<str> rfiles = io_recentfiles.value.split ('@');
+	
+	ulong idx = rfiles.find (path);
 	
 	// If this file already is in the list, pop it out.
-	if (pos != -1) {
-		if (~io_recentfiles.value == ~path)
+	if (idx != -1u) {
+		if (rfiles.size () == 1)
 			return; // only recent file - do nothing
 		
 		// Pop it out.
-		str front = io_recentfiles.value.substr (0, pos);
-		str back;
-		
-		if (pos + ~path + 1 < io_recentfiles.value.len ())
-			back = io_recentfiles.value.substr (pos + ~path + 1, -1);
-		else
-			back = "";
-		
-		io_recentfiles.value = front + back;
+		rfiles.erase (idx);
 	}
 	
 	// If there's too many recent files, drop one out.
-	while (io_recentfiles.value.count ('@') > 3)
-		io_recentfiles.value = io_recentfiles.value.substr (io_recentfiles.value.first ("@") + 1, -1);
+	while (rfiles.size () > (5 - 1))
+		rfiles.erase (0);
 	
 	// Add the file
-	if (~io_recentfiles.value > 0)
-		io_recentfiles.value += "@";
+	rfiles.push_back (path);
 	
-	io_recentfiles += path;
+	// Rebuild the config string
+	io_recentfiles = str::join (rfiles, "@");
 	
 	config::save ();
 	g_win->updateRecentFilesMenu ();
--- a/src/string.cpp	Mon Jun 10 15:51:08 2013 +0300
+++ b/src/string.cpp	Mon Jun 10 16:17:24 2013 +0300
@@ -418,4 +418,17 @@
 
 String::operator const QString () const {
 	return chars ();
+}
+
+str String::join (const vector<str>& items, const str& delim) {
+	str text;
+	
+	for (const str& item : items) {
+		if (item != items[0])
+			text += delim;
+		
+		text += item;
+	}
+	
+	return text;
 }
\ No newline at end of file
--- a/src/string.h	Mon Jun 10 15:51:08 2013 +0300
+++ b/src/string.h	Mon Jun 10 16:17:24 2013 +0300
@@ -91,6 +91,8 @@
 	operator QString              ();
 	operator const QString        () const;
 	
+	static str     join           (const vector<str>& items, const str& delim);
+	
 private:
 	std::string m_string;
 } str;
--- a/src/types.h	Mon Jun 10 15:51:08 2013 +0300
+++ b/src/types.h	Mon Jun 10 16:17:24 2013 +0300
@@ -257,6 +257,18 @@
 		std::sort (begin (), end ());
 	}
 	
+	ulong find (const T& needle) {
+		ulong i = 0;
+		for (const T& hay : *this) {
+			if (hay == needle)
+				return i;
+			
+			i++;
+		}
+		
+		return -1u;
+	}
+	
 private:
 	std::vector<T> m_vect;
 };

mercurial