Skip to content

Commit

Permalink
[Spreadsheet] Remove alias from dynamic properties on removeRows/Columns
Browse files Browse the repository at this point in the history
When removing a row in a spreadsheet which has an assigned alias, the
alias will not be removed from the list of dynamic properties.

This makes it impossible to create a new alias which uses the same name
even if the original was removed (using removeRows/removeColumns)

Fixes #4492
  • Loading branch information
hyarion committed Jan 4, 2021
1 parent d5092d7 commit d525b29
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Mod/Spreadsheet/App/PropertySheet.cpp
Expand Up @@ -741,6 +741,18 @@ bool PropertySheet::rowSortFunc(const CellAddress & a, const CellAddress & b) {
return false;
}

std::vector<CellAddress> PropertySheet::getRows(int row, int count) const
{
std::vector<CellAddress> keys;

for (const auto &i : data) {
auto key = i.first;
if (key.row() >= row && key.row() < row + count)
keys.push_back(key);
}
return keys;
}

void PropertySheet::removeRows(int row, int count)
{
std::vector<CellAddress> keys;
Expand Down Expand Up @@ -849,6 +861,18 @@ bool PropertySheet::colSortFunc(const CellAddress & a, const CellAddress & b) {
return false;
}

std::vector<CellAddress> PropertySheet::getColumns(int column, int count) const
{
std::vector<CellAddress> keys;

for (const auto &i : data) {
auto key = i.first;
if (key.col() >= column && key.col() < column + count)
keys.push_back(key);
}
return keys;
}

void PropertySheet::removeColumns(int col, int count)
{
std::vector<CellAddress> keys;
Expand Down
4 changes: 4 additions & 0 deletions src/Mod/Spreadsheet/App/PropertySheet.h
Expand Up @@ -130,10 +130,14 @@ class SpreadsheetExport PropertySheet : public App::PropertyExpressionContainer

void insertRows(int row, int count);

std::vector<App::CellAddress> getRows(int row, int count) const;

void removeRows(int row, int count);

void insertColumns(int col, int count);

std::vector<App::CellAddress> getColumns(int column, int count) const;

void removeColumns(int col, int count);

virtual unsigned int getMemSize (void) const override;
Expand Down
16 changes: 16 additions & 0 deletions src/Mod/Spreadsheet/App/Sheet.cpp
Expand Up @@ -1135,6 +1135,14 @@ void Sheet::insertColumns(int col, int count)

void Sheet::removeColumns(int col, int count)
{
// Remove aliases, if defined
for (auto address : cells.getColumns(col, count)) {
auto cell = getCell(address);
std::string aliasStr;
if (cell && cell->getAlias(aliasStr))
removeDynamicProperty(aliasStr.c_str());
}

cells.removeColumns(col, count);
updateColumnsOrRows(true,col,-count);
}
Expand Down Expand Up @@ -1163,6 +1171,14 @@ void Sheet::insertRows(int row, int count)

void Sheet::removeRows(int row, int count)
{
// Remove aliases, if defined
for (auto address : cells.getRows(row, count)) {
auto cell = getCell(address);
std::string aliasStr;
if (cell && cell->getAlias(aliasStr))
removeDynamicProperty(aliasStr.c_str());
}

cells.removeRows(row, count);
updateColumnsOrRows(false,row,-count);
}
Expand Down
16 changes: 16 additions & 0 deletions src/Mod/Spreadsheet/TestSpreadsheet.py
Expand Up @@ -1073,6 +1073,14 @@ def testRemoveRowsAlias(self):
self.doc.recompute()
self.assertEqual(sheet.A3, 3)

def testRemoveRowsAliasReuseName(self):
""" Regression test for issue 4492; deleted aliases remains in database"""
sheet = self.doc.addObject('Spreadsheet::Sheet','Spreadsheet')
sheet.setAlias('B2', 'test')
self.doc.recompute()
sheet.removeRows('2', 1)
sheet.setAlias('B3','test')

def testRemoveColumnsAlias(self):
""" Regression test for issue 4429; remove columns from sheet with aliases"""
sheet = self.doc.addObject('Spreadsheet::Sheet','Spreadsheet')
Expand All @@ -1086,6 +1094,14 @@ def testRemoveColumnsAlias(self):
self.doc.recompute()
self.assertEqual(sheet.C1, 3)

def testRemoveColumnsAliasReuseName(self):
""" Regression test for issue 4492; deleted aliases remains in database"""
sheet = self.doc.addObject('Spreadsheet::Sheet','Spreadsheet')
sheet.setAlias('B2', 'test')
self.doc.recompute()
sheet.removeColumns('B', 1)
sheet.setAlias('C3','test')

def tearDown(self):
#closing doc
FreeCAD.closeDocument(self.doc.Name)

0 comments on commit d525b29

Please sign in to comment.