sc/qa/unit/subsequent_export_test4.cxx |   61 +++++++++++++++++++++++++++++++++
 sc/source/filter/xml/xmlexprt.cxx      |    8 +---
 sc/source/filter/xml/xmlstyli.cxx      |   23 ++++++++++++
 sc/source/filter/xml/xmlstyli.hxx      |    8 ++++
 xmloff/source/draw/shapeexport.cxx     |    3 +
 xmloff/source/draw/ximpshap.cxx        |    6 ++-
 6 files changed, 102 insertions(+), 7 deletions(-)

New commits:
commit b1393fd5ce847f40abab49f57c67929bb0087fae
Author:     Maxim Monastirsky <momonas...@gmail.com>
AuthorDate: Fri Mar 17 14:54:30 2023 +0200
Commit:     Maxim Monastirsky <momonas...@gmail.com>
CommitDate: Thu Mar 23 08:54:06 2023 +0000

    sc drawstyles: ODF import and export
    
    Change-Id: Id92088a7d70c550682fe707d81e94157edc7caa8
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/149330
    Tested-by: Jenkins
    Reviewed-by: Maxim Monastirsky <momonas...@gmail.com>

diff --git a/sc/qa/unit/subsequent_export_test4.cxx 
b/sc/qa/unit/subsequent_export_test4.cxx
index 024019e89c1e..3033ff4fef85 100644
--- a/sc/qa/unit/subsequent_export_test4.cxx
+++ b/sc/qa/unit/subsequent_export_test4.cxx
@@ -39,6 +39,7 @@
 #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
 #include <com/sun/star/packages/zip/ZipFileAccess.hpp>
 #include <com/sun/star/sheet/GlobalSheetSettings.hpp>
+#include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
 #include <com/sun/star/text/XTextColumns.hpp>
 
 using namespace ::com::sun::star;
@@ -1456,6 +1457,66 @@ CPPUNIT_TEST_FIXTURE(ScExportTest4, 
testAutofilterHiddenButton)
     }
 }
 
+CPPUNIT_TEST_FIXTURE(ScExportTest4, testShapeStyles)
+{
+    createScDoc();
+
+    {
+        uno::Reference<lang::XMultiServiceFactory> xMSF(mxComponent, 
uno::UNO_QUERY_THROW);
+        uno::Reference<style::XStyleFamiliesSupplier> 
xStyleFamiliesSupplier(mxComponent,
+                                                                             
uno::UNO_QUERY_THROW);
+        uno::Reference<container::XNameContainer> xGraphicStyles(
+            
xStyleFamiliesSupplier->getStyleFamilies()->getByName("GraphicStyles"),
+            uno::UNO_QUERY_THROW);
+
+        // create styles
+        uno::Reference<style::XStyle> xStyle(
+            xMSF->createInstance("com.sun.star.style.GraphicStyle"), 
uno::UNO_QUERY_THROW);
+        xGraphicStyles->insertByName("MyStyle1", Any(xStyle));
+        uno::Reference<beans::XPropertySet>(xStyle, uno::UNO_QUERY_THROW)
+            ->setPropertyValue("FillColor", Any(COL_RED));
+
+        xStyle.set(xMSF->createInstance("com.sun.star.style.GraphicStyle"), 
uno::UNO_QUERY_THROW);
+        xGraphicStyles->insertByName("MyStyle2", Any(xStyle));
+        xStyle->setParentStyle("MyStyle1");
+
+        xStyle.set(xMSF->createInstance("com.sun.star.style.GraphicStyle"), 
uno::UNO_QUERY_THROW);
+        xGraphicStyles->insertByName("MyStyle3", Any(xStyle));
+        xStyle->setParentStyle("MyStyle2");
+
+        // create shape
+        uno::Reference<drawing::XShape> xShape(
+            xMSF->createInstance("com.sun.star.drawing.RectangleShape"), 
uno::UNO_QUERY_THROW);
+
+        uno::Reference<drawing::XDrawPagesSupplier> xDPS(mxComponent, 
uno::UNO_QUERY_THROW);
+        uno::Reference<drawing::XShapes> 
xShapes(xDPS->getDrawPages()->getByIndex(0),
+                                                 uno::UNO_QUERY_THROW);
+        xShapes->add(xShape);
+        uno::Reference<beans::XPropertySet>(xShape, uno::UNO_QUERY_THROW)
+            ->setPropertyValue("Style", Any(xStyle));
+    }
+
+    saveAndReload("calc8");
+
+    {
+        uno::Reference<drawing::XDrawPagesSupplier> xDPS(mxComponent, 
uno::UNO_QUERY_THROW);
+        uno::Reference<drawing::XShapes> 
xShapes(xDPS->getDrawPages()->getByIndex(0),
+                                                 uno::UNO_QUERY_THROW);
+        uno::Reference<beans::XPropertySet> xShape(xShapes->getByIndex(0), 
uno::UNO_QUERY_THROW);
+
+        // check style hierarchy
+        uno::Reference<style::XStyle> xStyle(xShape->getPropertyValue("Style"),
+                                             uno::UNO_QUERY_THROW);
+        CPPUNIT_ASSERT_EQUAL(OUString("MyStyle3"), xStyle->getName());
+        CPPUNIT_ASSERT_EQUAL(OUString("MyStyle2"), xStyle->getParentStyle());
+
+        // check that styles have effect on shapes
+        Color nColor;
+        xShape->getPropertyValue("FillColor") >>= nColor;
+        CPPUNIT_ASSERT_EQUAL(COL_RED, nColor);
+    }
+}
+
 CPPUNIT_PLUGIN_IMPLEMENT();
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/filter/xml/xmlexprt.cxx 
b/sc/source/filter/xml/xmlexprt.cxx
index 2e35f05367ca..df2d26e49d20 100644
--- a/sc/source/filter/xml/xmlexprt.cxx
+++ b/sc/source/filter/xml/xmlexprt.cxx
@@ -392,6 +392,8 @@ ScXMLExport::ScXMLExport(
     GetAutoStylePool()->AddFamily(XmlStyleFamily::TABLE_TABLE, 
XML_STYLE_FAMILY_TABLE_TABLE_STYLES_NAME,
         xTableStylesExportPropertySetMapper, 
XML_STYLE_FAMILY_TABLE_TABLE_STYLES_PREFIX);
 
+    GetShapeExport(); // make sure the graphics styles family is added
+
     if( !(getExportFlags() & 
(SvXMLExportFlags::STYLES|SvXMLExportFlags::AUTOSTYLES|SvXMLExportFlags::MASTERSTYLES|SvXMLExportFlags::CONTENT))
 )
         return;
 
@@ -1980,10 +1982,7 @@ void ScXMLExport::ExportStyles_( bool bUsed )
             uno::Reference <beans::XPropertySet> 
xProperties(xMultiServiceFactory->createInstance("com.sun.star.sheet.Defaults"),
 uno::UNO_QUERY);
             if (xProperties.is())
                 aStylesExp->exportDefaultStyle(xProperties, 
XML_STYLE_FAMILY_TABLE_CELL_STYLES_NAME, xCellStylesExportPropertySetMapper);
-            if (pSharedData->HasShapes())
-            {
-                GetShapeExport()->ExportGraphicDefaults();
-            }
+            GetShapeExport()->ExportGraphicDefaults();
         }
         collectDataStyles(false);
     }
@@ -2339,7 +2338,6 @@ void ScXMLExport::collectAutoStyles()
             // stored styles for notes
 
             rtl::Reference<SvXMLExportPropertyMapper> xShapeMapper = 
XMLShapeExport::CreateShapePropMapper( *this );
-            GetShapeExport(); // make sure the graphics styles family is added
 
             const std::vector<ScNoteStyleEntry>& rNoteEntries = 
pSheetData->GetNoteStyles();
             for (const auto& rNoteEntry : rNoteEntries)
diff --git a/sc/source/filter/xml/xmlstyli.cxx 
b/sc/source/filter/xml/xmlstyli.cxx
index 96fc38cbc04d..e15840ed3fe5 100644
--- a/sc/source/filter/xml/xmlstyli.cxx
+++ b/sc/source/filter/xml/xmlstyli.cxx
@@ -610,6 +610,8 @@ SvXMLStyleContext 
*XMLTableStylesContext::CreateStyleStyleChildContext(
     // use own wrapper for text and paragraph, to record style usage
     if (nFamily == XmlStyleFamily::TEXT_PARAGRAPH || nFamily == 
XmlStyleFamily::TEXT_TEXT)
         pStyle = new  ScCellTextStyleContext( GetImport(),*this, nFamily );
+    else if (nFamily == XmlStyleFamily::SD_GRAPHICS_ID)
+        pStyle = new ScShapeStyleContext( GetImport(), *this, nFamily );
     else
         pStyle = SvXMLStylesContext::CreateStyleStyleChildContext(
                     nFamily, nElement, xAttrList );
@@ -655,6 +657,7 @@ SvXMLStyleContext 
*XMLTableStylesContext::CreateDefaultStyleStyleChildContext(
 }
 
 constexpr OUStringLiteral 
gsCellStyleServiceName(u"com.sun.star.style.CellStyle");
+constexpr OUStringLiteral 
gsGraphicStyleServiceName(u"com.sun.star.style.GraphicStyle");
 
 XMLTableStylesContext::XMLTableStylesContext( SvXMLImport& rImport,
         const bool bTempAutoStyles )
@@ -772,6 +775,14 @@ uno::Reference < XNameContainer >
                     sName = "RowStyles";
             }
             break;
+            case XmlStyleFamily::SD_GRAPHICS_ID:
+            {
+                if( xGraphicStyles.is() )
+                    xStyles.set(xGraphicStyles);
+                else
+                    sName = "GraphicStyles";
+            }
+            break;
             default: break;
         }
         if( !xStyles.is() && !sName.isEmpty() && GetScImport().GetModel().is() 
)
@@ -806,6 +817,9 @@ uno::Reference < XNameContainer >
                 case XmlStyleFamily::TABLE_ROW:
                     const_cast<XMLTableStylesContext 
*>(this)->xRowStyles.set(xStyles);
                     break;
+                case XmlStyleFamily::SD_GRAPHICS_ID:
+                    const_cast<XMLTableStylesContext 
*>(this)->xGraphicStyles.set(xStyles);
+                    break;
                 default: break;
                 }
             }
@@ -834,6 +848,9 @@ OUString XMLTableStylesContext::GetServiceName( 
XmlStyleFamily nFamily ) const
         case XmlStyleFamily::TABLE_TABLE:
             sServiceName = XML_STYLE_FAMILY_TABLE_TABLE_STYLES_NAME;
             break;
+        case XmlStyleFamily::SD_GRAPHICS_ID:
+            sServiceName = gsGraphicStyleServiceName;
+            break;
         default: break;
         }
     }
@@ -1034,4 +1051,10 @@ void ScCellTextStyleContext::FillPropertySet( const 
uno::Reference<beans::XPrope
     }
 }
 
+void ScShapeStyleContext::Finish(bool bOverwrite)
+{
+    // set parent styles
+    XMLPropStyleContext::Finish(bOverwrite);
+}
+
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/filter/xml/xmlstyli.hxx 
b/sc/source/filter/xml/xmlstyli.hxx
index 48c401658f23..8101a72b1f7c 100644
--- a/sc/source/filter/xml/xmlstyli.hxx
+++ b/sc/source/filter/xml/xmlstyli.hxx
@@ -24,6 +24,7 @@
 #include <xmloff/xmlictxt.hxx>
 #include <xmloff/prstylei.hxx>
 #include <xmloff/xmlimppr.hxx>
+#include <xmloff/XMLShapeStyleContext.hxx>
 #include <xmloff/XMLTextMasterPageContext.hxx>
 #include <xmloff/txtstyli.hxx>
 #include "xmlimprt.hxx"
@@ -115,6 +116,7 @@ class XMLTableStylesContext : public SvXMLStylesContext
     css::uno::Reference< css::container::XNameContainer > xColumnStyles;
     css::uno::Reference< css::container::XNameContainer > xRowStyles;
     css::uno::Reference< css::container::XNameContainer > xTableStyles;
+    css::uno::Reference< css::container::XNameContainer > xGraphicStyles;
     sal_Int32 nNumberFormatIndex;
     sal_Int32 nConditionalFormatIndex;
     sal_Int32 nCellStyleIndex;
@@ -222,4 +224,10 @@ public:
             const css::uno::Reference< css::beans::XPropertySet > & rPropSet ) 
override;
 };
 
+class ScShapeStyleContext : public XMLShapeStyleContext
+{
+    using XMLShapeStyleContext::XMLShapeStyleContext;
+    void Finish(bool bOverwrite) override;
+};
+
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/xmloff/source/draw/shapeexport.cxx 
b/xmloff/source/draw/shapeexport.cxx
index da9fe7f0e438..056c3ce98267 100644
--- a/xmloff/source/draw/shapeexport.cxx
+++ b/xmloff/source/draw/shapeexport.cxx
@@ -1439,8 +1439,9 @@ void XMLShapeExport::ExportGraphicDefaults()
         {
             aStEx->exportDefaultStyle( xDefaults, 
XML_STYLE_FAMILY_SD_GRAPHICS_NAME, xPropertySetMapper );
 
-            // write graphic family styles
+            // write graphic styles (family name differs depending on the 
module)
             aStEx->exportStyleFamily("graphics", 
OUString(XML_STYLE_FAMILY_SD_GRAPHICS_NAME), xPropertySetMapper, false, 
XmlStyleFamily::SD_GRAPHICS_ID);
+            aStEx->exportStyleFamily("GraphicStyles", 
OUString(XML_STYLE_FAMILY_SD_GRAPHICS_NAME), xPropertySetMapper, false, 
XmlStyleFamily::SD_GRAPHICS_ID);
         }
     }
     catch(const lang::ServiceNotRegisteredException&)
diff --git a/xmloff/source/draw/ximpshap.cxx b/xmloff/source/draw/ximpshap.cxx
index 266135bad479..22f9d7ddb0a1 100644
--- a/xmloff/source/draw/ximpshap.cxx
+++ b/xmloff/source/draw/ximpshap.cxx
@@ -671,7 +671,11 @@ void SdXMLShapeContext::SetStyle( bool bSupportsStyle /* = 
true */)
                             else
                             {
                                 // get graphics family
-                                xFamilies->getByName("graphics") >>= xFamily;
+                                if (xFamilies->hasByName("graphics"))
+                                    xFamilies->getByName("graphics") >>= 
xFamily;
+                                else
+                                    xFamilies->getByName("GraphicStyles") >>= 
xFamily;
+
                                 aStyleName = GetImport().GetStyleDisplayName(
                                     XmlStyleFamily::SD_GRAPHICS_ID,
                                     aStyleName );

Reply via email to