diff --git a/src/Lib/Utils.hh b/src/Lib/Utils.hh index b8dee2bd794a8417373f0369188cc15a1126b179..ac421be7dd692377f98eb204e1485634734c7efe 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 ddaebac6927aad7ff39702eae3cfccb7e6847faa..f2e98133c7e406ccdca747d8378c3c665b77383d 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 1c1b46160bceeae94e94e90f07fc5e2398402533..8296cac69656bfd2b8d14b3eb2bac70722744884 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 52bb30ce0f6f7b029b5c812d498b29f684e26446..a661de0fd9af2c20772f60bfe63193ff10bb879b 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 4be9272d00f335ed259e5b17336480c5aa24f5aa..4f25db87fc728e9c9c06caea0f814069fd4ef912 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;