From 42e768b5b23b4bfacb00199c46619ae3b6a06687 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Wed, 4 Aug 2021 10:55:56 +0200
Subject: [PATCH] UI: Seek in the file using the slider

---
 src/Lib/Utils.hh                     |  4 ++++
 src/UI/DocumentViews/MpvContainer.cc | 13 ++++++++++++-
 src/UI/DocumentViews/MpvContainer.hh |  3 +++
 src/UI/DocumentViews/MpvControls.cc  | 12 +++++++++---
 src/UI/DocumentViews/MpvControls.hh  |  4 ++--
 5 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/src/Lib/Utils.hh b/src/Lib/Utils.hh
index b8dee2bd..ac421be7 100644
--- a/src/Lib/Utils.hh
+++ b/src/Lib/Utils.hh
@@ -13,6 +13,10 @@
 #include <QJsonDocument>
 #include <QtGlobal>
 #include <type_traits>
+#include <chrono>
+
+// Use chrono instead of std::chrono...
+namespace chrono = std::chrono;
 
 // Prety define for OpenMP's parallel for loop with indentation not fucked up
 // by clang-format.
diff --git a/src/UI/DocumentViews/MpvContainer.cc b/src/UI/DocumentViews/MpvContainer.cc
index ddaebac6..f2e98133 100644
--- a/src/UI/DocumentViews/MpvContainer.cc
+++ b/src/UI/DocumentViews/MpvContainer.cc
@@ -197,6 +197,7 @@ MpvContainer::handleMpvEventCommandReply(const AsyncCmdType type) noexcept
         sid = getAssSid();
         qDebug() << "Load ASS file with id:" << sid;
         break;
+
     case AsyncCmdType::UnloadAss:
         sid = getAssSid();
         qDebug().nospace() << "Unload Ass, rc = " << sid;
@@ -212,6 +213,10 @@ MpvContainer::handleMpvEventCommandReply(const AsyncCmdType type) noexcept
         unloadAssFile();
         break;
 
+    case AsyncCmdType::SeekTime:
+        qDebug() << "MPV - CMD: Seeked playback";
+        break;
+
     case AsyncCmdType::TogglePlayback:
         qDebug() << "MPV - CMD: Playback was toggled";
         break;
@@ -298,6 +303,12 @@ MpvContainer::asyncCommand(const AsyncCmdType cmd,
 void
 MpvContainer::unloadAssFile() noexcept
 {
-    // XXX: Debug thing for now
     asyncCommand(AsyncCmdType::UnloadAss, { "sub-remove", nullptr });
 }
+
+void
+MpvContainer::seekInFile(const chrono::seconds time) noexcept
+{
+    QByteArray seconds = QString::number(time.count()).toUtf8();
+    asyncCommand(AsyncCmdType::SeekTime, { "seek", seconds.data(), "absolute", nullptr });
+}
diff --git a/src/UI/DocumentViews/MpvContainer.hh b/src/UI/DocumentViews/MpvContainer.hh
index 1c1b4616..8296cac6 100644
--- a/src/UI/DocumentViews/MpvContainer.hh
+++ b/src/UI/DocumentViews/MpvContainer.hh
@@ -26,6 +26,7 @@ class MpvContainer final : public QWidget {
         LoadAssFile,
         ReloadAss,
         UnloadAss,
+        SeekTime,
         TogglePlayback,
     };
 
@@ -44,6 +45,8 @@ public:
     void loadAssFile(const QString &) noexcept;
     void reloadAssFile() noexcept;
 
+    void seekInFile(const chrono::seconds time) noexcept;
+
     // Register a callback for time change, don't use Qt's signals for that.
     // Here the function will likely be moved if necessary (I hope...).
     void registerMpvTimeCallback(std::function<void(double)>) noexcept;
diff --git a/src/UI/DocumentViews/MpvControls.cc b/src/UI/DocumentViews/MpvControls.cc
index 52bb30ce..a661de0f 100644
--- a/src/UI/DocumentViews/MpvControls.cc
+++ b/src/UI/DocumentViews/MpvControls.cc
@@ -31,9 +31,15 @@ MpvControls::MpvControls(MpvContainer *passedContainer, QWidget *parent) noexcep
         }
     });
 
-    connect(progressBar, &QAbstractSlider::valueChanged, this, [this](int value) noexcept -> void {
-        qDebug() << "Slider set to" << value << "max was" << timeDuration.count();
-        timePosition = chrono::seconds(value);
+    connect(progressBar, &QAbstractSlider::sliderMoved, this, [this](int value) noexcept -> void {
+        // FIXME: Detect if it was done by the registered time callback...
+        askedSliderPosition = value;
+    });
+
+    connect(progressBar, &QAbstractSlider::sliderReleased, this, [this]() noexcept -> void {
+        qDebug() << "Slider set to" << askedSliderPosition << "max was" << timeDuration.count();
+        timePosition = chrono::seconds(askedSliderPosition);
+        mpv->seekInFile(timePosition);
     });
 
     connect(togglePlaybackButton, &QAbstractButton::clicked, mpv, &MpvContainer::mpvTogglePlayback);
diff --git a/src/UI/DocumentViews/MpvControls.hh b/src/UI/DocumentViews/MpvControls.hh
index 4be9272d..4f25db87 100644
--- a/src/UI/DocumentViews/MpvControls.hh
+++ b/src/UI/DocumentViews/MpvControls.hh
@@ -4,8 +4,7 @@
 #error "This is a C++ header"
 #endif
 
-#include <chrono>
-namespace chrono = std::chrono;
+#include "../../Lib/Utils.hh"
 
 namespace Vivy
 {
@@ -19,6 +18,7 @@ private:
     MpvContainer *mpv{ nullptr };
     chrono::seconds timeDuration;
     chrono::seconds timePosition;
+    int askedSliderPosition{ 0 };
 
 public:
     explicit MpvControls(MpvContainer *mpv, QWidget *parent) noexcept;
-- 
GitLab