diff options
author | rtk0c <[email protected]> | 2021-06-30 11:42:13 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2021-06-30 11:42:13 -0700 |
commit | 782e95613da7fb2eb7a2fe9c3c9fbb5b6f756b09 (patch) | |
tree | 62337ef031f3213e520185a414664c5d6fd77891 /core/src/Model | |
parent | df1a386b067a62c879b48b1eeca50b2982b4f92d (diff) |
Fix table parameter map is not updated with the UI
Diffstat (limited to 'core/src/Model')
-rw-r--r-- | core/src/Model/Template/TableTemplate.cpp | 42 | ||||
-rw-r--r-- | core/src/Model/Template/TableTemplate.hpp | 12 |
2 files changed, 49 insertions, 5 deletions
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 <class TMap> +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); diff --git a/core/src/Model/Template/TableTemplate.hpp b/core/src/Model/Template/TableTemplate.hpp index a9a89d4..141e952 100644 --- a/core/src/Model/Template/TableTemplate.hpp +++ b/core/src/Model/Template/TableTemplate.hpp @@ -32,6 +32,7 @@ public: }; public: + /// Display content of this cell. This doesn't necessarily have to line up with the parameter name (if this cell is one). std::string Content; Vec2i Location; /// Location of the primary (top left) cell, if this cell is a part of a merged group. @@ -102,6 +103,7 @@ public: /// Find the location of the cell within this array group that has the given name. Vec2i FindCell(std::string_view name); + bool UpdateCellName(std::string_view oldName, std::string_view newName); }; // Forward declaration of libxlsxwriter structs @@ -165,13 +167,23 @@ public: const TableCell& GetCell(Vec2i pos) const; TableCell& GetCell(Vec2i pos); + /// <ul> + /// <li> In case of becoming a SingularParametricCell: the parameter name is filled with TableCell::Content. + /// <li> In case of becoming a ArrayGroupParametricCell: the array group name is automatically generated as the nth group it would be come. + /// i.e., if there aRe currently 3 groups, the newly created group would be named "4". + /// If this name collides with an existing group, hyphens \c - will be append to the name until no collision happens. + /// </ul> void SetCellType(Vec2i pos, TableCell::CellType type); + /// Updates the parameter cell to a new name. Returns true on success and false on failure (param not found or name duplicates). + bool UpdateParameterName(std::string_view oldName, std::string_view newName); + int GetArrayGroupCount() const; const TableArrayGroup& GetArrayGroup(int id) const; TableArrayGroup& GetArrayGroup(int id); TableArrayGroup* AddArrayGroup(int row, int left, int right); TableArrayGroup* AddArrayGroup(std::string_view name, int row, int left, int right); + bool UpdateArrayGroupName(std::string_view oldName, std::string_view newName); bool ExtendArrayGroupLeft(int id, int n); bool ExtendArrayGroupRight(int id, int n); |