File indexing completed on 2024-05-05 03:50:39

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2014 Calin Cruceru <crucerucalincristian@gmail.com>
0004 //
0005 
0006 // Self
0007 #include "MergingPolylineNodesAnimation.h"
0008 
0009 // Marble
0010 #include "PolylineAnnotation.h"
0011 #include "GeoDataPlacemark.h"
0012 #include "GeoDataLineString.h"
0013 
0014 
0015 namespace Marble {
0016 
0017 
0018 MergingPolylineNodesAnimation::MergingPolylineNodesAnimation( PolylineAnnotation *polyline ) :
0019     m_timer( new QTimer( this ) ),
0020 
0021     // To avoid long lines and repeated code
0022     m_firstNodeIndex( polyline->m_firstMergedNode ),
0023     m_secondNodeIndex( polyline->m_secondMergedNode ),
0024 
0025     m_lineString( static_cast<GeoDataLineString*>( polyline->placemark()->geometry() ) ),
0026     m_firstInitialCoords( m_lineString->at( polyline->m_firstMergedNode ) ),
0027     m_secondInitialCoords( m_lineString->at( polyline->m_secondMergedNode ) )
0028 {
0029     connect( m_timer, SIGNAL(timeout()), this, SLOT(updateNodes()) );
0030 }
0031 
0032 MergingPolylineNodesAnimation::~MergingPolylineNodesAnimation()
0033 {
0034     delete m_timer;
0035 }
0036 
0037 void MergingPolylineNodesAnimation::startAnimation()
0038 {
0039     static const int timeOffset = 1;
0040     m_timer->start( timeOffset );
0041 }
0042 
0043 void MergingPolylineNodesAnimation::updateNodes()
0044 {
0045     static const qreal ratio = 0.05;
0046     const qreal distanceOffset = m_firstInitialCoords.interpolate(m_secondInitialCoords, ratio)
0047                                                      .sphericalDistanceTo(m_firstInitialCoords) + 0.001;
0048 
0049     if ( nodesDistance() <  distanceOffset ) {
0050         m_lineString->at(m_secondNodeIndex) = newCoords();
0051         m_lineString->remove( m_firstNodeIndex );
0052 
0053         emit animationFinished();
0054     } else {
0055         m_lineString->at(m_firstNodeIndex) =  m_lineString->at(m_firstNodeIndex).interpolate(
0056                                                                                 m_secondInitialCoords,
0057                                                                                 ratio );
0058         m_lineString->at(m_secondNodeIndex) =  m_lineString->at(m_secondNodeIndex).interpolate(
0059                                                                                 m_firstInitialCoords,
0060                                                                                 ratio );
0061         emit nodesMoved();
0062     }
0063 }
0064 
0065 GeoDataCoordinates MergingPolylineNodesAnimation::newCoords()
0066 {
0067     return m_lineString->at(m_firstNodeIndex).interpolate( m_lineString->at(m_secondNodeIndex), 0.5 );
0068 }
0069 
0070 qreal MergingPolylineNodesAnimation::nodesDistance()
0071 {
0072     return m_lineString->at(m_firstNodeIndex).sphericalDistanceTo(m_lineString->at(m_secondNodeIndex));
0073 }
0074 
0075 } // namespace Marble
0076 
0077 #include "moc_MergingPolylineNodesAnimation.cpp"