WIP: Thumbnails improvements: Merge "thumbnails" and "thumbnails_format" options into just one "thumbnails" option with coString type

This commit is contained in:
YuSanka 2023-08-14 16:50:53 +02:00
parent 3cfe2f4a3a
commit a6dea25243
9 changed files with 189 additions and 99 deletions

View File

@ -886,14 +886,37 @@ void GCodeGenerator::_do_export(Print& print, GCodeOutputStream &file, Thumbnail
// Write information on the generator. // Write information on the generator.
file.write_format("; %s\n\n", Slic3r::header_slic3r_generated().c_str()); file.write_format("; %s\n\n", Slic3r::header_slic3r_generated().c_str());
// Unit tests or command line slicing may not define "thumbnails" or "thumbnails_format". // ??? Unit tests or command line slicing may not define "thumbnails" or "thumbnails_format".
// If "thumbnails_format" is not defined, export to PNG. // ??? If "thumbnails_format" is not defined, export to PNG.
if (const auto [thumbnails, thumbnails_format] = std::make_pair(
print.full_print_config().option<ConfigOptionPoints>("thumbnails"), // generate thumbnails data to process it
print.full_print_config().option<ConfigOptionEnum<GCodeThumbnailsFormat>>("thumbnails_format"));
thumbnails) std::vector<std::pair<GCodeThumbnailsFormat, Vec2d>> thumbnails_list;
GCodeThumbnails::export_thumbnails_to_file( if (const auto thumbnails_value = print.full_print_config().option<ConfigOptionString>("thumbnails")) {
thumbnail_cb, thumbnails->values, thumbnails_format ? thumbnails_format->value : GCodeThumbnailsFormat::PNG, std::string str = thumbnails_value->value;
std::istringstream is(str);
std::string point_str;
while (std::getline(is, point_str, ',')) {
Vec2d point(Vec2d::Zero());
GCodeThumbnailsFormat format;
std::istringstream iss(point_str);
std::string coord_str;
if (std::getline(iss, coord_str, 'x')) {
std::istringstream(coord_str) >> point(0);
if (std::getline(iss, coord_str, '/')) {
std::istringstream(coord_str) >> point(1);
std::string ext_str;
if (std::getline(iss, ext_str, '/'))
format = ext_str == "JPG" ? GCodeThumbnailsFormat::JPG :
ext_str == "QOI" ? GCodeThumbnailsFormat::QOI :GCodeThumbnailsFormat::PNG;
}
}
thumbnails_list.emplace_back(std::make_pair(format, point));
}
}
if (!thumbnails_list.empty())
GCodeThumbnails::export_thumbnails_to_file(thumbnail_cb, thumbnails_list,
[&file](const char* sz) { file.write(sz); }, [&file](const char* sz) { file.write(sz); },
[&print]() { print.throw_if_canceled(); }); [&print]() { print.throw_if_canceled(); });

View File

@ -24,12 +24,13 @@ struct CompressedImageBuffer
std::unique_ptr<CompressedImageBuffer> compress_thumbnail(const ThumbnailData &data, GCodeThumbnailsFormat format); std::unique_ptr<CompressedImageBuffer> compress_thumbnail(const ThumbnailData &data, GCodeThumbnailsFormat format);
template<typename WriteToOutput, typename ThrowIfCanceledCallback> template<typename WriteToOutput, typename ThrowIfCanceledCallback>
inline void export_thumbnails_to_file(ThumbnailsGeneratorCallback &thumbnail_cb, const std::vector<Vec2d> &sizes, GCodeThumbnailsFormat format, WriteToOutput output, ThrowIfCanceledCallback throw_if_canceled) inline void export_thumbnails_to_file(ThumbnailsGeneratorCallback &thumbnail_cb, const std::vector<std::pair<GCodeThumbnailsFormat, Vec2d>>& thumbnails_list, WriteToOutput output, ThrowIfCanceledCallback throw_if_canceled)
{ {
// Write thumbnails using base64 encoding // Write thumbnails using base64 encoding
if (thumbnail_cb != nullptr) { if (thumbnail_cb != nullptr) {
for (const auto& [format, size] : thumbnails_list) {
static constexpr const size_t max_row_length = 78; static constexpr const size_t max_row_length = 78;
ThumbnailsList thumbnails = thumbnail_cb(ThumbnailsParams{ sizes, true, true, true, true }); ThumbnailsList thumbnails = thumbnail_cb(ThumbnailsParams{ {size}, true, true, true, true });
for (const ThumbnailData& data : thumbnails) for (const ThumbnailData& data : thumbnails)
if (data.is_valid()) { if (data.is_valid()) {
auto compressed = compress_thumbnail(data, format); auto compressed = compress_thumbnail(data, format);
@ -54,6 +55,7 @@ inline void export_thumbnails_to_file(ThumbnailsGeneratorCallback &thumbnail_cb,
} }
} }
} }
}
} // namespace Slic3r::GCodeThumbnails } // namespace Slic3r::GCodeThumbnails

View File

@ -285,12 +285,12 @@ void PrintConfigDef::init_common_params()
def->mode = comAdvanced; def->mode = comAdvanced;
def->set_default_value(new ConfigOptionFloat(0.)); def->set_default_value(new ConfigOptionFloat(0.));
def = this->add("thumbnails", coPoints); def = this->add("thumbnails", coString);
def->label = L("G-code thumbnails"); def->label = L("G-code thumbnails");
def->tooltip = L("Picture sizes to be stored into a .gcode and .sl1 / .sl1s files, in the following format: \"XxY, XxY, ...\""); def->tooltip = L("Picture sizes to be stored into a .gcode and .sl1 / .sl1s files, in the following format: \"XxYxEXT, XxYxEXT, ...\"");
def->mode = comExpert; def->mode = comExpert;
def->gui_type = ConfigOptionDef::GUIType::one_string; def->gui_type = ConfigOptionDef::GUIType::one_string;
def->set_default_value(new ConfigOptionPoints()); def->set_default_value(new ConfigOptionString());
def = this->add("thumbnails_format", coEnum); def = this->add("thumbnails_format", coEnum);
def->label = L("Format of G-code thumbnails"); def->label = L("Format of G-code thumbnails");

View File

@ -835,7 +835,7 @@ PRINT_CONFIG_CLASS_DERIVED_DEFINE(
((ConfigOptionInt, standby_temperature_delta)) ((ConfigOptionInt, standby_temperature_delta))
((ConfigOptionInts, temperature)) ((ConfigOptionInts, temperature))
((ConfigOptionInt, threads)) ((ConfigOptionInt, threads))
((ConfigOptionPoints, thumbnails)) ((ConfigOptionString, thumbnails))
((ConfigOptionEnum<GCodeThumbnailsFormat>, thumbnails_format)) ((ConfigOptionEnum<GCodeThumbnailsFormat>, thumbnails_format))
((ConfigOptionFloat, top_solid_infill_acceleration)) ((ConfigOptionFloat, top_solid_infill_acceleration))
((ConfigOptionFloat, travel_acceleration)) ((ConfigOptionFloat, travel_acceleration))

View File

@ -8,6 +8,7 @@
#include "format.hpp" #include "format.hpp"
#include "libslic3r/PrintConfig.hpp" #include "libslic3r/PrintConfig.hpp"
#include "libslic3r/enum_bitmask.hpp"
#include <regex> #include <regex>
#include <wx/numformatter.h> #include <wx/numformatter.h>
@ -26,7 +27,13 @@
#define wxOSX false #define wxOSX false
#endif #endif
namespace Slic3r { namespace GUI { namespace Slic3r {
enum class ThumbnailError : int { InvalidVal, OutOfRange, InvalidExt };
using ThumbnailErrors = enum_bitmask<ThumbnailError>;
ENABLE_ENUM_BITMASK_OPERATORS(ThumbnailError);
namespace GUI {
wxString double_to_string(double const value, const int max_precision /*= 4*/) wxString double_to_string(double const value, const int max_precision /*= 4*/)
{ {
@ -58,14 +65,79 @@ wxString double_to_string(double const value, const int max_precision /*= 4*/)
return s; return s;
} }
wxString get_thumbnails_string(const std::vector<Vec2d>& values) bool is_valid_thumbnails_extention(wxString& ext)
{ {
wxString ret_str; ext.UpperCase();
for (size_t i = 0; i < values.size(); ++ i) { static const std::vector<wxString> extentions = { "PNG", "JPG", "QOI" };
const Vec2d& el = values[i];
ret_str += wxString::Format((i == 0) ? "%ix%i" : ", %ix%i", int(el[0]), int(el[1])); return std::find(extentions.begin(), extentions.end(), ext) != extentions.end();
} }
return ret_str;
ThumbnailErrors validate_thumbnails_string(wxString& str, const wxString& def_ext = "PNG")
{
bool invalid_val, out_of_range_val, invalid_ext;
invalid_val = out_of_range_val = invalid_ext = false;
str.Replace(" ", wxEmptyString, true);
if (!str.IsEmpty()) {
std::vector<std::pair<Vec2d, std::string>> out_thumbnails;
wxStringTokenizer thumbnails(str, ",");
while (thumbnails.HasMoreTokens()) {
wxString token = thumbnails.GetNextToken();
double x, y;
wxStringTokenizer thumbnail(token, "x");
if (thumbnail.HasMoreTokens()) {
wxString x_str = thumbnail.GetNextToken();
if (x_str.ToDouble(&x) && thumbnail.HasMoreTokens()) {
wxStringTokenizer y_and_ext(thumbnail.GetNextToken(), "/");
wxString y_str = y_and_ext.GetNextToken();
if (y_str.ToDouble(&y)) {
// thumbnail has no extension
if (0 < x && x < 1000 && 0 < y && y < 1000) {
wxString ext = y_and_ext.HasMoreTokens() ? y_and_ext.GetNextToken() : def_ext;
bool is_valid_ext = is_valid_thumbnails_extention(ext);
invalid_ext |= !is_valid_ext;
out_thumbnails.push_back({ Vec2d(x, y), into_u8(is_valid_ext ? ext : def_ext) });
continue;
}
out_of_range_val |= true;
continue;
}
}
}
invalid_val |= true;
}
str.Clear();
for (const auto& [size, ext] : out_thumbnails)
str += format_wxstr("%1%x%2%/%3%, ", size.x(), size.y(), ext);
str.resize(str.Len()-2);
}
ThumbnailErrors errors = only_if(invalid_val, ThumbnailError::InvalidVal) |
only_if(invalid_ext, ThumbnailError::InvalidExt) |
only_if(out_of_range_val, ThumbnailError::OutOfRange);
return errors;
}
wxString get_valid_thumbnails_string(const DynamicPrintConfig& config)
{
// >>> ysFIXME - temporary code, till "thumbnails_format" options exists in config
wxString format = "PNG";
if (const ConfigOptionDef* opt = config.def()->get("thumbnails_format"))
if (auto label = opt->enum_def->enum_to_label(config.option("thumbnails_format")->getInt());
label.has_value())
format = from_u8(*label);
// <<<
wxString str = from_u8(config.opt_string("thumbnails"));
validate_thumbnails_string(str, format);
return str;
} }
@ -355,56 +427,35 @@ void Field::get_value_by_opt_type(wxString& str, const bool check_value/* = true
} }
} }
if (m_opt.opt_key == "thumbnails") {
wxString str_out = str;
ThumbnailErrors errors = validate_thumbnails_string(str_out);
if (errors != enum_bitmask<ThumbnailError>()) {
set_value(str_out, true);
wxString error_str;
if (errors.has(ThumbnailError::InvalidVal))
error_str += format_wxstr(_L("Invalid input format. Expected vector of dimensions in the following format: \"%1%\""), "XxYxEXT, XxYxEXT, ...");
if (errors.has(ThumbnailError::OutOfRange)) {
if (!error_str.empty())
error_str += "\n\n";
error_str += _L("Input value is out of range");
}
if (errors.has(ThumbnailError::InvalidExt)) {
if (!error_str.empty())
error_str += "\n\n";
error_str += _L("Some input extention is invalid");
}
show_error(m_parent, error_str);
}
else if (str_out != str) {
str = str_out;
set_value(str, true);
}
}
m_value = into_u8(str); m_value = into_u8(str);
break; }
case coPoints: {
std::vector<Vec2d> out_values;
str.Replace(" ", wxEmptyString, true);
if (!str.IsEmpty()) {
bool invalid_val = false;
bool out_of_range_val = false;
wxStringTokenizer thumbnails(str, ",");
while (thumbnails.HasMoreTokens()) {
wxString token = thumbnails.GetNextToken();
double x, y;
wxStringTokenizer thumbnail(token, "x");
if (thumbnail.HasMoreTokens()) {
wxString x_str = thumbnail.GetNextToken();
if (x_str.ToDouble(&x) && thumbnail.HasMoreTokens()) {
wxString y_str = thumbnail.GetNextToken();
if (y_str.ToDouble(&y) && !thumbnail.HasMoreTokens()) {
if (0 < x && x < 1000 && 0 < y && y < 1000) {
out_values.push_back(Vec2d(x, y));
continue;
}
out_of_range_val = true;
break; break;
} }
}
}
invalid_val = true;
break;
}
if (out_of_range_val) {
wxString text_value;
if (!m_value.empty())
text_value = get_thumbnails_string(boost::any_cast<std::vector<Vec2d>>(m_value));
set_value(text_value, true);
show_error(m_parent, _L("Input value is out of range"));
}
else if (invalid_val) {
wxString text_value;
if (!m_value.empty())
text_value = get_thumbnails_string(boost::any_cast<std::vector<Vec2d>>(m_value));
set_value(text_value, true);
show_error(m_parent, format_wxstr(_L("Invalid input format. Expected vector of dimensions in the following format: \"%1%\""),"XxY, XxY, ..." ));
}
}
m_value = out_values;
break; }
default: default:
break; break;
@ -484,9 +535,6 @@ void TextCtrl::BUILD() {
text_value = vec->get_at(m_opt_idx); text_value = vec->get_at(m_opt_idx);
break; break;
} }
case coPoints:
text_value = get_thumbnails_string(m_opt.get_default_value<ConfigOptionPoints>()->values);
break;
default: default:
break; break;
} }

View File

@ -37,7 +37,7 @@ using t_change = std::function<void(const t_config_option_key&, const boost::any
using t_back_to_init = std::function<void(const std::string&)>; using t_back_to_init = std::function<void(const std::string&)>;
wxString double_to_string(double const value, const int max_precision = 4); wxString double_to_string(double const value, const int max_precision = 4);
wxString get_thumbnails_string(const std::vector<Vec2d>& values); wxString get_valid_thumbnails_string(const DynamicPrintConfig& config);
class UndoValueUIManager class UndoValueUIManager
{ {

View File

@ -904,9 +904,13 @@ boost::any ConfigOptionsGroup::get_config_value(const DynamicPrintConfig& config
ret = double_to_string(val); ret = double_to_string(val);
} }
break; break;
case coString: case coString: {
if (opt_key == "thumbnails")
ret = get_valid_thumbnails_string(config);
else
ret = from_u8(config.opt_string(opt_key)); ret = from_u8(config.opt_string(opt_key));
break; break;
}
case coStrings: case coStrings:
if (opt_key == "compatible_printers" || opt_key == "compatible_prints" || opt_key == "gcode_substitutions") { if (opt_key == "compatible_printers" || opt_key == "compatible_prints" || opt_key == "gcode_substitutions") {
ret = config.option<ConfigOptionStrings>(opt_key)->values; ret = config.option<ConfigOptionStrings>(opt_key)->values;
@ -946,8 +950,6 @@ boost::any ConfigOptionsGroup::get_config_value(const DynamicPrintConfig& config
case coPoints: case coPoints:
if (opt_key == "bed_shape") if (opt_key == "bed_shape")
ret = config.option<ConfigOptionPoints>(opt_key)->values; ret = config.option<ConfigOptionPoints>(opt_key)->values;
else if (opt_key == "thumbnails")
ret = get_thumbnails_string(config.option<ConfigOptionPoints>(opt_key)->values);
else else
ret = config.option<ConfigOptionPoints>(opt_key)->get_at(idx); ret = config.option<ConfigOptionPoints>(opt_key)->get_at(idx);
break; break;

View File

@ -636,6 +636,21 @@ void Tab::update_changed_ui()
if (tab->m_sys_extruders_count != tab->m_extruders_count) if (tab->m_sys_extruders_count != tab->m_extruders_count)
nonsys_options.emplace_back("extruders_count"); nonsys_options.emplace_back("extruders_count");
} }
// "thumbnails" can not containe a extentions in old config but are valid and use PNG extention by default
// So, check if "thumbnails" is really changed
// We will compare full strings for thumnails instead of exactly config values
{
auto check_thumbnails_option = [](std::vector<std::string>& keys, const DynamicPrintConfig& config, const DynamicPrintConfig& config_new) {
if (auto it = std::find(keys.begin(), keys.end(), "thumbnails"); it != keys.end())
if (get_valid_thumbnails_string(config) == get_valid_thumbnails_string(config_new))
// if those strings are actually the same, erase them from the list of dirty oprions
keys.erase(it);
};
check_thumbnails_option(dirty_options, m_presets->get_edited_preset().config, m_presets->get_selected_preset().config);
if (const Preset* parent_preset = m_presets->get_selected_preset_parent())
check_thumbnails_option(nonsys_options, m_presets->get_edited_preset().config, parent_preset->config);
}
} }
for (auto& it : m_options_list) for (auto& it : m_options_list)
@ -2526,7 +2541,6 @@ void TabPrinter::build_fff()
option = optgroup->get_option("thumbnails"); option = optgroup->get_option("thumbnails");
option.opt.full_width = true; option.opt.full_width = true;
optgroup->append_single_option_line(option); optgroup->append_single_option_line(option);
optgroup->append_single_option_line("thumbnails_format");
optgroup->append_single_option_line("silent_mode"); optgroup->append_single_option_line("silent_mode");
optgroup->append_single_option_line("remaining_times"); optgroup->append_single_option_line("remaining_times");

View File

@ -1154,8 +1154,11 @@ static wxString get_string_value(std::string opt_key, const DynamicPrintConfig&
} }
return _L("Undef"); return _L("Undef");
} }
case coString: case coString: {
if (opt_key == "thumbnails")
return get_valid_thumbnails_string(config);
return from_u8(config.opt_string(opt_key)); return from_u8(config.opt_string(opt_key));
}
case coStrings: { case coStrings: {
const ConfigOptionStrings* strings = config.opt<ConfigOptionStrings>(opt_key); const ConfigOptionStrings* strings = config.opt<ConfigOptionStrings>(opt_key);
if (strings) { if (strings) {
@ -1204,8 +1207,6 @@ static wxString get_string_value(std::string opt_key, const DynamicPrintConfig&
BedShape shape(*config.option<ConfigOptionPoints>(opt_key)); BedShape shape(*config.option<ConfigOptionPoints>(opt_key));
return shape.get_full_name_with_params(); return shape.get_full_name_with_params();
} }
if (opt_key == "thumbnails")
return get_thumbnails_string(config.option<ConfigOptionPoints>(opt_key)->values);
Vec2d val = config.opt<ConfigOptionPoints>(opt_key)->get_at(opt_idx); Vec2d val = config.opt<ConfigOptionPoints>(opt_key)->get_at(opt_idx);
return from_u8((boost::format("[%1%]") % ConfigOptionPoint(val).serialize()).str()); return from_u8((boost::format("[%1%]") % ConfigOptionPoint(val).serialize()).str());