Browse Source

qml: improve duration display formatting

* display time below the second
* round time to the closest unit
pull/138/head
Pierre Lamot 4 years ago
committed by Rémi Denis-Courmont
parent
commit
d552504dd5
  1. 115
      modules/gui/qt/util/vlctick.cpp
  2. 22
      modules/gui/qt/util/vlctick.hpp
  3. 1
      po/POTFILES.in

115
modules/gui/qt/util/vlctick.cpp

@ -16,6 +16,18 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include "vlctick.hpp"
#include "qt.hpp"
namespace {
int64_t roundNearestMultiple(int64_t number, int64_t multiple)
{
int64_t result = number + multiple/2;
result -= result % multiple;
return result;
}
}
VLCTick::VLCTick()
: m_ticks(VLC_TICK_INVALID)
@ -43,20 +55,100 @@ QString VLCTick::formatHMS() const
if (m_ticks == VLC_TICK_INVALID)
return "--:--";
int64_t t_sec = SEC_FROM_VLC_TICK(m_ticks);
int sec = t_sec % 60;
int min = (t_sec / 60) % 60;
int hour = t_sec / 3600;
int64_t t_ms = MS_FROM_VLC_TICK(m_ticks);
if (t_ms >= 1000)
{
//round to the nearest second
t_ms = roundNearestMultiple(t_ms, 1000);
int64_t t_sec = t_ms / 1000;
int sec = t_sec % 60;
int min = (t_sec / 60) % 60;
int hour = t_sec / 3600;
if (hour == 0)
return QString("%1:%2")
.arg(min, 2, 10, QChar('0'))
.arg(sec, 2, 10, QChar('0'));
else
return QString("%1:%2:%3")
.arg(hour, 2, 10, QChar('0'))
.arg(min, 2, 10, QChar('0'))
.arg(sec, 2, 10, QChar('0'));
}
else
return qtr("%1 ms").arg(MS_FROM_VLC_TICK(m_ticks));
}
QString VLCTick::formatLong() const
{
if (m_ticks == VLC_TICK_INVALID)
return "--:--";
int64_t t_ms = MS_FROM_VLC_TICK(m_ticks);
if (t_ms >= 60*60*1000)
{
//round to the nearest minute
t_ms = roundNearestMultiple(t_ms, 60*1000);
int64_t t_sec = t_ms / 1000;
int min = (t_sec / 60) % 60;
int hour = t_sec / 3600;
return qtr("%1 h %2 min")
.arg(hour)
.arg(min);
}
else if (t_ms >= 1000)
{
//round to the nearest second
t_ms = roundNearestMultiple(t_ms, 1000);
int64_t t_sec = t_ms / 1000;
int sec = t_sec % 60;
int min = (t_sec / 60) % 60;
if (min > 0)
return qtr("%1 min %2 s")
.arg(min)
.arg(sec);
else
return qtr("%1 sec").arg(sec);
}
else
return qtr("%1 ms").arg(t_ms);
}
QString VLCTick::formatShort() const
{
if (m_ticks == VLC_TICK_INVALID)
return "--:--";
int64_t t_ms = MS_FROM_VLC_TICK(m_ticks);
if (t_ms >= 60*60*1000)
{
//round to the nearest minute
t_ms = roundNearestMultiple(t_ms, 60*1000);
int64_t t_sec = t_ms / 1000;
int min = (t_sec / 60) % 60;
int hour = t_sec / 3600;
return qtr("%1h%2")
.arg(hour)
.arg(min, 2, 10, QChar('0'));
}
else if (t_ms >= 1000)
{
//round to the nearest second
t_ms = roundNearestMultiple(t_ms, 1000);
int64_t t_sec = t_ms / 1000;
int sec = t_sec % 60;
int min = (t_sec / 60) % 60;
if (hour == 0)
return QString("%1:%2")
.arg(min, 2, 10, QChar('0'))
.arg(sec, 2, 10, QChar('0'));
}
else
return QString("%1:%2:%3")
.arg(hour, 2, 10, QChar('0'))
.arg(min, 2, 10, QChar('0'))
.arg(sec, 2, 10, QChar('0'));
return qtr("%1ms").arg(t_ms);
}
VLCTick VLCTick::scale(float scalar) const
@ -93,3 +185,8 @@ int VLCTick::toHours() const
int64_t t_sec = SEC_FROM_VLC_TICK(m_ticks);
return (t_sec / 3600);
}
VLCTick VLCTick::fromMS(int64_t ms)
{
return VLCTick(VLC_TICK_FROM_MS(ms));
}

22
modules/gui/qt/util/vlctick.hpp

@ -43,12 +43,34 @@ public:
*/
Q_INVOKABLE QString formatHMS() const;
/**
* @brief formatLong
* @return time in literal form
* 1h 2min
* 5 min
* 10 sec
* 43 ms
*/
Q_INVOKABLE QString formatLong() const;
/**
* @brief formatShort
* @return time in literal form
* 1h02
* 02:42
* 43 ms
*/
Q_INVOKABLE QString formatShort() const;
Q_INVOKABLE VLCTick scale(float) const;
Q_INVOKABLE int toMinutes() const;
Q_INVOKABLE int toSeconds() const;
Q_INVOKABLE int toHours() const;
static VLCTick fromMS(int64_t ms);
private:
vlc_tick_t m_ticks;
};

1
po/POTFILES.in

@ -909,6 +909,7 @@ modules/gui/qt/util/registry.hpp
modules/gui/qt/util/singleton.hpp
modules/gui/qt/util/validators.cpp
modules/gui/qt/util/validators.hpp
modules/gui/qt/util/vlctick.cpp
modules/gui/qt/widgets/native/animators.cpp
modules/gui/qt/widgets/native/animators.hpp
modules/gui/qt/widgets/native/customwidgets.cpp

Loading…
Cancel
Save