Code refactoring for last commit

This commit is contained in:
YuSanka 2020-08-19 15:35:58 +02:00
parent 15285a68a0
commit 8af49d7d87
3 changed files with 167 additions and 255 deletions

View file

@ -21,44 +21,72 @@
namespace Slic3r { namespace Slic3r {
namespace GUI { namespace GUI {
void BedShapeDialog::build_dialog(const ConfigOptionPoints& default_pt, const ConfigOptionString& custom_texture, const ConfigOptionString& custom_model) BedShape::BedShape(const ConfigOptionPoints& points)
{ {
SetFont(wxGetApp().normal_font()); auto polygon = Polygon::new_scale(points.values);
m_panel = new BedShapePanel(this);
m_panel->build_panel(default_pt, custom_texture, custom_model);
auto main_sizer = new wxBoxSizer(wxVERTICAL); // is this a rectangle ?
main_sizer->Add(m_panel, 1, wxEXPAND); if (points.size() == 4) {
main_sizer->Add(CreateButtonSizer(wxOK | wxCANCEL), 0, wxALIGN_CENTER_HORIZONTAL | wxBOTTOM, 10); auto lines = polygon.lines();
if (lines[0].parallel_to(lines[2]) && lines[1].parallel_to(lines[3])) {
// okay, it's a rectangle
// find origin
coordf_t x_min, x_max, y_min, y_max;
x_max = x_min = points.values[0](0);
y_max = y_min = points.values[0](1);
for (auto pt : points.values)
{
x_min = std::min(x_min, pt(0));
x_max = std::max(x_max, pt(0));
y_min = std::min(y_min, pt(1));
y_max = std::max(y_max, pt(1));
}
SetSizer(main_sizer); m_type = Type::Rectangular;
SetMinSize(GetSize()); m_rectSize = Vec2d(x_max - x_min, y_max - y_min);
main_sizer->SetSizeHints(this); m_rectOrigin = Vec2d(-x_min, -y_min);
this->Bind(wxEVT_CLOSE_WINDOW, ([this](wxCloseEvent& evt) { return;
EndModal(wxID_CANCEL); }
})); }
// is this a circle ?
{
// Analyze the array of points.Do they reside on a circle ?
auto center = polygon.bounding_box().center();
std::vector<double> vertex_distances;
double avg_dist = 0;
for (auto pt : polygon.points)
{
double distance = (pt - center).cast<double>().norm();
vertex_distances.push_back(distance);
avg_dist += distance;
}
avg_dist /= vertex_distances.size();
bool defined_value = true;
for (auto el : vertex_distances)
{
if (abs(el - avg_dist) > 10 * SCALED_EPSILON)
defined_value = false;
break;
}
if (defined_value) {
// all vertices are equidistant to center
m_type = Type::Circular;
m_diameter = unscale<double>(avg_dist * 2);
return;
}
}
if (points.size() < 3)
return;
// This is a custom bed shape, use the polygon provided.
m_type = Type::Custom;
} }
void BedShapeDialog::on_dpi_changed(const wxRect &suggested_rect)
{
const int& em = em_unit();
m_panel->m_shape_options_book->SetMinSize(wxSize(25 * em, -1));
for (auto og : m_panel->m_optgroups)
og->msw_rescale();
const wxSize& size = wxSize(50 * em, -1);
SetMinSize(size);
SetSize(size);
Refresh();
}
const std::string BedShapePanel::NONE = "None";
const std::string BedShapePanel::EMPTY_STRING = "";
static std::string get_option_label(BedShape::Parameter param) static std::string get_option_label(BedShape::Parameter param)
{ {
switch (param) { switch (param) {
@ -118,22 +146,73 @@ wxString BedShape::get_name(Type type)
} }
} }
size_t BedShape::get_type()
{
return static_cast<size_t>(m_type == Type::Invalid ? Type::Rectangular : m_type);
}
wxString BedShape::get_full_name_with_params() wxString BedShape::get_full_name_with_params()
{ {
wxString out = _L("Shape") + ": " + get_name(type); wxString out = _L("Shape") + ": " + get_name(m_type);
if (type == Type::Rectangular) { if (m_type == Type::Rectangular) {
out += "\n" + get_option_label(Parameter::RectSize) + +": [" + ConfigOptionPoint(rectSize).serialize() + "]"; out += "\n" + _(get_option_label(Parameter::RectSize)) + ": [" + ConfigOptionPoint(m_rectSize).serialize() + "]";
out += "\n" + get_option_label(Parameter::RectOrigin) + +": [" + ConfigOptionPoint(rectOrigin).serialize() + "]"; out += "\n" + _(get_option_label(Parameter::RectOrigin))+ ": [" + ConfigOptionPoint(m_rectOrigin).serialize() + "]";
} }
else if (type == Type::Circular) else if (m_type == Type::Circular)
out += "\n" + get_option_label(Parameter::Diameter) + +": [" + double_to_string(diameter) + "]"; out += "\n" + _L(get_option_label(Parameter::Diameter)) + ": [" + double_to_string(m_diameter) + "]";
else if (type == Type::Custom)
out += "\n" + double_to_string(diameter);
return out; return out;
} }
void BedShape::apply_optgroup_values(ConfigOptionsGroupShp optgroup)
{
if (m_type == Type::Rectangular || m_type == Type::Invalid) {
optgroup->set_value("rect_size" , new ConfigOptionPoints{ m_rectSize });
optgroup->set_value("rect_origin" , new ConfigOptionPoints{ m_rectOrigin });
}
else if (m_type == Type::Circular)
optgroup->set_value("diameter", double_to_string(m_diameter));
}
void BedShapeDialog::build_dialog(const ConfigOptionPoints& default_pt, const ConfigOptionString& custom_texture, const ConfigOptionString& custom_model)
{
SetFont(wxGetApp().normal_font());
m_panel = new BedShapePanel(this);
m_panel->build_panel(default_pt, custom_texture, custom_model);
auto main_sizer = new wxBoxSizer(wxVERTICAL);
main_sizer->Add(m_panel, 1, wxEXPAND);
main_sizer->Add(CreateButtonSizer(wxOK | wxCANCEL), 0, wxALIGN_CENTER_HORIZONTAL | wxBOTTOM, 10);
SetSizer(main_sizer);
SetMinSize(GetSize());
main_sizer->SetSizeHints(this);
this->Bind(wxEVT_CLOSE_WINDOW, ([this](wxCloseEvent& evt) {
EndModal(wxID_CANCEL);
}));
}
void BedShapeDialog::on_dpi_changed(const wxRect &suggested_rect)
{
const int& em = em_unit();
m_panel->m_shape_options_book->SetMinSize(wxSize(25 * em, -1));
for (auto og : m_panel->m_optgroups)
og->msw_rescale();
const wxSize& size = wxSize(50 * em, -1);
SetMinSize(size);
SetSize(size);
Refresh();
}
const std::string BedShapePanel::NONE = "None";
const std::string BedShapePanel::EMPTY_STRING = "";
void BedShapePanel::build_panel(const ConfigOptionPoints& default_pt, const ConfigOptionString& custom_texture, const ConfigOptionString& custom_model) void BedShapePanel::build_panel(const ConfigOptionPoints& default_pt, const ConfigOptionString& custom_texture, const ConfigOptionString& custom_model)
{ {
m_shape = default_pt.values; m_shape = default_pt.values;
@ -147,36 +226,6 @@ void BedShapePanel::build_panel(const ConfigOptionPoints& default_pt, const Conf
m_shape_options_book = new wxChoicebook(this, wxID_ANY, wxDefaultPosition, wxSize(25*wxGetApp().em_unit(), -1), wxCHB_TOP); m_shape_options_book = new wxChoicebook(this, wxID_ANY, wxDefaultPosition, wxSize(25*wxGetApp().em_unit(), -1), wxCHB_TOP);
sbsizer->Add(m_shape_options_book); sbsizer->Add(m_shape_options_book);
/* auto optgroup = init_shape_options_page(_(L("Rectangular")));
ConfigOptionDef def;
def.type = coPoints;
def.set_default_value(new ConfigOptionPoints{ Vec2d(200, 200) });
def.min = 0;
def.max = 1200;
def.label = L("Size");
def.tooltip = L("Size in X and Y of the rectangular plate.");
Option option(def, "rect_size");
optgroup->append_single_option_line(option);
def.type = coPoints;
def.set_default_value(new ConfigOptionPoints{ Vec2d(0, 0) });
def.min = -600;
def.max = 600;
def.label = L("Origin");
def.tooltip = L("Distance of the 0,0 G-code coordinate from the front left corner of the rectangle.");
option = Option(def, "rect_origin");
optgroup->append_single_option_line(option);
optgroup = init_shape_options_page(_(L("Circular")));
def.type = coFloat;
def.set_default_value(new ConfigOptionFloat(200));
def.sidetext = L("mm");
def.label = L("Diameter");
def.tooltip = L("Diameter of the print bed. It is assumed that origin (0,0) is located in the center.");
option = Option(def, "diameter");
optgroup->append_single_option_line(option);
*/
auto optgroup = init_shape_options_page(BedShape::get_name(BedShape::Type::Rectangular)); auto optgroup = init_shape_options_page(BedShape::get_name(BedShape::Type::Rectangular));
BedShape::append_option_line(optgroup, BedShape::Parameter::RectSize); BedShape::append_option_line(optgroup, BedShape::Parameter::RectSize);
BedShape::append_option_line(optgroup, BedShape::Parameter::RectOrigin); BedShape::append_option_line(optgroup, BedShape::Parameter::RectOrigin);
@ -184,7 +233,6 @@ void BedShapePanel::build_panel(const ConfigOptionPoints& default_pt, const Conf
optgroup = init_shape_options_page(BedShape::get_name(BedShape::Type::Circular)); optgroup = init_shape_options_page(BedShape::get_name(BedShape::Type::Circular));
BedShape::append_option_line(optgroup, BedShape::Parameter::Diameter); BedShape::append_option_line(optgroup, BedShape::Parameter::Diameter);
// optgroup = init_shape_options_page(_(L("Custom")));
optgroup = init_shape_options_page(BedShape::get_name(BedShape::Type::Custom)); optgroup = init_shape_options_page(BedShape::get_name(BedShape::Type::Custom));
Line line{ "", "" }; Line line{ "", "" };
@ -233,10 +281,6 @@ void BedShapePanel::build_panel(const ConfigOptionPoints& default_pt, const Conf
update_preview(); update_preview();
} }
#define SHAPE_RECTANGULAR 0
#define SHAPE_CIRCULAR 1
#define SHAPE_CUSTOM 2
// Called from the constructor. // Called from the constructor.
// Create a panel for a rectangular / circular / custom bed shape. // Create a panel for a rectangular / circular / custom bed shape.
ConfigOptionsGroupShp BedShapePanel::init_shape_options_page(const wxString& title) ConfigOptionsGroupShp BedShapePanel::init_shape_options_page(const wxString& title)
@ -421,83 +465,18 @@ wxPanel* BedShapePanel::init_model_panel()
// with the list of points in the ini file directly. // with the list of points in the ini file directly.
void BedShapePanel::set_shape(const ConfigOptionPoints& points) void BedShapePanel::set_shape(const ConfigOptionPoints& points)
{ {
auto polygon = Polygon::new_scale(points.values); BedShape shape(points);
// is this a rectangle ? m_shape_options_book->SetSelection(shape.get_type());
if (points.size() == 4) { shape.apply_optgroup_values(m_optgroups[shape.get_type()]);
auto lines = polygon.lines();
if (lines[0].parallel_to(lines[2]) && lines[1].parallel_to(lines[3])) {
// okay, it's a rectangle
// find origin
coordf_t x_min, x_max, y_min, y_max;
x_max = x_min = points.values[0](0);
y_max = y_min = points.values[0](1);
for (auto pt : points.values)
{
x_min = std::min(x_min, pt(0));
x_max = std::max(x_max, pt(0));
y_min = std::min(y_min, pt(1));
y_max = std::max(y_max, pt(1));
}
auto origin = new ConfigOptionPoints{ Vec2d(-x_min, -y_min) }; // Copy the polygon to the canvas, make a copy of the array, if custom shape is selected
if (shape.is_custom())
m_loaded_shape = points.values;
m_shape_options_book->SetSelection(SHAPE_RECTANGULAR);
auto optgroup = m_optgroups[SHAPE_RECTANGULAR];
optgroup->set_value("rect_size", new ConfigOptionPoints{ Vec2d(x_max - x_min, y_max - y_min) });//[x_max - x_min, y_max - y_min]);
optgroup->set_value("rect_origin", origin);
update_shape();
return;
}
}
// is this a circle ?
{
// Analyze the array of points.Do they reside on a circle ?
auto center = polygon.bounding_box().center();
std::vector<double> vertex_distances;
double avg_dist = 0;
for (auto pt: polygon.points)
{
double distance = (pt - center).cast<double>().norm();
vertex_distances.push_back(distance);
avg_dist += distance;
}
avg_dist /= vertex_distances.size();
bool defined_value = true;
for (auto el: vertex_distances)
{
if (abs(el - avg_dist) > 10 * SCALED_EPSILON)
defined_value = false;
break;
}
if (defined_value) {
// all vertices are equidistant to center
m_shape_options_book->SetSelection(SHAPE_CIRCULAR);
auto optgroup = m_optgroups[SHAPE_CIRCULAR];
boost::any ret = wxNumberFormatter::ToString(unscale<double>(avg_dist * 2), 0);
optgroup->set_value("diameter", ret);
update_shape();
return;
}
}
if (points.size() < 3) {
// Invalid polygon.Revert to default bed dimensions.
m_shape_options_book->SetSelection(SHAPE_RECTANGULAR);
auto optgroup = m_optgroups[SHAPE_RECTANGULAR];
optgroup->set_value("rect_size", new ConfigOptionPoints{ Vec2d(200, 200) });
optgroup->set_value("rect_origin", new ConfigOptionPoints{ Vec2d(0, 0) });
update_shape();
return;
}
// This is a custom bed shape, use the polygon provided.
m_shape_options_book->SetSelection(SHAPE_CUSTOM);
// Copy the polygon to the canvas, make a copy of the array.
m_loaded_shape = points.values;
update_shape(); update_shape();
return;
} }
void BedShapePanel::update_preview() void BedShapePanel::update_preview()
@ -510,21 +489,20 @@ void BedShapePanel::update_preview()
void BedShapePanel::update_shape() void BedShapePanel::update_shape()
{ {
auto page_idx = m_shape_options_book->GetSelection(); auto page_idx = m_shape_options_book->GetSelection();
if (page_idx == SHAPE_RECTANGULAR) { auto opt_group = m_optgroups[page_idx];
BedShape::Type page_type = static_cast<BedShape::Type>(page_idx);
if (page_type == BedShape::Type::Rectangular) {
Vec2d rect_size(Vec2d::Zero()); Vec2d rect_size(Vec2d::Zero());
Vec2d rect_origin(Vec2d::Zero()); Vec2d rect_origin(Vec2d::Zero());
try{
rect_size = boost::any_cast<Vec2d>(m_optgroups[SHAPE_RECTANGULAR]->get_value("rect_size")); } try { rect_size = boost::any_cast<Vec2d>(opt_group->get_value("rect_size")); }
catch (const std::exception & /* e */) { catch (const std::exception& /* e */) { return; }
return;
} try { rect_origin = boost::any_cast<Vec2d>(opt_group->get_value("rect_origin")); }
try { catch (const std::exception & /* e */) { return; }
rect_origin = boost::any_cast<Vec2d>(m_optgroups[SHAPE_RECTANGULAR]->get_value("rect_origin"));
}
catch (const std::exception & /* e */) {
return;
}
auto x = rect_size(0); auto x = rect_size(0);
auto y = rect_size(1); auto y = rect_size(1);
// empty strings or '-' or other things // empty strings or '-' or other things
@ -546,14 +524,11 @@ void BedShapePanel::update_shape()
Vec2d(x1, y1), Vec2d(x1, y1),
Vec2d(x0, y1) }; Vec2d(x0, y1) };
} }
else if(page_idx == SHAPE_CIRCULAR) { else if (page_type == BedShape::Type::Circular) {
double diameter; double diameter;
try{ try { diameter = boost::any_cast<double>(opt_group->get_value("diameter")); }
diameter = boost::any_cast<double>(m_optgroups[SHAPE_CIRCULAR]->get_value("diameter")); catch (const std::exception & /* e */) { return; }
}
catch (const std::exception & /* e */) {
return;
}
if (diameter == 0.0) return ; if (diameter == 0.0) return ;
auto r = diameter / 2; auto r = diameter / 2;
auto twopi = 2 * PI; auto twopi = 2 * PI;
@ -565,7 +540,7 @@ void BedShapePanel::update_shape()
} }
m_shape = points; m_shape = points;
} }
else if (page_idx == SHAPE_CUSTOM) else if (page_type == BedShape::Type::Custom)
m_shape = m_loaded_shape; m_shape = m_loaded_shape;
update_preview(); update_preview();
@ -578,8 +553,8 @@ void BedShapePanel::load_stl()
if (dialog.ShowModal() != wxID_OK) if (dialog.ShowModal() != wxID_OK)
return; return;
m_custom_shape = dialog.GetPath().ToUTF8().data(); std::string file_name = dialog.GetPath().ToUTF8().data();
if (!boost::algorithm::iends_with(m_custom_shape, ".stl")) if (!boost::algorithm::iends_with(file_name, ".stl"))
{ {
show_error(this, _(L("Invalid file format."))); show_error(this, _(L("Invalid file format.")));
return; return;
@ -589,7 +564,7 @@ void BedShapePanel::load_stl()
Model model; Model model;
try { try {
model = Model::read_from_file(m_custom_shape); model = Model::read_from_file(file_name);
} }
catch (std::exception &) { catch (std::exception &) {
show_error(this, _(L("Error! Invalid model"))); show_error(this, _(L("Error! Invalid model")));

View file

@ -17,8 +17,8 @@ class ConfigOptionsGroup;
using ConfigOptionsGroupShp = std::shared_ptr<ConfigOptionsGroup>; using ConfigOptionsGroupShp = std::shared_ptr<ConfigOptionsGroup>;
struct BedShape { struct BedShape
{
enum class Type { enum class Type {
Rectangular = 0, Rectangular = 0,
Circular, Circular,
@ -32,83 +32,24 @@ struct BedShape {
Diameter Diameter
}; };
BedShape(const ConfigOptionPoints& points) { BedShape(const ConfigOptionPoints& points);
auto polygon = Polygon::new_scale(points.values);
// is this a rectangle ? bool is_custom() { return m_type == Type::Custom; }
if (points.size() == 4) {
auto lines = polygon.lines();
if (lines[0].parallel_to(lines[2]) && lines[1].parallel_to(lines[3])) {
// okay, it's a rectangle
// find origin
coordf_t x_min, x_max, y_min, y_max;
x_max = x_min = points.values[0](0);
y_max = y_min = points.values[0](1);
for (auto pt : points.values)
{
x_min = std::min(x_min, pt(0));
x_max = std::max(x_max, pt(0));
y_min = std::min(y_min, pt(1));
y_max = std::max(y_max, pt(1));
}
type = Type::Rectangular;
rectSize = Vec2d(x_max - x_min, y_max - y_min);
rectOrigin = Vec2d(-x_min, -y_min);
return;
}
}
// is this a circle ?
{
// Analyze the array of points.Do they reside on a circle ?
auto center = polygon.bounding_box().center();
std::vector<double> vertex_distances;
double avg_dist = 0;
for (auto pt : polygon.points)
{
double distance = (pt - center).cast<double>().norm();
vertex_distances.push_back(distance);
avg_dist += distance;
}
avg_dist /= vertex_distances.size();
bool defined_value = true;
for (auto el : vertex_distances)
{
if (abs(el - avg_dist) > 10 * SCALED_EPSILON)
defined_value = false;
break;
}
if (defined_value) {
// all vertices are equidistant to center
type = Type::Circular;
diameter = unscale<double>(avg_dist * 2);
return;
}
}
if (points.size() < 3) {
type = Type::Invalid;
return;
}
// This is a custom bed shape, use the polygon provided.
type = Type::Custom;
}
static void append_option_line(ConfigOptionsGroupShp optgroup, Parameter param); static void append_option_line(ConfigOptionsGroupShp optgroup, Parameter param);
static wxString get_name(Type type); static wxString get_name(Type type);
// convert Type to size_t
size_t get_type();
wxString get_full_name_with_params(); wxString get_full_name_with_params();
void apply_optgroup_values(ConfigOptionsGroupShp optgroup);
Type type = Type::Invalid; private:
Vec2d rectSize; Type m_type {Type::Invalid};
Vec2d rectOrigin; Vec2d m_rectSize {200, 200};
Vec2d m_rectOrigin {0, 0};
double diameter; double m_diameter {0};
}; };
class BedShapePanel : public wxPanel class BedShapePanel : public wxPanel
@ -119,7 +60,6 @@ class BedShapePanel : public wxPanel
Bed_2D* m_canvas; Bed_2D* m_canvas;
std::vector<Vec2d> m_shape; std::vector<Vec2d> m_shape;
std::vector<Vec2d> m_loaded_shape; std::vector<Vec2d> m_loaded_shape;
std::string m_custom_shape;
std::string m_custom_texture; std::string m_custom_texture;
std::string m_custom_model; std::string m_custom_model;

View file

@ -667,10 +667,10 @@ void UnsavedChangesDialog::show_info_line(Action action, std::string preset_name
text = _L("All changed options will be reverted."); text = _L("All changed options will be reverted.");
else { else {
if (action == Action::Save && preset_name.empty()) if (action == Action::Save && preset_name.empty())
text = _L("After press this button selected options will be saved"); text = _L("Press to save the selected options");
else { else {
std::string act_string = action == Action::Save ? _u8L("saved") : _u8L("moved"); std::string act_string = action == Action::Save ? _u8L("saved") : _u8L("moved");
text = from_u8((boost::format("After press this button selected options will be %1% to the preset \"%2%\".") % act_string % preset_name).str()); text = from_u8((boost::format("Press to %1% selected options to the preset \"%2%\".") % act_string % preset_name).str());
} }
text += "\n" + _L("Unselected options will be reverted."); text += "\n" + _L("Unselected options will be reverted.");
} }
@ -732,12 +732,12 @@ static std::string get_pure_opt_key(std::string opt_key)
static wxString get_string_value(std::string opt_key, const DynamicPrintConfig& config) static wxString get_string_value(std::string opt_key, const DynamicPrintConfig& config)
{ {
int opt_idx = get_id_from_opt_key(opt_key);
opt_key = get_pure_opt_key(opt_key); opt_key = get_pure_opt_key(opt_key);
if (config.option(opt_key)->is_nil()) if (config.option(opt_key)->is_nil())
return _L("N/A"); return _L("N/A");
int opt_idx = get_id_from_opt_key(opt_key);
wxString out; wxString out;
const ConfigOptionDef* opt = config.def()->get(opt_key); const ConfigOptionDef* opt = config.def()->get(opt_key);
@ -820,15 +820,12 @@ static wxString get_string_value(std::string opt_key, const DynamicPrintConfig&
break; break;
} }
case coPoints: { case coPoints: {
/*
if (opt_key == "bed_shape") { if (opt_key == "bed_shape") {
config.option<ConfigOptionPoints>(opt_key)->values = boost::any_cast<std::vector<Vec2d>>(value); BedShape shape(*config.option<ConfigOptionPoints>(opt_key));
break; return shape.get_full_name_with_params();
} }
ConfigOptionPoints* vec_new = new ConfigOptionPoints{ boost::any_cast<Vec2d>(value) }; Vec2d val = config.opt<ConfigOptionPoints>(opt_key)->get_at(opt_idx);
config.option<ConfigOptionPoints>(opt_key)->set_at(vec_new, opt_index, 0); return from_u8((boost::format("[%1%]") % ConfigOptionPoint(val).serialize()).str());
*/
return "Points";
} }
default: default:
break; break;