sd/inc/stlpool.hxx          |    1 +
 sd/inc/stlsheet.hxx         |    1 +
 sd/source/core/drawdoc3.cxx |   21 ++++++++++++++++++---
 sd/source/core/stlpool.cxx  |    9 +++++++++
 4 files changed, 29 insertions(+), 3 deletions(-)

New commits:
commit 441ecdc951211cc1e8453434d26ae40c121cb153
Author:     Maxim Monastirsky <momonas...@gmail.com>
AuthorDate: Mon Dec 5 14:25:31 2022 +0200
Commit:     Maxim Monastirsky <momonas...@gmail.com>
CommitDate: Wed Dec 7 10:59:29 2022 +0000

    sd: improve handling of table styles on page pasting
    
    - Remove table styles that were copied from the source
    document, but not actually used in the pasted pages.
    Same as we were doing already for graphic/cell styles.
    
    - Disable the undo record of inserted cell styles, as
    long as we don't have undo actions also for the table
    style. This prevents data loss, as otherwise the table
    style will be exported, but not the cell styles it uses.
    
    The difficulty with having an undo action for table
    styles is that once a style is removed from its family
    it is disposed, so we no longer able to use that style
    for a redo action. This was "solved" in i#120015 for
    graphic/cell styles, by not disposing them at all.
    However table styles maintain cyclic references with
    their cell styles, so we can't give up on disposing.
    
    I'll take the time to think on a proper solution for
    this (e.g. making copies of styles instead of dealing
    with the original ones might be an option). For now,
    let's just remove this unfinished undo attempt.
    
    Change-Id: Iae06273217b2d695401921a84e7edf096556530e
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/143749
    Tested-by: Jenkins
    Reviewed-by: Maxim Monastirsky <momonas...@gmail.com>

diff --git a/sd/inc/stlpool.hxx b/sd/inc/stlpool.hxx
index bb73e157ed3f..9e1693249f86 100644
--- a/sd/inc/stlpool.hxx
+++ b/sd/inc/stlpool.hxx
@@ -75,6 +75,7 @@ public:
     void                CopyGraphicSheets(SdStyleSheetPool& rSourcePool);
     void                CopyCellSheets(SdStyleSheetPool& rSourcePool);
     void                CopyTableStyles(SdStyleSheetPool const & rSourcePool);
+    void                CopyTableStyles(SdStyleSheetPool const & rSourcePool, 
XStyleVector& rCreatedSheets);
     void                CopyCellSheets(SdStyleSheetPool& rSourcePool, 
StyleSheetCopyResultVector& rCreatedSheets);
     void                RenameAndCopyGraphicSheets(SdStyleSheetPool& 
rSourcePool, StyleSheetCopyResultVector& rCreatedSheets, std::u16string_view 
rRenameSuffix);
 
diff --git a/sd/inc/stlsheet.hxx b/sd/inc/stlsheet.hxx
index 9080c3b8805b..e830c097e990 100644
--- a/sd/inc/stlsheet.hxx
+++ b/sd/inc/stlsheet.hxx
@@ -165,6 +165,7 @@ private:
 };
 
 typedef std::vector< rtl::Reference< SdStyleSheet > > SdStyleSheetVector;
+typedef std::vector< css::uno::Reference< css::style::XStyle > > XStyleVector;
 
 struct StyleSheetCopyResult
 {
diff --git a/sd/source/core/drawdoc3.cxx b/sd/source/core/drawdoc3.cxx
index 810969940bae..aaaabf7d304e 100644
--- a/sd/source/core/drawdoc3.cxx
+++ b/sd/source/core/drawdoc3.cxx
@@ -345,6 +345,21 @@ lcl_removeUnusedStyles(SfxStyleSheetBasePool* const 
pStyleSheetPool, StyleSheetC
     rStyles = aUsedStyles;
 }
 
+void
+lcl_removeUnusedTableStyles(SdStyleSheetPool* const pStyleSheetPool, 
XStyleVector const & rStyles)
+{
+    css::uno::Reference<css::container::XNameContainer> xTableFamily(
+        pStyleSheetPool->getByName("table"), css::uno::UNO_QUERY);
+    if (!xTableFamily)
+        return;
+
+    for (const auto& a : rStyles)
+    {
+        if (!a->isInUse())
+            xTableFamily->removeByName(a->getName());
+    }
+}
+
 SfxStyleSheet *lcl_findStyle(StyleSheetCopyResultVector& rStyles, 
std::u16string_view aStyleName)
 {
     for (const auto& a : rStyles)
@@ -524,7 +539,8 @@ bool SdDrawDocument::InsertBookmarkAsPage(
     rStyleSheetPool.CopyCellSheets(rBookmarkStyleSheetPool, aNewCellStyles);
 
     // TODO handle undo of table styles too
-    rStyleSheetPool.CopyTableStyles(rBookmarkStyleSheetPool);
+    XStyleVector aNewTableStyles;
+    rStyleSheetPool.CopyTableStyles(rBookmarkStyleSheetPool, aNewTableStyles);
 
     // Insert document
 
@@ -934,9 +950,8 @@ bool SdDrawDocument::InsertBookmarkAsPage(
     lcl_removeUnusedStyles(GetStyleSheetPool(), aNewGraphicStyles);
     if (!aNewGraphicStyles.empty() && pUndoMgr)
         
pUndoMgr->AddUndoAction(std::make_unique<SdMoveStyleSheetsUndoAction>(this, 
aNewGraphicStyles, true));
+    
lcl_removeUnusedTableStyles(static_cast<SdStyleSheetPool*>(GetStyleSheetPool()),
 aNewTableStyles);
     lcl_removeUnusedStyles(GetStyleSheetPool(), aNewCellStyles);
-    if (!aNewCellStyles.empty() && pUndoMgr)
-        
pUndoMgr->AddUndoAction(std::make_unique<SdMoveStyleSheetsUndoAction>(this, 
aNewCellStyles, true));
 
     if( bUndo )
         EndUndo();
diff --git a/sd/source/core/stlpool.cxx b/sd/source/core/stlpool.cxx
index 738279e8a440..4bca10fb71bc 100644
--- a/sd/source/core/stlpool.cxx
+++ b/sd/source/core/stlpool.cxx
@@ -521,6 +521,12 @@ void SdStyleSheetPool::CopyCellSheets(SdStyleSheetPool& 
rSourcePool)
 }
 
 void SdStyleSheetPool::CopyTableStyles(SdStyleSheetPool const & rSourcePool)
+{
+    XStyleVector aTmpSheets;
+    CopyTableStyles(rSourcePool, aTmpSheets);
+}
+
+void SdStyleSheetPool::CopyTableStyles(SdStyleSheetPool const & rSourcePool, 
XStyleVector& rCreatedSheets)
 {
     Reference< XIndexAccess > xSource( rSourcePool.mxTableFamily, UNO_QUERY );
     Reference< XNameContainer > xTarget( mxTableFamily, UNO_QUERY );
@@ -555,7 +561,10 @@ void SdStyleSheetPool::CopyTableStyles(SdStyleSheetPool 
const & rSourcePool)
         if( xTarget->hasByName( sName ) )
             Reference<XComponent>(xNewTableStyle, UNO_QUERY_THROW)->dispose();
         else
+        {
+            rCreatedSheets.emplace_back(xNewTableStyle, UNO_QUERY_THROW);
             xTarget->insertByName( sName, Any( xNewTableStyle ) );
+        }
     }
     catch( Exception& )
     {

Reply via email to