Rebased ref, commits from common ancestor:
commit 52e77ca54e1bff84c1c441d399b6a616863be186
Author:     Balazs Varga <[email protected]>
AuthorDate: Tue Sep 23 13:50:24 2025 +0200
Commit:     Andras Timar <[email protected]>
CommitDate: Tue Sep 30 14:22:12 2025 +0200

    tdf#167042 - sc cell comments texts should not be deleted
    
    but the note text should be remembered in maNoteData to be able
    to later reconstruct a caption from it.
    
    Originally the bPreserveData was introduced and set to true in commit:
    b8b657123cc508c906622d20669507628c93e104
    (tdf#104967 preserve isolated notes data in clipboard when closing document)
    The original bugdoc from tdf#104967 was checked with this patch and
    had no crash with it.
    
    Change-Id: I3b6c56e4828c11c2cab0351c95dd4bcb5762d86e
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/191396
    Tested-by: Jenkins
    Reviewed-by: Balazs Varga <[email protected]>
    (cherry picked from commit 01765ffe843953bbf7d1e2822512306d3776b3be)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/191461
    Reviewed-by: Xisco Fauli <[email protected]>
    Signed-off-by: Xisco Fauli <[email protected]>
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/191465

diff --git a/sc/qa/unit/tiledrendering/data/tdf167042.ods 
b/sc/qa/unit/tiledrendering/data/tdf167042.ods
new file mode 100644
index 000000000000..22e95e0597ef
Binary files /dev/null and b/sc/qa/unit/tiledrendering/data/tdf167042.ods differ
diff --git a/sc/qa/unit/tiledrendering/tiledrendering2.cxx 
b/sc/qa/unit/tiledrendering/tiledrendering2.cxx
index b9df067e40dc..e5d3cecaab45 100644
--- a/sc/qa/unit/tiledrendering/tiledrendering2.cxx
+++ b/sc/qa/unit/tiledrendering/tiledrendering2.cxx
@@ -21,6 +21,7 @@
 #include <docuno.hxx>
 #include <scmod.hxx>
 #include <tabvwsh.hxx>
+#include <postit.hxx>
 
 using namespace com::sun::star;
 
@@ -201,6 +202,56 @@ CPPUNIT_TEST_FIXTURE(ScTiledRenderingTest, 
testSplitPanesXLSX)
     assertXPath(pSheet, "/x:worksheet/x:sheetViews/x:sheetView/x:pane", 
"activePane", u"topRight");
 }
 
+CPPUNIT_TEST_FIXTURE(ScTiledRenderingTest, testTdf167042)
+{
+    ScModelObj* pModelObj = createDoc("tdf167042.ods");
+    ScDocument* pDoc = pModelObj->GetDocument();
+    ScTestViewCallback aView1;
+
+    uno::Sequence<beans::PropertyValue> aPropertyValues
+        = { comphelper::makePropertyValue("ToPoint", OUString("$A$1")) };
+    dispatchCommand(mxComponent, ".uno:GoToCell", aPropertyValues);
+
+    Point aPoint = aView1.m_aCellCursorBounds.Center();
+
+    aPropertyValues = { comphelper::makePropertyValue("ToPoint", 
OUString("$B$1")) };
+    dispatchCommand(mxComponent, ".uno:GoToCell", aPropertyValues);
+
+    // Check that we have the comment on A1
+    CPPUNIT_ASSERT_MESSAGE("There should be a note on A1", 
pDoc->HasNote(ScAddress(0, 0, 0)));
+    ScPostIt* pNote = pDoc->GetNote(ScAddress(0, 0, 0));
+    CPPUNIT_ASSERT(pNote);
+    CPPUNIT_ASSERT_EQUAL(u"test1"_ustr, pNote->GetText());
+
+    uno::Sequence aArgs{ comphelper::makePropertyValue(u"PersistentCopy"_ustr, 
false) };
+    dispatchCommand(mxComponent, u".uno:FormatPaintbrush"_ustr, aArgs);
+
+    pModelObj->postMouseEvent(LOK_MOUSEEVENT_MOUSEBUTTONDOWN, aPoint.getX(), 
aPoint.getY(), 1,
+                              MOUSE_LEFT, 0);
+    pModelObj->postMouseEvent(LOK_MOUSEEVENT_MOUSEBUTTONUP, aPoint.getX(), 
aPoint.getY(), 1,
+                              MOUSE_LEFT, 0);
+
+    // Check that FormatPaintbrush worked
+    vcl::Font aFont;
+    pDoc->GetPattern(0, 0, 0)->fillFontOnly(aFont);
+    CPPUNIT_ASSERT_EQUAL_MESSAGE("font should be bold A1", WEIGHT_BOLD, 
aFont.GetWeight());
+
+    // Check that we still have the comment on A1 after FormatPaintbrush
+    pNote = pDoc->GetNote(ScAddress(0, 0, 0));
+    CPPUNIT_ASSERT(pNote);
+    CPPUNIT_ASSERT_EQUAL(u"test1"_ustr, pNote->GetText());
+
+    dispatchCommand(mxComponent, u".uno:Undo"_ustr, {});
+
+    // Check that we still have the comment on A1 after Undo
+    pNote = pDoc->GetNote(ScAddress(0, 0, 0));
+    CPPUNIT_ASSERT(pNote);
+    // Without the fix in place, this test would have failed with
+    // - Expected : test1
+    // - Actual :
+    CPPUNIT_ASSERT_EQUAL(u"test1"_ustr, pNote->GetText());
+}
+
 CPPUNIT_PLUGIN_IMPLEMENT();
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/ui/undo/undoblk.cxx b/sc/source/ui/undo/undoblk.cxx
index 5b30ac8c470e..c44b9e7a1ebd 100644
--- a/sc/source/ui/undo/undoblk.cxx
+++ b/sc/source/ui/undo/undoblk.cxx
@@ -1021,9 +1021,13 @@ void ScUndoPaste::DoChange(bool bUndo)
     }
 
     sal_uInt16 nExtFlags = 0;
+
     pDocShell->UpdatePaintExt(nExtFlags, maBlockRanges.Combine());
 
-    rDoc.ForgetNoteCaptions(maBlockRanges, false);
+    // tdf#167042 - cell comments texts should not be deleted but
+    // the note text should be remembered in maNoteData to be able
+    // to later reconstruct a caption from it.
+    rDoc.ForgetNoteCaptions(maBlockRanges, true);
     aMarkData.MarkToMulti();
     rDoc.DeleteSelection(nUndoFlags, aMarkData, false); // no broadcasting here
     for (size_t i = 0, n = maBlockRanges.size(); i < n; ++i)

Reply via email to