Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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>
37 :
38 : #include <map>
39 :
40 : #include "comphelper/anytostring.hxx"
41 : #include "cppuhelper/exc_hlp.hxx"
42 : #include "rtl/ref.hxx"
43 : #include <animations/animationnodehelper.hxx>
44 :
45 : // header for class SdrObjListIter
46 : #include <svx/svditer.hxx>
47 :
48 : #include "sdpage.hxx"
49 :
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;
54 :
55 : using ::com::sun::star::drawing::XShape;
56 : using ::com::sun::star::beans::NamedValue;
57 :
58 : namespace sd
59 : {
60 0 : class CustomAnimationClonerImpl
61 : {
62 : public:
63 : CustomAnimationClonerImpl();
64 : Reference< XAnimationNode > Clone( const Reference< XAnimationNode >& xSourceNode, const SdPage* pSource = 0, const SdPage* pTarget = 0 );
65 :
66 : private:
67 : void transformNode( const Reference< XAnimationNode >& xNode );
68 : Any transformValue( const Any& rValue );
69 :
70 : Reference< XShape > getClonedShape( const Reference< XShape >& xSource ) const;
71 : Reference< XAnimationNode > getClonedNode( const Reference< XAnimationNode >& xSource ) const;
72 :
73 : mutable ::std::map< Reference< XShape >, Reference< XShape > > maShapeMap;
74 : std::vector< Reference< XAnimationNode > > maSourceNodeVector;
75 : std::vector< Reference< XAnimationNode > > maCloneNodeVector;
76 : };
77 :
78 0 : CustomAnimationClonerImpl::CustomAnimationClonerImpl()
79 : {
80 0 : }
81 :
82 0 : Reference< XAnimationNode > Clone( const Reference< XAnimationNode >& xSourceNode, const SdPage* pSource, const SdPage* pTarget )
83 : {
84 0 : CustomAnimationClonerImpl aCloner;
85 0 : return aCloner.Clone( xSourceNode, pSource, pTarget );
86 : }
87 :
88 0 : Reference< XAnimationNode > CustomAnimationClonerImpl::Clone( const Reference< XAnimationNode >& xSourceNode, const SdPage* pSourcePage, const SdPage* pTargetPage )
89 : {
90 : try
91 : {
92 : // clone animation hierarchie
93 0 : Reference< ::com::sun::star::util::XCloneable > xClonable( xSourceNode, UNO_QUERY_THROW );
94 0 : Reference< XAnimationNode > xCloneNode( xClonable->createClone(), UNO_QUERY_THROW );
95 :
96 : // create a dictionary to map source to cloned shapes
97 0 : if( pSourcePage && pTargetPage )
98 : {
99 0 : SdrObjListIter aSourceIter( *pSourcePage, IM_DEEPWITHGROUPS );
100 0 : SdrObjListIter aTargetIter( *pTargetPage, IM_DEEPWITHGROUPS );
101 :
102 0 : while( aSourceIter.IsMore() && aTargetIter.IsMore() )
103 : {
104 0 : SdrObject* pSource = aSourceIter.Next();
105 0 : SdrObject* pTarget = aTargetIter.Next();
106 :
107 0 : if( pSource && pTarget)
108 : {
109 0 : Reference< XShape > xSource( pSource->getUnoShape(), UNO_QUERY );
110 0 : Reference< XShape > xTarget( pTarget->getUnoShape(), UNO_QUERY );
111 0 : if( xSource.is() && xTarget.is() )
112 : {
113 0 : maShapeMap[xSource] = xTarget;
114 0 : }
115 : }
116 0 : }
117 : }
118 :
119 : // create a dictionary to map source to cloned nodes
120 0 : ::anim::create_deep_vector( xSourceNode, maSourceNodeVector );
121 0 : ::anim::create_deep_vector( xCloneNode, maCloneNodeVector );
122 :
123 0 : transformNode( xCloneNode );
124 :
125 0 : return xCloneNode;
126 : }
127 0 : catch( Exception& )
128 : {
129 : OSL_FAIL(
130 : OString(OString("sd::CustomAnimationClonerImpl::Clone(), "
131 : "exception caught: ") +
132 : OUStringToOString(
133 : comphelper::anyToString( cppu::getCaughtException() ),
134 : RTL_TEXTENCODING_UTF8 )).getStr() );
135 :
136 0 : Reference< XAnimationNode > xEmpty;
137 0 : return xEmpty;
138 : }
139 : }
140 :
141 0 : void CustomAnimationClonerImpl::transformNode( const Reference< XAnimationNode >& xNode )
142 : {
143 : try
144 : {
145 0 : xNode->setBegin( transformValue( xNode->getBegin() ) );
146 0 : xNode->setEnd( transformValue( xNode->getEnd() ) );
147 :
148 0 : sal_Int16 nNodeType( xNode->getType() );
149 0 : switch( nNodeType )
150 : {
151 : case AnimationNodeType::ITERATE:
152 : {
153 0 : Reference< XIterateContainer > xIter( xNode, UNO_QUERY_THROW );
154 0 : xIter->setTarget( transformValue( xIter->getTarget() ) );
155 : }
156 : // its intended that here is no break!
157 : case AnimationNodeType::PAR:
158 : case AnimationNodeType::SEQ:
159 : {
160 0 : Reference< XEnumerationAccess > xEnumerationAccess( xNode, UNO_QUERY_THROW );
161 0 : Reference< XEnumeration > xEnumeration( xEnumerationAccess->createEnumeration(), UNO_QUERY_THROW );
162 0 : while( xEnumeration->hasMoreElements() )
163 : {
164 0 : Reference< XAnimationNode > xChildNode( xEnumeration->nextElement(), UNO_QUERY_THROW );
165 0 : transformNode( xChildNode );
166 0 : }
167 : }
168 0 : break;
169 :
170 : case AnimationNodeType::ANIMATE:
171 : case AnimationNodeType::SET:
172 : case AnimationNodeType::ANIMATEMOTION:
173 : case AnimationNodeType::ANIMATECOLOR:
174 : case AnimationNodeType::ANIMATETRANSFORM:
175 : case AnimationNodeType::TRANSITIONFILTER:
176 : {
177 0 : Reference< XAnimate > xAnimate( xNode, UNO_QUERY_THROW );
178 0 : xAnimate->setTarget( transformValue( xAnimate->getTarget() ) );
179 : }
180 0 : break;
181 :
182 : case AnimationNodeType::COMMAND:
183 : {
184 0 : Reference< XCommand > xCommand( xNode, UNO_QUERY_THROW );
185 0 : xCommand->setTarget( transformValue( xCommand->getTarget() ) );
186 : }
187 0 : break;
188 :
189 : case AnimationNodeType::AUDIO:
190 : {
191 0 : Reference< XAudio > xAudio( xNode, UNO_QUERY_THROW );
192 0 : xAudio->setSource( transformValue( xAudio->getSource() ) );
193 : }
194 0 : break;
195 : }
196 :
197 0 : Sequence< NamedValue > aUserData( xNode->getUserData() );
198 0 : if( aUserData.hasElements() )
199 : {
200 0 : NamedValue* pValue = aUserData.getArray();
201 0 : const sal_Int32 nLength = aUserData.getLength();
202 : sal_Int32 nElement;
203 0 : for( nElement = 0; nElement < nLength; nElement++, pValue++ )
204 : {
205 0 : pValue->Value = transformValue( pValue->Value );
206 : }
207 :
208 0 : xNode->setUserData( aUserData );
209 0 : }
210 : }
211 0 : catch( Exception& )
212 : {
213 : OSL_FAIL(
214 : OString(OString("sd::CustomAnimationClonerImpl::transformNode(), "
215 : "exception caught: ") +
216 : OUStringToOString(
217 : comphelper::anyToString( cppu::getCaughtException() ),
218 : RTL_TEXTENCODING_UTF8 )).getStr() );
219 : }
220 0 : }
221 :
222 0 : Any CustomAnimationClonerImpl::transformValue( const Any& rValue )
223 : {
224 0 : if( rValue.hasValue() ) try
225 : {
226 0 : if( rValue.getValueType() == ::getCppuType((const ValuePair*)0) )
227 : {
228 0 : ValuePair aValuePair;
229 0 : rValue >>= aValuePair;
230 :
231 0 : aValuePair.First = transformValue( aValuePair.First );
232 0 : aValuePair.Second = transformValue( aValuePair.Second );
233 :
234 0 : return makeAny( aValuePair );
235 : }
236 0 : else if( rValue.getValueType() == ::getCppuType((Sequence<Any>*)0) )
237 : {
238 0 : Sequence<Any> aSequence;
239 0 : rValue >>= aSequence;
240 :
241 0 : const sal_Int32 nLength = aSequence.getLength();
242 : sal_Int32 nElement;
243 0 : Any* pAny = aSequence.getArray();
244 :
245 0 : for( nElement = 0; nElement < nLength; nElement++, pAny++ )
246 0 : *pAny = transformValue( *pAny );
247 :
248 0 : return makeAny( aSequence );
249 : }
250 0 : else if( rValue.getValueTypeClass() == TypeClass_INTERFACE )
251 : {
252 0 : Reference< XShape > xShape;
253 0 : rValue >>= xShape;
254 0 : if( xShape.is() )
255 : {
256 0 : return makeAny( getClonedShape( xShape ) );
257 : }
258 : else
259 : {
260 0 : Reference< XAnimationNode > xNode;
261 0 : rValue >>= xNode;
262 0 : if( xNode.is() )
263 0 : return makeAny( getClonedNode( xNode ) );
264 0 : }
265 : }
266 0 : else if( rValue.getValueType() == ::getCppuType((const ParagraphTarget*)0) )
267 : {
268 0 : ParagraphTarget aParaTarget;
269 0 : rValue >>= aParaTarget;
270 :
271 0 : aParaTarget.Shape = getClonedShape( aParaTarget.Shape );
272 :
273 0 : return makeAny( aParaTarget );
274 : }
275 0 : else if( rValue.getValueType() == ::getCppuType((const Event*)0) )
276 : {
277 0 : Event aEvent;
278 0 : rValue >>= aEvent;
279 :
280 0 : aEvent.Source = transformValue( aEvent.Source );
281 :
282 0 : return makeAny( aEvent );
283 : }
284 : }
285 0 : catch( Exception& )
286 : {
287 : OSL_FAIL(
288 : OString(OString("sd::CustomAnimationClonerImpl::transformValue(), "
289 : "exception caught: ") +
290 : OUStringToOString(
291 : comphelper::anyToString( cppu::getCaughtException() ),
292 : RTL_TEXTENCODING_UTF8 )).getStr() );
293 : }
294 :
295 0 : return rValue;
296 : }
297 :
298 0 : Reference< XShape > CustomAnimationClonerImpl::getClonedShape( const Reference< XShape >& xSource ) const
299 : {
300 0 : if( xSource.is() )
301 : {
302 0 : if( maShapeMap.find(xSource) != maShapeMap.end() )
303 : {
304 0 : return maShapeMap[xSource];
305 : }
306 :
307 : DBG_ASSERT( maShapeMap.empty(), "sd::CustomAnimationClonerImpl::getClonedShape() failed!" );
308 : }
309 0 : return xSource;
310 : }
311 :
312 0 : Reference< XAnimationNode > CustomAnimationClonerImpl::getClonedNode( const Reference< XAnimationNode >& xSource ) const
313 : {
314 0 : sal_Int32 nNode, nNodeCount = maSourceNodeVector.size();
315 :
316 0 : for( nNode = 0; nNode < nNodeCount; nNode++ )
317 : {
318 0 : if( maSourceNodeVector[nNode] == xSource )
319 0 : return maCloneNodeVector[nNode];
320 : }
321 :
322 : OSL_FAIL( "sd::CustomAnimationClonerImpl::getClonedNode() failed!" );
323 0 : return xSource;
324 : }
325 : }
326 :
327 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|