tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / slideshow / source / engine / activities / discreteactivitybase.cxx
blob66e340d07b6e6af8350d1c3701fe24644b2d083f
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 .
21 #include <comphelper/diagnose_ex.hxx>
23 #include "discreteactivitybase.hxx"
26 namespace slideshow::internal
28 DiscreteActivityBase::DiscreteActivityBase( const ActivityParameters& rParms ) :
29 ActivityBase( rParms ),
30 mpWakeupEvent( rParms.mpWakeupEvent ),
31 maDiscreteTimes( rParms.maDiscreteTimes ),
32 mnSimpleDuration( rParms.mnMinDuration ),
33 mnCurrPerformCalls( 0 )
35 ENSURE_OR_THROW( mpWakeupEvent,
36 "DiscreteActivityBase::DiscreteActivityBase(): Invalid wakeup event" );
38 ENSURE_OR_THROW( !maDiscreteTimes.empty(),
39 "DiscreteActivityBase::DiscreteActivityBase(): time vector is empty, why do you create me?" );
41 #ifdef DBG_UTIL
42 // check parameters: rDiscreteTimes must be sorted in
43 // ascending order, and contain values only from the range
44 // [0,1]
45 for( ::std::size_t i=1, len=maDiscreteTimes.size(); i<len; ++i )
47 if( maDiscreteTimes[i] < 0.0 ||
48 maDiscreteTimes[i] > 1.0 ||
49 maDiscreteTimes[i-1] < 0.0 ||
50 maDiscreteTimes[i-1] > 1.0 )
52 ENSURE_OR_THROW( false, "DiscreteActivityBase::DiscreteActivityBase(): time values not within [0,1] range!" );
55 if( maDiscreteTimes[i-1] > maDiscreteTimes[i] )
56 ENSURE_OR_THROW( false, "DiscreteActivityBase::DiscreteActivityBase(): time vector is not sorted in ascending order!" );
59 // TODO(E2): check this also in production code?
60 #endif
63 void DiscreteActivityBase::startAnimation()
65 // start timer on wakeup event
66 mpWakeupEvent->start();
69 sal_uInt32 DiscreteActivityBase::calcFrameIndex( sal_uInt32 nCurrCalls,
70 ::std::size_t nVectorSize ) const
72 if( isAutoReverse() )
74 // every full repeat run consists of one
75 // forward and one backward traversal.
76 sal_uInt32 nFrameIndex( nCurrCalls % (2*nVectorSize) );
78 // nFrameIndex values >= nVectorSize belong to
79 // the backward traversal
80 if( nFrameIndex >= nVectorSize )
81 nFrameIndex = 2*nVectorSize - nFrameIndex; // invert sweep
83 return nFrameIndex;
85 else
87 return nCurrCalls % nVectorSize ;
91 sal_uInt32 DiscreteActivityBase::calcRepeatCount( sal_uInt32 nCurrCalls,
92 ::std::size_t nVectorSize ) const
94 if( isAutoReverse() )
95 return nCurrCalls / (2*nVectorSize); // we've got 2 cycles per repeat
96 else
97 return nCurrCalls / nVectorSize;
100 bool DiscreteActivityBase::perform()
102 // call base class, for start() calls and end handling
103 if( !ActivityBase::perform() )
104 return false; // done, we're ended
106 const ::std::size_t nVectorSize( maDiscreteTimes.size() );
108 // actually perform something
109 // ==========================
111 // TODO(Q3): Refactor this mess
113 // call derived class with current frame index (modulo
114 // vector size, to cope with repeats)
115 perform( calcFrameIndex( mnCurrPerformCalls, nVectorSize ),
116 calcRepeatCount( mnCurrPerformCalls, nVectorSize ) );
118 // calc next index
119 ++mnCurrPerformCalls;
121 // calc currently reached repeat count
122 double nCurrRepeat( double(mnCurrPerformCalls) / nVectorSize );
124 // if auto-reverse is specified, halve the
125 // effective repeat count, since we pass every
126 // repeat run twice: once forward, once backward.
127 if( isAutoReverse() )
128 nCurrRepeat /= 2.0;
130 // schedule next frame, if either repeat is indefinite
131 // (repeat forever), or we've not yet reached the requested
132 // repeat count
133 if( !isRepeatCountValid() ||
134 nCurrRepeat < getRepeatCount() )
136 // add wake-up event to queue (modulo
137 // vector size, to cope with repeats).
139 // repeat is handled locally, only apply acceleration/deceleration.
140 // Scale time vector with simple duration, offset with full repeat
141 // times.
143 // Somewhat condensed, the argument for setNextTimeout below could
144 // be written as
146 // mnSimpleDuration*(nFullRepeats + calcAcceleratedTime( currentRepeatTime )),
148 // with currentRepeatTime = maDiscreteTimes[ currentRepeatIndex ]
150 // Note that calcAcceleratedTime() is only applied to the current repeat's value,
151 // not to the total resulting time. This is in accordance with the SMIL spec.
153 mpWakeupEvent->setNextTimeout(
154 mnSimpleDuration*(
155 calcRepeatCount(
156 mnCurrPerformCalls,
157 nVectorSize ) +
158 calcAcceleratedTime(
159 maDiscreteTimes[
160 calcFrameIndex(
161 mnCurrPerformCalls,
162 nVectorSize ) ] ) ) );
164 getEventQueue().addEvent( mpWakeupEvent );
166 else
168 // release event reference (relation to wakeup event
169 // is circular!)
170 mpWakeupEvent.reset();
172 // done with this activity
173 endActivity();
176 return false; // remove from queue, will be added back by the wakeup event.
179 void DiscreteActivityBase::dispose()
181 // dispose event
182 if( mpWakeupEvent )
183 mpWakeupEvent->dispose();
185 // release references
186 mpWakeupEvent.reset();
188 ActivityBase::dispose();
192 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */