Refactoring to allow to quickly build the various options to show the estimated printing time in gcode viewer scene

This commit is contained in:
enricoturri1966 2020-08-05 15:43:46 +02:00
parent 510e787bc7
commit 8fc5be7e4f
15 changed files with 125 additions and 174 deletions

View file

@ -332,7 +332,9 @@ void GCodeViewer::load(const GCodeProcessor::Result& gcode_result, const Print&
wxGetApp().plater()->set_bed_shape(bed_shape, "", "", true);
}
#if GCODE_VIEWER_TIME_ESTIMATE != TIME_ESTIMATE_NONE
m_time_statistics = gcode_result.time_statistics;
#endif // GCODE_VIEWER_TIME_ESTIMATE
}
void GCodeViewer::refresh(const GCodeProcessor::Result& gcode_result, const std::vector<std::string>& str_tool_colors)
@ -406,7 +408,9 @@ void GCodeViewer::reset()
m_layers_zs = std::vector<double>();
m_layers_z_range = { 0.0, 0.0 };
m_roles = std::vector<ExtrusionRole>();
#if GCODE_VIEWER_TIME_ESTIMATE != TIME_ESTIMATE_NONE
m_time_statistics.reset();
#endif // GCODE_VIEWER_TIME_ESTIMATE
#if ENABLE_GCODE_VIEWER_STATISTICS
m_statistics.reset_all();
@ -419,7 +423,7 @@ void GCodeViewer::render() const
m_statistics.reset_opengl();
#endif // ENABLE_GCODE_VIEWER_STATISTICS
#if ENABLE_GCODE_VIEWER_MODAL_TIME_ESTIMATE_DIALOG
#if GCODE_VIEWER_TIME_ESTIMATE == TIME_ESTIMATE_MODAL
if (m_roles.empty()) {
m_time_estimate_frames_count = 0;
return;
@ -427,7 +431,7 @@ void GCodeViewer::render() const
#else
if (m_roles.empty())
return;
#endif // ENABLE_GCODE_VIEWER_MODAL_TIME_ESTIMATE_DIALOG
#endif // GCODE_VIEWER_TIME_ESTIMATE
glsafe(::glEnable(GL_DEPTH_TEST));
render_toolpaths();
@ -435,7 +439,9 @@ void GCodeViewer::render() const
m_sequential_view.marker.render();
render_shells();
render_legend();
#if GCODE_VIEWER_TIME_ESTIMATE != TIME_ESTIMATE_NONE
render_time_estimate();
#endif // GCODE_VIEWER_TIME_ESTIMATE
#if ENABLE_GCODE_VIEWER_STATISTICS
render_statistics();
#endif // ENABLE_GCODE_VIEWER_STATISTICS
@ -474,9 +480,9 @@ unsigned int GCodeViewer::get_options_visibility_flags() const
flags = set_flag(flags, static_cast<unsigned int>(Preview::OptionType::Shells), m_shells.visible);
flags = set_flag(flags, static_cast<unsigned int>(Preview::OptionType::ToolMarker), m_sequential_view.marker.is_visible());
flags = set_flag(flags, static_cast<unsigned int>(Preview::OptionType::Legend), is_legend_enabled());
#if !ENABLE_GCODE_VIEWER_MODAL_TIME_ESTIMATE_DIALOG
#if GCODE_VIEWER_TIME_ESTIMATE == TIME_ESTIMATE_DEFAULT
flags = set_flag(flags, static_cast<unsigned int>(Preview::OptionType::TimeEstimate), is_time_estimate_enabled());
#endif // !ENABLE_GCODE_VIEWER_MODAL_TIME_ESTIMATE_DIALOG
#endif // GCODE_VIEWER_TIME_ESTIMATE
return flags;
}
@ -496,9 +502,9 @@ void GCodeViewer::set_options_visibility_from_flags(unsigned int flags)
m_shells.visible = is_flag_set(static_cast<unsigned int>(Preview::OptionType::Shells));
m_sequential_view.marker.set_visible(is_flag_set(static_cast<unsigned int>(Preview::OptionType::ToolMarker)));
enable_legend(is_flag_set(static_cast<unsigned int>(Preview::OptionType::Legend)));
#if !ENABLE_GCODE_VIEWER_MODAL_TIME_ESTIMATE_DIALOG
#if GCODE_VIEWER_TIME_ESTIMATE == TIME_ESTIMATE_DEFAULT
enable_time_estimate(is_flag_set(static_cast<unsigned int>(Preview::OptionType::TimeEstimate)));
#endif // !ENABLE_GCODE_VIEWER_MODAL_TIME_ESTIMATE_DIALOG
#endif // GCODE_VIEWER_TIME_ESTIMATE
}
void GCodeViewer::set_layers_z_range(const std::array<double, 2>& layers_z_range)
@ -510,6 +516,7 @@ void GCodeViewer::set_layers_z_range(const std::array<double, 2>& layers_z_range
wxGetApp().plater()->update_preview_moves_slider();
}
#if GCODE_VIEWER_TIME_ESTIMATE != TIME_ESTIMATE_NONE
void GCodeViewer::enable_time_estimate(bool enable)
{
m_time_estimate_enabled = enable;
@ -517,6 +524,7 @@ void GCodeViewer::enable_time_estimate(bool enable)
wxGetApp().plater()->get_current_canvas3D()->set_as_dirty();
wxGetApp().plater()->get_current_canvas3D()->request_extra_frame();
}
#endif // GCODE_VIEWER_TIME_ESTIMATE
void GCodeViewer::export_toolpaths_to_obj(const char* filename) const
{
@ -1682,24 +1690,25 @@ void GCodeViewer::render_legend() const
ImGui::PopStyleVar();
}
#if GCODE_VIEWER_TIME_ESTIMATE != TIME_ESTIMATE_NONE
void GCodeViewer::render_time_estimate() const
{
if (!m_time_estimate_enabled) {
#if ENABLE_GCODE_VIEWER_MODAL_TIME_ESTIMATE_DIALOG
#if GCODE_VIEWER_TIME_ESTIMATE == TIME_ESTIMATE_MODAL
m_time_estimate_frames_count = 0;
#endif // ENABLE_GCODE_VIEWER_MODAL_TIME_ESTIMATE_DIALOG
#endif // GCODE_VIEWER_TIME_ESTIMATE
return;
}
ImGuiWrapper& imgui = *wxGetApp().imgui();
#if ENABLE_GCODE_VIEWER_MODAL_TIME_ESTIMATE_DIALOG
#if GCODE_VIEWER_TIME_ESTIMATE == TIME_ESTIMATE_MODAL
// esc
if (ImGui::GetIO().KeysDown[27]) {
m_time_estimate_enabled = false;
return;
}
#endif // ENABLE_GCODE_VIEWER_MODAL_TIME_ESTIMATE_DIALOG
#endif // GCODE_VIEWER_TIME_ESTIMATE
using Times = std::pair<float, float>;
using TimesList = std::vector<std::pair<CustomGCode::Type, Times>>;
@ -1833,7 +1842,7 @@ void GCodeViewer::render_time_estimate() const
imgui.text(short_time(get_time_dhms(time)));
ImGui::SameLine(offsets[1]);
char buf[64];
::sprintf(buf, "%.2f%%", 100.0f * percentage);
::sprintf(buf, "%.1f%%", 100.0f * percentage);
ImGuiWindow* window = ImGui::GetCurrentWindow();
ImRect frame_bb;
frame_bb.Min = { ImGui::GetCursorScreenPos().x, window->DC.CursorPos.y };
@ -1978,7 +1987,7 @@ void GCodeViewer::render_time_estimate() const
};
Size cnv_size = wxGetApp().plater()->get_current_canvas3D()->get_canvas_size();
#if ENABLE_GCODE_VIEWER_MODAL_TIME_ESTIMATE_DIALOG
#if GCODE_VIEWER_TIME_ESTIMATE == TIME_ESTIMATE_MODAL
std::string title = _u8L("Estimated printing time");
ImGui::OpenPopup(title.c_str());
@ -1996,14 +2005,14 @@ void GCodeViewer::render_time_estimate() const
}
#else
imgui.set_next_window_pos(static_cast<float>(cnv_size.get_width()), static_cast<float>(cnv_size.get_height()), ImGuiCond_Always, 1.0f, 1.0f);
ImGui::SetNextWindowSizeConstraints({ 0.0f, 0.0f }, { -1.0f, 0.5f * static_cast<float>(cnv_size.get_height() }));
ImGui::SetNextWindowSizeConstraints({ 0.0f, 0.0f }, { -1.0f, 0.5f * static_cast<float>(cnv_size.get_height()) });
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
ImGui::SetNextWindowBgAlpha(0.6f);
imgui.begin(std::string("Time_estimate"), ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove);
// title
imgui.title(_u8L("Estimated printing time"));
#endif // ENABLE_GCODE_VIEWER_MODAL_TIME_ESTIMATE_DIALOG
#endif // GCODE_VIEWER_TIME_ESTIMATE
// mode tabs
ImGui::BeginTabBar("mode_tabs");
@ -2029,7 +2038,7 @@ void GCodeViewer::render_time_estimate() const
}
ImGui::EndTabBar();
#if ENABLE_GCODE_VIEWER_MODAL_TIME_ESTIMATE_DIALOG
#if GCODE_VIEWER_TIME_ESTIMATE == TIME_ESTIMATE_MODAL
// this is ugly, but it is the only way to ensure that the dialog is large
// enough to show enterely the title
// see: https://github.com/ocornut/imgui/issues/3239
@ -2044,9 +2053,10 @@ void GCodeViewer::render_time_estimate() const
}
#else
imgui.end();
#endif // ENABLE_GCODE_VIEWER_MODAL_TIME_ESTIMATE_DIALOG
#endif // GCODE_VIEWER_TIME_ESTIMATE
ImGui::PopStyleVar();
}
#endif // GCODE_VIEWER_TIME_ESTIMATE
#if ENABLE_GCODE_VIEWER_STATISTICS
void GCodeViewer::render_statistics() const

View file

@ -341,13 +341,15 @@ private:
Shells m_shells;
EViewType m_view_type{ EViewType::FeatureType };
bool m_legend_enabled{ true };
#if GCODE_VIEWER_TIME_ESTIMATE != TIME_ESTIMATE_NONE
PrintEstimatedTimeStatistics m_time_statistics;
#if ENABLE_GCODE_VIEWER_MODAL_TIME_ESTIMATE_DIALOG
#if GCODE_VIEWER_TIME_ESTIMATE == TIME_ESTIMATE_MODAL
mutable bool m_time_estimate_enabled{ false };
mutable unsigned int m_time_estimate_frames_count{ 0 };
#else
bool m_time_estimate_enabled{ false };
#endif // ENABLE_GCODE_VIEWER_MODAL_TIME_ESTIMATE_DIALOG
#endif // GCODE_VIEWER_TIME_ESTIMATE == TIME_ESTIMATE_MODAL
#endif // GCODE_VIEWER_TIME_ESTIMATE != TIME_ESTIMATE_NONE
#if ENABLE_GCODE_VIEWER_STATISTICS
mutable Statistics m_statistics;
#endif // ENABLE_GCODE_VIEWER_STATISTICS
@ -403,8 +405,10 @@ public:
bool is_legend_enabled() const { return m_legend_enabled; }
void enable_legend(bool enable) { m_legend_enabled = enable; }
#if GCODE_VIEWER_TIME_ESTIMATE != TIME_ESTIMATE_NONE
bool is_time_estimate_enabled() const { return m_time_estimate_enabled; }
void enable_time_estimate(bool enable);
#endif // GCODE_VIEWER_TIME_ESTIMATE
void export_toolpaths_to_obj(const char* filename) const;
@ -416,7 +420,9 @@ private:
void render_toolpaths() const;
void render_shells() const;
void render_legend() const;
#if GCODE_VIEWER_TIME_ESTIMATE != TIME_ESTIMATE_NONE
void render_time_estimate() const;
#endif // GCODE_VIEWER_TIME_ESTIMATE
#if ENABLE_GCODE_VIEWER_STATISTICS
void render_statistics() const;
#endif // ENABLE_GCODE_VIEWER_STATISTICS

View file

@ -3109,17 +3109,18 @@ void GLCanvas3D::on_char(wxKeyEvent& evt)
break;
}
#endif // ENABLE_RENDER_PICKING_PASS
#if ENABLE_GCODE_VIEWER
#if GCODE_VIEWER_TIME_ESTIMATE == TIME_ESTIMATE_DEFAULT || GCODE_VIEWER_TIME_ESTIMATE == TIME_ESTIMATE_MODAL
case 'T':
case 't': {
case 't':
{
if (!m_main_toolbar.is_enabled()) {
m_gcode_viewer.enable_time_estimate(!m_gcode_viewer.is_time_estimate_enabled());
m_dirty = true;
wxGetApp().plater()->update_preview_bottom_toolbar();
}
}
break;
}
#endif // ENABLE_GCODE_VIEWER
}
#endif // GCODE_VIEWER_TIME_ESTIMATE
case 'Z':
#if ENABLE_GCODE_VIEWER
case 'z':

View file

@ -558,7 +558,9 @@ public:
void reset_gcode_toolpaths() { m_gcode_viewer.reset(); }
const GCodeViewer::SequentialView& get_gcode_sequential_view() const { return m_gcode_viewer.get_sequential_view(); }
void update_gcode_sequential_view_current(unsigned int first, unsigned int last) { m_gcode_viewer.update_sequential_view_current(first, last); }
#if GCODE_VIEWER_TIME_ESTIMATE != TIME_ESTIMATE_NONE
bool is_time_estimate_enabled() const { return m_gcode_viewer.is_time_estimate_enabled(); }
#endif // GCODE_VIEWER_TIME_ESTIMATE
#endif // ENABLE_GCODE_VIEWER
void toggle_sla_auxiliaries_visibility(bool visible, const ModelObject* mo = nullptr, int instance_idx = -1);

View file

@ -323,12 +323,12 @@ bool Preview::init(wxWindow* parent, Model* model)
get_option_type_string(OptionType::CustomGCodes) + "|0|" +
get_option_type_string(OptionType::Shells) + "|0|" +
get_option_type_string(OptionType::ToolMarker) + "|0|" +
#if ENABLE_GCODE_VIEWER_MODAL_TIME_ESTIMATE_DIALOG
get_option_type_string(OptionType::Legend) + "|1"
#else
#if GCODE_VIEWER_TIME_ESTIMATE == TIME_ESTIMATE_DEFAULT
get_option_type_string(OptionType::Legend) + "|1|" +
get_option_type_string(OptionType::TimeEstimate) + "|1"
#endif // ENABLE_GCODE_VIEWER_MODAL_TIME_ESTIMATE_DIALOG
#else
get_option_type_string(OptionType::Legend) + "|1"
#endif // GCODE_VIEWER_TIME_ESTIMATE
);
Slic3r::GUI::create_combochecklist(m_combochecklist_options, GUI::into_u8(_L("Options")), options_items);
#else
@ -1459,7 +1459,9 @@ wxString Preview::get_option_type_string(OptionType type) const
case OptionType::Shells: { return _L("Shells"); }
case OptionType::ToolMarker: { return _L("Tool marker"); }
case OptionType::Legend: { return _L("Legend"); }
#if GCODE_VIEWER_TIME_ESTIMATE != TIME_ESTIMATE_NONE
case OptionType::TimeEstimate: { return _L("Estimated printing time"); }
#endif // GCODE_VIEWER_TIME_ESTIMATE != TIME_ESTIMATE_NONE
default: { return ""; }
}
}

View file

@ -150,7 +150,9 @@ public:
Shells,
ToolMarker,
Legend,
#if GCODE_VIEWER_TIME_ESTIMATE != TIME_ESTIMATE_NONE
TimeEstimate
#endif // GCODE_VIEWER_TIME_ESTIMATE
};
Preview(wxWindow* parent, Model* model, DynamicPrintConfig* config, BackgroundSlicingProcess* process,

View file

@ -204,13 +204,11 @@ void KBShortcutsDialog::fill_shortcuts()
{ "U", L("Upper Layer") },
{ "D", L("Lower Layer") },
{ "L", L("Show/Hide Legend") },
#if ENABLE_GCODE_VIEWER
#if ENABLE_GCODE_VIEWER_MODAL_TIME_ESTIMATE_DIALOG
{ "T", L("Show Estimated printing time") }
#else
#if GCODE_VIEWER_TIME_ESTIMATE == TIME_ESTIMATE_DEFAULT
{ "T", L("Show/Hide Estimated printing time") }
#endif // ENABLE_GCODE_VIEWER_MODAL_TIME_ESTIMATE_DIALOG
#endif // ENABLE_GCODE_VIEWER
#elif GCODE_VIEWER_TIME_ESTIMATE == TIME_ESTIMATE_MODAL
{ "T", L("Show Estimated printing time") }
#endif // GCODE_VIEWER_TIME_ESTIMATE
};
m_full_shortcuts.push_back(std::make_pair(_L("Preview"), preview_shortcuts));

View file

@ -1324,15 +1324,10 @@ void Sidebar::update_sliced_info_sizer()
p->sliced_info->SetTextAndShow(siCost, info_text, new_label);
#if ENABLE_GCODE_VIEWER
#if ENABLE_GCODE_VIEWER_USE_OLD_TIME_ESTIMATOR
if (p->plater->get_current_canvas3D()->is_time_estimate_enabled() || (ps.estimated_normal_print_time_str == "N/A" && ps.estimated_silent_print_time_str == "N/A"))
// hide the estimate time
p->sliced_info->SetTextAndShow(siEstimatedTime, "N/A");
#else
if (p->plater->get_current_canvas3D()->is_time_estimate_enabled() || (ps.estimated_normal_print_time <= 0.0f && ps.estimated_silent_print_time <= 0.0f))
#endif // ENABLE_GCODE_VIEWER_USE_OLD_TIME_ESTIMATOR
#else
if (ps.estimated_normal_print_time == "N/A" && ps.estimated_silent_print_time == "N/A")
#endif // ENABLE_GCODE_VIEWER
p->sliced_info->SetTextAndShow(siEstimatedTime, "N/A");
else {
new_label = _L("Estimated printing time") + ":";
@ -1340,15 +1335,7 @@ void Sidebar::update_sliced_info_sizer()
wxString str_color = _L("Color");
wxString str_pause = _L("Pause");
#if ENABLE_GCODE_VIEWER
#if ENABLE_GCODE_VIEWER_USE_OLD_TIME_ESTIMATOR
auto fill_labels = [str_color, str_pause](const std::vector<std::pair<CustomGCode::Type, std::pair<std::string, std::string>>>& times,
#else
auto fill_labels = [str_color, str_pause](const std::vector<std::pair<CustomGCode::Type, std::pair<float, float>>>& times,
#endif // ENABLE_GCODE_VIEWER_USE_OLD_TIME_ESTIMATOR
#else
auto fill_labels = [str_color, str_pause](const std::vector<std::pair<CustomGCode::Type, std::string>>& times,
#endif // ENABLE_GCODE_VIEWER
wxString& new_label, wxString& info_text)
{
int color_change_count = 0;
@ -1366,41 +1353,10 @@ void Sidebar::update_sliced_info_sizer()
if (i != (int)times.size() - 1 && times[i].first == CustomGCode::PausePrint)
new_label += format_wxstr(" -> %1%", str_pause);
#if ENABLE_GCODE_VIEWER
#if ENABLE_GCODE_VIEWER_USE_OLD_TIME_ESTIMATOR
info_text += format_wxstr("\n%1% (%2%)", times[i].second.first, times[i].second.second);
#else
info_text += format_wxstr("\n%1% (%2%)", short_time(get_time_dhms(times[i].second.first)), short_time(get_time_dhms(times[i].second.second)));
#endif // ENABLE_GCODE_VIEWER_USE_OLD_TIME_ESTIMATOR
#else
info_text += format_wxstr("\n%1%", times[i].second);
#endif // ENABLE_GCODE_VIEWER
}
};
#if ENABLE_GCODE_VIEWER
#if ENABLE_GCODE_VIEWER_USE_OLD_TIME_ESTIMATOR
if (ps.estimated_normal_print_time_str != "N/A") {
new_label += format_wxstr("\n - %1%", _L("normal mode"));
info_text += format_wxstr("\n%1%", ps.estimated_normal_print_time_str);
fill_labels(ps.estimated_normal_custom_gcode_print_times_str, new_label, info_text);
}
if (ps.estimated_silent_print_time_str != "N/A") {
new_label += format_wxstr("\n - %1%", _L("stealth mode"));
info_text += format_wxstr("\n%1%", ps.estimated_silent_print_time_str);
fill_labels(ps.estimated_silent_custom_gcode_print_times_str, new_label, info_text);
#else
if (ps.estimated_normal_print_time > 0.0f) {
new_label += format_wxstr("\n - %1%", _L("normal mode"));
info_text += format_wxstr("\n%1%", short_time(get_time_dhms(ps.estimated_normal_print_time)));
fill_labels(ps.estimated_normal_custom_gcode_print_times, new_label, info_text);
}
if (ps.estimated_silent_print_time > 0.0f) {
new_label += format_wxstr("\n - %1%", _L("stealth mode"));
info_text += format_wxstr("\n%1%", short_time(get_time_dhms(ps.estimated_silent_print_time)));
fill_labels(ps.estimated_silent_custom_gcode_print_times, new_label, info_text);
#endif // ENABLE_GCODE_VIEWER_USE_OLD_TIME_ESTIMATOR
#else
if (ps.estimated_normal_print_time != "N/A") {
new_label += format_wxstr("\n - %1%", _L("normal mode"));
info_text += format_wxstr("\n%1%", ps.estimated_normal_print_time);
@ -1416,10 +1372,10 @@ void Sidebar::update_sliced_info_sizer()
new_label += format_wxstr("\n - %1%", _L("stealth mode"));
info_text += format_wxstr("\n%1%", ps.estimated_silent_print_time);
fill_labels(ps.estimated_silent_custom_gcode_print_times, new_label, info_text);
#endif // ENABLE_GCODE_VIEWER
}
p->sliced_info->SetTextAndShow(siEstimatedTime, info_text, new_label);
}
#endif // !ENABLE_GCODE_VIEWER
// if there is a wipe tower, insert number of toolchanges info into the array:
p->sliced_info->SetTextAndShow(siWTNumbetOfToolchanges, is_wipe_tower ? wxString::Format("%.d", ps.total_toolchanges) : "N/A");
@ -1428,9 +1384,8 @@ void Sidebar::update_sliced_info_sizer()
p->sliced_info->SetTextAndShow(siMateril_unit, "N/A");
}
}
#if ENABLE_GCODE_VIEWER
Layout();
#endif // ENABLE_GCODE_VIEWER
}
void Sidebar::show_sliced_info_sizer(const bool show)