android: Update app-specific/MIME type icons
[LibreOffice.git] / slideshow / source / engine / slide / targetpropertiescreator.cxx
blob80f41cc4c795334b3a043db405e4aac07d8bacf9
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/animations/XIterateContainer.hpp>
21 #include <com/sun/star/presentation/ParagraphTarget.hpp>
22 #include <com/sun/star/drawing/XShape.hpp>
23 #include <com/sun/star/animations/AnimationNodeType.hpp>
24 #include <com/sun/star/animations/XAnimate.hpp>
25 #include <comphelper/sequence.hxx>
27 #include <unordered_map>
28 #include <utility>
29 #include <vector>
31 #include "targetpropertiescreator.hxx"
32 #include <tools.hxx>
34 namespace slideshow::internal
36 namespace
38 // Vector containing all properties for a given shape
39 typedef ::std::vector< beans::NamedValue > VectorOfNamedValues;
41 /** The hash map key
43 This key contains both XShape reference and a paragraph
44 index, as we somehow have to handle shape and paragraph
45 targets with the same data structure.
47 struct ShapeHashKey
49 /// Shape target
50 uno::Reference< drawing::XShape > mxRef;
52 /** Paragraph index.
54 If this is a pure shape target, mnParagraphIndex is
55 set to -1.
57 sal_Int16 mnParagraphIndex;
59 /// Comparison needed for unordered_map
60 bool operator==( const ShapeHashKey& rRHS ) const
62 return mxRef == rRHS.mxRef && mnParagraphIndex == rRHS.mnParagraphIndex;
66 // A hash functor for ShapeHashKey objects
67 struct ShapeKeyHasher
69 ::std::size_t operator()( const ShapeHashKey& rKey ) const
71 // TODO(P2): Maybe a better hash function would be to
72 // spread mnParagraphIndex to 32 bit: a0b0c0d0e0... Hakmem
73 // should have a formula.
75 // Yes it has:
76 // x = (x & 0x0000FF00) << 8) | (x >> 8) & 0x0000FF00 | x & 0xFF0000FF;
77 // x = (x & 0x00F000F0) << 4) | (x >> 4) & 0x00F000F0 | x & 0xF00FF00F;
78 // x = (x & 0x0C0C0C0C) << 2) | (x >> 2) & 0x0C0C0C0C | x & 0xC3C3C3C3;
79 // x = (x & 0x22222222) << 1) | (x >> 1) & 0x22222222 | x & 0x99999999;
81 // Costs about 17 cycles on a RISC machine with infinite
82 // instruction level parallelism (~42 basic
83 // instructions). Thus I truly doubt this pays off...
84 return reinterpret_cast< ::std::size_t >(rKey.mxRef.get()) ^ (rKey.mnParagraphIndex << 16);
88 // A hash map which maps a XShape to the corresponding vector of initial properties
89 typedef std::unordered_map< ShapeHashKey, VectorOfNamedValues, ShapeKeyHasher > XShapeToNamedValuesMap;
92 class NodeFunctor
94 public:
95 explicit NodeFunctor(
96 XShapeToNamedValuesMap& rShapeHash,
97 bool bInitial )
98 : mrShapeHash( rShapeHash ),
99 mxTargetShape(),
100 mnParagraphIndex( -1 ),
101 mbInitial( bInitial)
105 NodeFunctor( XShapeToNamedValuesMap& rShapeHash,
106 uno::Reference< drawing::XShape > xTargetShape,
107 sal_Int16 nParagraphIndex,
108 bool bInitial) :
109 mrShapeHash( rShapeHash ),
110 mxTargetShape(std::move( xTargetShape )),
111 mnParagraphIndex( nParagraphIndex ),
112 mbInitial( bInitial )
116 void operator()( const uno::Reference< animations::XAnimationNode >& xNode ) const
118 if( !xNode.is() )
120 OSL_FAIL( "AnimCore: NodeFunctor::operator(): invalid XAnimationNode" );
121 return;
124 uno::Reference< drawing::XShape > xTargetShape( mxTargetShape );
125 sal_Int16 nParagraphIndex( mnParagraphIndex );
127 switch( xNode->getType() )
129 case animations::AnimationNodeType::ITERATE:
131 // extract target shape from iterate node
132 // (will override the target for all children)
134 uno::Reference< animations::XIterateContainer > xIterNode( xNode,
135 uno::UNO_QUERY );
137 // TODO(E1): I'm not too sure what to expect here...
138 if( !xIterNode->getTarget().hasValue() )
140 OSL_FAIL( "animcore: NodeFunctor::operator(): no target on ITERATE node" );
141 return;
144 xTargetShape.set( xIterNode->getTarget(),
145 uno::UNO_QUERY );
147 if( !xTargetShape.is() )
149 css::presentation::ParagraphTarget aTarget;
151 // no shape provided. Maybe a ParagraphTarget?
152 if( !(xIterNode->getTarget() >>= aTarget) )
154 OSL_FAIL( "animcore: NodeFunctor::operator(): could not extract any "
155 "target information" );
156 return;
159 xTargetShape = aTarget.Shape;
160 nParagraphIndex = aTarget.Paragraph;
162 if( !xTargetShape.is() )
164 OSL_FAIL( "animcore: NodeFunctor::operator(): invalid shape in ParagraphTarget" );
165 return;
168 [[fallthrough]];
170 case animations::AnimationNodeType::PAR:
171 case animations::AnimationNodeType::SEQ:
173 /// forward bInitial
174 NodeFunctor aFunctor( mrShapeHash,
175 xTargetShape,
176 nParagraphIndex,
177 mbInitial );
178 if( !for_each_childNode( xNode, aFunctor ) )
180 OSL_FAIL( "AnimCore: NodeFunctor::operator(): child node iteration failed, "
181 "or extraneous container nodes encountered" );
184 break;
186 case animations::AnimationNodeType::CUSTOM:
187 case animations::AnimationNodeType::ANIMATE:
188 case animations::AnimationNodeType::ANIMATEMOTION:
189 case animations::AnimationNodeType::ANIMATECOLOR:
190 case animations::AnimationNodeType::ANIMATETRANSFORM:
191 case animations::AnimationNodeType::TRANSITIONFILTER:
192 case animations::AnimationNodeType::AUDIO:
193 /*default:
194 // ignore this node, no valuable content for now.
195 break;*/
197 case animations::AnimationNodeType::SET:
199 // evaluate set node content
200 uno::Reference< animations::XAnimate > xAnimateNode( xNode,
201 uno::UNO_QUERY );
203 if( !xAnimateNode.is() )
204 break; // invalid node
206 // determine target shape (if any)
207 ShapeHashKey aTarget;
208 if( xTargetShape.is() )
210 // override target shape with parent-supplied
211 aTarget.mxRef = xTargetShape;
212 aTarget.mnParagraphIndex = nParagraphIndex;
214 else
216 // no parent-supplied target, retrieve
217 // node target
218 if( xAnimateNode->getTarget() >>= aTarget.mxRef )
220 // pure shape target - set paragraph
221 // index to magic
222 aTarget.mnParagraphIndex = -1;
224 else
226 // not a pure shape target - maybe a
227 // ParagraphTarget?
228 presentation::ParagraphTarget aUnoTarget;
230 if( !(xAnimateNode->getTarget() >>= aUnoTarget) )
232 OSL_FAIL( "AnimCore: NodeFunctor::operator(): unknown target type encountered" );
233 break;
236 aTarget.mxRef = aUnoTarget.Shape;
237 aTarget.mnParagraphIndex = aUnoTarget.Paragraph;
241 if( !aTarget.mxRef.is() )
243 OSL_FAIL( "AnimCore: NodeFunctor::operator(): Found target, but XShape is NULL" );
244 break; // invalid target XShape
247 // check whether we already have an entry for
248 // this target (we only take the first set
249 // effect for every shape) - but keep going if
250 // we're requested the final state (which
251 // eventually gets overwritten in the
252 // unordered list, see tdf#96083)
253 if( mbInitial && mrShapeHash.find( aTarget ) != mrShapeHash.end() )
254 break; // already an entry in existence for given XShape
256 // if this is an appear effect, hide shape
257 // initially. This is currently the only place
258 // where a shape effect influences shape
259 // attributes outside it's effective duration.
260 bool bVisible( false );
261 if( xAnimateNode->getAttributeName().equalsIgnoreAsciiCase("visibility") )
264 uno::Any aAny( xAnimateNode->getTo() );
266 // try to extract bool value
267 if( !(aAny >>= bVisible) )
269 // try to extract string
270 OUString aString;
271 if( aAny >>= aString )
273 // we also take the strings "true" and "false",
274 // as well as "on" and "off" here
275 if( aString.equalsIgnoreAsciiCase("true") ||
276 aString.equalsIgnoreAsciiCase("on") )
278 bVisible = true;
280 if( aString.equalsIgnoreAsciiCase("false") ||
281 aString.equalsIgnoreAsciiCase("off") )
283 bVisible = false;
289 // if initial anim sets shape visible, set it
290 // to invisible. If we're asked for the final
291 // state, don't do anything obviously
292 if(mbInitial)
293 bVisible = !bVisible;
295 // target is set the 'visible' value,
296 // so we should record the opposite value
297 mrShapeHash.emplace(
298 aTarget,
299 VectorOfNamedValues(
301 beans::NamedValue(
302 //xAnimateNode->getAttributeName(),
303 "visibility",
304 uno::Any( bVisible ) ) ) );
305 break;
310 private:
311 XShapeToNamedValuesMap& mrShapeHash;
312 uno::Reference< drawing::XShape > mxTargetShape;
313 sal_Int16 mnParagraphIndex;
315 // get initial or final state
316 bool mbInitial;
320 uno::Sequence< animations::TargetProperties > TargetPropertiesCreator::createTargetProperties
322 const uno::Reference< animations::XAnimationNode >& xRootNode,
323 bool bInitial
324 ) //throw (uno::RuntimeException, std::exception)
326 // scan all nodes for visibility changes, and record first
327 // 'visibility=true' for each shape
328 XShapeToNamedValuesMap aShapeHash( 101 );
330 NodeFunctor aFunctor(
331 aShapeHash,
332 bInitial );
334 // TODO(F1): Maybe limit functor application to main sequence
335 // alone (CL said something that shape visibility is only
336 // affected by effects in the main sequence for PPT).
338 // OTOH, client code can pass us only the main sequence (which
339 // it actually does right now, for the slideshow implementation).
340 aFunctor( xRootNode );
342 // output to result sequence
343 uno::Sequence< animations::TargetProperties > aRes( aShapeHash.size() );
344 auto aResRange = asNonConstRange(aRes);
346 ::std::size_t nCurrIndex(0);
347 for( const auto& rIter : aShapeHash )
349 animations::TargetProperties& rCurrProps( aResRange[ nCurrIndex++ ] );
351 if( rIter.first.mnParagraphIndex == -1 )
353 rCurrProps.Target <<= rIter.first.mxRef;
355 else
357 rCurrProps.Target <<=
358 presentation::ParagraphTarget(
359 rIter.first.mxRef,
360 rIter.first.mnParagraphIndex );
363 rCurrProps.Properties = ::comphelper::containerToSequence( rIter.second );
366 return aRes;
369 } // namespace slideshow::internal
371 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */