Merge branch 'master' into wipe_tower_improvements

This commit is contained in:
Lukas Matena 2018-03-16 14:06:23 +01:00
commit 3d6f6530c0
59 changed files with 4062 additions and 12410 deletions

View file

@ -46,6 +46,25 @@ void GLIndexedVertexArray::load_mesh_flat_shading(const TriangleMesh &mesh)
}
}
void GLIndexedVertexArray::load_mesh_full_shading(const TriangleMesh &mesh)
{
assert(triangle_indices.empty() && vertices_and_normals_interleaved_size == 0);
assert(quad_indices.empty() && triangle_indices_size == 0);
assert(vertices_and_normals_interleaved.size() % 6 == 0 && quad_indices_size == vertices_and_normals_interleaved.size());
this->vertices_and_normals_interleaved.reserve(this->vertices_and_normals_interleaved.size() + 3 * 3 * 2 * mesh.facets_count());
unsigned int vertices_count = 0;
for (int i = 0; i < mesh.stl.stats.number_of_facets; ++i) {
const stl_facet &facet = mesh.stl.facet_start[i];
for (int j = 0; j < 3; ++j)
this->push_geometry(facet.vertex[j].x, facet.vertex[j].y, facet.vertex[j].z, facet.normal.x, facet.normal.y, facet.normal.z);
this->push_triangle(vertices_count, vertices_count + 1, vertices_count + 2);
vertices_count += 3;
}
}
void GLIndexedVertexArray::finalize_geometry(bool use_VBOs)
{
assert(this->vertices_and_normals_interleaved_VBO_id == 0);
@ -173,6 +192,45 @@ void GLIndexedVertexArray::render(
glDisableClientState(GL_NORMAL_ARRAY);
}
const float GLVolume::SELECTED_COLOR[4] = { 0.0f, 1.0f, 0.0f, 1.0f };
const float GLVolume::HOVER_COLOR[4] = { 0.4f, 0.9f, 0.1f, 1.0f };
const float GLVolume::OUTSIDE_COLOR[4] = { 0.75f, 0.0f, 0.75f, 1.0f };
const float GLVolume::SELECTED_OUTSIDE_COLOR[4] = { 1.0f, 0.0f, 1.0f, 1.0f };
void GLVolume::set_render_color(float r, float g, float b, float a)
{
render_color[0] = r;
render_color[1] = g;
render_color[2] = b;
render_color[3] = a;
}
void GLVolume::set_render_color(const float* rgba, unsigned int size)
{
size = std::min((unsigned int)4, size);
for (int i = 0; i < size; ++i)
{
render_color[i] = rgba[i];
}
}
void GLVolume::set_render_color()
{
if (selected)
{
if (is_outside)
set_render_color(SELECTED_OUTSIDE_COLOR, 4);
else
set_render_color(SELECTED_COLOR, 4);
}
else if (hover)
set_render_color(HOVER_COLOR, 4);
else if (is_outside)
set_render_color(OUTSIDE_COLOR, 4);
else
set_render_color(color, 4);
}
void GLVolume::set_range(double min_z, double max_z)
{
this->qverts_range.first = 0;
@ -223,6 +281,51 @@ void GLVolume::render() const
glPopMatrix();
}
void GLVolume::render_using_layer_height() const
{
if (!is_active)
return;
GLint current_program_id;
glGetIntegerv(GL_CURRENT_PROGRAM, &current_program_id);
if ((layer_height_texture_data.shader_id > 0) && (layer_height_texture_data.shader_id != current_program_id))
glUseProgram(layer_height_texture_data.shader_id);
GLint z_to_texture_row_id = (layer_height_texture_data.shader_id > 0) ? glGetUniformLocation(layer_height_texture_data.shader_id, "z_to_texture_row") : -1;
GLint z_texture_row_to_normalized_id = (layer_height_texture_data.shader_id > 0) ? glGetUniformLocation(layer_height_texture_data.shader_id, "z_texture_row_to_normalized") : -1;
GLint z_cursor_id = (layer_height_texture_data.shader_id > 0) ? glGetUniformLocation(layer_height_texture_data.shader_id, "z_cursor") : -1;
GLint z_cursor_band_width_id = (layer_height_texture_data.shader_id > 0) ? glGetUniformLocation(layer_height_texture_data.shader_id, "z_cursor_band_width") : -1;
if (z_to_texture_row_id >= 0)
glUniform1f(z_to_texture_row_id, (GLfloat)layer_height_texture_z_to_row_id());
if (z_texture_row_to_normalized_id >= 0)
glUniform1f(z_texture_row_to_normalized_id, (GLfloat)(1.0f / layer_height_texture_height()));
if (z_cursor_id >= 0)
glUniform1f(z_cursor_id, (GLfloat)(bounding_box.max.z * layer_height_texture_data.z_cursor_relative));
if (z_cursor_band_width_id >= 0)
glUniform1f(z_cursor_band_width_id, (GLfloat)layer_height_texture_data.edit_band_width);
unsigned int w = layer_height_texture_width();
unsigned int h = layer_height_texture_height();
glBindTexture(GL_TEXTURE_2D, layer_height_texture_data.texture_id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA8, w / 2, h / 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, layer_height_texture_data_ptr_level0());
glTexSubImage2D(GL_TEXTURE_2D, 1, 0, 0, w / 2, h / 2, GL_RGBA, GL_UNSIGNED_BYTE, layer_height_texture_data_ptr_level1());
render();
glBindTexture(GL_TEXTURE_2D, 0);
if ((current_program_id > 0) && (layer_height_texture_data.shader_id != current_program_id))
glUseProgram(current_program_id);
}
void GLVolume::generate_layer_height_texture(PrintObject *print_object, bool force)
{
GLTexture *tex = this->layer_height_texture.get();
@ -288,7 +391,11 @@ std::vector<int> GLVolumeCollection::load_object(
color[3] = model_volume->modifier ? 0.5f : 1.f;
this->volumes.emplace_back(new GLVolume(color));
GLVolume &v = *this->volumes.back();
v.indexed_vertex_array.load_mesh_flat_shading(mesh);
if (use_VBOs)
v.indexed_vertex_array.load_mesh_full_shading(mesh);
else
v.indexed_vertex_array.load_mesh_flat_shading(mesh);
// finalize_geometry() clears the vertex arrays, therefore the bounding box has to be computed before finalize_geometry().
v.bounding_box = v.indexed_vertex_array.bounding_box();
v.indexed_vertex_array.finalize_geometry(use_VBOs);
@ -318,12 +425,17 @@ int GLVolumeCollection::load_wipe_tower_preview(
float color[4] = { 1.0f, 1.0f, 0.0f, 0.5f };
this->volumes.emplace_back(new GLVolume(color));
GLVolume &v = *this->volumes.back();
auto mesh = make_cube(width, depth, height);
mesh.translate(-width/2.f,-depth/2.f,0.f);
Point origin_of_rotation(0.f,0.f);
mesh.rotate(rotation_angle,&origin_of_rotation);
v.indexed_vertex_array.load_mesh_flat_shading(mesh);
if (use_VBOs)
v.indexed_vertex_array.load_mesh_full_shading(mesh);
else
v.indexed_vertex_array.load_mesh_flat_shading(mesh);
v.origin = Pointf3(pos_x, pos_y, 0.);
// finalize_geometry() clears the vertex arrays, therefore the bounding box has to be computed before finalize_geometry().
v.bounding_box = v.indexed_vertex_array.bounding_box();
@ -336,15 +448,19 @@ int GLVolumeCollection::load_wipe_tower_preview(
void GLVolumeCollection::render_VBOs() const
{
// glEnable(GL_BLEND);
// glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glCullFace(GL_BACK);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
::glEnable(GL_BLEND);
::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
::glCullFace(GL_BACK);
::glEnableClientState(GL_VERTEX_ARRAY);
::glEnableClientState(GL_NORMAL_ARRAY);
GLint current_program_id;
glGetIntegerv(GL_CURRENT_PROGRAM, &current_program_id);
::glGetIntegerv(GL_CURRENT_PROGRAM, &current_program_id);
GLint color_id = (current_program_id > 0) ? glGetUniformLocation(current_program_id, "uniform_color") : -1;
GLint print_box_min_id = (current_program_id > 0) ? glGetUniformLocation(current_program_id, "print_box.min") : -1;
GLint print_box_max_id = (current_program_id > 0) ? glGetUniformLocation(current_program_id, "print_box.max") : -1;
GLint print_box_origin_id = (current_program_id > 0) ? glGetUniformLocation(current_program_id, "print_box.volume_origin") : -1;
for (GLVolume *volume : this->volumes) {
if (!volume->is_active)
@ -352,61 +468,110 @@ void GLVolumeCollection::render_VBOs() const
if (!volume->indexed_vertex_array.vertices_and_normals_interleaved_VBO_id)
continue;
if (volume->layer_height_texture_data.can_use())
{
::glDisableClientState(GL_VERTEX_ARRAY);
::glDisableClientState(GL_NORMAL_ARRAY);
volume->generate_layer_height_texture(volume->layer_height_texture_data.print_object, false);
volume->render_using_layer_height();
::glEnableClientState(GL_VERTEX_ARRAY);
::glEnableClientState(GL_NORMAL_ARRAY);
continue;
}
volume->set_render_color();
GLsizei n_triangles = GLsizei(std::min(volume->indexed_vertex_array.triangle_indices_size, volume->tverts_range.second - volume->tverts_range.first));
GLsizei n_quads = GLsizei(std::min(volume->indexed_vertex_array.quad_indices_size, volume->qverts_range.second - volume->qverts_range.first));
if (n_triangles + n_quads == 0)
{
if (_render_interleaved_only_volumes.enabled)
::glDisableClientState(GL_VERTEX_ARRAY);
::glDisableClientState(GL_NORMAL_ARRAY);
if (color_id >= 0)
{
::glDisableClientState(GL_VERTEX_ARRAY);
::glDisableClientState(GL_NORMAL_ARRAY);
::glEnable(GL_BLEND);
::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
if (color_id >= 0)
{
float color[4];
::memcpy((void*)color, (const void*)volume->color, 3 * sizeof(float));
color[3] = _render_interleaved_only_volumes.alpha;
::glUniform4fv(color_id, 1, (const GLfloat*)color);
}
else
::glColor4f(volume->color[0], volume->color[1], volume->color[2], _render_interleaved_only_volumes.alpha);
volume->render();
::glDisable(GL_BLEND);
::glEnableClientState(GL_VERTEX_ARRAY);
::glEnableClientState(GL_NORMAL_ARRAY);
float color[4];
::memcpy((void*)color, (const void*)volume->render_color, 4 * sizeof(float));
::glUniform4fv(color_id, 1, (const GLfloat*)color);
}
else
::glColor4f(volume->render_color[0], volume->render_color[1], volume->render_color[2], volume->render_color[3]);
if (print_box_min_id != -1)
::glUniform3fv(print_box_min_id, 1, (const GLfloat*)print_box_min);
if (print_box_max_id != -1)
::glUniform3fv(print_box_max_id, 1, (const GLfloat*)print_box_max);
if (print_box_origin_id != -1)
{
float origin[4] = { (float)volume->origin.x, (float)volume->origin.y, (float)volume->origin.z, volume->outside_printer_detection_enabled ? 1.0f : 0.0f };
::glUniform4fv(print_box_origin_id, 1, (const GLfloat*)origin);
}
volume->render();
::glEnableClientState(GL_VERTEX_ARRAY);
::glEnableClientState(GL_NORMAL_ARRAY);
continue;
}
if (color_id >= 0)
glUniform4fv(color_id, 1, (const GLfloat*)volume->color);
::glUniform4fv(color_id, 1, (const GLfloat*)volume->render_color);
else
glColor4f(volume->color[0], volume->color[1], volume->color[2], volume->color[3]);
glBindBuffer(GL_ARRAY_BUFFER, volume->indexed_vertex_array.vertices_and_normals_interleaved_VBO_id);
glVertexPointer(3, GL_FLOAT, 6 * sizeof(float), (const void*)(3 * sizeof(float)));
glNormalPointer(GL_FLOAT, 6 * sizeof(float), nullptr);
::glColor4f(volume->render_color[0], volume->render_color[1], volume->render_color[2], volume->render_color[3]);
if (print_box_min_id != -1)
::glUniform3fv(print_box_min_id, 1, (const GLfloat*)print_box_min);
if (print_box_max_id != -1)
::glUniform3fv(print_box_max_id, 1, (const GLfloat*)print_box_max);
if (print_box_origin_id != -1)
{
float origin[4] = { (float)volume->origin.x, (float)volume->origin.y, (float)volume->origin.z, volume->outside_printer_detection_enabled ? 1.0f : 0.0f };
::glUniform4fv(print_box_origin_id, 1, (const GLfloat*)origin);
}
::glBindBuffer(GL_ARRAY_BUFFER, volume->indexed_vertex_array.vertices_and_normals_interleaved_VBO_id);
::glVertexPointer(3, GL_FLOAT, 6 * sizeof(float), (const void*)(3 * sizeof(float)));
::glNormalPointer(GL_FLOAT, 6 * sizeof(float), nullptr);
bool has_offset = (volume->origin.x != 0) || (volume->origin.y != 0) || (volume->origin.z != 0);
if (has_offset) {
::glPushMatrix();
::glTranslated(volume->origin.x, volume->origin.y, volume->origin.z);
}
if (n_triangles > 0) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, volume->indexed_vertex_array.triangle_indices_VBO_id);
glDrawElements(GL_TRIANGLES, n_triangles, GL_UNSIGNED_INT, (const void*)(volume->tverts_range.first * 4));
::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, volume->indexed_vertex_array.triangle_indices_VBO_id);
::glDrawElements(GL_TRIANGLES, n_triangles, GL_UNSIGNED_INT, (const void*)(volume->tverts_range.first * 4));
}
if (n_quads > 0) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, volume->indexed_vertex_array.quad_indices_VBO_id);
glDrawElements(GL_QUADS, n_quads, GL_UNSIGNED_INT, (const void*)(volume->qverts_range.first * 4));
::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, volume->indexed_vertex_array.quad_indices_VBO_id);
::glDrawElements(GL_QUADS, n_quads, GL_UNSIGNED_INT, (const void*)(volume->qverts_range.first * 4));
}
if (has_offset)
::glPopMatrix();
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
// glDisable(GL_BLEND);
::glBindBuffer(GL_ARRAY_BUFFER, 0);
::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
::glDisableClientState(GL_VERTEX_ARRAY);
::glDisableClientState(GL_NORMAL_ARRAY);
::glDisable(GL_BLEND);
}
void GLVolumeCollection::render_legacy() const
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glCullFace(GL_BACK);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
@ -416,27 +581,25 @@ void GLVolumeCollection::render_legacy() const
if (!volume->is_active)
continue;
volume->set_render_color();
GLsizei n_triangles = GLsizei(std::min(volume->indexed_vertex_array.triangle_indices_size, volume->tverts_range.second - volume->tverts_range.first));
GLsizei n_quads = GLsizei(std::min(volume->indexed_vertex_array.quad_indices_size, volume->qverts_range.second - volume->qverts_range.first));
if (n_triangles + n_quads == 0)
{
if (_render_interleaved_only_volumes.enabled)
{
::glDisableClientState(GL_VERTEX_ARRAY);
::glDisableClientState(GL_NORMAL_ARRAY);
::glEnable(GL_BLEND);
::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
::glDisableClientState(GL_VERTEX_ARRAY);
::glDisableClientState(GL_NORMAL_ARRAY);
::glColor4f(volume->color[0], volume->color[1], volume->color[2], _render_interleaved_only_volumes.alpha);
volume->render();
::glColor4f(volume->render_color[0], volume->render_color[1], volume->render_color[2], volume->render_color[3]);
volume->render();
::glEnableClientState(GL_VERTEX_ARRAY);
::glEnableClientState(GL_NORMAL_ARRAY);
::glDisable(GL_BLEND);
::glEnableClientState(GL_VERTEX_ARRAY);
::glEnableClientState(GL_NORMAL_ARRAY);
}
continue;
}
glColor4f(volume->color[0], volume->color[1], volume->color[2], volume->color[3]);
glColor4f(volume->render_color[0], volume->render_color[1], volume->render_color[2], volume->render_color[3]);
glVertexPointer(3, GL_FLOAT, 6 * sizeof(float), volume->indexed_vertex_array.vertices_and_normals_interleaved.data() + 3);
glNormalPointer(GL_FLOAT, 6 * sizeof(float), volume->indexed_vertex_array.vertices_and_normals_interleaved.data());
bool has_offset = volume->origin.x != 0 || volume->origin.y != 0 || volume->origin.z != 0;
@ -449,11 +612,37 @@ void GLVolumeCollection::render_legacy() const
if (n_quads > 0)
glDrawElements(GL_QUADS, n_quads, GL_UNSIGNED_INT, volume->indexed_vertex_array.quad_indices.data() + volume->qverts_range.first);
if (has_offset)
glPushMatrix();
glPopMatrix();
}
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisable(GL_BLEND);
}
void GLVolumeCollection::update_outside_state(const DynamicPrintConfig* config, bool all_inside)
{
if (config == nullptr)
return;
const ConfigOptionPoints* opt = dynamic_cast<const ConfigOptionPoints*>(config->option("bed_shape"));
if (opt == nullptr)
return;
BoundingBox bed_box_2D = get_extents(Polygon::new_scale(opt->values));
BoundingBoxf3 print_volume(Pointf3(unscale(bed_box_2D.min.x), unscale(bed_box_2D.min.y), 0.0), Pointf3(unscale(bed_box_2D.max.x), unscale(bed_box_2D.max.y), config->opt_float("max_print_height")));
for (GLVolume* volume : this->volumes)
{
if (all_inside)
{
volume->is_outside = false;
continue;
}
volume->is_outside = !print_volume.contains(volume->transformed_bounding_box());
}
}
std::vector<double> GLVolumeCollection::get_current_print_zs() const
@ -1137,6 +1326,100 @@ static void point3_to_verts(const Point3& point, double width, double height, GL
_3DScene::GCodePreviewVolumeIndex _3DScene::s_gcode_preview_volume_index;
_3DScene::LegendTexture _3DScene::s_legend_texture;
_3DScene::WarningTexture _3DScene::s_warning_texture;
unsigned int _3DScene::TextureBase::finalize()
{
if (!m_data.empty()) {
// sends buffer to gpu
::glGenTextures(1, &m_tex_id);
::glBindTexture(GL_TEXTURE_2D, m_tex_id);
::glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, (GLsizei)m_tex_width, (GLsizei)m_tex_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const GLvoid*)m_data.data());
::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
::glBindTexture(GL_TEXTURE_2D, 0);
m_data.clear();
}
return (m_tex_width > 0 && m_tex_height > 0) ? m_tex_id : 0;
}
void _3DScene::TextureBase::_destroy_texture()
{
if (m_tex_id > 0)
{
::glDeleteTextures(1, &m_tex_id);
m_tex_id = 0;
m_tex_height = 0;
m_tex_width = 0;
}
m_data.clear();
}
const unsigned char _3DScene::WarningTexture::Background_Color[3] = { 9, 91, 134 };
const unsigned char _3DScene::WarningTexture::Opacity = 255;
// Generate a texture data, but don't load it into the GPU yet, as the GPU context may not yet be valid.
bool _3DScene::WarningTexture::generate(const std::string& msg)
{
// Mark the texture as released, but don't release the texture from the GPU yet.
m_tex_width = m_tex_height = 0;
m_data.clear();
if (msg.empty())
return false;
wxMemoryDC memDC;
// select default font
memDC.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
// calculates texture size
wxCoord w, h;
memDC.GetTextExtent(msg, &w, &h);
m_tex_width = (unsigned int)w;
m_tex_height = (unsigned int)h;
// generates bitmap
wxBitmap bitmap(m_tex_width, m_tex_height);
#if defined(__APPLE__) || defined(_MSC_VER)
bitmap.UseAlpha();
#endif
memDC.SelectObject(bitmap);
memDC.SetBackground(wxBrush(wxColour(Background_Color[0], Background_Color[1], Background_Color[2])));
memDC.Clear();
memDC.SetTextForeground(*wxWHITE);
// draw message
memDC.DrawText(msg, 0, 0);
memDC.SelectObject(wxNullBitmap);
// Convert the bitmap into a linear data ready to be loaded into the GPU.
{
wxImage image = bitmap.ConvertToImage();
image.SetMaskColour(Background_Color[0], Background_Color[1], Background_Color[2]);
// prepare buffer
m_data.assign(4 * m_tex_width * m_tex_height, 0);
for (unsigned int h = 0; h < m_tex_height; ++h)
{
unsigned int hh = h * m_tex_width;
unsigned char* px_ptr = m_data.data() + 4 * hh;
for (unsigned int w = 0; w < m_tex_width; ++w)
{
*px_ptr++ = image.GetRed(w, h);
*px_ptr++ = image.GetGreen(w, h);
*px_ptr++ = image.GetBlue(w, h);
*px_ptr++ = image.IsTransparent(w, h) ? 0 : Opacity;
}
}
}
return true;
}
const unsigned char _3DScene::LegendTexture::Squares_Border_Color[3] = { 64, 64, 64 };
const unsigned char _3DScene::LegendTexture::Background_Color[3] = { 9, 91, 134 };
@ -1280,34 +1563,6 @@ bool _3DScene::LegendTexture::generate(const GCodePreviewData& preview_data, con
return true;
}
unsigned int _3DScene::LegendTexture::finalize()
{
if (! m_data.empty()) {
// sends buffer to gpu
::glGenTextures(1, &m_tex_id);
::glBindTexture(GL_TEXTURE_2D, m_tex_id);
::glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, (GLsizei)m_tex_width, (GLsizei)m_tex_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const GLvoid*)m_data.data());
::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
::glBindTexture(GL_TEXTURE_2D, 0);
m_data.clear();
}
return (m_tex_width > 0 && m_tex_height > 0) ? m_tex_id : 0;
}
void _3DScene::LegendTexture::_destroy_texture()
{
if (m_tex_id > 0)
{
::glDeleteTextures(1, &m_tex_id);
m_tex_id = 0;
m_tex_height = 0;
m_tex_width = 0;
}
m_data.clear();
}
void _3DScene::_glew_init()
{
glewInit();
@ -1357,27 +1612,17 @@ void _3DScene::load_gcode_preview(const Print* print, const GCodePreviewData* pr
_load_gcode_unretractions(*preview_data, *volumes, use_VBOs);
if (volumes->empty())
{
reset_legend_texture();
volumes->set_render_interleaved_only_volumes(GLVolumeCollection::RenderInterleavedOnlyVolumes(false, 0.0f));
}
else
{
_generate_legend_texture(*preview_data, tool_colors);
_load_shells(*print, *volumes, use_VBOs);
volumes->set_render_interleaved_only_volumes(GLVolumeCollection::RenderInterleavedOnlyVolumes(true, 0.25f));
}
}
_update_gcode_volumes_visibility(*preview_data, *volumes);
}
unsigned int _3DScene::get_legend_texture_id()
{
return s_legend_texture.get_texture_id();
}
unsigned int _3DScene::get_legend_texture_width()
{
return s_legend_texture.get_texture_width();
@ -1393,6 +1638,36 @@ void _3DScene::reset_legend_texture()
s_legend_texture.reset_texture();
}
unsigned int _3DScene::finalize_legend_texture()
{
return s_legend_texture.finalize();
}
unsigned int _3DScene::get_warning_texture_width()
{
return s_warning_texture.get_texture_width();
}
unsigned int _3DScene::get_warning_texture_height()
{
return s_warning_texture.get_texture_height();
}
void _3DScene::generate_warning_texture(const std::string& msg)
{
s_warning_texture.generate(msg);
}
void _3DScene::reset_warning_texture()
{
s_warning_texture.reset_texture();
}
unsigned int _3DScene::finalize_warning_texture()
{
return s_warning_texture.finalize();
}
// Create 3D thick extrusion lines for a skirt and brim.
// Adds a new Slic3r::GUI::3DScene::Volume to volumes.
void _3DScene::_load_print_toolpaths(
@ -1505,6 +1780,7 @@ void _3DScene::_load_print_object_toolpaths(
auto new_volume = [volumes, &new_volume_mutex](const float *color) -> GLVolume* {
auto *volume = new GLVolume(color);
new_volume_mutex.lock();
volume->outside_printer_detection_enabled = false;
volumes->volumes.emplace_back(volume);
new_volume_mutex.unlock();
return volume;
@ -1655,6 +1931,7 @@ void _3DScene::_load_wipe_tower_toolpaths(
auto new_volume = [volumes, &new_volume_mutex](const float *color) -> GLVolume* {
auto *volume = new GLVolume(color);
new_volume_mutex.lock();
volume->outside_printer_detection_enabled = false;
volumes->volumes.emplace_back(volume);
new_volume_mutex.unlock();
return volume;
@ -2225,6 +2502,7 @@ void _3DScene::_update_gcode_volumes_visibility(const GCodePreviewData& preview_
for (std::vector<GLVolume*>::iterator it = begin; it != end; ++it)
{
GLVolume* volume = *it;
volume->outside_printer_detection_enabled = false;
switch (s_gcode_preview_volume_index.first_volumes[i].type)
{
@ -2257,6 +2535,7 @@ void _3DScene::_update_gcode_volumes_visibility(const GCodePreviewData& preview_
case GCodePreviewVolumeIndex::Shell:
{
volume->is_active = preview_data.shell.is_visible;
volume->color[3] = 0.25f;
volume->zoom_to_volumes = false;
break;
}
@ -2276,11 +2555,6 @@ void _3DScene::_generate_legend_texture(const GCodePreviewData& preview_data, co
s_legend_texture.generate(preview_data, tool_colors);
}
unsigned int _3DScene::finalize_legend_texture()
{
return s_legend_texture.finalize();
}
void _3DScene::_load_shells(const Print& print, GLVolumeCollection& volumes, bool use_VBOs)
{
size_t initial_volumes_count = volumes.volumes.size();

View file

@ -16,6 +16,7 @@ class PrintObject;
class Model;
class ModelObject;
class GCodePreviewData;
class DynamicPrintConfig;
// A container for interleaved arrays of 3D vertices and normals,
// possibly indexed by triangles and / or quads.
@ -85,6 +86,7 @@ public:
unsigned int quad_indices_VBO_id;
void load_mesh_flat_shading(const TriangleMesh &mesh);
void load_mesh_full_shading(const TriangleMesh &mesh);
inline bool has_VBOs() const { return vertices_and_normals_interleaved_VBO_id != 0; }
@ -208,7 +210,38 @@ public:
};
class GLVolume {
struct LayerHeightTextureData
{
// ID of the layer height texture
unsigned int texture_id;
// ID of the shader used to render with the layer height texture
unsigned int shader_id;
// The print object to update when generating the layer height texture
PrintObject* print_object;
float z_cursor_relative;
float edit_band_width;
LayerHeightTextureData() { reset(); }
void reset()
{
texture_id = 0;
shader_id = 0;
print_object = nullptr;
z_cursor_relative = 0.0f;
edit_band_width = 0.0f;
}
bool can_use() { return (texture_id > 0) && (shader_id > 0) && (print_object != nullptr); }
};
public:
static const float SELECTED_COLOR[4];
static const float HOVER_COLOR[4];
static const float OUTSIDE_COLOR[4];
static const float SELECTED_OUTSIDE_COLOR[4];
GLVolume(float r = 1.f, float g = 1.f, float b = 1.f, float a = 1.f) :
composite_id(-1),
select_group_id(-1),
@ -216,6 +249,8 @@ public:
selected(false),
is_active(true),
zoom_to_volumes(true),
outside_printer_detection_enabled(true),
is_outside(false),
hover(false),
tverts_range(0, size_t(-1)),
qverts_range(0, size_t(-1))
@ -224,6 +259,7 @@ public:
color[1] = g;
color[2] = b;
color[3] = a;
set_render_color(r, g, b, a);
}
GLVolume(const float *rgba) : GLVolume(rgba[0], rgba[1], rgba[2], rgba[3]) {}
@ -243,7 +279,8 @@ public:
Pointf3 origin;
// Color of the triangles / quads held by this volume.
float color[4];
// Color used to render this volume.
float render_color[4];
// An ID containing the object ID, volume ID and instance ID.
int composite_id;
// An ID for group selection. It may be the same for all meshes of all object instances, or for just a single object instance.
@ -256,6 +293,10 @@ public:
bool is_active;
// Whether or not to use this volume when applying zoom_to_volumes()
bool zoom_to_volumes;
// Wheter or not this volume is enabled for outside print volume detection.
bool outside_printer_detection_enabled;
// Wheter or not this volume is outside print volume.
bool is_outside;
// Boolean: Is mouse over this object?
bool hover;
@ -271,6 +312,10 @@ public:
// Offset into qverts & tverts, or offsets into indices stored into an OpenGL name_index_buffer.
std::vector<size_t> offsets;
void set_render_color(float r, float g, float b, float a);
void set_render_color(const float* rgba, unsigned int size);
// Sets render color in dependence of current state
void set_render_color();
int object_idx() const { return this->composite_id / 1000000; }
int volume_idx() const { return (this->composite_id / 1000) % 1000; }
@ -282,11 +327,14 @@ public:
void set_range(coordf_t low, coordf_t high);
void render() const;
void render_using_layer_height() const;
void finalize_geometry(bool use_VBOs) { this->indexed_vertex_array.finalize_geometry(use_VBOs); }
void release_geometry() { this->indexed_vertex_array.release_geometry(); }
/************************************************ Layer height texture ****************************************************/
std::shared_ptr<GLTexture> layer_height_texture;
// Data to render this volume using the layer height texture
LayerHeightTextureData layer_height_texture_data;
bool has_layer_height_texture() const
{ return this->layer_height_texture.get() != nullptr; }
@ -296,11 +344,11 @@ public:
{ return (this->layer_height_texture.get() == nullptr) ? 0 : this->layer_height_texture->height; }
size_t layer_height_texture_cells() const
{ return (this->layer_height_texture.get() == nullptr) ? 0 : this->layer_height_texture->cells; }
void* layer_height_texture_data_ptr_level0() {
void* layer_height_texture_data_ptr_level0() const {
return (layer_height_texture.get() == nullptr) ? 0 :
(void*)layer_height_texture->data.data();
}
void* layer_height_texture_data_ptr_level1() {
void* layer_height_texture_data_ptr_level1() const {
return (layer_height_texture.get() == nullptr) ? 0 :
(void*)(layer_height_texture->data.data() + layer_height_texture->width * layer_height_texture->height * 4);
}
@ -309,31 +357,24 @@ public:
double(this->layer_height_texture->cells - 1) / (double(this->layer_height_texture->width) * bounding_box.max.z);
}
void generate_layer_height_texture(PrintObject *print_object, bool force);
void set_layer_height_texture_data(unsigned int texture_id, unsigned int shader_id, PrintObject* print_object, float z_cursor_relative, float edit_band_width)
{
layer_height_texture_data.texture_id = texture_id;
layer_height_texture_data.shader_id = shader_id;
layer_height_texture_data.print_object = print_object;
layer_height_texture_data.z_cursor_relative = z_cursor_relative;
layer_height_texture_data.edit_band_width = edit_band_width;
}
void reset_layer_height_texture_data() { layer_height_texture_data.reset(); }
};
class GLVolumeCollection
{
public:
struct RenderInterleavedOnlyVolumes
{
bool enabled;
float alpha; // [0..1]
RenderInterleavedOnlyVolumes()
: enabled(false)
, alpha(0.0f)
{
}
RenderInterleavedOnlyVolumes(bool enabled, float alpha)
: enabled(enabled)
, alpha(alpha)
{
}
};
private:
RenderInterleavedOnlyVolumes _render_interleaved_only_volumes;
// min and max vertex of the print box volume
float print_box_min[3];
float print_box_max[3];
public:
std::vector<GLVolume*> volumes;
@ -370,7 +411,12 @@ public:
bool empty() const { return volumes.empty(); }
void set_range(double low, double high) { for (GLVolume *vol : this->volumes) vol->set_range(low, high); }
void set_render_interleaved_only_volumes(const RenderInterleavedOnlyVolumes& render_interleaved_only_volumes) { _render_interleaved_only_volumes = render_interleaved_only_volumes; }
void set_print_box(float min_x, float min_y, float min_z, float max_x, float max_y, float max_z) {
print_box_min[0] = min_x; print_box_min[1] = min_y; print_box_min[2] = min_z;
print_box_max[0] = max_x; print_box_max[1] = max_y; print_box_max[2] = max_z;
}
void update_outside_state(const DynamicPrintConfig* config, bool all_inside);
// Returns a vector containing the sorted list of all the print_zs of the volumes contained in this collection
std::vector<double> get_current_print_zs() const;
@ -411,27 +457,20 @@ class _3DScene
static GCodePreviewVolumeIndex s_gcode_preview_volume_index;
class LegendTexture
class TextureBase
{
static const unsigned int Px_Title_Offset = 5;
static const unsigned int Px_Text_Offset = 5;
static const unsigned int Px_Square = 20;
static const unsigned int Px_Square_Contour = 1;
static const unsigned int Px_Border = Px_Square / 2;
static const unsigned char Squares_Border_Color[3];
static const unsigned char Background_Color[3];
static const unsigned char Opacity;
protected:
unsigned int m_tex_id;
unsigned int m_tex_width;
unsigned int m_tex_height;
// generate() fills in m_data with the pixels, while finalize() moves the data to the GPU before rendering.
std::vector<unsigned char> m_data;
public:
LegendTexture() : m_tex_id(0), m_tex_width(0), m_tex_height(0) {}
~LegendTexture() { _destroy_texture(); }
// Generate a texture data, but don't load it into the GPU yet, as the glcontext may not be valid yet.
bool generate(const GCodePreviewData& preview_data, const std::vector<float>& tool_colors);
TextureBase() : m_tex_id(0), m_tex_width(0), m_tex_height(0) {}
virtual ~TextureBase() { _destroy_texture(); }
// If not loaded, load the texture data into the GPU. Return a texture ID or 0 if the texture has zero size.
unsigned int finalize();
@ -442,26 +481,61 @@ class _3DScene
void reset_texture() { _destroy_texture(); }
private:
bool _create_texture(const GCodePreviewData& preview_data, const wxBitmap& bitmap);
void _destroy_texture();
// generate() fills in m_data with the pixels, while finalize() moves the data to the GPU before rendering.
std::vector<unsigned char> m_data;
};
class WarningTexture : public TextureBase
{
static const unsigned char Background_Color[3];
static const unsigned char Opacity;
public:
WarningTexture() : TextureBase() {}
// Generate a texture data, but don't load it into the GPU yet, as the glcontext may not be valid yet.
bool generate(const std::string& msg);
};
class LegendTexture : public TextureBase
{
static const unsigned int Px_Title_Offset = 5;
static const unsigned int Px_Text_Offset = 5;
static const unsigned int Px_Square = 20;
static const unsigned int Px_Square_Contour = 1;
static const unsigned int Px_Border = Px_Square / 2;
static const unsigned char Squares_Border_Color[3];
static const unsigned char Background_Color[3];
static const unsigned char Opacity;
public:
LegendTexture() : TextureBase() {}
// Generate a texture data, but don't load it into the GPU yet, as the glcontext may not be valid yet.
bool generate(const GCodePreviewData& preview_data, const std::vector<float>& tool_colors);
};
static LegendTexture s_legend_texture;
static WarningTexture s_warning_texture;
public:
static void _glew_init();
static void load_gcode_preview(const Print* print, const GCodePreviewData* preview_data, GLVolumeCollection* volumes, const std::vector<std::string>& str_tool_colors, bool use_VBOs);
static unsigned int get_legend_texture_id();
static unsigned int get_legend_texture_width();
static unsigned int get_legend_texture_height();
static void reset_legend_texture();
static unsigned int finalize_legend_texture();
static unsigned int get_warning_texture_width();
static unsigned int get_warning_texture_height();
// generates a warning texture containing the given message
static void generate_warning_texture(const std::string& msg);
static void reset_warning_texture();
static unsigned int finalize_warning_texture();
static void _load_print_toolpaths(
const Print *print,
GLVolumeCollection *volumes,

View file

@ -0,0 +1,200 @@
#include "slic3r/Utils/Bonjour.hpp" // On Windows, boost needs to be included before wxWidgets headers
#include "BonjourDialog.hpp"
#include <set>
#include <mutex>
#include <wx/sizer.h>
#include <wx/button.h>
#include <wx/listctrl.h>
#include <wx/stattext.h>
#include <wx/timer.h>
#include "slic3r/GUI/GUI.hpp"
#include "slic3r/Utils/Bonjour.hpp"
namespace Slic3r {
struct BonjourReplyEvent : public wxEvent
{
BonjourReply reply;
BonjourReplyEvent(wxEventType eventType, int winid, BonjourReply &&reply) :
wxEvent(winid, eventType),
reply(std::move(reply))
{}
virtual wxEvent *Clone() const
{
return new BonjourReplyEvent(*this);
}
};
wxDEFINE_EVENT(EVT_BONJOUR_REPLY, BonjourReplyEvent);
wxDECLARE_EVENT(EVT_BONJOUR_COMPLETE, wxCommandEvent);
wxDEFINE_EVENT(EVT_BONJOUR_COMPLETE, wxCommandEvent);
class ReplySet: public std::set<BonjourReply> {};
struct LifetimeGuard
{
std::mutex mutex;
BonjourDialog *dialog;
LifetimeGuard(BonjourDialog *dialog) : dialog(dialog) {}
};
BonjourDialog::BonjourDialog(wxWindow *parent) :
wxDialog(parent, wxID_ANY, _(L("Network lookup"))),
list(new wxListView(this, wxID_ANY, wxDefaultPosition, wxSize(800, 300))),
replies(new ReplySet),
label(new wxStaticText(this, wxID_ANY, "")),
timer(new wxTimer()),
timer_state(0)
{
wxBoxSizer *vsizer = new wxBoxSizer(wxVERTICAL);
vsizer->Add(label, 0, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, 10);
list->SetSingleStyle(wxLC_SINGLE_SEL);
list->SetSingleStyle(wxLC_SORT_DESCENDING);
list->AppendColumn(_(L("Address")), wxLIST_FORMAT_LEFT, 50);
list->AppendColumn(_(L("Hostname")), wxLIST_FORMAT_LEFT, 100);
list->AppendColumn(_(L("Service name")), wxLIST_FORMAT_LEFT, 200);
list->AppendColumn(_(L("OctoPrint version")), wxLIST_FORMAT_LEFT, 50);
vsizer->Add(list, 1, wxEXPAND | wxALL, 10);
wxBoxSizer *button_sizer = new wxBoxSizer(wxHORIZONTAL);
button_sizer->Add(new wxButton(this, wxID_OK, "OK"), 0, wxALL, 10);
button_sizer->Add(new wxButton(this, wxID_CANCEL, "Cancel"), 0, wxALL, 10);
// ^ Note: The Ok/Cancel labels are translated by wxWidgets
vsizer->Add(button_sizer, 0, wxALIGN_CENTER);
SetSizerAndFit(vsizer);
Bind(EVT_BONJOUR_REPLY, &BonjourDialog::on_reply, this);
Bind(EVT_BONJOUR_COMPLETE, [this](wxCommandEvent &) {
this->timer_state = 0;
});
Bind(wxEVT_TIMER, &BonjourDialog::on_timer, this);
}
BonjourDialog::~BonjourDialog()
{
// Needed bacuse of forward defs
}
bool BonjourDialog::show_and_lookup()
{
Show(); // Because we need GetId() to work before ShowModal()
timer->Stop();
timer->SetOwner(this);
timer_state = 1;
timer->Start(1000);
wxTimerEvent evt_dummy;
on_timer(evt_dummy);
// The background thread needs to queue messages for this dialog
// and for that it needs a valid pointer to it (mandated by the wxWidgets API).
// Here we put the pointer under a shared_ptr and protect it by a mutex,
// so that both threads can access it safely.
auto dguard = std::make_shared<LifetimeGuard>(this);
bonjour = std::move(Bonjour("octoprint")
.set_retries(3)
.set_timeout(4)
.on_reply([dguard](BonjourReply &&reply) {
std::lock_guard<std::mutex> lock_guard(dguard->mutex);
auto dialog = dguard->dialog;
if (dialog != nullptr) {
auto evt = new BonjourReplyEvent(EVT_BONJOUR_REPLY, dialog->GetId(), std::move(reply));
wxQueueEvent(dialog, evt);
}
})
.on_complete([dguard]() {
std::lock_guard<std::mutex> lock_guard(dguard->mutex);
auto dialog = dguard->dialog;
if (dialog != nullptr) {
auto evt = new wxCommandEvent(EVT_BONJOUR_COMPLETE, dialog->GetId());
wxQueueEvent(dialog, evt);
}
})
.lookup()
);
bool res = ShowModal() == wxID_OK && list->GetFirstSelected() >= 0;
{
// Tell the background thread the dialog is going away...
std::lock_guard<std::mutex> lock_guard(dguard->mutex);
dguard->dialog = nullptr;
}
return res;
}
wxString BonjourDialog::get_selected() const
{
auto sel = list->GetFirstSelected();
return sel >= 0 ? list->GetItemText(sel) : wxString();
}
// Private
void BonjourDialog::on_reply(BonjourReplyEvent &e)
{
if (replies->find(e.reply) != replies->end()) {
// We already have this reply
return;
}
replies->insert(std::move(e.reply));
auto selected = get_selected();
list->DeleteAllItems();
// The whole list is recreated so that we benefit from it already being sorted in the set.
// (And also because wxListView's sorting API is bananas.)
for (const auto &reply : *replies) {
auto item = list->InsertItem(0, reply.full_address);
list->SetItem(item, 1, reply.hostname);
list->SetItem(item, 2, reply.service_name);
list->SetItem(item, 3, reply.version);
}
for (int i = 0; i < 4; i++) {
this->list->SetColumnWidth(i, wxLIST_AUTOSIZE);
if (this->list->GetColumnWidth(i) < 100) { this->list->SetColumnWidth(i, 100); }
}
if (!selected.IsEmpty()) {
// Attempt to preserve selection
auto hit = list->FindItem(-1, selected);
if (hit >= 0) { list->SetItemState(hit, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED); }
}
}
void BonjourDialog::on_timer(wxTimerEvent &)
{
const auto search_str = _(L("Searching for devices"));
if (timer_state > 0) {
const std::string dots(timer_state, '.');
label->SetLabel(wxString::Format("%s %s", search_str, dots));
timer_state = (timer_state) % 3 + 1;
} else {
label->SetLabel(wxString::Format("%s: %s", search_str, _(L("Finished."))));
timer->Stop();
}
}
}

View file

@ -0,0 +1,49 @@
#ifndef slic3r_BonjourDialog_hpp_
#define slic3r_BonjourDialog_hpp_
#include <memory>
#include <wx/dialog.h>
class wxListView;
class wxStaticText;
class wxTimer;
class wxTimerEvent;
namespace Slic3r {
class Bonjour;
class BonjourReplyEvent;
class ReplySet;
class BonjourDialog: public wxDialog
{
public:
BonjourDialog(wxWindow *parent);
BonjourDialog(BonjourDialog &&) = delete;
BonjourDialog(const BonjourDialog &) = delete;
BonjourDialog &operator=(BonjourDialog &&) = delete;
BonjourDialog &operator=(const BonjourDialog &) = delete;
~BonjourDialog();
bool show_and_lookup();
wxString get_selected() const;
private:
wxListView *list;
std::unique_ptr<ReplySet> replies;
wxStaticText *label;
std::shared_ptr<Bonjour> bonjour;
std::unique_ptr<wxTimer> timer;
unsigned timer_state;
void on_reply(BonjourReplyEvent &);
void on_timer(wxTimerEvent &);
};
}
#endif

View file

@ -18,6 +18,17 @@ namespace Slic3r { namespace GUI {
wxNumberFormatter::ToString(value, precision, wxNumberFormatter::Style_None);
}
void Field::PostInitialize(){
m_Undo_btn = new wxButton(m_parent, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT | wxNO_BORDER);
// use bouth of temporary_icons till don't have "undo_icon"
auto color = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
if (wxMSW) m_Undo_btn->SetBackgroundColour(color);
m_Undo_btn->SetBitmap(wxBitmap(from_u8(var("bullet_white.png")), wxBITMAP_TYPE_PNG));
m_Undo_btn->Bind(wxEVT_BUTTON, ([this](wxCommandEvent){ on_back_to_initial_value(); }));
BUILD();
}
void Field::on_kill_focus(wxEvent& event) {
// Without this, there will be nasty focus bugs on Windows.
// Also, docs for wxEvent::Skip() say "In general, it is recommended to skip all
@ -34,6 +45,12 @@ namespace Slic3r { namespace GUI {
m_on_change(m_opt_id, get_value());
}
void Field::on_back_to_initial_value()
{
if (m_back_to_initial_value != nullptr && m_is_modified_value)
m_back_to_initial_value(m_opt_id);
}
wxString Field::get_tooltip_text(const wxString& default_string)
{
wxString tooltip_text("");
@ -187,6 +204,17 @@ void CheckBox::BUILD() {
window = dynamic_cast<wxWindow*>(temp);
}
boost::any CheckBox::get_value()
{
boost::any ret_val;
bool value = dynamic_cast<wxCheckBox*>(window)->GetValue();
if (m_opt.type == coBool)
ret_val = static_cast<bool>(value);
else
ret_val = static_cast<unsigned char>(value);
return ret_val;
}
int undef_spin_val = -9999; //! Probably, It's not necessary
void SpinCtrl::BUILD() {
@ -217,8 +245,13 @@ void SpinCtrl::BUILD() {
break;
}
const int min_val = m_opt_id == "standby_temperature_delta" ?
-500 : m_opt.min > 0 ?
m_opt.min : 0;
const int max_val = m_opt.max < 2147483647 ? m_opt.max : 2147483647;
auto temp = new wxSpinCtrl(m_parent, wxID_ANY, text_value, wxDefaultPosition, size,
0, m_opt.min >0 ? m_opt.min : 0, m_opt.max < 2147483647 ? m_opt.max : 2147483647, default_value);
0, min_val, max_val, default_value);
temp->Bind(wxEVT_SPINCTRL, ([this](wxCommandEvent e) { tmp_value = undef_spin_val; on_change_field(); }), temp->GetId());
temp->Bind(wxEVT_KILL_FOCUS, ([this](wxEvent& e) { tmp_value = undef_spin_val; on_kill_focus(e); }), temp->GetId());
@ -228,7 +261,7 @@ void SpinCtrl::BUILD() {
// # when it was changed from the text control, so the on_change callback
// # gets the old one, and on_kill_focus resets the control to the old value.
// # As a workaround, we get the new value from $event->GetString and store
// # here temporarily so that we can return it from $self->get_value
// # here temporarily so that we can return it from $self->get_value
std::string value = e.GetString().utf8_str().data();
if (is_matched(value, "^\\d+$"))
tmp_value = std::stoi(value);
@ -261,8 +294,10 @@ void Choice::BUILD() {
if (m_opt.enum_labels.empty() && m_opt.enum_values.empty()){
}
else{
for (auto el : m_opt.enum_labels.empty() ? m_opt.enum_values : m_opt.enum_labels)
temp->Append(wxString(el));
for (auto el : m_opt.enum_labels.empty() ? m_opt.enum_values : m_opt.enum_labels){
const wxString& str = m_opt_id == "support" ? L_str(el) : el;
temp->Append(str);
}
set_selection();
}
temp->Bind(wxEVT_TEXT, ([this](wxCommandEvent e) { on_change_field(); }), temp->GetId());
@ -330,9 +365,9 @@ void Choice::set_selection()
}
}
void Choice::set_value(const std::string value) //! Redundant?
void Choice::set_value(const std::string value, bool change_event) //! Redundant?
{
m_disable_change_event = true;
m_disable_change_event = !change_event;
size_t idx=0;
for (auto el : m_opt.enum_values)
@ -349,9 +384,9 @@ void Choice::set_value(const std::string value) //! Redundant?
m_disable_change_event = false;
}
void Choice::set_value(boost::any value)
void Choice::set_value(boost::any value, bool change_event)
{
m_disable_change_event = true;
m_disable_change_event = !change_event;
switch (m_opt.type){
case coInt:
@ -394,7 +429,7 @@ void Choice::set_values(const std::vector<std::string> values)
return;
m_disable_change_event = true;
// # it looks that Clear() also clears the text field in recent wxWidgets versions,
// # it looks that Clear() also clears the text field in recent wxWidgets versions,
// # but we want to preserve it
auto ww = dynamic_cast<wxComboBox*>(window);
auto value = ww->GetValue();
@ -411,6 +446,9 @@ boost::any Choice::get_value()
boost::any ret_val;
wxString ret_str = static_cast<wxComboBox*>(window)->GetValue();
if (m_opt_id == "support")
return ret_str;
if (m_opt.type != coEnum)
ret_val = get_value_by_opt_type(ret_str);
else
@ -503,9 +541,9 @@ void PointCtrl::BUILD()
y_textctrl->SetToolTip(get_tooltip_text(X+", "+Y));
}
void PointCtrl::set_value(const Pointf value)
void PointCtrl::set_value(const Pointf value, bool change_event)
{
m_disable_change_event = true;
m_disable_change_event = !change_event;
double val = value.x;
x_textctrl->SetValue(val - int(val) == 0 ? wxString::Format(_T("%i"), int(val)) : wxNumberFormatter::ToString(val, 2, wxNumberFormatter::Style_None));
@ -515,7 +553,7 @@ void PointCtrl::set_value(const Pointf value)
m_disable_change_event = false;
}
void PointCtrl::set_value(boost::any value)
void PointCtrl::set_value(boost::any value, bool change_event)
{
Pointf pt;
Pointf *ptf = boost::any_cast<Pointf>(&value);
@ -541,7 +579,7 @@ void PointCtrl::set_value(boost::any value)
// return;
// }
// }
set_value(pt);
set_value(pt, change_event);
}
boost::any PointCtrl::get_value()

View file

@ -18,6 +18,13 @@
//#include "slic3r_gui.hpp"
#include "GUI.hpp"
#include "Utils.hpp"
#ifdef __WXMSW__
#define wxMSW true
#else
#define wxMSW false
#endif
namespace Slic3r { namespace GUI {
@ -25,13 +32,14 @@ class Field;
using t_field = std::unique_ptr<Field>;
using t_kill_focus = std::function<void()>;
using t_change = std::function<void(t_config_option_key, boost::any)>;
using t_back_to_init = std::function<void(std::string)>;
wxString double_to_string(double const value);
class Field {
protected:
// factory function to defer and enforce creation of derived type.
virtual void PostInitialize() { BUILD(); }
virtual void PostInitialize();
/// Finish constructing the Field's wxWidget-related properties, including setting its own sizer, etc.
virtual void BUILD() = 0;
@ -42,6 +50,8 @@ protected:
void on_kill_focus(wxEvent& event);
/// Call the attached on_change method.
void on_change_field();
/// Call the attached m_back_to_initial_value method.
void on_back_to_initial_value();
public:
/// parent wx item, opportunity to refactor (probably not necessary - data duplication)
@ -53,8 +63,13 @@ public:
/// Function object to store callback passed in from owning object.
t_change m_on_change {nullptr};
/// Function object to store callback passed in from owning object.
t_back_to_init m_back_to_initial_value{ nullptr };
// This is used to avoid recursive invocation of the field change/update by wxWidgets.
bool m_disable_change_event {false};
// This is used to avoid recursive invocation of the field change/update by wxWidgets.
bool m_is_modified_value {false};
/// Copy of ConfigOption for deduction purposes
const ConfigOptionDef m_opt {ConfigOptionDef()};
@ -63,7 +78,7 @@ public:
/// Sets a value for this control.
/// subclasses should overload with a specific version
/// Postcondition: Method does not fire the on_change event.
virtual void set_value(boost::any value) = 0;
virtual void set_value(boost::any value, bool change_event) = 0;
/// Gets a boost::any representing this control.
/// subclasses should overload with a specific version
@ -72,6 +87,9 @@ public:
virtual void enable() = 0;
virtual void disable() = 0;
wxStaticText* m_Label = nullptr;
wxButton* m_Undo_btn = nullptr;
/// Fires the enable or disable function, based on the input.
inline void toggle(bool en) { en ? enable() : disable(); }
@ -85,7 +103,7 @@ public:
virtual wxWindow* getWindow() { return nullptr; }
bool is_matched(std::string string, std::string pattern);
boost::any get_value_by_opt_type(wxString str);
boost::any get_value_by_opt_type(wxString str);
/// Factory method for generating new derived classes.
template<class T>
@ -116,13 +134,13 @@ public:
void BUILD();
wxWindow* window {nullptr};
virtual void set_value(std::string value) {
m_disable_change_event = true;
virtual void set_value(std::string value, bool change_event = false) {
m_disable_change_event = !change_event;
dynamic_cast<wxTextCtrl*>(window)->SetValue(wxString(value));
m_disable_change_event = false;
}
virtual void set_value(boost::any value) {
m_disable_change_event = true;
virtual void set_value(boost::any value, bool change_event = false) {
m_disable_change_event = !change_event;
dynamic_cast<wxTextCtrl*>(window)->SetValue(boost::any_cast<wxString>(value));
m_disable_change_event = false;
}
@ -143,19 +161,17 @@ public:
wxWindow* window{ nullptr };
void BUILD() override;
void set_value(const bool value) {
m_disable_change_event = true;
void set_value(const bool value, bool change_event = false) {
m_disable_change_event = !change_event;
dynamic_cast<wxCheckBox*>(window)->SetValue(value);
m_disable_change_event = false;
}
void set_value(boost::any value) {
m_disable_change_event = true;
void set_value(boost::any value, bool change_event = false) {
m_disable_change_event = !change_event;
dynamic_cast<wxCheckBox*>(window)->SetValue(boost::any_cast<bool>(value));
m_disable_change_event = false;
}
boost::any get_value() override {
return boost::any(dynamic_cast<wxCheckBox*>(window)->GetValue());
}
boost::any get_value() override;
void enable() override { dynamic_cast<wxCheckBox*>(window)->Enable(); }
void disable() override { dynamic_cast<wxCheckBox*>(window)->Disable(); }
@ -173,13 +189,13 @@ public:
wxWindow* window{ nullptr };
void BUILD() override;
void set_value(const std::string value) {
m_disable_change_event = true;
void set_value(const std::string value, bool change_event = false) {
m_disable_change_event = !change_event;
dynamic_cast<wxSpinCtrl*>(window)->SetValue(value);
m_disable_change_event = false;
}
void set_value(boost::any value) {
m_disable_change_event = true;
void set_value(boost::any value, bool change_event = false) {
m_disable_change_event = !change_event;
dynamic_cast<wxSpinCtrl*>(window)->SetValue(boost::any_cast<int>(value));
m_disable_change_event = false;
}
@ -202,8 +218,8 @@ public:
void BUILD() override;
void set_selection();
void set_value(const std::string value);
void set_value(boost::any value);
void set_value(const std::string value, bool change_event = false);
void set_value(boost::any value, bool change_event = false);
void set_values(const std::vector<std::string> values);
boost::any get_value() override;
@ -221,13 +237,13 @@ public:
wxWindow* window{ nullptr };
void BUILD() override;
void set_value(const std::string value) {
m_disable_change_event = true;
void set_value(const std::string value, bool change_event = false) {
m_disable_change_event = !change_event;
dynamic_cast<wxColourPickerCtrl*>(window)->SetColour(value);
m_disable_change_event = false;
}
void set_value(boost::any value) {
m_disable_change_event = true;
void set_value(boost::any value, bool change_event = false) {
m_disable_change_event = !change_event;
dynamic_cast<wxColourPickerCtrl*>(window)->SetColour(boost::any_cast<wxString>(value));
m_disable_change_event = false;
}
@ -251,8 +267,8 @@ public:
void BUILD() override;
void set_value(const Pointf value);
void set_value(boost::any value);
void set_value(const Pointf value, bool change_event = false);
void set_value(boost::any value, bool change_event = false);
boost::any get_value() override;
void enable() override {

View file

@ -45,6 +45,7 @@
#include "AppConfig.hpp"
#include "Utils.hpp"
#include "Preferences.hpp"
#include "PresetBundle.hpp"
namespace Slic3r { namespace GUI {
@ -172,11 +173,15 @@ wxApp *g_wxApp = nullptr;
wxFrame *g_wxMainFrame = nullptr;
wxNotebook *g_wxTabPanel = nullptr;
AppConfig *g_AppConfig = nullptr;
PresetBundle *g_PresetBundle= nullptr;
std::vector<Tab *> g_tabs_list;
wxLocale* g_wxLocale;
std::shared_ptr<ConfigOptionsGroup> m_optgroup;
double m_brim_width = 0.0;
void set_wxapp(wxApp *app)
{
g_wxApp = app;
@ -197,6 +202,11 @@ void set_app_config(AppConfig *app_config)
g_AppConfig = app_config;
}
void set_preset_bundle(PresetBundle *preset_bundle)
{
g_PresetBundle = preset_bundle;
}
std::vector<Tab *>& get_tabs_list()
{
return g_tabs_list;
@ -240,6 +250,7 @@ bool select_language(wxArrayString & names,
g_wxLocale->Init(identifiers[index]);
g_wxLocale->AddCatalogLookupPathPrefix(wxPathOnly(localization_dir()));
g_wxLocale->AddCatalog(g_wxApp->GetAppName());
wxSetlocale(LC_NUMERIC, "C");
return true;
}
return false;
@ -268,6 +279,7 @@ bool load_language()
g_wxLocale->Init(identifiers[i]);
g_wxLocale->AddCatalogLookupPathPrefix(wxPathOnly(localization_dir()));
g_wxLocale->AddCatalog(g_wxApp->GetAppName());
wxSetlocale(LC_NUMERIC, "C");
return true;
}
}
@ -346,26 +358,17 @@ void open_preferences_dialog(int event_preferences)
dlg->ShowModal();
}
void create_preset_tabs(PresetBundle *preset_bundle,
bool no_controller, bool is_disabled_button_browse, bool is_user_agent,
int event_value_change, int event_presets_changed,
int event_button_browse, int event_button_test)
void create_preset_tabs(bool no_controller, int event_value_change, int event_presets_changed)
{
add_created_tab(new TabPrint (g_wxTabPanel, no_controller), preset_bundle);
add_created_tab(new TabFilament (g_wxTabPanel, no_controller), preset_bundle);
add_created_tab(new TabPrinter (g_wxTabPanel, no_controller, is_disabled_button_browse, is_user_agent),
preset_bundle);
add_created_tab(new TabPrint (g_wxTabPanel, no_controller));
add_created_tab(new TabFilament (g_wxTabPanel, no_controller));
add_created_tab(new TabPrinter (g_wxTabPanel, no_controller));
for (size_t i = 0; i < g_wxTabPanel->GetPageCount(); ++ i) {
Tab *tab = dynamic_cast<Tab*>(g_wxTabPanel->GetPage(i));
if (! tab)
continue;
tab->set_event_value_change(wxEventType(event_value_change));
tab->set_event_presets_changed(wxEventType(event_presets_changed));
if (tab->name() == "printer"){
TabPrinter* tab_printer = static_cast<TabPrinter*>(tab);
tab_printer->set_event_button_browse(wxEventType(event_button_browse));
tab_printer->set_event_button_test(wxEventType(event_button_test));
}
}
}
@ -419,9 +422,13 @@ void change_opt_value(DynamicPrintConfig& config, t_config_option_key opt_key, b
config.set_key_value(opt_key, new ConfigOptionString(boost::any_cast<std::string>(value)));
break;
case coStrings:{
if (opt_key.compare("compatible_printers") == 0){
if (opt_key.compare("compatible_printers") == 0 ||
config.def()->get(opt_key)->gui_flags.compare("serialized") == 0){
config.option<ConfigOptionStrings>(opt_key)->values.resize(0);
for (auto el : boost::any_cast<std::vector<std::string>>(value))
std::vector<std::string> values = boost::any_cast<std::vector<std::string>>(value);
if (values.size() == 1 && values[0] == "")
break;
for (auto el : values)
config.option<ConfigOptionStrings>(opt_key)->values.push_back(el);
}
else{
@ -434,7 +441,7 @@ void change_opt_value(DynamicPrintConfig& config, t_config_option_key opt_key, b
config.set_key_value(opt_key, new ConfigOptionBool(boost::any_cast<bool>(value)));
break;
case coBools:{
ConfigOptionBools* vec_new = new ConfigOptionBools{ boost::any_cast<bool>(value) };
ConfigOptionBools* vec_new = new ConfigOptionBools{ (bool)boost::any_cast<unsigned char>(value) };
config.option<ConfigOptionBools>(opt_key)->set_at(vec_new, opt_index, 0);
break;}
case coInt:
@ -458,9 +465,8 @@ void change_opt_value(DynamicPrintConfig& config, t_config_option_key opt_key, b
}
break;
case coPoints:{
ConfigOptionPoints points;
points.values = boost::any_cast<std::vector<Pointf>>(value);
config.set_key_value(opt_key, new ConfigOptionPoints(points));
ConfigOptionPoints* vec_new = new ConfigOptionPoints{ boost::any_cast<Pointf>(value) };
config.option<ConfigOptionPoints>(opt_key)->set_at(vec_new, opt_index, 0);
}
break;
case coNone:
@ -475,9 +481,9 @@ void change_opt_value(DynamicPrintConfig& config, t_config_option_key opt_key, b
}
}
void add_created_tab(Tab* panel, PresetBundle *preset_bundle)
void add_created_tab(Tab* panel)
{
panel->create_preset_tab(preset_bundle);
panel->create_preset_tab(g_PresetBundle);
// Load the currently selected preset into the GUI, update the preset selection box.
panel->load_current_preset();
@ -505,6 +511,11 @@ wxApp* get_app(){
return g_wxApp;
}
wxColour* get_modified_label_clr()
{
return new wxColour(253, 88, 0);
}
void create_combochecklist(wxComboCtrl* comboCtrl, std::string text, std::string items, bool initial_value)
{
if (comboCtrl == nullptr)
@ -562,29 +573,117 @@ AppConfig* get_app_config()
return g_AppConfig;
}
wxString L_str(std::string str)
wxString L_str(const std::string &str)
{
//! Explicitly specify that the source string is already in UTF-8 encoding
return wxGetTranslation(wxString(str.c_str(), wxConvUTF8));
}
wxString from_u8(std::string str)
wxString from_u8(const std::string &str)
{
return wxString::FromUTF8(str.c_str());
}
wxWindow *get_widget_by_id(int id)
void add_frequently_changed_parameters(wxWindow* parent, wxBoxSizer* sizer, wxFlexGridSizer* preset_sizer)
{
if (g_wxMainFrame == nullptr) {
throw std::runtime_error("Main frame not set");
}
DynamicPrintConfig* config = &g_PresetBundle->prints.get_edited_preset().config;
m_optgroup = std::make_shared<ConfigOptionsGroup>(parent, "", config);
const wxArrayInt& ar = preset_sizer->GetColWidths();
m_optgroup->label_width = ar.IsEmpty() ? 100 : ar.front();
m_optgroup->m_on_change = [config](t_config_option_key opt_key, boost::any value){
TabPrint* tab_print = nullptr;
for (size_t i = 0; i < g_wxTabPanel->GetPageCount(); ++i) {
Tab *tab = dynamic_cast<Tab*>(g_wxTabPanel->GetPage(i));
if (!tab)
continue;
if (tab->name() == "print"){
tab_print = static_cast<TabPrint*>(tab);
break;
}
}
if (tab_print == nullptr)
return;
wxWindow *window = g_wxMainFrame->FindWindow(id);
if (window == nullptr) {
throw std::runtime_error((boost::format("Could not find widget by ID: %1%") % id).str());
}
if (opt_key == "fill_density"){
value = m_optgroup->get_config_value(*config, opt_key);
tab_print->set_value(opt_key, value);
tab_print->update();
}
else{
DynamicPrintConfig new_conf = *config;
if (opt_key == "brim"){
double new_val;
double brim_width = config->opt_float("brim_width");
if (boost::any_cast<bool>(value) == true)
{
new_val = m_brim_width == 0.0 ? 10 :
m_brim_width < 0.0 ? m_brim_width * (-1) :
m_brim_width;
}
else{
m_brim_width = brim_width * (-1);
new_val = 0;
}
new_conf.set_key_value("brim_width", new ConfigOptionFloat(new_val));
}
else{ //(opt_key == "support")
const wxString& selection = boost::any_cast<wxString>(value);
auto support_material = selection == _("None") ? false : true;
new_conf.set_key_value("support_material", new ConfigOptionBool(support_material));
return window;
if (selection == _("Everywhere"))
new_conf.set_key_value("support_material_buildplate_only", new ConfigOptionBool(false));
else if (selection == _("Support on build plate only"))
new_conf.set_key_value("support_material_buildplate_only", new ConfigOptionBool(true));
}
tab_print->load_config(new_conf);
}
tab_print->update_dirty();
};
const int width = 250;
Option option = m_optgroup->get_option("fill_density");
option.opt.sidetext = "";
option.opt.width = width;
m_optgroup->append_single_option_line(option);
ConfigOptionDef def;
def.label = L("Support");
def.type = coStrings;
def.gui_type = "select_open";
def.tooltip = L("Select what kind of support do you need");
def.enum_labels.push_back(L("None"));
def.enum_labels.push_back(L("Support on build plate only"));
def.enum_labels.push_back(L("Everywhere"));
std::string selection = !config->opt_bool("support_material") ?
"None" :
config->opt_bool("support_material_buildplate_only") ?
"Support on build plate only" :
"Everywhere";
def.default_value = new ConfigOptionStrings { selection };
option = Option(def, "support");
option.opt.width = width;
m_optgroup->append_single_option_line(option);
m_brim_width = config->opt_float("brim_width");
def.label = L("Brim");
def.type = coBool;
def.tooltip = L("This flag enables the brim that will be printed around each object on the first layer.");
def.gui_type = "";
def.default_value = new ConfigOptionBool{ m_brim_width > 0.0 ? true : false };
option = Option(def, "brim");
m_optgroup->append_single_option_line(option);
sizer->Add(m_optgroup->sizer, 0, wxEXPAND | wxBOTTOM | wxBottom, 1);
}
ConfigOptionsGroup* get_optgroup()
{
return m_optgroup.get();
}
} }

View file

@ -15,6 +15,9 @@ class wxComboCtrl;
class wxString;
class wxArrayString;
class wxArrayLong;
class wxColour;
class wxBoxSizer;
class wxFlexGridSizer;
namespace Slic3r {
@ -36,11 +39,12 @@ class TabIface;
#define _CHB(s) wxGetTranslation(wxString(s, wxConvUTF8)).utf8_str()
// Minimal buffer length for translated string (char buf[MIN_BUF_LENGTH_FOR_L])
#define MIN_BUF_LENGTH_FOR_L 128
#define MIN_BUF_LENGTH_FOR_L 512
namespace GUI {
class Tab;
class ConfigOptionsGroup;
// Map from an file_type name to full file wildcard name.
typedef std::map<std::string, std::string> t_file_wild_card;
inline t_file_wild_card& get_file_wild_card() {
@ -70,9 +74,11 @@ void set_wxapp(wxApp *app);
void set_main_frame(wxFrame *main_frame);
void set_tab_panel(wxNotebook *tab_panel);
void set_app_config(AppConfig *app_config);
void set_preset_bundle(PresetBundle *preset_bundle);
AppConfig* get_app_config();
wxApp* get_app();
wxColour* get_modified_label_clr();
void add_debug_menu(wxMenuBar *menu, int event_language_change);
@ -80,14 +86,11 @@ void add_debug_menu(wxMenuBar *menu, int event_language_change);
void open_preferences_dialog(int event_preferences);
// Create a new preset tab (print, filament and printer),
void create_preset_tabs(PresetBundle *preset_bundle,
bool no_controller, bool is_disabled_button_browse, bool is_user_agent,
int event_value_change, int event_presets_changed,
int event_button_browse, int event_button_test);
void create_preset_tabs(bool no_controller, int event_value_change, int event_presets_changed);
TabIface* get_preset_tab_iface(char *name);
// add it at the end of the tab panel.
void add_created_tab(Tab* panel, PresetBundle *preset_bundle);
void add_created_tab(Tab* panel);
// Change option value in config
void change_opt_value(DynamicPrintConfig& config, t_config_option_key opt_key, boost::any value, int opt_index = 0);
@ -118,11 +121,14 @@ void create_combochecklist(wxComboCtrl* comboCtrl, std::string text, std::string
int combochecklist_get_flags(wxComboCtrl* comboCtrl);
// Return translated std::string as a wxString
wxString L_str(std::string str);
wxString L_str(const std::string &str);
// Return wxString from std::string in UTF8
wxString from_u8(std::string str);
wxString from_u8(const std::string &str);
wxWindow *get_widget_by_id(int id);
void add_frequently_changed_parameters(wxWindow* parent, wxBoxSizer* sizer, wxFlexGridSizer* preset_sizer);
ConfigOptionsGroup* get_optgroup();
}
}

View file

@ -3,18 +3,21 @@
#include <utility>
#include <wx/numformatter.h>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include "Utils.hpp"
namespace Slic3r { namespace GUI {
const t_field& OptionsGroup::build_field(const Option& opt) {
return build_field(opt.opt_id, opt.opt);
const t_field& OptionsGroup::build_field(const Option& opt, wxStaticText* label/* = nullptr*/) {
return build_field(opt.opt_id, opt.opt, label);
}
const t_field& OptionsGroup::build_field(const t_config_option_key& id) {
const t_field& OptionsGroup::build_field(const t_config_option_key& id, wxStaticText* label/* = nullptr*/) {
const ConfigOptionDef& opt = m_options.at(id).opt;
return build_field(id, opt);
return build_field(id, opt, label);
}
const t_field& OptionsGroup::build_field(const t_config_option_key& id, const ConfigOptionDef& opt) {
const t_field& OptionsGroup::build_field(const t_config_option_key& id, const ConfigOptionDef& opt, wxStaticText* label/* = nullptr*/) {
// Check the gui_type field first, fall through
// is the normal type.
if (opt.gui_type.compare("select") == 0) {
@ -72,7 +75,16 @@ const t_field& OptionsGroup::build_field(const t_config_option_key& id, const Co
this->on_kill_focus();
};
field->m_parent = parent();
// assign function objects for callbacks, etc.
//! Label to change background color, when option is modified
field->m_Label = label;
field->m_back_to_initial_value = [this](std::string opt_id){
if (!this->m_disabled)
this->back_to_initial_value(opt_id);
};
if (!m_is_tab_opt) field->m_Undo_btn->Hide();
// assign function objects for callbacks, etc.
return field;
}
@ -100,6 +112,7 @@ void OptionsGroup::append_line(const Line& line) {
const auto& option = option_set.front();
const auto& field = build_field(option);
sizer->Add(field->m_Undo_btn);
if (is_window_field(field))
sizer->Add(field->getWindow(), 0, wxEXPAND | wxALL, wxOSX ? 0 : 5);
if (is_sizer_field(field))
@ -110,8 +123,9 @@ void OptionsGroup::append_line(const Line& line) {
auto grid_sizer = m_grid_sizer;
// Build a label if we have it
wxStaticText* label=nullptr;
if (label_width != 0) {
auto label = new wxStaticText(parent(), wxID_ANY, line.label + (line.label.IsEmpty() ? "" : ":"),
label = new wxStaticText(parent(), wxID_ANY, line.label + (line.label.IsEmpty() ? "" : ":"),
wxDefaultPosition, wxSize(label_width, -1));
label->SetFont(label_font);
label->Wrap(label_width); // avoid a Linux/GTK bug
@ -128,25 +142,24 @@ void OptionsGroup::append_line(const Line& line) {
}
// if we have a single option with no sidetext just add it directly to the grid sizer
if (option_set.size() == 1 && option_set.front().opt.sidetext.size() == 0 &&
option_set.front().side_widget == nullptr && line.get_extra_widgets().size() == 0) {
const auto& option = option_set.front();
const auto& field = build_field(option);
//! std::cerr << "single option, no sidetext.\n";
//! std::cerr << "field parent is not null?: " << (field->parent != nullptr) << "\n";
auto sizer = new wxBoxSizer(wxHORIZONTAL);
grid_sizer->Add(sizer, 0, wxEXPAND | wxALL, 0);
if (option_set.size() == 1 && option_set.front().opt.sidetext.size() == 0 &&
option_set.front().side_widget == nullptr && line.get_extra_widgets().size() == 0) {
const auto& option = option_set.front();
const auto& field = build_field(option, label);
if (is_window_field(field))
grid_sizer->Add(field->getWindow(), 0, (option.opt.full_width ? wxEXPAND : 0) |
sizer->Add(field->m_Undo_btn, 0, wxALIGN_CENTER_VERTICAL);
if (is_window_field(field))
sizer->Add(field->getWindow(), 0, (option.opt.full_width ? wxEXPAND : 0) |
wxBOTTOM | wxTOP | wxALIGN_CENTER_VERTICAL, wxOSX ? 0 : 2);
if (is_sizer_field(field))
grid_sizer->Add(field->getSizer(), 0, (option.opt.full_width ? wxEXPAND : 0) | wxALIGN_CENTER_VERTICAL, 0);
return;
}
if (is_sizer_field(field))
sizer->Add(field->getSizer(), 0, (option.opt.full_width ? wxEXPAND : 0) | wxALIGN_CENTER_VERTICAL, 0);
return;
}
// if we're here, we have more than one option or a single option with sidetext
// so we need a horizontal sizer to arrange these things
auto sizer = new wxBoxSizer(wxHORIZONTAL);
grid_sizer->Add(sizer, 0, wxEXPAND | wxALL, 0);
for (auto opt : option_set) {
ConfigOptionDef option = opt.opt;
// add label if any
@ -156,14 +169,15 @@ void OptionsGroup::append_line(const Line& line) {
// wxString str_label = (option.label == "Top" || option.label == "Bottom") ?
// wxGETTEXT_IN_CONTEXT("Layers", wxString(option.label.c_str()):
// L_str(option.label);
auto field_label = new wxStaticText(parent(), wxID_ANY, str_label + ":", wxDefaultPosition, wxDefaultSize);
field_label->SetFont(label_font);
sizer->Add(field_label, 0, wxALIGN_CENTER_VERTICAL, 0);
label = new wxStaticText(parent(), wxID_ANY, str_label + ":", wxDefaultPosition, wxDefaultSize);
label->SetFont(label_font);
sizer->Add(label, 0, wxALIGN_CENTER_VERTICAL, 0);
}
// add field
const Option& opt_ref = opt;
auto& field = build_field(opt_ref);
auto& field = build_field(opt_ref, label);
sizer->Add(field->m_Undo_btn, 0, wxALIGN_CENTER_VERTICAL, 0);
is_sizer_field(field) ?
sizer->Add(field->getSizer(), 0, wxALIGN_CENTER_VERTICAL, 0) :
sizer->Add(field->getWindow(), 0, wxALIGN_CENTER_VERTICAL, 0);
@ -244,6 +258,12 @@ void ConfigOptionsGroup::on_change_OG(t_config_option_key opt_id, boost::any val
// # Currently used for the post_process config value only.
// my @values = split / ; / , $field_value;
// $self->config->set($opt_key, \@values);
std::string str = boost::any_cast<std::string>(value);
if (str.back() == ';')
str.pop_back();
std::vector<std::string> values;
boost::split(values, str, boost::is_any_of(";"));
change_opt_value(*m_config, opt_key, values);
}
else {
if (opt_index == -1) {
@ -263,6 +283,30 @@ void ConfigOptionsGroup::on_change_OG(t_config_option_key opt_id, boost::any val
OptionsGroup::on_change_OG(opt_id, value); //!? Why doing this
}
void ConfigOptionsGroup::back_to_initial_value(const std::string opt_key)
{
if (m_get_initial_config == nullptr)
return;
DynamicPrintConfig config = m_get_initial_config();
boost::any value;
if (opt_key == "extruders_count"){
auto *nozzle_diameter = dynamic_cast<const ConfigOptionFloats*>(config.option("nozzle_diameter"));
value = int(nozzle_diameter->values.size());
}
else if (m_opt_map.find(opt_key) != m_opt_map.end())
{
auto opt_id = m_opt_map.find(opt_key)->first;
std::string opt_short_key = m_opt_map.at(opt_id).first;
int opt_index = m_opt_map.at(opt_id).second;
value = get_config_value(config, opt_short_key, opt_index);
}
else
value = get_config_value(config, opt_key);
set_value(opt_key, value);
on_change_OG(opt_key, get_value(opt_key));
}
void ConfigOptionsGroup::reload_config(){
for (std::map< std::string, std::pair<std::string, int> >::iterator it = m_opt_map.begin(); it != m_opt_map.end(); ++it) {
auto opt_id = it->first;
@ -323,7 +367,7 @@ boost::any ConfigOptionsGroup::get_config_value(DynamicPrintConfig& config, std:
double val = opt->type == coFloats ?
config.opt_float(opt_key, idx) :
opt->type == coFloat ? config.opt_float(opt_key) :
config.option<ConfigOptionPercents>(opt_key)->values.at(idx);
config.option<ConfigOptionPercents>(opt_key)->get_at(idx);
ret = double_to_string(val);
}
break;
@ -333,6 +377,12 @@ boost::any ConfigOptionsGroup::get_config_value(DynamicPrintConfig& config, std:
case coStrings:
if (config.option<ConfigOptionStrings>(opt_key)->values.empty())
ret = text_value;
else if (opt->gui_flags.compare("serialized") == 0){
std::vector<std::string> values = config.option<ConfigOptionStrings>(opt_key)->values;
for (auto el : values)
text_value += el + ";";
ret = text_value;
}
else
ret = static_cast<wxString>(config.opt_string(opt_key, static_cast<unsigned int>(idx)));
break;
@ -363,10 +413,8 @@ boost::any ConfigOptionsGroup::get_config_value(DynamicPrintConfig& config, std:
ret = static_cast<int>(config.option<ConfigOptionEnum<SeamPosition>>(opt_key)->value);
}
break;
case coPoints:{
const auto &value = *config.option<ConfigOptionPoints>(opt_key);
ret = value.values.at(idx);
}
case coPoints:
ret = config.option<ConfigOptionPoints>(opt_key)->get_at(idx);
break;
case coNone:
default:
@ -376,6 +424,9 @@ boost::any ConfigOptionsGroup::get_config_value(DynamicPrintConfig& config, std:
}
Field* ConfigOptionsGroup::get_fieldc(t_config_option_key opt_key, int opt_index){
Field* field = get_field(opt_key);
if (field != nullptr)
return field;
std::string opt_id = "";
for (std::map< std::string, std::pair<std::string, int> >::iterator it = m_opt_map.begin(); it != m_opt_map.end(); ++it) {
if (opt_key == m_opt_map.at(it->first).first && opt_index == m_opt_map.at(it->first).second){

View file

@ -27,6 +27,9 @@ namespace Slic3r { namespace GUI {
using widget_t = std::function<wxSizer*(wxWindow*)>;//!std::function<wxWindow*(wxWindow*)>;
using column_t = std::function<wxSizer*(const Line&)>;
//auto default_label_clr = wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT); //GetSystemColour
//auto modified_label_clr = *new wxColour(254, 189, 101);
/// Wraps a ConfigOptionDef and adds function object for creating a side_widget.
struct Option {
ConfigOptionDef opt { ConfigOptionDef() };
@ -75,6 +78,7 @@ public:
wxSizer* sizer {nullptr};
column_t extra_column {nullptr};
t_change m_on_change {nullptr};
std::function<DynamicPrintConfig()> m_get_initial_config{ nullptr };
wxFont sidetext_font {wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT) };
wxFont label_font {wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT) };
@ -93,9 +97,9 @@ public:
if (m_fields.find(id) == m_fields.end()) return nullptr;
return m_fields.at(id).get();
}
bool set_value(t_config_option_key id, boost::any value) {
bool set_value(t_config_option_key id, boost::any value, bool change_event = false) {
if (m_fields.find(id) == m_fields.end()) return false;
m_fields.at(id)->set_value(value);
m_fields.at(id)->set_value(value, change_event);
return true;
}
boost::any get_value(t_config_option_key id) {
@ -109,8 +113,8 @@ public:
inline void enable() { for (auto& field : m_fields) field.second->enable(); }
inline void disable() { for (auto& field : m_fields) field.second->disable(); }
OptionsGroup(wxWindow* _parent, wxString title) :
m_parent(_parent), title(title) {
OptionsGroup(wxWindow* _parent, wxString title, bool is_tab_opt=false) :
m_parent(_parent), title(title), m_is_tab_opt(is_tab_opt), staticbox(title!="") {
sizer = (staticbox ? new wxStaticBoxSizer(new wxStaticBox(_parent, wxID_ANY, title), wxVERTICAL) : new wxBoxSizer(wxVERTICAL));
auto num_columns = 1U;
if (label_width != 0) num_columns++;
@ -132,22 +136,25 @@ protected:
t_optionfield_map m_fields;
bool m_disabled {false};
wxGridSizer* m_grid_sizer {nullptr};
// "true" if option is created in preset tabs
bool m_is_tab_opt{ false };
/// Generate a wxSizer or wxWindow from a configuration option
/// Precondition: opt resolves to a known ConfigOption
/// Postcondition: fields contains a wx gui object.
const t_field& build_field(const t_config_option_key& id, const ConfigOptionDef& opt);
const t_field& build_field(const t_config_option_key& id);
const t_field& build_field(const Option& opt);
const t_field& build_field(const t_config_option_key& id, const ConfigOptionDef& opt, wxStaticText* label = nullptr);
const t_field& build_field(const t_config_option_key& id, wxStaticText* label = nullptr);
const t_field& build_field(const Option& opt, wxStaticText* label = nullptr);
virtual void on_kill_focus (){};
virtual void on_change_OG(t_config_option_key opt_id, boost::any value);
virtual void back_to_initial_value(const std::string opt_key){};
};
class ConfigOptionsGroup: public OptionsGroup {
public:
ConfigOptionsGroup(wxWindow* parent, wxString title, DynamicPrintConfig* _config = nullptr) :
OptionsGroup(parent, title), m_config(_config) {}
ConfigOptionsGroup(wxWindow* parent, wxString title, DynamicPrintConfig* _config = nullptr, bool is_tab_opt = false) :
OptionsGroup(parent, title, is_tab_opt), m_config(_config) {}
/// reference to libslic3r config, non-owning pointer (?).
DynamicPrintConfig* m_config {nullptr};
@ -169,10 +176,8 @@ public:
}
void on_change_OG(t_config_option_key opt_id, boost::any value) override;
void on_kill_focus() override
{
reload_config();
}
void back_to_initial_value(const std::string opt_key) override;
void on_kill_focus() override{ reload_config();}
void reload_config();
boost::any config_value(std::string opt_key, int opt_index, bool deserialize);
// return option value from config

View file

@ -227,7 +227,8 @@ const std::vector<std::string>& Preset::printer_options()
"bed_shape", "z_offset", "gcode_flavor", "use_relative_e_distances", "serial_port", "serial_speed",
"octoprint_host", "octoprint_apikey", "octoprint_cafile", "use_firmware_retraction", "use_volumetric_e", "variable_layer_height",
"single_extruder_multi_material", "start_gcode", "end_gcode", "before_layer_gcode", "layer_gcode", "toolchange_gcode",
"between_objects_gcode", "printer_notes", "cooling_tube_retraction", "cooling_tube_length", "parking_pos_retraction"
"between_objects_gcode", "printer_notes", "cooling_tube_retraction", "cooling_tube_length", "parking_pos_retraction",
"max_print_height"
};
s_opts.insert(s_opts.end(), Preset::nozzle_options().begin(), Preset::nozzle_options().end());
}

View file

@ -13,10 +13,11 @@
namespace Slic3r {
#define MIN_BUF_LENGTH 4096
std::string PresetHints::cooling_description(const Preset &preset)
{
std::string out;
char buf[4096];
char buf[MIN_BUF_LENGTH/*4096*/];
if (preset.config.opt_bool("cooling", 0)) {
int slowdown_below_layer_time = preset.config.opt_int("slowdown_below_layer_time", 0);
int min_fan_speed = preset.config.opt_int("min_fan_speed", 0);
@ -220,7 +221,7 @@ std::string PresetHints::maximum_volumetric_flow_description(const PresetBundle
+ _CHB(L(" with a volumetric rate "));
if (limited_by_max_volumetric_speed)
max_flow = max_volumetric_speed;
char buf[2048];
char buf[MIN_BUF_LENGTH/*2048*/];
sprintf(buf, _CHB(L("%3.2f mm³/s")), max_flow);
out += buf;
sprintf(buf, _CHB(L(" at filament speed %3.2f mm/s.")), max_flow / filament_crossection);
@ -258,7 +259,7 @@ std::string PresetHints::recommended_thin_wall_thickness(const PresetBundle &pre
if (num_perimeters > 0) {
int num_lines = std::min(num_perimeters * 2, 10);
char buf[256];
char buf[MIN_BUF_LENGTH/*256*/];
sprintf(buf, _CHB(L("Recommended object thin wall thickness for layer height %.2f and ")), layer_height);
out += buf;
// Start with the width of two closely spaced

View file

@ -3,6 +3,10 @@
#include "PresetBundle.hpp"
#include "PresetHints.hpp"
#include "../../libslic3r/Utils.hpp"
#include "slic3r/Utils/Http.hpp"
#include "slic3r/Utils/OctoPrint.hpp"
#include "BonjourDialog.hpp"
#include "WipeTowerDialog.hpp"
#include <wx/app.h>
@ -15,6 +19,7 @@
#include <wx/treectrl.h>
#include <wx/imaglist.h>
#include <wx/settings.h>
#include <wx/filedlg.h>
#include <boost/algorithm/string/predicate.hpp>
@ -34,19 +39,23 @@ void Tab::create_preset_tab(PresetBundle *preset_bundle)
// preset chooser
m_presets_choice = new wxBitmapComboBox(panel, wxID_ANY, "", wxDefaultPosition, wxSize(270, -1), 0, 0,wxCB_READONLY);
const wxBitmap* bmp = new wxBitmap(from_u8(Slic3r::var("flag-green-icon.png")), wxBITMAP_TYPE_PNG);
auto color = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
//buttons
wxBitmap bmpMenu;
bmpMenu = wxBitmap(from_u8(Slic3r::var("disk.png")), wxBITMAP_TYPE_PNG);
m_btn_save_preset = new wxBitmapButton(panel, wxID_ANY, bmpMenu, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE);
if (wxMSW) m_btn_save_preset->SetBackgroundColour(color);
bmpMenu = wxBitmap(from_u8(Slic3r::var("delete.png")), wxBITMAP_TYPE_PNG);
m_btn_delete_preset = new wxBitmapButton(panel, wxID_ANY, bmpMenu, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE);
if (wxMSW) m_btn_delete_preset->SetBackgroundColour(color);
m_show_incompatible_presets = false;
m_bmp_show_incompatible_presets = new wxBitmap(from_u8(Slic3r::var("flag-red-icon.png")), wxBITMAP_TYPE_PNG);
m_bmp_hide_incompatible_presets = new wxBitmap(from_u8(Slic3r::var("flag-green-icon.png")), wxBITMAP_TYPE_PNG);
m_btn_hide_incompatible_presets = new wxBitmapButton(panel, wxID_ANY, *m_bmp_hide_incompatible_presets, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE);
if (wxMSW) m_btn_hide_incompatible_presets->SetBackgroundColour(color);
m_btn_save_preset->SetToolTip(_(L("Save current ")) + m_title);
m_btn_delete_preset->SetToolTip(_(L("Delete this preset")));
@ -94,6 +103,7 @@ void Tab::create_preset_tab(PresetBundle *preset_bundle)
if (selected_item >= 0){
std::string selected_string = m_presets_choice->GetString(selected_item).ToUTF8().data();
select_preset(selected_string);
update_changed_ui();
}
}));
@ -135,11 +145,93 @@ PageShp Tab::add_options_page(wxString title, std::string icon, bool is_extruder
return page;
}
template<class T>
void add_correct_opts_to_dirty_options(const std::string &opt_key, std::vector<std::string> *vec, TabPrinter *tab)
{
auto opt_init = static_cast<T*>(tab->m_presets->get_selected_preset().config.option(opt_key));
auto opt_cur = static_cast<T*>(tab->m_config->option(opt_key));
int opt_init_max_id = opt_init->values.size()-1;
for (int i = 0; i < opt_cur->values.size(); i++)
{
int init_id = i <= opt_init_max_id ? i : 0;
if (opt_cur->values[i] != opt_init->values[init_id])
vec->emplace_back(opt_key + "#" + std::to_string(i));
}
}
// Update UI according to changes
void Tab::update_changed_ui()
{
auto dirty_options = m_presets->current_dirty_options();
if (name() == "printer"){
// Update dirty_options in case changes of Extruder's options
TabPrinter* tab = static_cast<TabPrinter*>(this);
std::vector<std::string> new_dirty;
for (auto opt_key : dirty_options)
{
switch (m_config->option(opt_key)->type())
{
case coInts: add_correct_opts_to_dirty_options<ConfigOptionInts >(opt_key, &new_dirty, tab); break;
case coBools: add_correct_opts_to_dirty_options<ConfigOptionBools >(opt_key, &new_dirty, tab); break;
case coFloats: add_correct_opts_to_dirty_options<ConfigOptionFloats >(opt_key, &new_dirty, tab); break;
case coStrings: add_correct_opts_to_dirty_options<ConfigOptionStrings >(opt_key, &new_dirty, tab); break;
case coPercents:add_correct_opts_to_dirty_options<ConfigOptionPercents >(opt_key, &new_dirty, tab); break;
case coPoints: add_correct_opts_to_dirty_options<ConfigOptionPoints >(opt_key, &new_dirty, tab); break;
default: new_dirty.emplace_back(opt_key); break;
}
}
dirty_options.resize(0);
dirty_options = new_dirty;
if (tab->m_initial_extruders_count != tab->m_extruders_count){
dirty_options.emplace_back("extruders_count");
}
}
// Add new dirty options to m_dirty_options
for (auto opt_key : dirty_options){
Field* field = get_field(opt_key);
if (field != nullptr && find(m_dirty_options.begin(), m_dirty_options.end(), opt_key) == m_dirty_options.end()){
if (field->m_Label != nullptr){
field->m_Label->SetForegroundColour(*get_modified_label_clr());
field->m_Label->Refresh(true);
}
field->m_Undo_btn->SetBitmap(wxBitmap(from_u8(wxMSW ? var("action_undo.png") : var("arrow_undo.png")), wxBITMAP_TYPE_PNG));
field->m_is_modified_value = true;
m_dirty_options.push_back(opt_key);
}
}
// Delete clear options from m_dirty_options
for (auto i = 0; i < m_dirty_options.size(); ++i)
{
const std::string &opt_key = m_dirty_options[i];
Field* field = get_field(opt_key);
if (field != nullptr && find(dirty_options.begin(), dirty_options.end(), opt_key) == dirty_options.end())
{
field->m_Undo_btn->SetBitmap(wxBitmap(from_u8(var("bullet_white.png")), wxBITMAP_TYPE_PNG));
if (field->m_Label != nullptr){
field->m_Label->SetForegroundColour(wxSYS_COLOUR_WINDOWTEXT);
field->m_Label->Refresh(true);
}
field->m_is_modified_value = false;
std::vector<std::string>::iterator itr = find(m_dirty_options.begin(), m_dirty_options.end(), opt_key);
if (itr != m_dirty_options.end()){
m_dirty_options.erase(itr);
--i;
}
}
}
}
// Update the combo box label of the selected preset based on its "dirty" state,
// comparing the selected preset config with $self->{config}.
void Tab::update_dirty(){
m_presets->update_dirty_ui(m_presets_choice);
on_presets_changed();
on_presets_changed();
update_changed_ui();
}
void Tab::update_tab_ui()
@ -147,81 +239,13 @@ void Tab::update_tab_ui()
m_presets->update_tab_ui(m_presets_choice, m_show_incompatible_presets);
}
template<class T>
boost::any get_new_value(const DynamicPrintConfig &config_new, const DynamicPrintConfig &config_old, std::string opt_key, int &index)
{
for (int i = 0; i < config_new.option<T>(opt_key)->values.size(); i++)
if (config_new.option<T>(opt_key)->values[i] !=
config_old.option<T>(opt_key)->values[i]){
index = i;
break;
}
return config_new.option<T>(opt_key)->values[index];
}
// Load a provied DynamicConfig into the tab, modifying the active preset.
// This could be used for example by setting a Wipe Tower position by interactive manipulation in the 3D view.
void Tab::load_config(DynamicPrintConfig config)
{
bool modified = 0;
boost::any value;
int opt_index = 0;
for(auto opt_key : m_config->diff(config)) {
switch ( config.def()->get(opt_key)->type ){
case coFloatOrPercent:
value = config.option<ConfigOptionFloatOrPercent>(opt_key)->value;
break;
case coPercent:
value = config.option<ConfigOptionPercent>(opt_key)->value;
break;
case coFloat:
value = config.opt_float(opt_key);
break;
case coString:
value = config.opt_string(opt_key);
break;
case coPercents:
value = get_new_value<ConfigOptionPercents>(config, *m_config, opt_key, opt_index);
break;
case coFloats:
value = get_new_value<ConfigOptionFloats>(config, *m_config, opt_key, opt_index);
break;
case coStrings:
value = config.option<ConfigOptionStrings>(opt_key)->values.empty() ? "" :
get_new_value<ConfigOptionStrings>(config, *m_config, opt_key, opt_index);
break;
case coBool:
value = config.opt_bool(opt_key);
break;
case coBools:
value = get_new_value<ConfigOptionBools>(config, *m_config, opt_key, opt_index);
break;
case coInt:
value = config.opt_int(opt_key);
break;
case coInts:
value = get_new_value<ConfigOptionInts>(config, *m_config, opt_key, opt_index);
break;
case coEnum:{
if (opt_key.compare("external_fill_pattern") == 0 ||
opt_key.compare("fill_pattern") == 0)
value = config.option<ConfigOptionEnum<InfillPattern>>(opt_key)->value;
else if (opt_key.compare("gcode_flavor") == 0)
value = config.option<ConfigOptionEnum<GCodeFlavor>>(opt_key)->value;
else if (opt_key.compare("support_material_pattern") == 0)
value = config.option<ConfigOptionEnum<SupportMaterialPattern>>(opt_key)->value;
else if (opt_key.compare("seam_position") == 0)
value = config.option<ConfigOptionEnum<SeamPosition>>(opt_key)->value;
}
break;
case coPoints:
break;
case coNone:
break;
default:
break;
}
change_opt_value(*m_config, opt_key, value, opt_index);
m_config->set_key_value(opt_key, config.option(opt_key)->clone());
modified = 1;
}
if (modified) {
@ -292,6 +316,27 @@ void Tab::on_value_change(std::string opt_key, boost::any value)
}
g_wxMainFrame->ProcessWindowEvent(event);
}
if (opt_key == "fill_density")
{
value = get_optgroup()->get_config_value(*m_config, opt_key);
get_optgroup()->set_value(opt_key, value);
}
if (opt_key == "support_material" || opt_key == "support_material_buildplate_only")
{
wxString new_selection = !m_config->opt_bool("support_material") ?
_("None") :
m_config->opt_bool("support_material_buildplate_only") ?
_("Support on build plate only") :
_("Everywhere");
get_optgroup()->set_value("support", new_selection);
}
if (opt_key == "brim_width")
{
bool val = m_config->opt_float("brim_width") > 0.0 ? true : false;
get_optgroup()->set_value("brim", val);
}
update();
}
@ -309,6 +354,22 @@ void Tab::on_presets_changed()
}
}
void Tab::update_frequently_changed_parameters()
{
boost::any value = get_optgroup()->get_config_value(*m_config, "fill_density");
get_optgroup()->set_value("fill_density", value);
wxString new_selection = !m_config->opt_bool("support_material") ?
_("None") :
m_config->opt_bool("support_material_buildplate_only") ?
_("Support on build plate only") :
_("Everywhere");
get_optgroup()->set_value("support", new_selection);
bool val = m_config->opt_float("brim_width") > 0.0 ? true : false;
get_optgroup()->set_value("brim", val);
}
void Tab::reload_compatible_printers_widget()
{
bool has_any = !m_config->option<ConfigOptionStrings>("compatible_printers")->values.empty();
@ -598,7 +659,8 @@ void TabPrint::update()
DynamicPrintConfig new_conf = *m_config;
if (dialog->ShowModal() == wxID_YES) {
const auto &val = *m_config->option<ConfigOptionFloatOrPercent>("first_layer_height");
new_conf.set_key_value("first_layer_height", new ConfigOptionFloatOrPercent(0.2, val.percent));
auto percent = val.percent;
new_conf.set_key_value("first_layer_height", new ConfigOptionFloatOrPercent(0.2, percent));
if (m_config->opt_float("layer_height") < 0.15) new_conf.set_key_value("layer_height", new ConfigOptionFloat(0.15));
if (m_config->opt_float("layer_height") > 0.35) new_conf.set_key_value("layer_height", new ConfigOptionFloat(0.35));
@ -696,13 +758,16 @@ void TabPrint::update()
"\nShall I switch to rectilinear fill pattern?"));
auto dialog = new wxMessageDialog(parent(), msg_text, _(L("Infill")), wxICON_WARNING | wxYES | wxNO);
DynamicPrintConfig new_conf = *m_config;
double fill_density;
if (dialog->ShowModal() == wxID_YES) {
new_conf.set_key_value("fill_pattern", new ConfigOptionEnum<InfillPattern>(ipRectilinear));
new_conf.set_key_value("fill_density", new ConfigOptionPercent(100));
fill_density = 100;
}
else
new_conf.set_key_value("fill_density", new ConfigOptionPercent(40));
fill_density = m_presets->get_selected_preset().config.option<ConfigOptionPercent>("fill_density")->value;
new_conf.set_key_value("fill_density", new ConfigOptionPercent(fill_density));
load_config(new_conf);
on_value_change("fill_density", fill_density);
}
}
}
@ -990,14 +1055,14 @@ void TabPrinter::build()
auto default_config = m_preset_bundle->full_config();
auto *nozzle_diameter = dynamic_cast<const ConfigOptionFloats*>(m_config->option("nozzle_diameter"));
m_extruders_count = nozzle_diameter->values.size();
m_initial_extruders_count = m_extruders_count = nozzle_diameter->values.size();
auto page = add_options_page(_(L("General")), "printer_empty.png");
auto optgroup = page->new_optgroup(_(L("Size and coordinates")));
Line line{ _(L("Bed shape")), "" };
line.widget = [this](wxWindow* parent){
auto btn = new wxButton(parent, wxID_ANY, _(L(" Set "))+"\u2026", wxDefaultPosition, wxDefaultSize, wxBU_LEFT | wxBU_EXACTFIT);
auto btn = new wxButton(parent, wxID_ANY, _(L(" Set "))+"\u2026", wxDefaultPosition, wxDefaultSize, wxBU_LEFT | wxBU_EXACTFIT);
// btn->SetFont(Slic3r::GUI::small_font);
btn->SetBitmap(wxBitmap(from_u8(Slic3r::var("printer_empty.png")), wxBITMAP_TYPE_PNG));
@ -1015,7 +1080,8 @@ void TabPrinter::build()
return sizer;
};
optgroup->append_line(line);
optgroup->append_single_option_line("z_offset");
optgroup->append_single_option_line("max_print_height");
optgroup->append_single_option_line("z_offset");
optgroup = page->new_optgroup(_(L("Capabilities")));
ConfigOptionDef def;
@ -1088,39 +1154,18 @@ void TabPrinter::build()
}
optgroup = page->new_optgroup(_(L("OctoPrint upload")));
// # append two buttons to the Host line
auto octoprint_host_browse = [this] (wxWindow* parent) {
auto octoprint_host_browse = [this, optgroup] (wxWindow* parent) {
auto btn = new wxButton(parent, wxID_ANY, _(L(" Browse "))+"\u2026", wxDefaultPosition, wxDefaultSize, wxBU_LEFT);
// btn->SetFont($Slic3r::GUI::small_font);
btn->SetBitmap(wxBitmap(from_u8(Slic3r::var("zoom.png")), wxBITMAP_TYPE_PNG));
auto sizer = new wxBoxSizer(wxHORIZONTAL);
sizer->Add(btn);
if (m_is_disabled_button_browse)
btn->Disable();
btn->Bind(wxEVT_BUTTON, [this, parent](wxCommandEvent e){
if (m_event_button_browse > 0){
wxCommandEvent event(m_event_button_browse);
event.SetString("Button BROWSE was clicked!");
g_wxMainFrame->ProcessWindowEvent(event);
btn->Bind(wxEVT_BUTTON, [this, parent, optgroup](wxCommandEvent e) {
BonjourDialog dialog(parent);
if (dialog.show_and_lookup()) {
optgroup->set_value("octoprint_host", std::move(dialog.get_selected()), true);
}
// // # look for devices
// auto entries;
// {
// my $res = Net::Bonjour->new('http');
// $res->discover;
// $entries = [$res->entries];
// }
// if (@{$entries}) {
// my $dlg = Slic3r::GUI::BonjourBrowser->new($self, $entries);
// $self->_load_key_value('octoprint_host', $dlg->GetValue . ":".$dlg->GetPort)
// if $dlg->ShowModal == wxID_OK;
// }
// else {
// auto msg_window = new wxMessageDialog(parent, "No Bonjour device found", "Device Browser", wxOK | wxICON_INFORMATION);
// msg_window->ShowModal();
// }
});
return sizer;
@ -1129,33 +1174,23 @@ void TabPrinter::build()
auto octoprint_host_test = [this](wxWindow* parent) {
auto btn = m_octoprint_host_test_btn = new wxButton(parent, wxID_ANY, _(L("Test")),
wxDefaultPosition, wxDefaultSize, wxBU_LEFT | wxBU_EXACTFIT);
// btn->SetFont($Slic3r::GUI::small_font);
btn->SetBitmap(wxBitmap(from_u8(Slic3r::var("wrench.png")), wxBITMAP_TYPE_PNG));
auto sizer = new wxBoxSizer(wxHORIZONTAL);
sizer->Add(btn);
btn->Bind(wxEVT_BUTTON, [this, parent](wxCommandEvent e) {
if (m_event_button_test > 0){
wxCommandEvent event(m_event_button_test);
event.SetString("Button TEST was clicked!");
g_wxMainFrame->ProcessWindowEvent(event);
btn->Bind(wxEVT_BUTTON, [this](wxCommandEvent e) {
OctoPrint octoprint(m_config);
wxString msg;
if (octoprint.test(msg)) {
show_info(this, _(L("Connection to OctoPrint works correctly.")), _(L("Success!")));
} else {
const auto text = wxString::Format("%s: %s\n\n%s",
_(L("Could not connect to OctoPrint")), msg, _(L("Note: OctoPrint version at least 1.1.0 is required."))
);
show_error(this, text);
}
// my $ua = LWP::UserAgent->new;
// $ua->timeout(10);
//
// my $res = $ua->get(
// "http://".$self->{config}->octoprint_host . "/api/version",
// 'X-Api-Key' = > $self->{config}->octoprint_apikey,
// );
// if ($res->is_success) {
// show_info(parent, "Connection to OctoPrint works correctly.", "Success!");
// }
// else {
// show_error(parent,
// "I wasn't able to connect to OctoPrint (".$res->status_line . "). "
// . "Check hostname and OctoPrint version (at least 1.1.0 is required).");
// }
});
});
return sizer;
};
@ -1165,6 +1200,45 @@ void TabPrinter::build()
optgroup->append_line(host_line);
optgroup->append_single_option_line("octoprint_apikey");
if (Http::ca_file_supported()) {
Line cafile_line = optgroup->create_single_option_line("octoprint_cafile");
auto octoprint_cafile_browse = [this, optgroup] (wxWindow* parent) {
auto btn = new wxButton(parent, wxID_ANY, _(L(" Browse "))+"\u2026", wxDefaultPosition, wxDefaultSize, wxBU_LEFT);
btn->SetBitmap(wxBitmap(from_u8(Slic3r::var("zoom.png")), wxBITMAP_TYPE_PNG));
auto sizer = new wxBoxSizer(wxHORIZONTAL);
sizer->Add(btn);
btn->Bind(wxEVT_BUTTON, [this, optgroup] (wxCommandEvent e){
static const auto filemasks = _(L("Certificate files (*.crt, *.pem)|*.crt;*.pem|All files|*.*"));
wxFileDialog openFileDialog(this, _(L("Open CA certificate file")), "", "", filemasks, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
if (openFileDialog.ShowModal() != wxID_CANCEL) {
optgroup->set_value("octoprint_cafile", std::move(openFileDialog.GetPath()), true);
}
});
return sizer;
};
cafile_line.append_widget(octoprint_cafile_browse);
optgroup->append_line(cafile_line);
auto octoprint_cafile_hint = [this, optgroup] (wxWindow* parent) {
auto txt = new wxStaticText(parent, wxID_ANY,
_(L("HTTPS CA file is optional. It is only needed if you use HTTPS with a self-signed certificate.")));
auto sizer = new wxBoxSizer(wxHORIZONTAL);
sizer->Add(txt);
return sizer;
};
Line cafile_hint { "", "" };
cafile_hint.full_width = 1;
cafile_hint.widget = std::move(octoprint_cafile_hint);
optgroup->append_line(cafile_hint);
}
optgroup = page->new_optgroup(_(L("Firmware")));
optgroup->append_single_option_line("gcode_flavor");
@ -1238,6 +1312,7 @@ void TabPrinter::extruders_count_changed(size_t extruders_count){
m_preset_bundle->printers.get_edited_preset().set_num_extruders(extruders_count);
m_preset_bundle->update_multi_material_filament_presets();
build_extruder_pages();
reload_config();
on_value_change("extruders_count", extruders_count);
}
@ -1325,13 +1400,8 @@ void TabPrinter::update(){
m_serial_test_btn->Disable();
}
en = !m_config->opt_string("octoprint_host").empty();
if ( en && m_is_user_agent)
m_octoprint_host_test_btn->Enable();
else
m_octoprint_host_test_btn->Disable();
get_field("octoprint_apikey")->toggle(en);
m_octoprint_host_test_btn->Enable(!m_config->opt_string("octoprint_host").empty());
bool have_multiple_extruders = m_extruders_count > 1;
get_field("toolchange_gcode")->toggle(have_multiple_extruders);
get_field("single_extruder_multi_material")->toggle(have_multiple_extruders);
@ -1376,8 +1446,9 @@ void TabPrinter::update(){
DynamicPrintConfig new_conf = *m_config;
if (dialog->ShowModal() == wxID_YES) {
auto wipe = static_cast<ConfigOptionBools*>(m_config->option("wipe"));
wipe->values[i] = 0;
auto wipe = static_cast<ConfigOptionBools*>(m_config->option("wipe")->clone());
for (int w = 0; w < wipe->values.size(); w++)
wipe->values[w] = false;
new_conf.set_key_value("wipe", wipe);
}
else {
@ -1400,15 +1471,13 @@ void TabPrinter::update(){
void Tab::load_current_preset()
{
auto preset = m_presets->get_edited_preset();
// try{
// local $SIG{ __WARN__ } = Slic3r::GUI::warning_catcher($self);
preset.is_default ? m_btn_delete_preset->Disable() : m_btn_delete_preset->Enable(true);
update();
// For the printer profile, generate the extruder pages.
on_preset_loaded();
// Reload preset pages with the new configuration values.
reload_config();
// };
preset.is_default ? m_btn_delete_preset->Disable() : m_btn_delete_preset->Enable(true);
update();
// For the printer profile, generate the extruder pages.
on_preset_loaded();
// Reload preset pages with the new configuration values.
reload_config();
// use CallAfter because some field triggers schedule on_change calls using CallAfter,
// and we don't want them to be called after this update_dirty() as they would mark the
// preset dirty again
@ -1419,6 +1488,12 @@ void Tab::load_current_preset()
return;
update_tab_ui();
on_presets_changed();
if (name() == "print")
update_frequently_changed_parameters();
if (m_name == "printer")
static_cast<TabPrinter*>(this)->m_initial_extruders_count = static_cast<TabPrinter*>(this)->m_extruders_count;
update_changed_ui();
});
}
@ -1787,7 +1862,7 @@ bool Page::set_value(t_config_option_key opt_key, boost::any value){
ConfigOptionsGroupShp Page::new_optgroup(wxString title, int noncommon_label_width /*= -1*/)
{
//! config_ have to be "right"
ConfigOptionsGroupShp optgroup = std::make_shared<ConfigOptionsGroup>(this, title, m_config);
ConfigOptionsGroupShp optgroup = std::make_shared<ConfigOptionsGroup>(this, title, m_config, true);
if (noncommon_label_width >= 0)
optgroup->label_width = noncommon_label_width;
@ -1801,6 +1876,11 @@ ConfigOptionsGroupShp Page::new_optgroup(wxString title, int noncommon_label_wid
//! });
};
optgroup->m_get_initial_config = [this](){
DynamicPrintConfig config = static_cast<Tab*>(GetParent())->m_presets->get_selected_preset().config;
return config;
};
vsizer()->Add(optgroup->sizer, 0, wxEXPAND | wxALL, 10);
m_optgroups.push_back(optgroup);

View file

@ -100,6 +100,7 @@ protected:
bool m_no_controller;
std::vector<std::string> m_reload_dependent_tabs = {};
std::vector<std::string> m_dirty_options = {};
// The two following two event IDs are generated at Plater.pm by calling Wx::NewEventType.
wxEventType m_event_value_change = 0;
@ -146,6 +147,7 @@ public:
void toggle_show_hide_incompatible();
void update_show_hide_incompatible_button();
void update_ui_from_settings();
void update_changed_ui();
PageShp add_options_page(wxString title, std::string icon, bool is_extruder_pages = false);
@ -172,6 +174,7 @@ public:
protected:
void on_presets_changed();
void update_frequently_changed_parameters();
};
//Slic3r::GUI::Tab::Print;
@ -212,23 +215,16 @@ public:
//Slic3r::GUI::Tab::Printer;
class TabPrinter : public Tab
{
bool m_is_disabled_button_browse;
bool m_is_user_agent;
// similar event by clicking Buttons "Browse" & "Test"
wxEventType m_event_button_browse = 0;
wxEventType m_event_button_test = 0;
public:
wxButton* m_serial_test_btn;
wxButton* m_octoprint_host_test_btn;
size_t m_extruders_count;
size_t m_initial_extruders_count;
std::vector<PageShp> m_extruder_pages;
TabPrinter() {}
TabPrinter(wxNotebook* parent, bool no_controller, bool is_disabled_btn_browse, bool is_user_agent) :
Tab(parent, _(L("Printer Settings")), "printer", no_controller),
m_is_disabled_button_browse(is_disabled_btn_browse),
m_is_user_agent(is_user_agent) {}
TabPrinter(wxNotebook* parent, bool no_controller) : Tab(parent, _(L("Printer Settings")), "printer", no_controller) {}
~TabPrinter(){}
void build() override;
@ -237,10 +233,6 @@ public:
void extruders_count_changed(size_t extruders_count);
void build_extruder_pages();
void on_preset_loaded() override;
// Set the events to the callbacks posted to the main frame window (currently implemented in Perl).
void set_event_button_browse(wxEventType evt) { m_event_button_browse = evt; }
void set_event_button_test(wxEventType evt) { m_event_button_test = evt; }
};
class SavePresetWindow :public wxDialog

View file

@ -1,9 +1,7 @@
#include "Bonjour.hpp"
#include <iostream> // XXX
#include <cstdint>
#include <algorithm>
#include <unordered_map>
#include <array>
#include <vector>
#include <string>
@ -23,16 +21,18 @@ namespace asio = boost::asio;
using boost::asio::ip::udp;
// TODO: Fuzzing test (done without TXT)
// FIXME: check char retype to unsigned
namespace Slic3r {
// Minimal implementation of a MDNS/DNS-SD client
// This implementation is extremely simple, only the bits that are useful
// for very basic MDNS discovery are present.
// for basic MDNS discovery of OctoPi devices are present.
// However, the bits that are present are implemented with security in mind.
// Only fully correct DNS replies are allowed through.
// While decoding the decoder will bail the moment it encounters anything fishy.
// At least that's the idea. To help prove this is actually the case,
// the implementations has been tested with AFL.
struct DnsName: public std::string
{
@ -48,8 +48,7 @@ struct DnsName: public std::string
return boost::none;
}
// Check for recursion depth to prevent parsing names that are nested too deeply
// or end up cyclic:
// Check for recursion depth to prevent parsing names that are nested too deeply or end up cyclic:
if (depth >= MAX_RECURSION) {
return boost::none;
}
@ -443,6 +442,30 @@ private:
}
};
std::ostream& operator<<(std::ostream &os, const DnsMessage &msg)
{
os << "DnsMessage(ID: " << msg.header.id << ", "
<< "Q: " << (msg.question ? msg.question->name.c_str() : "none") << ", "
<< "A: " << (msg.rr_a ? msg.rr_a->ip.to_string() : "none") << ", "
<< "AAAA: " << (msg.rr_aaaa ? msg.rr_aaaa->ip.to_string() : "none") << ", "
<< "services: [";
enum { SRV_PRINT_MAX = 3 };
unsigned i = 0;
for (const auto &sdpair : msg.sdmap) {
os << sdpair.first << ", ";
if (++i >= SRV_PRINT_MAX) {
os << "...";
break;
}
}
os << "])";
return os;
}
struct BonjourRequest
{
@ -515,6 +538,7 @@ struct Bonjour::priv
const std::string protocol;
const std::string service_dn;
unsigned timeout;
unsigned retries;
uint16_t rq_id;
std::vector<char> buffer;
@ -524,6 +548,7 @@ struct Bonjour::priv
priv(std::string service, std::string protocol);
std::string strip_service_dn(const std::string &service_name) const;
void udp_receive(udp::endpoint from, size_t bytes);
void lookup_perform();
};
@ -533,11 +558,26 @@ Bonjour::priv::priv(std::string service, std::string protocol) :
protocol(std::move(protocol)),
service_dn((boost::format("_%1%._%2%.local") % this->service % this->protocol).str()),
timeout(10),
retries(1),
rq_id(0)
{
buffer.resize(DnsMessage::MAX_SIZE);
}
std::string Bonjour::priv::strip_service_dn(const std::string &service_name) const
{
if (service_name.size() <= service_dn.size()) {
return service_name;
}
auto needle = service_name.rfind(service_dn);
if (needle == service_name.size() - service_dn.size()) {
return service_name.substr(0, needle - 1);
} else {
return service_name;
}
}
void Bonjour::priv::udp_receive(udp::endpoint from, size_t bytes)
{
if (bytes == 0 || !replyfn) {
@ -557,7 +597,10 @@ void Bonjour::priv::udp_receive(udp::endpoint from, size_t bytes)
}
const auto &srv = *sdpair.second.srv;
BonjourReply reply(ip, sdpair.first, srv.hostname);
auto service_name = strip_service_dn(sdpair.first);
std::string path;
std::string version;
if (sdpair.second.txt) {
static const std::string tag_path = "path=";
@ -565,13 +608,14 @@ void Bonjour::priv::udp_receive(udp::endpoint from, size_t bytes)
for (const auto &value : sdpair.second.txt->values) {
if (value.size() > tag_path.size() && value.compare(0, tag_path.size(), tag_path) == 0) {
reply.path = value.substr(tag_path.size());
path = std::move(value.substr(tag_path.size()));
} else if (value.size() > tag_version.size() && value.compare(0, tag_version.size(), tag_version) == 0) {
reply.version = value.substr(tag_version.size());
version = std::move(value.substr(tag_version.size()));
}
}
}
BonjourReply reply(ip, srv.port, std::move(service_name), srv.hostname, std::move(path), std::move(version));
replyfn(std::move(reply));
}
}
@ -595,15 +639,26 @@ void Bonjour::priv::lookup_perform()
udp::endpoint mcast(BonjourRequest::MCAST_IP4, BonjourRequest::MCAST_PORT);
socket.send_to(asio::buffer(brq->data), mcast);
bool timeout = false;
bool expired = false;
bool retry = false;
asio::deadline_timer timer(io_service);
timer.expires_from_now(boost::posix_time::seconds(10));
timer.async_wait([=, &timeout](const error_code &error) {
timeout = true;
if (self->completefn) {
self->completefn();
retries--;
std::function<void(const error_code &)> timer_handler = [&](const error_code &error) {
if (retries == 0 || error) {
expired = true;
if (self->completefn) {
self->completefn();
}
} else {
retry = true;
retries--;
timer.expires_from_now(boost::posix_time::seconds(timeout));
timer.async_wait(timer_handler);
}
});
};
timer.expires_from_now(boost::posix_time::seconds(timeout));
timer.async_wait(timer_handler);
udp::endpoint recv_from;
const auto recv_handler = [&](const error_code &error, size_t bytes) {
@ -612,8 +667,11 @@ void Bonjour::priv::lookup_perform()
socket.async_receive_from(asio::buffer(buffer, buffer.size()), recv_from, recv_handler);
while (io_service.run_one()) {
if (timeout) {
if (expired) {
socket.cancel();
} else if (retry) {
retry = false;
socket.send_to(asio::buffer(brq->data), mcast);
} else {
buffer.resize(DnsMessage::MAX_SIZE);
socket.async_receive_from(asio::buffer(buffer, buffer.size()), recv_from, recv_handler);
@ -626,13 +684,39 @@ void Bonjour::priv::lookup_perform()
// API - public part
BonjourReply::BonjourReply(boost::asio::ip::address ip, std::string service_name, std::string hostname) :
BonjourReply::BonjourReply(boost::asio::ip::address ip, uint16_t port, std::string service_name, std::string hostname, std::string path, std::string version) :
ip(std::move(ip)),
port(port),
service_name(std::move(service_name)),
hostname(std::move(hostname)),
path("/"),
version("Unknown")
{}
path(path.empty() ? std::move(std::string("/")) : std::move(path)),
version(version.empty() ? std::move(std::string("Unknown")) : std::move(version))
{
std::string proto;
std::string port_suffix;
if (port == 443) { proto = "https://"; }
if (port != 443 && port != 80) { port_suffix = std::to_string(port).insert(0, 1, ':'); }
if (this->path[0] != '/') { this->path.insert(0, 1, '/'); }
full_address = proto + ip.to_string() + port_suffix;
if (this->path != "/") { full_address += path; }
}
bool BonjourReply::operator==(const BonjourReply &other) const
{
return this->full_address == other.full_address
&& this->service_name == other.service_name;
}
bool BonjourReply::operator<(const BonjourReply &other) const
{
if (this->ip != other.ip) {
// So that the common case doesn't involve string comparison
return this->ip < other.ip;
} else {
auto cmp = this->full_address.compare(other.full_address);
return cmp != 0 ? cmp < 0 : this->service_name < other.service_name;
}
}
std::ostream& operator<<(std::ostream &os, const BonjourReply &reply)
{
@ -641,6 +725,7 @@ std::ostream& operator<<(std::ostream &os, const BonjourReply &reply)
return os;
}
Bonjour::Bonjour(std::string service, std::string protocol) :
p(new priv(std::move(service), std::move(protocol)))
{}
@ -660,6 +745,12 @@ Bonjour& Bonjour::set_timeout(unsigned timeout)
return *this;
}
Bonjour& Bonjour::set_retries(unsigned retries)
{
if (p && retries > 0) { p->retries = retries; }
return *this;
}
Bonjour& Bonjour::on_reply(ReplyFn fn)
{
if (p) { p->replyfn = std::move(fn); }
@ -677,7 +768,7 @@ Bonjour::Ptr Bonjour::lookup()
auto self = std::make_shared<Bonjour>(std::move(*this));
if (self->p) {
auto io_thread = std::thread([self](){
auto io_thread = std::thread([self]() {
self->p->lookup_perform();
});
self->p->io_thread = std::move(io_thread);
@ -687,18 +778,4 @@ Bonjour::Ptr Bonjour::lookup()
}
void Bonjour::pokus() // XXX
{
auto bonjour = Bonjour("octoprint")
.set_timeout(15)
.on_reply([](BonjourReply &&reply) {
std::cerr << "BonjourReply: " << reply << std::endl;
})
.on_complete([](){
std::cerr << "MDNS lookup complete" << std::endl;
})
.lookup();
}
}

View file

@ -1,26 +1,31 @@
#ifndef slic3r_Bonjour_hpp_
#define slic3r_Bonjour_hpp_
#include <cstdint>
#include <memory>
#include <string>
#include <functional>
// #include <ostream>
#include <boost/asio/ip/address.hpp>
namespace Slic3r {
// TODO: reply data structure
struct BonjourReply
{
boost::asio::ip::address ip;
uint16_t port;
std::string service_name;
std::string hostname;
std::string full_address;
std::string path;
std::string version;
BonjourReply(boost::asio::ip::address ip, std::string service_name, std::string hostname);
BonjourReply() = delete;
BonjourReply(boost::asio::ip::address ip, uint16_t port, std::string service_name, std::string hostname, std::string path, std::string version);
bool operator==(const BonjourReply &other) const;
bool operator<(const BonjourReply &other) const;
};
std::ostream& operator<<(std::ostream &, const BonjourReply &);
@ -32,7 +37,7 @@ private:
struct priv;
public:
typedef std::shared_ptr<Bonjour> Ptr;
typedef std::function<void(BonjourReply &&reply)> ReplyFn;
typedef std::function<void(BonjourReply &&)> ReplyFn;
typedef std::function<void()> CompleteFn;
Bonjour(std::string service, std::string protocol = "tcp");
@ -40,12 +45,15 @@ public:
~Bonjour();
Bonjour& set_timeout(unsigned timeout);
Bonjour& set_retries(unsigned retries);
// ^ Note: By default there is 1 retry (meaning 1 broadcast is sent).
// Timeout is per one retry, ie. total time spent listening = retries * timeout.
// If retries > 1, then care needs to be taken as more than one reply from the same service may be received.
Bonjour& on_reply(ReplyFn fn);
Bonjour& on_complete(CompleteFn fn);
Ptr lookup();
static void pokus(); // XXX: remove
private:
std::unique_ptr<priv> p;
};

View file

@ -3,7 +3,6 @@
#include <cstdlib>
#include <functional>
#include <thread>
#include <iostream>
#include <tuple>
#include <boost/format.hpp>
@ -45,7 +44,9 @@ struct Http::priv
priv(const std::string &url);
~priv();
static bool ca_file_supported(::CURL *curl);
static size_t writecb(void *data, size_t size, size_t nmemb, void *userp);
std::string curl_error(CURLcode curlcode);
std::string body_size_error();
void http_perform();
};
@ -71,6 +72,29 @@ Http::priv::~priv()
::curl_slist_free_all(headerlist);
}
bool Http::priv::ca_file_supported(::CURL *curl)
{
#ifdef _WIN32
bool res = false;
#else
bool res = true;
#endif
if (curl == nullptr) { return res; }
#if LIBCURL_VERSION_MAJOR >= 7 && LIBCURL_VERSION_MINOR >= 48
::curl_tlssessioninfo *tls;
if (::curl_easy_getinfo(curl, CURLINFO_TLS_SSL_PTR, &tls) == CURLE_OK) {
if (tls->backend == CURLSSLBACKEND_SCHANNEL || tls->backend == CURLSSLBACKEND_DARWINSSL) {
// With Windows and OS X native SSL support, cert files cannot be set
res = false;
}
}
#endif
return res;
}
size_t Http::priv::writecb(void *data, size_t size, size_t nmemb, void *userp)
{
auto self = static_cast<priv*>(userp);
@ -88,6 +112,14 @@ size_t Http::priv::writecb(void *data, size_t size, size_t nmemb, void *userp)
return realsize;
}
std::string Http::priv::curl_error(CURLcode curlcode)
{
return (boost::format("%1% (%2%)")
% ::curl_easy_strerror(curlcode)
% curlcode
).str();
}
std::string Http::priv::body_size_error()
{
return (boost::format("HTTP body data size exceeded limit (%1% bytes)") % limit).str();
@ -121,7 +153,7 @@ void Http::priv::http_perform()
if (res == CURLE_WRITE_ERROR) {
error = std::move(body_size_error());
} else {
error = ::curl_easy_strerror(res);
error = std::move(curl_error(res));
};
if (errorfn) {
@ -180,7 +212,7 @@ Http& Http::remove_header(std::string name)
Http& Http::ca_file(const std::string &name)
{
if (p) {
if (p && priv::ca_file_supported(p->curl)) {
::curl_easy_setopt(p->curl, CURLOPT_CAINFO, name.c_str());
}
@ -257,5 +289,13 @@ Http Http::post(std::string url)
return http;
}
bool Http::ca_file_supported()
{
::CURL *curl = ::curl_easy_init();
bool res = priv::ca_file_supported(curl);
if (curl != nullptr) { ::curl_easy_cleanup(curl); }
return res;
}
}

View file

@ -41,6 +41,7 @@ public:
Ptr perform();
void perform_sync();
static bool ca_file_supported();
private:
Http(const std::string &url);

View file

@ -20,16 +20,19 @@ OctoPrint::OctoPrint(DynamicPrintConfig *config) :
cafile(config->opt_string("octoprint_cafile"))
{}
std::string OctoPrint::test() const
bool OctoPrint::test(wxString &msg) const
{
// Since the request is performed synchronously here,
// it is ok to refer to `res` from within the closure
std::string res;
// it is ok to refer to `msg` from within the closure
auto http = Http::get(std::move(make_url("api/version")));
bool res = true;
auto url = std::move(make_url("api/version"));
auto http = Http::get(std::move(url));
set_auth(http);
http.on_error([&](std::string, std::string error, unsigned status) {
res = format_error(error, status);
res = false;
msg = format_error(error, status);
})
.perform_sync();
@ -43,21 +46,26 @@ void OctoPrint::send_gcode(int windowId, int completeEvt, int errorEvt, const st
http.form_add("print", print ? "true" : "false")
.form_add_file("file", filename)
.on_complete([=](std::string body, unsigned status) {
wxWindow *window = GUI::get_widget_by_id(windowId);
wxWindow *window = wxWindow::FindWindowById(windowId);
if (window == nullptr) { return; }
wxCommandEvent* evt = new wxCommandEvent(completeEvt);
evt->SetString("G-code file successfully uploaded to the OctoPrint server");
evt->SetString(_(L("G-code file successfully uploaded to the OctoPrint server")));
evt->SetInt(100);
wxQueueEvent(window, evt);
})
.on_error([=](std::string body, std::string error, unsigned status) {
wxWindow *window = GUI::get_widget_by_id(windowId);
wxWindow *window = wxWindow::FindWindowById(windowId);
if (window == nullptr) { return; }
wxCommandEvent* evt_complete = new wxCommandEvent(completeEvt);
evt_complete->SetInt(100);
wxQueueEvent(window, evt_complete);
wxCommandEvent* evt_error = new wxCommandEvent(errorEvt);
evt_error->SetString(wxString::Format("Error while uploading to the OctoPrint server: %s", format_error(error, status)));
evt_error->SetString(wxString::Format("%s: %s",
_(L("Error while uploading to the OctoPrint server")),
format_error(error, status)));
wxQueueEvent(window, evt_error);
})
.perform();
@ -85,19 +93,15 @@ std::string OctoPrint::make_url(const std::string &path) const
}
}
std::string OctoPrint::format_error(std::string error, unsigned status)
wxString OctoPrint::format_error(std::string error, unsigned status)
{
const wxString wxerror = error;
if (status != 0) {
std::string res{"HTTP "};
res.append(std::to_string(status));
if (status == 401) {
res.append(": Invalid API key");
}
return std::move(res);
return wxString::Format("HTTP %u: %s", status,
(status == 401 ? _(L("Invalid API key")) : wxerror));
} else {
return std::move(error);
return std::move(wxerror);
}
}

View file

@ -2,8 +2,8 @@
#define slic3r_OctoPrint_hpp_
#include <string>
#include <wx/string.h>
// #include "Http.hpp" // XXX: ?
namespace Slic3r {
@ -16,8 +16,7 @@ class OctoPrint
public:
OctoPrint(DynamicPrintConfig *config);
std::string test() const;
// XXX: style
bool test(wxString &curl_msg) const;
void send_gcode(int windowId, int completeEvt, int errorEvt, const std::string &filename, bool print = false) const;
private:
std::string host;
@ -26,7 +25,7 @@ private:
void set_auth(Http &http) const;
std::string make_url(const std::string &path) const;
static std::string format_error(std::string error, unsigned status);
static wxString format_error(std::string error, unsigned status);
};