summaryrefslogtreecommitdiffstats
path: root/ksvg/impl/SVGTimeScheduler.cpp
blob: b38eceaaf33ac56ff90911340e7c45e4098440bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
    Copyright (C) 2003 KSVG Team
    This file is part of the KDE project

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "KSVGCanvas.h"
#include "CanvasItem.h"
#include "SVGShapeImpl.h"
#include "SVGDocumentImpl.h"
#include "SVGTimeScheduler.moc"

using namespace KSVG;

SVGTimer::SVGTimer(TQObject *scheduler, unsigned int ms, bool singleShot)
{
	m_ms = ms;
	m_singleShot = singleShot;
	m_timer = new TQTimer(scheduler);
}

SVGTimer::~SVGTimer()
{
	delete m_timer;
}

bool SVGTimer::operator==(const TQTimer *timer)
{
	return (m_timer == timer);
}

const TQTimer *SVGTimer::qtimer() const
{
	return m_timer;
}

void SVGTimer::start(TQObject *receiver, const char *member)
{
	TQObject::connect(m_timer, TQT_SIGNAL(timeout()), receiver, member);
	m_timer->start(m_ms, m_singleShot);
}

void SVGTimer::stop()
{
	m_timer->stop();
}

bool SVGTimer::isActive() const
{
	return m_timer->isActive();
}

unsigned int SVGTimer::ms() const
{
	return m_ms;
}

bool SVGTimer::singleShot() const
{
	return m_singleShot;
}

void SVGTimer::notifyAll()
{
	if(m_notifyList.isEmpty())
		return;

	TQValueList<SVGElementImpl *> elements;
	for(unsigned int i = m_notifyList.count();i > 0; i--)
	{
		SVGElementImpl *element = m_notifyList[i - 1];
		if(!element)
			continue;

		SVGAnimationElementImpl *animation = dynamic_cast<SVGAnimationElementImpl *>(element);
		if(animation)
		{
			animation->handleTimerEvent();

			SVGElementImpl *target = animation->targetElement();
			if(!elements.contains(target))
				elements.append(target);
		}
	}

	// Optimized update logic (to avoid 4 updates, on the same element)
	TQValueList<SVGElementImpl *>::iterator it2;
	for(it2 = elements.begin(); it2 != elements.end(); ++it2)
	{
		SVGShapeImpl *shape = dynamic_cast<SVGShapeImpl *>(*it2);
		if(shape && shape->item())
			shape->item()->update(UPDATE_TRANSFORM);
	}
}

void SVGTimer::addNotify(SVGElementImpl *element)
{
	m_notifyList.append(element);
}

void SVGTimer::removeNotify(SVGElementImpl *element)
{
	m_notifyList.remove(element);
	
	if(m_notifyList.isEmpty())
		stop();
}

const unsigned int SVGTimeScheduler::staticTimerInterval = 15; // milliseconds

SVGTimeScheduler::SVGTimeScheduler(SVGDocumentImpl *doc) : TQObject(), m_doc(doc)
{
	// Create static interval timers but don't start it yet!
	m_intervalTimer = new SVGTimer(this, staticTimerInterval, false);	
	m_creationTime.start();
}

SVGTimeScheduler::~SVGTimeScheduler()
{
	// Usually singleShot timers cleanup themselves, after usage
	SVGTimerList::iterator it;
	for(it = m_timerList.begin(); it != m_timerList.end(); ++it)
	{
		SVGTimer *svgTimer = *it;
		delete svgTimer;
	}
	delete m_intervalTimer;
}

void SVGTimeScheduler::addTimer(SVGElementImpl *element, unsigned int ms)
{
	SVGTimer *svgTimer = new SVGTimer(this, ms, true);
	svgTimer->addNotify(element);
	m_timerList.append(svgTimer);
}

void SVGTimeScheduler::connectIntervalTimer(SVGElementImpl *element)
{
	m_intervalTimer->addNotify(element);
}

void SVGTimeScheduler::disconnectIntervalTimer(SVGElementImpl *element)
{
	m_intervalTimer->removeNotify(element);
}

void SVGTimeScheduler::startAnimations()
{
	SVGTimerList::iterator it;
	for(it = m_timerList.begin(); it != m_timerList.end(); ++it)
	{
		SVGTimer *svgTimer = *it;
		if(svgTimer && !svgTimer->isActive())
			svgTimer->start(this, TQT_SLOT(slotTimerNotify()));
	}
}

void SVGTimeScheduler::toggleAnimations()
{
	if(m_intervalTimer->isActive())
		m_intervalTimer->stop();
	else
		m_intervalTimer->start(this, TQT_SLOT(slotTimerNotify()));
}

bool SVGTimeScheduler::animationsPaused() const
{
	return !m_intervalTimer->isActive();
}

void SVGTimeScheduler::slotTimerNotify()
{
	TQTimer *senderTimer = const_cast<TQTimer *>(static_cast<const TQTimer *>(sender()));

	SVGTimer *svgTimer = 0;
	SVGTimerList::iterator it;
	for(it = m_timerList.begin(); it != m_timerList.end(); ++it)
	{
		SVGTimer *cur = *it;
		if(*cur == senderTimer)
		{
			svgTimer = cur;
			break;
		}
	}

	if(!svgTimer)
	{
		svgTimer = (*m_intervalTimer == senderTimer) ? m_intervalTimer : 0;
	
		if(!svgTimer)
			return;
	}

	svgTimer->notifyAll();

	// Animations need direct updates
	if(m_doc->canvas())
		m_doc->canvas()->update();
	emit m_doc->finishedRendering();

	if(svgTimer->singleShot())
	{
		m_timerList.remove(svgTimer);
		delete svgTimer;
	}

	// The singleShot timers of ie. <animate> with begin="3s" are notified
	// by the previous call, and now all connections to the interval timer
	// are created and now we just need to fire that timer (Niko)
	if(svgTimer != m_intervalTimer && !m_intervalTimer->isActive())
		m_intervalTimer->start(this, TQT_SLOT(slotTimerNotify()));
}

float SVGTimeScheduler::elapsed() const
{
	return float(m_creationTime.elapsed()) / 1000.0;
}