Wt examples  4.3.0
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_ = cpp14::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 }
CountDownWidget::timerTick
void timerTick()
Process one timer tick.
Definition: CountDownWidget.C:34
CountDownWidget::CountDownWidget
CountDownWidget(int start, int stop, std::chrono::milliseconds msec)
Create a new CountDownWidget.
Definition: CountDownWidget.C:12
CountDownWidget::cancel
void cancel()
Cancel the count down.
Definition: CountDownWidget.C:29
CountDownWidget::stop_
int stop_
Definition: CountDownWidget.h:47
CountDownWidget::done_
Signal done_
Definition: CountDownWidget.h:45
Wt::WText::setText
bool setText(const WString &text)
CountDownWidget.h
CountDownWidget::start_
int start_
Definition: CountDownWidget.h:46
CountDownWidget::timer_
std::unique_ptr< WTimer > timer_
Definition: CountDownWidget.h:51
CountDownWidget::current_
int current_
Definition: CountDownWidget.h:49
Wt::Signal::emit
void emit(A... args) const
Wt::WText

Generated on Thu Mar 26 2020 for the C++ Web Toolkit (Wt) by doxygen 1.8.17