LibreOffice Module sd (master) 1
CustomAnimationCloner.cxx
Go to the documentation of this file.
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/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>
32
33#include <map>
34
35#include <tools/debug.hxx>
38
39#include <svx/svditer.hxx>
40
42#include <sdpage.hxx>
43
44using namespace ::com::sun::star::uno;
45using namespace ::com::sun::star::animations;
46using namespace ::com::sun::star::presentation;
47using namespace ::com::sun::star::container;
48
49using ::com::sun::star::drawing::XShape;
50using ::com::sun::star::beans::NamedValue;
51
52namespace sd
53{
54 namespace {
55
56 class CustomAnimationClonerImpl
57 {
58 public:
59 CustomAnimationClonerImpl();
60 Reference< XAnimationNode > Clone( const Reference< XAnimationNode >& xSourceNode, const SdPage* pSource, const SdPage* pTarget );
61
62 private:
63 void transformNode( const Reference< XAnimationNode >& xNode );
64 Any transformValue( const Any& rValue );
65
66 Reference< XShape > getClonedShape( const Reference< XShape >& xSource ) const;
67 Reference< XAnimationNode > getClonedNode( const Reference< XAnimationNode >& xSource ) const;
68
69 mutable ::std::map< Reference< XShape >, Reference< XShape > > maShapeMap;
70 std::vector< Reference< XAnimationNode > > maSourceNodeVector;
71 std::vector< Reference< XAnimationNode > > maCloneNodeVector;
72 };
73
74 }
75
76 CustomAnimationClonerImpl::CustomAnimationClonerImpl()
77 {
78 }
79
80 Reference< XAnimationNode > Clone( const Reference< XAnimationNode >& xSourceNode, const SdPage* pSource, const SdPage* pTarget )
81 {
82 CustomAnimationClonerImpl aCloner;
83 return aCloner.Clone( xSourceNode, pSource, pTarget );
84 }
85
86 Reference< XAnimationNode > CustomAnimationClonerImpl::Clone( const Reference< XAnimationNode >& xSourceNode, const SdPage* pSourcePage, const SdPage* pTargetPage )
87 {
88 try
89 {
90 // clone animation hierarchy
91 Reference< css::util::XCloneable > xClonable( xSourceNode, UNO_QUERY_THROW );
92 Reference< XAnimationNode > xCloneNode( xClonable->createClone(), UNO_QUERY_THROW );
93
94 // create a dictionary to map source to cloned shapes
95 if( pSourcePage && pTargetPage )
96 {
97 SdrObjListIter aSourceIter( pSourcePage, SdrIterMode::DeepWithGroups );
98 SdrObjListIter aTargetIter( pTargetPage, SdrIterMode::DeepWithGroups );
99
100 while( aSourceIter.IsMore() && aTargetIter.IsMore() )
101 {
102 SdrObject* pSource = aSourceIter.Next();
103 SdrObject* pTarget = aTargetIter.Next();
104
105 if( pSource && pTarget)
106 {
107 Reference< XShape > xSource( pSource->getUnoShape(), UNO_QUERY );
108 Reference< XShape > xTarget( pTarget->getUnoShape(), UNO_QUERY );
109 if( xSource.is() && xTarget.is() )
110 {
111 maShapeMap[xSource] = xTarget;
112 }
113 }
114 }
115 }
116
117 // create a dictionary to map source to cloned nodes
118 ::anim::create_deep_vector( xSourceNode, maSourceNodeVector );
119 ::anim::create_deep_vector( xCloneNode, maCloneNodeVector );
120
121 transformNode( xCloneNode );
122
123 return xCloneNode;
124 }
125 catch( Exception& )
126 {
127 TOOLS_WARN_EXCEPTION( "sd", "sd::CustomAnimationClonerImpl::Clone()" );
128 Reference< XAnimationNode > xEmpty;
129 return xEmpty;
130 }
131 }
132
133 void CustomAnimationClonerImpl::transformNode( const Reference< XAnimationNode >& xNode )
134 {
135 try
136 {
137 xNode->setBegin( transformValue( xNode->getBegin() ) );
138 xNode->setEnd( transformValue( xNode->getEnd() ) );
139
140 sal_Int16 nNodeType( xNode->getType() );
141 switch( nNodeType )
142 {
143 case AnimationNodeType::ITERATE:
144 {
145 Reference< XIterateContainer > xIter( xNode, UNO_QUERY_THROW );
146 xIter->setTarget( transformValue( xIter->getTarget() ) );
147 [[fallthrough]];
148 }
149 case AnimationNodeType::PAR:
150 case AnimationNodeType::SEQ:
151 {
152 Reference< XEnumerationAccess > xEnumerationAccess( xNode, UNO_QUERY_THROW );
153 Reference< XEnumeration > xEnumeration( xEnumerationAccess->createEnumeration(), UNO_SET_THROW );
154 while( xEnumeration->hasMoreElements() )
155 {
156 Reference< XAnimationNode > xChildNode( xEnumeration->nextElement(), UNO_QUERY_THROW );
157 transformNode( xChildNode );
158 }
159 }
160 break;
161
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:
169 {
170 Reference< XAnimate > xAnimate( xNode, UNO_QUERY_THROW );
171 xAnimate->setTarget( transformValue( xAnimate->getTarget() ) );
172 }
173 break;
174
175 case AnimationNodeType::COMMAND:
176 {
177 Reference< XCommand > xCommand( xNode, UNO_QUERY_THROW );
178 xCommand->setTarget( transformValue( xCommand->getTarget() ) );
179 }
180 break;
181
182 case AnimationNodeType::AUDIO:
183 {
184 Reference< XAudio > xAudio( xNode, UNO_QUERY_THROW );
185 xAudio->setSource( transformValue( xAudio->getSource() ) );
186 }
187 break;
188 }
189
190 Sequence< NamedValue > aUserData( xNode->getUserData() );
191 if( aUserData.hasElements() )
192 {
193 for( NamedValue & namedValue : asNonConstRange(aUserData) )
194 {
195 namedValue.Value = transformValue( namedValue.Value );
196 }
197
198 xNode->setUserData( aUserData );
199 }
200 }
201 catch( Exception& )
202 {
203 TOOLS_WARN_EXCEPTION( "sd", "sd::CustomAnimationClonerImpl::transformNode()" );
204 }
205 }
206
207 Any CustomAnimationClonerImpl::transformValue( const Any& rValue )
208 {
209 if( rValue.hasValue() ) try
210 {
211 if( rValue.getValueType() == cppu::UnoType<ValuePair>::get() )
212 {
213 ValuePair aValuePair;
214 rValue >>= aValuePair;
215
216 aValuePair.First = transformValue( aValuePair.First );
217 aValuePair.Second = transformValue( aValuePair.Second );
218
219 return Any( aValuePair );
220 }
221 else if( rValue.getValueType() == cppu::UnoType< Sequence<Any> >::get() )
222 {
223 Sequence<Any> aSequence;
224 rValue >>= aSequence;
225
226 for( Any& rAny : asNonConstRange(aSequence) )
227 rAny = transformValue( rAny );
228
229 return Any( aSequence );
230 }
231 else if( rValue.getValueTypeClass() == TypeClass_INTERFACE )
232 {
233 Reference< XShape > xShape;
234 rValue >>= xShape;
235 if( xShape.is() )
236 {
237 return Any( getClonedShape( xShape ) );
238 }
239 else
240 {
241 Reference< XAnimationNode > xNode;
242 rValue >>= xNode;
243 if( xNode.is() )
244 return Any( getClonedNode( xNode ) );
245 }
246 }
247 else if( rValue.getValueType() == cppu::UnoType<ParagraphTarget>::get() )
248 {
249 ParagraphTarget aParaTarget;
250 rValue >>= aParaTarget;
251
252 aParaTarget.Shape = getClonedShape( aParaTarget.Shape );
253
254 return Any( aParaTarget );
255 }
256 else if( rValue.getValueType() == cppu::UnoType<Event>::get() )
257 {
258 Event aEvent;
259 rValue >>= aEvent;
260
261 aEvent.Source = transformValue( aEvent.Source );
262
263 return Any( aEvent );
264 }
265 }
266 catch( Exception& )
267 {
268 TOOLS_WARN_EXCEPTION( "sd", "sd::CustomAnimationClonerImpl::transformValue()" );
269 }
270
271 return rValue;
272 }
273
274 Reference< XShape > CustomAnimationClonerImpl::getClonedShape( const Reference< XShape >& xSource ) const
275 {
276 if( xSource.is() )
277 {
278 if( maShapeMap.find(xSource) != maShapeMap.end() )
279 {
280 return maShapeMap[xSource];
281 }
282
283 DBG_ASSERT( maShapeMap.empty(), "sd::CustomAnimationClonerImpl::getClonedShape() failed!" );
284 }
285 return xSource;
286 }
287
288 Reference< XAnimationNode > CustomAnimationClonerImpl::getClonedNode( const Reference< XAnimationNode >& xSource ) const
289 {
290 std::size_t nNodeCount = maSourceNodeVector.size();
291 std::size_t nCloneNodeCount = maCloneNodeVector.size();
292
293 if (nNodeCount != nCloneNodeCount)
294 SAL_WARN("sd.core", "Sizes of maSourceNodeVector and maCloneNodeVector mismatch!");
295
296 for( std::size_t nNode = 0; nNode < nNodeCount && nNode < nCloneNodeCount; ++nNode )
297 {
298 if( maSourceNodeVector[nNode] == xSource )
299 return maCloneNodeVector[nNode];
300 }
301
302 OSL_FAIL( "sd::CustomAnimationClonerImpl::getClonedNode() failed!" );
303 return xSource;
304 }
305}
306
307/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
AnyEventRef aEvent
virtual css::uno::Reference< css::drawing::XShape > getUnoShape()
#define DBG_ASSERT(sCon, aError)
#define TOOLS_WARN_EXCEPTION(area, stream)
Reference< XInterface > xTarget
#define SAL_WARN(area, stream)
Reference< XAnimationNode > Clone(const Reference< XAnimationNode > &xSourceNode, const SdPage *pSource, const SdPage *pTarget)