-
Notifications
You must be signed in to change notification settings - Fork 2
/
playerbar.cpp
56 lines (48 loc) · 1.68 KB
/
playerbar.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <array>
#include "playerbar.hpp"
PlayerBar::PlayerBar() :
hBoxLayout(this),
timeLeft(1)
{
timeLeft.display("-");
hBoxLayout.addWidget(&player,Qt::AlignLeft);
separator.setFrameShape(QFrame::VLine);
hBoxLayout.addWidget(&separator);
hBoxLayout.addWidget(&used,Qt::AlignRight);
hBoxLayout.addWidget(&timeLeft,Qt::AlignCenter);
hBoxLayout.addWidget(&potentialReserve,Qt::AlignLeft);
timeLeft.setSegmentStyle(QLCDNumber::Outline);
}
void PlayerBar::setTimes(const std::array<qint64,3>& times)
{
const auto timeLeft_=timeDisplay(std::get<0>(times));
timeLeft.setDigitCount(timeLeft_.size());
timeLeft.display(timeLeft_);
potentialReserve.setText(timeDisplay(std::get<1>(times)));
used.setText(timeDisplay(std::get<2>(times)));
nextChange=1000-std::get<2>(times)%1000;
if (std::get<0>(times)>=1000)
nextChange=std::min(nextChange.get(),1+std::get<0>(times)%1000);
if (std::get<1>(times)>=1000)
nextChange=std::min(nextChange.get(),1+std::get<1>(times)%1000);
}
void PlayerBar::setActive(const bool active)
{
timeLeft.setSegmentStyle(active ? QLCDNumber::Flat : QLCDNumber::Outline);
}
QString PlayerBar::timeDisplay(const qint64 milliseconds)
{
const auto seconds=std::max(qint64(0),milliseconds)/1000;
if (seconds<60)
return QString::number(seconds);
QString result=':'+QString::number(seconds%60).rightJustified(2,'0');
const auto minutes=seconds/60;
if (minutes<60)
return QString::number(minutes)+result;
result.prepend(':'+QString::number(minutes%60).rightJustified(2,'0'));
const auto hours=minutes/60;
result.prepend(QString::number(hours%24));
if (hours<24)
return result;
return QString::number(hours/24)+"d "+result;
}