Marble

SerialTrack.cpp
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 //
3 // SPDX-FileCopyrightText: 2014 Sanjiban Bairagya <[email protected]>
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 
14 namespace Marble
15 {
16 
17 SerialTrack::SerialTrack(): QObject()
18 {
19  m_currentIndex = 0;
20  m_finishedPosition = 0;
21  m_currentPosition = 0;
22  m_paused = true;
23 }
24 
25 SerialTrack::~SerialTrack()
26 {
27  clear();
28 }
29 
30 void 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 
45 void SerialTrack::play()
46 {
47  m_paused = false;
48  m_items[m_currentIndex]->play();
49 }
50 
51 void SerialTrack::pause()
52 {
53  m_paused = true;
54  m_items[m_currentIndex]->pause();
55 }
56 
57 void 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 
68 void 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 
114 double 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 
123 void 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 
133 void 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 
149 void SerialTrack::changeProgress( double progress )
150 {
151  m_currentPosition = m_finishedPosition + progress;
152  emit progressChanged( m_currentPosition );
153 }
154 
155 int SerialTrack::size() const
156 {
157  return m_items.size();
158 }
159 
160 PlaybackItem* SerialTrack::at( int i )
161 {
162  return m_items.at( i );
163 }
164 
165 double SerialTrack::currentPosition()
166 {
167  return m_currentPosition;
168 }
169 
170 }
171 
172 #include "moc_SerialTrack.cpp"
void stop(Ekos::AlignState mode)
Q_SCRIPTABLE Q_NOREPLY void pause()
Binds a QML item to a specific geodetic location in screen coordinates.
QAction * clear(const QObject *recvr, const char *slot, QObject *parent)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Thu Sep 21 2023 04:12:28 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.