tdf#151828 related: Make cut shape have the same name after paste
when possible
Fixes shape names without a number at the end of the name, having the
number 1 appended to the end of the name after cut and paste in the
same document.
When possible - because it might happen that a shape is inserted
after the cut operation and is given the same name as the cut shape
name. If so, when the shape is pasted into the same document it will
not have the same name as before it was cut.
Change-Id: I1d1ebc76d07d81da0286d1f0bb7d34ad973a43b8
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/142983
Tested-by: Jenkins
Reviewed-by: Xisco Fauli <xiscofauli@libreoffice.org>
Reviewed-by: Jim Raykowski <raykowj@gmail.com>
diff --git a/svx/source/svdraw/svdobj.cxx b/svx/source/svdraw/svdobj.cxx
index 877744f..34688e5 100644
--- a/svx/source/svdraw/svdobj.cxx
+++ b/svx/source/svdraw/svdobj.cxx
@@ -3094,6 +3094,7 @@ void SdrObject::MakeNameUnique()
{
if (GetName().isEmpty())
{
OUString aName;
if (const E3dScene* pE3dObj = DynCastE3dScene(this))
{
SdrObjList* pObjList = pE3dObj->GetSubList();
@@ -3101,11 +3102,12 @@ void SdrObject::MakeNameUnique()
{
SdrObject* pObj0 = pObjList->GetObj(0);
if (pObj0)
SetName(pObj0->TakeObjNameSingul());
aName = pObj0->TakeObjNameSingul();
}
}
else
SetName(TakeObjNameSingul());
aName = TakeObjNameSingul();
SetName(aName + " 1");
}
std::unordered_set<OUString> aNameSet;
@@ -3143,8 +3145,6 @@ void SdrObject::MakeNameUnique(std::unordered_set<OUString>& rNameSet)
while (nPos > 0 && rtl::isAsciiDigit(sName[--nPos]));
sRootName = o3tl::trim(sName.subView(0, nPos + 1));
}
else
sName += " 1";
for (sal_uInt32 n = 1; rNameSet.find(sName) != rNameSet.end(); n++)
sName = sRootName + " " + OUString::number(n);
diff --git a/sw/qa/extras/uiwriter/uiwriter6.cxx b/sw/qa/extras/uiwriter/uiwriter6.cxx
index eac32ec..2c0900f 100644
--- a/sw/qa/extras/uiwriter/uiwriter6.cxx
+++ b/sw/qa/extras/uiwriter/uiwriter6.cxx
@@ -2222,6 +2222,43 @@ CPPUNIT_TEST_FIXTURE(SwUiWriterTest6, testCaptionShape)
CPPUNIT_ASSERT_EQUAL(1, getShapes());
}
CPPUNIT_TEST_FIXTURE(SwUiWriterTest6, testTdf151828_Comment2)
{
createSwDoc();
// Add a basic shape to the document.
uno::Sequence<beans::PropertyValue> aArgs(
comphelper::InitPropertySequence({ { "KeyModifier", uno::Any(KEY_MOD1) } }));
dispatchCommand(mxComponent, ".uno:BasicShapes", aArgs);
Scheduler::ProcessEventsToIdle();
auto xBasicShape = getShape(1);
auto pObject = SdrObject::getSdrObjectFromXShape(xBasicShape);
CPPUNIT_ASSERT_EQUAL(1, getShapes());
// rename the shape name
pObject->SetName("Shape");
// cut and paste it
dispatchCommand(mxComponent, ".uno:Cut", {});
Scheduler::ProcessEventsToIdle();
CPPUNIT_ASSERT_EQUAL(0, getShapes());
dispatchCommand(mxComponent, ".uno:Paste", {});
Scheduler::ProcessEventsToIdle();
CPPUNIT_ASSERT_EQUAL(1, getShapes());
// it is required to get the shape objet again after paste
xBasicShape = getShape(1);
pObject = SdrObject::getSdrObjectFromXShape(xBasicShape);
// Without fix in place, the shape name was 'Shape 1' after paste.
CPPUNIT_ASSERT_EQUAL(OUString("Shape"), pObject->GetName());
}
CPPUNIT_PLUGIN_IMPLEMENT();
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */