Merge remote-tracking branch 'remotes/origin/vb_undo_redo'

This commit is contained in:
bubnikv 2019-07-15 11:34:18 +02:00
commit ab7ecc1819
60 changed files with 2799 additions and 578 deletions

View file

@ -70,6 +70,7 @@
#include "../Utils/ASCIIFolding.hpp"
#include "../Utils/PrintHost.hpp"
#include "../Utils/FixModelByWin10.hpp"
#include "../Utils/UndoRedo.hpp"
#include <wx/glcanvas.h> // Needs to be last because reasons :-/
#include "WipeTowerDialog.hpp"
@ -1254,6 +1255,8 @@ const std::regex PlaterDropTarget::pattern_drop(".*[.](stl|obj|amf|3mf|prusa)",
bool PlaterDropTarget::OnDropFiles(wxCoord x, wxCoord y, const wxArrayString &filenames)
{
plater->take_snapshot(_(L("Load Files")));
std::vector<fs::path> paths;
for (const auto &filename : filenames) {
@ -1319,7 +1322,12 @@ struct Plater::priv
Slic3r::Model model;
PrinterTechnology printer_technology = ptFFF;
Slic3r::GCodePreviewData gcode_preview_data;
Slic3r::UndoRedo::Stack undo_redo_stack;
int m_prevent_snapshots = 0; /* Used for avoid of excess "snapshoting".
* Like for "delete selected" or "set numbers of copies"
* we should call tack_snapshot just ones
* instead of calls for each action separately
* */
// GUI elements
wxSizer* panel_sizer{ nullptr };
wxPanel* current_panel{ nullptr };
@ -1619,6 +1627,23 @@ struct Plater::priv
void split_object();
void split_volume();
void scale_selection_to_fit_print_volume();
void take_snapshot(const std::string& snapshot_name)
{
if (this->m_prevent_snapshots > 0)
return;
assert(this->m_prevent_snapshots >= 0);
this->undo_redo_stack.take_snapshot(snapshot_name, model, view3D->get_canvas3d()->get_selection());
}
void take_snapshot(const wxString& snapshot_name) { this->take_snapshot(std::string(snapshot_name.ToUTF8().data())); }
int get_active_snapshot_index();
void undo();
void redo();
void undo_to(size_t time_to_load);
void redo_to(size_t time_to_load);
void suppress_snapshots() { this->m_prevent_snapshots++; }
void allow_snapshots() { this->m_prevent_snapshots--; }
bool background_processing_enabled() const { return this->get_config("background_processing") == "1"; }
void update_print_volume_state();
void schedule_background_process();
@ -1709,6 +1734,7 @@ private:
void update_fff_scene();
void update_sla_scene();
void update_after_undo_redo();
// path to project file stored with no extension
wxString m_project_filename;
@ -1811,6 +1837,8 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame)
view3D_canvas->Bind(EVT_GLCANVAS_MOUSE_DRAGGING_FINISHED, &priv::on_3dcanvas_mouse_dragging_finished, this);
view3D_canvas->Bind(EVT_GLCANVAS_TAB, [this](SimpleEvent&) { select_next_view_3D(); });
view3D_canvas->Bind(EVT_GLCANVAS_RESETGIZMOS, [this](SimpleEvent&) { reset_all_gizmos(); });
view3D_canvas->Bind(EVT_GLCANVAS_UNDO, [this](SimpleEvent&) { this->undo(); });
view3D_canvas->Bind(EVT_GLCANVAS_REDO, [this](SimpleEvent&) { this->redo(); });
// 3DScene/Toolbar:
view3D_canvas->Bind(EVT_GLTOOLBAR_ADD, &priv::on_action_add, this);
@ -1849,6 +1877,9 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame)
// updates camera type from .ini file
camera.set_type(get_config("use_perspective_camera"));
// Initialize the Undo / Redo stack with a first snapshot.
this->take_snapshot(_(L("New Project")));
}
Plater::priv::~priv()
@ -2219,7 +2250,7 @@ std::vector<size_t> Plater::priv::load_model_objects(const ModelObjectPtrs &mode
// the size of the object is too big -> this could lead to overflow when moving to clipper coordinates,
// so scale down the mesh
double inv = 1. / max_ratio;
object->scale_mesh(Vec3d(inv, inv, inv));
object->scale_mesh_after_creation(Vec3d(inv, inv, inv));
object->origin_translation = Vec3d::Zero();
object->center_around_origin();
scaled_down = true;
@ -2398,12 +2429,15 @@ void Plater::priv::object_list_changed()
void Plater::priv::select_all()
{
// this->take_snapshot(_(L("Select All")));
view3D->select_all();
this->sidebar->obj_list()->update_selections();
}
void Plater::priv::deselect_all()
{
// this->take_snapshot(_(L("Deselect All")));
view3D->deselect_all();
}
@ -2425,6 +2459,7 @@ void Plater::priv::remove(size_t obj_idx)
void Plater::priv::delete_object_from_model(size_t obj_idx)
{
this->take_snapshot(_(L("Delete Object")));
model.delete_object(obj_idx);
update();
object_list_changed();
@ -2432,6 +2467,8 @@ void Plater::priv::delete_object_from_model(size_t obj_idx)
void Plater::priv::reset()
{
this->take_snapshot(_(L("Reset Project")));
set_project_filename(wxEmptyString);
// Prevent toolpaths preview from rendering while we modify the Print object
@ -2457,17 +2494,20 @@ void Plater::priv::reset()
void Plater::priv::mirror(Axis axis)
{
this->take_snapshot(_(L("Mirror")));
view3D->mirror_selection(axis);
}
void Plater::priv::arrange()
{
this->take_snapshot(_(L("Arrange")));
m_ui_jobs.start(Jobs::Arrange);
}
// This method will find an optimal orientation for the currently selected item
// Very similar in nature to the arrange method above...
void Plater::priv::sla_optimize_rotation() {
this->take_snapshot(_(L("Optimize Rotation")));
m_ui_jobs.start(Jobs::Rotoptimize);
}
@ -2623,6 +2663,8 @@ void Plater::priv::split_object()
Slic3r::GUI::warning_catcher(q, _(L("The selected object couldn't be split because it contains only one part.")));
else
{
this->take_snapshot(_(L("Split to Objects")));
unsigned int counter = 1;
for (ModelObject* m : new_objects)
m->name = current_model_object->name + "_" + std::to_string(counter++);
@ -2861,6 +2903,8 @@ void Plater::priv::update_sla_scene()
void Plater::priv::reload_from_disk()
{
this->take_snapshot(_(L("Reload from Disk")));
const auto &selection = get_selection();
const auto obj_orig_idx = selection.get_object_idx();
if (selection.is_wipe_tower() || obj_orig_idx == -1) { return; }
@ -2894,6 +2938,9 @@ void Plater::priv::fix_through_netfabb(const int obj_idx, const int vol_idx/* =
{
if (obj_idx < 0)
return;
this->take_snapshot(_(L("Fix Throught NetFabb")));
fix_model_by_win10_sdk_gui(*model.objects[obj_idx], vol_idx);
this->update();
this->object_list_changed();
@ -3139,6 +3186,8 @@ void Plater::priv::on_action_layersediting(SimpleEvent&)
void Plater::priv::on_object_select(SimpleEvent& evt)
{
// this->take_snapshot(_(L("Object Selection")));
wxGetApp().obj_list()->update_selections();
selection_changed();
}
@ -3414,8 +3463,7 @@ void Plater::priv::init_view_toolbar()
item.icon_filename = "editor.svg";
item.tooltip = _utf8(L("3D editor view")) + " [" + GUI::shortkey_ctrl_prefix() + "5]";
item.sprite_id = 0;
item.action_callback = [this]() { if (this->q != nullptr) wxPostEvent(this->q, SimpleEvent(EVT_GLVIEWTOOLBAR_3D)); };
item.is_toggable = false;
item.left.action_callback = [this]() { if (this->q != nullptr) wxPostEvent(this->q, SimpleEvent(EVT_GLVIEWTOOLBAR_3D)); };
if (!view_toolbar.add_item(item))
return;
@ -3423,8 +3471,7 @@ void Plater::priv::init_view_toolbar()
item.icon_filename = "preview.svg";
item.tooltip = _utf8(L("Preview")) + " [" + GUI::shortkey_ctrl_prefix() + "6]";
item.sprite_id = 1;
item.action_callback = [this]() { if (this->q != nullptr) wxPostEvent(this->q, SimpleEvent(EVT_GLVIEWTOOLBAR_PREVIEW)); };
item.is_toggable = false;
item.left.action_callback = [this]() { if (this->q != nullptr) wxPostEvent(this->q, SimpleEvent(EVT_GLVIEWTOOLBAR_PREVIEW)); };
if (!view_toolbar.add_item(item))
return;
@ -3554,6 +3601,51 @@ void Plater::priv::show_action_buttons(const bool is_ready_to_slice) const
}
}
int Plater::priv::get_active_snapshot_index()
{
const size_t& active_snapshot_time = this->undo_redo_stack.active_snapshot_time();
const std::vector<UndoRedo::Snapshot>& ss_stack = this->undo_redo_stack.snapshots();
const auto it = std::lower_bound(ss_stack.begin(), ss_stack.end(), UndoRedo::Snapshot(active_snapshot_time));
return it - ss_stack.begin();
}
void Plater::priv::undo()
{
if (this->undo_redo_stack.undo(model, this->view3D->get_canvas3d()->get_selection()))
this->update_after_undo_redo();
}
void Plater::priv::redo()
{
if (this->undo_redo_stack.redo(model))
this->update_after_undo_redo();
}
void Plater::priv::undo_to(size_t time_to_load)
{
if (this->undo_redo_stack.undo(model, this->view3D->get_canvas3d()->get_selection(), time_to_load))
this->update_after_undo_redo();
}
void Plater::priv::redo_to(size_t time_to_load)
{
if (this->undo_redo_stack.redo(model, time_to_load))
this->update_after_undo_redo();
}
void Plater::priv::update_after_undo_redo()
{
this->view3D->get_canvas3d()->get_selection().clear();
this->update(false); // update volumes from the deserializd model
//YS_FIXME update obj_list from the deserialized model (maybe store ObjectIDs into the tree?) (no selections at this point of time)
this->view3D->get_canvas3d()->get_selection().set_deserialized(GUI::Selection::EMode(this->undo_redo_stack.selection_deserialized().mode), this->undo_redo_stack.selection_deserialized().volumes_and_instances);
wxGetApp().obj_list()->update_after_undo_redo();
//FIXME what about the state of the manipulators?
//FIXME what about the focus? Cursor in the side panel?
}
void Sidebar::set_btn_label(const ActionButtonType btn_type, const wxString& label) const
{
switch (btn_type)
@ -3591,6 +3683,8 @@ void Plater::new_project()
void Plater::load_project()
{
this->take_snapshot(_(L("Load Project")));
wxString input_file;
wxGetApp().load_project(this, input_file);
load_project(input_file);
@ -3616,6 +3710,8 @@ void Plater::add_model()
if (input_files.empty())
return;
this->take_snapshot(_(L("Add object(s)")));
std::vector<fs::path> input_paths;
for (const auto &file : input_files) {
input_paths.push_back(into_path(file));
@ -3673,13 +3769,18 @@ void Plater::delete_object_from_model(size_t obj_idx) { p->delete_object_from_mo
void Plater::remove_selected()
{
this->take_snapshot(_(L("Delete Selected Objects")));
this->suppress_snapshots();
this->p->view3D->delete_selected();
this->allow_snapshots();
}
void Plater::increase_instances(size_t num)
{
if (! can_increase_instances()) { return; }
this->take_snapshot(_(L("Increase Instances")));
int obj_idx = p->get_selected_object_idx();
ModelObject* model_object = p->model.objects[obj_idx];
@ -3714,6 +3815,8 @@ void Plater::decrease_instances(size_t num)
{
if (! can_decrease_instances()) { return; }
this->take_snapshot(_(L("Decrease Instances")));
int obj_idx = p->get_selected_object_idx();
ModelObject* model_object = p->model.objects[obj_idx];
@ -3748,11 +3851,16 @@ void Plater::set_number_of_copies(/*size_t num*/)
if (num < 0)
return;
this->take_snapshot(wxString::Format(_(L("Set numbers of copies to %d")), num));
this->suppress_snapshots();
int diff = (int)num - (int)model_object->instances.size();
if (diff > 0)
increase_instances(diff);
else if (diff < 0)
decrease_instances(-diff);
this->allow_snapshots();
}
bool Plater::is_selection_empty() const
@ -3776,6 +3884,8 @@ void Plater::cut(size_t obj_idx, size_t instance_idx, coordf_t z, bool keep_uppe
return;
}
this->take_snapshot(_(L("Cut")));
wxBusyCursor wait;
const auto new_objects = object->cut(instance_idx, z, keep_upper, keep_lower, rotate_lower);
@ -4065,6 +4175,45 @@ void Plater::send_gcode()
}
}
void Plater::take_snapshot(const std::string &snapshot_name) { p->take_snapshot(snapshot_name); }
void Plater::take_snapshot(const wxString &snapshot_name) { p->take_snapshot(snapshot_name); }
void Plater::suppress_snapshots() { p->suppress_snapshots(); }
void Plater::allow_snapshots() { p->allow_snapshots(); }
void Plater::undo() { p->undo(); }
void Plater::redo() { p->redo(); }
void Plater::undo_to(int selection)
{
if (selection == 0) {
p->undo();
return;
}
const int idx = p->get_active_snapshot_index() - selection - 1;
p->undo_to(p->undo_redo_stack.snapshots()[idx].timestamp);
}
void Plater::redo_to(int selection)
{
if (selection == 0) {
p->redo();
return;
}
const int idx = p->get_active_snapshot_index() + selection + 1;
p->redo_to(p->undo_redo_stack.snapshots()[idx].timestamp);
}
bool Plater::undo_redo_string_getter(const bool is_undo, int idx, const char** out_text)
{
const std::vector<UndoRedo::Snapshot>& ss_stack = p->undo_redo_stack.snapshots();
const int idx_in_ss_stack = p->get_active_snapshot_index() + (is_undo ? -(++idx) : idx);
if (0 < idx_in_ss_stack && idx_in_ss_stack < ss_stack.size() - 1) {
*out_text = ss_stack[idx_in_ss_stack].name.c_str();
return true;
}
return false;
}
void Plater::on_extruders_change(int num_extruders)
{
auto& choices = sidebar().combos_filament();
@ -4290,8 +4439,11 @@ void Plater::copy_selection_to_clipboard()
void Plater::paste_from_clipboard()
{
if (can_paste_from_clipboard())
p->view3D->get_canvas3d()->get_selection().paste_from_clipboard();
if (!can_paste_from_clipboard())
return;
this->take_snapshot(_(L("Paste From Clipboard")));
p->view3D->get_canvas3d()->get_selection().paste_from_clipboard();
}
void Plater::msw_rescale()
@ -4356,6 +4508,16 @@ bool Plater::can_copy_to_clipboard() const
return true;
}
bool Plater::can_undo() const
{
return p->undo_redo_stack.has_undo_snapshot();
}
bool Plater::can_redo() const
{
return p->undo_redo_stack.has_redo_snapshot();
}
SuppressBackgroundProcessingUpdate::SuppressBackgroundProcessingUpdate() :
m_was_running(wxGetApp().plater()->is_background_process_running())
{