sc/inc/conditio.hxx               |    7 ++++++-
 sc/source/core/data/table2.cxx    |    6 +++---
 sc/source/ui/docshell/docfunc.cxx |    4 ++--
 sc/source/ui/undo/undoblk.cxx     |   19 +++++++------------
 4 files changed, 18 insertions(+), 18 deletions(-)

New commits:
commit cd3332dd1dd8e951bdd8be22d78a4681e1d4b6c7
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Wed Jan 22 13:20:23 2025 +0500
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Wed Jan 22 13:11:55 2025 +0100

    Introduce ScConditionalFormatList::Clone and use it to simplify
    
    Potentialy could also return a unique_ptr, would need to change the
    methods where the result is passed.
    
    Change-Id: I9cbd876e446094b93e2d6f72b16aefe58327e7d2
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/180572
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/sc/inc/conditio.hxx b/sc/inc/conditio.hxx
index fe708bc6e356..2e3ea5639f96 100644
--- a/sc/inc/conditio.hxx
+++ b/sc/inc/conditio.hxx
@@ -677,12 +677,17 @@ private:
                 CompareScConditionalFormat> ConditionalFormatContainer;
     ConditionalFormatContainer m_ConditionalFormats;
 
+    ScConditionalFormatList(ScDocument& rDoc, const ScConditionalFormatList& 
rList);
     void operator =(ScConditionalFormatList const &) = delete;
 
 public:
     ScConditionalFormatList() {}
     SC_DLLPUBLIC ScConditionalFormatList(const ScConditionalFormatList& rList);
-    ScConditionalFormatList(ScDocument& rDoc, const ScConditionalFormatList& 
rList);
+
+    ScConditionalFormatList* Clone(ScDocument& rDestDoc) const
+    {
+        return new ScConditionalFormatList(rDestDoc, *this);
+    }
 
     void    InsertNew( std::unique_ptr<ScConditionalFormat> pNew );
 
diff --git a/sc/source/core/data/table2.cxx b/sc/source/core/data/table2.cxx
index c4e8f0cfa11e..1afd60752ab2 100644
--- a/sc/source/core/data/table2.cxx
+++ b/sc/source/core/data/table2.cxx
@@ -512,7 +512,7 @@ void ScTable::CopyToClip(
 
     mpCondFormatList->startRendering();
     mpCondFormatList->updateValues();
-    pTable->mpCondFormatList.reset(new 
ScConditionalFormatList(pTable->rDocument, *mpCondFormatList));
+    pTable->mpCondFormatList.reset(mpCondFormatList->Clone(pTable->rDocument));
     mpCondFormatList->endRendering();
 }
 
@@ -1344,7 +1344,7 @@ void ScTable::CopyToTable(
 
     if (bToUndoDoc && (nFlags & InsertDeleteFlags::ATTRIB))
     {
-        pDestTab->mpCondFormatList.reset(new 
ScConditionalFormatList(pDestTab->rDocument, *mpCondFormatList));
+        
pDestTab->mpCondFormatList.reset(mpCondFormatList->Clone(pDestTab->rDocument));
     }
 
     if (pDBDataNoName)
@@ -1523,7 +1523,7 @@ void ScTable::UndoToTable(
     }
 
     if (nFlags & InsertDeleteFlags::ATTRIB)
-        pDestTab->mpCondFormatList.reset(new 
ScConditionalFormatList(pDestTab->rDocument, *mpCondFormatList));
+        
pDestTab->mpCondFormatList.reset(mpCondFormatList->Clone(pDestTab->rDocument));
 
     if (!(bWidth||bHeight))
         return;
diff --git a/sc/source/ui/docshell/docfunc.cxx 
b/sc/source/ui/docshell/docfunc.cxx
index e745b922315e..9b5413cd1c17 100644
--- a/sc/source/ui/docshell/docfunc.cxx
+++ b/sc/source/ui/docshell/docfunc.cxx
@@ -5703,7 +5703,7 @@ void ScDocFunc::SetConditionalFormatList( 
ScConditionalFormatList* pList, SCTAB
         ScConditionalFormatList* pOld = rDoc.GetCondFormList(nTab);
 
         if (pOld)
-            pUndoDoc->SetCondFormList(new ScConditionalFormatList(*pUndoDoc, 
*pOld), nTab);
+            pUndoDoc->SetCondFormList(pOld->Clone(*pUndoDoc), nTab);
         else
             pUndoDoc->SetCondFormList(nullptr, nTab);
 
@@ -5723,7 +5723,7 @@ void ScDocFunc::SetConditionalFormatList( 
ScConditionalFormatList* pList, SCTAB
     {
         ScDocumentUniquePtr pRedoDoc(new ScDocument(SCDOCMODE_UNDO));
         pRedoDoc->InitUndo( rDoc, nTab, nTab );
-        pRedoDoc->SetCondFormList(new ScConditionalFormatList(*pRedoDoc, 
*pList), nTab);
+        pRedoDoc->SetCondFormList(pList->Clone(*pRedoDoc), nTab);
 
         rDocShell.GetUndoManager()->AddUndoAction(
                 std::make_unique<ScUndoConditionalFormatList>(&rDocShell, 
std::move(pUndoDoc), std::move(pRedoDoc), nTab));
diff --git a/sc/source/ui/undo/undoblk.cxx b/sc/source/ui/undo/undoblk.cxx
index b70394179524..f5b3124c8795 100644
--- a/sc/source/ui/undo/undoblk.cxx
+++ b/sc/source/ui/undo/undoblk.cxx
@@ -1626,7 +1626,7 @@ ScDocumentUniquePtr 
ScUndoConditionalFormat::createUndoRedoData()
     ScDocumentUniquePtr pUndoRedoDoc(new ScDocument(SCDOCMODE_UNDO));
     pUndoRedoDoc->InitUndo(rDoc, mnTab, mnTab);
     if (const auto* pList = rDoc.GetCondFormList(mnTab))
-        pUndoRedoDoc->SetCondFormList(new 
ScConditionalFormatList(*pUndoRedoDoc, *pList), mnTab);
+        pUndoRedoDoc->SetCondFormList(pList->Clone(*pUndoRedoDoc), mnTab);
     return pUndoRedoDoc;
 }
 
@@ -1670,7 +1670,7 @@ void ScUndoConditionalFormat::DoChange(ScDocument* 
pSrcDoc)
             for (const auto& range : cond->GetRange())
                 aCombinedRange.Join(range);
         }
-        rDoc.SetCondFormList(new ScConditionalFormatList(rDoc, *pNewList), 
mnTab);
+        rDoc.SetCondFormList(pNewList->Clone(rDoc), mnTab);
     }
     else
     {
@@ -1734,7 +1734,7 @@ void ScUndoConditionalFormatList::DoChange(const 
ScDocument* pSrcDoc)
         mpUndoDoc->GetCondFormList(mnTab)->RemoveFromDocument(rDoc);
         mpRedoDoc->GetCondFormList(mnTab)->AddToDocument(rDoc);
     }
-    rDoc.SetCondFormList(new ScConditionalFormatList(rDoc, 
*pSrcDoc->GetCondFormList(mnTab)), mnTab);
+    rDoc.SetCondFormList(pSrcDoc->GetCondFormList(mnTab)->Clone(rDoc), mnTab);
 
     pDocShell->PostPaintGridAll();
     pDocShell->PostDataChanged();
commit 7a51400b327b94929ff51035196253341ccf9036
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Wed Jan 22 15:08:29 2025 +0500
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Wed Jan 22 13:11:47 2025 +0100

    tdf#164785: make sure to not modify CF list in undo/redo
    
    Commit 21830c58f27977ca4a1dbacd2f41583c9f3ae775 added a call to
    CopyToDocument to CF undo/redo. But that could modify the CF list
    (calling its DeleteArea method, and tab's CopyConditionalFormat).
    This change prevents that by using document's AddCondFormatData
    instead of CopyToDocument.
    
    Change-Id: Ic59a6724b5cc70ef16ec1ef3b3e2674cb3cc83f1
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/180570
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/sc/source/ui/undo/undoblk.cxx b/sc/source/ui/undo/undoblk.cxx
index fbe1d4557213..b70394179524 100644
--- a/sc/source/ui/undo/undoblk.cxx
+++ b/sc/source/ui/undo/undoblk.cxx
@@ -1626,12 +1626,7 @@ ScDocumentUniquePtr 
ScUndoConditionalFormat::createUndoRedoData()
     ScDocumentUniquePtr pUndoRedoDoc(new ScDocument(SCDOCMODE_UNDO));
     pUndoRedoDoc->InitUndo(rDoc, mnTab, mnTab);
     if (const auto* pList = rDoc.GetCondFormList(mnTab))
-    {
         pUndoRedoDoc->SetCondFormList(new 
ScConditionalFormatList(*pUndoRedoDoc, *pList), mnTab);
-        // Save CF keys in cells' attributes
-        for (const auto& range : pList->GetCombinedRange())
-            rDoc.CopyToDocument(range, InsertDeleteFlags::ATTRIB, false, 
*pUndoRedoDoc);
-    }
     return pUndoRedoDoc;
 }
 
@@ -1668,14 +1663,14 @@ void ScUndoConditionalFormat::DoChange(ScDocument* 
pSrcDoc)
 
     if (const auto* pNewList = pSrcDoc->GetCondFormList(mnTab))
     {
-        ScRangeList aCombinedRange2 = pNewList->GetCombinedRange();
-        rDoc.SetCondFormList(new ScConditionalFormatList(rDoc, *pNewList), 
mnTab);
-        for (const auto& range : aCombinedRange2)
+        for (const auto& cond : *pNewList)
         {
-            aCombinedRange.Join(range);
             // Restore the CF keys to cell attributes
-            pSrcDoc->CopyToDocument(range, InsertDeleteFlags::ATTRIB, false, 
rDoc);
+            rDoc.AddCondFormatData(cond->GetRange(), mnTab, cond->GetKey());
+            for (const auto& range : cond->GetRange())
+                aCombinedRange.Join(range);
         }
+        rDoc.SetCondFormList(new ScConditionalFormatList(rDoc, *pNewList), 
mnTab);
     }
     else
     {

Reply via email to