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/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>
35 #include <comphelper/anytostring.hxx>
36 #include <cppuhelper/exc_hlp.hxx>
37 #include <rtl/ref.hxx>
38 #include <animations/animationnodehelper.hxx>
40 #include <svx/svditer.hxx>
42 #include <CustomAnimationCloner.hxx>
45 using namespace ::com::sun::star::uno
;
46 using namespace ::com::sun::star::animations
;
47 using namespace ::com::sun::star::presentation
;
48 using namespace ::com::sun::star::container
;
50 using ::com::sun::star::drawing::XShape
;
51 using ::com::sun::star::beans::NamedValue
;
55 class CustomAnimationClonerImpl
58 CustomAnimationClonerImpl();
59 Reference
< XAnimationNode
> Clone( const Reference
< XAnimationNode
>& xSourceNode
, const SdPage
* pSource
, const SdPage
* pTarget
);
62 void transformNode( const Reference
< XAnimationNode
>& xNode
);
63 Any
transformValue( const Any
& rValue
);
65 Reference
< XShape
> getClonedShape( const Reference
< XShape
>& xSource
) const;
66 Reference
< XAnimationNode
> getClonedNode( const Reference
< XAnimationNode
>& xSource
) const;
68 mutable ::std::map
< Reference
< XShape
>, Reference
< XShape
> > maShapeMap
;
69 std::vector
< Reference
< XAnimationNode
> > maSourceNodeVector
;
70 std::vector
< Reference
< XAnimationNode
> > maCloneNodeVector
;
73 CustomAnimationClonerImpl::CustomAnimationClonerImpl()
77 Reference
< XAnimationNode
> Clone( const Reference
< XAnimationNode
>& xSourceNode
, const SdPage
* pSource
, const SdPage
* pTarget
)
79 CustomAnimationClonerImpl aCloner
;
80 return aCloner
.Clone( xSourceNode
, pSource
, pTarget
);
83 Reference
< XAnimationNode
> CustomAnimationClonerImpl::Clone( const Reference
< XAnimationNode
>& xSourceNode
, const SdPage
* pSourcePage
, const SdPage
* pTargetPage
)
87 // clone animation hierarchy
88 Reference
< css::util::XCloneable
> xClonable( xSourceNode
, UNO_QUERY_THROW
);
89 Reference
< XAnimationNode
> xCloneNode( xClonable
->createClone(), UNO_QUERY_THROW
);
91 // create a dictionary to map source to cloned shapes
92 if( pSourcePage
&& pTargetPage
)
94 SdrObjListIter
aSourceIter( *pSourcePage
, SdrIterMode::DeepWithGroups
);
95 SdrObjListIter
aTargetIter( *pTargetPage
, SdrIterMode::DeepWithGroups
);
97 while( aSourceIter
.IsMore() && aTargetIter
.IsMore() )
99 SdrObject
* pSource
= aSourceIter
.Next();
100 SdrObject
* pTarget
= aTargetIter
.Next();
102 if( pSource
&& pTarget
)
104 Reference
< XShape
> xSource( pSource
->getUnoShape(), UNO_QUERY
);
105 Reference
< XShape
> xTarget( pTarget
->getUnoShape(), UNO_QUERY
);
106 if( xSource
.is() && xTarget
.is() )
108 maShapeMap
[xSource
] = xTarget
;
114 // create a dictionary to map source to cloned nodes
115 ::anim::create_deep_vector( xSourceNode
, maSourceNodeVector
);
116 ::anim::create_deep_vector( xCloneNode
, maCloneNodeVector
);
118 transformNode( xCloneNode
);
124 SAL_WARN( "sd", "sd::CustomAnimationClonerImpl::Clone(), "
125 "exception caught: " << comphelper::anyToString( cppu::getCaughtException() ) );
126 Reference
< XAnimationNode
> xEmpty
;
131 void CustomAnimationClonerImpl::transformNode( const Reference
< XAnimationNode
>& xNode
)
135 xNode
->setBegin( transformValue( xNode
->getBegin() ) );
136 xNode
->setEnd( transformValue( xNode
->getEnd() ) );
138 sal_Int16
nNodeType( xNode
->getType() );
141 case AnimationNodeType::ITERATE
:
143 Reference
< XIterateContainer
> xIter( xNode
, UNO_QUERY_THROW
);
144 xIter
->setTarget( transformValue( xIter
->getTarget() ) );
147 case AnimationNodeType::PAR
:
148 case AnimationNodeType::SEQ
:
150 Reference
< XEnumerationAccess
> xEnumerationAccess( xNode
, UNO_QUERY_THROW
);
151 Reference
< XEnumeration
> xEnumeration( xEnumerationAccess
->createEnumeration(), UNO_QUERY_THROW
);
152 while( xEnumeration
->hasMoreElements() )
154 Reference
< XAnimationNode
> xChildNode( xEnumeration
->nextElement(), UNO_QUERY_THROW
);
155 transformNode( xChildNode
);
160 case AnimationNodeType::ANIMATE
:
161 case AnimationNodeType::SET
:
162 case AnimationNodeType::ANIMATEMOTION
:
163 case AnimationNodeType::ANIMATECOLOR
:
164 case AnimationNodeType::ANIMATETRANSFORM
:
165 case AnimationNodeType::TRANSITIONFILTER
:
167 Reference
< XAnimate
> xAnimate( xNode
, UNO_QUERY_THROW
);
168 xAnimate
->setTarget( transformValue( xAnimate
->getTarget() ) );
172 case AnimationNodeType::COMMAND
:
174 Reference
< XCommand
> xCommand( xNode
, UNO_QUERY_THROW
);
175 xCommand
->setTarget( transformValue( xCommand
->getTarget() ) );
179 case AnimationNodeType::AUDIO
:
181 Reference
< XAudio
> xAudio( xNode
, UNO_QUERY_THROW
);
182 xAudio
->setSource( transformValue( xAudio
->getSource() ) );
187 Sequence
< NamedValue
> aUserData( xNode
->getUserData() );
188 if( aUserData
.hasElements() )
190 for( NamedValue
& namedValue
: aUserData
)
192 namedValue
.Value
= transformValue( namedValue
.Value
);
195 xNode
->setUserData( aUserData
);
200 SAL_WARN( "sd", "sd::CustomAnimationClonerImpl::transformNode(), "
202 << comphelper::anyToString( cppu::getCaughtException() ) );
206 Any
CustomAnimationClonerImpl::transformValue( const Any
& rValue
)
208 if( rValue
.hasValue() ) try
210 if( rValue
.getValueType() == cppu::UnoType
<ValuePair
>::get() )
212 ValuePair aValuePair
;
213 rValue
>>= aValuePair
;
215 aValuePair
.First
= transformValue( aValuePair
.First
);
216 aValuePair
.Second
= transformValue( aValuePair
.Second
);
218 return makeAny( aValuePair
);
220 else if( rValue
.getValueType() == cppu::UnoType
< Sequence
<Any
> >::get() )
222 Sequence
<Any
> aSequence
;
223 rValue
>>= aSequence
;
225 const sal_Int32 nLength
= aSequence
.getLength();
227 Any
* pAny
= aSequence
.getArray();
229 for( nElement
= 0; nElement
< nLength
; nElement
++, pAny
++ )
230 *pAny
= transformValue( *pAny
);
232 return makeAny( aSequence
);
234 else if( rValue
.getValueTypeClass() == TypeClass_INTERFACE
)
236 Reference
< XShape
> xShape
;
240 return makeAny( getClonedShape( xShape
) );
244 Reference
< XAnimationNode
> xNode
;
247 return makeAny( getClonedNode( xNode
) );
250 else if( rValue
.getValueType() == cppu::UnoType
<ParagraphTarget
>::get() )
252 ParagraphTarget aParaTarget
;
253 rValue
>>= aParaTarget
;
255 aParaTarget
.Shape
= getClonedShape( aParaTarget
.Shape
);
257 return makeAny( aParaTarget
);
259 else if( rValue
.getValueType() == cppu::UnoType
<Event
>::get() )
264 aEvent
.Source
= transformValue( aEvent
.Source
);
266 return makeAny( aEvent
);
271 SAL_WARN( "sd", "sd::CustomAnimationClonerImpl::transformValue(), "
273 << comphelper::anyToString( cppu::getCaughtException() ) );
279 Reference
< XShape
> CustomAnimationClonerImpl::getClonedShape( const Reference
< XShape
>& xSource
) const
283 if( maShapeMap
.find(xSource
) != maShapeMap
.end() )
285 return maShapeMap
[xSource
];
288 DBG_ASSERT( maShapeMap
.empty(), "sd::CustomAnimationClonerImpl::getClonedShape() failed!" );
293 Reference
< XAnimationNode
> CustomAnimationClonerImpl::getClonedNode( const Reference
< XAnimationNode
>& xSource
) const
295 sal_Int32 nNode
, nNodeCount
= maSourceNodeVector
.size();
297 for( nNode
= 0; nNode
< nNodeCount
; nNode
++ )
299 if( maSourceNodeVector
[nNode
] == xSource
)
300 return maCloneNodeVector
[nNode
];
303 OSL_FAIL( "sd::CustomAnimationClonerImpl::getClonedNode() failed!" );
308 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */