Update ooo320-m1
[ooovba.git] / sd / source / core / CustomAnimationCloner.cxx
blob15c0a3ca60df8bd4aa6e3fad62c4ca75b09b0155
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: CustomAnimationCloner.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sd.hxx"
33 #include <com/sun/star/animations/XAnimationNode.hpp>
34 #include <com/sun/star/animations/Event.hpp>
35 #ifndef _COM_SUN_STAR_ANIMATIONS_XAnimateColor_HPP_
36 #include <com/sun/star/animations/XAnimateColor.hpp>
37 #endif
38 #ifndef _COM_SUN_STAR_ANIMATIONS_XAnimateSet_HPP_
39 #include <com/sun/star/animations/XAnimateSet.hpp>
40 #endif
41 #include <com/sun/star/animations/XCommand.hpp>
42 #ifndef _COM_SUN_STAR_ANIMATIONS_XAnimateMotion_HPP_
43 #include <com/sun/star/animations/XAnimateMotion.hpp>
44 #endif
45 #ifndef _COM_SUN_STAR_ANIMATIONS_XAnimateTransform_HPP_
46 #include <com/sun/star/animations/XAnimateTransform.hpp>
47 #endif
48 #ifndef _COM_SUN_STAR_ANIMATIONS_XTransitionFilter_HPP_
49 #include <com/sun/star/animations/XTransitionFilter.hpp>
50 #endif
51 #include <com/sun/star/animations/XIterateContainer.hpp>
52 #include <com/sun/star/animations/XAudio.hpp>
53 #include <com/sun/star/animations/AnimationNodeType.hpp>
54 #include <com/sun/star/animations/ValuePair.hpp>
55 #include <com/sun/star/presentation/EffectNodeType.hpp>
56 #include <com/sun/star/util/XCloneable.hpp>
57 #include <com/sun/star/presentation/ParagraphTarget.hpp>
58 #include <com/sun/star/container/XEnumerationAccess.hpp>
59 #include <com/sun/star/beans/NamedValue.hpp>
61 #include <map>
63 #include "comphelper/anytostring.hxx"
64 #include "cppuhelper/exc_hlp.hxx"
65 #include "rtl/ref.hxx"
66 #include <animations/animationnodehelper.hxx>
68 // header for class SdrObjListIter
69 #include <svx/svditer.hxx>
71 #include "sdpage.hxx"
73 using namespace ::com::sun::star::uno;
74 using namespace ::com::sun::star::animations;
75 using namespace ::com::sun::star::presentation;
76 using namespace ::com::sun::star::container;
78 using ::rtl::OUString;
79 using ::rtl::OString;
80 using ::com::sun::star::drawing::XShape;
81 using ::com::sun::star::beans::NamedValue;
83 namespace sd
85 class CustomAnimationClonerImpl
87 public:
88 CustomAnimationClonerImpl();
89 Reference< XAnimationNode > Clone( const Reference< XAnimationNode >& xSourceNode, const SdPage* pSource = 0, const SdPage* pTarget = 0 );
91 private:
92 void transformNode( const Reference< XAnimationNode >& xNode );
93 Any transformValue( const Any& rValue );
95 Reference< XShape > getClonedShape( const Reference< XShape >& xSource ) const;
96 Reference< XAnimationNode > getClonedNode( const Reference< XAnimationNode >& xSource ) const;
98 mutable ::std::map< Reference< XShape >, Reference< XShape > > maShapeMap;
99 std::vector< Reference< XAnimationNode > > maSourceNodeVector;
100 std::vector< Reference< XAnimationNode > > maCloneNodeVector;
103 CustomAnimationClonerImpl::CustomAnimationClonerImpl()
107 Reference< XAnimationNode > Clone( const Reference< XAnimationNode >& xSourceNode, const SdPage* pSource, const SdPage* pTarget )
109 CustomAnimationClonerImpl aCloner;
110 return aCloner.Clone( xSourceNode, pSource, pTarget );
113 Reference< XAnimationNode > CustomAnimationClonerImpl::Clone( const Reference< XAnimationNode >& xSourceNode, const SdPage* pSourcePage, const SdPage* pTargetPage )
117 // clone animation hierarchie
118 Reference< ::com::sun::star::util::XCloneable > xClonable( xSourceNode, UNO_QUERY_THROW );
119 Reference< XAnimationNode > xCloneNode( xClonable->createClone(), UNO_QUERY_THROW );
121 // create a dictionary to map source to cloned shapes
122 if( pSourcePage && pTargetPage )
124 SdrObjListIter aSourceIter( *pSourcePage, IM_DEEPWITHGROUPS );
125 SdrObjListIter aTargetIter( *pTargetPage, IM_DEEPWITHGROUPS );
127 while( aSourceIter.IsMore() && aTargetIter.IsMore() )
129 SdrObject* pSource = aSourceIter.Next();
130 SdrObject* pTarget = aTargetIter.Next();
132 if( pSource && pTarget)
134 Reference< XShape > xSource( pSource->getUnoShape(), UNO_QUERY );
135 Reference< XShape > xTarget( pTarget->getUnoShape(), UNO_QUERY );
136 if( xSource.is() && xTarget.is() )
138 maShapeMap[xSource] = xTarget;
144 // create a dictionary to map source to cloned nodes
145 ::anim::create_deep_vector( xSourceNode, maSourceNodeVector );
146 ::anim::create_deep_vector( xCloneNode, maCloneNodeVector );
148 transformNode( xCloneNode );
150 return xCloneNode;
152 catch( Exception& e )
154 (void)e;
155 DBG_ERROR(
156 (OString("sd::CustomAnimationClonerImpl::Clone(), "
157 "exception caught: ") +
158 rtl::OUStringToOString(
159 comphelper::anyToString( cppu::getCaughtException() ),
160 RTL_TEXTENCODING_UTF8 )).getStr() );
162 Reference< XAnimationNode > xEmpty;
163 return xEmpty;
167 void CustomAnimationClonerImpl::transformNode( const Reference< XAnimationNode >& xNode )
171 xNode->setBegin( transformValue( xNode->getBegin() ) );
172 xNode->setEnd( transformValue( xNode->getEnd() ) );
174 sal_Int16 nNodeType( xNode->getType() );
175 switch( nNodeType )
177 case AnimationNodeType::ITERATE:
179 Reference< XIterateContainer > xIter( xNode, UNO_QUERY_THROW );
180 xIter->setTarget( transformValue( xIter->getTarget() ) );
182 // its intended that here is no break!
183 case AnimationNodeType::PAR:
184 case AnimationNodeType::SEQ:
186 Reference< XEnumerationAccess > xEnumerationAccess( xNode, UNO_QUERY_THROW );
187 Reference< XEnumeration > xEnumeration( xEnumerationAccess->createEnumeration(), UNO_QUERY_THROW );
188 while( xEnumeration->hasMoreElements() )
190 Reference< XAnimationNode > xChildNode( xEnumeration->nextElement(), UNO_QUERY_THROW );
191 transformNode( xChildNode );
194 break;
196 case AnimationNodeType::ANIMATE:
197 case AnimationNodeType::SET:
198 case AnimationNodeType::ANIMATEMOTION:
199 case AnimationNodeType::ANIMATECOLOR:
200 case AnimationNodeType::ANIMATETRANSFORM:
201 case AnimationNodeType::TRANSITIONFILTER:
203 Reference< XAnimate > xAnimate( xNode, UNO_QUERY_THROW );
204 xAnimate->setTarget( transformValue( xAnimate->getTarget() ) );
206 break;
208 case AnimationNodeType::COMMAND:
210 Reference< XCommand > xCommand( xNode, UNO_QUERY_THROW );
211 xCommand->setTarget( transformValue( xCommand->getTarget() ) );
213 break;
215 case AnimationNodeType::AUDIO:
217 Reference< XAudio > xAudio( xNode, UNO_QUERY_THROW );
218 xAudio->setSource( transformValue( xAudio->getSource() ) );
220 break;
223 Sequence< NamedValue > aUserData( xNode->getUserData() );
224 if( aUserData.hasElements() )
226 NamedValue* pValue = aUserData.getArray();
227 const sal_Int32 nLength = aUserData.getLength();
228 sal_Int32 nElement;
229 for( nElement = 0; nElement < nLength; nElement++, pValue++ )
231 pValue->Value = transformValue( pValue->Value );
234 xNode->setUserData( aUserData );
237 catch( Exception& e )
239 (void)e;
240 DBG_ERROR(
241 (OString("sd::CustomAnimationClonerImpl::transformNode(), "
242 "exception caught: ") +
243 rtl::OUStringToOString(
244 comphelper::anyToString( cppu::getCaughtException() ),
245 RTL_TEXTENCODING_UTF8 )).getStr() );
249 Any CustomAnimationClonerImpl::transformValue( const Any& rValue )
251 if( rValue.hasValue() ) try
253 if( rValue.getValueType() == ::getCppuType((const ValuePair*)0) )
255 ValuePair aValuePair;
256 rValue >>= aValuePair;
258 aValuePair.First = transformValue( aValuePair.First );
259 aValuePair.Second = transformValue( aValuePair.Second );
261 return makeAny( aValuePair );
263 else if( rValue.getValueType() == ::getCppuType((Sequence<Any>*)0) )
265 Sequence<Any> aSequence;
266 rValue >>= aSequence;
268 const sal_Int32 nLength = aSequence.getLength();
269 sal_Int32 nElement;
270 Any* pAny = aSequence.getArray();
272 for( nElement = 0; nElement < nLength; nElement++, pAny++ )
273 *pAny = transformValue( *pAny );
275 return makeAny( aSequence );
277 else if( rValue.getValueTypeClass() == TypeClass_INTERFACE )
279 Reference< XShape > xShape;
280 rValue >>= xShape;
281 if( xShape.is() )
283 return makeAny( getClonedShape( xShape ) );
285 else
287 Reference< XAnimationNode > xNode;
288 rValue >>= xNode;
289 if( xNode.is() )
290 return makeAny( getClonedNode( xNode ) );
293 else if( rValue.getValueType() == ::getCppuType((const ParagraphTarget*)0) )
295 ParagraphTarget aParaTarget;
296 rValue >>= aParaTarget;
298 aParaTarget.Shape = getClonedShape( aParaTarget.Shape );
300 return makeAny( aParaTarget );
302 else if( rValue.getValueType() == ::getCppuType((const Event*)0) )
304 Event aEvent;
305 rValue >>= aEvent;
307 aEvent.Source = transformValue( aEvent.Source );
309 return makeAny( aEvent );
312 catch( Exception& e )
314 (void)e;
315 DBG_ERROR(
316 (OString("sd::CustomAnimationClonerImpl::transformValue(), "
317 "exception caught: ") +
318 rtl::OUStringToOString(
319 comphelper::anyToString( cppu::getCaughtException() ),
320 RTL_TEXTENCODING_UTF8 )).getStr() );
323 return rValue;
326 Reference< XShape > CustomAnimationClonerImpl::getClonedShape( const Reference< XShape >& xSource ) const
328 if( xSource.is() )
330 if( maShapeMap.find(xSource) != maShapeMap.end() )
332 return maShapeMap[xSource];
335 DBG_ASSERT( maShapeMap.empty(), "sd::CustomAnimationClonerImpl::getClonedShape() failed!" );
337 return xSource;
340 Reference< XAnimationNode > CustomAnimationClonerImpl::getClonedNode( const Reference< XAnimationNode >& xSource ) const
342 sal_Int32 nNode, nNodeCount = maSourceNodeVector.size();
344 for( nNode = 0; nNode < nNodeCount; nNode++ )
346 if( maSourceNodeVector[nNode] == xSource )
347 return maCloneNodeVector[nNode];
350 DBG_ERROR( "sd::CustomAnimationClonerImpl::getClonedNode() failed!" );
351 return xSource;