From 6d6af2dd3b85680c3e259d99b724a0d8bb3e1be7 Mon Sep 17 00:00:00 2001 From: "Zheng, Lei" Date: Fri, 6 May 2022 11:07:28 +0800 Subject: [PATCH] Spreadsheet: add new API PropertySheet::getNonEmptyCells() To exclude cells without any text content. Used when printing (among other cases) to skip empty cells. --- src/Mod/Spreadsheet/App/PropertySheet.cpp | 22 ++++++++++++++++++---- src/Mod/Spreadsheet/App/PropertySheet.h | 4 +++- src/Mod/Spreadsheet/App/Sheet.cpp | 12 ++++-------- src/Mod/Spreadsheet/Gui/SheetTableView.cpp | 4 ++-- 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/Mod/Spreadsheet/App/PropertySheet.cpp b/src/Mod/Spreadsheet/App/PropertySheet.cpp index 96f75baded..8ad50d7c13 100644 --- a/src/Mod/Spreadsheet/App/PropertySheet.cpp +++ b/src/Mod/Spreadsheet/App/PropertySheet.cpp @@ -152,13 +152,27 @@ bool PropertySheet::isValidAlias(const std::string &candidate) return false; } -std::set PropertySheet::getUsedCells() const +std::vector PropertySheet::getUsedCells() const { - std::set usedSet; + std::vector usedSet; for (std::map::const_iterator i = data.begin(); i != data.end(); ++i) { if (i->second->isUsed()) - usedSet.insert(i->first); + usedSet.push_back(i->first); + } + + return usedSet; +} + +std::vector PropertySheet::getNonEmptyCells() const +{ + std::vector usedSet; + + std::string str; + for (std::map::const_iterator i = data.begin(); i != data.end(); ++i) { + str.clear(); + if (i->second->isUsed() && i->second->getStringContent(str) && !str.empty()) + usedSet.push_back(i->first); } return usedSet; @@ -178,7 +192,7 @@ void PropertySheet::setDirty(CellAddress address) void PropertySheet::setDirty() { AtomicPropertyChange signaller(*this); - for(auto &address : getUsedCells()) { + for(auto &address : getNonEmptyCells()) { auto cell = cellAt(address); std::string content; if(cell && cell->getStringContent(content,false)) { diff --git a/src/Mod/Spreadsheet/App/PropertySheet.h b/src/Mod/Spreadsheet/App/PropertySheet.h index 4b67f8ed46..a40fb5fd35 100644 --- a/src/Mod/Spreadsheet/App/PropertySheet.h +++ b/src/Mod/Spreadsheet/App/PropertySheet.h @@ -113,7 +113,9 @@ public: bool isValidAlias(const std::string &candidate); - std::set getUsedCells() const; + std::vector getUsedCells() const; + + std::vector getNonEmptyCells() const; Sheet * sheet() const { return owner; } diff --git a/src/Mod/Spreadsheet/App/Sheet.cpp b/src/Mod/Spreadsheet/App/Sheet.cpp index 53f88b49a2..477d496b03 100644 --- a/src/Mod/Spreadsheet/App/Sheet.cpp +++ b/src/Mod/Spreadsheet/App/Sheet.cpp @@ -274,8 +274,8 @@ bool Sheet::exportToFile(const std::string &filename, char delimiter, char quote if (!file.is_open()) return false; - std::set usedCells = cells.getUsedCells(); - std::set::const_iterator i = usedCells.begin(); + auto usedCells = cells.getNonEmptyCells(); + auto i = usedCells.begin(); while (i != usedCells.end()) { Property * prop = getProperty(*i); @@ -1184,12 +1184,8 @@ int Sheet::getRowHeight(int row) const std::vector Sheet::getUsedCells() const { std::vector usedCells; - - // Insert int usedSet - std::set usedSet = cells.getUsedCells(); - - for (std::set::const_iterator i = usedSet.begin(); i != usedSet.end(); ++i) - usedCells.push_back(i->toString()); + for (const auto &addr : cells.getUsedCells()) + usedCells.push_back(addr.toString()); return usedCells; } diff --git a/src/Mod/Spreadsheet/Gui/SheetTableView.cpp b/src/Mod/Spreadsheet/Gui/SheetTableView.cpp index 2c85283a8c..509aa02ff1 100644 --- a/src/Mod/Spreadsheet/Gui/SheetTableView.cpp +++ b/src/Mod/Spreadsheet/Gui/SheetTableView.cpp @@ -769,7 +769,7 @@ void SheetTableView::finishEditWithMove(int keyPressed, Qt::KeyboardModifiers mo { // End should take you to the last occupied cell in the current column // Ctrl-End takes you to the last cell in the sheet - auto usedCells = sheet->getCells()->getUsedCells(); + auto usedCells = sheet->getCells()->getNonEmptyCells(); for (const auto& cell : usedCells) { if (modifiers == Qt::NoModifier) { if (cell.col() == targetColumn) @@ -984,7 +984,7 @@ void SheetTableView::contextMenuEvent(QContextMenuEvent *) QString SheetTableView::toHtml() const { - std::set cells = sheet->getCells()->getUsedCells(); + auto cells = sheet->getCells()->getNonEmptyCells(); int rowCount = 1; int colCount = 1; for (const auto& it : cells) {