Simplify file saving code.

There is no need to record default file format choice, as
the first one is selected when an empty string is passed.
This commit is contained in:
whitequark 2016-01-11 08:44:56 +00:00
parent 259d8e0d38
commit 750842610c
6 changed files with 44 additions and 36 deletions

View File

@ -749,8 +749,6 @@ bool SolveSpace::GetOpenFile(std::string &file, const std::string &defExtension,
bool SolveSpace::GetSaveFile(std::string &file, const std::string &defExtension,
const char *selPattern) {
NSSavePanel *panel = [NSSavePanel savePanel];
[panel setNameFieldStringValue:[@"untitled"
stringByAppendingPathExtension:[NSString stringWithUTF8String:defExtension.c_str()]]];
SaveFormatController *controller =
[[SaveFormatController alloc] initWithNibName:@"SaveFormatAccessory" bundle:nil];
@ -773,8 +771,16 @@ bool SolveSpace::GetSaveFile(std::string &file, const std::string &defExtension,
[filterExtensions componentsJoinedByString:@", "]]];
[extensions addObject:[filterExtensions objectAtIndex:0]];
}
[button selectItemAtIndex:[extensions
indexOfObject:[NSString stringWithUTF8String:defExtension.c_str()]]];
int extensionIndex = 0;
if(defExtension != "") {
extensionIndex = [extensions indexOfObject:
[NSString stringWithUTF8String:defExtension.c_str()]];
}
[button selectItemAtIndex:extensionIndex];
[panel setNameFieldStringValue:[@"untitled"
stringByAppendingPathExtension:[extensions objectAtIndex:extensionIndex]]];
if([panel runModal] == NSFileHandlingPanelOKButton) {
file = [[NSFileManager defaultManager]

View File

@ -188,7 +188,7 @@ void Group::MenuGroup(int id) {
g.type = IMPORTED;
g.opA = SS.GW.activeGroup;
if(g.impFile.empty()) {
if(!GetOpenFile(g.impFile, SLVS_EXT, SLVS_PATTERN)) return;
if(!GetOpenFile(g.impFile, "", SLVS_PATTERN)) return;
}
// Assign the default name of the group based on the name of

View File

@ -1023,9 +1023,9 @@ void RefreshRecentMenus(void) {
/* Save/load */
static void FiltersFromPattern(const std::string &active, const char *patterns,
Gtk::FileChooser &chooser) {
Glib::ustring uactive = "*." + active;
static std::string FiltersFromPattern(const std::string &active, const char *patterns,
Gtk::FileChooser &chooser) {
Glib::ustring uactive = active;
Glib::ustring upatterns = patterns;
#ifdef HAVE_GTK3
@ -1044,7 +1044,9 @@ static void FiltersFromPattern(const std::string &active, const char *patterns,
has_name = true;
} else {
filter->add_pattern(frag);
if(uactive == frag)
if(uactive == "")
uactive = frag.substr(2);
if("*." + uactive == frag)
is_active = true;
if(desc == "")
desc = frag;
@ -1075,16 +1077,19 @@ static void FiltersFromPattern(const std::string &active, const char *patterns,
last = i + 1;
}
return uactive;
}
bool GetOpenFile(std::string &file, const std::string &active, const char *patterns) {
bool GetOpenFile(std::string &file, const std::string &activeOrEmpty,
const char *patterns) {
Gtk::FileChooserDialog chooser(*GW, "SolveSpace - Open File");
chooser.set_filename(file);
chooser.add_button("_Cancel", Gtk::RESPONSE_CANCEL);
chooser.add_button("_Open", Gtk::RESPONSE_OK);
chooser.set_current_folder(CnfThawString("", "FileChooserPath"));
FiltersFromPattern(active, patterns, chooser);
FiltersFromPattern(activeOrEmpty, patterns, chooser);
if(chooser.run() == Gtk::RESPONSE_OK) {
CnfFreezeString(chooser.get_current_folder(), "FileChooserPath");
@ -1133,14 +1138,15 @@ static void ChooserFilterChanged(Gtk::FileChooserDialog *chooser)
}
}
bool GetSaveFile(std::string &file, const std::string &active, const char *patterns) {
bool GetSaveFile(std::string &file, const std::string &activeOrEmpty,
const char *patterns) {
Gtk::FileChooserDialog chooser(*GW, "SolveSpace - Save File",
Gtk::FILE_CHOOSER_ACTION_SAVE);
chooser.set_do_overwrite_confirmation(true);
chooser.add_button("_Cancel", Gtk::RESPONSE_CANCEL);
chooser.add_button("_Save", Gtk::RESPONSE_OK);
FiltersFromPattern(active, patterns, chooser);
std::string active = FiltersFromPattern(activeOrEmpty, patterns, chooser);
chooser.set_current_folder(CnfThawString("", "FileChooserPath"));
chooser.set_current_name(std::string("untitled.") + active);

View File

@ -349,7 +349,7 @@ bool SolveSpaceUI::GetFilenameAndSave(bool saveAs) {
std::string prevSaveFile = saveFile;
if(saveAs || saveFile.empty()) {
if(!GetSaveFile(saveFile, SLVS_EXT, SLVS_PATTERN)) return false;
if(!GetSaveFile(saveFile, "", SLVS_PATTERN)) return false;
// need to get new filename directly into saveFile, since that
// determines impFileRel path
}
@ -433,7 +433,7 @@ void SolveSpaceUI::MenuFile(int id) {
if(!SS.OkayToStartNewFile()) break;
std::string newFile;
if(GetOpenFile(newFile, SLVS_EXT, SLVS_PATTERN)) {
if(GetOpenFile(newFile, "", SLVS_PATTERN)) {
SS.OpenFile(newFile);
}
break;
@ -449,15 +449,15 @@ void SolveSpaceUI::MenuFile(int id) {
case GraphicsWindow::MNU_EXPORT_PNG: {
std::string exportFile;
if(!GetSaveFile(exportFile, PNG_EXT, PNG_PATTERN)) break;
if(!GetSaveFile(exportFile, "", PNG_PATTERN)) break;
SS.ExportAsPngTo(exportFile);
break;
}
case GraphicsWindow::MNU_EXPORT_VIEW: {
std::string exportFile;
std::string exportFormat = CnfThawString(VEC_EXT, "ViewExportFormat");
if(!GetSaveFile(exportFile, exportFormat, VEC_PATTERN)) break;
if(!GetSaveFile(exportFile, CnfThawString("", "ViewExportFormat"),
VEC_PATTERN)) break;
CnfFreezeString(Extension(exportFile), "ViewExportFormat");
// If the user is exporting something where it would be
@ -478,8 +478,8 @@ void SolveSpaceUI::MenuFile(int id) {
case GraphicsWindow::MNU_EXPORT_WIREFRAME: {
std::string exportFile;
std::string exportFormat = CnfThawString(V3D_EXT, "WireframeExportFormat");
if(!GetSaveFile(exportFile, exportFormat, V3D_PATTERN)) break;
if(!GetSaveFile(exportFile, CnfThawString("", "WireframeExportFormat"),
V3D_PATTERN)) break;
CnfFreezeString(Extension(exportFile), "WireframeExportFormat");
SS.ExportViewOrWireframeTo(exportFile, true);
@ -488,8 +488,8 @@ void SolveSpaceUI::MenuFile(int id) {
case GraphicsWindow::MNU_EXPORT_SECTION: {
std::string exportFile;
std::string exportFormat = CnfThawString(VEC_EXT, "SectionExportFormat");
if(!GetSaveFile(exportFile, exportFormat, VEC_PATTERN)) break;
if(!GetSaveFile(exportFile, CnfThawString("", "SectionExportFormat"),
VEC_PATTERN)) break;
CnfFreezeString(Extension(exportFile), "SectionExportFormat");
SS.ExportSectionTo(exportFile);
@ -498,8 +498,8 @@ void SolveSpaceUI::MenuFile(int id) {
case GraphicsWindow::MNU_EXPORT_MESH: {
std::string exportFile;
std::string exportFormat = CnfThawString(MESH_EXT, "MeshExportFormat");
if(!GetSaveFile(exportFile, exportFormat, MESH_PATTERN)) break;
if(!GetSaveFile(exportFile, CnfThawString("", "MeshExportFormat"),
MESH_PATTERN)) break;
CnfFreezeString(Extension(exportFile), "MeshExportFormat");
SS.ExportMeshTo(exportFile);
@ -508,8 +508,8 @@ void SolveSpaceUI::MenuFile(int id) {
case GraphicsWindow::MNU_EXPORT_SURFACES: {
std::string exportFile;
std::string exportFormat = CnfThawString(SRF_EXT, "SurfacesExportFormat");
if(!GetSaveFile(exportFile, exportFormat, SRF_PATTERN)) break;
if(!GetSaveFile(exportFile, CnfThawString("", "SurfacesExportFormat"),
SRF_PATTERN)) break;
CnfFreezeString(Extension(exportFile), "SurfacesExportFormat");
StepFileWriter sfw = {};
@ -731,7 +731,7 @@ void SolveSpaceUI::MenuAnalyze(int id) {
case GraphicsWindow::MNU_STOP_TRACING: {
std::string exportFile;
if(GetSaveFile(exportFile, CSV_EXT, CSV_PATTERN)) {
if(GetSaveFile(exportFile, "", CSV_PATTERN)) {
FILE *f = ssfopen(exportFile, "wb");
if(f) {
int i;

View File

@ -182,20 +182,16 @@ int LoadAutosaveYesNo(void);
// SolveSpace native file format
#define SLVS_PATTERN PAT1("SolveSpace Models", "slvs") ENDPAT
#define SLVS_EXT "slvs"
// PNG format bitmap
#define PNG_PATTERN PAT1("PNG", "png") ENDPAT
#define PNG_EXT "png"
// Triangle mesh
#define MESH_PATTERN \
PAT1("STL Mesh", "stl") \
PAT1("Wavefront OBJ Mesh", "obj") \
PAT1("Three.js-compatible JavaScript Mesh", "js") \
ENDPAT
#define MESH_EXT "stl"
// NURBS surfaces
#define SRF_PATTERN PAT2("STEP File", "step", "stp") ENDPAT
#define SRF_EXT "step"
// 2d vector (lines and curves) format
#define VEC_PATTERN \
PAT1("PDF File", "pdf") \
@ -206,16 +202,16 @@ int LoadAutosaveYesNo(void);
PAT2("HPGL File", "plt", "hpgl") \
PAT1("G Code", "txt") \
ENDPAT
#define VEC_EXT "pdf"
// 3d vector (wireframe lines and curves) format
#define V3D_PATTERN \
PAT2("STEP File", "step", "stp") \
PAT1("DXF File", "dxf") \
ENDPAT
#define V3D_EXT "step"
// Comma-separated value, like a spreadsheet would use
#define CSV_PATTERN PAT1("CSV File", "csv") ENDPAT
#define CSV_EXT "csv"
#define CSV_PATTERN \
PAT1("CSV File", "csv") \
ENDPAT
bool GetSaveFile(std::string &filename, const std::string &defExtension,
const char *selPattern);
bool GetOpenFile(std::string &filename, const std::string &defExtension,

View File

@ -365,7 +365,7 @@ void TextWindow::ScreenBackgroundImage(int link, uint32_t v) {
png_info *info_ptr = NULL;
std::string importFile;
if(!GetOpenFile(importFile, PNG_EXT, PNG_PATTERN)) goto err;
if(!GetOpenFile(importFile, "", PNG_PATTERN)) goto err;
f = ssfopen(importFile, "rb");
if(!f) goto err;