sc/inc/SheetView.hxx | 36 +++++++++++++++++++++++-- sc/inc/SheetViewManager.hxx | 6 +--- sc/source/core/data/SheetView.cxx | 43 ++++++++++++++++++------------- sc/source/core/data/SheetViewManager.cxx | 34 ++---------------------- sc/source/ui/view/viewfunc.cxx | 9 +++++- 5 files changed, 70 insertions(+), 58 deletions(-)
New commits: commit caca64b2720de17259c6d60c4de62a8ccd7ece3c Author: Tomaž Vajngerl <[email protected]> AuthorDate: Fri Sep 12 21:57:29 2025 +0200 Commit: Miklos Vajna <[email protected]> CommitDate: Fri Jan 23 10:32:21 2026 +0100 sc: introduce SortOrderReverser to handle sort order conversion SortOrderReverser class handles the reversing of the sort order, which is needed on the sheet view and default view level and is more or less duplicated code in SheetView and SheetViewManager. With SortOrderReverser it is possible to remove the duplication and write a unit test just to test reversing the sort. Change-Id: I7a0726bf71e0b25bcf68403b7c13c3dfac889dd5 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/190910 Reviewed-by: Tomaž Vajngerl <[email protected]> Tested-by: Jenkins Reviewed-on: https://gerrit.libreoffice.org/c/core/+/197820 Tested-by: Jenkins CollaboraOffice <[email protected]> Reviewed-by: Miklos Vajna <[email protected]> diff --git a/sc/inc/SheetView.hxx b/sc/inc/SheetView.hxx index 5c36ddf01fc8..3a45450c9d79 100644 --- a/sc/inc/SheetView.hxx +++ b/sc/inc/SheetView.hxx @@ -13,11 +13,35 @@ #include "types.hxx" #include <rtl/ustring.hxx> #include "SheetViewTypes.hxx" +#include <optional> class ScTable; namespace sc { +/** Stores the sort order and can reverse the sorting of rows (unsorting). */ +struct SortOrderReverser +{ +public: + SCROW mnFirstRow; + SCROW mnLastRow; + std::vector<SCCOLROW> maOrder; + + /** Reverse the sort for the input row and output the unsorted row. + * + * Uses the sort order. The row must be between range of [first row, last row] + * or it will return the input row without modification. + **/ + SCROW unsort(SCROW nRow) const; + + /** Adds or combines the order indices. + * + * Adds the indices if none are present, or combines the indices if the order indices + * were already added previously. + **/ + void addOrderIndices(std::vector<SCCOLROW> const& rOrder, SCROW firstRow, SCROW lastRow); +}; + /** Stores information of a sheet view. * * A sheet view is a special view of a sheet that can be filtered and sorted @@ -30,9 +54,7 @@ private: bool mbSynced = true; OUString maName; - SCROW mnFirstRow; - SCROW mnLastRow; - std::vector<SCCOLROW> maOrder; + std::optional<SortOrderReverser> moSortOrder; SheetViewID mnID; public: @@ -53,8 +75,14 @@ public: /** Is the sheet view synced with its default view. */ bool isSynced() const { return mbSynced; } + std::optional<SortOrderReverser> const& getSortOrder() const { return moSortOrder; } + + /** Adds or combines the order indices. + * + * Adds the indices if none are present, or combines the indices if the order indices + * were already added previously. + **/ void addOrderIndices(std::vector<SCCOLROW> const& rOrder, SCROW firstRow, SCROW lastRow); - SCROW unsort(SCROW nRow) const; }; } diff --git a/sc/inc/SheetViewManager.hxx b/sc/inc/SheetViewManager.hxx index a0e6527b1d11..205f08feb682 100644 --- a/sc/inc/SheetViewManager.hxx +++ b/sc/inc/SheetViewManager.hxx @@ -28,9 +28,7 @@ private: std::vector<std::shared_ptr<SheetView>> maViews; sal_Int32 maNameCounter = 0; - std::vector<SCCOLROW> maOrder; - SCROW mnFirstRow; - SCROW mnLastRow; + std::optional<SortOrderReverser> moSortOrder; bool isValidSheetViewID(SheetViewID nID) const { @@ -70,8 +68,8 @@ public: static OUString defaultViewName(); + std::optional<SortOrderReverser> const& getSortOrder() const { return moSortOrder; } void addOrderIndices(std::vector<SCCOLROW> const& rOrder, SCROW firstRow, SCROW lastRow); - SCROW unsort(SCROW nRow); }; } diff --git a/sc/source/core/data/SheetView.cxx b/sc/source/core/data/SheetView.cxx index a7b103deeb7c..f70a1000c272 100644 --- a/sc/source/core/data/SheetView.cxx +++ b/sc/source/core/data/SheetView.cxx @@ -12,25 +12,12 @@ namespace sc { -SheetView::SheetView(ScTable* pTable, OUString const& rName, SheetViewID nID) - : mpTable(pTable) - , maName(rName) - , mnID(nID) -{ -} - -ScTable* SheetView::getTablePointer() const { return mpTable; } - -SCTAB SheetView::getTableNumber() const -{ - assert(mpTable); - return mpTable->GetTab(); -} - -void SheetView::addOrderIndices(std::vector<SCCOLROW> const& rOrder, SCROW firstRow, SCROW lastRow) +void SortOrderReverser::addOrderIndices(std::vector<SCCOLROW> const& rOrder, SCROW firstRow, + SCROW lastRow) { mnFirstRow = firstRow; mnLastRow = lastRow; + if (maOrder.empty()) { maOrder = rOrder; @@ -48,7 +35,7 @@ void SheetView::addOrderIndices(std::vector<SCCOLROW> const& rOrder, SCROW first } } -SCROW SheetView::unsort(SCROW nRow) const +SCROW SortOrderReverser::unsort(SCROW nRow) const { if (maOrder.empty()) return nRow; @@ -61,6 +48,28 @@ SCROW SheetView::unsort(SCROW nRow) const } return nRow; } + +SheetView::SheetView(ScTable* pTable, OUString const& rName, SheetViewID nID) + : mpTable(pTable) + , maName(rName) + , mnID(nID) +{ +} + +ScTable* SheetView::getTablePointer() const { return mpTable; } + +SCTAB SheetView::getTableNumber() const +{ + assert(mpTable); + return mpTable->GetTab(); +} + +void SheetView::addOrderIndices(std::vector<SCCOLROW> const& rOrder, SCROW firstRow, SCROW lastRow) +{ + if (!moSortOrder) + moSortOrder.emplace(); + moSortOrder->addOrderIndices(rOrder, firstRow, lastRow); +} } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/source/core/data/SheetViewManager.cxx b/sc/source/core/data/SheetViewManager.cxx index 02a791dab158..f021a26ba306 100644 --- a/sc/source/core/data/SheetViewManager.cxx +++ b/sc/source/core/data/SheetViewManager.cxx @@ -133,37 +133,9 @@ OUString SheetViewManager::defaultViewName() { return ScResId(STR_SHEET_VIEW_DEF void SheetViewManager::addOrderIndices(std::vector<SCCOLROW> const& rOrder, SCROW nFirstRow, SCROW nLastRow) { - mnFirstRow = nFirstRow; - mnLastRow = nLastRow; - if (maOrder.empty()) - { - maOrder = rOrder; - } - else - { - assert(maOrder.size() == rOrder.size()); - std::vector<SCCOLROW> newOrder(maOrder.size()); - for (size_t nIndex = 0; nIndex < maOrder.size(); ++nIndex) - { - size_t nSortedIndex = rOrder[nIndex]; - newOrder[nIndex] = maOrder[nSortedIndex - 1]; - } - maOrder = newOrder; - } -} - -SCROW SheetViewManager::unsort(SCROW nRow) -{ - if (maOrder.empty()) - return nRow; - - if (nRow >= mnFirstRow && nRow <= mnLastRow) - { - size_t index = nRow - mnFirstRow; - auto nUnsortedRow = mnFirstRow + maOrder[index] - 1; - return nUnsortedRow; - } - return nRow; + if (!moSortOrder) + moSortOrder.emplace(); + moSortOrder->addOrderIndices(rOrder, nFirstRow, nLastRow); } } diff --git a/sc/source/ui/view/viewfunc.cxx b/sc/source/ui/view/viewfunc.cxx index 77b584dfbab7..30c0dce7d68f 100644 --- a/sc/source/ui/view/viewfunc.cxx +++ b/sc/source/ui/view/viewfunc.cxx @@ -852,7 +852,8 @@ void ScViewFunc::EnterData( SCCOL nCol, SCROW nRow, SCTAB nTab, if (GetViewData().GetSheetViewID() == sc::DefaultSheetViewID) { SCROW nUnsortedRow = nRow; - nUnsortedRow = pManager->unsort(nUnsortedRow); + if (pManager->getSortOrder()) + nUnsortedRow = pManager->getSortOrder()->unsort(nUnsortedRow); applyText(*this, nCol, nUnsortedRow, nSheetViewTab, rString, bNumFmtChanged); } else if (GetViewData().GetSheetViewID() == pSheetView->getID()) @@ -871,7 +872,11 @@ void ScViewFunc::EnterData( SCCOL nCol, SCROW nRow, SCTAB nTab, if (nSheetViewID != sc::DefaultSheetViewID) { auto pSheetView = pManager->get(nSheetViewID); - nUnsortedRow = pSheetView->unsort(nUnsortedRow); + + if (pSheetView->getSortOrder()) + nUnsortedRow = pSheetView->getSortOrder()->unsort(nUnsortedRow); + if (pManager->getSortOrder()) + nUnsortedRow = pManager->getSortOrder()->unsort(nUnsortedRow); } applyText(*this, nCol, nUnsortedRow, rTab, rString, bNumFmtChanged); }
