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, bool SolveSpace::GetSaveFile(std::string &file, const std::string &defExtension,
const char *selPattern) { const char *selPattern) {
NSSavePanel *panel = [NSSavePanel savePanel]; NSSavePanel *panel = [NSSavePanel savePanel];
[panel setNameFieldStringValue:[@"untitled"
stringByAppendingPathExtension:[NSString stringWithUTF8String:defExtension.c_str()]]];
SaveFormatController *controller = SaveFormatController *controller =
[[SaveFormatController alloc] initWithNibName:@"SaveFormatAccessory" bundle:nil]; [[SaveFormatController alloc] initWithNibName:@"SaveFormatAccessory" bundle:nil];
@ -773,8 +771,16 @@ bool SolveSpace::GetSaveFile(std::string &file, const std::string &defExtension,
[filterExtensions componentsJoinedByString:@", "]]]; [filterExtensions componentsJoinedByString:@", "]]];
[extensions addObject:[filterExtensions objectAtIndex:0]]; [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) { if([panel runModal] == NSFileHandlingPanelOKButton) {
file = [[NSFileManager defaultManager] file = [[NSFileManager defaultManager]

View File

@ -188,7 +188,7 @@ void Group::MenuGroup(int id) {
g.type = IMPORTED; g.type = IMPORTED;
g.opA = SS.GW.activeGroup; g.opA = SS.GW.activeGroup;
if(g.impFile.empty()) { 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 // Assign the default name of the group based on the name of

View File

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

View File

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

View File

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