Marble

PlaybackFlyToItem.cpp
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 //
3 // SPDX-FileCopyrightText: 2014 Sanjiban Bairagya <[email protected]>
4 //
5 
6 #include "PlaybackFlyToItem.h"
7 
8 #include "GeoDataLookAt.h"
9 #include "GeoDataCamera.h"
10 #include "GeoDataFlyTo.h"
11 #include "Quaternion.h"
12 
13 #include <QTimer>
14 
15 namespace Marble
16 {
17 PlaybackFlyToItem::PlaybackFlyToItem( const GeoDataFlyTo* flyTo ):
18  m_flyTo( flyTo ),
19  m_before( nullptr ),
20  m_next( nullptr ),
21  m_isPlaying( false ),
22  m_isFirst( false )
23 {
24  //do nothing
25 }
26 
27 const GeoDataFlyTo* PlaybackFlyToItem::flyTo() const
28 {
29  return m_flyTo;
30 }
31 
32 double PlaybackFlyToItem::duration() const
33 {
34  // We use duration 0 for first FlyTo for instantly flight to it.
35  return m_isFirst ? 0 : m_flyTo->duration();
36 }
37 
38 void PlaybackFlyToItem::play()
39 {
40  if( m_isPlaying ){
41  return;
42  } else {
43  m_isPlaying = true;
44  if ( !( m_start.isValid() ) ){
45  m_start = QDateTime::currentDateTime();
46  Q_ASSERT( m_start.isValid() );
47  } else {
48  m_start = m_start.addMSecs( m_pause.msecsTo( QDateTime::currentDateTime() ) );
49  }
50  playNext();
51  }
52 }
53 
54 void PlaybackFlyToItem::playNext()
55 {
56  if( !m_start.isValid() ){
57  return;
58  }
59  double const progress = m_start.msecsTo( QDateTime::currentDateTime() ) / 1000.0;
60  Q_ASSERT( progress >= 0.0 );
61  double const t = progress / duration();
62  if( t <= 1 ){
63  if( m_isPlaying ){
64  center( t );
65  emit progressChanged( progress );
66  QTimer::singleShot( 5, this, SLOT(playNext()) );
67  }
68  } else {
69  center( 1.0 );
70  emit finished();
71  stop();
72  }
73 }
74 
76 {
77  m_isPlaying = false;
78  m_pause = QDateTime::currentDateTime();
79 }
80 
81 void PlaybackFlyToItem::seek( double t )
82 {
83  m_start = QDateTime::currentDateTime().addMSecs( -t * duration() * 1000 );
84  m_pause = QDateTime::currentDateTime();
85  center( t );
86 }
87 
89 {
90  m_isPlaying = false;
91  m_start = QDateTime();
92  m_pause = QDateTime();
93 }
94 
95 void PlaybackFlyToItem::center( double t )
96 {
97  Q_ASSERT( t >= 0.0 && t <= 1.0 );
98  Q_ASSERT( m_before );
99  if ( m_flyTo->flyToMode() == GeoDataFlyTo::Bounce || !m_before->m_before || !m_next ) {
100  GeoDataCoordinates const a = m_before->m_flyTo->view()->coordinates();
101  GeoDataCoordinates const b = m_flyTo->view()->coordinates();
102  emit centerOn( a.interpolate( b, t ) );
103  } else {
104  Q_ASSERT( m_flyTo->flyToMode() == GeoDataFlyTo::Smooth );
105  GeoDataCoordinates const a = m_before->m_before->m_flyTo->view()->coordinates();
106  GeoDataCoordinates const b = m_before->m_flyTo->view()->coordinates();
107  GeoDataCoordinates const c = m_flyTo->view()->coordinates();
108  GeoDataCoordinates const d = m_next->m_flyTo->view()->coordinates();
109  emit centerOn( b.interpolate( a, c, d, t ) );
110  }
111 }
112 
113 void PlaybackFlyToItem::setBefore( PlaybackFlyToItem *before )
114 {
115  m_before = before;
116 }
117 
118 void PlaybackFlyToItem::setNext( PlaybackFlyToItem *next )
119 {
120  m_next = next;
121 }
122 
123 void PlaybackFlyToItem::setFirst(bool isFirst)
124 {
125  m_isFirst = isFirst;
126 }
127 
128 }
129 
130 #include "moc_PlaybackFlyToItem.cpp"
QDateTime addMSecs(qint64 msecs) const const
void stop(Ekos::AlignState mode)
QDateTime currentDateTime()
Q_SCRIPTABLE Q_NOREPLY void pause()
Binds a QML item to a specific geodetic location in screen coordinates.
QTextStream & center(QTextStream &stream)
QAction * next(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:27 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.