sc/qa/unit/data/ods/tdf167450_source.ods |binary
 sc/qa/unit/data/ods/tdf167450_target.ods |binary
 sc/qa/unit/scshapetest.cxx               |   51 +++++++++++++++++++++++++++----
 sc/source/core/data/documen9.cxx         |    9 +++++
 4 files changed, 55 insertions(+), 5 deletions(-)

New commits:
commit 054b38fd2eecc3279f117d34bd976f5dfc1a3883
Author:     Regina Henschel <rb.hensc...@t-online.de>
AuthorDate: Sun Jul 13 15:22:17 2025 +0200
Commit:     Xisco Fauli <xiscofa...@libreoffice.org>
CommitDate: Tue Jul 15 17:42:13 2025 +0200

    tdf#167450 adapt sheet index in anchor to new pos
    
    When a sheet is copied to another document, the tab index might change.
    The tab index was not adapted in the anchor info in the shape, that are
    maStart and maEnd in m_pPlusData in SdrObject. That resulted in a wrong
    shape position after save and reload.
    
    Change-Id: If69dcc9067e1346e1e115adf3c5ca68a9423e250
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/187802
    Tested-by: Jenkins
    Reviewed-by: Regina Henschel <rb.hensc...@t-online.de>
    (cherry picked from commit 704dd0d279af6c1ace1bde61fec93e7c3a8d1035)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/187924
    Reviewed-by: Xisco Fauli <xiscofa...@libreoffice.org>

diff --git a/sc/qa/unit/data/ods/tdf167450_source.ods 
b/sc/qa/unit/data/ods/tdf167450_source.ods
new file mode 100644
index 000000000000..4d04486cd63d
Binary files /dev/null and b/sc/qa/unit/data/ods/tdf167450_source.ods differ
diff --git a/sc/qa/unit/data/ods/tdf167450_target.ods 
b/sc/qa/unit/data/ods/tdf167450_target.ods
new file mode 100644
index 000000000000..d82e2c9a6623
Binary files /dev/null and b/sc/qa/unit/data/ods/tdf167450_target.ods differ
diff --git a/sc/qa/unit/scshapetest.cxx b/sc/qa/unit/scshapetest.cxx
index d6d018f4ab60..fbea25178581 100644
--- a/sc/qa/unit/scshapetest.cxx
+++ b/sc/qa/unit/scshapetest.cxx
@@ -50,18 +50,26 @@ static SdrPage* lcl_getSdrPageWithAssert(ScDocument& rDoc)
     return pPage;
 }
 
-static SdrObject* lcl_getSdrObjectWithAssert(ScDocument& rDoc, sal_uInt16 
nObjNumber)
+static SdrObject* lcl_getSdrObjectWithAssert(ScDocument& rDoc, sal_uInt16 
nObjNumber,
+                                             sal_uInt16 nPageIndex)
 {
     ScDrawLayer* pDrawLayer = rDoc.GetDrawLayer();
     CPPUNIT_ASSERT_MESSAGE("No ScDrawLayer", pDrawLayer);
-    const SdrPage* pPage = pDrawLayer->GetPage(0);
-    CPPUNIT_ASSERT_MESSAGE("No draw page", pPage);
+    const SdrPage* pPage = pDrawLayer->GetPage(nPageIndex);
+    OString sMsg1 = "No draw page " + OString::number(nPageIndex);
+    CPPUNIT_ASSERT_MESSAGE(sMsg1.getStr(), pPage);
     SdrObject* pObj = pPage->GetObj(nObjNumber);
-    OString sMsg = "no Object " + OString::number(nObjNumber);
-    CPPUNIT_ASSERT_MESSAGE(sMsg.getStr(), pObj);
+    OString sMsg2
+        = "No Object " + OString::number(nObjNumber) + " on page " + 
OString::number(nPageIndex);
+    CPPUNIT_ASSERT_MESSAGE(sMsg2.getStr(), pObj);
     return pObj;
 }
 
+static SdrObject* lcl_getSdrObjectWithAssert(ScDocument& rDoc, sal_uInt16 
nObjNumber)
+{
+    return lcl_getSdrObjectWithAssert(rDoc, nObjNumber, 0);
+}
+
 static SdrObject* lcl_getSdrObjectbyName(ScDocument& rDoc, std::u16string_view 
rName)
 {
     ScDrawLayer* pDrawLayer = rDoc.GetDrawLayer();
@@ -1332,6 +1340,39 @@ CPPUNIT_TEST_FIXTURE(ScShapeTest, 
testTdf160329_sortWithHiddenRows)
     CPPUNIT_ASSERT_POINT_EQUAL_WITH_TOLERANCE(Point(2600, 4399), aPos, 1);
 }
 
+CPPUNIT_TEST_FIXTURE(ScShapeTest, testTdf167450_copySheet)
+{
+    // Copy a sheet that contains an image from one document to another. Make 
sure the image has the
+    // correct position after save and reload of the target document.
+
+    // Open source and target document
+    mxComponent = loadFromDesktop(createFileURL(u"ods/tdf167450_target.ods"));
+    ScDocument* pDocTarget = getScDoc();
+    mxComponent2 = loadFromDesktop(createFileURL(u"ods/tdf167450_source.ods"));
+    ScDocument* pDocSource = getScDoc2();
+
+    // Copy sheet to target document. 32767 means 'to end position'
+    uno::Sequence<beans::PropertyValue> aArgs
+        = { comphelper::makePropertyValue(u"DocName"_ustr, 
u"tdf167450_target"_ustr),
+            comphelper::makePropertyValue(u"Index"_ustr, sal_Int32(32767)),
+            comphelper::makePropertyValue(u"Copy"_ustr, true) };
+    dispatchCommand(mxComponent2, u".uno:Move"_ustr, aArgs);
+
+    // Get object in source document. It is on first sheet.
+    SdrObject* pObjSource = lcl_getSdrObjectWithAssert(*pDocSource, 0, 0);
+    tools::Rectangle aRectSource = pObjSource->GetLogicRect();
+
+    // verify sheet inclusive image is copied
+    SdrObject* pObjTarget = lcl_getSdrObjectWithAssert(*pDocTarget, 0, 1);
+    CPPUNIT_ASSERT_RECTANGLE_EQUAL_WITH_TOLERANCE(aRectSource, 
pObjTarget->GetLogicRect(), 1);
+
+    // Compare positions after save and reload. Without fix the position was 
wrong after reload.
+    saveAndReload(u"calc8"_ustr);
+    pDocTarget = getScDoc();
+    pObjTarget = lcl_getSdrObjectWithAssert(*pDocTarget, 0, 1);
+    CPPUNIT_ASSERT_RECTANGLE_EQUAL_WITH_TOLERANCE(aRectSource, 
pObjTarget->GetLogicRect(), 1);
+}
+
 CPPUNIT_PLUGIN_IMPLEMENT();
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/core/data/documen9.cxx b/sc/source/core/data/documen9.cxx
index 8c537b4c984a..23a7baa90d9a 100644
--- a/sc/source/core/data/documen9.cxx
+++ b/sc/source/core/data/documen9.cxx
@@ -50,6 +50,7 @@
 #include <charthelper.hxx>
 #include <conditio.hxx>
 #include <documentlinkmgr.hxx>
+#include <userdat.hxx>
 
 using namespace ::com::sun::star;
 
@@ -88,6 +89,14 @@ void ScDocument::TransferDrawPage(const ScDocument& rSrcDoc, 
SCTAB nSrcPos, SCTA
                 pNewObject->NbcMove(Size(0,0));
                 pNewPage->InsertObject( pNewObject.get() );
 
+                // tdf#167450 adapt anchor tab
+                ScDrawObjData* pNewData = 
ScDrawLayer::GetObjData(pNewObject.get());
+                if (pNewData)
+                {
+                    pNewData->maStart.SetTab(nDestPos);
+                    pNewData->maEnd.SetTab(nDestPos);
+                }
+
                 if (mpDrawLayer->IsRecording())
                     mpDrawLayer->AddCalcUndo( 
std::make_unique<SdrUndoInsertObj>( *pNewObject ) );
 

Reply via email to