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 <tools/debug.hxx>
36 #include <tools/diagnose_ex.h>
37 #include <animations/animationnodehelper.hxx>
39 #include <svx/svditer.hxx>
41 #include <CustomAnimationCloner.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
;
56 class CustomAnimationClonerImpl
59 CustomAnimationClonerImpl();
60 Reference
< XAnimationNode
> Clone( const Reference
< XAnimationNode
>& xSourceNode
, const SdPage
* pSource
, const SdPage
* pTarget
);
63 void transformNode( const Reference
< XAnimationNode
>& xNode
);
64 Any
transformValue( const Any
& rValue
);
66 Reference
< XShape
> getClonedShape( const Reference
< XShape
>& xSource
) const;
67 Reference
< XAnimationNode
> getClonedNode( const Reference
< XAnimationNode
>& xSource
) const;
69 mutable ::std::map
< Reference
< XShape
>, Reference
< XShape
> > maShapeMap
;
70 std::vector
< Reference
< XAnimationNode
> > maSourceNodeVector
;
71 std::vector
< Reference
< XAnimationNode
> > maCloneNodeVector
;
76 CustomAnimationClonerImpl::CustomAnimationClonerImpl()
80 Reference
< XAnimationNode
> Clone( const Reference
< XAnimationNode
>& xSourceNode
, const SdPage
* pSource
, const SdPage
* pTarget
)
82 CustomAnimationClonerImpl aCloner
;
83 return aCloner
.Clone( xSourceNode
, pSource
, pTarget
);
86 Reference
< XAnimationNode
> CustomAnimationClonerImpl::Clone( const Reference
< XAnimationNode
>& xSourceNode
, const SdPage
* pSourcePage
, const SdPage
* pTargetPage
)
90 // clone animation hierarchy
91 Reference
< css::util::XCloneable
> xClonable( xSourceNode
, UNO_QUERY_THROW
);
92 Reference
< XAnimationNode
> xCloneNode( xClonable
->createClone(), UNO_QUERY_THROW
);
94 // create a dictionary to map source to cloned shapes
95 if( pSourcePage
&& pTargetPage
)
97 SdrObjListIter
aSourceIter( pSourcePage
, SdrIterMode::DeepWithGroups
);
98 SdrObjListIter
aTargetIter( pTargetPage
, SdrIterMode::DeepWithGroups
);
100 while( aSourceIter
.IsMore() && aTargetIter
.IsMore() )
102 SdrObject
* pSource
= aSourceIter
.Next();
103 SdrObject
* pTarget
= aTargetIter
.Next();
105 if( pSource
&& pTarget
)
107 Reference
< XShape
> xSource( pSource
->getUnoShape(), UNO_QUERY
);
108 Reference
< XShape
> xTarget( pTarget
->getUnoShape(), UNO_QUERY
);
109 if( xSource
.is() && xTarget
.is() )
111 maShapeMap
[xSource
] = xTarget
;
117 // create a dictionary to map source to cloned nodes
118 ::anim::create_deep_vector( xSourceNode
, maSourceNodeVector
);
119 ::anim::create_deep_vector( xCloneNode
, maCloneNodeVector
);
121 transformNode( xCloneNode
);
127 TOOLS_WARN_EXCEPTION( "sd", "sd::CustomAnimationClonerImpl::Clone()" );
128 Reference
< XAnimationNode
> xEmpty
;
133 void CustomAnimationClonerImpl::transformNode( const Reference
< XAnimationNode
>& xNode
)
137 xNode
->setBegin( transformValue( xNode
->getBegin() ) );
138 xNode
->setEnd( transformValue( xNode
->getEnd() ) );
140 sal_Int16
nNodeType( xNode
->getType() );
143 case AnimationNodeType::ITERATE
:
145 Reference
< XIterateContainer
> xIter( xNode
, UNO_QUERY_THROW
);
146 xIter
->setTarget( transformValue( xIter
->getTarget() ) );
149 case AnimationNodeType::PAR
:
150 case AnimationNodeType::SEQ
:
152 Reference
< XEnumerationAccess
> xEnumerationAccess( xNode
, UNO_QUERY_THROW
);
153 Reference
< XEnumeration
> xEnumeration( xEnumerationAccess
->createEnumeration(), UNO_SET_THROW
);
154 while( xEnumeration
->hasMoreElements() )
156 Reference
< XAnimationNode
> xChildNode( xEnumeration
->nextElement(), UNO_QUERY_THROW
);
157 transformNode( xChildNode
);
162 case AnimationNodeType::ANIMATE
:
163 case AnimationNodeType::SET
:
164 case AnimationNodeType::ANIMATEMOTION
:
165 case AnimationNodeType::ANIMATEPHYSICS
:
166 case AnimationNodeType::ANIMATECOLOR
:
167 case AnimationNodeType::ANIMATETRANSFORM
:
168 case AnimationNodeType::TRANSITIONFILTER
:
170 Reference
< XAnimate
> xAnimate( xNode
, UNO_QUERY_THROW
);
171 xAnimate
->setTarget( transformValue( xAnimate
->getTarget() ) );
175 case AnimationNodeType::COMMAND
:
177 Reference
< XCommand
> xCommand( xNode
, UNO_QUERY_THROW
);
178 xCommand
->setTarget( transformValue( xCommand
->getTarget() ) );
182 case AnimationNodeType::AUDIO
:
184 Reference
< XAudio
> xAudio( xNode
, UNO_QUERY_THROW
);
185 xAudio
->setSource( transformValue( xAudio
->getSource() ) );
190 Sequence
< NamedValue
> aUserData( xNode
->getUserData() );
191 if( aUserData
.hasElements() )
193 for( NamedValue
& namedValue
: asNonConstRange(aUserData
) )
195 namedValue
.Value
= transformValue( namedValue
.Value
);
198 xNode
->setUserData( aUserData
);
203 TOOLS_WARN_EXCEPTION( "sd", "sd::CustomAnimationClonerImpl::transformNode()" );
207 Any
CustomAnimationClonerImpl::transformValue( const Any
& rValue
)
209 if( rValue
.hasValue() ) try
211 if( rValue
.getValueType() == cppu::UnoType
<ValuePair
>::get() )
213 ValuePair aValuePair
;
214 rValue
>>= aValuePair
;
216 aValuePair
.First
= transformValue( aValuePair
.First
);
217 aValuePair
.Second
= transformValue( aValuePair
.Second
);
219 return makeAny( aValuePair
);
221 else if( rValue
.getValueType() == cppu::UnoType
< Sequence
<Any
> >::get() )
223 Sequence
<Any
> aSequence
;
224 rValue
>>= aSequence
;
226 for( Any
& rAny
: asNonConstRange(aSequence
) )
227 rAny
= transformValue( rAny
);
229 return makeAny( aSequence
);
231 else if( rValue
.getValueTypeClass() == TypeClass_INTERFACE
)
233 Reference
< XShape
> xShape
;
237 return makeAny( getClonedShape( xShape
) );
241 Reference
< XAnimationNode
> xNode
;
244 return makeAny( getClonedNode( xNode
) );
247 else if( rValue
.getValueType() == cppu::UnoType
<ParagraphTarget
>::get() )
249 ParagraphTarget aParaTarget
;
250 rValue
>>= aParaTarget
;
252 aParaTarget
.Shape
= getClonedShape( aParaTarget
.Shape
);
254 return makeAny( aParaTarget
);
256 else if( rValue
.getValueType() == cppu::UnoType
<Event
>::get() )
261 aEvent
.Source
= transformValue( aEvent
.Source
);
263 return makeAny( aEvent
);
268 TOOLS_WARN_EXCEPTION( "sd", "sd::CustomAnimationClonerImpl::transformValue()" );
274 Reference
< XShape
> CustomAnimationClonerImpl::getClonedShape( const Reference
< XShape
>& xSource
) const
278 if( maShapeMap
.find(xSource
) != maShapeMap
.end() )
280 return maShapeMap
[xSource
];
283 DBG_ASSERT( maShapeMap
.empty(), "sd::CustomAnimationClonerImpl::getClonedShape() failed!" );
288 Reference
< XAnimationNode
> CustomAnimationClonerImpl::getClonedNode( const Reference
< XAnimationNode
>& xSource
) const
290 std::size_t nNodeCount
= maSourceNodeVector
.size();
291 std::size_t nCloneNodeCount
= maCloneNodeVector
.size();
293 if (nNodeCount
!= nCloneNodeCount
)
294 SAL_WARN("sd.core", "Sizes of maSourceNodeVector and maCloneNodeVector mismatch!");
296 for( std::size_t nNode
= 0; nNode
< nNodeCount
&& nNode
< nCloneNodeCount
; ++nNode
)
298 if( maSourceNodeVector
[nNode
] == xSource
)
299 return maCloneNodeVector
[nNode
];
302 OSL_FAIL( "sd::CustomAnimationClonerImpl::getClonedNode() failed!" );
307 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */