Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
#5565: Improve algorithm for adding/removing items
  • Loading branch information
codereader committed Mar 28, 2021
1 parent d989e42 commit 9007cdc
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions radiant/ui/materials/MaterialPopulator.cpp
Expand Up @@ -174,12 +174,22 @@ void MaterialPopulator::AddSingleMaterial(const wxutil::TreeModel::Ptr& model, c
parts.insert(parts.begin(), otherMaterialsFolder);
}

while (parts.back().empty())
{
parts.pop_back();
}

wxDataViewItem parentItem;
std::string parentPath;

// Ensure all the parent folders are present
for (auto i = 0; i < parts.size() - 1; ++i)
{
if (parts[i].empty())
{
continue;
}

parentPath += !parentPath.empty() ? "/" : "";
parentPath += parts[i];

Expand Down Expand Up @@ -207,10 +217,25 @@ void MaterialPopulator::AddSingleMaterial(const wxutil::TreeModel::Ptr& model, c

void MaterialPopulator::RemoveSingleMaterial(const wxutil::TreeModel::Ptr& model, const std::string& materialName)
{
auto item = model->FindString(materialName, _columns.fullName);
if (!item.IsOk()) return;
auto normalisedName = string::replace_all_copy(materialName, "//", "/");

model->RemoveItem(item);
// Walk up the parents to check if the removed material leaves any empty folders behind
auto item = model->FindString(normalisedName, _columns.fullName);

while (item.IsOk())
{
auto parentItem = model->GetParent(item);

model->RemoveItem(item);

wxDataViewItemArray children;
if (!parentItem.IsOk() || model->GetChildren(parentItem, children) > 0)
{
break;
}

item = parentItem; // remove the parent too
}
}

void MaterialPopulator::PopulateModel(const wxutil::TreeModel::Ptr& model)
Expand Down

0 comments on commit 9007cdc

Please sign in to comment.