Skip to content

Commit

Permalink
displaywidget: handle out-of-bounds countdowns gracefully
Browse files Browse the repository at this point in the history
This means handling countdowns that do not begin at zero time elapsed.
  • Loading branch information
flopcat committed Jan 7, 2018
1 parent 0dc6bc0 commit 3dbd643
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
13 changes: 9 additions & 4 deletions displaywidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,17 @@ DisplayWidget::DisplayWidget(QWidget *parent, bool widgetMode) : QWidget(parent)
}

void DisplayWidget::startCountdown(int msecDuration)
{
startCountdownPartway(0, msecDuration);
}

void DisplayWidget::startCountdownPartway(int msecPosition, int msecDuration)
{
displayMode = DisplayingCountdown;
QDateTime nowTime = QDateTime::currentDateTime();
endTime = nowTime.addMSecs(msecDuration);
endTime = nowTime.addMSecs(msecDuration - msecPosition);
this->msecDuration = msecDuration;
msecLeft = msecDuration - 1;
msecLeft = msecDuration - msecPosition;
timer.start();

startFader(FadingIn);
Expand Down Expand Up @@ -175,7 +180,7 @@ void DisplayWidget::paintCountdown()
f.setPixelSize(d * 0.2);
p.setFont(f);

int time = std::max((msecLeft + (updateMsec/2)) / 1000, 0ll);
int time = std::min(std::max((msecLeft + (updateMsec/2)) / 1000, 0ll), 3599ll);
QString text = QTime(0, time/60, time%60).toString("m:ss");
QFontMetrics metrics(f);
QSize sz = metrics.size(Qt::TextSingleLine, text);
Expand All @@ -187,7 +192,7 @@ void DisplayWidget::paintCountdown()
QPointF pieCenter(d*0.2, h*0.5);
double radius = d * 0.2;
double radius2 = radius * 0.6;
double factor = std::max(msecLeft/(double)msecDuration, 0.0);
double factor = std::min(std::max(msecLeft/(double)msecDuration, 0.0), 1.0);
QRectF pieRect(pieCenter - QPoint(radius,radius),
pieCenter + QPoint(radius,radius));
p.setPen(Qt::NoPen);
Expand Down
1 change: 1 addition & 0 deletions displaywidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class DisplayWidget : public QWidget
public:
explicit DisplayWidget(QWidget *parent = nullptr, bool widgetMode = false);
void startCountdown(int msecDuration);
void startCountdownPartway(int msecPosition, int msecDuration);
void displayFile(const QString &filename);
void stop();

Expand Down

0 comments on commit 3dbd643

Please sign in to comment.