From 4f65900e7e4fca23fb33eb9258d89e30d1f854e3 Mon Sep 17 00:00:00 2001 From: NamedKitten Date: Mon, 19 Nov 2018 05:07:22 +0000 Subject: [PATCH] [Backend] Improved createTimestamp readability and also cache duration string. --- src/DirectMpvPlayerBackend.cpp | 21 +++------------------ src/DirectMpvPlayerBackend.h | 5 ++--- src/MpvPlayerBackend.cpp | 21 +++------------------ src/MpvPlayerBackend.h | 3 +-- src/qml/main.qml | 8 ++------ src/utils.cpp | 16 ++++++++++++++++ src/utils.hpp | 2 ++ 7 files changed, 29 insertions(+), 47 deletions(-) diff --git a/src/DirectMpvPlayerBackend.cpp b/src/DirectMpvPlayerBackend.cpp index 8d90a7a..0ea1086 100644 --- a/src/DirectMpvPlayerBackend.cpp +++ b/src/DirectMpvPlayerBackend.cpp @@ -433,22 +433,6 @@ DirectMpvPlayerBackend::playerCommand(const Enums::Commands& cmd, return QVariant("NoOutput"); } -QVariant -DirectMpvPlayerBackend::createTimestamp(const QVariant& seconds) const -{ - int d = seconds.toInt(); - double h = floor(d / 3600); - double m = floor(d % 3600 / 60); - double s = floor(d % 3600 % 60); - - QString hour = h > 0 ? QString::number(h) + ":" : ""; - QString minute = h < 1 ? QString::number(m) + ":" - : (m < 10 ? "0" + QString::number(m) + ":" - : QString::number(m) + ":"); - QString second = s < 10 ? "0" + QString::number(s) : QString::number(s); - return hour + minute + second; -} - void DirectMpvPlayerBackend::handleWindowChanged(QQuickWindow* win) { @@ -496,8 +480,8 @@ DirectMpvPlayerBackend::updateDurationString() { emit durationStringChanged( QString("%1 / %2 (%3x)") - .arg(createTimestamp(getProperty("time-pos")).toString(), - createTimestamp(getProperty("duration")).toString(), + .arg(Utils::createTimestamp(getProperty("time-pos").toInt()), + totalDurationString, getProperty("speed").toString())); } @@ -521,6 +505,7 @@ DirectMpvPlayerBackend::handle_mpv_event(mpv_event* event) } else if (strcmp(prop->name, "duration") == 0) { if (prop->format == MPV_FORMAT_DOUBLE) { double time = *(double*)prop->data; + totalDurationString = Utils::createTimestamp(time); emit durationChanged(time); } } else if (strcmp(prop->name, "mute") == 0 || diff --git a/src/DirectMpvPlayerBackend.h b/src/DirectMpvPlayerBackend.h index b45581b..d351b44 100644 --- a/src/DirectMpvPlayerBackend.h +++ b/src/DirectMpvPlayerBackend.h @@ -42,6 +42,7 @@ class DirectMpvPlayerBackend mpv_opengl_cb_context* mpv_gl; MpvRenderer* renderer; bool onTop = false; + QString totalDurationString; public: static void on_update(void* ctx); @@ -60,9 +61,7 @@ public slots: void setProperty(const QString& name, const QVariant& value); void setOption(const QString& name, const QVariant& value); QVariant getProperty(const QString& name) const; - // Misc - QVariant createTimestamp(const QVariant& seconds) const; - // + void sync(); void swapped(); void cleanup(); diff --git a/src/MpvPlayerBackend.cpp b/src/MpvPlayerBackend.cpp index f88b409..f903017 100644 --- a/src/MpvPlayerBackend.cpp +++ b/src/MpvPlayerBackend.cpp @@ -423,22 +423,6 @@ MpvPlayerBackend::playerCommand(const Enums::Commands& cmd, return QVariant("NoOutput"); } -QVariant -MpvPlayerBackend::createTimestamp(const QVariant& seconds) const -{ - int d = seconds.toInt(); - double h = floor(d / 3600); - double m = floor(d % 3600 / 60); - double s = floor(d % 3600 % 60); - - QString hour = h > 0 ? QString::number(h) + ":" : ""; - QString minute = h < 1 ? QString::number(m) + ":" - : (m < 10 ? "0" + QString::number(m) + ":" - : QString::number(m) + ":"); - QString second = s < 10 ? "0" + QString::number(s) : QString::number(s); - return hour + minute + second; -} - void MpvPlayerBackend::toggleOnTop() { @@ -463,8 +447,8 @@ MpvPlayerBackend::updateDurationString() { emit durationStringChanged( QString("%1 / %2 (%3x)") - .arg(createTimestamp(getProperty("time-pos")).toString(), - createTimestamp(getProperty("duration")).toString(), + .arg(Utils::createTimestamp(getProperty("time-pos").toInt()), + totalDurationString, getProperty("speed").toString())); } @@ -488,6 +472,7 @@ MpvPlayerBackend::handle_mpv_event(mpv_event* event) } else if (strcmp(prop->name, "duration") == 0) { if (prop->format == MPV_FORMAT_DOUBLE) { double time = *(double*)prop->data; + totalDurationString = Utils::createTimestamp(time); emit durationChanged(time); } } else if (strcmp(prop->name, "mute") == 0 || diff --git a/src/MpvPlayerBackend.h b/src/MpvPlayerBackend.h index 527a991..dd12599 100644 --- a/src/MpvPlayerBackend.h +++ b/src/MpvPlayerBackend.h @@ -25,6 +25,7 @@ class MpvPlayerBackend mpv_handle* mpv; mpv_render_context* mpv_gl; bool onTop = false; + QString totalDurationString; friend class MpvRenderer; @@ -46,8 +47,6 @@ public slots: void setProperty(const QString& name, const QVariant& value); void setOption(const QString& name, const QVariant& value); QVariant getProperty(const QString& name) const; - // Misc - QVariant createTimestamp(const QVariant& seconds) const; signals: void onUpdate(); diff --git a/src/qml/main.qml b/src/qml/main.qml index 4c79310..2f58b24 100644 --- a/src/qml/main.qml +++ b/src/qml/main.qml @@ -189,19 +189,15 @@ ApplicationWindow { player.playerCommand(Enums.Commands.SeekAbsolute, skipto) } - function isAnyMenuOpen() { - return menuBar.anythingOpen() - } - function hideControls(force) { - if (!isAnyMenuOpen() || force) { + if (!menuBar.anythingOpen() || force) { controlsShowing = false mouseAreaPlayer.cursorShape = Qt.BlankCursor } } function showControls() { - if (!controlsBar.controls.visible) { + if (!controlsShowing) { controlsShowing = true mouseAreaPlayer.cursorShape = Qt.ArrowCursor } diff --git a/src/utils.cpp b/src/utils.cpp index 98675e2..41ae9d5 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include #ifdef __linux__ @@ -39,6 +41,20 @@ updateAppImage() qApp->exit(); } +QString +createTimestamp(int seconds) +{ + int h = floor(seconds / 3600); + int m = floor(seconds % 3600 / 60); + int s = floor(seconds % 3600 % 60); + + if (h > 0) { + return QString::asprintf("%02d:%02d:%02d", h, m, s); + } else { + return QString::asprintf("%02d:%02d", m, s); + } +} + #ifdef __linux__ void SetDPMS(bool on) diff --git a/src/utils.hpp b/src/utils.hpp index 158d123..e820b00 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -10,5 +10,7 @@ void AlwaysOnTop(WId wid, bool on); void updateAppImage(); +QString +createTimestamp(int seconds); } #endif