tdf#54819 keep style & numbering at tracked deletion

and direct paragraph formattings after wholly deleted
paragraphs. Clean-up and extension of the previous
workaround, now with Undo.

See commit 22639148ae5400bac98f32a75d7431b857c80195
"change tracking: keep paragraph style after full deletion"
and commit 23c159d948af360baecfe243e1321987760916d7
"change tracking: don't copy page break". (Note: the first
fixed the numbering, the second reverted it accidentally.)

Change-Id: Ic97af3b171490b011523d1bae11f75aaf4baab54
Reviewed-on: https://gerrit.libreoffice.org/74299
Tested-by: Jenkins
Reviewed-by: László Németh <nemeth@numbertext.org>
diff --git a/sw/qa/extras/uiwriter/data2/tdf54819b.odt b/sw/qa/extras/uiwriter/data2/tdf54819b.odt
new file mode 100644
index 0000000..0fb7a6f
--- /dev/null
+++ b/sw/qa/extras/uiwriter/data2/tdf54819b.odt
Binary files differ
diff --git a/sw/qa/extras/uiwriter/uiwriter2.cxx b/sw/qa/extras/uiwriter/uiwriter2.cxx
index 4da4123..e00bf02 100644
--- a/sw/qa/extras/uiwriter/uiwriter2.cxx
+++ b/sw/qa/extras/uiwriter/uiwriter2.cxx
@@ -282,6 +282,92 @@
                         getProperty<OUString>(getParagraph(1), "ParaStyleName"));
}

CPPUNIT_TEST_FIXTURE(SwUiWriterTest2, testTdf54819_keep_numbering_with_Undo)
{
    load(DATA_DIRECTORY, "tdf54819b.odt");

    SwXTextDocument* pTextDoc = dynamic_cast<SwXTextDocument*>(mxComponent.get());
    CPPUNIT_ASSERT(pTextDoc);

    // heading

    CPPUNIT_ASSERT_EQUAL(OUString("Heading 1"),
                         getProperty<OUString>(getParagraph(2), "ParaStyleName"));
    CPPUNIT_ASSERT_EQUAL(OUString("Outline"),
                         getProperty<OUString>(getParagraph(2), "NumberingStyleName"));

    // next paragraph: bulleted list item

    CPPUNIT_ASSERT_EQUAL(OUString("Standard"),
                         getProperty<OUString>(getParagraph(3), "ParaStyleName"));
    OUString sNumName = getProperty<OUString>(getParagraph(3), "NumberingStyleName");
    CPPUNIT_ASSERT_MESSAGE("Missing numbering style", !sNumName.isEmpty());
    CPPUNIT_ASSERT_MESSAGE("Not a bulleted list item", sNumName != "Outline");

    //turn on red-lining and show changes
    SwDoc* pDoc = pTextDoc->GetDocShell()->GetDoc();
    pDoc->getIDocumentRedlineAccess().SetRedlineFlags(RedlineFlags::On | RedlineFlags::ShowDelete
                                                      | RedlineFlags::ShowInsert);
    pDoc->getIDocumentRedlineAccess().SetRedlineFlags(RedlineFlags::On);
    CPPUNIT_ASSERT_MESSAGE("redlining should be on",
                           pDoc->getIDocumentRedlineAccess().IsRedlineOn());
    CPPUNIT_ASSERT_MESSAGE("redlines shouldn't be visible",
                           !IDocumentRedlineAccess::IsShowChanges(
                               pDoc->getIDocumentRedlineAccess().GetRedlineFlags()));

    // remove heading with paragraph break
    SwWrtShell* pWrtShell = pTextDoc->GetDocShell()->GetWrtShell();

    pWrtShell->Down(/*bSelect=*/false);
    pWrtShell->Down(/*bSelect=*/false);
    pWrtShell->Down(/*bSelect=*/false);
    pWrtShell->Down(/*bSelect=*/false);
    pWrtShell->Down(/*bSelect=*/false);
    pWrtShell->EndPara(/*bSelect=*/true);
    pWrtShell->Right(CRSR_SKIP_CHARS, /*bSelect=*/true, 1, /*bBasicCall=*/false);
    rtl::Reference<SwTransferable> pTransfer = new SwTransferable(*pWrtShell);
    pTransfer->Cut();

    // solved problem: changing paragraph style after deletion
    CPPUNIT_ASSERT_EQUAL(OUString("Standard"),
                         getProperty<OUString>(getParagraph(2), "ParaStyleName"));

    sNumName = getProperty<OUString>(getParagraph(2), "NumberingStyleName");
    // solved problem: lost numbering
    CPPUNIT_ASSERT_MESSAGE("Missing numbering style", !sNumName.isEmpty());
    CPPUNIT_ASSERT_MESSAGE("Not a bulleted list item", sNumName != "Outline");

    // accept deletion, remaining (now second) paragraph: still bulleted list item
    IDocumentRedlineAccess& rIDRA(pDoc->getIDocumentRedlineAccess());
    rIDRA.AcceptAllRedline(true);

    CPPUNIT_ASSERT_EQUAL(OUString("Standard"),
                         getProperty<OUString>(getParagraph(2), "ParaStyleName"));
    sNumName = getProperty<OUString>(getParagraph(2), "NumberingStyleName");
    CPPUNIT_ASSERT_MESSAGE("Missing numbering style", !sNumName.isEmpty());
    CPPUNIT_ASSERT_MESSAGE("Not a bulleted list item", sNumName != "Outline");

    // solved problem: Undo with the workaround
    sw::UndoManager& rUndoManager = pDoc->GetUndoManager();
    rUndoManager.Undo();
    rUndoManager.Undo();

    // heading

    CPPUNIT_ASSERT_EQUAL(OUString("Heading 1"),
                         getProperty<OUString>(getParagraph(2), "ParaStyleName"));
    CPPUNIT_ASSERT_EQUAL(OUString("Outline"),
                         getProperty<OUString>(getParagraph(2), "NumberingStyleName"));

    // next paragraph: bulleted list item

    CPPUNIT_ASSERT_EQUAL(OUString("Standard"),
                         getProperty<OUString>(getParagraph(3), "ParaStyleName"));
    sNumName = getProperty<OUString>(getParagraph(3), "NumberingStyleName");
    CPPUNIT_ASSERT_MESSAGE("Missing numbering style", !sNumName.isEmpty());
    CPPUNIT_ASSERT_MESSAGE("Not a bulleted list item", sNumName != "Outline");
}

CPPUNIT_TEST_FIXTURE(SwUiWriterTest2, testTdf109376_redline)
{
    SwDoc* pDoc = createDoc();
diff --git a/sw/source/core/doc/DocumentContentOperationsManager.cxx b/sw/source/core/doc/DocumentContentOperationsManager.cxx
index a49fcd7..a4e0e67 100644
--- a/sw/source/core/doc/DocumentContentOperationsManager.cxx
+++ b/sw/source/core/doc/DocumentContentOperationsManager.cxx
@@ -3715,6 +3715,11 @@
        return false;
    }

    // tdf#54819 current redlining needs also modification of paragraph style and
    // attributes added to the same grouped Undo
    if (m_rDoc.GetIDocumentUndoRedo().DoesGroupUndo())
        m_rDoc.GetIDocumentUndoRedo().StartUndo(SwUndoId::DELETE, nullptr);

    auto & rDMA(*m_rDoc.getIDocumentMarkAccess());
    std::vector<std::unique_ptr<SwUndo>> MarkUndos;
    for (auto iter = rDMA.getAnnotationMarksBegin();
@@ -3815,6 +3820,10 @@
        }
        m_rDoc.getIDocumentRedlineAccess().SetRedlineFlags( eOld );
    }

    if (m_rDoc.GetIDocumentUndoRedo().DoesGroupUndo())
        m_rDoc.GetIDocumentUndoRedo().EndUndo(SwUndoId::DELETE, nullptr);

    return true;
}

diff --git a/sw/source/core/doc/DocumentRedlineManager.cxx b/sw/source/core/doc/DocumentRedlineManager.cxx
index 5142be7..e327ff5 100644
--- a/sw/source/core/doc/DocumentRedlineManager.cxx
+++ b/sw/source/core/doc/DocumentRedlineManager.cxx
@@ -40,6 +40,7 @@
#include <editsh.hxx>
#include <vcl/svapp.hxx>
#include <vcl/weld.hxx>
#include <svl/intitem.hxx>
#include <sal/log.hxx>

using namespace com::sun::star;
@@ -1933,7 +1934,39 @@
                        SwTextNode* pDelNode = pStt->nNode.GetNode().GetTextNode();
                        SwTextNode* pTextNode = pEnd->nNode.GetNode().GetTextNode();
                        if (pDelNode != nullptr && pTextNode != nullptr && pDelNode != pTextNode)
                            pDelNode->ChgFormatColl( pTextNode->GetTextColl() );
                        {
                            const SwPaM aPam(*pDelNode);
                            // using Undo, apply paragraph style
                            m_rDoc.SetTextFormatColl(aPam, pTextNode->GetTextColl());

                            // using Undo, remove direct paragraph formatting of the first deleted paragraph,
                            // and apply direct paragraph formatting of the next remaining paragraph
                            SfxItemSet aTmp(
                                m_rDoc.GetAttrPool(),
                                svl::Items<
                                    RES_PARATR_LINESPACING, RES_PARATR_OUTLINELEVEL,
                                    RES_PARATR_LIST_BEGIN, RES_PARATR_LIST_END - 1>{});

                            SfxItemSet aTmp2(
                                m_rDoc.GetAttrPool(),
                                svl::Items<
                                    RES_PARATR_LINESPACING, RES_PARATR_OUTLINELEVEL,
                                    RES_PARATR_LIST_BEGIN, RES_PARATR_LIST_END - 1>{});

                            pDelNode->GetParaAttr(aTmp, 0, 0);
                            pTextNode->GetParaAttr(aTmp2, 0, 0);

                            for( sal_uInt16 nItem = 0; nItem < aTmp.TotalCount(); ++nItem)
                            {
                                sal_uInt16 nWhich = aTmp.GetWhichByPos(nItem);
                                if( SfxItemState::SET == aTmp.GetItemState( nWhich, false ) &&
                                    SfxItemState::SET != aTmp2.GetItemState( nWhich, false ) )
                                        aTmp2.Put( aTmp.GetPool()->GetDefaultItem(nWhich), nWhich );
                            }

                            if (aTmp2.Count())
                                m_rDoc.getIDocumentContentOperations().InsertItemSet(aPam, aTmp2);
                        }
                    }
                    else
                    {