From f4c50e3701cc8591c85894f13368778a199292ca Mon Sep 17 00:00:00 2001 From: Chris Hennes Date: Mon, 20 Sep 2021 15:55:34 -0500 Subject: [PATCH 1/2] [Spreadsheet] Refactor and simplify paste code --- src/Mod/Spreadsheet/App/PropertySheet.cpp | 56 ++++++++--------------- 1 file changed, 18 insertions(+), 38 deletions(-) diff --git a/src/Mod/Spreadsheet/App/PropertySheet.cpp b/src/Mod/Spreadsheet/App/PropertySheet.cpp index c4798e74d1..3e857b547d 100644 --- a/src/Mod/Spreadsheet/App/PropertySheet.cpp +++ b/src/Mod/Spreadsheet/App/PropertySheet.cpp @@ -367,71 +367,51 @@ void PropertySheet::copyCells(Base::Writer &writer, const std::vector &ra writer.Stream() << "" << std::endl; } -void PropertySheet::pasteCells(XMLReader &reader, const CellAddress &addr) { +void PropertySheet::pasteCells(XMLReader& reader, const CellAddress& addr) { AtomicPropertyChange signaller(*this); bool first = true; - int roffset=0,coffset=0; + int roffset = 0, coffset = 0; reader.readElement("Cells"); - int rangeCount = reader.getAttributeAsInteger("count"); - - for(;rangeCount;--rangeCount) { + for (int rangeCount = reader.getAttributeAsInteger("count"); rangeCount != 0; --rangeCount) { reader.readElement("Range"); CellAddress from(reader.getAttribute("from")); CellAddress to(reader.getAttribute("to")); - int cellCount = reader.getAttributeAsInteger("count"); - Range range(from,to); - bool hasCells = !!cellCount; - for(;cellCount;--cellCount) { - reader.readElement("Cell"); - CellAddress src(reader.getAttribute("address")); - if(first) { + Range range(from, to); + for (int cellCount = reader.getAttributeAsInteger("count"); cellCount != 0; --cellCount) { + if (first) { first = false; roffset = addr.row() - from.row(); coffset = addr.col() - from.col(); - }else - if (!range.next()) - break; - while(src!=*range) { - CellAddress dst(*range); - dst.setRow(dst.row()+roffset); - dst.setCol(dst.col()+coffset); - owner->clear(dst); - owner->cellUpdated(dst); - if (!range.next()) - break; } - CellAddress dst(src.row()+roffset, src.col()+coffset); + reader.readElement("Cell"); + CellAddress src(reader.getAttribute("address")); + CellAddress dst(src.row() + roffset, src.col() + coffset); + owner->clear(dst); + owner->cellUpdated(dst); + auto cell = owner->getNewCell(dst); - cell->setSpans(-1,-1); - cell->restore(reader,true); + cell->setSpans(-1, -1); + cell->restore(reader, true); int rows, cols; - if (cell->getSpans(rows, cols) && (rows > 1 || cols > 1)) + if (cell->getSpans(rows, cols) && (rows > 1 || cols > 1)) mergeCells(dst, CellAddress(dst.row() + rows - 1, dst.col() + cols - 1)); - if(roffset || coffset) { + if (roffset || coffset) { OffsetCellsExpressionVisitor visitor(*this, roffset, coffset); cell->visit(visitor); - if(visitor.changed()) + if (visitor.changed()) recomputeDependencies(dst); } dirty.insert(dst); owner->cellUpdated(dst); } - if(!hasCells || range.next()) { - do { - CellAddress dst(*range); - dst.setRow(dst.row()+roffset); - dst.setCol(dst.col()+coffset); - owner->clear(dst); - owner->cellUpdated(dst); - }while(range.next()); - } } signaller.tryInvoke(); } + Cell * PropertySheet::cellAt(CellAddress address) { std::map::const_iterator j = mergedCells.find(address); From a27d270fa9a72eb71d48870d27a5048bac604f07 Mon Sep 17 00:00:00 2001 From: Chris Hennes Date: Tue, 21 Sep 2021 14:32:42 -0500 Subject: [PATCH 2/2] [Spreadsheet] Add support for copying empty cells --- src/Mod/Spreadsheet/App/PropertySheet.cpp | 27 ++++++++++++----------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/Mod/Spreadsheet/App/PropertySheet.cpp b/src/Mod/Spreadsheet/App/PropertySheet.cpp index 3e857b547d..8a6fa6542d 100644 --- a/src/Mod/Spreadsheet/App/PropertySheet.cpp +++ b/src/Mod/Spreadsheet/App/PropertySheet.cpp @@ -341,25 +341,26 @@ void PropertySheet::Restore(Base::XMLReader &reader) signaller.tryInvoke(); } -void PropertySheet::copyCells(Base::Writer &writer, const std::vector &ranges) const { +void PropertySheet::copyCells(Base::Writer& writer, const std::vector& ranges) const { writer.Stream() << "" << std::endl; writer.Stream() << "" << std::endl; writer.incInd(); - for(auto range : ranges) { - auto r = range; - int count = 0; - do { - if(getValue(*r)) - ++count; - }while(r.next()); + for (auto range : ranges) { writer.Stream() << writer.ind() << "" << std::endl; + << "\" to=\"" << range.toCellString() << "\" count=\"" << range.size() << "\">" << std::endl; writer.incInd(); do { auto cell = getValue(*range); - if(cell) + if (cell) { cell->save(writer); - }while(range.next()); + } + else { + // The cell is empty, so when it's pasted it needs to clear the existing contents + writer.Stream() << writer.ind() << ""; + } + } while (range.next()); writer.decInd(); writer.Stream() << writer.ind() << "" << std::endl; } @@ -374,12 +375,12 @@ void PropertySheet::pasteCells(XMLReader& reader, const CellAddress& addr) { int roffset = 0, coffset = 0; reader.readElement("Cells"); - for (int rangeCount = reader.getAttributeAsInteger("count"); rangeCount != 0; --rangeCount) { + for (int rangeCount = reader.getAttributeAsInteger("count"); rangeCount > 0; --rangeCount) { reader.readElement("Range"); CellAddress from(reader.getAttribute("from")); CellAddress to(reader.getAttribute("to")); Range range(from, to); - for (int cellCount = reader.getAttributeAsInteger("count"); cellCount != 0; --cellCount) { + for (int cellCount = reader.getAttributeAsInteger("count"); cellCount > 0; --cellCount) { if (first) { first = false; roffset = addr.row() - from.row();