CountDownWidget.C 882 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
  3. *
  4. * See the LICENSE file for terms of use.
  5. */
  6. #include <boost/lexical_cast.hpp>
  7. #include <Wt/WTimer>
  8. #include "CountDownWidget.h"
  9. CountDownWidget::CountDownWidget(int start, int stop, unsigned msec,
  10. WContainerWidget *parent)
  11. : WText(parent),
  12. done_(this),
  13. start_(start),
  14. stop_(stop)
  15. {
  16. stop_ = std::min(start_ - 1, stop_); // stop must be smaller than start
  17. current_ = start_;
  18. timer_ = new WTimer(this);
  19. timer_->setInterval(msec);
  20. timer_->timeout().connect(this, &CountDownWidget::timerTick);
  21. timer_->start();
  22. setText(boost::lexical_cast<std::string>(current_));
  23. }
  24. void CountDownWidget::cancel()
  25. {
  26. timer_->stop();
  27. }
  28. void CountDownWidget::timerTick()
  29. {
  30. setText(boost::lexical_cast<std::string>(--current_));
  31. if (current_ <= stop_) {
  32. timer_->stop();
  33. done_.emit();
  34. }
  35. }