diff --git a/sc/inc/document.hxx b/sc/inc/document.hxx index 97499dd665bc..495c86037525 100644 --- a/sc/inc/document.hxx +++ b/sc/inc/document.hxx @@ -2458,6 +2458,14 @@ public: /** Returns the sheet view ID for a given tab. */ SC_DLLPUBLIC sc::SheetViewID GetTableSheetViewID(SCTAB nTab) const; + /** Synchronizes all sheet views for the given default view table. + * + * Specifically, it goes through all sheet views and overwrites the + * content of the sheet view table with the content of default view table, + * then reapplies the sheet views's sorting and filtering for the auto-filter. + */ + void SyncSheetViews(SCTAB nDefaultViewTable); + private: ScDocument(const ScDocument& r) = delete; diff --git a/sc/source/core/data/document10.cxx b/sc/source/core/data/document10.cxx index 4867583018f2..d64d3c3a710a 100644 --- a/sc/source/core/data/document10.cxx +++ b/sc/source/core/data/document10.cxx @@ -28,6 +28,10 @@ #include #include #include +#include +#include +#include +#include #include #include @@ -1216,4 +1220,54 @@ SCTAB ScDocument::GetSheetViewNumber(SCTAB nTab, sc::SheetViewID nID) return -1; } +void ScDocument::SyncSheetViews(SCTAB nDefaultViewTable) +{ + std::shared_ptr pManager = GetSheetViewManager(nDefaultViewTable); + if (!pManager || pManager->isEmpty()) + return; + + // Iterate all valid sheet views + for (auto& rSheetView : pManager->iterateValidSheetViews()) + { + SCTAB nSheetViewTab = rSheetView.getTableNumber(); + // Get the current query (filter) state of the auto-filter + std::optional oQueryParam; + ScDBData* pNoNameData = GetAnonymousDBData(nSheetViewTab); + if (pNoNameData && pNoNameData->HasAutoFilter()) + { + if (pNoNameData->HasQueryParam()) + { + oQueryParam.emplace(); + pNoNameData->GetQueryParam(*oQueryParam); + } + } + + // Overwrite the content from the default view table + OverwriteContent(nDefaultViewTable, nSheetViewTab); + + // Revert the sorting of the default view table. + // It can happen that the default view was sorted after the sheet view + // was created. + if (auto const& rReorderParameters = rSheetView.getReorderParameters()) + { + sc::ReorderParam aReorderParameters(*rReorderParameters); + aReorderParameters.maSortRange.aStart.SetTab(nSheetViewTab); + aReorderParameters.maSortRange.aEnd.SetTab(nSheetViewTab); + aReorderParameters.reverse(); + Reorder(aReorderParameters); + } + + // Sort the sheet view if it was sorted before + auto const& oSortParam = rSheetView.getSortParam(); + if (oSortParam) + { + rSheetView.resetSortOrder(); + Sort(nSheetViewTab, *oSortParam, false, false, nullptr, nullptr); + } + // Apply the stored query state (if available) + if (oQueryParam) + Query(nSheetViewTab, *oQueryParam, false, false); + } +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/source/ui/inc/undobase.hxx b/sc/source/ui/inc/undobase.hxx index 08c76bfb27cb..5798f8d2f5f8 100644 --- a/sc/source/ui/inc/undobase.hxx +++ b/sc/source/ui/inc/undobase.hxx @@ -61,6 +61,8 @@ protected: void BeginRedo(); void EndRedo(); + void SyncSheetViews(); + void BroadcastChanges( const ScRange& rRange ); /** diff --git a/sc/source/ui/operation/Operation.cxx b/sc/source/ui/operation/Operation.cxx index 6524c9d056a6..00b68b7d9869 100644 --- a/sc/source/ui/operation/Operation.cxx +++ b/sc/source/ui/operation/Operation.cxx @@ -10,18 +10,15 @@ #include #include #include +#include +#include #include #include -#include #include #include #include #include -#include -#include -#include - namespace sc { namespace @@ -196,49 +193,7 @@ void Operation::syncSheetViews() auto& rDocument = mpViewData->GetDocument(); SCTAB nTab = mpViewData->GetDefaultViewTab(); - - std::shared_ptr pManager = rDocument.GetSheetViewManager(nTab); - if (!pManager || pManager->isEmpty()) - return; - - for (auto& rSheetView : pManager->iterateValidSheetViews()) - { - SCTAB nSheetViewTab = rSheetView.getTableNumber(); - - std::optional oQueryParam; - ScDBData* pNoNameData = rDocument.GetAnonymousDBData(nSheetViewTab); - if (pNoNameData && pNoNameData->HasAutoFilter()) - { - if (pNoNameData->HasQueryParam()) - { - oQueryParam.emplace(); - pNoNameData->GetQueryParam(*oQueryParam); - } - } - - rDocument.OverwriteContent(nTab, nSheetViewTab); - - // Reverse the sorting of the default view in the sheet view - if (auto const& rReorderParameters = rSheetView.getReorderParameters()) - { - sc::ReorderParam aReorderParameters(*rReorderParameters); - aReorderParameters.maSortRange.aStart.SetTab(nSheetViewTab); - aReorderParameters.maSortRange.aEnd.SetTab(nSheetViewTab); - aReorderParameters.reverse(); - rDocument.Reorder(aReorderParameters); - } - - auto const& oSortParam = rSheetView.getSortParam(); - if (oSortParam) - { - // We need to reset the sort order for the sheet view as we will sort again - rSheetView.resetSortOrder(); - rDocument.Sort(nSheetViewTab, *oSortParam, false, false, nullptr, nullptr); - } - - if (oQueryParam) - rDocument.Query(nSheetViewTab, *oQueryParam, false, false); - } + rDocument.SyncSheetViews(nTab); } bool Operation::isInputOnSheetView() const { return getCurrentSheetView(mpViewData) != nullptr; } diff --git a/sc/source/ui/undo/undobase.cxx b/sc/source/ui/undo/undobase.cxx index b2e841d54372..be4b1f8e796b 100644 --- a/sc/source/ui/undo/undobase.cxx +++ b/sc/source/ui/undo/undobase.cxx @@ -124,6 +124,9 @@ namespace void ScSimpleUndo::EndUndo() { + // After undo we need to restore the sheet views as well. + SyncSheetViews(); + { // rhbz#1352881 Temporarily turn off undo generation during // SetDocumentModified @@ -156,6 +159,9 @@ void ScSimpleUndo::EndRedo() if (pDetectiveUndo) pDetectiveUndo->Redo(); + // After redo we need to restore the sheet views as well. + SyncSheetViews(); + { // rhbz#1352881 Temporarily turn off undo generation during // SetDocumentModified @@ -174,6 +180,17 @@ void ScSimpleUndo::EndRedo() rDocShell.SetInUndo( false ); } +void ScSimpleUndo::SyncSheetViews() +{ + ScViewData* pViewData = ScDocShell::GetViewData(); + if (!pViewData) + return; + + ScDocument& rDocument = rDocShell.GetDocument(); + SCTAB nDefaultViewTable = rDocument.GetDefaultViewTableNumber(pViewData->GetTabNumber()); + rDocument.SyncSheetViews(nDefaultViewTable); +} + void ScSimpleUndo::BroadcastChanges( const ScRange& rRange ) { ScDocument& rDoc = rDocShell.GetDocument();