Functions "Add/Delete" objects to/from list works correct now

This commit is contained in:
YuSanka 2018-05-30 17:52:28 +02:00
parent 5c4c912132
commit c857b68fbe
5 changed files with 64 additions and 30 deletions

View file

@ -917,15 +917,18 @@ wxBoxSizer* content_edit_object_buttons(wxWindow* win)
{ {
auto item = m_objects_ctrl->GetSelection(); auto item = m_objects_ctrl->GetSelection();
if (!item) return; if (!item) return;
if (m_objects_model->GetParent(item) != wxDataViewItem(0))
item = m_objects_model->GetParent(item);
if (!item) return;
wxString name = "Part"; wxString name = "Part";
m_objects_model->AddChild(item, name); m_objects_ctrl->Select(m_objects_model->AddChild(item, name));
}); });
btn_delete->Bind(wxEVT_BUTTON, [](wxEvent&) btn_delete->Bind(wxEVT_BUTTON, [](wxEvent&)
{ {
auto item = m_objects_ctrl->GetSelection(); auto item = m_objects_ctrl->GetSelection();
if (!item) return; if (!item) return;
m_objects_model->Delete(item); m_objects_ctrl->Select(m_objects_model->Delete(item));
}); });
//*** //***
@ -1035,6 +1038,22 @@ wxBoxSizer* content_settings(wxWindow *win)
return sizer; return sizer;
} }
void add_object(const std::string &name)
{
wxString item = name;
m_objects_ctrl->Select(m_objects_model->Add(item));
}
void del_object()
{
auto item = m_objects_ctrl->GetSelection();
if (!item) return;
m_objects_ctrl->Select(m_objects_model->Delete(item));
if (m_objects_model->IsEmpty())
m_collpane_settings->show_it(false);
}
void add_expert_mode_part(wxWindow* parent, wxBoxSizer* sizer) void add_expert_mode_part(wxWindow* parent, wxBoxSizer* sizer)
{ {
wxWindowUpdateLocker noUpdates(parent); wxWindowUpdateLocker noUpdates(parent);
@ -1073,17 +1092,10 @@ void add_expert_mode_part(wxWindow* parent, wxBoxSizer* sizer)
add_btn->Bind(wxEVT_BUTTON, [](wxEvent& ) add_btn->Bind(wxEVT_BUTTON, [](wxEvent& )
{ {
wxString name = "Object"; wxString name = "Object";
m_objects_model->Add(name); m_objects_ctrl->Select(m_objects_model->Add(name));
}); });
del_btn->Bind(wxEVT_BUTTON, [](wxEvent& ) del_btn->Bind(wxEVT_BUTTON, [](wxEvent& ) { del_object(); });
{
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 // More experiments with UI
// auto listctrl = new wxDataViewListCtrl(main_page, wxID_ANY, wxDefaultPosition, wxSize(-1, 100)); // auto listctrl = new wxDataViewListCtrl(main_page, wxID_ANY, wxDefaultPosition, wxSize(-1, 100));

View file

@ -174,6 +174,10 @@ wxString L_str(const std::string &str);
// Return wxString from std::string in UTF8 // Return wxString from std::string in UTF8
wxString from_u8(const std::string &str); wxString from_u8(const std::string &str);
// Add object to the list
void add_object(const std::string &name);
// Delete object from the list
void del_object();
void add_expert_mode_part(wxWindow* parent, wxBoxSizer* sizer); void add_expert_mode_part(wxWindow* parent, wxBoxSizer* sizer);
void add_frequently_changed_parameters(wxWindow* parent, wxBoxSizer* sizer, wxFlexGridSizer* preset_sizer); void add_frequently_changed_parameters(wxWindow* parent, wxBoxSizer* sizer, wxFlexGridSizer* preset_sizer);

View file

@ -352,20 +352,21 @@ bool PrusaCollapsiblePane::Layout()
// MyObjectTreeModel // MyObjectTreeModel
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void MyObjectTreeModel::Add(wxString &name) wxDataViewItem MyObjectTreeModel::Add(wxString &name)
{ {
auto root = new MyObjectTreeModelNode(name); auto root = new MyObjectTreeModelNode(name);
m_objects.emplace(root); m_objects.push_back(root);
// notify control // notify control
wxDataViewItem child((void*)root); wxDataViewItem child((void*)root);
wxDataViewItem parent((void*)NULL); wxDataViewItem parent((void*)NULL);
ItemAdded(parent, child); ItemAdded(parent, child);
return child;
} }
void MyObjectTreeModel::AddChild(const wxDataViewItem &parent_item, wxString &name) wxDataViewItem MyObjectTreeModel::AddChild(const wxDataViewItem &parent_item, wxString &name)
{ {
MyObjectTreeModelNode *root = (MyObjectTreeModelNode*)parent_item.GetID(); MyObjectTreeModelNode *root = (MyObjectTreeModelNode*)parent_item.GetID();
if (!root) return; if (!root) return wxDataViewItem(0);
if (root->GetChildren().Count() == 0) if (root->GetChildren().Count() == 0)
{ {
@ -381,13 +382,15 @@ void MyObjectTreeModel::AddChild(const wxDataViewItem &parent_item, wxString &na
// notify control // notify control
wxDataViewItem child((void*)node); wxDataViewItem child((void*)node);
ItemAdded(parent_item, child); ItemAdded(parent_item, child);
return child;
} }
void MyObjectTreeModel::Delete(const wxDataViewItem &item) wxDataViewItem MyObjectTreeModel::Delete(const wxDataViewItem &item)
{ {
auto ret_item = wxDataViewItem(0);
MyObjectTreeModelNode *node = (MyObjectTreeModelNode*)item.GetID(); MyObjectTreeModelNode *node = (MyObjectTreeModelNode*)item.GetID();
if (!node) // happens if item.IsOk()==false if (!node) // happens if item.IsOk()==false
return; return ret_item;
auto node_parent = node->GetParent(); auto node_parent = node->GetParent();
wxDataViewItem parent(node_parent); wxDataViewItem parent(node_parent);
@ -395,23 +398,37 @@ void MyObjectTreeModel::Delete(const wxDataViewItem &item)
// first remove the node from the parent's array of children; // first remove the node from the parent's array of children;
// NOTE: MyObjectTreeModelNodePtrArray is only an array of _pointers_ // NOTE: MyObjectTreeModelNodePtrArray is only an array of _pointers_
// thus removing the node from it doesn't result in freeing it // thus removing the node from it doesn't result in freeing it
if (node_parent) if (node_parent){
auto id = node_parent->GetChildren().Index(node);
node_parent->GetChildren().Remove(node); node_parent->GetChildren().Remove(node);
if (id > 0){
if(id == node_parent->GetChildCount()) id--;
ret_item = wxDataViewItem(node_parent->GetChildren().Item(id));
}
}
else else
{ {
auto it = m_objects.find(node); auto it = find(m_objects.begin(), m_objects.end(), node);
auto id = it - m_objects.begin();
if (it != m_objects.end()) if (it != m_objects.end())
m_objects.erase(it); m_objects.erase(it);
if (id > 0){
if(id == m_objects.size()) id--;
ret_item = wxDataViewItem(m_objects[id]);
}
} }
// free the node // free the node
delete node; delete node;
// set m_containet to FALSE if parent has no child // set m_containet to FALSE if parent has no child
if (node_parent && node_parent->GetChildCount() == 0) if (node_parent && node_parent->GetChildCount() == 0){
node_parent->m_container = false; node_parent->m_container = false;
ret_item = parent;
}
// notify control // notify control
ItemDeleted(parent, item); ItemDeleted(parent, item);
return ret_item;
} }
wxString MyObjectTreeModel::GetName(const wxDataViewItem &item) const wxString MyObjectTreeModel::GetName(const wxDataViewItem &item) const
@ -441,11 +458,6 @@ wxString MyObjectTreeModel::GetScale(const wxDataViewItem &item) const
return node->m_scale; return node->m_scale;
} }
// void MyObjectTreeModel::Delete(const wxDataViewItem &item)
// {
//
// }
void MyObjectTreeModel::GetValue(wxVariant &variant, const wxDataViewItem &item, unsigned int col) const void MyObjectTreeModel::GetValue(wxVariant &variant, const wxDataViewItem &item, unsigned int col) const
{ {
wxASSERT(item.IsOk()); wxASSERT(item.IsOk());
@ -504,7 +516,7 @@ wxDataViewItem MyObjectTreeModel::GetParent(const wxDataViewItem &item) const
MyObjectTreeModelNode *node = (MyObjectTreeModelNode*)item.GetID(); MyObjectTreeModelNode *node = (MyObjectTreeModelNode*)item.GetID();
// objects nodes has no parent too // objects nodes has no parent too
if (m_objects.find(node) != m_objects.end()) if (find(m_objects.begin(), m_objects.end(),node) != m_objects.end())
return wxDataViewItem(0); return wxDataViewItem(0);
return wxDataViewItem((void*)node->GetParent()); return wxDataViewItem((void*)node->GetParent());

View file

@ -207,7 +207,7 @@ public:
class MyObjectTreeModel :public wxDataViewModel class MyObjectTreeModel :public wxDataViewModel
{ {
std::set<MyObjectTreeModelNode*> m_objects; std::vector<MyObjectTreeModelNode*> m_objects;
public: public:
MyObjectTreeModel(){} MyObjectTreeModel(){}
~MyObjectTreeModel() ~MyObjectTreeModel()
@ -216,9 +216,9 @@ public:
delete object; delete object;
} }
void Add(wxString &name); wxDataViewItem Add(wxString &name);
void AddChild(const wxDataViewItem &parent_item, wxString &name); wxDataViewItem AddChild(const wxDataViewItem &parent_item, wxString &name);
void Delete(const wxDataViewItem &item); wxDataViewItem Delete(const wxDataViewItem &item);
bool IsEmpty() { return m_objects.empty(); } bool IsEmpty() { return m_objects.empty(); }
// helper method for wxLog // helper method for wxLog

View file

@ -122,6 +122,12 @@ void set_show_manifold_warning_icon(bool show)
void update_mode() void update_mode()
%code%{ Slic3r::GUI::update_mode(); %}; %code%{ Slic3r::GUI::update_mode(); %};
void add_object(const char *name)
%code%{ Slic3r::GUI::add_object(name); %};
void del_object()
%code%{ Slic3r::GUI::del_object(); %};
std::string fold_utf8_to_ascii(const char *src) std::string fold_utf8_to_ascii(const char *src)
%code%{ RETVAL = Slic3r::fold_utf8_to_ascii(src); %}; %code%{ RETVAL = Slic3r::fold_utf8_to_ascii(src); %};