sc/inc/SheetViewManager.hxx | 7 + sc/inc/sortparam.hxx | 21 ++-- sc/qa/unit/tiledrendering/SheetViewTest.cxx | 134 +++++++++++++++++++++------- sc/source/core/data/SheetViewManager.cxx | 36 +++++++ sc/source/core/data/sortparam.cxx | 24 +---- sc/source/core/data/table3.cxx | 7 + sc/source/ui/dbgui/tpsort.cxx | 3 sc/source/ui/view/viewfunc.cxx | 3 8 files changed, 177 insertions(+), 58 deletions(-)
New commits: commit 0b42a7dbd04fc6f1a1e92d6ea837c084cdd32792 Author: Tomaž Vajngerl <[email protected]> AuthorDate: Wed Oct 29 10:41:31 2025 +0900 Commit: Tomaž Vajngerl <[email protected]> CommitDate: Mon Feb 23 13:01:08 2026 +0100 sc: simplify ScSortKeyState Use the default values so it's not needed to set them, just set what is different from the default. Document the struct. Change-Id: I7365080b006bc1959e890b21346fee847e382d25 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/193290 Tested-by: Jenkins Reviewed-by: Tomaž Vajngerl <[email protected]> Reviewed-on: https://gerrit.libreoffice.org/c/core/+/200012 Tested-by: Jenkins CollaboraOffice <[email protected]> diff --git a/sc/inc/sortparam.hxx b/sc/inc/sortparam.hxx index 3f266984bd47..0de0e3bbdb1b 100644 --- a/sc/inc/sortparam.hxx +++ b/sc/inc/sortparam.hxx @@ -37,19 +37,27 @@ struct ScQueryParam; class SdrObject; class ScPostIt; +/** Sort by which color */ enum class ScColorSortMode { None, TextColor, BackgroundColor }; +/** Sort key state defines one way how to sort the range. + * + * A range of values can be sorted in multiple way, each column a different way. + * + * For example: sort column A ascending and if the column when there are same values, define that those should be + * sorted descending usign the column C. + **/ struct ScSortKeyState { - SCCOLROW nField; - bool bDoSort; - bool bAscending; - ScColorSortMode aColorSortMode; - Color aColorSortColor; + SCCOLROW nField = 0; + bool bDoSort = false; + bool bAscending = true; + ScColorSortMode aColorSortMode = ScColorSortMode::None; + Color aColorSortColor; }; /** Struct to hold non-data extended area, used with @@ -144,8 +152,7 @@ struct SC_DLLPUBLIC ScSortParam SCTAB nDestTab; SCCOL nDestCol; SCROW nDestRow; - ::std::vector<ScSortKeyState> - maKeyState; + std::vector<ScSortKeyState> maKeyState; css::lang::Locale aCollatorLocale; OUString aCollatorAlgorithm; sal_uInt16 nCompatHeader; diff --git a/sc/source/core/data/sortparam.cxx b/sc/source/core/data/sortparam.cxx index 9e583bbeb5d2..9b6f483192e1 100644 --- a/sc/source/core/data/sortparam.cxx +++ b/sc/source/core/data/sortparam.cxx @@ -52,8 +52,6 @@ ScSortParam::~ScSortParam() {} void ScSortParam::Clear() { - ScSortKeyState aKeyState; - nCol1=nCol2=nDestCol = 0; nRow1=nRow2=nDestRow = 0; nSourceTab = 0; @@ -69,13 +67,8 @@ void ScSortParam::Clear() aCollatorLocale = css::lang::Locale(); aCollatorAlgorithm.clear(); - aKeyState.bDoSort = false; - aKeyState.nField = 0; - aKeyState.bAscending = true; - aKeyState.aColorSortMode = ScColorSortMode::None; - // Initialize to default size - maKeyState.assign( DEFSORT, aKeyState ); + maKeyState.assign(DEFSORT, {}); } ScSortParam& ScSortParam::operator=( const ScSortParam& r ) @@ -184,7 +177,6 @@ ScSortParam::ScSortParam( const ScSubTotalParam& rSub, const ScSortParam& rOld ) key.bDoSort = true; key.nField = group.nField; key.bAscending = rSub.bAscending; - key.aColorSortMode = ScColorSortMode::None; maKeyState.push_back(key); } @@ -203,7 +195,6 @@ ScSortParam::ScSortParam( const ScSubTotalParam& rSub, const ScSortParam& rOld ) key.bDoSort = true; key.nField = nThisField; key.bAscending = rOld.maKeyState[i].bAscending; - key.aColorSortMode = ScColorSortMode::None; maKeyState.push_back(key); } } @@ -224,18 +215,13 @@ ScSortParam::ScSortParam( const ScQueryParam& rParam, SCCOL nCol ) : ScSortKeyState aKeyState; aKeyState.bDoSort = true; - aKeyState.nField = nCol; aKeyState.bAscending = true; - aKeyState.aColorSortMode = ScColorSortMode::None; - - maKeyState.push_back( aKeyState ); + aKeyState.nField = nCol; + maKeyState.push_back(aKeyState); // Set the rest - aKeyState.bDoSort = false; - aKeyState.nField = 0; - - for (sal_uInt16 i=1; i<GetSortKeyCount(); i++) - maKeyState.push_back( aKeyState ); + for (sal_uInt16 i = 1; i < GetSortKeyCount(); i++) + maKeyState.emplace_back(); } void ScSortParam::MoveToDest() diff --git a/sc/source/ui/dbgui/tpsort.cxx b/sc/source/ui/dbgui/tpsort.cxx index caafc07b800f..3c7ac5387ff1 100644 --- a/sc/source/ui/dbgui/tpsort.cxx +++ b/sc/source/ui/dbgui/tpsort.cxx @@ -392,8 +392,7 @@ sal_uInt16 ScTabPageSortFields::GetFieldSelPos( SCCOLROW nField ) void ScTabPageSortFields::SetLastSortKey( sal_uInt16 nItem ) { // Extend local SortParam copy - const ScSortKeyState atempKeyState = { 0, false, true, ScColorSortMode::None, Color() }; - aSortData.maKeyState.push_back( atempKeyState ); + aSortData.maKeyState.emplace_back(); // insert default key state // Add Sort Key Item ++nSortKeyCount; commit 8c803e0dd13a29f08dbd062ca16a5719783ef97c Author: Tomaž Vajngerl <[email protected]> AuthorDate: Thu Sep 4 14:05:08 2025 +0200 Commit: Tomaž Vajngerl <[email protected]> CommitDate: Mon Feb 23 13:00:58 2026 +0100 sc: store sort order and reverse the sort for default view We store the sort order of the default view in SheetViewManager, so we can reverse the sort when the sheet view is changed. Tested scenarios are when the auto-filter is sorted in the default view, and the sheet view is changed. Additional scenario is when the auto-filter is sorted again (for example descending following with the ascending order) and the changing the sheet view should still work correctly. Change-Id: Ic42e8a51d6ce17db6b2df80bafb62f6f8c48a6a8 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/190599 Reviewed-by: Tomaž Vajngerl <[email protected]> Tested-by: Jenkins Reviewed-on: https://gerrit.libreoffice.org/c/core/+/200011 Tested-by: Jenkins CollaboraOffice <[email protected]> diff --git a/sc/inc/SheetViewManager.hxx b/sc/inc/SheetViewManager.hxx index dc2bf8677230..a0e6527b1d11 100644 --- a/sc/inc/SheetViewManager.hxx +++ b/sc/inc/SheetViewManager.hxx @@ -28,6 +28,10 @@ private: std::vector<std::shared_ptr<SheetView>> maViews; sal_Int32 maNameCounter = 0; + std::vector<SCCOLROW> maOrder; + SCROW mnFirstRow; + SCROW mnLastRow; + bool isValidSheetViewID(SheetViewID nID) const { return nID >= 0 && o3tl::make_unsigned(nID) < maViews.size(); @@ -65,6 +69,9 @@ public: void unsyncAllSheetViews(); static OUString defaultViewName(); + + void addOrderIndices(std::vector<SCCOLROW> const& rOrder, SCROW firstRow, SCROW lastRow); + SCROW unsort(SCROW nRow); }; } diff --git a/sc/qa/unit/tiledrendering/SheetViewTest.cxx b/sc/qa/unit/tiledrendering/SheetViewTest.cxx index f8ce5152718a..9a59c2733bab 100644 --- a/sc/qa/unit/tiledrendering/SheetViewTest.cxx +++ b/sc/qa/unit/tiledrendering/SheetViewTest.cxx @@ -23,6 +23,23 @@ using namespace css; class SheetViewTest : public ScTiledRenderingTest { +protected: + static bool checkValues(ScTabViewShell* pTabView, SCCOL nCol, SCROW nStartRow, SCROW nEndRow, + std::vector<std::u16string_view> const& rValues) + { + size_t nSize = nEndRow - nStartRow + 1; + if (nSize != rValues.size()) + return false; + + for (size_t nIndex = 0; nIndex < nSize; nIndex++) + { + OUString value = pTabView->GetCurrentString(nCol, nStartRow + nIndex); + if (value != rValues[nIndex]) + return false; + } + + return true; + } }; /** Check auto-filter sorting. @@ -54,20 +71,14 @@ CPPUNIT_TEST_FIXTURE(SheetViewTest, testSheetViewAutoFilter) Scheduler::ProcessEventsToIdle(); // Check AutoFilter values - CPPUNIT_ASSERT_EQUAL(u"4"_ustr, pTabView1->GetCurrentString(0, 1)); - CPPUNIT_ASSERT_EQUAL(u"5"_ustr, pTabView1->GetCurrentString(0, 2)); - CPPUNIT_ASSERT_EQUAL(u"3"_ustr, pTabView1->GetCurrentString(0, 3)); - CPPUNIT_ASSERT_EQUAL(u"7"_ustr, pTabView1->GetCurrentString(0, 4)); + CPPUNIT_ASSERT(checkValues(pTabView1, 0, 1, 4, { u"4", u"5", u"3", u"7" })); // Switch to view 2 SfxLokHelper::setView(aView2.getViewID()); Scheduler::ProcessEventsToIdle(); // Check auto-filter values - CPPUNIT_ASSERT_EQUAL(u"4"_ustr, pTabView2->GetCurrentString(0, 1)); - CPPUNIT_ASSERT_EQUAL(u"5"_ustr, pTabView2->GetCurrentString(0, 2)); - CPPUNIT_ASSERT_EQUAL(u"3"_ustr, pTabView2->GetCurrentString(0, 3)); - CPPUNIT_ASSERT_EQUAL(u"7"_ustr, pTabView2->GetCurrentString(0, 4)); + CPPUNIT_ASSERT(checkValues(pTabView2, 0, 1, 4, { u"4", u"5", u"3", u"7" })); // Check what sheet we currently have selected for view 1 & 2 CPPUNIT_ASSERT_EQUAL(SCTAB(0), pTabView1->GetViewData().GetTabNumber()); @@ -88,16 +99,10 @@ CPPUNIT_TEST_FIXTURE(SheetViewTest, testSheetViewAutoFilter) CPPUNIT_ASSERT_EQUAL(SCTAB(0), pTabView2->GetViewData().GetTabNumber()); // Check view 2 - sorted - CPPUNIT_ASSERT_EQUAL(u"7"_ustr, pTabView2->GetCurrentString(0, 1)); - CPPUNIT_ASSERT_EQUAL(u"5"_ustr, pTabView2->GetCurrentString(0, 2)); - CPPUNIT_ASSERT_EQUAL(u"4"_ustr, pTabView2->GetCurrentString(0, 3)); - CPPUNIT_ASSERT_EQUAL(u"3"_ustr, pTabView2->GetCurrentString(0, 4)); + CPPUNIT_ASSERT(checkValues(pTabView2, 0, 1, 4, { u"7", u"5", u"4", u"3" })); // Check view 1 - unsorted - CPPUNIT_ASSERT_EQUAL(u"4"_ustr, pTabView1->GetCurrentString(0, 1)); - CPPUNIT_ASSERT_EQUAL(u"5"_ustr, pTabView1->GetCurrentString(0, 2)); - CPPUNIT_ASSERT_EQUAL(u"3"_ustr, pTabView1->GetCurrentString(0, 3)); - CPPUNIT_ASSERT_EQUAL(u"7"_ustr, pTabView1->GetCurrentString(0, 4)); + CPPUNIT_ASSERT(checkValues(pTabView1, 0, 1, 4, { u"4", u"5", u"3", u"7" })); } CPPUNIT_TEST_FIXTURE(SheetViewTest, testSyncValuesBetweenMainSheetAndSheetView) @@ -213,15 +218,8 @@ CPPUNIT_TEST_FIXTURE(SheetViewTest, testRemoveSheetView) Scheduler::ProcessEventsToIdle(); // Check AutoFilter values for each view - CPPUNIT_ASSERT_EQUAL(u"4"_ustr, pTabView1->GetCurrentString(0, 1)); - CPPUNIT_ASSERT_EQUAL(u"5"_ustr, pTabView1->GetCurrentString(0, 2)); - CPPUNIT_ASSERT_EQUAL(u"3"_ustr, pTabView1->GetCurrentString(0, 3)); - CPPUNIT_ASSERT_EQUAL(u"7"_ustr, pTabView1->GetCurrentString(0, 4)); - - CPPUNIT_ASSERT_EQUAL(u"4"_ustr, pTabView2->GetCurrentString(0, 1)); - CPPUNIT_ASSERT_EQUAL(u"5"_ustr, pTabView2->GetCurrentString(0, 2)); - CPPUNIT_ASSERT_EQUAL(u"3"_ustr, pTabView2->GetCurrentString(0, 3)); - CPPUNIT_ASSERT_EQUAL(u"7"_ustr, pTabView2->GetCurrentString(0, 4)); + CPPUNIT_ASSERT(checkValues(pTabView1, 0, 1, 4, { u"4", u"5", u"3", u"7" })); + CPPUNIT_ASSERT(checkValues(pTabView2, 0, 1, 4, { u"4", u"5", u"3", u"7" })); // Switch to View2 SfxLokHelper::setView(aView2.getViewID()); @@ -232,10 +230,8 @@ CPPUNIT_TEST_FIXTURE(SheetViewTest, testRemoveSheetView) Scheduler::ProcessEventsToIdle(); // Check values are sorted for view 2 - CPPUNIT_ASSERT_EQUAL(u"7"_ustr, pTabView2->GetCurrentString(0, 1)); - CPPUNIT_ASSERT_EQUAL(u"5"_ustr, pTabView2->GetCurrentString(0, 2)); - CPPUNIT_ASSERT_EQUAL(u"4"_ustr, pTabView2->GetCurrentString(0, 3)); - CPPUNIT_ASSERT_EQUAL(u"3"_ustr, pTabView2->GetCurrentString(0, 4)); + CPPUNIT_ASSERT(checkValues(pTabView1, 0, 1, 4, { u"4", u"5", u"3", u"7" })); + CPPUNIT_ASSERT(checkValues(pTabView2, 0, 1, 4, { u"7", u"5", u"4", u"3" })); // Sheet view must be present auto pSheetViewManager = pDocument->GetSheetViewManager(0); @@ -731,6 +727,86 @@ CPPUNIT_TEST_FIXTURE(SheetViewTest, testRenderStateInSheetView) CPPUNIT_ASSERT_EQUAL("S;Default"_ostr, pModelObj->getViewRenderState()); } +CPPUNIT_TEST_FIXTURE(SheetViewTest, testSyncAfterSorting_DefaultViewSort) +{ + // Two related scenarios tested: + // 1. Auto-filter is sorted in the default view, then the data is changed in a sheet view. + // In this case the sheet view is unsorted and the default view is sorted, so the data + // in the default view needs to be first unsorted so the correct cell is changed. + // 2. Continuation of scenario 1, where the default view is sorted again (ascending then + // descending order). In this case the sort orders must be combined correctly, so the + // change in the sheet view would still change the correct cell in default view. + + // Create two views, and leave the second one current. + ScModelObj* pModelObj = createDoc("SheetView_AutoFilter.ods"); + pModelObj->initializeForTiledRendering(uno::Sequence<beans::PropertyValue>()); + + // Setup views + ScTestViewCallback aSheetView; + ScTabViewShell* pTabViewSheetView = aSheetView.getTabViewShell(); + + SfxLokHelper::createView(); + Scheduler::ProcessEventsToIdle(); + + ScTestViewCallback aDefaultView; + ScTabViewShell* pTabViewDefaultView = aDefaultView.getTabViewShell(); + + CPPUNIT_ASSERT(pTabViewSheetView != pTabViewDefaultView); + CPPUNIT_ASSERT(aSheetView.getViewID() != aDefaultView.getViewID()); + + // Switch to Sheet View and Create + { + SfxLokHelper::setView(aSheetView.getViewID()); + Scheduler::ProcessEventsToIdle(); + + CPPUNIT_ASSERT_EQUAL(SCTAB(0), pTabViewSheetView->GetViewData().GetTabNumber()); + CPPUNIT_ASSERT_EQUAL(SCTAB(0), pTabViewDefaultView->GetViewData().GetTabNumber()); + + dispatchCommand(mxComponent, u".uno:NewSheetView"_ustr, {}); + + CPPUNIT_ASSERT_EQUAL(SCTAB(0), pTabViewSheetView->GetViewData().GetTabNumber()); + CPPUNIT_ASSERT_EQUAL(SCTAB(0), pTabViewDefaultView->GetViewData().GetTabNumber()); + } + + // Switch to Default View + { + SfxLokHelper::setView(aDefaultView.getViewID()); + Scheduler::ProcessEventsToIdle(); + + CPPUNIT_ASSERT_EQUAL(SCTAB(0), pTabViewSheetView->GetViewData().GetTabNumber()); + CPPUNIT_ASSERT_EQUAL(SCTAB(0), pTabViewDefaultView->GetViewData().GetTabNumber()); + + // Sort AutoFilter ascending + dispatchCommand(mxComponent, u".uno:SortAscending"_ustr, {}); + + CPPUNIT_ASSERT_EQUAL(SCTAB(0), pTabViewSheetView->GetViewData().GetTabNumber()); + CPPUNIT_ASSERT_EQUAL(SCTAB(0), pTabViewDefaultView->GetViewData().GetTabNumber()); + + // Check values + CPPUNIT_ASSERT(checkValues(pTabViewDefaultView, 0, 1, 4, { u"3", u"4", u"5", u"7" })); + CPPUNIT_ASSERT(checkValues(pTabViewSheetView, 0, 1, 4, { u"4", u"5", u"3", u"7" })); + + typeCharsInCell(std::string("9"), 0, 1, pTabViewDefaultView, pModelObj); + + // Check values + CPPUNIT_ASSERT(checkValues(pTabViewDefaultView, 0, 1, 4, { u"9", u"4", u"5", u"7" })); + CPPUNIT_ASSERT(checkValues(pTabViewSheetView, 0, 1, 4, { u"4", u"5", u"9", u"7" })); + + // Sort AutoFilter AGAIN descending + dispatchCommand(mxComponent, u".uno:SortDescending"_ustr, {}); + + // Check values + CPPUNIT_ASSERT(checkValues(pTabViewDefaultView, 0, 1, 4, { u"9", u"7", u"5", u"4" })); + CPPUNIT_ASSERT(checkValues(pTabViewSheetView, 0, 1, 4, { u"4", u"5", u"9", u"7" })); + + typeCharsInCell(std::string("6"), 0, 3, pTabViewDefaultView, pModelObj); + + // Check values + CPPUNIT_ASSERT(checkValues(pTabViewDefaultView, 0, 1, 4, { u"9", u"7", u"6", u"4" })); + CPPUNIT_ASSERT(checkValues(pTabViewSheetView, 0, 1, 4, { u"4", u"6", u"9", u"7" })); + } +} + CPPUNIT_PLUGIN_IMPLEMENT(); /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/source/core/data/SheetViewManager.cxx b/sc/source/core/data/SheetViewManager.cxx index c2b95bf44d10..015bba79718e 100644 --- a/sc/source/core/data/SheetViewManager.cxx +++ b/sc/source/core/data/SheetViewManager.cxx @@ -134,6 +134,42 @@ OUString SheetViewManager::generateName() } OUString SheetViewManager::defaultViewName() { return ScResId(STR_SHEET_VIEW_DEFAULT_VIEW_NAME); } + +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; +} } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/source/core/data/table3.cxx b/sc/source/core/data/table3.cxx index a71d1c034ebf..b3ced669834c 100644 --- a/sc/source/core/data/table3.cxx +++ b/sc/source/core/data/table3.cxx @@ -64,6 +64,7 @@ #include <drwlayer.hxx> #include <queryevaluator.hxx> #include <scopetools.hxx> +#include <SheetViewManager.hxx> #include <svl/sharedstringpool.hxx> @@ -1802,6 +1803,12 @@ void ScTable::Sort( pUndo->maDataAreaExtras.mnStartRow = nRow1; pUndo->maOrderIndices = pArray->GetOrderIndices(); } + + bool bAutoFilter = GetDoc().HasAutoFilter(rSortParam.nCol1, nRow1, GetTab()); + if (bAutoFilter) + { + GetSheetViewManager()->addOrderIndices(pArray->GetOrderIndices(), nRow1, nLastRow); + } } } else diff --git a/sc/source/ui/view/viewfunc.cxx b/sc/source/ui/view/viewfunc.cxx index 86ad9add6256..3b61a0b18f0c 100644 --- a/sc/source/ui/view/viewfunc.cxx +++ b/sc/source/ui/view/viewfunc.cxx @@ -846,7 +846,8 @@ void ScViewFunc::EnterData( SCCOL nCol, SCROW nRow, SCTAB nTab, continue; SCTAB nSheetViewTab = pSheetView->getTableNumber(); - applyText(*this, nCol, nRow, nSheetViewTab, rString, bNumFmtChanged); + SCROW nUnsortedRow = pManager->unsort(nRow); + applyText(*this, nCol, nUnsortedRow, nSheetViewTab, rString, bNumFmtChanged); } } applyText(*this, nCol, nRow, rTab, rString, bNumFmtChanged);
