xref: /trunk/main/sd/source/core/CustomAnimationCloner.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1*5b190011SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*5b190011SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*5b190011SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*5b190011SAndrew Rist  * distributed with this work for additional information
6*5b190011SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*5b190011SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*5b190011SAndrew Rist  * "License"); you may not use this file except in compliance
9*5b190011SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*5b190011SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*5b190011SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*5b190011SAndrew Rist  * software distributed under the License is distributed on an
15*5b190011SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*5b190011SAndrew Rist  * KIND, either express or implied.  See the License for the
17*5b190011SAndrew Rist  * specific language governing permissions and limitations
18*5b190011SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*5b190011SAndrew Rist  *************************************************************/
21*5b190011SAndrew Rist 
22*5b190011SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sd.hxx"
26cdf0e10cSrcweir #include <com/sun/star/animations/XAnimationNode.hpp>
27cdf0e10cSrcweir #include <com/sun/star/animations/Event.hpp>
28cdf0e10cSrcweir #ifndef _COM_SUN_STAR_ANIMATIONS_XAnimateColor_HPP_
29cdf0e10cSrcweir #include <com/sun/star/animations/XAnimateColor.hpp>
30cdf0e10cSrcweir #endif
31cdf0e10cSrcweir #ifndef _COM_SUN_STAR_ANIMATIONS_XAnimateSet_HPP_
32cdf0e10cSrcweir #include <com/sun/star/animations/XAnimateSet.hpp>
33cdf0e10cSrcweir #endif
34cdf0e10cSrcweir #include <com/sun/star/animations/XCommand.hpp>
35cdf0e10cSrcweir #ifndef _COM_SUN_STAR_ANIMATIONS_XAnimateMotion_HPP_
36cdf0e10cSrcweir #include <com/sun/star/animations/XAnimateMotion.hpp>
37cdf0e10cSrcweir #endif
38cdf0e10cSrcweir #ifndef _COM_SUN_STAR_ANIMATIONS_XAnimateTransform_HPP_
39cdf0e10cSrcweir #include <com/sun/star/animations/XAnimateTransform.hpp>
40cdf0e10cSrcweir #endif
41cdf0e10cSrcweir #ifndef _COM_SUN_STAR_ANIMATIONS_XTransitionFilter_HPP_
42cdf0e10cSrcweir #include <com/sun/star/animations/XTransitionFilter.hpp>
43cdf0e10cSrcweir #endif
44cdf0e10cSrcweir #include <com/sun/star/animations/XIterateContainer.hpp>
45cdf0e10cSrcweir #include <com/sun/star/animations/XAudio.hpp>
46cdf0e10cSrcweir #include <com/sun/star/animations/AnimationNodeType.hpp>
47cdf0e10cSrcweir #include <com/sun/star/animations/ValuePair.hpp>
48cdf0e10cSrcweir #include <com/sun/star/presentation/EffectNodeType.hpp>
49cdf0e10cSrcweir #include <com/sun/star/util/XCloneable.hpp>
50cdf0e10cSrcweir #include <com/sun/star/presentation/ParagraphTarget.hpp>
51cdf0e10cSrcweir #include <com/sun/star/container/XEnumerationAccess.hpp>
52cdf0e10cSrcweir #include <com/sun/star/beans/NamedValue.hpp>
53cdf0e10cSrcweir 
54cdf0e10cSrcweir #include <map>
55cdf0e10cSrcweir 
56cdf0e10cSrcweir #include "comphelper/anytostring.hxx"
57cdf0e10cSrcweir #include "cppuhelper/exc_hlp.hxx"
58cdf0e10cSrcweir #include "rtl/ref.hxx"
59cdf0e10cSrcweir #include <animations/animationnodehelper.hxx>
60cdf0e10cSrcweir 
61cdf0e10cSrcweir // header for class SdrObjListIter
62cdf0e10cSrcweir #include <svx/svditer.hxx>
63cdf0e10cSrcweir 
64cdf0e10cSrcweir #include "sdpage.hxx"
65cdf0e10cSrcweir 
66cdf0e10cSrcweir using namespace ::com::sun::star::uno;
67cdf0e10cSrcweir using namespace ::com::sun::star::animations;
68cdf0e10cSrcweir using namespace ::com::sun::star::presentation;
69cdf0e10cSrcweir using namespace ::com::sun::star::container;
70cdf0e10cSrcweir 
71cdf0e10cSrcweir using ::rtl::OUString;
72cdf0e10cSrcweir using ::rtl::OString;
73cdf0e10cSrcweir using ::com::sun::star::drawing::XShape;
74cdf0e10cSrcweir using ::com::sun::star::beans::NamedValue;
75cdf0e10cSrcweir 
76cdf0e10cSrcweir namespace sd
77cdf0e10cSrcweir {
78cdf0e10cSrcweir     class CustomAnimationClonerImpl
79cdf0e10cSrcweir     {
80cdf0e10cSrcweir     public:
81cdf0e10cSrcweir         CustomAnimationClonerImpl();
82cdf0e10cSrcweir         Reference< XAnimationNode > Clone( const Reference< XAnimationNode >& xSourceNode, const SdPage* pSource = 0, const SdPage* pTarget = 0 );
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     private:
85cdf0e10cSrcweir         void transformNode( const Reference< XAnimationNode >& xNode );
86cdf0e10cSrcweir         Any transformValue( const Any& rValue );
87cdf0e10cSrcweir 
88cdf0e10cSrcweir         Reference< XShape > getClonedShape( const Reference< XShape >& xSource ) const;
89cdf0e10cSrcweir         Reference< XAnimationNode > getClonedNode( const Reference< XAnimationNode >& xSource ) const;
90cdf0e10cSrcweir 
91cdf0e10cSrcweir         mutable ::std::map< Reference< XShape >, Reference< XShape > > maShapeMap;
92cdf0e10cSrcweir         std::vector< Reference< XAnimationNode > > maSourceNodeVector;
93cdf0e10cSrcweir         std::vector< Reference< XAnimationNode > > maCloneNodeVector;
94cdf0e10cSrcweir     };
95cdf0e10cSrcweir 
CustomAnimationClonerImpl()96cdf0e10cSrcweir     CustomAnimationClonerImpl::CustomAnimationClonerImpl()
97cdf0e10cSrcweir     {
98cdf0e10cSrcweir     }
99cdf0e10cSrcweir 
Clone(const Reference<XAnimationNode> & xSourceNode,const SdPage * pSource,const SdPage * pTarget)100cdf0e10cSrcweir     Reference< XAnimationNode > Clone( const Reference< XAnimationNode >& xSourceNode, const SdPage* pSource, const SdPage* pTarget )
101cdf0e10cSrcweir     {
102cdf0e10cSrcweir         CustomAnimationClonerImpl aCloner;
103cdf0e10cSrcweir         return aCloner.Clone( xSourceNode, pSource, pTarget );
104cdf0e10cSrcweir     }
105cdf0e10cSrcweir 
Clone(const Reference<XAnimationNode> & xSourceNode,const SdPage * pSourcePage,const SdPage * pTargetPage)106cdf0e10cSrcweir     Reference< XAnimationNode > CustomAnimationClonerImpl::Clone( const Reference< XAnimationNode >& xSourceNode, const SdPage* pSourcePage, const SdPage* pTargetPage )
107cdf0e10cSrcweir     {
108cdf0e10cSrcweir         try
109cdf0e10cSrcweir         {
110cdf0e10cSrcweir             // clone animation hierarchie
111cdf0e10cSrcweir             Reference< ::com::sun::star::util::XCloneable > xClonable( xSourceNode, UNO_QUERY_THROW );
112cdf0e10cSrcweir             Reference< XAnimationNode > xCloneNode( xClonable->createClone(), UNO_QUERY_THROW );
113cdf0e10cSrcweir 
114cdf0e10cSrcweir             // create a dictionary to map source to cloned shapes
115cdf0e10cSrcweir             if( pSourcePage && pTargetPage )
116cdf0e10cSrcweir             {
117cdf0e10cSrcweir                 SdrObjListIter aSourceIter( *pSourcePage, IM_DEEPWITHGROUPS );
118cdf0e10cSrcweir                 SdrObjListIter aTargetIter( *pTargetPage, IM_DEEPWITHGROUPS );
119cdf0e10cSrcweir 
120cdf0e10cSrcweir                 while( aSourceIter.IsMore() && aTargetIter.IsMore() )
121cdf0e10cSrcweir                 {
122cdf0e10cSrcweir                     SdrObject* pSource = aSourceIter.Next();
123cdf0e10cSrcweir                     SdrObject* pTarget = aTargetIter.Next();
124cdf0e10cSrcweir 
125cdf0e10cSrcweir                     if( pSource && pTarget)
126cdf0e10cSrcweir                     {
127cdf0e10cSrcweir                         Reference< XShape > xSource( pSource->getUnoShape(), UNO_QUERY );
128cdf0e10cSrcweir                         Reference< XShape > xTarget( pTarget->getUnoShape(), UNO_QUERY );
129cdf0e10cSrcweir                         if( xSource.is() && xTarget.is() )
130cdf0e10cSrcweir                         {
131cdf0e10cSrcweir                             maShapeMap[xSource] = xTarget;
132cdf0e10cSrcweir                         }
133cdf0e10cSrcweir                     }
134cdf0e10cSrcweir                 }
135cdf0e10cSrcweir             }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir             // create a dictionary to map source to cloned nodes
138cdf0e10cSrcweir             ::anim::create_deep_vector( xSourceNode, maSourceNodeVector );
139cdf0e10cSrcweir             ::anim::create_deep_vector( xCloneNode, maCloneNodeVector );
140cdf0e10cSrcweir 
141cdf0e10cSrcweir             transformNode( xCloneNode );
142cdf0e10cSrcweir 
143cdf0e10cSrcweir             return xCloneNode;
144cdf0e10cSrcweir         }
145cdf0e10cSrcweir         catch( Exception& e )
146cdf0e10cSrcweir         {
147cdf0e10cSrcweir             (void)e;
148cdf0e10cSrcweir             DBG_ERROR(
149cdf0e10cSrcweir                 (OString("sd::CustomAnimationClonerImpl::Clone(), "
150cdf0e10cSrcweir                         "exception caught: ") +
151cdf0e10cSrcweir                 rtl::OUStringToOString(
152cdf0e10cSrcweir                     comphelper::anyToString( cppu::getCaughtException() ),
153cdf0e10cSrcweir                     RTL_TEXTENCODING_UTF8 )).getStr() );
154cdf0e10cSrcweir 
155cdf0e10cSrcweir             Reference< XAnimationNode > xEmpty;
156cdf0e10cSrcweir             return xEmpty;
157cdf0e10cSrcweir         }
158cdf0e10cSrcweir     }
159cdf0e10cSrcweir 
transformNode(const Reference<XAnimationNode> & xNode)160cdf0e10cSrcweir     void CustomAnimationClonerImpl::transformNode( const Reference< XAnimationNode >& xNode )
161cdf0e10cSrcweir     {
162cdf0e10cSrcweir         try
163cdf0e10cSrcweir         {
164cdf0e10cSrcweir             xNode->setBegin( transformValue( xNode->getBegin() ) );
165cdf0e10cSrcweir             xNode->setEnd( transformValue( xNode->getEnd() ) );
166cdf0e10cSrcweir 
167cdf0e10cSrcweir             sal_Int16 nNodeType( xNode->getType() );
168cdf0e10cSrcweir             switch( nNodeType )
169cdf0e10cSrcweir             {
170cdf0e10cSrcweir             case AnimationNodeType::ITERATE:
171cdf0e10cSrcweir             {
172cdf0e10cSrcweir                 Reference< XIterateContainer > xIter( xNode, UNO_QUERY_THROW );
173cdf0e10cSrcweir                 xIter->setTarget( transformValue( xIter->getTarget() ) );
174cdf0e10cSrcweir             }
175cdf0e10cSrcweir             // its intended that here is no break!
176cdf0e10cSrcweir             case AnimationNodeType::PAR:
177cdf0e10cSrcweir             case AnimationNodeType::SEQ:
178cdf0e10cSrcweir             {
179cdf0e10cSrcweir                 Reference< XEnumerationAccess > xEnumerationAccess( xNode, UNO_QUERY_THROW );
180cdf0e10cSrcweir                 Reference< XEnumeration > xEnumeration( xEnumerationAccess->createEnumeration(), UNO_QUERY_THROW );
181cdf0e10cSrcweir                 while( xEnumeration->hasMoreElements() )
182cdf0e10cSrcweir                 {
183cdf0e10cSrcweir                     Reference< XAnimationNode > xChildNode( xEnumeration->nextElement(), UNO_QUERY_THROW );
184cdf0e10cSrcweir                     transformNode( xChildNode );
185cdf0e10cSrcweir                 }
186cdf0e10cSrcweir             }
187cdf0e10cSrcweir             break;
188cdf0e10cSrcweir 
189cdf0e10cSrcweir             case AnimationNodeType::ANIMATE:
190cdf0e10cSrcweir             case AnimationNodeType::SET:
191cdf0e10cSrcweir             case AnimationNodeType::ANIMATEMOTION:
192cdf0e10cSrcweir             case AnimationNodeType::ANIMATECOLOR:
193cdf0e10cSrcweir             case AnimationNodeType::ANIMATETRANSFORM:
194cdf0e10cSrcweir             case AnimationNodeType::TRANSITIONFILTER:
195cdf0e10cSrcweir             {
196cdf0e10cSrcweir                 Reference< XAnimate > xAnimate( xNode, UNO_QUERY_THROW );
197cdf0e10cSrcweir                 xAnimate->setTarget( transformValue( xAnimate->getTarget() ) );
198cdf0e10cSrcweir             }
199cdf0e10cSrcweir             break;
200cdf0e10cSrcweir 
201cdf0e10cSrcweir             case AnimationNodeType::COMMAND:
202cdf0e10cSrcweir             {
203cdf0e10cSrcweir                 Reference< XCommand > xCommand( xNode, UNO_QUERY_THROW );
204cdf0e10cSrcweir                 xCommand->setTarget( transformValue( xCommand->getTarget() ) );
205cdf0e10cSrcweir             }
206cdf0e10cSrcweir             break;
207cdf0e10cSrcweir 
208cdf0e10cSrcweir             case AnimationNodeType::AUDIO:
209cdf0e10cSrcweir             {
210cdf0e10cSrcweir                 Reference< XAudio > xAudio( xNode, UNO_QUERY_THROW );
211cdf0e10cSrcweir                 xAudio->setSource( transformValue( xAudio->getSource() ) );
212cdf0e10cSrcweir             }
213cdf0e10cSrcweir             break;
214cdf0e10cSrcweir             }
215cdf0e10cSrcweir 
216cdf0e10cSrcweir             Sequence< NamedValue > aUserData( xNode->getUserData() );
217cdf0e10cSrcweir             if( aUserData.hasElements() )
218cdf0e10cSrcweir             {
219cdf0e10cSrcweir                 NamedValue* pValue = aUserData.getArray();
220cdf0e10cSrcweir                 const sal_Int32 nLength = aUserData.getLength();
221cdf0e10cSrcweir                 sal_Int32 nElement;
222cdf0e10cSrcweir                 for( nElement = 0; nElement < nLength; nElement++, pValue++ )
223cdf0e10cSrcweir                 {
224cdf0e10cSrcweir                     pValue->Value = transformValue( pValue->Value );
225cdf0e10cSrcweir                 }
226cdf0e10cSrcweir 
227cdf0e10cSrcweir                 xNode->setUserData( aUserData );
228cdf0e10cSrcweir             }
229cdf0e10cSrcweir         }
230cdf0e10cSrcweir         catch( Exception& e )
231cdf0e10cSrcweir         {
232cdf0e10cSrcweir             (void)e;
233cdf0e10cSrcweir             DBG_ERROR(
234cdf0e10cSrcweir                 (OString("sd::CustomAnimationClonerImpl::transformNode(), "
235cdf0e10cSrcweir                         "exception caught: ") +
236cdf0e10cSrcweir                 rtl::OUStringToOString(
237cdf0e10cSrcweir                     comphelper::anyToString( cppu::getCaughtException() ),
238cdf0e10cSrcweir                     RTL_TEXTENCODING_UTF8 )).getStr() );
239cdf0e10cSrcweir         }
240cdf0e10cSrcweir     }
241cdf0e10cSrcweir 
transformValue(const Any & rValue)242cdf0e10cSrcweir     Any CustomAnimationClonerImpl::transformValue( const Any& rValue )
243cdf0e10cSrcweir     {
244cdf0e10cSrcweir         if( rValue.hasValue() ) try
245cdf0e10cSrcweir         {
246cdf0e10cSrcweir             if( rValue.getValueType() == ::getCppuType((const ValuePair*)0) )
247cdf0e10cSrcweir             {
248cdf0e10cSrcweir                 ValuePair aValuePair;
249cdf0e10cSrcweir                 rValue >>= aValuePair;
250cdf0e10cSrcweir 
251cdf0e10cSrcweir                 aValuePair.First = transformValue( aValuePair.First );
252cdf0e10cSrcweir                 aValuePair.Second = transformValue( aValuePair.Second );
253cdf0e10cSrcweir 
254cdf0e10cSrcweir                 return makeAny( aValuePair );
255cdf0e10cSrcweir             }
256cdf0e10cSrcweir             else if( rValue.getValueType() == ::getCppuType((Sequence<Any>*)0) )
257cdf0e10cSrcweir             {
258cdf0e10cSrcweir                 Sequence<Any> aSequence;
259cdf0e10cSrcweir                 rValue >>= aSequence;
260cdf0e10cSrcweir 
261cdf0e10cSrcweir                 const sal_Int32 nLength = aSequence.getLength();
262cdf0e10cSrcweir                 sal_Int32 nElement;
263cdf0e10cSrcweir                 Any* pAny = aSequence.getArray();
264cdf0e10cSrcweir 
265cdf0e10cSrcweir                 for( nElement = 0; nElement < nLength; nElement++, pAny++ )
266cdf0e10cSrcweir                     *pAny = transformValue( *pAny );
267cdf0e10cSrcweir 
268cdf0e10cSrcweir                 return makeAny( aSequence );
269cdf0e10cSrcweir             }
270cdf0e10cSrcweir             else if( rValue.getValueTypeClass() == TypeClass_INTERFACE )
271cdf0e10cSrcweir             {
272cdf0e10cSrcweir                 Reference< XShape > xShape;
273cdf0e10cSrcweir                 rValue >>= xShape;
274cdf0e10cSrcweir                 if( xShape.is() )
275cdf0e10cSrcweir                 {
276cdf0e10cSrcweir                     return makeAny( getClonedShape( xShape ) );
277cdf0e10cSrcweir                 }
278cdf0e10cSrcweir                 else
279cdf0e10cSrcweir                 {
280cdf0e10cSrcweir                     Reference< XAnimationNode > xNode;
281cdf0e10cSrcweir                     rValue >>= xNode;
282cdf0e10cSrcweir                     if( xNode.is() )
283cdf0e10cSrcweir                         return makeAny( getClonedNode( xNode ) );
284cdf0e10cSrcweir                 }
285cdf0e10cSrcweir             }
286cdf0e10cSrcweir             else if( rValue.getValueType() == ::getCppuType((const ParagraphTarget*)0) )
287cdf0e10cSrcweir             {
288cdf0e10cSrcweir                 ParagraphTarget aParaTarget;
289cdf0e10cSrcweir                 rValue >>= aParaTarget;
290cdf0e10cSrcweir 
291cdf0e10cSrcweir                 aParaTarget.Shape = getClonedShape( aParaTarget.Shape );
292cdf0e10cSrcweir 
293cdf0e10cSrcweir                 return makeAny( aParaTarget );
294cdf0e10cSrcweir             }
295cdf0e10cSrcweir             else if( rValue.getValueType() == ::getCppuType((const Event*)0) )
296cdf0e10cSrcweir             {
297cdf0e10cSrcweir                 Event aEvent;
298cdf0e10cSrcweir                 rValue >>= aEvent;
299cdf0e10cSrcweir 
300cdf0e10cSrcweir                 aEvent.Source = transformValue( aEvent.Source );
301cdf0e10cSrcweir 
302cdf0e10cSrcweir                 return makeAny( aEvent );
303cdf0e10cSrcweir             }
304cdf0e10cSrcweir         }
305cdf0e10cSrcweir         catch( Exception& e )
306cdf0e10cSrcweir         {
307cdf0e10cSrcweir             (void)e;
308cdf0e10cSrcweir             DBG_ERROR(
309cdf0e10cSrcweir                 (OString("sd::CustomAnimationClonerImpl::transformValue(), "
310cdf0e10cSrcweir                         "exception caught: ") +
311cdf0e10cSrcweir                 rtl::OUStringToOString(
312cdf0e10cSrcweir                     comphelper::anyToString( cppu::getCaughtException() ),
313cdf0e10cSrcweir                     RTL_TEXTENCODING_UTF8 )).getStr() );
314cdf0e10cSrcweir         }
315cdf0e10cSrcweir 
316cdf0e10cSrcweir         return rValue;
317cdf0e10cSrcweir     }
318cdf0e10cSrcweir 
getClonedShape(const Reference<XShape> & xSource) const319cdf0e10cSrcweir     Reference< XShape > CustomAnimationClonerImpl::getClonedShape( const Reference< XShape >& xSource ) const
320cdf0e10cSrcweir     {
321cdf0e10cSrcweir         if( xSource.is() )
322cdf0e10cSrcweir         {
323cdf0e10cSrcweir             if( maShapeMap.find(xSource) != maShapeMap.end() )
324cdf0e10cSrcweir             {
325cdf0e10cSrcweir                 return maShapeMap[xSource];
326cdf0e10cSrcweir             }
327cdf0e10cSrcweir 
328cdf0e10cSrcweir             DBG_ASSERT( maShapeMap.empty(), "sd::CustomAnimationClonerImpl::getClonedShape() failed!" );
329cdf0e10cSrcweir         }
330cdf0e10cSrcweir         return xSource;
331cdf0e10cSrcweir     }
332cdf0e10cSrcweir 
getClonedNode(const Reference<XAnimationNode> & xSource) const333cdf0e10cSrcweir     Reference< XAnimationNode > CustomAnimationClonerImpl::getClonedNode( const Reference< XAnimationNode >& xSource ) const
334cdf0e10cSrcweir     {
335cdf0e10cSrcweir         sal_Int32 nNode, nNodeCount = maSourceNodeVector.size();
336cdf0e10cSrcweir 
337cdf0e10cSrcweir         for( nNode = 0; nNode < nNodeCount; nNode++ )
338cdf0e10cSrcweir         {
339cdf0e10cSrcweir             if( maSourceNodeVector[nNode] == xSource )
340cdf0e10cSrcweir                 return maCloneNodeVector[nNode];
341cdf0e10cSrcweir         }
342cdf0e10cSrcweir 
343cdf0e10cSrcweir         DBG_ERROR( "sd::CustomAnimationClonerImpl::getClonedNode() failed!" );
344cdf0e10cSrcweir         return xSource;
345cdf0e10cSrcweir     }
346cdf0e10cSrcweir }
347