- now prints the filesize upon successful save

Mon, 07 Jul 2014 18:52:10 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Mon, 07 Jul 2014 18:52:10 +0300
changeset 834
3e697ba996e8
parent 833
ad681d97d341
child 835
268413885cb1

- now prints the filesize upon successful save

src/ldDocument.cc file | annotate | diff | comparison | revisions
src/ldDocument.h file | annotate | diff | comparison | revisions
src/mainWindow.cc file | annotate | diff | comparison | revisions
src/miscallenous.cc file | annotate | diff | comparison | revisions
src/miscallenous.h file | annotate | diff | comparison | revisions
--- a/src/ldDocument.cc	Mon Jul 07 15:38:45 2014 +0300
+++ b/src/ldDocument.cc	Mon Jul 07 18:52:10 2014 +0300
@@ -654,10 +654,9 @@
 	// If this file already is in the list, pop it out.
 	if (idx != -1)
 	{
-		if (rfiles.size() == 1)
-			return; // only recent file - abort and do nothing
+		if (idx == rfiles.size() - 1)
+			return; // first recent file - abort and do nothing
 
-		// Pop it out.
 		rfiles.removeAt (idx);
 	}
 
@@ -738,13 +737,13 @@
 
 // =============================================================================
 //
-bool LDDocument::save (QString savepath)
+bool LDDocument::save (QString path, int64* sizeptr)
 {
 	if (isImplicit())
 		return false;
 
-	if (not savepath.length())
-		savepath = fullPath();
+	if (not path.length())
+		path = fullPath();
 
 	// If the second object in the list holds the file name, update that now.
 	LDObjectPtr nameObject = getObject (1);
@@ -755,7 +754,7 @@
 
 		if (nameComment->text().left (6) == "Name: ")
 		{
-			QString newname = shortenName (savepath);
+			QString newname = shortenName (path);
 			nameComment->setText (format ("Name: %1", newname));
 			g_win->buildObjList();
 		}
@@ -763,12 +762,21 @@
 
 	QByteArray data;
 
+	if (sizeptr != null)
+		*sizeptr = 0;
+
 	// File is open, now save the model to it. Note that LDraw requires files to
 	// have DOS line endings, so we terminate the lines with \r\n.
 	for (LDObjectPtr obj : objects())
-		data.append ((obj->asText() + "\r\n").toUtf8());
+	{
+		QByteArray subdata ((obj->asText() + "\r\n").toUtf8());
+		data.append (subdata);
 
-	QFile f (savepath);
+		if (sizeptr != null)
+			*sizeptr += subdata.size();
+	}
+
+	QFile f (path);
 
 	if (not f.open (QIODevice::WriteOnly))
 		return false;
@@ -778,8 +786,8 @@
 
 	// We have successfully saved, update the save position now.
 	setSavePosition (history()->position());
-	setFullPath (savepath);
-	setName (shortenName (savepath));
+	setFullPath (path);
+	setName (shortenName (path));
 
 	g_win->updateDocumentListItem (self().toStrongRef());
 	g_win->updateTitle();
--- a/src/ldDocument.h	Mon Jul 07 15:38:45 2014 +0300
+++ b/src/ldDocument.h	Mon Jul 07 18:52:10 2014 +0300
@@ -95,7 +95,7 @@
 	void insertObj (int pos, LDObjectPtr obj);
 	int getObjectCount() const;
 	LDObjectPtr getObject (int pos) const;
-	bool save (QString path = ""); // Saves this file to disk.
+	bool save (QString path = "", int64* sizeptr = null); // Saves this file to disk.
 	void swapObjects (LDObjectPtr one, LDObjectPtr other);
 	bool isSafeToClose(); // Perform safety checks. Do this before closing any files!
 	void setObject (int idx, LDObjectPtr obj);
--- a/src/mainWindow.cc	Mon Jul 07 15:38:45 2014 +0300
+++ b/src/mainWindow.cc	Mon Jul 07 18:52:10 2014 +0300
@@ -717,6 +717,7 @@
 bool MainWindow::save (LDDocumentPtr doc, bool saveAs)
 {
 	QString path = doc->fullPath();
+	int64 savesize;
 
 	if (saveAs || path.isEmpty())
 	{
@@ -738,12 +739,12 @@
 		}
 	}
 
-	if (doc->save (path))
+	if (doc->save (path, &savesize))
 	{
 		if (doc == getCurrentDocument())
 			updateTitle();
 
-		print ("Saved to %1.", path);
+		print ("Saved to %1 (%2)", path, prettyFileSize (savesize));
 
 		// Add it to recent files
 		addRecentFile (path);
--- a/src/miscallenous.cc	Mon Jul 07 15:38:45 2014 +0300
+++ b/src/miscallenous.cc	Mon Jul 07 18:52:10 2014 +0300
@@ -290,3 +290,20 @@
 	assert (false);
 	return 0.0;
 }
+
+
+// =============================================================================
+//
+QString prettyFileSize (qint64 size)
+{
+	QString result;
+
+	if (size < 1024)
+		return QString::number (size) + " bytes";
+	else if (size < (1024 * 1024))
+		return QString::number ((double) size / 1024, 'f', 1) + " Kb";
+	else if (size < (1024 * 1024 * 1024))
+		return QString::number ((double) size / (1024 * 1024), 'f', 1) + " Mb";
+	else
+		return QString::number ((double) size / (1024 * 1024 * 1024), 'f', 1) + " Gb";
+}
--- a/src/miscallenous.h	Mon Jul 07 15:38:45 2014 +0300
+++ b/src/miscallenous.h	Mon Jul 07 18:52:10 2014 +0300
@@ -44,6 +44,7 @@
 
 double getCoordinate (const Vertex& a, Axis ax);
 QString join (QList< StringFormatArg > vals, QString delim = " ");
+QString prettyFileSize (qint64 size);
 
 // Grid stuff
 struct gridinfo

mercurial