26#include "moc_qgstemporalnavigationobject.cpp"
28using namespace Qt::StringLiterals;
33 mNewFrameTimer =
new QTimer(
this );
35 connect( mNewFrameTimer, &QTimer::timeout,
36 this, &QgsTemporalNavigationObject::timerTimeout );
39void QgsTemporalNavigationObject::timerTimeout()
41 switch ( mPlayBackMode )
48 mCurrentFrameNumber = -1;
56 if ( mCurrentFrameNumber <= 0 )
73 return mTotalMovieFrames;
78 if ( frames == mTotalMovieFrames )
81 mTotalMovieFrames = frames;
89 return mLoopAnimation;
94 mLoopAnimation = loopAnimation;
99 auto scope = std::make_unique< QgsExpressionContextScope >( u
"temporal"_s );
100 scope->setVariable( u
"frame_rate"_s, mFramesPerSecond,
true );
101 scope->setVariable( u
"frame_number"_s, mCurrentFrameNumber,
true );
102 scope->setVariable( u
"frame_duration"_s, mFrameDuration,
true );
103 scope->setVariable( u
"frame_timestep"_s, mFrameDuration.originalDuration(),
true );
104 scope->setVariable( u
"frame_timestep_unit"_s,
static_cast< int >( mFrameDuration.originalUnit() ),
true );
105 scope->setVariable( u
"frame_timestep_units"_s,
QgsUnitTypes::toString( mFrameDuration.originalUnit() ),
true );
106 scope->setVariable( u
"animation_start_time"_s, mTemporalExtents.begin(),
true );
107 scope->setVariable( u
"animation_end_time"_s, mTemporalExtents.end(),
true );
108 scope->setVariable( u
"animation_interval"_s,
QgsInterval( mTemporalExtents.end() - mTemporalExtents.begin() ),
true );
111 scope->addHiddenVariable( u
"frame_timestep_unit"_s );
113 return scope.release();
118 const QDateTime start = mTemporalExtents.
begin();
123 const long long nextFrame = frame + 1;
129 if ( mAllRanges.empty() )
132 return frame < mAllRanges.size() ? mAllRanges.at( frame ) : mAllRanges.constLast();
140 QDateTime frameStart = begin;
142 if ( mCumulativeTemporalRange )
150 if ( mNavigationMode == mode )
153 mNavigationMode = mode;
156 if ( !mBlockUpdateTemporalRangeSignal )
158 switch ( mNavigationMode )
185 switch ( mNavigationMode )
197 if ( !mBlockUpdateTemporalRangeSignal )
209 return mTemporalExtents;
224 if ( mCurrentFrameNumber != frameNumber )
226 mCurrentFrameNumber = std::max( 0LL, std::min( frameNumber,
totalFrameCount() - 1 ) );
229 if ( !mBlockUpdateTemporalRangeSignal )
236 return mCurrentFrameNumber;
261 return mFrameDuration;
266 if ( framesPerSeconds > 0 )
268 mFramesPerSecond = framesPerSeconds;
269 mNewFrameTimer->setInterval(
static_cast< int >( ( 1.0 / mFramesPerSecond ) * 1000 ) );
275 return mFramesPerSecond;
280 if ( mCumulativeTemporalRange == state )
283 mCumulativeTemporalRange = state;
293 return mCumulativeTemporalRange;
298 mNewFrameTimer->start(
static_cast< int >( ( 1.0 / mFramesPerSecond ) * 1000 ) );
303 mNewFrameTimer->stop();
355 return mTotalMovieFrames;
359 return mAllRanges.count();
363 const QgsInterval totalAnimationLength = mTemporalExtents.end() - mTemporalExtents.begin();
364 return static_cast< long long >( std::ceil( totalAnimationLength.
seconds() / mFrameDuration.seconds() ) );
370 if ( mode != mPlayBackMode )
372 mPlayBackMode = mode;
379 return mPlayBackMode;
384 long long bestFrame = 0;
389 if ( range.contains( frameStart ) )
391 else if ( range.begin() > frameStart )
393 return std::max( 0LL, bestFrame - 1 );
396 return mAllRanges.count() - 1;
402 long long roughFrameStart = 0;
410 if ( mTemporalExtents.contains( frameStart ) )
412 roughFrameStart =
static_cast< long long >( std::floor(
QgsInterval( frameStart - mTemporalExtents.begin() ).
seconds() / mFrameDuration.seconds() ) );
414 roughFrameEnd = roughFrameStart + 100;
416 for (
long long i = roughFrameStart; i < roughFrameEnd; ++i )
TemporalNavigationMode
Temporal navigation modes.
@ Animated
Temporal navigation relies on frames within a datetime range.
@ Movie
Movie mode – behaves like a video player, with a fixed frame duration and no temporal range.
@ FixedRange
Temporal navigation relies on a fixed datetime range.
@ Disabled
Temporal navigation is disabled.
@ IrregularStep
Special 'irregular step' time unit, used for temporal data which uses irregular, non-real-world unit ...
AnimationState
Animation states.
@ Forward
Animation is playing forward.
@ Reverse
Animation is playing in reverse.
@ Idle
Animation is paused.
Single scope for storing variables and functions for use within a QgsExpressionContext.
A representation of the interval between two datetime values.
double seconds() const
Returns the interval duration in seconds.
QgsTemporalController(QObject *parent=nullptr)
Constructor for QgsTemporalController, with the specified parent object.
void updateTemporalRange(const QgsDateTimeRange &range)
Signals that a temporal range has changed and needs to be updated in all connected objects.
void stateChanged(Qgis::AnimationState state)
Emitted whenever the animation state changes.
bool isLooping() const
Returns true if the animation should loop after hitting the end or start frame.
long long totalMovieFrames() const
Returns the total number of frames for the movie.
void previous()
Jumps back to the previous frame.
double framesPerSecond() const
Returns the animation frame rate, in frames per second.
void setAvailableTemporalRanges(const QList< QgsDateTimeRange > &ranges)
Sets the list of all available temporal ranges which have data available.
void setFrameDuration(const QgsInterval &duration)
Sets the frame duration, which dictates the temporal length of each frame in the animation.
long long findBestFrameNumberForFrameStart(const QDateTime &frameStart) const
Returns the best suited frame number for the specified datetime, based on the start of the correspond...
void navigationModeChanged(Qgis::TemporalNavigationMode mode)
Emitted whenever the navigation mode changes.
void setNavigationMode(const Qgis::TemporalNavigationMode mode)
Sets the temporal navigation mode.
QgsExpressionContextScope * createExpressionContextScope() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
void playForward()
Starts the animation playing in a forward direction up till the end of all frames.
long long currentFrameNumber() const
Returns the current frame number.
void rewindToStart()
Rewinds the temporal navigation to start of the temporal extent.
void pause()
Pauses the temporal navigation.
void setCurrentFrameNumber(long long frame)
Sets the current animation frame number.
Qgis::AnimationState animationState() const
Returns the current animation state.
long long totalFrameCount() const
Returns the total number of frames for the navigation.
void skipToEnd()
Skips the temporal navigation to end of the temporal extent.
void temporalFrameDurationChanged(const QgsInterval &interval)
Emitted whenever the frameDuration interval of the controller changes.
void setFramesPerSecond(double rate)
Sets the animation frame rate, in frames per second.
QgsDateTimeRange temporalExtents() const
Returns the navigation temporal extents, which dictate the earliest and latest date time possible in ...
bool temporalRangeCumulative() const
Returns the animation temporal range cumulative settings.
void next()
Advances to the next frame.
void totalMovieFramesChanged(long long frames)
Emitted whenever the total number of frames in the movie is changed.
void setTotalMovieFrames(long long frames)
Sets the total number of frames for the movie.
QgsDateTimeRange dateTimeRangeForFrameNumber(long long frame) const
Calculates the temporal range associated with a particular animation frame.
void setTemporalExtents(const QgsDateTimeRange &extents)
Sets the navigation temporal extents, which dictate the earliest and latest date time possible in the...
void setTemporalRangeCumulative(bool state)
Sets the animation temporal range as cumulative.
QList< QgsDateTimeRange > availableTemporalRanges() const
Returns the list of all available temporal ranges which have data available.
void play()
Starts playing the temporal navigation from its current frame, using the direction specified by anima...
void setLooping(bool loop)
Sets whether the animation should loop after hitting the end or start frame.
void playBackward()
Starts the animation playing in a reverse direction until the beginning of the time range.
void temporalExtentsChanged(const QgsDateTimeRange &extent)
Emitted whenever the temporalExtent extent changes.
QgsTemporalNavigationObject(QObject *parent=nullptr)
Constructor for QgsTemporalNavigationObject, with the specified parent object.
void setAnimationState(Qgis::AnimationState state)
Sets the current animation state.
QgsInterval frameDuration() const
Returns the current set frame duration, which dictates the temporal length of each frame in the anima...
T begin() const
Returns the beginning of the range.
bool overlaps(const QgsTemporalRange< T > &other) const
Returns true if this range overlaps another range.
static QDateTime calculateFrameTime(const QDateTime &start, const long long frame, const QgsInterval &interval)
Calculates the frame time for an animation.
static Q_INVOKABLE QString toString(Qgis::DistanceUnit unit)
Returns a translated string representing a distance unit.
QgsTemporalRange< QDateTime > QgsDateTimeRange
QgsRange which stores a range of date times.