Wt examples  4.10.4
CountDownWidget.C
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 Emweb bv, Herent, Belgium.
3  *
4  * See the LICENSE file for terms of use.
5  */
6 
7 #include <Wt/WTimer.h>
8 #include "CountDownWidget.h"
9 #include <string>
10 #include <algorithm>
11 
12 CountDownWidget::CountDownWidget(int start, int stop, std::chrono::milliseconds msec)
13  : WText(),
14  done_(),
15  start_(start),
16  stop_(stop)
17 {
18  stop_ = std::min(start_ - 1, stop_); // stop must be smaller than start
19  current_ = start_;
20 
21  timer_ = std::make_unique<WTimer>();
22  timer_->setInterval(msec);
23  timer_->timeout().connect(this, &CountDownWidget::timerTick);
24  timer_->start();
25 
26  setText(std::to_string(current_));
27 }
28 
30 {
31  timer_->stop();
32 }
33 
35 {
36  setText(std::to_string(--current_));
37 
38  if (current_ <= stop_) {
39  timer_->stop();
40  done_.emit();
41  }
42 }
void cancel()
Cancel the count down.
std::unique_ptr< WTimer > timer_
void timerTick()
Process one timer tick.
CountDownWidget(int start, int stop, std::chrono::milliseconds msec)
Create a new CountDownWidget.
void emit(A... args) const
bool setText(const WString &text)