From d0e045b1bb4d34534308a42f4474423a6109b4c4 Mon Sep 17 00:00:00 2001
From: "d.kilic" <d.kilic@fz-juelich.de>
Date: Tue, 24 Nov 2020 14:06:49 +0100
Subject: [PATCH] feat: Looping video playback fix: Video now always ends with
 last frame, even when fixing framerate

---
 include/petrack.h |  1 +
 include/player.h  |  2 ++
 src/petrack.cpp   |  5 +++++
 src/player.cpp    | 28 +++++++++++++++++++++++++---
 4 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/include/petrack.h b/include/petrack.h
index 0ddd8dbfb..468976444 100644
--- a/include/petrack.h
+++ b/include/petrack.h
@@ -484,6 +484,7 @@ private:
     QAction *mSetTo1p50;
     QAction *mSetTo1p75;
     QAction *mSetTo2p00;
+    QAction *mPlayerLooping;
     QAction *mDelPastAct;
     QAction *mDelFutureAct;
     QAction *mDelAllRoiAct;
diff --git a/include/player.h b/include/player.h
index 9b85afd5c..86ef443ba 100644
--- a/include/player.h
+++ b/include/player.h
@@ -73,6 +73,7 @@ public slots:
     void setPlayerSpeedLimited(bool fixed);
     bool getPlayerSpeedLimited();
     void setPlayerSpeedFixed(bool fixed);
+    void setLooping(bool looping);
     void setFrameInNum(int in=-1.);
     void setFrameOutNum(int out=-1.);
     int getFrameInNum();
@@ -94,6 +95,7 @@ private:
     PlayerState mState = PlayerState::PAUSE;
     bool mPlayerSpeedLimited;
     bool mPlayerSpeedFixed = false;
+    bool mLooping = false;
     bool mSliderSet;
     bool mRec;
 
diff --git a/src/petrack.cpp b/src/petrack.cpp
index 28de3c9ff..1ace3dd28 100644
--- a/src/petrack.cpp
+++ b/src/petrack.cpp
@@ -1762,6 +1762,10 @@ void Petrack::createActions()
     connect(mSetTo0p50, &QAction::triggered, mPlayerWidget, [&](){mPlayerWidget->setSpeedRelativeToRealtime(0.5);});
     mSetTo0p25 = new QAction(tr("&x0.25"));
     connect(mSetTo0p25, &QAction::triggered, mPlayerWidget, [&](){mPlayerWidget->setSpeedRelativeToRealtime(0.25);});
+
+    mPlayerLooping = new QAction(tr("&Loop"));
+    mPlayerLooping->setCheckable(true);
+    connect(mPlayerLooping, &QAction::triggered, mPlayerWidget, &Player::setLooping);
     // -------------------------------------------------------------------------------------------------------
 
     QSignalMapper* signalMapper = new QSignalMapper(this);
@@ -1836,6 +1840,7 @@ void Petrack::createMenus()
     mPlaybackSpeedMenu->addAction(mSetTo0p75);
     mPlaybackSpeedMenu->addAction(mSetTo0p50);
     mPlaybackSpeedMenu->addAction(mSetTo0p25);
+    mViewMenu->addAction(mPlayerLooping);
     mViewMenu->addSeparator();
     mViewMenu->addAction(mFitViewAct);
     mViewMenu->addAction(mFitROIAct);
diff --git a/src/player.cpp b/src/player.cpp
index e9921c0ef..1c6f03748 100644
--- a/src/player.cpp
+++ b/src/player.cpp
@@ -213,6 +213,11 @@ void Player::setPlayerSpeedFixed(bool fixed)
     mPlayerSpeedFixed = fixed;
 }
 
+void Player::setLooping(bool looping)
+{
+    mLooping = looping;
+}
+
 void Player::setSpeedRelativeToRealtime(double factor)
 {
     setFPS(mAnimation->getOriginalFPS()*factor);
@@ -376,7 +381,7 @@ void Player::playVideo(){
                     if(overtime >= supposedDiff){
                         mAnimation->skipFrame(static_cast<int>(overtime / supposedDiff));
                         overtime = overtime % supposedDiff;
-                        currentFrame = mAnimation->getCurrentFrameNum() + 1;
+                        currentFrame = std::min(mAnimation->getCurrentFrameNum() + 1, mAnimation->getSourceOutFrameNum());
                     }
                 }
 
@@ -405,8 +410,25 @@ void Player::playVideo(){
 
         if(!updateImage()){
             mState = PlayerState::PAUSE;
-            if(mState == PlayerState::FORWARD && mAnimation->getCurrentFrameNum() != mAnimation->getSourceOutFrameNum()){
-                debout << "Warning: video unexpected finished." << std::endl;
+            if( mAnimation->getCurrentFrameNum() != 0 && mAnimation->getCurrentFrameNum() != mAnimation->getSourceOutFrameNum()){
+                debout << "Warning: video unexpectedly finished." << std::endl;
+            }
+        }else{
+            if(mMainWindow->getControlWidget()->trackOnlineCalc->checkState() == Qt::Checked)
+            {
+                QMessageBox::warning(this, "Error: No tracking while looping", "Looping and tracking are incompatible. Please disable one first.");
+                mState = PlayerState::PAUSE;
+                break;
+            }
+            if(mLooping)
+            {
+                if(mState == PlayerState::FORWARD && mAnimation->getCurrentFrameNum() == mAnimation->getSourceOutFrameNum())
+                {
+                    currentFrame = 0;
+                }else if(mState == PlayerState::BACKWARD && mAnimation->getCurrentFrameNum() == 0)
+                {
+                    currentFrame = mAnimation->getSourceOutFrameNum();
+                }
             }
         }
     }
-- 
GitLab