From 5c4c9121323b5f5d04db737fc046c7f1ada861b2 Mon Sep 17 00:00:00 2001 From: YuSanka Date: Wed, 30 May 2018 00:36:44 +0200 Subject: [PATCH] Extended "Delete" functions --- xs/src/slic3r/GUI/GUI.cpp | 23 +++++++++++++++++++- xs/src/slic3r/GUI/wxExtensions.cpp | 34 +++++++++++++++++++++--------- xs/src/slic3r/GUI/wxExtensions.hpp | 3 ++- 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/xs/src/slic3r/GUI/GUI.cpp b/xs/src/slic3r/GUI/GUI.cpp index 991402a51..296dd7691 100644 --- a/xs/src/slic3r/GUI/GUI.cpp +++ b/xs/src/slic3r/GUI/GUI.cpp @@ -920,6 +920,13 @@ wxBoxSizer* content_edit_object_buttons(wxWindow* win) wxString name = "Part"; m_objects_model->AddChild(item, name); }); + + btn_delete->Bind(wxEVT_BUTTON, [](wxEvent&) + { + auto item = m_objects_ctrl->GetSelection(); + if (!item) return; + m_objects_model->Delete(item); + }); //*** btn_move_up->SetMinSize(wxSize(20, -1)); @@ -1036,7 +1043,12 @@ void add_expert_mode_part(wxWindow* parent, wxBoxSizer* sizer) auto add_btn = new wxButton(parent, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT | wxNO_BORDER); if (wxMSW) add_btn->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); add_btn->SetBitmap(wxBitmap(from_u8(Slic3r::var("add.png")), wxBITMAP_TYPE_PNG)); - sizer->Add(add_btn, 0, wxALIGN_LEFT | wxLEFT, 20); + sizer->Add(add_btn, 0, wxALIGN_LEFT | wxLEFT | wxTOP, 20); + + auto del_btn = new wxButton(parent, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT | wxNO_BORDER); + if (wxMSW) del_btn->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); + del_btn->SetBitmap(wxBitmap(from_u8(Slic3r::var("brick_delete.png")), wxBITMAP_TYPE_PNG)); + sizer->Add(del_btn, 0, wxALIGN_LEFT | wxLEFT, 20); // *** Objects List *** auto collpane = add_prusa_collapsible_pane(parent, sizer, "Objects List:", content_objects_list); @@ -1064,6 +1076,15 @@ void add_expert_mode_part(wxWindow* parent, wxBoxSizer* sizer) m_objects_model->Add(name); }); + del_btn->Bind(wxEVT_BUTTON, [](wxEvent& ) + { + auto item = m_objects_ctrl->GetSelection(); + if (!item) return; + m_objects_model->Delete(item); + if (m_objects_model->IsEmpty()) + m_collpane_settings->show_it(false); + }); + // More experiments with UI // auto listctrl = new wxDataViewListCtrl(main_page, wxID_ANY, wxDefaultPosition, wxSize(-1, 100)); // listctrl->AppendToggleColumn("Toggle"); diff --git a/xs/src/slic3r/GUI/wxExtensions.cpp b/xs/src/slic3r/GUI/wxExtensions.cpp index 7e7792ee4..84953ebaa 100644 --- a/xs/src/slic3r/GUI/wxExtensions.cpp +++ b/xs/src/slic3r/GUI/wxExtensions.cpp @@ -367,6 +367,15 @@ void MyObjectTreeModel::AddChild(const wxDataViewItem &parent_item, wxString &na MyObjectTreeModelNode *root = (MyObjectTreeModelNode*)parent_item.GetID(); if (!root) return; + if (root->GetChildren().Count() == 0) + { + auto node = new MyObjectTreeModelNode(root, root->m_name); + root->Append(node); + // notify control + wxDataViewItem child((void*)node); + ItemAdded(parent_item, child); + } + auto node = new MyObjectTreeModelNode(root, name); root->Append(node); // notify control @@ -380,22 +389,27 @@ void MyObjectTreeModel::Delete(const wxDataViewItem &item) if (!node) // happens if item.IsOk()==false return; - wxDataViewItem parent(node->GetParent()); - if (!parent.IsOk()) - { -// wxASSERT(node == m_root); - // don't make the control completely empty: - //wxLogError("Cannot remove the root item!"); - return; - } + auto node_parent = node->GetParent(); + wxDataViewItem parent(node_parent); // first remove the node from the parent's array of children; // NOTE: MyObjectTreeModelNodePtrArray is only an array of _pointers_ // thus removing the node from it doesn't result in freeing it - node->GetParent()->GetChildren().Remove(node); + if (node_parent) + node_parent->GetChildren().Remove(node); + else + { + auto it = m_objects.find(node); + if (it != m_objects.end()) + m_objects.erase(it); + } // free the node delete node; + // set m_containet to FALSE if parent has no child + if (node_parent && node_parent->GetChildCount() == 0) + node_parent->m_container = false; + // notify control ItemDeleted(parent, item); } @@ -498,7 +512,7 @@ wxDataViewItem MyObjectTreeModel::GetParent(const wxDataViewItem &item) const bool MyObjectTreeModel::IsContainer(const wxDataViewItem &item) const { - // the invisble root node can have children + // the invisible root node can have children if (!item.IsOk()) return true; diff --git a/xs/src/slic3r/GUI/wxExtensions.hpp b/xs/src/slic3r/GUI/wxExtensions.hpp index 02bcfe148..cabd1e7ca 100644 --- a/xs/src/slic3r/GUI/wxExtensions.hpp +++ b/xs/src/slic3r/GUI/wxExtensions.hpp @@ -195,7 +195,7 @@ public: m_container = true; m_children.Add(child); } - unsigned int GetChildCount() const + size_t GetChildCount() const { return m_children.GetCount(); } @@ -219,6 +219,7 @@ public: void Add(wxString &name); void AddChild(const wxDataViewItem &parent_item, wxString &name); void Delete(const wxDataViewItem &item); + bool IsEmpty() { return m_objects.empty(); } // helper method for wxLog