| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtCore/qthreadstorage.h> |
| 5 | |
| 6 | #include "private/qabstractanimationjob_p.h" |
| 7 | #include "private/qanimationgroupjob_p.h" |
| 8 | #include "private/qanimationjobutil_p.h" |
| 9 | #include "private/qqmlengine_p.h" |
| 10 | #include "private/qqmlglobal_p.h" |
| 11 | #include "private/qdoubleendedlist_p.h" |
| 12 | |
| 13 | QT_BEGIN_NAMESPACE |
| 14 | |
| 15 | #ifndef QT_NO_THREAD |
| 16 | Q_GLOBAL_STATIC(QThreadStorage<QQmlAnimationTimer *>, animationTimer) |
| 17 | #endif |
| 18 | |
| 19 | DEFINE_BOOL_CONFIG_OPTION(animationTickDump, QML_ANIMATION_TICK_DUMP); |
| 20 | |
| 21 | QAnimationJobChangeListener::~QAnimationJobChangeListener() |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | QQmlAnimationTimer::QQmlAnimationTimer() : |
| 26 | QAbstractAnimationTimer(), lastTick(0), |
| 27 | currentAnimationIdx(0), insideTick(false), |
| 28 | startAnimationPending(false), stopTimerPending(false), |
| 29 | runningLeafAnimations(0) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | void QQmlAnimationTimer::unsetJobTimer(QAbstractAnimationJob *animation) |
| 34 | { |
| 35 | if (!animation) |
| 36 | return; |
| 37 | if (animation->m_timer == this) |
| 38 | animation->m_timer = nullptr; |
| 39 | |
| 40 | if (animation->m_isPause) |
| 41 | runningPauseAnimations.removeOne(t: animation); |
| 42 | |
| 43 | if (animation->isGroup()) { |
| 44 | QAnimationGroupJob *group = static_cast<QAnimationGroupJob *>(animation); |
| 45 | if (const auto children = group->children()) { |
| 46 | for (auto *child : *children) |
| 47 | unsetJobTimer(animation: child); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | QQmlAnimationTimer::~QQmlAnimationTimer() |
| 53 | { |
| 54 | for (const auto &animation : std::as_const(t&: animations)) |
| 55 | unsetJobTimer(animation); |
| 56 | for (const auto &animation : std::as_const(t&: animationsToStart)) |
| 57 | unsetJobTimer(animation); |
| 58 | for (const auto &animation : std::as_const(t&: runningPauseAnimations)) |
| 59 | unsetJobTimer(animation); |
| 60 | } |
| 61 | |
| 62 | QQmlAnimationTimer *QQmlAnimationTimer::instance(bool create) |
| 63 | { |
| 64 | QQmlAnimationTimer *inst; |
| 65 | if (create && !animationTimer()->hasLocalData()) { |
| 66 | inst = new QQmlAnimationTimer; |
| 67 | animationTimer()->setLocalData(inst); |
| 68 | } else { |
| 69 | inst = animationTimer() ? animationTimer()->localData() : 0; |
| 70 | } |
| 71 | return inst; |
| 72 | } |
| 73 | |
| 74 | QQmlAnimationTimer *QQmlAnimationTimer::instance() |
| 75 | { |
| 76 | return instance(create: true); |
| 77 | } |
| 78 | |
| 79 | void QQmlAnimationTimer::ensureTimerUpdate() |
| 80 | { |
| 81 | QUnifiedTimer *instU = QUnifiedTimer::instance(create: false); |
| 82 | if (instU && isPaused) |
| 83 | instU->updateAnimationTimers(); |
| 84 | } |
| 85 | |
| 86 | void QQmlAnimationTimer::updateAnimationsTime(qint64 delta) |
| 87 | { |
| 88 | //setCurrentTime can get this called again while we're the for loop. At least with pauseAnimations |
| 89 | if (insideTick) |
| 90 | return; |
| 91 | |
| 92 | lastTick += delta; |
| 93 | |
| 94 | //we make sure we only call update time if the time has actually changed |
| 95 | //it might happen in some cases that the time doesn't change because events are delayed |
| 96 | //when the CPU load is high |
| 97 | if (delta) { |
| 98 | insideTick = true; |
| 99 | for (currentAnimationIdx = 0; currentAnimationIdx < animations.size(); ++currentAnimationIdx) { |
| 100 | QAbstractAnimationJob *animation = animations.at(i: currentAnimationIdx); |
| 101 | int elapsed = animation->m_totalCurrentTime |
| 102 | + (animation->direction() == QAbstractAnimationJob::Forward ? delta : -delta); |
| 103 | animation->setCurrentTime(elapsed); |
| 104 | } |
| 105 | if (animationTickDump()) { |
| 106 | qDebug() << "***** Dumping Animation Tree ***** ( tick:" << lastTick << "delta:" << delta << ")" ; |
| 107 | for (int i = 0; i < animations.size(); ++i) |
| 108 | qDebug() << animations.at(i); |
| 109 | } |
| 110 | insideTick = false; |
| 111 | currentAnimationIdx = 0; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void QQmlAnimationTimer::updateAnimationTimer() |
| 116 | { |
| 117 | restartAnimationTimer(); |
| 118 | } |
| 119 | |
| 120 | void QQmlAnimationTimer::restartAnimationTimer() |
| 121 | { |
| 122 | if (runningLeafAnimations == 0 && !runningPauseAnimations.isEmpty()) |
| 123 | QUnifiedTimer::pauseAnimationTimer(timer: this, duration: closestPauseAnimationTimeToFinish()); |
| 124 | else if (isPaused) |
| 125 | QUnifiedTimer::resumeAnimationTimer(timer: this); |
| 126 | else if (!isRegistered) |
| 127 | QUnifiedTimer::startAnimationTimer(timer: this); |
| 128 | } |
| 129 | |
| 130 | void QQmlAnimationTimer::startAnimations() |
| 131 | { |
| 132 | if (!startAnimationPending) |
| 133 | return; |
| 134 | startAnimationPending = false; |
| 135 | //force timer to update, which prevents large deltas for our newly added animations |
| 136 | QUnifiedTimer::instance()->maybeUpdateAnimationsToCurrentTime(); |
| 137 | |
| 138 | //we transfer the waiting animations into the "really running" state |
| 139 | animations += animationsToStart; |
| 140 | animationsToStart.clear(); |
| 141 | if (!animations.isEmpty()) |
| 142 | restartAnimationTimer(); |
| 143 | } |
| 144 | |
| 145 | void QQmlAnimationTimer::stopTimer() |
| 146 | { |
| 147 | stopTimerPending = false; |
| 148 | bool pendingStart = startAnimationPending && animationsToStart.size() > 0; |
| 149 | if (animations.isEmpty() && !pendingStart) { |
| 150 | QUnifiedTimer::resumeAnimationTimer(timer: this); |
| 151 | QUnifiedTimer::stopAnimationTimer(timer: this); |
| 152 | // invalidate the start reference time |
| 153 | lastTick = 0; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void QQmlAnimationTimer::registerAnimation(QAbstractAnimationJob *animation, bool isTopLevel) |
| 158 | { |
| 159 | if (animation->userControlDisabled()) |
| 160 | return; |
| 161 | |
| 162 | registerRunningAnimation(animation); |
| 163 | if (isTopLevel) { |
| 164 | Q_ASSERT(!animation->m_hasRegisteredTimer); |
| 165 | animation->m_hasRegisteredTimer = true; |
| 166 | animationsToStart << animation; |
| 167 | if (!startAnimationPending) { |
| 168 | startAnimationPending = true; |
| 169 | QMetaObject::invokeMethod(obj: this, member: "startAnimations" , c: Qt::QueuedConnection); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void QQmlAnimationTimer::unregisterAnimation(QAbstractAnimationJob *animation) |
| 175 | { |
| 176 | unregisterRunningAnimation(animation); |
| 177 | |
| 178 | if (!animation->m_hasRegisteredTimer) |
| 179 | return; |
| 180 | |
| 181 | int idx = animations.indexOf(t: animation); |
| 182 | if (idx != -1) { |
| 183 | animations.removeAt(i: idx); |
| 184 | // this is needed if we unregister an animation while its running |
| 185 | if (idx <= currentAnimationIdx) |
| 186 | --currentAnimationIdx; |
| 187 | |
| 188 | if (animations.isEmpty() && !stopTimerPending) { |
| 189 | stopTimerPending = true; |
| 190 | QMetaObject::invokeMethod(obj: this, member: "stopTimer" , c: Qt::QueuedConnection); |
| 191 | } |
| 192 | } else { |
| 193 | animationsToStart.removeOne(t: animation); |
| 194 | } |
| 195 | animation->m_hasRegisteredTimer = false; |
| 196 | } |
| 197 | |
| 198 | void QQmlAnimationTimer::registerRunningAnimation(QAbstractAnimationJob *animation) |
| 199 | { |
| 200 | Q_ASSERT(!animation->userControlDisabled()); |
| 201 | |
| 202 | if (animation->m_isGroup) |
| 203 | return; |
| 204 | |
| 205 | if (animation->m_isPause) { |
| 206 | runningPauseAnimations << animation; |
| 207 | } else |
| 208 | runningLeafAnimations++; |
| 209 | } |
| 210 | |
| 211 | void QQmlAnimationTimer::unregisterRunningAnimation(QAbstractAnimationJob *animation) |
| 212 | { |
| 213 | unsetJobTimer(animation); |
| 214 | if (animation->userControlDisabled()) |
| 215 | return; |
| 216 | |
| 217 | if (animation->m_isGroup) |
| 218 | return; |
| 219 | |
| 220 | if (!animation->m_isPause) |
| 221 | runningLeafAnimations--; |
| 222 | |
| 223 | Q_ASSERT(runningLeafAnimations >= 0); |
| 224 | } |
| 225 | |
| 226 | int QQmlAnimationTimer::closestPauseAnimationTimeToFinish() |
| 227 | { |
| 228 | int closestTimeToFinish = INT_MAX; |
| 229 | for (int i = 0; i < runningPauseAnimations.size(); ++i) { |
| 230 | QAbstractAnimationJob *animation = runningPauseAnimations.at(i); |
| 231 | int timeToFinish; |
| 232 | |
| 233 | if (animation->direction() == QAbstractAnimationJob::Forward) |
| 234 | timeToFinish = animation->duration() - animation->currentLoopTime(); |
| 235 | else |
| 236 | timeToFinish = animation->currentLoopTime(); |
| 237 | |
| 238 | if (timeToFinish < closestTimeToFinish) |
| 239 | closestTimeToFinish = timeToFinish; |
| 240 | } |
| 241 | return closestTimeToFinish; |
| 242 | } |
| 243 | |
| 244 | ///////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 245 | |
| 246 | QAbstractAnimationJob::QAbstractAnimationJob() |
| 247 | : m_loopCount(1) |
| 248 | , m_group(nullptr) |
| 249 | , m_direction(QAbstractAnimationJob::Forward) |
| 250 | , m_state(QAbstractAnimationJob::Stopped) |
| 251 | , m_totalCurrentTime(0) |
| 252 | , m_currentTime(0) |
| 253 | , m_currentLoop(0) |
| 254 | , m_uncontrolledFinishTime(-1) |
| 255 | , m_currentLoopStartTime(0) |
| 256 | , m_hasRegisteredTimer(false) |
| 257 | , m_isPause(false) |
| 258 | , m_isGroup(false) |
| 259 | , m_disableUserControl(false) |
| 260 | , m_hasCurrentTimeChangeListeners(false) |
| 261 | , m_isRenderThreadJob(false) |
| 262 | , m_isRenderThreadProxy(false) |
| 263 | |
| 264 | { |
| 265 | } |
| 266 | |
| 267 | QAbstractAnimationJob::~QAbstractAnimationJob() |
| 268 | { |
| 269 | //we can't call stop here. Otherwise we get pure virtual calls |
| 270 | if (m_state != Stopped) { |
| 271 | State oldState = m_state; |
| 272 | m_state = Stopped; |
| 273 | stateChanged(newState: oldState, oldState: m_state); |
| 274 | |
| 275 | Q_ASSERT(m_state == Stopped); |
| 276 | if (oldState == Running) { |
| 277 | if (m_timer) { |
| 278 | Q_ASSERT(QQmlAnimationTimer::instance(false) == m_timer); |
| 279 | m_timer->unregisterAnimation(animation: this); |
| 280 | } |
| 281 | } |
| 282 | Q_ASSERT(!m_hasRegisteredTimer); |
| 283 | } |
| 284 | |
| 285 | if (m_group) |
| 286 | m_group->removeAnimation(animation: this); |
| 287 | } |
| 288 | |
| 289 | void QAbstractAnimationJob::fireTopLevelAnimationLoopChanged() |
| 290 | { |
| 291 | m_uncontrolledFinishTime = -1; |
| 292 | if (m_group) |
| 293 | m_currentLoopStartTime = 0; |
| 294 | topLevelAnimationLoopChanged(); |
| 295 | } |
| 296 | |
| 297 | void QAbstractAnimationJob::setState(QAbstractAnimationJob::State newState) |
| 298 | { |
| 299 | if (m_state == newState) |
| 300 | return; |
| 301 | |
| 302 | if (m_loopCount == 0) |
| 303 | return; |
| 304 | |
| 305 | if (!m_timer) // don't create a timer just to stop the animation |
| 306 | m_timer = QQmlAnimationTimer::instance(create: newState != Stopped); |
| 307 | Q_ASSERT(m_timer || newState == Stopped); |
| 308 | |
| 309 | State oldState = m_state; |
| 310 | int oldCurrentTime = m_currentTime; |
| 311 | int oldCurrentLoop = m_currentLoop; |
| 312 | Direction oldDirection = m_direction; |
| 313 | |
| 314 | // check if we should Rewind |
| 315 | if ((newState == Paused || newState == Running) && oldState == Stopped) { |
| 316 | //here we reset the time if needed |
| 317 | //we don't call setCurrentTime because this might change the way the animation |
| 318 | //behaves: changing the state or changing the current value |
| 319 | m_totalCurrentTime = m_currentTime = (m_direction == Forward) ? |
| 320 | 0 : (m_loopCount == -1 ? duration() : totalDuration()); |
| 321 | |
| 322 | // Reset uncontrolled finish time and currentLoopStartTime for this run. |
| 323 | m_uncontrolledFinishTime = -1; |
| 324 | if (!m_group) |
| 325 | m_currentLoopStartTime = m_totalCurrentTime; |
| 326 | } |
| 327 | |
| 328 | m_state = newState; |
| 329 | //(un)registration of the animation must always happen before calls to |
| 330 | //virtual function (updateState) to ensure a correct state of the timer |
| 331 | bool isTopLevel = !m_group || m_group->isStopped(); |
| 332 | if (oldState == Running) { |
| 333 | if (newState == Paused && m_hasRegisteredTimer) |
| 334 | m_timer->ensureTimerUpdate(); |
| 335 | // the animation is not running any more |
| 336 | if (m_timer) |
| 337 | m_timer->unregisterAnimation(animation: this); |
| 338 | } else if (newState == Running) { |
| 339 | m_timer->registerAnimation(animation: this, isTopLevel); |
| 340 | } |
| 341 | |
| 342 | //starting an animation qualifies as a top level loop change |
| 343 | if (newState == Running && oldState == Stopped && !m_group) |
| 344 | fireTopLevelAnimationLoopChanged(); |
| 345 | |
| 346 | RETURN_IF_DELETED(updateState(newState, oldState)); |
| 347 | |
| 348 | if (newState != m_state) //this is to be safe if updateState changes the state |
| 349 | return; |
| 350 | |
| 351 | // Notify state change |
| 352 | RETURN_IF_DELETED(stateChanged(newState, oldState)); |
| 353 | if (newState != m_state) //this is to be safe if updateState changes the state |
| 354 | return; |
| 355 | |
| 356 | switch (m_state) { |
| 357 | case Paused: |
| 358 | break; |
| 359 | case Running: |
| 360 | { |
| 361 | // this ensures that the value is updated now that the animation is running |
| 362 | if (oldState == Stopped) { |
| 363 | m_currentLoop = 0; |
| 364 | if (isTopLevel) { |
| 365 | // currentTime needs to be updated if pauseTimer is active |
| 366 | RETURN_IF_DELETED(m_timer->ensureTimerUpdate()); |
| 367 | RETURN_IF_DELETED(setCurrentTime(m_totalCurrentTime)); |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | break; |
| 372 | case Stopped: |
| 373 | // Leave running state. |
| 374 | int dura = duration(); |
| 375 | |
| 376 | if (dura == -1 || m_loopCount < 0 |
| 377 | || (oldDirection == Forward && (oldCurrentTime * (oldCurrentLoop + 1)) == (dura * m_loopCount)) |
| 378 | || (oldDirection == Backward && oldCurrentTime == 0)) { |
| 379 | finished(); |
| 380 | } |
| 381 | break; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | void QAbstractAnimationJob::setDirection(Direction direction) |
| 386 | { |
| 387 | if (m_direction == direction) |
| 388 | return; |
| 389 | |
| 390 | if (m_state == Stopped) { |
| 391 | if (m_direction == Backward) { |
| 392 | m_currentTime = duration(); |
| 393 | m_currentLoop = m_loopCount - 1; |
| 394 | } else { |
| 395 | m_currentTime = 0; |
| 396 | m_currentLoop = 0; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | // the commands order below is important: first we need to setCurrentTime with the old direction, |
| 401 | // then update the direction on this and all children and finally restart the pauseTimer if needed |
| 402 | if (m_hasRegisteredTimer) |
| 403 | m_timer->ensureTimerUpdate(); |
| 404 | |
| 405 | m_direction = direction; |
| 406 | updateDirection(direction); |
| 407 | |
| 408 | if (m_hasRegisteredTimer) |
| 409 | // needed to update the timer interval in case of a pause animation |
| 410 | m_timer->updateAnimationTimer(); |
| 411 | } |
| 412 | |
| 413 | void QAbstractAnimationJob::setLoopCount(int loopCount) |
| 414 | { |
| 415 | if (m_loopCount == loopCount) |
| 416 | return; |
| 417 | m_loopCount = loopCount; |
| 418 | updateLoopCount(loopCount); |
| 419 | } |
| 420 | |
| 421 | int QAbstractAnimationJob::totalDuration() const |
| 422 | { |
| 423 | int dura = duration(); |
| 424 | if (dura <= 0) |
| 425 | return dura; |
| 426 | int loopcount = loopCount(); |
| 427 | if (loopcount < 0) |
| 428 | return -1; |
| 429 | return dura * loopcount; |
| 430 | } |
| 431 | |
| 432 | void QAbstractAnimationJob::setCurrentTime(int msecs) |
| 433 | { |
| 434 | msecs = qMax(a: msecs, b: 0); |
| 435 | // Calculate new time and loop. |
| 436 | int dura = duration(); |
| 437 | int totalDura; |
| 438 | int oldLoop = m_currentLoop; |
| 439 | |
| 440 | if (dura < 0 && m_direction == Forward) { |
| 441 | totalDura = -1; |
| 442 | if (m_uncontrolledFinishTime >= 0 && msecs >= m_uncontrolledFinishTime) { |
| 443 | msecs = m_uncontrolledFinishTime; |
| 444 | if (m_currentLoop == m_loopCount - 1) { |
| 445 | totalDura = m_uncontrolledFinishTime; |
| 446 | } else { |
| 447 | ++m_currentLoop; |
| 448 | m_currentLoopStartTime = msecs; |
| 449 | m_uncontrolledFinishTime = -1; |
| 450 | } |
| 451 | } |
| 452 | m_totalCurrentTime = msecs; |
| 453 | m_currentTime = msecs - m_currentLoopStartTime; |
| 454 | } else { |
| 455 | totalDura = dura <= 0 ? dura : ((m_loopCount < 0) ? -1 : dura * m_loopCount); |
| 456 | if (totalDura != -1) |
| 457 | msecs = qMin(a: totalDura, b: msecs); |
| 458 | m_totalCurrentTime = msecs; |
| 459 | |
| 460 | // Update new values. |
| 461 | m_currentLoop = ((dura <= 0) ? 0 : (msecs / dura)); |
| 462 | if (m_currentLoop == m_loopCount) { |
| 463 | //we're at the end |
| 464 | m_currentTime = qMax(a: 0, b: dura); |
| 465 | m_currentLoop = qMax(a: 0, b: m_loopCount - 1); |
| 466 | } else { |
| 467 | if (m_direction == Forward) { |
| 468 | m_currentTime = (dura <= 0) ? msecs : (msecs % dura); |
| 469 | } else { |
| 470 | m_currentTime = (dura <= 0) ? msecs : ((msecs - 1) % dura) + 1; |
| 471 | if (m_currentTime == dura) |
| 472 | --m_currentLoop; |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | |
| 478 | if (m_currentLoop != oldLoop && !m_group) //### verify Running as well? |
| 479 | fireTopLevelAnimationLoopChanged(); |
| 480 | |
| 481 | RETURN_IF_DELETED(updateCurrentTime(m_currentTime)); |
| 482 | |
| 483 | if (m_currentLoop != oldLoop) { |
| 484 | // CurrentLoop listeners may restart the job if e.g. from has changed. Stopping a job will |
| 485 | // destroy it, so account for that here. |
| 486 | RETURN_IF_DELETED(currentLoopChanged()); |
| 487 | } |
| 488 | |
| 489 | // All animations are responsible for stopping the animation when their |
| 490 | // own end state is reached; in this case the animation is time driven, |
| 491 | // and has reached the end. |
| 492 | if ((m_direction == Forward && m_totalCurrentTime == totalDura) |
| 493 | || (m_direction == Backward && m_totalCurrentTime == 0)) { |
| 494 | RETURN_IF_DELETED(stop()); |
| 495 | } |
| 496 | |
| 497 | if (m_hasCurrentTimeChangeListeners) |
| 498 | currentTimeChanged(currentTime: m_currentTime); |
| 499 | } |
| 500 | |
| 501 | void QAbstractAnimationJob::start() |
| 502 | { |
| 503 | if (m_state == Running) |
| 504 | return; |
| 505 | |
| 506 | if (QQmlEnginePrivate::designerMode()) { |
| 507 | if (state() != Stopped) { |
| 508 | m_currentTime = duration(); |
| 509 | m_totalCurrentTime = totalDuration(); |
| 510 | setState(Running); |
| 511 | setState(Stopped); |
| 512 | } |
| 513 | } else { |
| 514 | setState(Running); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | void QAbstractAnimationJob::stop() |
| 519 | { |
| 520 | if (m_state == Stopped) |
| 521 | return; |
| 522 | setState(Stopped); |
| 523 | } |
| 524 | |
| 525 | void QAbstractAnimationJob::complete() |
| 526 | { |
| 527 | // Simulate the full animation cycle |
| 528 | setState(Running); |
| 529 | setCurrentTime(m_direction == Forward ? duration() : 0); |
| 530 | setState(Stopped); |
| 531 | } |
| 532 | |
| 533 | void QAbstractAnimationJob::pause() |
| 534 | { |
| 535 | if (m_state == Stopped) { |
| 536 | qWarning(msg: "QAbstractAnimationJob::pause: Cannot pause a stopped animation" ); |
| 537 | return; |
| 538 | } |
| 539 | |
| 540 | setState(Paused); |
| 541 | } |
| 542 | |
| 543 | void QAbstractAnimationJob::resume() |
| 544 | { |
| 545 | if (m_state != Paused) { |
| 546 | qWarning(msg: "QAbstractAnimationJob::resume: " |
| 547 | "Cannot resume an animation that is not paused" ); |
| 548 | return; |
| 549 | } |
| 550 | setState(Running); |
| 551 | } |
| 552 | |
| 553 | void QAbstractAnimationJob::setEnableUserControl() |
| 554 | { |
| 555 | m_disableUserControl = false; |
| 556 | } |
| 557 | |
| 558 | bool QAbstractAnimationJob::userControlDisabled() const |
| 559 | { |
| 560 | return m_disableUserControl; |
| 561 | } |
| 562 | |
| 563 | void QAbstractAnimationJob::setDisableUserControl() |
| 564 | { |
| 565 | m_disableUserControl = true; |
| 566 | start(); |
| 567 | pause(); |
| 568 | } |
| 569 | |
| 570 | void QAbstractAnimationJob::updateState(QAbstractAnimationJob::State newState, |
| 571 | QAbstractAnimationJob::State oldState) |
| 572 | { |
| 573 | Q_UNUSED(oldState); |
| 574 | Q_UNUSED(newState); |
| 575 | } |
| 576 | |
| 577 | void QAbstractAnimationJob::updateDirection(QAbstractAnimationJob::Direction direction) |
| 578 | { |
| 579 | Q_UNUSED(direction); |
| 580 | } |
| 581 | |
| 582 | void QAbstractAnimationJob::finished() |
| 583 | { |
| 584 | //TODO: update this code so it is valid to delete the animation in animationFinished |
| 585 | for (const auto &change : changeListeners) { |
| 586 | if (change.types & QAbstractAnimationJob::Completion) { |
| 587 | RETURN_IF_DELETED(change.listener->animationFinished(this)); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | if (m_group && (duration() == -1 || loopCount() < 0)) { |
| 592 | //this is an uncontrolled animation, need to notify the group animation we are finished |
| 593 | m_group->uncontrolledAnimationFinished(animation: this); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | void QAbstractAnimationJob::stateChanged(QAbstractAnimationJob::State newState, QAbstractAnimationJob::State oldState) |
| 598 | { |
| 599 | for (const auto &change : changeListeners) { |
| 600 | if (change.types & QAbstractAnimationJob::StateChange) { |
| 601 | RETURN_IF_DELETED(change.listener->animationStateChanged(this, newState, oldState)); |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | void QAbstractAnimationJob::currentLoopChanged() |
| 607 | { |
| 608 | for (const auto &change : changeListeners) { |
| 609 | if (change.types & QAbstractAnimationJob::CurrentLoop) { |
| 610 | RETURN_IF_DELETED(change.listener->animationCurrentLoopChanged(this)); |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | void QAbstractAnimationJob::currentTimeChanged(int currentTime) |
| 616 | { |
| 617 | Q_ASSERT(m_hasCurrentTimeChangeListeners); |
| 618 | |
| 619 | for (const auto &change : changeListeners) { |
| 620 | if (change.types & QAbstractAnimationJob::CurrentTime) { |
| 621 | RETURN_IF_DELETED(change.listener->animationCurrentTimeChanged(this, currentTime)); |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | void QAbstractAnimationJob::addAnimationChangeListener(QAnimationJobChangeListener *listener, QAbstractAnimationJob::ChangeTypes changes) |
| 627 | { |
| 628 | if (changes & QAbstractAnimationJob::CurrentTime) |
| 629 | m_hasCurrentTimeChangeListeners = true; |
| 630 | |
| 631 | changeListeners.push_back(x: ChangeListener(listener, changes)); |
| 632 | } |
| 633 | |
| 634 | void QAbstractAnimationJob::removeAnimationChangeListener(QAnimationJobChangeListener *listener, QAbstractAnimationJob::ChangeTypes changes) |
| 635 | { |
| 636 | m_hasCurrentTimeChangeListeners = false; |
| 637 | |
| 638 | const auto it = std::find(first: changeListeners.begin(), last: changeListeners.end(), val: ChangeListener(listener, changes)); |
| 639 | if (it != changeListeners.end()) |
| 640 | changeListeners.erase(position: it); |
| 641 | |
| 642 | for (const auto &change: changeListeners) { |
| 643 | if (change.types & QAbstractAnimationJob::CurrentTime) { |
| 644 | m_hasCurrentTimeChangeListeners = true; |
| 645 | break; |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | void QAbstractAnimationJob::debugAnimation(QDebug d) const |
| 651 | { |
| 652 | d << "AbstractAnimationJob(" << Qt::hex << (const void *) this << Qt::dec << ") state:" |
| 653 | << m_state << "duration:" << duration(); |
| 654 | } |
| 655 | |
| 656 | QDebug operator<<(QDebug d, const QAbstractAnimationJob *job) |
| 657 | { |
| 658 | if (!job) { |
| 659 | d << "AbstractAnimationJob(null)" ; |
| 660 | return d; |
| 661 | } |
| 662 | job->debugAnimation(d); |
| 663 | return d; |
| 664 | } |
| 665 | |
| 666 | QT_END_NAMESPACE |
| 667 | |
| 668 | //#include "moc_qabstractanimation2_p.cpp" |
| 669 | #include "moc_qabstractanimationjob_p.cpp" |
| 670 | |