File indexing completed on 2024-05-19 15:12:17

0001 /*
0002     Large image displaying library.
0003 
0004     Copyright (C) 2004 Maks Orlovich (maksim@kde.org)
0005 
0006     Permission is hereby granted, free of charge, to any person obtaining a copy
0007     of this software and associated documentation files (the "Software"), to deal
0008     in the Software without restriction, including without limitation the rights
0009     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0010     copies of the Software, and to permit persons to whom the Software is
0011     furnished to do so, subject to the following conditions:
0012 
0013     The above copyright notice and this permission notice shall be included in
0014     all copies or substantial portions of the Software.
0015 
0016     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0017     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0018     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
0019     AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
0020     AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0021     CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0022 
0023 */
0024 
0025 #include "animtimer.h"
0026 #include "animprovider.h"
0027 
0028 #include <QTimer>
0029 #include <QVector>
0030 
0031 namespace khtmlImLoad
0032 {
0033 
0034 AnimTimer::AnimTimer()
0035 {
0036     animTicks = new QTimer(this);
0037     connect(animTicks, SIGNAL(timeout()),
0038             this,      SLOT(tick()));
0039 }
0040 
0041 void AnimTimer::nextFrameIn(AnimProvider *provider, int ms)
0042 {
0043     if (pending.contains(provider)) {
0044         return;
0045     }
0046 
0047     pending[provider] = ms;
0048 
0049     if (!animTicks->isActive()) {
0050         animTicks->start(10); //### lower resolution, perhaps?
0051         lastTime = QTime::currentTime();
0052     }
0053 }
0054 
0055 void AnimTimer::tick()
0056 {
0057     QTime newTime = QTime::currentTime();
0058     int   change  = lastTime.msecsTo(newTime);
0059     lastTime      = newTime;
0060     if (change < 1) {
0061         change = 1;    //Just in case someone changes the clock or something
0062     }
0063 
0064     QVector<AnimProvider *> toHandle;
0065 
0066     for (QMap<AnimProvider *, int>::iterator iter = pending.begin();
0067             iter != pending.end(); ++iter) {
0068         iter.value() -= change;
0069         if (iter.value() <= 0) {
0070             toHandle.append(iter.key());
0071         }
0072     }
0073 
0074     //Notify all images for the given slice.
0075     QVector<AnimProvider *>::const_iterator iter;
0076     for (iter  = toHandle.constBegin();
0077             iter != toHandle.constEnd(); ++iter) {
0078         pending.remove(*iter);
0079         (*iter)->switchFrame();
0080     }
0081 
0082     if (pending.isEmpty()) {
0083         animTicks->stop();
0084     }
0085 }
0086 
0087 void AnimTimer::destroyed(AnimProvider *provider)
0088 {
0089     pending.remove(provider);
0090 }
0091 
0092 }
0093 
0094 #include "moc_animtimer.cpp"