Skip to content

Commit

Permalink
Sheet: modernize C++: use range-based for loop
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Aug 15, 2023
1 parent 6bb6c0c commit a49e104
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 94 deletions.
85 changes: 41 additions & 44 deletions src/Mod/Spreadsheet/App/PropertySheet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,10 @@ TYPESYSTEM_SOURCE(Spreadsheet::PropertySheet , App::PropertyExpressionContainer)

void PropertySheet::clear()
{
std::map<CellAddress, Cell* >::iterator i = data.begin();

/* Clear cells */
while (i != data.end()) {
delete i->second;
setDirty(i->first);
++i;
for (auto & it : data) {
delete it.second;

Check warning on line 62 in src/Mod/Spreadsheet/App/PropertySheet.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

deleting a pointer through a type that is not marked 'gsl::owner<>'; consider using a smart pointer instead [cppcoreguidelines-owning-memory]
setDirty(it.first);
}

/* Clear from map */
Expand Down Expand Up @@ -188,9 +185,9 @@ std::vector<CellAddress> PropertySheet::getUsedCells() const
{
std::vector<CellAddress> usedSet;

for (std::map<CellAddress, Cell*>::const_iterator i = data.begin(); i != data.end(); ++i) {
if (i->second->isUsed())
usedSet.push_back(i->first);
for (const auto& i : data) {
if (i.second->isUsed())
usedSet.push_back(i.first);
}

return usedSet;
Expand All @@ -207,10 +204,10 @@ std::vector<CellAddress> PropertySheet::getNonEmptyCells() const
std::vector<CellAddress> usedSet;

std::string str;
for (std::map<CellAddress, Cell*>::const_iterator i = data.begin(); i != data.end(); ++i) {
for (const auto& i : data) {
str.clear();
if (i->second->isUsed() && i->second->getStringContent(str) && !str.empty())
usedSet.push_back(i->first);
if (i.second->isUsed() && i.second->getStringContent(str) && !str.empty())
usedSet.push_back(i.first);
}

return usedSet;
Expand Down Expand Up @@ -927,16 +924,16 @@ void PropertySheet::removeRows(int row, int count)
AtomicPropertyChange signaller(*this);

// move all the aliases first so dependencies can be calculated correctly
for (std::vector<CellAddress>::const_iterator i = keys.begin(); i != keys.end(); ++i) {
if (i->row() >= row && i->row() < row + count)
clearAlias(*i);
else if (i->row() >= row + count)
moveAlias(*i, CellAddress(i->row() - count, i->col()));
for (const auto& key : keys) {
if (key.row() >= row && key.row() < row + count)
clearAlias(key);
else if (key.row() >= row + count)
moveAlias(key, CellAddress(key.row() - count, key.col()));
}

int spanRows, spanCols;
for (std::vector<CellAddress>::const_iterator i = keys.begin(); i != keys.end(); ++i) {
std::map<CellAddress, Cell*>::iterator j = data.find(*i);
for (const auto& key : keys) {
std::map<CellAddress, Cell*>::iterator j = data.find(key);

assert(j != data.end());

Expand All @@ -946,19 +943,19 @@ void PropertySheet::removeRows(int row, int count)
visitor.reset();
cell->visit(visitor);
if (visitor.changed()) {
setDirty(*i);
recomputeDependencies(*i);
setDirty(key);
recomputeDependencies(key);
}

if (i->row() >= row && i->row() < row + count)
clear(*i, false); // aliases were cleared earlier
else if (i->row() >= row + count)
moveCell(*i, CellAddress(i->row() - count, i->col()), renames);
else if (cell->getSpans(spanRows, spanCols) && i->row() + spanRows >= row) {
if (i->row() + spanRows >= row + count)
if (key.row() >= row && key.row() < row + count)
clear(key, false); // aliases were cleared earlier
else if (key.row() >= row + count)
moveCell(key, CellAddress(key.row() - count, key.col()), renames);
else if (cell->getSpans(spanRows, spanCols) && key.row() + spanRows >= row) {
if (key.row() + spanRows >= row + count)
spanRows -= count;
else
spanRows = i->row() - row;
spanRows = key.row() - row;
mergeCells(j->first, CellAddress(j->first.row()+spanRows-1, j->first.col()+spanCols-1));
}
}
Expand Down Expand Up @@ -1055,16 +1052,16 @@ void PropertySheet::removeColumns(int col, int count)
AtomicPropertyChange signaller(*this);

// move all the aliases first so dependencies can be calculated correctly
for (std::vector<CellAddress>::const_iterator i = keys.begin(); i != keys.end(); ++i) {
if (i->col() >= col && i->col() < col + count)
clearAlias(*i);
else if (i->col() >= col + count)
moveAlias(*i, CellAddress(i->row(), i->col() - count));
for (const auto& key : keys) {
if (key.col() >= col && key.col() < col + count)
clearAlias(key);
else if (key.col() >= col + count)
moveAlias(key, CellAddress(key.row(), key.col() - count));
}

int spanRows, spanCols;
for (std::vector<CellAddress>::const_iterator i = keys.begin(); i != keys.end(); ++i) {
std::map<CellAddress, Cell*>::iterator j = data.find(*i);
for (const auto& key : keys) {
std::map<CellAddress, Cell*>::iterator j = data.find(key);

assert(j != data.end());

Expand All @@ -1074,19 +1071,19 @@ void PropertySheet::removeColumns(int col, int count)
visitor.reset();
cell->visit(visitor);
if (visitor.changed()) {
setDirty(*i);
recomputeDependencies(*i);
setDirty(key);
recomputeDependencies(key);
}

if (i->col() >= col && i->col() < col + count)
clear(*i, false); // aliases were cleared earlier
else if (i->col() >= col + count)
moveCell(*i, CellAddress(i->row(), i->col() - count), renames);
else if (cell->getSpans(spanRows, spanCols) && i->col() + spanCols >= col) {
if (i->col() + spanCols >= col + count)
if (key.col() >= col && key.col() < col + count)
clear(key, false); // aliases were cleared earlier
else if (key.col() >= col + count)
moveCell(key, CellAddress(key.row(), key.col() - count), renames);
else if (cell->getSpans(spanRows, spanCols) && key.col() + spanCols >= col) {
if (key.col() + spanCols >= col + count)
spanCols -= count;
else
spanCols = i->col() - col;
spanCols = key.col() - col;
mergeCells(j->first, CellAddress(j->first.row()+spanRows-1, j->first.col()+spanCols-1));
}
}
Expand Down
33 changes: 18 additions & 15 deletions src/Mod/Spreadsheet/App/Sheet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,18 @@ void Sheet::clearAll()

std::vector<std::string> propNames = props.getDynamicPropertyNames();

for (std::vector<std::string>::const_iterator i = propNames.begin(); i != propNames.end(); ++i)
this->removeDynamicProperty((*i).c_str());
for (const auto & propName : propNames) {
this->removeDynamicProperty(propName.c_str());
}

propAddress.clear();
cellErrors.clear();
columnWidths.clear();
rowHeights.clear();

for (ObserverMap::iterator i = observers.begin(); i != observers.end(); ++i)
delete i->second;
for (auto & observer : observers) {
delete observer.second;
}
observers.clear();
}

Expand Down Expand Up @@ -230,8 +232,7 @@ bool Sheet::importFromFile(const std::string &filename, char delimiter, char quo

static void writeEscaped(std::string const& s, char quoteChar, char escapeChar, std::ostream & out) {
out << quoteChar;
for (std::string::const_iterator i = s.begin(), end = s.end(); i != end; ++i) {
unsigned char c = *i;
for (unsigned char c : s) {
if (c != quoteChar)
out << c;
else {
Expand Down Expand Up @@ -909,9 +910,9 @@ DocumentObjectExecReturn *Sheet::execute()
std::set<CellAddress> dirtyCells = cells.getDirty();

// Always recompute cells that have failed
for (std::set<CellAddress>::const_iterator i = cellErrors.begin(); i != cellErrors.end(); ++i) {
cells.recomputeDependencies(*i);
dirtyCells.insert(*i);
for (auto cellError : cellErrors) {
cells.recomputeDependencies(cellError);
dirtyCells.insert(cellError);
}

DependencyList graph;
Expand Down Expand Up @@ -1033,14 +1034,15 @@ DocumentObjectExecReturn *Sheet::execute()
// Signal update of column widths
const std::set<int> & dirtyColumns = columnWidths.getDirty();

for (std::set<int>::const_iterator i = dirtyColumns.begin(); i != dirtyColumns.end(); ++i)
columnWidthChanged(*i, columnWidths.getValue(*i));
for (int dirtyColumn : dirtyColumns) {
columnWidthChanged(dirtyColumn, columnWidths.getValue(dirtyColumn));
}

// Signal update of row heights
const std::set<int> & dirtyRows = rowHeights.getDirty();

for (std::set<int>::const_iterator i = dirtyRows.begin(); i != dirtyRows.end(); ++i)
rowHeightChanged(*i, rowHeights.getValue(*i));
for (int dirtyRow : dirtyRows)
rowHeightChanged(dirtyRow, rowHeights.getValue(dirtyRow));

//cells.clearDirty();
rowHeights.clearDirty();
Expand Down Expand Up @@ -1460,8 +1462,9 @@ void Sheet::providesTo(CellAddress address, std::set<std::string> & result) cons
std::string fullName = getFullName() + ".";
std::set<CellAddress> tmpResult = cells.getDeps(fullName + address.toString());

for (std::set<CellAddress>::const_iterator i = tmpResult.begin(); i != tmpResult.end(); ++i)
result.insert(fullName + i->toString());
for (const auto& i : tmpResult) {
result.insert(fullName + i.toString());
}
}

/**
Expand Down
27 changes: 16 additions & 11 deletions src/Mod/Spreadsheet/App/SheetPyImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,9 @@ PyObject* SheetPy::setStyle(PyObject *args)
if (cell)
cell->getStyle(oldStyle);

for (std::set<std::string>::const_iterator it = oldStyle.begin(); it != oldStyle.end(); ++it)
style.insert(*it);
for (const auto & it : oldStyle) {
style.insert(it);
}

// Set new style
getSheetPtr()->setStyle(*rangeIter, style);
Expand All @@ -385,8 +386,9 @@ PyObject* SheetPy::setStyle(PyObject *args)
if (cell)
cell->getStyle(oldStyle);

for (std::set<std::string>::const_iterator it = style.begin(); it != style.end(); ++it)
oldStyle.erase(*it);
for (const auto & it : style) {
oldStyle.erase(it);
}

// Set new style
getSheetPtr()->setStyle(*rangeIter, oldStyle);
Expand All @@ -406,13 +408,15 @@ PyObject* SheetPy::setStyle(PyObject *args)
newStyle = oldStyle;
}

for (std::set<std::string>::const_iterator i = style.begin(); i != style.end(); ++i) {
if (oldStyle.find(*i) == oldStyle.end())
for (const auto & i : style) {
if (oldStyle.find(i) == oldStyle.end()) {
// Not found in oldstyle; add it to newStyle
newStyle.insert(*i);
else
newStyle.insert(i);
}
else {
// Found in oldStyle, remove it from newStyle
newStyle.erase(*i);
newStyle.erase(i);
}
}

// Set new style
Expand Down Expand Up @@ -449,8 +453,9 @@ PyObject* SheetPy::getStyle(PyObject *args)
if (cell && cell->getStyle(style)) {
PyObject * s = PySet_New(nullptr);

for (std::set<std::string>::const_iterator i = style.begin(); i != style.end(); ++i)
PySet_Add(s, PyUnicode_FromString((*i).c_str()));
for (const auto & i : style) {
PySet_Add(s, PyUnicode_FromString(i.c_str()));
}

return s;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Mod/Spreadsheet/Gui/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,8 @@ void CmdSpreadsheetStyleBold::activated(int iMsg)
if (!selection.empty()) {
bool allBold = true;

for (QModelIndexList::const_iterator it = selection.cbegin(); it != selection.cend(); ++it) {
const Cell * cell = sheet->getCell(CellAddress((*it).row(), (*it).column()));
for (const auto& it : selection) {
const Cell * cell = sheet->getCell(CellAddress(it.row(), it.column()));

if (cell) {
std::set<std::string> style;
Expand Down Expand Up @@ -689,8 +689,8 @@ void CmdSpreadsheetStyleItalic::activated(int iMsg)
if (!selection.empty()) {
bool allItalic = true;

for (QModelIndexList::const_iterator it = selection.cbegin(); it != selection.cend(); ++it) {
const Cell * cell = sheet->getCell(CellAddress((*it).row(), (*it).column()));
for (const auto& it : selection) {
const Cell * cell = sheet->getCell(CellAddress(it.row(), it.column()));

if (cell) {
std::set<std::string> style;
Expand Down Expand Up @@ -763,8 +763,8 @@ void CmdSpreadsheetStyleUnderline::activated(int iMsg)
if (!selection.empty()) {
bool allUnderline = true;

for (QModelIndexList::const_iterator it = selection.cbegin(); it != selection.cend(); ++it) {
const Cell * cell = sheet->getCell(CellAddress((*it).row(), (*it).column()));
for (const auto& it : selection) {
const Cell * cell = sheet->getCell(CellAddress(it.row(), it.column()));

if (cell) {
std::set<std::string> style;
Expand Down
8 changes: 4 additions & 4 deletions src/Mod/Spreadsheet/Gui/SheetModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ QVariant SheetModel::data(const QModelIndex& index, int role) const
if (role == Qt::FontRole && cell->getStyle(style)) {
QFont f;

for (std::set<std::string>::const_iterator i = style.begin(); i != style.end(); ++i) {
if (*i == "bold")
for (const auto& i : style) {
if (i == "bold")
f.setBold(true);
else if (*i == "italic")
else if (i == "italic")
f.setItalic(true);
else if (*i == "underline")
else if (i == "underline")
f.setUnderline(true);
}

Expand Down
Loading

0 comments on commit a49e104

Please sign in to comment.