Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sd / source / ui / unoidl / randomnode.cxx
blob74f4eb0a30e445a0d6e6b5c0089ec558ae5759fe
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/XTimeContainer.hpp>
21 #include <com/sun/star/presentation/ParagraphTarget.hpp>
22 #include <com/sun/star/animations/AnimationFill.hpp>
23 #include <com/sun/star/animations/AnimationNodeType.hpp>
24 #include <com/sun/star/animations/XAnimate.hpp>
25 #include <com/sun/star/animations/AnimationRestart.hpp>
26 #include <com/sun/star/animations/ParallelTimeContainer.hpp>
27 #include <com/sun/star/lang/IllegalArgumentException.hpp>
28 #include <com/sun/star/lang/XServiceInfo.hpp>
29 #include <com/sun/star/lang/XInitialization.hpp>
30 #include <com/sun/star/container/XEnumerationAccess.hpp>
31 #include <com/sun/star/util/XCloneable.hpp>
32 #include <comphelper/processfactory.hxx>
34 #include <cppuhelper/implbase.hxx>
35 #include <cppuhelper/supportsservice.hxx>
36 #include <cppuhelper/weakref.hxx>
37 #include <CustomAnimationPreset.hxx>
38 #include <randomnode.hxx>
39 #include <mutex>
41 using ::com::sun::star::uno::Reference;
42 using ::com::sun::star::uno::Sequence;
43 using ::com::sun::star::uno::Any;
44 using ::com::sun::star::uno::UNO_QUERY;
45 using ::com::sun::star::uno::XInterface;
46 using ::com::sun::star::uno::WeakReference;
47 using ::com::sun::star::beans::NamedValue;
48 using ::com::sun::star::lang::IllegalArgumentException;
49 using ::com::sun::star::container::XEnumeration;
50 using ::com::sun::star::container::XEnumerationAccess;
51 using ::com::sun::star::util::XCloneable;
52 using ::com::sun::star::lang::XServiceInfo;
53 using ::com::sun::star::lang::XInitialization;
54 using ::com::sun::star::uno::Type;
55 using ::com::sun::star::uno::XWeak;
56 using ::com::sun::star::presentation::ParagraphTarget;
57 using ::com::sun::star::drawing::XShape;
59 using namespace ::com::sun::star::animations;
61 namespace sd
64 typedef ::cppu::WeakImplHelper< XTimeContainer, XEnumerationAccess, XCloneable, XServiceInfo, XInitialization > RandomAnimationNodeBase;
66 namespace {
68 class RandomAnimationNode : public RandomAnimationNodeBase
70 public:
71 RandomAnimationNode( const RandomAnimationNode& rNode );
72 explicit RandomAnimationNode( sal_Int16 nPresetClass );
73 RandomAnimationNode();
75 void init( sal_Int16 nPresetClass );
77 // XInitialization
78 void SAL_CALL initialize( const Sequence< Any >& aArguments ) override;
80 // XChild
81 Reference< XInterface > SAL_CALL getParent( ) override;
82 void SAL_CALL setParent( const Reference< XInterface >& Parent ) override;
84 // XCloneable
85 virtual Reference< XCloneable > SAL_CALL createClone() override;
87 // XServiceInfo
88 OUString SAL_CALL getImplementationName() override;
89 Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
90 sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
92 // XAnimationNode
93 ::sal_Int16 SAL_CALL getType() override;
94 Any SAL_CALL getBegin() override;
95 void SAL_CALL setBegin( const Any& _begin ) override;
96 Any SAL_CALL getDuration() override;
97 void SAL_CALL setDuration( const Any& _duration ) override;
98 Any SAL_CALL getEnd() override;
99 void SAL_CALL setEnd( const Any& _end ) override;
100 Any SAL_CALL getEndSync() override;
101 void SAL_CALL setEndSync( const Any& _endsync ) override;
102 Any SAL_CALL getRepeatCount() override;
103 void SAL_CALL setRepeatCount( const Any& _repeatcount ) override;
104 Any SAL_CALL getRepeatDuration() override;
105 void SAL_CALL setRepeatDuration( const Any& _repeatduration ) override;
106 ::sal_Int16 SAL_CALL getFill() override;
107 void SAL_CALL setFill( ::sal_Int16 _fill ) override;
108 ::sal_Int16 SAL_CALL getFillDefault() override;
109 void SAL_CALL setFillDefault( ::sal_Int16 _filldefault ) override;
110 ::sal_Int16 SAL_CALL getRestart() override;
111 void SAL_CALL setRestart( ::sal_Int16 _restart ) override;
112 ::sal_Int16 SAL_CALL getRestartDefault() override;
113 void SAL_CALL setRestartDefault( ::sal_Int16 _restartdefault ) override;
114 double SAL_CALL getAcceleration() override;
115 void SAL_CALL setAcceleration( double _acceleration ) override;
116 double SAL_CALL getDecelerate() override;
117 void SAL_CALL setDecelerate( double _decelerate ) override;
118 sal_Bool SAL_CALL getAutoReverse() override;
119 void SAL_CALL setAutoReverse( sal_Bool _autoreverse ) override;
120 Sequence< NamedValue > SAL_CALL getUserData() override;
121 void SAL_CALL setUserData( const Sequence< NamedValue >& _userdata ) override;
123 // XElementAccess
124 virtual Type SAL_CALL getElementType() override;
125 virtual sal_Bool SAL_CALL hasElements() override;
127 // XEnumerationAccess
128 virtual Reference< XEnumeration > SAL_CALL createEnumeration() override;
130 // XTimeContainer
131 Reference< XAnimationNode > SAL_CALL insertBefore( const Reference< XAnimationNode >& newChild, const Reference< XAnimationNode >& refChild ) override;
132 Reference< XAnimationNode > SAL_CALL insertAfter( const Reference< XAnimationNode >& newChild, const Reference< XAnimationNode >& refChild ) override;
133 Reference< XAnimationNode > SAL_CALL replaceChild( const Reference< XAnimationNode >& newChild, const Reference< XAnimationNode >& oldChild ) override;
134 Reference< XAnimationNode > SAL_CALL removeChild( const Reference< XAnimationNode >& oldChild ) override;
135 Reference< XAnimationNode > SAL_CALL appendChild( const Reference< XAnimationNode >& newChild ) override;
137 private:
138 // our first, last and only protection from multi-threads!
139 std::mutex maMutex;
141 sal_Int16 mnPresetClass;
142 WeakReference<XInterface> mxParent;
144 Any maBegin, maDuration, maEnd, maEndSync, maRepeatCount, maRepeatDuration, maTarget;
145 sal_Int16 mnFill, mnFillDefault, mnRestart, mnRestartDefault;
146 double mfAcceleration, mfDecelerate;
147 bool mbAutoReverse;
148 Sequence< NamedValue > maUserData;
150 Reference< XAnimate > mxFirstNode;
155 Reference< XInterface > RandomAnimationNode_createInstance( sal_Int16 nPresetClass )
157 Reference< XInterface > xInt( static_cast<XWeak*>( new RandomAnimationNode( nPresetClass ) ) );
158 return xInt;
161 RandomAnimationNode::RandomAnimationNode( const RandomAnimationNode& rNode )
162 : RandomAnimationNodeBase(rNode),
163 mnPresetClass( rNode.mnPresetClass ),
164 maBegin( rNode.maBegin ),
165 maDuration( rNode.maDuration ),
166 maEnd( rNode.maEnd ),
167 maEndSync( rNode.maEndSync ),
168 maRepeatCount( rNode.maRepeatCount ),
169 maRepeatDuration( rNode.maRepeatDuration ),
170 maTarget( rNode.maTarget ),
171 mnFill( rNode.mnFill ),
172 mnFillDefault( rNode.mnFillDefault ),
173 mnRestart( rNode.mnRestart ),
174 mnRestartDefault( rNode.mnRestartDefault ),
175 mfAcceleration( rNode.mfAcceleration ),
176 mfDecelerate( rNode.mfDecelerate ),
177 mbAutoReverse( rNode.mbAutoReverse ),
178 maUserData( rNode.maUserData )
182 RandomAnimationNode::RandomAnimationNode( sal_Int16 nPresetClass )
184 init( nPresetClass );
187 RandomAnimationNode::RandomAnimationNode()
189 init( 1 );
192 void RandomAnimationNode::init( sal_Int16 nPresetClass )
194 mnPresetClass = nPresetClass;
195 mnFill = AnimationFill::DEFAULT;
196 mnFillDefault = AnimationFill::INHERIT;
197 mnRestart = AnimationRestart::DEFAULT;
198 mnRestartDefault = AnimationRestart::INHERIT;
199 mfAcceleration = 0.0;
200 mfDecelerate = 0.0;
201 mbAutoReverse = false;
204 // XInitialization
205 void SAL_CALL RandomAnimationNode::initialize( const Sequence< Any >& aArguments )
207 if( aArguments.getLength() != 1 )
208 throw IllegalArgumentException();
210 if( aArguments[0].getValueType() == ::cppu::UnoType<sal_Int16>::get() )
212 aArguments[0] >>= mnPresetClass;
214 else if( aArguments[0].getValueType() != ::cppu::UnoType<ParagraphTarget>::get() )
216 Reference< XShape > xShape;
217 aArguments[0] >>= xShape;
218 if( !xShape.is() )
219 throw IllegalArgumentException();
221 maTarget = aArguments[0];
224 // XAnimationNode
225 sal_Int16 SAL_CALL RandomAnimationNode::getType()
227 return css::animations::AnimationNodeType::PAR;
230 // XAnimationNode
231 Any SAL_CALL RandomAnimationNode::getBegin()
233 std::unique_lock aGuard( maMutex );
234 return maBegin;
237 // XAnimationNode
238 void SAL_CALL RandomAnimationNode::setBegin( const Any& _begin )
240 std::unique_lock aGuard( maMutex );
241 maBegin = _begin;
244 // XAnimationNode
245 Any SAL_CALL RandomAnimationNode::getDuration()
247 std::unique_lock aGuard( maMutex );
248 return maDuration;
251 // XAnimationNode
252 void SAL_CALL RandomAnimationNode::setDuration( const Any& _duration )
254 std::unique_lock aGuard( maMutex );
255 maDuration = _duration;
258 // XAnimationNode
259 Any SAL_CALL RandomAnimationNode::getEnd()
261 std::unique_lock aGuard( maMutex );
262 return maEnd;
265 // XAnimationNode
266 void SAL_CALL RandomAnimationNode::setEnd( const Any& _end )
268 std::unique_lock aGuard( maMutex );
269 maEnd = _end;
272 // XAnimationNode
273 Any SAL_CALL RandomAnimationNode::getEndSync()
275 std::unique_lock aGuard( maMutex );
276 return maEndSync;
279 // XAnimationNode
280 void SAL_CALL RandomAnimationNode::setEndSync( const Any& _endsync )
282 std::unique_lock aGuard( maMutex );
283 maEndSync = _endsync;
286 // XAnimationNode
287 Any SAL_CALL RandomAnimationNode::getRepeatCount()
289 std::unique_lock aGuard( maMutex );
290 return maRepeatCount;
293 // XAnimationNode
294 void SAL_CALL RandomAnimationNode::setRepeatCount( const Any& _repeatcount )
296 std::unique_lock aGuard( maMutex );
297 maRepeatCount = _repeatcount;
300 // XAnimationNode
301 Any SAL_CALL RandomAnimationNode::getRepeatDuration()
303 std::unique_lock aGuard( maMutex );
304 return maRepeatDuration;
307 // XAnimationNode
308 void SAL_CALL RandomAnimationNode::setRepeatDuration( const Any& _repeatduration )
310 std::unique_lock aGuard( maMutex );
311 maRepeatDuration = _repeatduration;
314 // XAnimationNode
315 sal_Int16 SAL_CALL RandomAnimationNode::getFill()
317 std::unique_lock aGuard( maMutex );
318 return mnFill;
321 // XAnimationNode
322 void SAL_CALL RandomAnimationNode::setFill( sal_Int16 _fill )
324 std::unique_lock aGuard( maMutex );
325 mnFill = _fill;
328 // XAnimationNode
329 sal_Int16 SAL_CALL RandomAnimationNode::getFillDefault()
331 std::unique_lock aGuard( maMutex );
332 return mnFillDefault;
335 // XAnimationNode
336 void SAL_CALL RandomAnimationNode::setFillDefault( sal_Int16 _filldefault )
338 std::unique_lock aGuard( maMutex );
339 mnFillDefault = _filldefault;
342 // XAnimationNode
343 sal_Int16 SAL_CALL RandomAnimationNode::getRestart()
345 std::unique_lock aGuard( maMutex );
346 return mnRestart;
349 // XAnimationNode
350 void SAL_CALL RandomAnimationNode::setRestart( sal_Int16 _restart )
352 std::unique_lock aGuard( maMutex );
353 mnRestart = _restart;
356 // XAnimationNode
357 sal_Int16 SAL_CALL RandomAnimationNode::getRestartDefault()
359 std::unique_lock aGuard( maMutex );
360 return mnRestartDefault;
363 // XAnimationNode
364 void SAL_CALL RandomAnimationNode::setRestartDefault( sal_Int16 _restartdefault )
366 std::unique_lock aGuard( maMutex );
367 mnRestartDefault = _restartdefault;
370 // XAnimationNode
371 double SAL_CALL RandomAnimationNode::getAcceleration()
373 std::unique_lock aGuard( maMutex );
374 return mfAcceleration;
377 // XAnimationNode
378 void SAL_CALL RandomAnimationNode::setAcceleration( double _acceleration )
380 std::unique_lock aGuard( maMutex );
381 mfAcceleration = _acceleration;
384 // XAnimationNode
385 double SAL_CALL RandomAnimationNode::getDecelerate()
387 std::unique_lock aGuard( maMutex );
388 return mfDecelerate;
391 // XAnimationNode
392 void SAL_CALL RandomAnimationNode::setDecelerate( double _decelerate )
394 std::unique_lock aGuard( maMutex );
395 mfDecelerate = _decelerate;
398 // XAnimationNode
399 sal_Bool SAL_CALL RandomAnimationNode::getAutoReverse()
401 std::unique_lock aGuard( maMutex );
402 return mbAutoReverse;
405 // XAnimationNode
406 void SAL_CALL RandomAnimationNode::setAutoReverse( sal_Bool _autoreverse )
408 std::unique_lock aGuard( maMutex );
409 mbAutoReverse = _autoreverse;
412 Sequence< NamedValue > SAL_CALL RandomAnimationNode::getUserData()
414 std::unique_lock aGuard( maMutex );
415 return maUserData;
418 void SAL_CALL RandomAnimationNode::setUserData( const Sequence< NamedValue >& _userdata )
420 std::unique_lock aGuard( maMutex );
421 maUserData = _userdata;
424 // XChild
425 Reference< XInterface > SAL_CALL RandomAnimationNode::getParent()
427 std::unique_lock aGuard( maMutex );
428 return mxParent.get();
431 // XChild
432 void SAL_CALL RandomAnimationNode::setParent( const Reference< XInterface >& Parent )
434 std::unique_lock aGuard( maMutex );
435 mxParent = Parent;
438 // XCloneable
439 Reference< XCloneable > SAL_CALL RandomAnimationNode::createClone()
441 Reference< XCloneable > xNewNode( new RandomAnimationNode( *this ) );
442 return xNewNode;
445 // XElementAccess
446 Type SAL_CALL RandomAnimationNode::getElementType()
448 return cppu::UnoType<XAnimationNode>::get();
451 // XElementAccess
452 sal_Bool SAL_CALL RandomAnimationNode::hasElements()
454 return true;
457 // XEnumerationAccess
458 Reference< XEnumeration > SAL_CALL RandomAnimationNode::createEnumeration()
460 std::unique_lock aGuard( maMutex );
462 if( !maTarget.hasValue() && mxFirstNode.is() )
464 Any aTarget( mxFirstNode->getTarget() );
465 if( aTarget.hasValue() )
467 maTarget = aTarget;
468 mxFirstNode.clear();
472 Reference< XEnumeration > xEnum;
474 Reference< XEnumerationAccess > aEnumAccess( CustomAnimationPresets::getCustomAnimationPresets().getRandomPreset( mnPresetClass ), UNO_QUERY );
476 if( aEnumAccess.is() )
478 Reference< XEnumeration > xEnumeration = aEnumAccess->createEnumeration();
479 if( xEnumeration.is() )
481 while( xEnumeration->hasMoreElements() )
483 Reference< XAnimate > xAnimate( xEnumeration->nextElement(), UNO_QUERY );
484 if( xAnimate.is() )
485 xAnimate->setTarget( maTarget );
488 xEnum = aEnumAccess->createEnumeration();
490 else
492 // no presets? give empty node!
493 Reference< XParallelTimeContainer > xTimeContainer = ParallelTimeContainer::create( comphelper::getProcessComponentContext() );
494 xEnum = xTimeContainer->createEnumeration();
497 return xEnum;
500 // XTimeContainer
501 Reference< XAnimationNode > SAL_CALL RandomAnimationNode::insertBefore( const Reference< XAnimationNode >& newChild, const Reference< XAnimationNode >& )
503 return appendChild( newChild );
506 // XTimeContainer
507 Reference< XAnimationNode > SAL_CALL RandomAnimationNode::insertAfter( const Reference< XAnimationNode >& newChild, const Reference< XAnimationNode >& )
509 return appendChild( newChild );
512 // XTimeContainer
513 Reference< XAnimationNode > SAL_CALL RandomAnimationNode::replaceChild( const Reference< XAnimationNode >& newChild, const Reference< XAnimationNode >& )
515 return appendChild( newChild );
518 // XTimeContainer
519 Reference< XAnimationNode > SAL_CALL RandomAnimationNode::removeChild( const Reference< XAnimationNode >& oldChild )
521 return oldChild;
524 // XTimeContainer
525 Reference< XAnimationNode > SAL_CALL RandomAnimationNode::appendChild( const Reference< XAnimationNode >& newChild )
527 Reference< XAnimate > xAnimate( newChild, UNO_QUERY );
528 if( xAnimate.is() )
530 Any aTarget( xAnimate->getTarget() );
531 if( aTarget.hasValue() )
532 maTarget = aTarget;
535 if( !maTarget.hasValue() && !mxFirstNode.is() )
536 mxFirstNode = xAnimate;
538 return newChild;
541 // XServiceInfo
542 OUString RandomAnimationNode::getImplementationName()
544 return "sd::RandomAnimationNode" ;
547 // XServiceInfo
548 sal_Bool RandomAnimationNode::supportsService(const OUString& ServiceName)
550 return cppu::supportsService(this, ServiceName);
553 // XServiceInfo
554 Sequence< OUString > RandomAnimationNode::getSupportedServiceNames()
556 return { "com.sun.star.animations.ParallelTimeContainer", "com.sun.star.comp.sd.RandomAnimationNode" };
561 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
562 RandomAnimationNode_get_implementation(css::uno::XComponentContext*,
563 css::uno::Sequence<css::uno::Any> const &)
565 return cppu::acquire(new sd::RandomAnimationNode());
569 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */