From 062031a4c6a9fa4d941955f9547e416055f65c02 Mon Sep 17 00:00:00 2001 From: YuSanka Date: Wed, 14 Jul 2021 09:22:03 +0200 Subject: [PATCH] Replace with STL: Fixed update of the item name after replacing + OSX specific: Code refactoring for ObjectList:Rename() function --- src/slic3r/GUI/GUI_ObjectList.cpp | 40 ++++++++++++++------------ src/slic3r/GUI/GUI_ObjectList.hpp | 1 + src/slic3r/GUI/ObjectDataViewModel.cpp | 24 ++++++++++++++++ src/slic3r/GUI/ObjectDataViewModel.hpp | 1 + src/slic3r/GUI/Plater.cpp | 7 +++++ 5 files changed, 55 insertions(+), 18 deletions(-) diff --git a/src/slic3r/GUI/GUI_ObjectList.cpp b/src/slic3r/GUI/GUI_ObjectList.cpp index c9ad24778..7f3155435 100644 --- a/src/slic3r/GUI/GUI_ObjectList.cpp +++ b/src/slic3r/GUI/GUI_ObjectList.cpp @@ -595,13 +595,31 @@ void ObjectList::update_name_in_model(const wxDataViewItem& item) const take_snapshot(volume_id < 0 ? _(L("Rename Object")) : _(L("Rename Sub-object"))); + ModelObject* obj = object(obj_idx); if (m_objects_model->GetItemType(item) & itObject) { - (*m_objects)[obj_idx]->name = m_objects_model->GetName(item).ToUTF8().data(); + obj->name = m_objects_model->GetName(item).ToUTF8().data(); + // if object has just one volume, rename this volume too + if (obj->volumes.size() == 1) + obj->volumes[0]->name = obj->name; return; } if (volume_id < 0) return; - (*m_objects)[obj_idx]->volumes[volume_id]->name = m_objects_model->GetName(item).ToUTF8().data(); + obj->volumes[volume_id]->name = m_objects_model->GetName(item).ToUTF8().data(); +} + +void ObjectList::update_name_in_list(int obj_idx, int vol_idx) const +{ + if (obj_idx < 0) return; + wxDataViewItem item = GetSelection(); + if (!item || !(m_objects_model->GetItemType(item) & (itVolume | itObject))) + return; + + wxString new_name = from_u8(object(obj_idx)->volumes[vol_idx]->name); + if (new_name.IsEmpty() || m_objects_model->GetName(item) == new_name) + return; + + m_objects_model->SetName(new_name, item); } void ObjectList::selection_changed() @@ -3746,22 +3764,8 @@ void ObjectList::rename_item() return; } - // The icon can't be edited so get its old value and reuse it. - wxVariant valueOld; - m_objects_model->GetValue(valueOld, item, colName); - - DataViewBitmapText bmpText; - bmpText << valueOld; - - // But replace the text with the value entered by user. - bmpText.SetText(new_name); - - wxVariant value; - value << bmpText; - m_objects_model->SetValue(value, item, colName); - m_objects_model->ItemChanged(item); - - update_name_in_model(item); + if (m_objects_model->SetName(new_name, item)) + update_name_in_model(item); } void ObjectList::fix_through_netfabb() diff --git a/src/slic3r/GUI/GUI_ObjectList.hpp b/src/slic3r/GUI/GUI_ObjectList.hpp index 590bbf429..394329554 100644 --- a/src/slic3r/GUI/GUI_ObjectList.hpp +++ b/src/slic3r/GUI/GUI_ObjectList.hpp @@ -202,6 +202,7 @@ public: void update_extruder_in_config(const wxDataViewItem& item); // update changed name in the object model void update_name_in_model(const wxDataViewItem& item) const; + void update_name_in_list(int obj_idx, int vol_idx) const; void update_extruder_values_for_items(const size_t max_extruder); // Get obj_idx and vol_idx values for the selected (by default) or an adjusted item diff --git a/src/slic3r/GUI/ObjectDataViewModel.cpp b/src/slic3r/GUI/ObjectDataViewModel.cpp index 0e0e39a2b..3eb0cd5c9 100644 --- a/src/slic3r/GUI/ObjectDataViewModel.cpp +++ b/src/slic3r/GUI/ObjectDataViewModel.cpp @@ -1293,6 +1293,30 @@ void ObjectDataViewModel::SetExtruder(const wxString& extruder, wxDataViewItem i UpdateVolumesExtruderBitmap(item); } +bool ObjectDataViewModel::SetName(const wxString& new_name, wxDataViewItem item) +{ + if (!item.IsOk()) + return false; + + // The icon can't be edited so get its old value and reuse it. + wxVariant valueOld; + GetValue(valueOld, item, colName); + + DataViewBitmapText bmpText; + bmpText << valueOld; + + // But replace the text with the value entered by user. + bmpText.SetText(new_name); + + wxVariant value; + value << bmpText; + if (SetValue(value, item, colName)) { + ItemChanged(item); + return true; + } + return false; +} + void ObjectDataViewModel::AddAllChildren(const wxDataViewItem& parent) { ObjectDataViewModelNode* node = static_cast(parent.GetID()); diff --git a/src/slic3r/GUI/ObjectDataViewModel.hpp b/src/slic3r/GUI/ObjectDataViewModel.hpp index a858b2619..da251ef84 100644 --- a/src/slic3r/GUI/ObjectDataViewModel.hpp +++ b/src/slic3r/GUI/ObjectDataViewModel.hpp @@ -324,6 +324,7 @@ public: unsigned int col); void SetExtruder(const wxString& extruder, wxDataViewItem item); + bool SetName (const wxString& new_name, wxDataViewItem item); // For parent move child from cur_volume_id place to new_volume_id // Remaining items will moved up/down accordingly diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index 0287dcec7..eb727bebe 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -3284,6 +3284,13 @@ void Plater::priv::replace_with_stl() old_model_object->ensure_on_bed(); old_model_object->sort_volumes(wxGetApp().app_config->get("order_volumes") == "1"); + // if object has just one volume, rename object too + if (old_model_object->volumes.size() == 1) + old_model_object->name = old_model_object->volumes[0]->name; + + // update new name in ObjectList + sidebar->obj_list()->update_name_in_list(object_idx, volume_idx); + sla::reproject_points_and_holes(old_model_object); // update 3D scene