From 782e95613da7fb2eb7a2fe9c3c9fbb5b6f756b09 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Wed, 30 Jun 2021 11:42:13 -0700 Subject: Fix table parameter map is not updated with the UI --- core/src/Model/Template/TableTemplate.cpp | 42 +++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 5 deletions(-) (limited to 'core/src/Model/Template/TableTemplate.cpp') diff --git a/core/src/Model/Template/TableTemplate.cpp b/core/src/Model/Template/TableTemplate.cpp index 6c3d86b..ce1d6a5 100644 --- a/core/src/Model/Template/TableTemplate.cpp +++ b/core/src/Model/Template/TableTemplate.cpp @@ -39,6 +39,29 @@ Vec2i TableArrayGroup::FindCell(std::string_view name) { } +template +static bool UpdateElementName(TMap& map, std::string_view oldName, std::string_view newName) +{ + auto iter = map.find(oldName); + if (iter == map.end()) { + return false; + } + + auto elm = iter.value(); + auto [DISCARD, inserted] = map.insert(newName, elm); + if (!inserted) { + return false; + } + + map.erase(iter); + return true; +} + +bool TableArrayGroup::UpdateCellName(std::string_view oldName, std::string_view newName) +{ + return ::UpdateElementName(mName2Cell, oldName, newName); +} + TableInstantiationParameters::TableInstantiationParameters(const TableTemplate& table) : mTable{ &table } { @@ -164,12 +187,11 @@ void TableTemplate::SetCellType(Vec2i pos, TableCell::CellType type) case TableCell::ArrayParametricCell: { auto& ag = mArrayGroups[cell.DataId]; - if (cell.Location.x == ag.LeftCell) { + if (pos.x == ag.LeftCell) { ag.LeftCell++; - } else if (cell.Location.x == ag.RightCell) { + } else if (pos.x == ag.RightCell) { ag.RightCell--; } else { - } } break; } @@ -180,7 +202,7 @@ void TableTemplate::SetCellType(Vec2i pos, TableCell::CellType type) case TableCell::SingularParametricCell: { int idx = pos.y * GetTableWidth() + pos.x; - auto [DISCARD, inserted] = mName2Parameters.emplace(cell.Content, idx); + auto [DISCARD, inserted] = mName2Parameters.insert(cell.Content, idx); // Duplicate name if (!inserted) { @@ -201,6 +223,11 @@ void TableTemplate::SetCellType(Vec2i pos, TableCell::CellType type) cell.Type = type; } +bool TableTemplate::UpdateParameterName(std::string_view oldName, std::string_view newName) +{ + return ::UpdateElementName(mName2Parameters, oldName, newName); +} + int TableTemplate::GetArrayGroupCount() const { return mArrayGroups.size(); @@ -239,7 +266,7 @@ TableArrayGroup* TableTemplate::AddArrayGroup(std::string_view name, int row, in std::swap(left, right); } - auto [DISCARD, inserted] = mName2ArrayGroups.emplace(name, (int)mArrayGroups.size()); + auto [DISCARD, inserted] = mName2ArrayGroups.insert(name, (int)mArrayGroups.size()); if (!inserted) { return nullptr; } @@ -252,6 +279,11 @@ TableArrayGroup* TableTemplate::AddArrayGroup(std::string_view name, int row, in return &mArrayGroups.back(); } +bool TableTemplate::UpdateArrayGroupName(std::string_view oldName, std::string_view newName) +{ + return ::UpdateElementName(mName2ArrayGroups, oldName, newName); +} + bool TableTemplate::ExtendArrayGroupLeft(int id, int n) { assert(n > 0); -- cgit v1.2.3-70-g09d2