Marble

SerialTrack.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2014 Sanjiban Bairagya <sanjiban22393@gmail.com>
4//
5
6#include "SerialTrack.h"
7#include "GeoDataCamera.h"
8#include "GeoDataLookAt.h"
9#include "PlaybackFlyToItem.h"
10#include "PlaybackTourControlItem.h"
11#include "PlaybackWaitItem.h"
12#include "TourPlayback.h"
13
14namespace Marble
15{
16
17SerialTrack::SerialTrack()
18 : QObject()
19{
20 m_currentIndex = 0;
21 m_finishedPosition = 0;
22 m_currentPosition = 0;
23 m_paused = true;
24}
25
26SerialTrack::~SerialTrack()
27{
28 clear();
29}
30
31void SerialTrack::append(PlaybackItem *item)
32{
33 connect(item, &PlaybackItem::progressChanged, this, &SerialTrack::changeProgress);
34 connect(item, &PlaybackItem::centerOn, this, &SerialTrack::centerOn);
35 connect(item, &PlaybackItem::finished, this, &SerialTrack::handleFinishedItem);
36 connect(item, &PlaybackItem::paused, this, &SerialTrack::pause);
37 m_items.append(item);
38 if (m_items.size() == 1) {
39 auto flyTo = dynamic_cast<PlaybackFlyToItem *>(item);
40 if (flyTo != nullptr) {
41 flyTo->setFirst(true);
42 }
43 }
44}
45
46void SerialTrack::play()
47{
48 m_paused = false;
49 m_items[m_currentIndex]->play();
50}
51
52void SerialTrack::pause()
53{
54 m_paused = true;
55 m_items[m_currentIndex]->pause();
56}
57
58void SerialTrack::stop()
59{
60 m_paused = true;
61 if (m_items.size() != 0 && m_currentIndex >= 0 && m_currentIndex <= m_items.size() - 1) {
62 m_items[m_currentIndex]->stop();
63 }
64 m_finishedPosition = 0;
65 Q_EMIT progressChanged(m_finishedPosition);
66 m_currentIndex = 0;
67}
68
69void SerialTrack::seek(double offset)
70{
71 m_currentPosition = offset;
72 int index = -1;
73 for (int i = 0; i < m_items.size(); i++) {
74 if (offset < m_items[i]->duration()) {
75 index = i;
76 break;
77 } else {
78 m_items[i]->stop();
79 offset -= m_items[i]->duration();
80 }
81 }
82
83 if (index == -1) {
84 index = m_items.size() - 1;
85 }
86
87 if (index < m_items.size() - 1) {
88 for (int i = index + 1; i < m_items.size(); i++) {
89 m_items[i]->stop();
90 }
91 }
92
93 if (index > m_currentIndex) {
94 for (int i = m_currentIndex; i < index; i++) {
95 m_finishedPosition += m_items[i]->duration();
96 }
97 } else {
98 for (int i = m_currentIndex - 1; i >= index && i >= 0; i--) {
99 m_finishedPosition -= m_items[i]->duration();
100 }
101 }
102
103 if (m_currentIndex != index && !m_paused) {
104 m_items[index]->play();
105 }
106
107 m_currentIndex = index;
108 if (m_currentIndex != -1) {
109 double t = offset / m_items[m_currentIndex]->duration();
110 Q_ASSERT(t >= 0 && t <= 1);
111 m_items[m_currentIndex]->seek(t);
112 }
113}
114
115double SerialTrack::duration() const
116{
117 double duration = 0.0;
118 for (PlaybackItem *item : m_items) {
119 duration += item->duration();
120 }
121 return duration;
122}
123
124void SerialTrack::clear()
125{
126 qDeleteAll(m_items);
127 m_items.clear();
128 m_currentIndex = 0;
129 m_finishedPosition = 0;
130 m_currentPosition = 0;
131 m_paused = true;
132}
133
134void SerialTrack::handleFinishedItem()
135{
136 if (m_paused) {
137 return;
138 }
139 if (m_currentIndex + 1 < m_items.size()) {
140 m_finishedPosition += m_items[m_currentIndex]->duration();
141 m_currentIndex++;
142 m_items[m_currentIndex]->play();
143 Q_EMIT itemFinished(m_currentIndex + 1);
144
145 } else {
146 Q_EMIT finished();
147 }
148}
149
150void SerialTrack::changeProgress(double progress)
151{
152 m_currentPosition = m_finishedPosition + progress;
153 Q_EMIT progressChanged(m_currentPosition);
154}
155
156int SerialTrack::size() const
157{
158 return m_items.size();
159}
160
161PlaybackItem *SerialTrack::at(int i)
162{
163 return m_items.at(i);
164}
165
166double SerialTrack::currentPosition()
167{
168 return m_currentPosition;
169}
170
171}
172
173#include "moc_SerialTrack.cpp"
KGuiItem clear()
Binds a QML item to a specific geodetic location in screen coordinates.
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:15:46 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.