1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/XAnimationNode.hpp>
21 #include <com/sun/star/animations/Event.hpp>
22 #include <com/sun/star/animations/XAnimateColor.hpp>
23 #include <com/sun/star/animations/XAnimateSet.hpp>
24 #include <com/sun/star/animations/XCommand.hpp>
25 #include <com/sun/star/animations/XAnimateMotion.hpp>
26 #include <com/sun/star/animations/XAnimateTransform.hpp>
27 #include <com/sun/star/animations/XTransitionFilter.hpp>
28 #include <com/sun/star/animations/XIterateContainer.hpp>
29 #include <com/sun/star/animations/XAudio.hpp>
30 #include <com/sun/star/animations/AnimationNodeType.hpp>
31 #include <com/sun/star/animations/ValuePair.hpp>
32 #include <com/sun/star/presentation/EffectNodeType.hpp>
33 #include <com/sun/star/util/XCloneable.hpp>
34 #include <com/sun/star/presentation/ParagraphTarget.hpp>
35 #include <com/sun/star/container/XEnumerationAccess.hpp>
36 #include <com/sun/star/beans/NamedValue.hpp>
40 #include "comphelper/anytostring.hxx"
41 #include "cppuhelper/exc_hlp.hxx"
42 #include "rtl/ref.hxx"
43 #include <animations/animationnodehelper.hxx>
45 #include <svx/svditer.hxx>
47 #include "CustomAnimationCloner.hxx"
50 using namespace ::com::sun::star::uno
;
51 using namespace ::com::sun::star::animations
;
52 using namespace ::com::sun::star::presentation
;
53 using namespace ::com::sun::star::container
;
55 using ::com::sun::star::drawing::XShape
;
56 using ::com::sun::star::beans::NamedValue
;
60 class CustomAnimationClonerImpl
63 CustomAnimationClonerImpl();
64 Reference
< XAnimationNode
> Clone( const Reference
< XAnimationNode
>& xSourceNode
, const SdPage
* pSource
= 0, const SdPage
* pTarget
= 0 );
67 void transformNode( const Reference
< XAnimationNode
>& xNode
);
68 Any
transformValue( const Any
& rValue
);
70 Reference
< XShape
> getClonedShape( const Reference
< XShape
>& xSource
) const;
71 Reference
< XAnimationNode
> getClonedNode( const Reference
< XAnimationNode
>& xSource
) const;
73 mutable ::std::map
< Reference
< XShape
>, Reference
< XShape
> > maShapeMap
;
74 std::vector
< Reference
< XAnimationNode
> > maSourceNodeVector
;
75 std::vector
< Reference
< XAnimationNode
> > maCloneNodeVector
;
78 CustomAnimationClonerImpl::CustomAnimationClonerImpl()
82 Reference
< XAnimationNode
> Clone( const Reference
< XAnimationNode
>& xSourceNode
, const SdPage
* pSource
, const SdPage
* pTarget
)
84 CustomAnimationClonerImpl aCloner
;
85 return aCloner
.Clone( xSourceNode
, pSource
, pTarget
);
88 Reference
< XAnimationNode
> CustomAnimationClonerImpl::Clone( const Reference
< XAnimationNode
>& xSourceNode
, const SdPage
* pSourcePage
, const SdPage
* pTargetPage
)
92 // clone animation hierarchy
93 Reference
< ::com::sun::star::util::XCloneable
> xClonable( xSourceNode
, UNO_QUERY_THROW
);
94 Reference
< XAnimationNode
> xCloneNode( xClonable
->createClone(), UNO_QUERY_THROW
);
96 // create a dictionary to map source to cloned shapes
97 if( pSourcePage
&& pTargetPage
)
99 SdrObjListIter
aSourceIter( *pSourcePage
, IM_DEEPWITHGROUPS
);
100 SdrObjListIter
aTargetIter( *pTargetPage
, IM_DEEPWITHGROUPS
);
102 while( aSourceIter
.IsMore() && aTargetIter
.IsMore() )
104 SdrObject
* pSource
= aSourceIter
.Next();
105 SdrObject
* pTarget
= aTargetIter
.Next();
107 if( pSource
&& pTarget
)
109 Reference
< XShape
> xSource( pSource
->getUnoShape(), UNO_QUERY
);
110 Reference
< XShape
> xTarget( pTarget
->getUnoShape(), UNO_QUERY
);
111 if( xSource
.is() && xTarget
.is() )
113 maShapeMap
[xSource
] = xTarget
;
119 // create a dictionary to map source to cloned nodes
120 ::anim::create_deep_vector( xSourceNode
, maSourceNodeVector
);
121 ::anim::create_deep_vector( xCloneNode
, maCloneNodeVector
);
123 transformNode( xCloneNode
);
130 OString(OString("sd::CustomAnimationClonerImpl::Clone(), "
131 "exception caught: ") +
133 comphelper::anyToString( cppu::getCaughtException() ),
134 RTL_TEXTENCODING_UTF8
)).getStr() );
136 Reference
< XAnimationNode
> xEmpty
;
141 void CustomAnimationClonerImpl::transformNode( const Reference
< XAnimationNode
>& xNode
)
145 xNode
->setBegin( transformValue( xNode
->getBegin() ) );
146 xNode
->setEnd( transformValue( xNode
->getEnd() ) );
148 sal_Int16
nNodeType( xNode
->getType() );
151 case AnimationNodeType::ITERATE
:
153 Reference
< XIterateContainer
> xIter( xNode
, UNO_QUERY_THROW
);
154 xIter
->setTarget( transformValue( xIter
->getTarget() ) );
156 // its intended that here is no break!
157 case AnimationNodeType::PAR
:
158 case AnimationNodeType::SEQ
:
160 Reference
< XEnumerationAccess
> xEnumerationAccess( xNode
, UNO_QUERY_THROW
);
161 Reference
< XEnumeration
> xEnumeration( xEnumerationAccess
->createEnumeration(), UNO_QUERY_THROW
);
162 while( xEnumeration
->hasMoreElements() )
164 Reference
< XAnimationNode
> xChildNode( xEnumeration
->nextElement(), UNO_QUERY_THROW
);
165 transformNode( xChildNode
);
170 case AnimationNodeType::ANIMATE
:
171 case AnimationNodeType::SET
:
172 case AnimationNodeType::ANIMATEMOTION
:
173 case AnimationNodeType::ANIMATECOLOR
:
174 case AnimationNodeType::ANIMATETRANSFORM
:
175 case AnimationNodeType::TRANSITIONFILTER
:
177 Reference
< XAnimate
> xAnimate( xNode
, UNO_QUERY_THROW
);
178 xAnimate
->setTarget( transformValue( xAnimate
->getTarget() ) );
182 case AnimationNodeType::COMMAND
:
184 Reference
< XCommand
> xCommand( xNode
, UNO_QUERY_THROW
);
185 xCommand
->setTarget( transformValue( xCommand
->getTarget() ) );
189 case AnimationNodeType::AUDIO
:
191 Reference
< XAudio
> xAudio( xNode
, UNO_QUERY_THROW
);
192 xAudio
->setSource( transformValue( xAudio
->getSource() ) );
197 Sequence
< NamedValue
> aUserData( xNode
->getUserData() );
198 if( aUserData
.hasElements() )
200 NamedValue
* pValue
= aUserData
.getArray();
201 const sal_Int32 nLength
= aUserData
.getLength();
203 for( nElement
= 0; nElement
< nLength
; nElement
++, pValue
++ )
205 pValue
->Value
= transformValue( pValue
->Value
);
208 xNode
->setUserData( aUserData
);
214 OString(OString("sd::CustomAnimationClonerImpl::transformNode(), "
215 "exception caught: ") +
217 comphelper::anyToString( cppu::getCaughtException() ),
218 RTL_TEXTENCODING_UTF8
)).getStr() );
222 Any
CustomAnimationClonerImpl::transformValue( const Any
& rValue
)
224 if( rValue
.hasValue() ) try
226 if( rValue
.getValueType() == cppu::UnoType
<ValuePair
>::get() )
228 ValuePair aValuePair
;
229 rValue
>>= aValuePair
;
231 aValuePair
.First
= transformValue( aValuePair
.First
);
232 aValuePair
.Second
= transformValue( aValuePair
.Second
);
234 return makeAny( aValuePair
);
236 else if( rValue
.getValueType() == cppu::UnoType
< Sequence
<Any
> >::get() )
238 Sequence
<Any
> aSequence
;
239 rValue
>>= aSequence
;
241 const sal_Int32 nLength
= aSequence
.getLength();
243 Any
* pAny
= aSequence
.getArray();
245 for( nElement
= 0; nElement
< nLength
; nElement
++, pAny
++ )
246 *pAny
= transformValue( *pAny
);
248 return makeAny( aSequence
);
250 else if( rValue
.getValueTypeClass() == TypeClass_INTERFACE
)
252 Reference
< XShape
> xShape
;
256 return makeAny( getClonedShape( xShape
) );
260 Reference
< XAnimationNode
> xNode
;
263 return makeAny( getClonedNode( xNode
) );
266 else if( rValue
.getValueType() == cppu::UnoType
<ParagraphTarget
>::get() )
268 ParagraphTarget aParaTarget
;
269 rValue
>>= aParaTarget
;
271 aParaTarget
.Shape
= getClonedShape( aParaTarget
.Shape
);
273 return makeAny( aParaTarget
);
275 else if( rValue
.getValueType() == cppu::UnoType
<Event
>::get() )
280 aEvent
.Source
= transformValue( aEvent
.Source
);
282 return makeAny( aEvent
);
288 OString(OString("sd::CustomAnimationClonerImpl::transformValue(), "
289 "exception caught: ") +
291 comphelper::anyToString( cppu::getCaughtException() ),
292 RTL_TEXTENCODING_UTF8
)).getStr() );
298 Reference
< XShape
> CustomAnimationClonerImpl::getClonedShape( const Reference
< XShape
>& xSource
) const
302 if( maShapeMap
.find(xSource
) != maShapeMap
.end() )
304 return maShapeMap
[xSource
];
307 DBG_ASSERT( maShapeMap
.empty(), "sd::CustomAnimationClonerImpl::getClonedShape() failed!" );
312 Reference
< XAnimationNode
> CustomAnimationClonerImpl::getClonedNode( const Reference
< XAnimationNode
>& xSource
) const
314 sal_Int32 nNode
, nNodeCount
= maSourceNodeVector
.size();
316 for( nNode
= 0; nNode
< nNodeCount
; nNode
++ )
318 if( maSourceNodeVector
[nNode
] == xSource
)
319 return maCloneNodeVector
[nNode
];
322 OSL_FAIL( "sd::CustomAnimationClonerImpl::getClonedNode() failed!" );
327 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */