Warn for save failures with the save and save as actions, also provide with a button to save the file under a different name.

Mon, 22 Apr 2013 16:30:33 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Mon, 22 Apr 2013 16:30:33 +0300
changeset 128
73a7edf82ca9
parent 127
a6e2067bb2f1
child 129
9cf313447c8f

Warn for save failures with the save and save as actions, also provide with a button to save the file under a different name.

file.cpp file | annotate | diff | comparison | revisions
gui_actions.cpp file | annotate | diff | comparison | revisions
--- a/file.cpp	Mon Apr 22 16:06:41 2013 +0300
+++ b/file.cpp	Mon Apr 22 16:30:33 2013 +0300
@@ -317,7 +317,6 @@
 	savePos = History::pos ();
 	
 	g_ForgeWindow->setTitle ();
-	logf ("Saved successfully to %s\n", path.chars ());
 	
 	return true;
 }
--- a/gui_actions.cpp	Mon Apr 22 16:06:41 2013 +0300
+++ b/gui_actions.cpp	Mon Apr 22 16:30:33 2013 +0300
@@ -49,36 +49,56 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-void doSaveAs () {
-	str zName;
-	zName += QFileDialog::getSaveFileName (g_ForgeWindow, "Save As",
+void doSave (bool saveAs) {
+	str path = g_CurrentFile->zFileName;
+	
+	if (~path == 0 || saveAs) {
+		path = QFileDialog::getSaveFileName (g_ForgeWindow, "Save As",
 		"", "LDraw files (*.dat *.ldr)");
+		
+		if (~path == 0) {
+			// User didn't give a file name. This happens if the user cancelled
+			// saving in the save file dialog. Abort.
+			return;
+		}
+	}
 	
-	if (~zName && g_CurrentFile->save (zName))
-		g_CurrentFile->zFileName = zName;
+	if (g_CurrentFile->save (path)) {
+		g_CurrentFile->zFileName = path;
+		g_ForgeWindow->setTitle ();
+		
+		logf ("Saved successfully to %s\n", path.chars ());
+	} else {
+		setlocale (LC_ALL, "C");
+		
+		// Tell the user the save failed, and give the option for saving as with it.
+		QMessageBox dlg (QMessageBox::Warning, "Save Failure",
+			format ("Failed to save to %s\nReason: %s", path.chars(), strerror (g_CurrentFile->lastError)),
+			QMessageBox::Close, g_ForgeWindow);
+		
+		QPushButton* saveAsBtn = new QPushButton ("Save As");
+		saveAsBtn->setIcon (getIcon ("file-save-as"));
+		dlg.addButton (saveAsBtn, QMessageBox::ActionRole);
+		dlg.setDefaultButton (QMessageBox::Close);
+		dlg.exec ();
+		
+		if (dlg.clickedButton () == saveAsBtn)
+			doSave (true); // yay recursion!
+	}
 }
 
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
 ACTION (save, "&Save", "file-save", "Save the part model.", CTRL (S)) {
-	if (!~g_CurrentFile->zFileName) {
-		// If we don't have a file name, this is an anonymous file created
-		// with the new file command. We cannot save without a name so ask
-		// the user for one.
-		doSaveAs ();
-		return;
-	}
-	
-	g_CurrentFile->save ();
+	doSave (false);
 }
 
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-ACTION (saveAs, "Save &As", "file-save-as", "Save the part model to a specific file.", CTRL_SHIFT (S))
-{
-	doSaveAs ();
+ACTION (saveAs, "Save &As", "file-save-as", "Save the part model to a specific file.", CTRL_SHIFT (S)) {
+	doSave (true);
 }
 
 // =============================================================================

mercurial