bump product version to 6.4.0.3
[LibreOffice.git] / sd / source / core / CustomAnimationCloner.cxx
blob3049abb60d5cd068bc863bee866c348f1d6a1670
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/animations/XAnimate.hpp>
21 #include <com/sun/star/animations/XAnimationNode.hpp>
22 #include <com/sun/star/animations/Event.hpp>
23 #include <com/sun/star/animations/XCommand.hpp>
24 #include <com/sun/star/animations/XIterateContainer.hpp>
25 #include <com/sun/star/animations/XAudio.hpp>
26 #include <com/sun/star/animations/AnimationNodeType.hpp>
27 #include <com/sun/star/animations/ValuePair.hpp>
28 #include <com/sun/star/util/XCloneable.hpp>
29 #include <com/sun/star/presentation/ParagraphTarget.hpp>
30 #include <com/sun/star/container/XEnumerationAccess.hpp>
31 #include <com/sun/star/beans/NamedValue.hpp>
33 #include <map>
35 #include <tools/debug.hxx>
36 #include <tools/diagnose_ex.h>
37 #include <animations/animationnodehelper.hxx>
39 #include <svx/svditer.hxx>
41 #include <CustomAnimationCloner.hxx>
42 #include <sdpage.hxx>
44 using namespace ::com::sun::star::uno;
45 using namespace ::com::sun::star::animations;
46 using namespace ::com::sun::star::presentation;
47 using namespace ::com::sun::star::container;
49 using ::com::sun::star::drawing::XShape;
50 using ::com::sun::star::beans::NamedValue;
52 namespace sd
54 class CustomAnimationClonerImpl
56 public:
57 CustomAnimationClonerImpl();
58 Reference< XAnimationNode > Clone( const Reference< XAnimationNode >& xSourceNode, const SdPage* pSource, const SdPage* pTarget );
60 private:
61 void transformNode( const Reference< XAnimationNode >& xNode );
62 Any transformValue( const Any& rValue );
64 Reference< XShape > getClonedShape( const Reference< XShape >& xSource ) const;
65 Reference< XAnimationNode > getClonedNode( const Reference< XAnimationNode >& xSource ) const;
67 mutable ::std::map< Reference< XShape >, Reference< XShape > > maShapeMap;
68 std::vector< Reference< XAnimationNode > > maSourceNodeVector;
69 std::vector< Reference< XAnimationNode > > maCloneNodeVector;
72 CustomAnimationClonerImpl::CustomAnimationClonerImpl()
76 Reference< XAnimationNode > Clone( const Reference< XAnimationNode >& xSourceNode, const SdPage* pSource, const SdPage* pTarget )
78 CustomAnimationClonerImpl aCloner;
79 return aCloner.Clone( xSourceNode, pSource, pTarget );
82 Reference< XAnimationNode > CustomAnimationClonerImpl::Clone( const Reference< XAnimationNode >& xSourceNode, const SdPage* pSourcePage, const SdPage* pTargetPage )
84 try
86 // clone animation hierarchy
87 Reference< css::util::XCloneable > xClonable( xSourceNode, UNO_QUERY_THROW );
88 Reference< XAnimationNode > xCloneNode( xClonable->createClone(), UNO_QUERY_THROW );
90 // create a dictionary to map source to cloned shapes
91 if( pSourcePage && pTargetPage )
93 SdrObjListIter aSourceIter( pSourcePage, SdrIterMode::DeepWithGroups );
94 SdrObjListIter aTargetIter( pTargetPage, SdrIterMode::DeepWithGroups );
96 while( aSourceIter.IsMore() && aTargetIter.IsMore() )
98 SdrObject* pSource = aSourceIter.Next();
99 SdrObject* pTarget = aTargetIter.Next();
101 if( pSource && pTarget)
103 Reference< XShape > xSource( pSource->getUnoShape(), UNO_QUERY );
104 Reference< XShape > xTarget( pTarget->getUnoShape(), UNO_QUERY );
105 if( xSource.is() && xTarget.is() )
107 maShapeMap[xSource] = xTarget;
113 // create a dictionary to map source to cloned nodes
114 ::anim::create_deep_vector( xSourceNode, maSourceNodeVector );
115 ::anim::create_deep_vector( xCloneNode, maCloneNodeVector );
117 transformNode( xCloneNode );
119 return xCloneNode;
121 catch( Exception& )
123 TOOLS_WARN_EXCEPTION( "sd", "sd::CustomAnimationClonerImpl::Clone()" );
124 Reference< XAnimationNode > xEmpty;
125 return xEmpty;
129 void CustomAnimationClonerImpl::transformNode( const Reference< XAnimationNode >& xNode )
133 xNode->setBegin( transformValue( xNode->getBegin() ) );
134 xNode->setEnd( transformValue( xNode->getEnd() ) );
136 sal_Int16 nNodeType( xNode->getType() );
137 switch( nNodeType )
139 case AnimationNodeType::ITERATE:
141 Reference< XIterateContainer > xIter( xNode, UNO_QUERY_THROW );
142 xIter->setTarget( transformValue( xIter->getTarget() ) );
143 [[fallthrough]];
145 case AnimationNodeType::PAR:
146 case AnimationNodeType::SEQ:
148 Reference< XEnumerationAccess > xEnumerationAccess( xNode, UNO_QUERY_THROW );
149 Reference< XEnumeration > xEnumeration( xEnumerationAccess->createEnumeration(), UNO_SET_THROW );
150 while( xEnumeration->hasMoreElements() )
152 Reference< XAnimationNode > xChildNode( xEnumeration->nextElement(), UNO_QUERY_THROW );
153 transformNode( xChildNode );
156 break;
158 case AnimationNodeType::ANIMATE:
159 case AnimationNodeType::SET:
160 case AnimationNodeType::ANIMATEMOTION:
161 case AnimationNodeType::ANIMATECOLOR:
162 case AnimationNodeType::ANIMATETRANSFORM:
163 case AnimationNodeType::TRANSITIONFILTER:
165 Reference< XAnimate > xAnimate( xNode, UNO_QUERY_THROW );
166 xAnimate->setTarget( transformValue( xAnimate->getTarget() ) );
168 break;
170 case AnimationNodeType::COMMAND:
172 Reference< XCommand > xCommand( xNode, UNO_QUERY_THROW );
173 xCommand->setTarget( transformValue( xCommand->getTarget() ) );
175 break;
177 case AnimationNodeType::AUDIO:
179 Reference< XAudio > xAudio( xNode, UNO_QUERY_THROW );
180 xAudio->setSource( transformValue( xAudio->getSource() ) );
182 break;
185 Sequence< NamedValue > aUserData( xNode->getUserData() );
186 if( aUserData.hasElements() )
188 for( NamedValue & namedValue : aUserData )
190 namedValue.Value = transformValue( namedValue.Value );
193 xNode->setUserData( aUserData );
196 catch( Exception& )
198 TOOLS_WARN_EXCEPTION( "sd", "sd::CustomAnimationClonerImpl::transformNode()" );
202 Any CustomAnimationClonerImpl::transformValue( const Any& rValue )
204 if( rValue.hasValue() ) try
206 if( rValue.getValueType() == cppu::UnoType<ValuePair>::get() )
208 ValuePair aValuePair;
209 rValue >>= aValuePair;
211 aValuePair.First = transformValue( aValuePair.First );
212 aValuePair.Second = transformValue( aValuePair.Second );
214 return makeAny( aValuePair );
216 else if( rValue.getValueType() == cppu::UnoType< Sequence<Any> >::get() )
218 Sequence<Any> aSequence;
219 rValue >>= aSequence;
221 for( Any& rAny : aSequence )
222 rAny = transformValue( rAny );
224 return makeAny( aSequence );
226 else if( rValue.getValueTypeClass() == TypeClass_INTERFACE )
228 Reference< XShape > xShape;
229 rValue >>= xShape;
230 if( xShape.is() )
232 return makeAny( getClonedShape( xShape ) );
234 else
236 Reference< XAnimationNode > xNode;
237 rValue >>= xNode;
238 if( xNode.is() )
239 return makeAny( getClonedNode( xNode ) );
242 else if( rValue.getValueType() == cppu::UnoType<ParagraphTarget>::get() )
244 ParagraphTarget aParaTarget;
245 rValue >>= aParaTarget;
247 aParaTarget.Shape = getClonedShape( aParaTarget.Shape );
249 return makeAny( aParaTarget );
251 else if( rValue.getValueType() == cppu::UnoType<Event>::get() )
253 Event aEvent;
254 rValue >>= aEvent;
256 aEvent.Source = transformValue( aEvent.Source );
258 return makeAny( aEvent );
261 catch( Exception& )
263 TOOLS_WARN_EXCEPTION( "sd", "sd::CustomAnimationClonerImpl::transformValue()" );
266 return rValue;
269 Reference< XShape > CustomAnimationClonerImpl::getClonedShape( const Reference< XShape >& xSource ) const
271 if( xSource.is() )
273 if( maShapeMap.find(xSource) != maShapeMap.end() )
275 return maShapeMap[xSource];
278 DBG_ASSERT( maShapeMap.empty(), "sd::CustomAnimationClonerImpl::getClonedShape() failed!" );
280 return xSource;
283 Reference< XAnimationNode > CustomAnimationClonerImpl::getClonedNode( const Reference< XAnimationNode >& xSource ) const
285 std::size_t nNodeCount = maSourceNodeVector.size();
286 std::size_t nCloneNodeCount = maCloneNodeVector.size();
288 if (nNodeCount != nCloneNodeCount)
289 SAL_WARN("sd.core", "Sizes of maSourceNodeVector and maCloneNodeVector mismatch!");
291 for( std::size_t nNode = 0; nNode < nNodeCount && nNode < nCloneNodeCount; ++nNode )
293 if( maSourceNodeVector[nNode] == xSource )
294 return maCloneNodeVector[nNode];
297 OSL_FAIL( "sd::CustomAnimationClonerImpl::getClonedNode() failed!" );
298 return xSource;
302 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */