Refactoring of UnsavedChangesDialog:

Use std containers instead of wxWidgets containers.
Use unique_ptr for memory management.
This commit is contained in:
Vojtech Bubnik 2020-10-16 12:20:51 +02:00
parent dd68c7cf0f
commit 54cbf2abc3
2 changed files with 23 additions and 39 deletions

View File

@ -234,8 +234,8 @@ wxDataViewItem UnsavedChangesModel::AddPreset(Preset::Type type, wxString preset
ModelNode* UnsavedChangesModel::AddOption(ModelNode* group_node, wxString option_name, wxString old_value, wxString new_value) ModelNode* UnsavedChangesModel::AddOption(ModelNode* group_node, wxString option_name, wxString old_value, wxString new_value)
{ {
ModelNode* option = new ModelNode(group_node, option_name, old_value, new_value); group_node->Append(std::make_unique<ModelNode>(group_node, option_name, old_value, new_value));
group_node->Append(option); ModelNode* option = group_node->GetChildren().back().get();
wxDataViewItem group_item = wxDataViewItem((void*)group_node); wxDataViewItem group_item = wxDataViewItem((void*)group_node);
ItemAdded(group_item, wxDataViewItem((void*)option)); ItemAdded(group_item, wxDataViewItem((void*)option));
@ -245,8 +245,8 @@ ModelNode* UnsavedChangesModel::AddOption(ModelNode* group_node, wxString option
ModelNode* UnsavedChangesModel::AddOptionWithGroup(ModelNode* category_node, wxString group_name, wxString option_name, wxString old_value, wxString new_value) ModelNode* UnsavedChangesModel::AddOptionWithGroup(ModelNode* category_node, wxString group_name, wxString option_name, wxString old_value, wxString new_value)
{ {
ModelNode* group_node = new ModelNode(category_node, group_name); category_node->Append(std::make_unique<ModelNode>(category_node, group_name));
category_node->Append(group_node); ModelNode* group_node = category_node->GetChildren().back().get();
ItemAdded(wxDataViewItem((void*)category_node), wxDataViewItem((void*)group_node)); ItemAdded(wxDataViewItem((void*)category_node), wxDataViewItem((void*)group_node));
return AddOption(group_node, option_name, old_value, new_value); return AddOption(group_node, option_name, old_value, new_value);
@ -255,8 +255,8 @@ ModelNode* UnsavedChangesModel::AddOptionWithGroup(ModelNode* category_node, wxS
ModelNode* UnsavedChangesModel::AddOptionWithGroupAndCategory(ModelNode* preset_node, wxString category_name, wxString group_name, ModelNode* UnsavedChangesModel::AddOptionWithGroupAndCategory(ModelNode* preset_node, wxString category_name, wxString group_name,
wxString option_name, wxString old_value, wxString new_value, const std::string category_icon_name) wxString option_name, wxString old_value, wxString new_value, const std::string category_icon_name)
{ {
ModelNode* category_node = new ModelNode(preset_node, category_name, category_icon_name); preset_node->Append(std::make_unique<ModelNode>(preset_node, category_name, category_icon_name));
preset_node->Append(category_node); ModelNode* category_node = preset_node->GetChildren().back().get();
ItemAdded(wxDataViewItem((void*)preset_node), wxDataViewItem((void*)category_node)); ItemAdded(wxDataViewItem((void*)preset_node), wxDataViewItem((void*)category_node));
return AddOptionWithGroup(category_node, group_name, option_name, old_value, new_value); return AddOptionWithGroup(category_node, group_name, option_name, old_value, new_value);
@ -278,14 +278,14 @@ wxDataViewItem UnsavedChangesModel::AddOption(Preset::Type type, wxString catego
for (ModelNode* preset : m_preset_nodes) for (ModelNode* preset : m_preset_nodes)
if (preset->type() == type) if (preset->type() == type)
{ {
for (ModelNode* category : preset->GetChildren()) for (std::unique_ptr<ModelNode> &category : preset->GetChildren())
if (category->text() == category_name) if (category->text() == category_name)
{ {
for (ModelNode* group : category->GetChildren()) for (std::unique_ptr<ModelNode> &group : category->GetChildren())
if (group->text() == group_name) if (group->text() == group_name)
return wxDataViewItem((void*)AddOption(group, option_name, old_value, new_value)); return wxDataViewItem((void*)AddOption(group.get(), option_name, old_value, new_value));
return wxDataViewItem((void*)AddOptionWithGroup(category, group_name, option_name, old_value, new_value)); return wxDataViewItem((void*)AddOptionWithGroup(category.get(), group_name, option_name, old_value, new_value));
} }
return wxDataViewItem((void*)AddOptionWithGroupAndCategory(preset, category_name, group_name, option_name, old_value, new_value, category_icon_name)); return wxDataViewItem((void*)AddOptionWithGroupAndCategory(preset, category_name, group_name, option_name, old_value, new_value, category_icon_name));
@ -298,10 +298,10 @@ static void update_children(ModelNode* parent)
{ {
if (parent->IsContainer()) { if (parent->IsContainer()) {
bool toggle = parent->IsToggled(); bool toggle = parent->IsToggled();
for (ModelNode* child : parent->GetChildren()) { for (std::unique_ptr<ModelNode> &child : parent->GetChildren()) {
child->Toggle(toggle); child->Toggle(toggle);
child->UpdateEnabling(); child->UpdateEnabling();
update_children(child); update_children(child.get());
} }
} }
} }
@ -311,7 +311,7 @@ static void update_parents(ModelNode* node)
ModelNode* parent = node->GetParent(); ModelNode* parent = node->GetParent();
if (parent) { if (parent) {
bool toggle = false; bool toggle = false;
for (ModelNode* child : parent->GetChildren()) { for (std::unique_ptr<ModelNode> &child : parent->GetChildren()) {
if (child->IsToggled()) { if (child->IsToggled()) {
toggle = true; toggle = true;
break; break;
@ -476,16 +476,10 @@ unsigned int UnsavedChangesModel::GetChildren(const wxDataViewItem& parent, wxDa
return m_preset_nodes.size(); return m_preset_nodes.size();
} }
if (node->GetChildCount() == 0) for (std::unique_ptr<ModelNode> &child : node->GetChildren())
return 0; array.Add(wxDataViewItem((void*)child.get()));
unsigned int count = node->GetChildren().GetCount(); return node->GetChildCount();
for (unsigned int pos = 0; pos < count; pos++) {
ModelNode* child = node->GetChildren().Item(pos);
array.Add(wxDataViewItem((void*)child));
}
return count;
} }
@ -506,9 +500,9 @@ wxString UnsavedChangesModel::GetColumnType(unsigned int col) const
static void rescale_children(ModelNode* parent) static void rescale_children(ModelNode* parent)
{ {
if (parent->IsContainer()) { if (parent->IsContainer()) {
for (ModelNode* child : parent->GetChildren()) { for (std::unique_ptr<ModelNode> &child : parent->GetChildren()) {
child->UpdateIcons(); child->UpdateIcons();
rescale_children(child); rescale_children(child.get());
} }
} }
} }

View File

@ -20,7 +20,7 @@ namespace GUI{
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
class ModelNode; class ModelNode;
WX_DEFINE_ARRAY_PTR(ModelNode*, ModelNodePtrArray); using ModelNodePtrArray = std::vector<std::unique_ptr<ModelNode>>;
// On all of 3 different platforms Bitmap+Text icon column looks different // On all of 3 different platforms Bitmap+Text icon column looks different
// because of Markup text is missed or not implemented. // because of Markup text is missed or not implemented.
@ -47,7 +47,7 @@ class ModelNode
// needs to know in advance if a node is or _will be_ a container. // needs to know in advance if a node is or _will be_ a container.
// Thus implementing: // Thus implementing:
// bool IsContainer() const // bool IsContainer() const
// { return m_children.GetCount()>0; } // { return m_children.size()>0; }
// doesn't work with wxGTK when UnsavedChangesModel::AddToClassical is called // doesn't work with wxGTK when UnsavedChangesModel::AddToClassical is called
// AND the classical node was removed (a new node temporary without children // AND the classical node was removed (a new node temporary without children
// would be added to the control) // would be added to the control)
@ -87,15 +87,6 @@ public:
// option node // option node
ModelNode(ModelNode* parent, const wxString& text, const wxString& old_value, const wxString& new_value); ModelNode(ModelNode* parent, const wxString& text, const wxString& old_value, const wxString& new_value);
~ModelNode() {
// free all our children nodes
size_t count = m_children.GetCount();
for (size_t i = 0; i < count; i++) {
ModelNode* child = m_children[i];
delete child;
}
}
bool IsContainer() const { return m_container; } bool IsContainer() const { return m_container; }
bool IsToggled() const { return m_toggle; } bool IsToggled() const { return m_toggle; }
void Toggle(bool toggle = true) { m_toggle = toggle; } void Toggle(bool toggle = true) { m_toggle = toggle; }
@ -105,11 +96,10 @@ public:
ModelNode* GetParent() { return m_parent; } ModelNode* GetParent() { return m_parent; }
ModelNodePtrArray& GetChildren() { return m_children; } ModelNodePtrArray& GetChildren() { return m_children; }
ModelNode* GetNthChild(unsigned int n) { return m_children.Item(n); } ModelNode* GetNthChild(unsigned int n) { return m_children[n].get(); }
unsigned int GetChildCount() const { return m_children.GetCount(); } unsigned int GetChildCount() const { return m_children.size(); }
void Insert(ModelNode* child, unsigned int n) { m_children.Insert(child, n); } void Append(std::unique_ptr<ModelNode> child) { m_children.emplace_back(std::move(child)); }
void Append(ModelNode* child) { m_children.Add(child); }
void UpdateEnabling(); void UpdateEnabling();
void UpdateIcons(); void UpdateIcons();