Extended "Delete" functions

This commit is contained in:
YuSanka 2018-05-30 00:36:44 +02:00
parent db7c58009c
commit 5c4c912132
3 changed files with 48 additions and 12 deletions

View file

@ -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");

View file

@ -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;

View file

@ -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