Fix of SPE-1850 GH #1221

Broken with 99861f1b6ec3793025d528f0badb63e837f8c808 SPE-1207
Custom file extension needs to be included in the list of extensions
of the file dialog. On MacOS the custom extension needs to be the
first one in the list as it will be forced upon the default file
name provided to the file dialog.
This commit is contained in:
Vojtech Bubnik 2023-08-31 16:38:26 +02:00
parent 175c4f99ce
commit e0f577a33c
4 changed files with 28 additions and 87 deletions

View File

@ -52,14 +52,4 @@
// Enable OpenGL debug messages using debug context
#define ENABLE_OPENGL_DEBUG_OPTION (1 && ENABLE_GL_CORE_PROFILE)
//====================
// 2.6.0.alpha1 techs
//====================
#define ENABLE_2_6_0_ALPHA1 1
// Enable alternative version of file_wildcards()
#define ENABLE_ALTERNATIVE_FILE_WILDCARDS_GENERATOR (1 && ENABLE_2_6_0_ALPHA1)
#endif // _prusaslicer_technologies_h_

View File

@ -496,87 +496,44 @@ static const FileWildcards file_wildcards_by_type[FT_SIZE] = {
/* FT_ZIP */ { "Zip files"sv, { ".zip"sv } },
};
static wxString file_wildcards(const FileWildcards& data)
{
std::string title;
std::string mask;
// Generate cumulative first item
for (const std::string_view& ext : data.file_extensions) {
if (title.empty()) {
title = "*";
title += ext;
mask = title;
}
else {
title += ", *";
title += ext;
mask += ";*";
mask += ext;
}
mask += ";*";
mask += boost::to_upper_copy(std::string(ext));
}
wxString ret = GUI::format_wxstr("%s (%s)|%s", data.title, title, mask);
// Adds an item for each of the extensions
if (data.file_extensions.size() > 1) {
for (const std::string_view& ext : data.file_extensions) {
title = "*";
title += ext;
ret += GUI::format_wxstr("|%s (%s)|%s", data.title, title, title);
}
}
return ret;
}
#if ENABLE_ALTERNATIVE_FILE_WILDCARDS_GENERATOR
wxString file_wildcards(FileType file_type)
{
const FileWildcards& data = file_wildcards_by_type[file_type];
return file_wildcards(data);
}
#else
// This function produces a Win32 file dialog file template mask to be consumed by wxWidgets on all platforms.
// The function accepts a custom extension parameter. If the parameter is provided, the custom extension
// will be added as a fist to the list. This is important for a "file save" dialog on OSX, which strips
// an extension from the provided initial file name and substitutes it with the default extension (the first one in the template).
wxString file_wildcards(FileType file_type, const std::string &custom_extension)
static wxString file_wildcards(const FileWildcards &wildcards, const std::string &custom_extension)
{
const FileWildcards& data = file_wildcards_by_type[file_type];
std::string title;
std::string mask;
std::string custom_ext_lower;
// Collects items for each of the extensions one by one.
wxString out_one_by_one;
auto add_single = [&out_one_by_one](const std::string_view title, const std::string_view ext) {
out_one_by_one += GUI::format_wxstr("|%s (*%s)|*%s", title, ext, ext);
};
if (! custom_extension.empty()) {
// Generate an extension into the title mask and into the list of extensions.
// Generate a custom extension into the title mask and into the list of extensions.
// Add default version (upper, lower or mixed) first based on custom extension provided.
title = std::string("*") + custom_extension;
mask = title;
add_single(wildcards.title, custom_extension);
custom_ext_lower = boost::to_lower_copy(custom_extension);
const std::string custom_ext_upper = boost::to_upper_copy(custom_extension);
if (custom_ext_lower == custom_extension) {
// Add a lower case version.
title = std::string("*") + custom_ext_lower;
mask = title;
// Add an upper case version.
mask += ";*";
mask += custom_ext_upper;
// Add one more variant - the upper case extension.
mask += ";*";
mask += custom_ext_upper;
add_single(wildcards.title, custom_ext_upper);
} else if (custom_ext_upper == custom_extension) {
// Add an upper case version.
title = std::string("*") + custom_ext_upper;
mask = title;
// Add a lower case version.
// Add one more variant - the lower case extension.
mask += ";*";
mask += custom_ext_lower;
} else {
// Add the mixed case version only.
title = std::string("*") + custom_extension;
mask = title;
add_single(wildcards.title, custom_ext_lower);
}
}
for (const std::string_view &ext : data.file_extensions)
for (const std::string_view &ext : wildcards.file_extensions)
// Only add an extension if it was not added first as the custom extension.
if (ext != custom_ext_lower) {
if (title.empty()) {
@ -591,11 +548,16 @@ wxString file_wildcards(FileType file_type, const std::string &custom_extension)
}
mask += ";*";
mask += boost::to_upper_copy(std::string(ext));
add_single(wildcards.title, ext);
}
return GUI::format_wxstr("%s (%s)|%s", data.title, title, mask);
return GUI::format_wxstr("%s (%s)|%s", wildcards.title, title, mask) + out_one_by_one;
}
wxString file_wildcards(FileType file_type, const std::string &custom_extension)
{
return file_wildcards(file_wildcards_by_type[file_type], custom_extension);
}
#endif // ENABLE_ALTERNATIVE_FILE_WILDCARDS_GENERATOR
wxString sla_wildcards(const char *formatid)
{
@ -617,7 +579,7 @@ wxString sla_wildcards(const char *formatid)
wc.file_extensions.emplace_back(ext);
}
ret = file_wildcards(wc);
ret = file_wildcards(wc, {});
}
if (ret.empty())

View File

@ -78,11 +78,7 @@ enum FileType
FT_SIZE,
};
#if ENABLE_ALTERNATIVE_FILE_WILDCARDS_GENERATOR
extern wxString file_wildcards(FileType file_type);
#else
extern wxString file_wildcards(FileType file_type, const std::string &custom_extension = std::string{});
#endif // ENABLE_ALTERNATIVE_FILE_WILDCARDS_GENERATOR
extern wxString file_wildcards(FileType file_type, const std::string &custom_extension = {});
wxString sla_wildcards(const char *formatid);

View File

@ -6440,19 +6440,12 @@ void Plater::export_gcode(bool prefer_removable)
fs::path output_path;
{
#if !ENABLE_ALTERNATIVE_FILE_WILDCARDS_GENERATOR
std::string ext = default_output_file.extension().string();
#endif // !ENABLE_ALTERNATIVE_FILE_WILDCARDS_GENERATOR
wxFileDialog dlg(this, (printer_technology() == ptFFF) ? _L("Save G-code file as:") : _L("Save SL1 / SL1S file as:"),
start_dir,
from_path(default_output_file.filename()),
#if ENABLE_ALTERNATIVE_FILE_WILDCARDS_GENERATOR
printer_technology() == ptFFF ? GUI::file_wildcards(FT_GCODE) :
GUI::sla_wildcards(p->sla_print.printer_config().sla_archive_format.value.c_str()),
#else
printer_technology() == ptFFF ? GUI::file_wildcards(FT_GCODE, ext) :
GUI::sla_wildcards(p->sla_print.printer_config().sla_archive_format.value.c_str()),
#endif // ENABLE_ALTERNATIVE_FILE_WILDCARDS_GENERATOR
wxFD_SAVE | wxFD_OVERWRITE_PROMPT
);
if (dlg.ShowModal() == wxID_OK) {