summaryrefslogtreecommitdiff
path: root/oox
diff options
context:
space:
mode:
authorArmin Le Grand (Allotropia) <Armin.Le.Grand@me.com>2022-02-18 16:07:28 +0100
committerArmin Le Grand <Armin.Le.Grand@me.com>2022-02-22 18:09:24 +0100
commiteaaf5ef8f99404797ffbb44ceeebf8795d85f07e (patch)
tree12bce3931b78d2f009252a81cc219e342fcdd8e7 /oox
parent8187737a7844fe4669f80bf0e8ef0950148dc58f (diff)
Advanced Diagram support: first additions/reorganizations
To allow advanced Diagram/SmartArt support in the future this is a first step to organize imported SmartArt Data in a way that will allow to re-layout loaded SmartArts, under re-usage of the oox::Theme (held available). It is designed to work without holding available the original XML snippets defining the imported Diagram in any way, also for performance reasons. It tries to re-use some of the already basically added functionality, including the systematic layouting using the generic layout algorithm, plus some already available text extraction. Before being sure that the former state can be completely replaced this is optoinal and used when SAL_ENABLE_ADVANCED_SMART_ART is defined. Some new stuff is already done but e.g. the redefined reLayout method will not (yet) be triggered. It works and reliably produces a re-layouted identical version, also preserving transformations. Change-Id: I08cfbae04afa663d0589530aae549216d853128d Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130171 Tested-by: Jenkins Reviewed-by: Armin Le Grand <Armin.Le.Grand@me.com>
Diffstat (limited to 'oox')
-rw-r--r--oox/Library_oox.mk1
-rw-r--r--oox/source/drawingml/diagram/diagram.cxx9
-rw-r--r--oox/source/drawingml/diagram/diagramhelper.cxx140
-rw-r--r--oox/source/drawingml/diagram/diagramhelper.hxx63
-rw-r--r--oox/source/drawingml/shape.cxx42
-rw-r--r--oox/source/ppt/pptimport.cxx5
-rw-r--r--oox/source/ppt/pptshape.cxx6
7 files changed, 266 insertions, 0 deletions
diff --git a/oox/Library_oox.mk b/oox/Library_oox.mk
index 979635a2f213..ca88ab100039 100644
--- a/oox/Library_oox.mk
+++ b/oox/Library_oox.mk
@@ -150,6 +150,7 @@ $(eval $(call gb_Library_add_exception_objects,oox,\
oox/source/drawingml/diagram/datamodel \
oox/source/drawingml/diagram/datamodelcontext \
oox/source/drawingml/diagram/diagram \
+ oox/source/drawingml/diagram/diagramhelper \
oox/source/drawingml/diagram/diagramdefinitioncontext \
oox/source/drawingml/diagram/diagramfragmenthandler \
oox/source/drawingml/diagram/diagramlayoutatoms \
diff --git a/oox/source/drawingml/diagram/diagram.cxx b/oox/source/drawingml/diagram/diagram.cxx
index 13fc4aa26b3b..e23ce68a14a8 100644
--- a/oox/source/drawingml/diagram/diagram.cxx
+++ b/oox/source/drawingml/diagram/diagram.cxx
@@ -34,6 +34,7 @@
#include <oox/token/namespaces.hxx>
#include <basegfx/matrix/b2dhommatrix.hxx>
#include <svx/svdpage.hxx>
+#include <oox/ppt/pptimport.hxx>
#include "diagramlayoutatoms.hxx"
#include "layoutatomvisitors.hxx"
@@ -362,6 +363,14 @@ void loadDiagram( ShapePtr const & pShape,
pDiagram->addTo(pShape);
pShape->setDiagramData(pData);
pShape->setDiagramDoms(pDiagram->getDomsAsPropertyValues());
+
+ // We need the shared_ptr to oox::Theme here, so do something direct when
+ // we can identify the expected type of the used import filter
+ oox::ppt::PowerPointImport* pFilter(dynamic_cast<oox::ppt::PowerPointImport*>(&rFilter));
+ const std::shared_ptr<::oox::drawingml::Theme> aThemePtr(pFilter ? pFilter->getCurrentThemePtr() : nullptr);
+
+ // Prepare support for the advanced DiagramHelper using Diagram & Theme data
+ pShape->prepareDiagramHelper(pDiagram, aThemePtr);
}
void loadDiagram(ShapePtr const& pShape,
diff --git a/oox/source/drawingml/diagram/diagramhelper.cxx b/oox/source/drawingml/diagram/diagramhelper.cxx
new file mode 100644
index 000000000000..61179993461d
--- /dev/null
+++ b/oox/source/drawingml/diagram/diagramhelper.cxx
@@ -0,0 +1,140 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include "diagramhelper.hxx"
+#include "diagram.hxx"
+
+#include <basegfx/matrix/b2dhommatrix.hxx>
+#include <oox/shape/ShapeFilterBase.hxx>
+#include <oox/ppt/pptimport.hxx>
+#include <svx/svdmodel.hxx>
+#include <comphelper/processfactory.hxx>
+
+using namespace ::com::sun::star;
+
+namespace oox::drawingml {
+
+AdvancedDiagramHelper::AdvancedDiagramHelper(
+ const std::shared_ptr< Diagram >& rDiagramPtr,
+ const std::shared_ptr<::oox::drawingml::Theme>& rTheme)
+: DiagramHelper()
+, mpDiagramPtr(rDiagramPtr)
+, mpThemePtr(rTheme)
+{
+}
+
+AdvancedDiagramHelper::~AdvancedDiagramHelper()
+{
+}
+
+void AdvancedDiagramHelper::reLayout()
+{
+ if(mpDiagramPtr)
+ {
+ // Get the oox::Shape that represents the Diagram GraphicObject
+ const ShapePtr & pParentShape = mpDiagramPtr->getShape();
+
+ // Remove it's children which represent the oox::Shapes created by
+ // the layout process as preparation to re-creation. These should
+ // already be cleared, but make sure.
+ pParentShape->getChildren().clear();
+
+ // Re-create the oox::Shapes for the diagram content
+ mpDiagramPtr->addTo(pParentShape);
+
+ // Access the GroupObject representing the SmartArt in DrawingLayer
+ SdrObjGroup* pAnchorObj(dynamic_cast<SdrObjGroup*>(SdrObject::getSdrObjectFromXShape(pParentShape->getXShape())));
+
+ // Rescue/remember geometric transformation of existing Diagram
+ basegfx::B2DHomMatrix aTransformation;
+ basegfx::B2DPolyPolygon aPolyPolygon;
+ pAnchorObj->TRGetBaseGeometry(aTransformation, aPolyPolygon);
+
+ // Delete all existing shapes in that group to prepare re-creation
+ pAnchorObj->getChildrenOfSdrObject()->ClearSdrObjList();
+
+ // For re-creation we need to use ::addShape functionality from the
+ // oox import filter since currently Shape import is very tightly
+ // coupled to Shape creation. It converts a oox::Shape representation
+ // combined with an oox::Theme to incarrnated XShapes representing the
+ // Diagram.
+ // To use that functionality, we have to create a temporary filter
+ // (based on ShapeFilterBase). Problems are that this needs to know
+ // the oox:Theme and a ComponentModel from TargetDocument.
+ // The DiagramHelper holds/delivers the oox::Theme to use, so
+ // it does not need to be re-imported from oox repeatedly.
+ // The ComponentModel can be derived from the existing XShape/GroupShape
+ // when knowing where to get it from, making it independent from app.
+ //
+ // NOTE: Using another (buffered) oox::Theme would allow to re-create
+ // using another theming in the future.
+ // NOTE: The incarnation of import filter (ShapeFilterBase) is only
+ // used for XShape creation, no xml snippets/data gets imported
+ // here. XShape creation may be isolated in the future.
+ SdrModel& rModel(pAnchorObj->getSdrModelFromSdrObject());
+ uno::Reference< uno::XInterface > const & rUnoModel(rModel.getUnoModel());
+ css::uno::Reference<css::uno::XComponentContext> xContext(comphelper::getProcessComponentContext());
+ rtl::Reference<oox::shape::ShapeFilterBase> xFilter(new oox::shape::ShapeFilterBase(xContext));
+ xFilter->setCurrentTheme(mpThemePtr);
+ css::uno::Reference< css::lang::XComponent > aComponentModel( rUnoModel, uno::UNO_QUERY );
+ xFilter->setTargetDocument(aComponentModel);
+
+ // Prepare the target for the to-be-created XShapes
+ uno::Reference<drawing::XShapes> xShapes(pParentShape->getXShape(), uno::UNO_QUERY_THROW);
+
+ for (auto const& child : pParentShape->getChildren())
+ {
+ // Create all sub-shapes. This will recursively create needed geometry using
+ // filter-internal ::createShapes
+ child->addShape(
+ *xFilter,
+ xFilter->getCurrentTheme(),
+ xShapes,
+ aTransformation,
+ pParentShape->getFillProperties());
+ }
+
+ // Re-apply remembered geometry
+ pAnchorObj->TRSetBaseGeometry(aTransformation, aPolyPolygon);
+
+ // Delete oox::Shapes that represented the content of the
+ // diagram. These were needed for creating the XShapes/SdrObjects
+ // (created by ::addTo above) but are no longer needed, so free
+ // the memory
+ pParentShape->getChildren().clear();
+ }
+}
+
+void AdvancedDiagramHelper::doAnchor(SdrObjGroup& rTarget)
+{
+ const ShapePtr & pParentShape = mpDiagramPtr->getShape();
+
+ if(pParentShape)
+ {
+ // The oox::Shapes childs are not needed for holding the original data,
+ // free that memory
+ pParentShape->getChildren().clear();
+ }
+
+ anchorToSdrObjGroup(rTarget);
+}
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/oox/source/drawingml/diagram/diagramhelper.hxx b/oox/source/drawingml/diagram/diagramhelper.hxx
new file mode 100644
index 000000000000..c6bd507f32e6
--- /dev/null
+++ b/oox/source/drawingml/diagram/diagramhelper.hxx
@@ -0,0 +1,63 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#ifndef INCLUDED_OOX_DRAWINGML_DIAGRAM_DIAGRAMHELPER_HXX
+#define INCLUDED_OOX_DRAWINGML_DIAGRAM_DIAGRAMHELPER_HXX
+
+#include <rtl/ustring.hxx>
+#include <oox/drawingml/theme.hxx>
+#include <svx/svdogrp.hxx>
+
+namespace oox::drawingml {
+
+class Diagram;
+
+// Advanced DiagramHelper
+//
+// This helper tries to hold all neccessary data to re-layout
+// all XShapes/SdrObjects of an already imported Diagram. The
+// Diagram holds the SmarArt model data before it gets layouted,
+// while Theme holds the oox Fill/Line/Style definitions to
+// apply.
+// Re-Layouting (re-reating) is rather complex, for detailed
+// information see ::reLayout implementation.
+// This helper class may/should be extended to:
+// - deliver representative data from the Diagram-Model
+// - modify it eventually
+// - im/export Diagram model to other representations
+class AdvancedDiagramHelper final : public DiagramHelper
+{
+ const std::shared_ptr< Diagram > mpDiagramPtr;
+ const std::shared_ptr<::oox::drawingml::Theme> mpThemePtr;
+
+public:
+ AdvancedDiagramHelper(
+ const std::shared_ptr< Diagram >& rDiagramPtr,
+ const std::shared_ptr<::oox::drawingml::Theme>& rTheme);
+ virtual ~AdvancedDiagramHelper();
+
+ virtual void reLayout();
+ void doAnchor(SdrObjGroup& rTarget);
+};
+
+}
+
+#endif
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/oox/source/drawingml/shape.cxx b/oox/source/drawingml/shape.cxx
index 40377e29c230..97f87a2683ab 100644
--- a/oox/source/drawingml/shape.cxx
+++ b/oox/source/drawingml/shape.cxx
@@ -50,6 +50,7 @@
#include <oox/mathml/import.hxx>
#include <oox/token/properties.hxx>
#include "diagram/datamodel.hxx"
+#include "diagram/diagramhelper.hxx"
#include <comphelper/classids.hxx>
#include <comphelper/propertysequence.hxx>
@@ -102,6 +103,7 @@
#include <vcl/wmfexternal.hxx>
#include <sal/log.hxx>
#include <svx/sdtaitm.hxx>
+#include <oox/drawingml/diagram/diagram.hxx>
using namespace ::oox::core;
using namespace ::com::sun::star;
@@ -140,6 +142,7 @@ Shape::Shape( const char* pServiceName, bool bDefaultHeight )
, mbTextBox( false )
, mbHasLinkedTxbx( false )
, maDiagramDoms( 0 )
+, mpDiagramHelper( nullptr )
{
if ( pServiceName )
msServiceName = OUString::createFromAscii( pServiceName );
@@ -189,10 +192,49 @@ Shape::Shape( const ShapePtr& pSourceShape )
, mfAspectRatio(pSourceShape->mfAspectRatio)
, mbUseBgFill(pSourceShape->mbUseBgFill)
, maDiagramFontHeights(pSourceShape->maDiagramFontHeights)
+, mpDiagramHelper( nullptr )
{}
Shape::~Shape()
{
+ // DiagramHelper should not be set here anymore, see
+ // propagateDiagramHelper below (maybe assert..?)
+ delete mpDiagramHelper;
+}
+
+void Shape::prepareDiagramHelper(
+ const std::shared_ptr< Diagram >& rDiagramPtr,
+ const std::shared_ptr<::oox::drawingml::Theme>& rTheme)
+{
+ // Prepare Diagam data collecting for this Shape
+ if( nullptr == mpDiagramHelper && FRAMETYPE_DIAGRAM == meFrameType )
+ {
+ mpDiagramHelper = new AdvancedDiagramHelper(rDiagramPtr, rTheme);
+ }
+}
+
+void Shape::propagateDiagramHelper()
+{
+ // Propagate collected Diagram data to data holder
+ if (FRAMETYPE_DIAGRAM == meFrameType && nullptr != mpDiagramHelper)
+ {
+ SdrObjGroup* pAnchorObj = dynamic_cast<SdrObjGroup*>(SdrObject::getSdrObjectFromXShape(mxShape));
+
+ if(pAnchorObj)
+ {
+ static_cast<AdvancedDiagramHelper*>(mpDiagramHelper)->doAnchor(*pAnchorObj);
+ mpDiagramHelper = nullptr;
+ }
+ }
+
+ // If propagation failed, delete/cleanup here. Since the DiagramHelper
+ // holds a Diagram and that this Shape it is necessary - the destructor
+ // will not be called and will be too late
+ if (nullptr != mpDiagramHelper)
+ {
+ delete mpDiagramHelper;
+ mpDiagramHelper = nullptr;
+ }
}
table::TablePropertiesPtr const & Shape::getTableProperties()
diff --git a/oox/source/ppt/pptimport.cxx b/oox/source/ppt/pptimport.cxx
index 8e35217ea590..87d2e1ee43d7 100644
--- a/oox/source/ppt/pptimport.cxx
+++ b/oox/source/ppt/pptimport.cxx
@@ -182,6 +182,11 @@ const ::oox::drawingml::Theme* PowerPointImport::getCurrentTheme() const
return mpActualSlidePersist ? mpActualSlidePersist->getTheme().get() : nullptr;
}
+std::shared_ptr<::oox::drawingml::Theme> PowerPointImport::getCurrentThemePtr() const
+{
+ return mpActualSlidePersist ? mpActualSlidePersist->getTheme() : std::shared_ptr<::oox::drawingml::Theme>();
+}
+
sal_Bool SAL_CALL PowerPointImport::filter( const Sequence< PropertyValue >& rDescriptor )
{
if( XmlFilterBase::filter( rDescriptor ) )
diff --git a/oox/source/ppt/pptshape.cxx b/oox/source/ppt/pptshape.cxx
index f94abea01c7a..d8ab4dfef7ca 100644
--- a/oox/source/ppt/pptshape.cxx
+++ b/oox/source/ppt/pptshape.cxx
@@ -564,6 +564,12 @@ void PPTShape::addShape(
syncDiagramFontHeights();
}
+ // Support advanced DiagramHelper
+ if (FRAMETYPE_DIAGRAM == meFrameType)
+ {
+ propagateDiagramHelper();
+ }
+
getShapeProperties().getProperty(PROP_URL) >>= sURL;
if (!sURL.isEmpty() && !xShapes.is())
aURLShapes.push_back({ sURL, xShape });