Sheet: modernize C++: use range-based for loop

This commit is contained in:
wmayer
2023-08-15 18:50:26 +02:00
committed by wwmayer
parent 6bb6c0c886
commit a49e104993
6 changed files with 104 additions and 94 deletions

View File

@@ -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;
setDirty(it.first);
}
/* Clear from map */
@@ -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;
@@ -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;
@@ -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());
@@ -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));
}
}
@@ -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());
@@ -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));
}
}

View File

@@ -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();
}
@@ -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 {
@@ -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;
@@ -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();
@@ -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());
}
}
/**

View File

@@ -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);
@@ -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);
@@ -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
@@ -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;
}

View File

@@ -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;
@@ -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;
@@ -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;

View File

@@ -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);
}

View File

@@ -317,8 +317,9 @@ void SheetTableView::insertRows()
std::vector<int> sortedRows;
/* Make sure rows are sorted in ascending order */
for (QModelIndexList::const_iterator it = rows.cbegin(); it != rows.cend(); ++it)
sortedRows.push_back(it->row());
for (const auto& it : rows) {
sortedRows.push_back(it.row());
}
std::sort(sortedRows.begin(), sortedRows.end());
/* Insert rows */
@@ -368,14 +369,15 @@ void SheetTableView::removeRows()
std::vector<int> sortedRows;
/* Make sure rows are sorted in descending order */
for (QModelIndexList::const_iterator it = rows.cbegin(); it != rows.cend(); ++it)
sortedRows.push_back(it->row());
for (const auto& it : rows) {
sortedRows.push_back(it.row());
}
std::sort(sortedRows.begin(), sortedRows.end(), std::greater<>());
/* Remove rows */
Gui::Command::openCommand(QT_TRANSLATE_NOOP("Command", "Remove rows"));
for (std::vector<int>::const_iterator it = sortedRows.begin(); it != sortedRows.end(); ++it) {
Gui::cmdAppObjectArgs(sheet, "removeRows('%s', %d)", rowName(*it).c_str(), 1);
for (const auto& it : sortedRows) {
Gui::cmdAppObjectArgs(sheet, "removeRows('%s', %d)", rowName(it).c_str(), 1);
}
Gui::Command::commitCommand();
Gui::Command::doCommand(Gui::Command::Doc, "App.ActiveDocument.recompute()");
@@ -389,8 +391,9 @@ void SheetTableView::insertColumns()
std::vector<int> sortedColumns;
/* Make sure rows are sorted in ascending order */
for (QModelIndexList::const_iterator it = cols.cbegin(); it != cols.cend(); ++it)
sortedColumns.push_back(it->column());
for (const auto& it : cols) {
sortedColumns.push_back(it.column());
}
std::sort(sortedColumns.begin(), sortedColumns.end());
/* Insert columns */
@@ -441,15 +444,17 @@ void SheetTableView::removeColumns()
std::vector<int> sortedColumns;
/* Make sure rows are sorted in descending order */
for (QModelIndexList::const_iterator it = cols.cbegin(); it != cols.cend(); ++it)
sortedColumns.push_back(it->column());
for (const auto& it : cols) {
sortedColumns.push_back(it.column());
}
std::sort(sortedColumns.begin(), sortedColumns.end(), std::greater<>());
/* Remove columns */
Gui::Command::openCommand(QT_TRANSLATE_NOOP("Command", "Remove rows"));
for (std::vector<int>::const_iterator it = sortedColumns.begin(); it != sortedColumns.end(); ++it)
for (const auto& it : sortedColumns) {
Gui::cmdAppObjectArgs(sheet, "removeColumns('%s', %d)",
columnName(*it).c_str(), 1);
columnName(it).c_str(), 1);
}
Gui::Command::commitCommand();
Gui::Command::doCommand(Gui::Command::Doc, "App.ActiveDocument.recompute()");
}
@@ -488,8 +493,8 @@ void SheetTableView::setSheet(Sheet* _sheet)
// Update row and column spans
std::vector<std::string> usedCells = sheet->getUsedCells();
for (std::vector<std::string>::const_iterator i = usedCells.begin(); i != usedCells.end(); ++i) {
CellAddress address(*i);
for (const auto& i : usedCells) {
CellAddress address(i);
if (sheet->isMergedCell(address)) {
int rows, cols;