ObjectDataViewModel: Fixed AddInfoChild() and Delete() functions

Delete () function did not account for InfoItems that were added before VolumeItems
As a result, There was possibility when deletion of penult VolumeItem wasn't invoke deletion of the last VolumeItem

AddInfoChild() was not respect to existed SettingsItem
SettingsItem have to be on a first place always.
This commit is contained in:
YuSanka 2021-09-09 17:50:14 +02:00
parent 9c5c9a0e78
commit 0abab45efa
2 changed files with 35 additions and 34 deletions

View file

@ -406,8 +406,10 @@ wxDataViewItem ObjectDataViewModel::AddInfoChild(const wxDataViewItem &parent_it
// The new item should be added according to its order in InfoItemType. // The new item should be added according to its order in InfoItemType.
// Find last info item with lower index and append after it. // Find last info item with lower index and append after it.
const auto& children = root->GetChildren(); const auto& children = root->GetChildren();
int idx = -1; // If SettingsItem exists, it have to be on the first position always
for (int i=0; i<int(children.size()); ++i) { bool is_settings_item = children.size() > 0 && children[0]->GetType() == itSettings;
int idx = is_settings_item ? 0 : -1;
for (size_t i = is_settings_item ? 1 : 0; i < children.size(); ++i) {
if (children[i]->GetType() == itInfo && int(children[i]->GetInfoItemType()) < int(info_type) ) if (children[i]->GetType() == itInfo && int(children[i]->GetInfoItemType()) < int(info_type) )
idx = i; idx = i;
} }
@ -619,6 +621,15 @@ wxDataViewItem ObjectDataViewModel::AddLayersChild(const wxDataViewItem &parent_
return layer_item; return layer_item;
} }
size_t ObjectDataViewModel::GetItemIndexForFirstVolume(ObjectDataViewModelNode* node_parent)
{
assert(node_parent->m_volumes_cnt > 0);
for (size_t vol_idx = 0; vol_idx < node_parent->GetChildCount(); vol_idx++)
if (node_parent->GetNthChild(vol_idx)->GetType() == itVolume)
return vol_idx;
return -1;
}
wxDataViewItem ObjectDataViewModel::Delete(const wxDataViewItem &item) wxDataViewItem ObjectDataViewModel::Delete(const wxDataViewItem &item)
{ {
auto ret_item = wxDataViewItem(0); auto ret_item = wxDataViewItem(0);
@ -714,44 +725,34 @@ wxDataViewItem ObjectDataViewModel::Delete(const wxDataViewItem &item)
} }
// if there is last volume item after deleting, delete this last volume too // if there is last volume item after deleting, delete this last volume too
if (node_parent->GetChildCount() <= 3) // 3??? #ys_FIXME if (node_parent->m_volumes_cnt == 1)
{ {
int vol_cnt = 0; // delete selected (penult) volume
int vol_idx = 0; delete node;
for (size_t i = 0; i < node_parent->GetChildCount(); ++i) { ItemDeleted(parent, item);
if (node_parent->GetNthChild(i)->GetType() == itVolume) {
vol_idx = i;
vol_cnt++;
}
if (vol_cnt > 1)
break;
}
if (vol_cnt == 1) { // get index of the last VolumeItem in CildrenList
delete node; size_t vol_idx = GetItemIndexForFirstVolume(node_parent);
ItemDeleted(parent, item);
ObjectDataViewModelNode *last_child_node = node_parent->GetNthChild(vol_idx); // delete this last volume
DeleteSettings(wxDataViewItem(last_child_node)); ObjectDataViewModelNode *last_child_node = node_parent->GetNthChild(vol_idx);
node_parent->GetChildren().Remove(last_child_node); DeleteSettings(wxDataViewItem(last_child_node));
node_parent->m_volumes_cnt = 0; node_parent->GetChildren().Remove(last_child_node);
delete last_child_node; node_parent->m_volumes_cnt = 0;
delete last_child_node;
#ifndef __WXGTK__ #ifndef __WXGTK__
if (node_parent->GetChildCount() == 0) if (node_parent->GetChildCount() == 0)
node_parent->m_container = false; node_parent->m_container = false;
#endif //__WXGTK__ #endif //__WXGTK__
ItemDeleted(parent, wxDataViewItem(last_child_node)); ItemDeleted(parent, wxDataViewItem(last_child_node));
wxCommandEvent event(wxCUSTOMEVT_LAST_VOLUME_IS_DELETED); wxCommandEvent event(wxCUSTOMEVT_LAST_VOLUME_IS_DELETED);
auto it = find(m_objects.begin(), m_objects.end(), node_parent); auto it = find(m_objects.begin(), m_objects.end(), node_parent);
event.SetInt(it == m_objects.end() ? -1 : it - m_objects.begin()); event.SetInt(it == m_objects.end() ? -1 : it - m_objects.begin());
wxPostEvent(m_ctrl, event); wxPostEvent(m_ctrl, event);
ret_item = parent; return parent;
return ret_item;
}
} }
} }
else else
@ -1361,8 +1362,7 @@ wxDataViewItem ObjectDataViewModel::ReorganizeChildren( const int current_volume
if (!node_parent) // happens if item.IsOk()==false if (!node_parent) // happens if item.IsOk()==false
return ret_item; return ret_item;
size_t shift; size_t shift = GetItemIndexForFirstVolume(node_parent);
for (shift = 0; shift < node_parent->GetChildCount() && node_parent->GetNthChild(shift)->GetType() != itVolume; shift ++);
ObjectDataViewModelNode *deleted_node = node_parent->GetNthChild(current_volume_id+shift); ObjectDataViewModelNode *deleted_node = node_parent->GetNthChild(current_volume_id+shift);
node_parent->GetChildren().Remove(deleted_node); node_parent->GetChildren().Remove(deleted_node);

View file

@ -279,6 +279,7 @@ public:
const t_layer_height_range& layer_range, const t_layer_height_range& layer_range,
const int extruder = 0, const int extruder = 0,
const int index = -1); const int index = -1);
size_t GetItemIndexForFirstVolume(ObjectDataViewModelNode* node_parent);
wxDataViewItem Delete(const wxDataViewItem &item); wxDataViewItem Delete(const wxDataViewItem &item);
wxDataViewItem DeleteLastInstance(const wxDataViewItem &parent_item, size_t num); wxDataViewItem DeleteLastInstance(const wxDataViewItem &parent_item, size_t num);
void DeleteAll(); void DeleteAll();