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 "PlaybackFlyToItem.h"
8#include "PlaybackWaitItem.h"
9#include "PlaybackTourControlItem.h"
10#include "GeoDataCamera.h"
11#include "GeoDataLookAt.h"
12#include "TourPlayback.h"
13
14namespace Marble
15{
16
17SerialTrack::SerialTrack(): QObject()
18{
19 m_currentIndex = 0;
20 m_finishedPosition = 0;
21 m_currentPosition = 0;
22 m_paused = true;
23}
24
25SerialTrack::~SerialTrack()
26{
27 clear();
28}
29
30void SerialTrack::append(PlaybackItem* item)
31{
32 connect( item, SIGNAL(progressChanged(double)), this, SLOT(changeProgress(double)) );
33 connect( item, SIGNAL(centerOn(GeoDataCoordinates)), this, SIGNAL(centerOn(GeoDataCoordinates)) );
34 connect( item, SIGNAL(finished()), this, SLOT(handleFinishedItem()) ) ;
35 connect( item, SIGNAL(paused()), this, SLOT(pause()) ) ;
36 m_items.append( item );
37 if( m_items.size() == 1 ) {
38 PlaybackFlyToItem *flyTo = dynamic_cast<PlaybackFlyToItem*>( item );
39 if( flyTo != nullptr ) {
40 flyTo->setFirst( true )
41; }
42 }
43}
44
45void SerialTrack::play()
46{
47 m_paused = false;
48 m_items[m_currentIndex]->play();
49}
50
51void SerialTrack::pause()
52{
53 m_paused = true;
54 m_items[m_currentIndex]->pause();
55}
56
57void SerialTrack::stop()
58{
59 m_paused = true;
60 if( m_items.size() != 0 && m_currentIndex >= 0 && m_currentIndex <= m_items.size() - 1 ){
61 m_items[m_currentIndex]->stop();
62 }
63 m_finishedPosition = 0;
64 emit progressChanged( m_finishedPosition );
65 m_currentIndex = 0;
66}
67
68void SerialTrack::seek( double offset )
69{
70 m_currentPosition = offset;
71 int index = -1;
72 for( int i = 0; i < m_items.size(); i++ ){
73 if( offset < m_items[i]->duration() ){
74 index = i;
75 break;
76 } else {
77 m_items[i]->stop();
78 offset -= m_items[i]->duration();
79 }
80 }
81
82 if( index == -1 ){
83 index = m_items.size() - 1;
84 }
85
86 if( index < m_items.size() - 1 ){
87 for( int i = index + 1; i < m_items.size(); i++ ){
88 m_items[ i ]->stop();
89 }
90 }
91
92 if( index > m_currentIndex ){
93 for( int i = m_currentIndex; i < index ; i++ ){
94 m_finishedPosition += m_items[ i ]->duration();
95 }
96 }else{
97 for( int i = m_currentIndex - 1; i >= index && i >= 0; i-- ){
98 m_finishedPosition -= m_items[ i ]->duration();
99 }
100 }
101
102 if (m_currentIndex != index && !m_paused) {
103 m_items[ index ]->play();
104 }
105
106 m_currentIndex = index;
107 if ( m_currentIndex != -1 ){
108 double t = offset / m_items[ m_currentIndex ]->duration();
109 Q_ASSERT( t >= 0 && t <= 1 );
110 m_items[ m_currentIndex ]->seek( t );
111 }
112}
113
114double SerialTrack::duration() const
115{
116 double duration = 0.0;
117 for (PlaybackItem* item: m_items) {
118 duration += item->duration();
119 }
120 return duration;
121}
122
123void SerialTrack::clear()
124{
125 qDeleteAll( m_items );
126 m_items.clear();
127 m_currentIndex = 0;
128 m_finishedPosition = 0;
129 m_currentPosition = 0;
130 m_paused = true;
131}
132
133void SerialTrack::handleFinishedItem()
134{
135 if( m_paused ){
136 return;
137 }
138 if ( m_currentIndex + 1 < m_items.size() ) {
139 m_finishedPosition += m_items[m_currentIndex]->duration();
140 m_currentIndex++;
141 m_items[m_currentIndex]->play();
142 emit itemFinished( m_currentIndex + 1 );
143
144 } else {
145 emit finished();
146 }
147}
148
149void SerialTrack::changeProgress( double progress )
150{
151 m_currentPosition = m_finishedPosition + progress;
152 emit progressChanged( m_currentPosition );
153}
154
155int SerialTrack::size() const
156{
157 return m_items.size();
158}
159
160PlaybackItem* SerialTrack::at( int i )
161{
162 return m_items.at( i );
163}
164
165double SerialTrack::currentPosition()
166{
167 return m_currentPosition;
168}
169
170}
171
172#include "moc_SerialTrack.cpp"
Q_SCRIPTABLE Q_NOREPLY void pause()
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 Fri Jun 21 2024 12:00:07 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.