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

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 "updater.h"
0026 
0027 #include <assert.h>
0028 
0029 #include <QTimer>
0030 #include "image.h"
0031 
0032 namespace khtmlImLoad
0033 {
0034 
0035 /**
0036  We keep 10 separate tables for each of ten 100ms portions of a second.
0037  Each entry is scheduled to be delivered 5 slots ahead
0038 */
0039 
0040 Updater::Updater()
0041 {
0042     timePortion = 0;
0043 
0044     updatePusher = new QTimer(this);
0045     connect(updatePusher, SIGNAL(timeout()),
0046             this,         SLOT(pushUpdates()));
0047 }
0048 
0049 void Updater::haveUpdates(Image *frame)
0050 {
0051     assert(frame);
0052     int schedulePortion = (timePortion + 1) % 10;
0053     frames[schedulePortion].append(frame);
0054     if (!updatePusher->isActive()) {
0055         updatePusher->start(100);
0056     }
0057 }
0058 
0059 bool Updater::updatesPending()
0060 {
0061     for (int i = 0; i < 10; ++i)
0062         if (!frames[i].isEmpty()) {
0063             return true;
0064         }
0065 
0066     return false;
0067 }
0068 
0069 void Updater::destroyed(Image *frame)
0070 {
0071     for (int i = 0; i < 10; ++i) {
0072         int pos = frames[i].indexOf(frame);
0073         if (pos != -1) {
0074             frames[i].remove(pos);
0075         }
0076     }
0077 
0078     if (!updatesPending()) {
0079         updatePusher->stop();
0080     }
0081 }
0082 
0083 void Updater::pushUpdates()
0084 {
0085     timePortion++;
0086     if (timePortion >= 10) {
0087         timePortion = 0;
0088     }
0089 
0090     //Notify all images for the given slice.
0091     QVector<Image *>::const_iterator iter;
0092     for (iter = frames[timePortion].constBegin();
0093             iter != frames[timePortion].constEnd(); ++iter) {
0094         (*iter)->notifyPerformUpdate();
0095     }
0096 
0097     //Dump the contents of the table, everything delivered
0098     frames[timePortion].clear();
0099 
0100     if (!updatesPending()) {
0101         updatePusher->stop();
0102     }
0103 }
0104 
0105 }
0106 
0107 #include "moc_updater.cpp"