update credits
[LibreOffice.git] / animations / source / animcore / targetpropertiescreator.cxx
blob9e9fd27c0cb8002baaf21f013ad4bb53324a3e8d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/uno/XComponentContext.hpp>
21 #include <com/sun/star/lang/XServiceInfo.hpp>
22 #include <com/sun/star/lang/XTypeProvider.hpp>
23 #include <com/sun/star/animations/XTargetPropertiesCreator.hpp>
24 #include <com/sun/star/animations/XIterateContainer.hpp>
25 #include <com/sun/star/animations/TargetProperties.hpp>
26 #include <com/sun/star/presentation/ParagraphTarget.hpp>
27 #include <com/sun/star/registry/XRegistryKey.hpp>
28 #include <com/sun/star/lang/XInitialization.hpp>
29 #include <com/sun/star/lang/XServiceName.hpp>
30 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
31 #include <com/sun/star/drawing/XShape.hpp>
32 #include <com/sun/star/animations/AnimationNodeType.hpp>
33 #include <com/sun/star/animations/XAnimate.hpp>
34 #include <cppuhelper/compbase3.hxx>
35 #include <cppuhelper/factory.hxx>
36 #include <cppuhelper/implementationentry.hxx>
37 #include <comphelper/broadcasthelper.hxx>
38 #include <comphelper/sequence.hxx>
40 #include <animations/animationnodehelper.hxx>
42 #include <vector>
43 #include <boost/unordered_map.hpp>
46 using namespace ::com::sun::star;
48 #define IMPLEMENTATION_NAME "animcore::TargetPropertiesCreator"
49 #define SERVICE_NAME "com.sun.star.animations.TargetPropertiesCreator"
51 namespace animcore
53 typedef ::cppu::WeakComponentImplHelper3< ::com::sun::star::animations::XTargetPropertiesCreator,
54 lang::XServiceInfo,
55 lang::XServiceName > TargetPropertiesCreator_Base;
57 class TargetPropertiesCreator : public ::comphelper::OBaseMutex,
58 public TargetPropertiesCreator_Base
60 public:
61 static uno::Reference< uno::XInterface > SAL_CALL createInstance( const uno::Reference< uno::XComponentContext >& xContext ) throw ( uno::Exception )
63 return uno::Reference< uno::XInterface >( static_cast<cppu::OWeakObject*>(new TargetPropertiesCreator( xContext )) );
66 /// Dispose all internal references
67 virtual void SAL_CALL disposing();
69 // XTargetPropertiesCreator
70 virtual uno::Sequence< animations::TargetProperties > SAL_CALL createInitialTargetProperties( const uno::Reference< animations::XAnimationNode >& rootNode ) throw (uno::RuntimeException);
72 // XServiceInfo
73 virtual OUString SAL_CALL getImplementationName() throw( uno::RuntimeException );
74 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw( uno::RuntimeException );
75 virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() throw( uno::RuntimeException );
77 // XServiceName
78 virtual OUString SAL_CALL getServiceName( ) throw (uno::RuntimeException);
80 protected:
81 ~TargetPropertiesCreator(); // we're a ref-counted UNO class. _We_ destroy ourselves.
83 private:
84 // default: disabled copy/assignment
85 TargetPropertiesCreator(const TargetPropertiesCreator&);
86 TargetPropertiesCreator& operator=( const TargetPropertiesCreator& );
88 TargetPropertiesCreator( const uno::Reference< uno::XComponentContext >& rxContext );
91 // --------------------------------------------------------------------
93 uno::Reference< uno::XInterface > SAL_CALL createInstance_TargetPropertiesCreator( const uno::Reference< uno::XComponentContext > & rSMgr ) throw (uno::Exception)
95 return TargetPropertiesCreator::createInstance( rSMgr );
98 OUString getImplementationName_TargetPropertiesCreator()
100 return OUString( IMPLEMENTATION_NAME );
103 uno::Sequence< OUString > getSupportedServiceNames_TargetPropertiesCreator(void)
105 uno::Sequence< OUString > aRet(1);
106 aRet.getArray()[0] = SERVICE_NAME;
107 return aRet;
110 // --------------------------------------------------------------------
112 namespace
114 // Vector containing all properties for a given shape
115 typedef ::std::vector< beans::NamedValue > VectorOfNamedValues;
117 /** The hash map key
119 This key contains both XShape reference and a paragraph
120 index, as we somehow have to handle shape and paragraph
121 targets with the same data structure.
123 struct ShapeHashKey
125 /// Shape target
126 uno::Reference< drawing::XShape > mxRef;
128 /** Paragraph index.
130 If this is a pure shape target, mnParagraphIndex is
131 set to -1.
133 sal_Int16 mnParagraphIndex;
135 /// Comparison needed for boost::unordered_map
136 bool operator==( const ShapeHashKey& rRHS ) const
138 return mxRef == rRHS.mxRef && mnParagraphIndex == rRHS.mnParagraphIndex;
142 // A hash map which maps a XShape to the corresponding vector of initial properties
143 typedef ::boost::unordered_map< ShapeHashKey,
144 VectorOfNamedValues,
145 ::std::size_t (*)(const ShapeHashKey&) > XShapeHash;
147 ::std::size_t refhasher( const ShapeHashKey& rKey )
149 // TODO(P2): Maybe a better hash function would be to
150 // spread mnParagraphIndex to 32 bit: a0b0c0d0e0... Hakmem
151 // should have a formula.
153 // Yes it has:
154 // x = (x & 0x0000FF00) << 8) | (x >> 8) & 0x0000FF00 | x & 0xFF0000FF;
155 // x = (x & 0x00F000F0) << 4) | (x >> 4) & 0x00F000F0 | x & 0xF00FF00F;
156 // x = (x & 0x0C0C0C0C) << 2) | (x >> 2) & 0x0C0C0C0C | x & 0xC3C3C3C3;
157 // x = (x & 0x22222222) << 1) | (x >> 1) & 0x22222222 | x & 0x99999999;
159 // Costs about 17 cycles on a RISC machine with infinite
160 // instruction level parallelism (~42 basic
161 // instructions). Thus I truly doubt this pays off...
162 return reinterpret_cast< ::std::size_t >(rKey.mxRef.get()) ^ (rKey.mnParagraphIndex << 16L);
166 class NodeFunctor
168 public:
169 explicit NodeFunctor( XShapeHash& rShapeHash ) :
170 mrShapeHash( rShapeHash ),
171 mxTargetShape(),
172 mnParagraphIndex( -1 )
176 NodeFunctor( XShapeHash& rShapeHash,
177 const uno::Reference< drawing::XShape >& rTargetShape,
178 sal_Int16 nParagraphIndex ) :
179 mrShapeHash( rShapeHash ),
180 mxTargetShape( rTargetShape ),
181 mnParagraphIndex( nParagraphIndex )
185 void operator()( const uno::Reference< animations::XAnimationNode >& xNode ) const
187 if( !xNode.is() )
189 OSL_FAIL( "AnimCore: NodeFunctor::operator(): invalid XAnimationNode" );
190 return;
193 uno::Reference< drawing::XShape > xTargetShape( mxTargetShape );
194 sal_Int16 nParagraphIndex( mnParagraphIndex );
196 switch( xNode->getType() )
198 case animations::AnimationNodeType::ITERATE:
200 // extract target shape from iterate node
201 // (will override the target for all children)
202 // --------------------------------------------------
204 uno::Reference< animations::XIterateContainer > xIterNode( xNode,
205 uno::UNO_QUERY );
207 // TODO(E1): I'm not too sure what to expect here...
208 if( !xIterNode->getTarget().hasValue() )
210 OSL_FAIL( "animcore: NodeFunctor::operator(): no target on ITERATE node" );
211 return;
214 xTargetShape.set( xIterNode->getTarget(),
215 uno::UNO_QUERY );
217 if( !xTargetShape.is() )
219 ::com::sun::star::presentation::ParagraphTarget aTarget;
221 // no shape provided. Maybe a ParagraphTarget?
222 if( !(xIterNode->getTarget() >>= aTarget) )
224 OSL_FAIL( "animcore: NodeFunctor::operator(): could not extract any "
225 "target information" );
226 return;
229 xTargetShape = aTarget.Shape;
230 nParagraphIndex = aTarget.Paragraph;
232 if( !xTargetShape.is() )
234 OSL_FAIL( "animcore: NodeFunctor::operator(): invalid shape in ParagraphTarget" );
235 return;
239 // FALLTHROUGH intended
240 case animations::AnimationNodeType::PAR:
241 // FALLTHROUGH intended
242 case animations::AnimationNodeType::SEQ:
244 NodeFunctor aFunctor( mrShapeHash,
245 xTargetShape,
246 nParagraphIndex );
247 if( !::anim::for_each_childNode( xNode,
248 aFunctor ) )
250 OSL_FAIL( "AnimCore: NodeFunctor::operator(): child node iteration failed, "
251 "or extraneous container nodes encountered" );
254 break;
256 case animations::AnimationNodeType::CUSTOM:
257 // FALLTHROUGH intended
258 case animations::AnimationNodeType::ANIMATE:
259 // FALLTHROUGH intended
260 case animations::AnimationNodeType::ANIMATEMOTION:
261 // FALLTHROUGH intended
262 case animations::AnimationNodeType::ANIMATECOLOR:
263 // FALLTHROUGH intended
264 case animations::AnimationNodeType::ANIMATETRANSFORM:
265 // FALLTHROUGH intended
266 case animations::AnimationNodeType::TRANSITIONFILTER:
267 // FALLTHROUGH intended
268 case animations::AnimationNodeType::AUDIO:
269 // FALLTHROUGH intended
270 default:
271 // ignore this node, no valuable content for now.
272 break;
274 case animations::AnimationNodeType::SET:
276 // evaluate set node content
277 uno::Reference< animations::XAnimate > xAnimateNode( xNode,
278 uno::UNO_QUERY );
280 if( !xAnimateNode.is() )
281 break; // invalid node
283 // determine target shape (if any)
284 ShapeHashKey aTarget;
285 if( xTargetShape.is() )
287 // override target shape with parent-supplied
288 aTarget.mxRef = xTargetShape;
289 aTarget.mnParagraphIndex = nParagraphIndex;
291 else
293 // no parent-supplied target, retrieve
294 // node target
295 if( (xAnimateNode->getTarget() >>= aTarget.mxRef) )
297 // pure shape target - set paragraph
298 // index to magic
299 aTarget.mnParagraphIndex = -1;
301 else
303 // not a pure shape target - maybe a
304 // ParagraphTarget?
305 presentation::ParagraphTarget aUnoTarget;
307 if( !(xAnimateNode->getTarget() >>= aUnoTarget) )
309 OSL_FAIL( "AnimCore: NodeFunctor::operator(): unknown target type encountered" );
310 break;
313 aTarget.mxRef = aUnoTarget.Shape;
314 aTarget.mnParagraphIndex = aUnoTarget.Paragraph;
318 if( !aTarget.mxRef.is() )
320 OSL_FAIL( "AnimCore: NodeFunctor::operator(): Found target, but XShape is NULL" );
321 break; // invalid target XShape
324 // check whether we already have an entry for
325 // this target (we only take the first set
326 // effect for every shape)
327 XShapeHash::const_iterator aIter;
328 if( (aIter=mrShapeHash.find( aTarget )) != mrShapeHash.end() )
329 break; // already an entry in existence for given XShape
331 // if this is an appear effect, hide shape
332 // initially. This is currently the only place
333 // where a shape effect influences shape
334 // attributes outside it's effective duration.
335 if( xAnimateNode->getAttributeName().equalsIgnoreAsciiCase("visibility") )
337 sal_Bool bVisible( sal_False );
339 uno::Any aAny( xAnimateNode->getTo() );
341 // try to extract bool value
342 if( !(aAny >>= bVisible) )
344 // try to extract string
345 OUString aString;
346 if( (aAny >>= aString) )
348 // we also take the strings "true" and "false",
349 // as well as "on" and "off" here
350 if( aString.equalsIgnoreAsciiCase("true") ||
351 aString.equalsIgnoreAsciiCase("on") )
353 bVisible = sal_True;
355 if( aString.equalsIgnoreAsciiCase("false") ||
356 aString.equalsIgnoreAsciiCase("off") )
358 bVisible = sal_False;
363 if( bVisible )
365 // target is set to 'visible' at the
366 // first relevant effect. Thus, target
367 // must be initially _hidden_, for the
368 // effect to have visible impact.
369 mrShapeHash.insert(
370 XShapeHash::value_type(
371 aTarget,
372 VectorOfNamedValues(
374 beans::NamedValue(
375 xAnimateNode->getAttributeName(),
376 uno::makeAny( sal_False ) ) ) ) );
380 break;
384 private:
385 XShapeHash& mrShapeHash;
386 uno::Reference< drawing::XShape > mxTargetShape;
387 sal_Int16 mnParagraphIndex;
391 // --------------------------------------------------------------------
393 TargetPropertiesCreator::TargetPropertiesCreator( const uno::Reference< uno::XComponentContext >& ) :
394 TargetPropertiesCreator_Base( m_aMutex )
398 TargetPropertiesCreator::~TargetPropertiesCreator()
402 void SAL_CALL TargetPropertiesCreator::disposing()
404 ::osl::MutexGuard aGuard( m_aMutex );
407 // XTargetPropertiesCreator
408 uno::Sequence< animations::TargetProperties > SAL_CALL TargetPropertiesCreator::createInitialTargetProperties
410 const uno::Reference< animations::XAnimationNode >& xRootNode
411 ) throw (uno::RuntimeException)
413 ::osl::MutexGuard aGuard( m_aMutex );
415 // scan all nodes for visibility changes, and record first
416 // 'visibility=true' for each shape
417 XShapeHash aShapeHash( 101,
418 &refhasher );
420 NodeFunctor aFunctor( aShapeHash );
422 // TODO(F1): Maybe limit functor application to main sequence
423 // alone (CL said something that shape visibility is only
424 // affected by effects in the main sequence for PPT).
426 // OTOH, client code can pass us only the main sequence (which
427 // it actually does right now, for the slideshow implementation).
428 aFunctor( xRootNode );
431 // output to result sequence
432 // ----------------------------------------------------------------------
434 uno::Sequence< animations::TargetProperties > aRes( aShapeHash.size() );
436 ::std::size_t nCurrIndex(0);
437 XShapeHash::const_iterator aCurr( aShapeHash.begin() );
438 const XShapeHash::const_iterator aEnd ( aShapeHash.end() );
439 while( aCurr != aEnd )
441 animations::TargetProperties& rCurrProps( aRes[ nCurrIndex++ ] );
443 if( aCurr->first.mnParagraphIndex == -1 )
445 rCurrProps.Target = uno::makeAny( aCurr->first.mxRef );
447 else
449 rCurrProps.Target = uno::makeAny(
450 presentation::ParagraphTarget(
451 aCurr->first.mxRef,
452 aCurr->first.mnParagraphIndex ) );
455 rCurrProps.Properties = ::comphelper::containerToSequence( aCurr->second );
457 ++aCurr;
460 return aRes;
463 // XServiceInfo
464 OUString SAL_CALL TargetPropertiesCreator::getImplementationName() throw( uno::RuntimeException )
466 return OUString( IMPLEMENTATION_NAME );
469 sal_Bool SAL_CALL TargetPropertiesCreator::supportsService( const OUString& ServiceName ) throw( uno::RuntimeException )
471 return ServiceName.equalsIgnoreAsciiCase(SERVICE_NAME);
474 uno::Sequence< OUString > SAL_CALL TargetPropertiesCreator::getSupportedServiceNames() throw( uno::RuntimeException )
476 uno::Sequence< OUString > aRet(1);
477 aRet[0] = SERVICE_NAME;
479 return aRet;
482 // XServiceName
483 OUString SAL_CALL TargetPropertiesCreator::getServiceName( ) throw (uno::RuntimeException)
485 return OUString( SERVICE_NAME );
488 } // namespace animcore
490 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */