Implement timeString method to create HH:mm:ss strings from qint64

This method is now used in all places where a HH:mm:ss string is
required.
This commit is contained in:
Bart De Vries 2021-05-02 09:38:48 +02:00
parent 173b86b85d
commit 34c5e5309c
4 changed files with 22 additions and 7 deletions

View File

@ -10,6 +10,7 @@
#include <QAudio>
#include <QEventLoop>
#include <QTimer>
#include <QtMath>
#include <algorithm>
#include "datamanager.h"
@ -70,6 +71,18 @@ AudioManager::~AudioManager()
d->mPowerInterface.setPreventSleep(false);
}
QString AudioManager::timeString(qint64 timeInMicroSeconds)
{
QString outputString;
outputString += qFloor(timeInMicroSeconds / 3600000) < 10 ? QStringLiteral("0") : QStringLiteral("");
outputString += QString::number(qFloor(timeInMicroSeconds / 3600000)) + QStringLiteral(":");
outputString += qFloor(timeInMicroSeconds / 60000) % 60 < 10 ? QStringLiteral("0") : QStringLiteral("");
outputString += QString::number(qFloor(timeInMicroSeconds / 60000) % 60) + QStringLiteral(":");
outputString += qFloor(timeInMicroSeconds / 1000) % 60 < 10 ? QStringLiteral("0") : QStringLiteral("");
outputString += QString::number(qFloor(timeInMicroSeconds / 1000) % 60);
return outputString;
}
Entry *AudioManager::entry() const
{
return d->m_entry;

View File

@ -48,6 +48,8 @@ public:
~AudioManager() override;
Q_INVOKABLE static QString timeString(qint64 timeInMicroSeconds);
[[nodiscard]] Entry *entry() const;
[[nodiscard]] bool muted() const;

View File

@ -91,7 +91,7 @@ Kirigami.SwipeListItem {
Component {
id: subtitle
Controls.Label {
text: (Math.floor(entry.enclosure.duration/3600) < 10 ? "0" : "") + Math.floor(entry.enclosure.duration/3600) + ":" + (Math.floor(entry.enclosure.duration/60) % 60 < 10 ? "0" : "") + Math.floor(entry.enclosure.duration/60) % 60 + ":" + (Math.floor(entry.enclosure.duration) % 60 < 10 ? "0" : "") + Math.floor(entry.enclosure.duration) % 60
text: audio.timeString(entry.enclosure.duration * 1000)
Layout.fillWidth: true
elide: Text.ElideRight
font: Kirigami.Theme.smallFont
@ -113,7 +113,7 @@ Kirigami.SwipeListItem {
id: playProgress
RowLayout {
Controls.Label {
text: (Math.floor(entry.enclosure.playPosition/3600000) < 10 ? "0" : "") + Math.floor(entry.enclosure.playPosition/3600000) + ":" + (Math.floor(entry.enclosure.playPosition/60000) % 60 < 10 ? "0" : "") + Math.floor(entry.enclosure.playPosition/60000) % 60 + ":" + (Math.floor(entry.enclosure.playPosition/1000) % 60 < 10 ? "0" : "") + Math.floor(entry.enclosure.playPosition/1000) % 60
text: audio.timeString(entry.enclosure.playPosition)
elide: Text.ElideRight
font: Kirigami.Theme.smallFont
opacity: 0.7
@ -121,11 +121,11 @@ Kirigami.SwipeListItem {
Controls.ProgressBar {
from: 0
to: entry.enclosure.duration
value: entry.enclosure.playPosition/1000
value: entry.enclosure.playPosition / 1000
Layout.fillWidth: true
}
Controls.Label {
text: (Math.floor(entry.enclosure.duration/3600) < 10 ? "0" : "") + Math.floor(entry.enclosure.duration/3600) + ":" + (Math.floor(entry.enclosure.duration/60) % 60 < 10 ? "0" : "") + Math.floor(entry.enclosure.duration/60) % 60 + ":" + (Math.floor(entry.enclosure.duration) % 60 < 10 ? "0" : "") + Math.floor(entry.enclosure.duration) % 60
text: audio.timeString(entry.enclosure.duration * 1000)
elide: Text.ElideRight
font: Kirigami.Theme.smallFont
opacity: 0.7

View File

@ -162,7 +162,7 @@ Kirigami.Page {
Layout.fillWidth: true
Controls.Label {
//anchor.left: parent.left
text: (Math.floor(audio.position/3600000) < 10 ? "0" : "") + Math.floor(audio.position/3600000) + ":" + (Math.floor(audio.position/60000) % 60 < 10 ? "0" : "") + Math.floor(audio.position/60000) % 60 + ":" + (Math.floor(audio.position/1000) % 60 < 10 ? "0" : "") + Math.floor(audio.position/1000) % 60
text: audio.timeString(audio.position)
}
Item {
Layout.fillWidth: true
@ -175,8 +175,8 @@ Kirigami.Page {
anchors.right: parent.right
anchors.verticalCenter: parent.verticalCenter
text: (SettingsManager.toggleRemainingTime) ?
((Math.floor((audio.duration-audio.position)/3600000) < 10 ? "-0" : "-") + Math.floor((audio.duration-audio.position)/3600000) + ":" + (Math.floor((audio.duration-audio.position)/60000) % 60 < 10 ? "0" : "") + Math.floor((audio.duration-audio.position)/60000) % 60 + ":" + (Math.floor((audio.duration-audio.position)/1000) % 60 < 10 ? "0" : "") + Math.floor((audio.duration-audio.position)/1000) % 60)
: ((Math.floor(audio.duration/3600000) < 10 ? "0" : "") + Math.floor(audio.duration/3600000) + ":" + (Math.floor(audio.duration/60000) % 60 < 10 ? "0" : "") + Math.floor(audio.duration/60000) % 60 + ":" + (Math.floor(audio.duration/1000) % 60 < 10 ? "0" : "") + Math.floor(audio.duration/1000) % 60)
"-" + audio.timeString(audio.duration-audio.position)
: audio.timeString(audio.duration)
}
MouseArea {